forked from camptocamp/helm-sops
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelm_wrapper.go
203 lines (171 loc) · 5.08 KB
/
helm_wrapper.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
package main
import (
"crypto/sha256"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"regexp"
"sync"
"syscall"
"go.mozilla.org/sops/v3/decrypt"
)
type HelmWrapper struct {
Errors []error
errMutex sync.Mutex
ExitCode int
helmBinPath string
pipeWriterWaitGroup sync.WaitGroup
valuesArgRegexp *regexp.Regexp
temporaryDirectory string
}
func NewHelmWrapper() (*HelmWrapper, error) {
c := HelmWrapper{}
c.Errors = []error{}
c.pipeWriterWaitGroup = sync.WaitGroup{}
c.valuesArgRegexp = regexp.MustCompile("^(-f|--values)(?:=(.+))?$")
// Determine the name of the helm binary by examining our binary name
helmBinName := "helm"
ourBinName := path.Base(os.Args[0])
if ourBinName == "helm" || ourBinName == "helm2" || ourBinName == "helm3" {
helmBinName = fmt.Sprintf("_%s", ourBinName)
}
var err error
c.helmBinPath, err = exec.LookPath(helmBinName)
if err != nil {
return nil, fmt.Errorf("failed to find Helm binary '%s': %s", helmBinName, err)
}
return &c, nil
}
func (c *HelmWrapper) errorf(msg string, a ...interface{}) error {
e := fmt.Errorf(msg, a...)
c.errMutex.Lock()
c.Errors = append(c.Errors, e)
c.errMutex.Unlock()
return e
}
func (c *HelmWrapper) pipeWriter(outPipeName string, data []byte) {
c.pipeWriterWaitGroup.Add(1)
defer c.pipeWriterWaitGroup.Done()
cleartextSecretFile, err := os.OpenFile(outPipeName, os.O_WRONLY, 0)
if err != nil {
c.errorf("failed to open cleartext secret pipe '%s' in pipe writer: %s", outPipeName, err)
return
}
defer func() {
err := cleartextSecretFile.Close()
if err != nil {
c.errorf("failed to close cleartext secret pipe '%s' in pipe writer: %s", outPipeName, err)
}
}()
_, err = cleartextSecretFile.Write(data)
if err != nil {
c.errorf("failed to write cleartext secret to pipe '%s': %s", outPipeName, err)
}
}
func (c *HelmWrapper) valuesArg(args []string) (string, string, error) {
valuesArgRegexpMatches := c.valuesArgRegexp.FindStringSubmatch(args[0])
if valuesArgRegexpMatches == nil {
return "", "", nil
}
var filename string
if len(valuesArgRegexpMatches[2]) > 0 {
// current arg is in the format --values=filename
filename = valuesArgRegexpMatches[2]
} else if len(args) > 1 {
// arg is in the format "-f filename"
filename = args[1]
} else {
return "", "", c.errorf("missing filename after -f or --values")
}
cleartextSecretFilename := fmt.Sprintf("%s/%x", c.temporaryDirectory, sha256.Sum256([]byte(filename)))
return filename, cleartextSecretFilename, nil
}
func (c *HelmWrapper) replaceValueFileArg(args []string, cleartextSecretFilename string) {
valuesArgRegexpMatches := c.valuesArgRegexp.FindStringSubmatch(args[0])
// replace the filename with our pipe
if len(valuesArgRegexpMatches[2]) > 0 {
args[0] = fmt.Sprintf("%s=%s", valuesArgRegexpMatches[1], cleartextSecretFilename)
} else {
args[1] = cleartextSecretFilename
}
}
func (c *HelmWrapper) mkTmpDir() (func(), error) {
var err error
c.temporaryDirectory, err = ioutil.TempDir("", fmt.Sprintf("%s.", path.Base(os.Args[0])))
if err != nil {
return nil, c.errorf("failed to create temporary directory: %s", err)
}
return func() {
err := os.RemoveAll(c.temporaryDirectory)
if err != nil {
c.errorf("failed to remove temporary directory '%s': %s", c.temporaryDirectory, err)
}
}, nil
}
func (c *HelmWrapper) mkPipe(cleartextSecretFilename string) (func(), error) {
err := syscall.Mkfifo(cleartextSecretFilename, 0600)
if err != nil {
return nil, c.errorf("failed to create cleartext secret pipe '%s': %s", cleartextSecretFilename, err)
}
return func() {
err := os.Remove(cleartextSecretFilename)
if err != nil {
c.errorf("failed to remove cleartext secret pipe '%s': %s", cleartextSecretFilename, err)
}
}, nil
}
func (c *HelmWrapper) RunHelm() {
var err error
// Setup temporary directory and defer cleanup
cleanFn, err := c.mkTmpDir()
if err != nil {
return
}
defer cleanFn()
// Loop through arguments looking for --values or -f.
// If we find a values argument, check if file has a sops section indicating it is encrypted.
// Setup a named pipe and write the decrypted data into that for helm.
for i := range os.Args {
args := os.Args[i:]
filename, cleartextSecretFilename, err := c.valuesArg(args)
if err != nil {
return
}
if filename == "" {
continue
}
encrypted, err := DetectSopsYaml(filename)
if err != nil {
c.errorf("error checking if file is encrypted: %s", err)
return
}
if !encrypted {
continue
}
c.replaceValueFileArg(args, cleartextSecretFilename)
cleartextSecrets, err := decrypt.File(filename, "yaml")
if err != nil {
c.errorf("failed to decrypt secret file '%s': %s", filename, err)
return
}
cleanFn, err := c.mkPipe(cleartextSecretFilename)
if err != nil {
return
}
defer cleanFn()
go c.pipeWriter(cleartextSecretFilename, cleartextSecrets)
}
defer c.pipeWriterWaitGroup.Wait()
cmd := exec.Command(c.helmBinPath, os.Args[1:]...)
cmd.Env = os.Environ()
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
c.ExitCode = cmd.ProcessState.ExitCode()
c.errorf("failed to run Helm: %s", err)
}
}