-
Notifications
You must be signed in to change notification settings - Fork 0
/
kube_env_sops.go
181 lines (145 loc) · 4.64 KB
/
kube_env_sops.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
package main
import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"log"
)
const yml =
`apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
- name: environment-secrets
envs:
- .env.local
generatorOptions:
disableNameSuffixHash: true
`
// Returns the current working directory
func getCwd() string {
cwd, err := os.Getwd()
if err != nil {
cwd="./"
}
return cwd
}
// Returns whether or not the path exists
func path_exists(file_path string) bool {
_, err := os.Stat(file_path)
if err != nil {
return false
}
return true
}
// Creates a new file with data
func create_file_with_data(file_path string, data string) {
f, err := os.Create(file_path)
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err2 := f.WriteString(data)
if err2 != nil {
log.Fatal(err2)
}
}
// Removes a file by file path
func remove_file(file_path string) {
os.Remove(file_path)
// TODO: Print on debug
// fmt.Println("Removed file: " + file_path)
// err := os.Remove(file_path)
// if err != nil {
// fmt.Println(err)
// }
}
// Checks if a command exists
func command_exists(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
// Execute a command
func exec_command(cwd_path string, name string, args ...string) (string, error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd := exec.Command(name, args...)
cmd.Dir = cwd_path
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Env = os.Environ()
err := cmd.Run()
if err != nil {
return (fmt.Sprint(err) + ": " + stderr.String()), err
}
return stdout.String(), err
}
func main() {
// Ensure that the required commands are installed
required_commands := []string{"kubectl", "sops"}
for _, command := range required_commands {
if(!command_exists(command)) {
log.Fatal(command + " is required. Please install.")
}
}
// Set the path that files should be generated in
cwd_path := flag.String("cwd_path", getCwd(), "The path to generate the encrypted secrets.")
dot_env_enc_file_name := flag.String("dot_env_enc_file_name", ".env-enc.yml", "An optional name for the encrypted yml file.")
save_secret := flag.Bool("save", true, "If set to false, the encrypted secret will be output to stdout.")
force_save := flag.Bool("force", false, "If set to true, the encrypted secret will be overriden.")
// Parse command line into the defined flags
flag.Parse()
kustomization_file_path := filepath.Join(*cwd_path, "kustomization.yaml")
kustomization_file_path_exists := path_exists(kustomization_file_path)
dot_env_local_file_path := filepath.Join(*cwd_path, ".env.local")
dot_env_dec_file_path := filepath.Join(*cwd_path, ".env-dec.yml")
dot_env_enc_file_path := filepath.Join(*cwd_path, *dot_env_enc_file_name)
// If a kustomization file does not exist then we will delete the file
if(!kustomization_file_path_exists) {
// Delay the removal of the file after main is complete
defer remove_file(kustomization_file_path)
}
// Delay the removal of the file after main is complete
defer remove_file(dot_env_dec_file_path)
// TODO: Print if debug mode
// Print the input parameters
// fmt.Println("Debug Info: ")
// fmt.Println("cwd_path: ", *cwd_path)
// If the .env.local does not exist
if(!path_exists(dot_env_local_file_path) && !kustomization_file_path_exists) {
log.Fatal("A .env.local does not exist. Please create a .env.local file.\n\n")
}
// If the user would like to save an encrypted secret that exists
// without specifying the force save flag then send an alert
if (*save_secret && path_exists(dot_env_enc_file_path) && !*force_save) {
log.Fatal(*dot_env_enc_file_name + " exist. Set the -force flag if you would like to overwrite.\n\n")
}
// If a kustomization file does not exist then create one
if(!kustomization_file_path_exists) {
// Create the kustomization file
create_file_with_data(kustomization_file_path, yml)
}
// Execute the kustomization
std_out, err := exec_command(*cwd_path, "kubectl", "kustomize", *cwd_path)
if err != nil {
log.Fatal(err)
}
// Create the .env-dec.yml file
create_file_with_data(dot_env_dec_file_path, std_out)
// Execute the sops encryption
std_out2, err2 := exec_command(*cwd_path, "sops", "--encrypt", "--encrypted-regex", "^(data|stringData)$", dot_env_dec_file_path)
if err2 != nil {
log.Fatal(err)
}
// If the save secret is false then print to the console
if !*save_secret {
fmt.Print(std_out2)
return
}
// Create the .env-enc.yml file
create_file_with_data(dot_env_enc_file_path, std_out2)
// Print a success to the console
fmt.Println("Successfully created the encrypted secret: " + *dot_env_enc_file_name)
}