-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecrets.go
137 lines (111 loc) · 4.11 KB
/
secrets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package secrets
import (
"context"
"io/fs"
"log/slog"
"os"
"path/filepath"
"strings"
"github.com/x-ethr/levels"
)
// Secret represents the kubernetes secret. On a pod's filesystem, [Secret] value represents the directory where the volume was mounted.
type Secret string
// Key represents a kubernetes secret's key. On a pod's filesystem, [Key] represents a file's name.
type Key string
// Value represents a kubernetes secret's value. On a pod's filesystem, [Value] represents the [Key] file's contents.
type Value string
func (v Value) Bytes() []byte {
return []byte(v)
}
// Secrets represents a map[string]map[string][]byte mapping of [Secret] -> [Key] -> [Value].
type Secrets map[Secret]map[Key]Value
// Walk recursively traverses the specified directory and its subdirectories.
// It collects file paths, directory names, and file contents to build a Secrets map; ignores hidden files and directories that start with a dot.
// - Returns an error if any occurred during the traversal.
func (s Secrets) Walk(ctx context.Context, directory string) error {
e := filepath.WalkDir(directory, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !(strings.HasPrefix(d.Name(), ".")) {
slog.Log(ctx, levels.Trace, "Secrets WalK", slog.String("path", path), slog.String("name", d.Name()), slog.Bool("directory", d.IsDir()))
if d.IsDir() {
secret := Secret(d.Name())
s[secret] = make(map[Key]Value)
return nil
}
key := Key(d.Name())
secret := Secret(filepath.Base(filepath.Dir(path)))
if strings.HasPrefix(string(secret), ".") {
// --> avoid ..data and .symbolic-link directories
secret = Secret(filepath.Base(filepath.Dir(filepath.Dir(path))))
}
value, exception := os.ReadFile(path)
if exception != nil {
return exception
}
s[secret][key] = Value(value)
}
return nil
})
if e != nil {
slog.WarnContext(ctx, "Error Walking Directory", slog.String("error", e.Error()))
return e
}
return nil
}
// FS walks the specified file system and populates the Secrets map.
// It ignores hidden files and directories that start with a dot.
// - Returns an error if any occurred during the file system walk.
func (s Secrets) FS(ctx context.Context, filesystem fs.FS) error {
e := fs.WalkDir(filesystem, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !(strings.HasPrefix(d.Name(), ".")) {
slog.Log(ctx, levels.Trace, "Secrets FS Walk", slog.String("path", path), slog.String("name", d.Name()), slog.Bool("directory", d.IsDir()))
if d.IsDir() {
secret := Secret(d.Name())
s[secret] = make(map[Key]Value)
return nil
}
key := Key(d.Name())
secret := Secret(filepath.Base(filepath.Dir(path)))
if strings.HasPrefix(string(secret), ".") {
// --> avoid ..data and .symbolic-link directories
secret = Secret(filepath.Base(filepath.Dir(filepath.Dir(path))))
}
value, exception := os.ReadFile(path)
if exception != nil {
return exception
}
s[secret][key] = Value(value)
}
return nil
})
if e != nil {
slog.WarnContext(ctx, "Error Walking Filesystem", slog.String("error", e.Error()))
return e
}
return nil
}
// New returns a new instance of the Secrets type.
// It initializes a Secrets map with an empty map for each secret.
func New() Secrets {
return make(Secrets)
}
// Walk takes a context and a directory path, and returns a Secrets map and an error.
// It creates a new instance of the Secrets type, then calls the Walk method of that instance with the given context and directory.
// It returns the updated instance and any error that occurred during the Walk operation.
func Walk(ctx context.Context, directory string) (Secrets, error) {
instance := New()
e := instance.Walk(ctx, directory)
return instance, e
}
// FS creates a new instance of Secrets and populates it by walking the provided file system using the given context.
// It returns the populated Secrets and any error encountered during the file system walk.
func FS(ctx context.Context, filesystem fs.FS) (Secrets, error) {
instance := New()
e := instance.FS(ctx, filesystem)
return instance, e
}