-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgoseal.go
250 lines (204 loc) · 5.65 KB
/
goseal.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
package main
import (
"bytes"
"errors"
"fmt"
"log"
"os"
"os/exec"
"regexp"
"strings"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
)
func main() {
app := &cli.App{
Name: "goseal",
Usage: "Used to automatically generate kubernetes secret files (and optionally seal them)",
Version: "v0.3.0",
Commands: []*cli.Command{
{
Name: "yaml",
HelpName: "yaml",
Description: "creates a sealed secret from yaml input key-value pairs",
Usage: "Create a secret file with key-value pairs as in the yaml file",
Aliases: []string{"y"},
Flags: getStandardFlags(),
Action: Yaml,
},
{
Name: "file",
HelpName: "file",
Description: "creates a (sealed) kubernetes secret with a file as secret value",
Usage: "Create a secret with a file as secret value.",
Action: File,
Flags: append(getStandardFlags(), &cli.StringFlag{
Name: "key",
Usage: "the secret key, under which the file can be accessed",
Aliases: []string{"k"},
Required: true,
}),
},
},
}
app.EnableBashCompletion = true
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func getStandardFlags() []cli.Flag {
return []cli.Flag{
cli.BashCompletionFlag,
cli.HelpFlag,
&cli.StringFlag{
Name: "namespace",
Usage: "the namespace of the secret",
Required: true,
Aliases: []string{"n"},
},
&cli.StringFlag{
Name: "file",
Usage: "the input file in yaml format",
Required: true,
Aliases: []string{"f"},
},
&cli.StringFlag{
Name: "secret-name",
Usage: "the secret name",
Required: true,
Aliases: []string{"s"},
},
&cli.StringFlag{
Name: "cert",
Usage: "if set, will run kubeseal with given cert",
Aliases: []string{"c"},
},
}
}
// ErrEmptyFile is returned if the provided file has no content.
var ErrEmptyFile = errors.New("file content is empty")
// Yaml is a cli command
func Yaml(c *cli.Context) error {
filePath := c.String("file")
namespace := c.String("namespace")
secretName := c.String("secret-name")
certPath := c.String("cert")
file, err := os.ReadFile(filePath)
if err != nil {
return err
}
if len(file) == 0 {
return ErrEmptyFile
}
var secrets map[string]string
if err := yaml.Unmarshal(file, &secrets); err != nil {
return err
}
if certPath != "" {
return sealSecret(secrets, secretName, namespace, certPath)
}
return createSecret(secrets, secretName, namespace)
}
// File is a cli command
func File(c *cli.Context) error {
filePath := c.String("file")
secretKey := c.String("key")
namespace := c.String("namespace")
secretName := c.String("secret-name")
certPath := c.String("cert")
file, err := os.ReadFile(filePath)
if err != nil {
return err
}
if len(file) == 0 {
return ErrEmptyFile
}
secrets := map[string]string{secretKey: string(file)}
if certPath != "" {
return sealSecret(secrets, secretName, namespace, certPath)
}
return createSecret(secrets, secretName, namespace)
}
// regexCreationTimestamp is a regex used to remove the creationTimestamp from the output of kubectl create secret.
var regexCreationTimestamp = regexp.MustCompile(`\s*creationTimestamp: null`)
// runs the kubectl create secret command and prints the output to stdout.
func createSecret(secrets map[string]string, secretName, namespace string) error {
kubectlCreateSecret := getCreateSecretFileCmd(secrets, secretName, namespace)
var stdout bytes.Buffer
kubectlCreateSecret.Stdout = &stdout
if err := runCommand(kubectlCreateSecret); err != nil {
return err
}
outputWithCreationTimestamp := stdout.String()
output := regexCreationTimestamp.ReplaceAllString(outputWithCreationTimestamp, "")
fmt.Println(output)
return nil
}
// runs the kubectl create secret command, pipes the output to the kubeseal command and prints the output to stdout.
func sealSecret(secrets map[string]string, secretName, namespace, certPath string) error {
kubectlCreateSecret := getCreateSecretFileCmd(secrets, secretName, namespace)
kubeseal := exec.Command("kubeseal", "--format", "yaml", "--cert", certPath)
var (
err error
stdout, stderr bytes.Buffer
)
kubeseal.Stdout = &stdout
kubeseal.Stderr = &stderr
// Get stdout of first command and attach it to stdin of second command.
kubeseal.Stdin, err = kubectlCreateSecret.StdoutPipe()
if err != nil {
return err
}
if err := kubeseal.Start(); err != nil {
return err
}
if err = runCommand(kubectlCreateSecret); err != nil {
return err
}
if err = kubeseal.Wait(); err != nil {
return errors.New(getErrText(err, kubeseal.Args, stderr.String()))
}
outputWithCreationTimestamp := stdout.String()
output := regexCreationTimestamp.ReplaceAllString(outputWithCreationTimestamp, "")
fmt.Println(output)
return nil
}
// retrieve a printable error text from cmd errors
func getErrText(err error, cmdArgs []string, stdErr string) string {
text := fmt.Sprintf(
"command '%s' failed: %s",
strings.Join(cmdArgs, " "),
err.Error(),
)
errText := strings.TrimSpace(stdErr)
if len(errText) > 0 {
text += "\n" + errText
}
return text
}
func runCommand(cmd *exec.Cmd) error {
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return errors.New(getErrText(err, cmd.Args, stderr.String()))
}
return nil
}
// creates
func getCreateSecretFileCmd(secrets map[string]string, secretName, namespace string) *exec.Cmd {
args := []string{
"create",
"secret",
"generic",
secretName,
"-n",
namespace,
"--dry-run",
"-o",
"yaml",
}
for k, v := range secrets {
args = append(args, fmt.Sprintf("--from-literal=%s=%s", k, v))
}
return exec.Command("kubectl", args...)
}