-
Notifications
You must be signed in to change notification settings - Fork 3
/
attrs.go
270 lines (241 loc) · 6.5 KB
/
attrs.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package cuepp
import (
"encoding/json"
"fmt"
"io/fs"
"log"
"path/filepath"
"strings"
"cuelang.org/go/cue"
"github.com/getsops/sops/v3/cmd/sops/formats"
"github.com/getsops/sops/v3/decrypt"
"github.com/joho/godotenv"
"gopkg.in/yaml.v3"
)
var cuegenAttrs = []string{"read", "readfile", "readmap"}
func (p CuePP) processAttribute(value, node cue.Value, attr cue.Attribute, fsys fs.FS, dir string) (cue.Value, error) {
log.Printf("process attribute: %v @%v(%v)", node.Path().String(), attr.Name(), attr.Contents())
var err error
switch attr.Name() {
case "readfile":
value, err = p.attrReadFile(value, node.Path().String(), attr, fsys, dir)
if err != nil {
err = fmt.Errorf("attrReadFile: %v", err)
}
case "readmap":
value, err = p.attrReadMap(value, node.Path().String(), attr, fsys, dir)
if err != nil {
err = fmt.Errorf("attrReadMap: %v", err)
}
case "read":
value, err = p.attrRead(value, node.Path().String(), attr, fsys, dir)
if err != nil {
err = fmt.Errorf("attrRead: %v", err)
}
default:
err = fmt.Errorf("unknown attribute %q", attr.Name())
}
if err != nil {
return cue.Value{}, err
}
return value, nil
}
func (p CuePP) attrReadFile(value cue.Value, path string, attr cue.Attribute, fsys fs.FS, dir string) (cue.Value, error) {
alldata := ""
asBytesFlag := false
for i := 0; i < attr.NumArgs(); i++ {
file, flag := attr.Arg(i)
data, err := p.readFile(fsys, filepath.Join(dir, file))
if err != nil {
return value, fmt.Errorf("readFile: %v", err)
}
switch flag {
case "nl":
alldata = alldata + strings.TrimRight(data, "\n") + "\n"
case "trim":
alldata = alldata + strings.TrimSpace(data)
case "bytes":
asBytesFlag = true
fallthrough
default:
alldata = alldata + data
}
}
if p.asBytes(path, asBytesFlag) {
value = value.FillPath(cue.ParsePath(path), []byte(alldata))
} else {
value = value.FillPath(cue.ParsePath(path), alldata)
}
return value, nil
}
func (p CuePP) attrReadMap(value cue.Value, cuePath string, attr cue.Attribute, fsys fs.FS, dir string) (cue.Value, error) {
for i := 0; i < attr.NumArgs(); i++ {
item, suffix := attr.Arg(i)
data, err := p.readPath(fsys, filepath.Join(dir, item))
if err != nil {
return value, fmt.Errorf("attrRead: %v", err)
}
for k, v := range data {
pathItems := cue.ParsePath(fmt.Sprintf("%v.%q", cuePath, k))
switch stringValue := v.(type) {
case string:
if p.asBytes(cuePath, suffix == "bytes") {
value = value.FillPath(pathItems, []byte(stringValue))
} else {
value = value.FillPath(pathItems, stringValue)
}
default:
return value, fmt.Errorf("value of type %T not allowed with readmap", v)
}
}
}
return value, nil
}
func (p CuePP) attrRead(value cue.Value, cuePath string, attr cue.Attribute, fsys fs.FS, dir string) (cue.Value, error) {
for i := 0; i < attr.NumArgs(); i++ {
item, _ := attr.Arg(i)
data, err := p.readPath(fsys, filepath.Join(dir, item))
if err != nil {
return value, fmt.Errorf("attrRead: %v", err)
}
value = value.FillPath(cue.ParsePath(cuePath), data)
}
return value, nil
}
func (p CuePP) readPath(fsys fs.FS, path string) (map[string]any, error) {
fileinfo, err := fs.Stat(fsys, path)
if err != nil {
return nil, fmt.Errorf("readPath: %v", err)
}
if fileinfo.IsDir() {
entries, err := fs.ReadDir(fsys, path)
if err != nil {
return nil, fmt.Errorf("readPath: %v", err)
}
data := map[string]any{}
for _, entry := range entries {
if !entry.Type().IsRegular() {
continue
}
stringData, err := p.readFile(fsys, filepath.Join(path, entry.Name()))
if err != nil {
return nil, fmt.Errorf("readPath: %v", err)
}
data[entry.Name()] = stringData
}
return data, nil
}
if fileinfo.Mode().IsRegular() {
return p.readStructFile(fsys, path)
}
return nil, fmt.Errorf("readPath: can't handle %q", path)
}
func (p CuePP) readStructFile(fsys fs.FS, file string) (map[string]any, error) {
contents, err := p.readFile(fsys, file)
if err != nil {
return nil, fmt.Errorf("%q: %v", file, err)
}
fileExt := filepath.Ext(file)
data := map[string]any{}
switch fileExt {
case ".env":
env, err := godotenv.Unmarshal(contents)
if err != nil {
return data, fmt.Errorf("%q: %v", file, err)
}
for k, v := range env {
data[k] = v
}
case ".yml":
fallthrough
case ".yaml":
err = yaml.Unmarshal([]byte(contents), &data)
if err != nil {
return data, fmt.Errorf("%q: %v", file, err)
}
case ".json":
err = json.Unmarshal([]byte(contents), &data)
if err != nil {
return data, fmt.Errorf("%q: %v", file, err)
}
default:
return map[string]any{}, fmt.Errorf("%q: file type not supported", file)
}
return data, nil
}
var (
probeStringsJsonOrYamlOrBinary = []string{
"sops", "version", "unencrypted_suffix", "lastmodified", "mac",
}
probeStringsEnv = []string{
"sops_version", "sops_unencrypted_suffix", "sops_lastmodified", "sops_mac",
}
)
func (p CuePP) readFile(fsys fs.FS, file string) (string, error) {
if p.Debug {
log.Printf(" readFile: %v", file)
}
data, err := fs.ReadFile(fsys, file)
if err != nil {
return "", fmt.Errorf("ReadFile: %q: %v", file, err)
}
var format formats.Format
var probes []string
stringData := string(data)
// guess file format from extension
switch filepath.Ext(file) {
case ".env":
format = formats.Dotenv
probes = probeStringsEnv
case ".yml":
fallthrough
case ".yaml":
format = formats.Yaml
probes = probeStringsJsonOrYamlOrBinary
case ".json":
format = formats.Json
probes = probeStringsJsonOrYamlOrBinary
default:
format = formats.Binary
probes = probeStringsJsonOrYamlOrBinary
}
// detect whether contents looks like sops encrypted
allMatched := true
for _, probe := range probes {
allMatched = allMatched && strings.Contains(stringData, probe)
}
if allMatched {
if p.Debug {
log.Printf(" decrypt with sops")
}
plaintext, err := decrypt.DataWithFormat(data, format)
if err == nil {
return string(plaintext), nil
}
log.Printf("WARN: %q: looks like sops encrypted, but decrypt failed: %v", file, err)
}
return stringData, nil
}
func (p CuePP) asBytes(cuePath string, asBytesFlag bool) bool {
if p.SecretsPath == "" {
return false
}
if asBytesFlag {
return true
}
secretPathItems := strings.Split(p.SecretsPath, ".")
pathItems := strings.Split(cuePath, ".")
asBytes := true
if len(secretPathItems) > len(pathItems) {
asBytes = false
}
if asBytes {
for n, key := range secretPathItems {
if key != "*" && key != pathItems[n] {
asBytes = false
break
}
}
}
return asBytes
}