forked from viaduct-ai/kustomize-sops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_plugin.go
More file actions
71 lines (55 loc) · 1.4 KB
/
Copy pathexec_plugin.go
File metadata and controls
71 lines (55 loc) · 1.4 KB
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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"go.mozilla.org/sops/v3/cmd/sops/formats"
"go.mozilla.org/sops/v3/decrypt"
"sigs.k8s.io/yaml"
)
type ksops struct {
Files []string `json:"files,omitempty" yaml:"files,omitempty"`
}
// main executes KOSPS as an exec plugin
func main() {
if len(os.Args) != 2 {
fmt.Println("received too few args:", os.Args)
fmt.Println("always invoke this via kustomize plugins")
os.Exit(1)
}
// ignore the first file name argument
// load the second argument, the file path
content, err := ioutil.ReadFile(os.Args[1])
if err != nil {
fmt.Println("unable to read in manifest", os.Args[1])
os.Exit(1)
}
var manifest ksops
err = yaml.Unmarshal(content, &manifest)
if err != nil {
fmt.Printf("error unmarshalling manifest content: %q \n%s\n", err, content)
os.Exit(1)
}
if manifest.Files == nil {
fmt.Println("missing the required 'files' key in the ksops manifests")
os.Exit(1)
}
var output bytes.Buffer
for _, file := range manifest.Files {
b, err := ioutil.ReadFile(file)
if err != nil {
fmt.Printf("error reading %q: %q\n", file, err.Error())
os.Exit(1)
}
format := formats.FormatForPath(file)
data, err := decrypt.DataWithFormat(b, format)
if err != nil {
fmt.Printf("trouble decrypting file %s", err.Error())
os.Exit(1)
}
output.Write(data)
output.WriteString("\n---\n")
}
fmt.Print(output.String())
}