-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
175 lines (150 loc) · 3.55 KB
/
main.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
package main
import (
"encoding/base64"
"fmt"
tty "github.com/mattn/go-tty"
"io/ioutil"
"os"
"os/exec"
"github.com/urfave/cli"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)
// DecodedSecret ...
type DecodedSecret struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
// fix map[string][]bytes -> map[string]string to decode base64 secret data.
Data map[string]string `json:"data,omitempty" protobuf:"bytes,2,rep,name=data"`
StringData map[string]string `json:"stringData,omitempty" protobuf:"bytes,4,rep,name=stringData"`
Type apiv1.SecretType `json:"type,omitempty" protobuf:"bytes,3,opt,name=type,casttype=SecretType"`
}
func encodeSecret(secret *DecodedSecret) {
for key, data := range secret.Data {
decoded := base64.StdEncoding.EncodeToString([]byte(data))
secret.Data[key] = decoded
}
}
func decodeSecret(secret *DecodedSecret) {
for key, data := range secret.Data {
decoded, err := base64.StdEncoding.DecodeString(data)
if err != nil {
panic(err)
}
secret.Data[key] = string(decoded)
}
}
func editSecretWithEditor(secret *DecodedSecret, editor string) {
tempFile, err := ioutil.TempFile("", "secret")
if err != nil {
panic(err)
}
bytes, err := yaml.Marshal(secret)
if err != nil {
panic(err)
}
tempFile.Write(bytes)
tempFile.Close()
c := exec.Command(editor, tempFile.Name())
tty, err := tty.Open()
if err != nil {
panic(err)
}
defer tty.Close()
c.Stdin = tty.Input()
c.Stdout = tty.Output()
c.Stderr = os.Stderr
err = c.Run()
if err != nil {
panic(err)
}
edited, err := ioutil.ReadFile(tempFile.Name())
*secret = *&DecodedSecret{}
err = yaml.Unmarshal(edited, secret)
if err != nil {
panic(err)
}
}
func readSecretYml(filepath string, secret *DecodedSecret) {
bytes, err := ioutil.ReadFile(filepath)
if err != nil {
panic(err)
}
err = yaml.Unmarshal(bytes, secret)
if err != nil {
panic(err)
}
}
func readSecretYmlFromStdin(secret *DecodedSecret) {
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
err = yaml.Unmarshal(bytes, secret)
if err != nil {
panic(err)
}
}
func main() {
app := cli.NewApp()
app.Name = "ksedit"
app.Usage = "kubernetest secret resource edit"
app.UsageText = "ksedit [global options] filepath"
app.Description = ""
app.Version = "0.0.1"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "write, w",
Usage: "write secret",
},
cli.BoolFlag{
Name: "encode, e",
Usage: "encode secret",
},
cli.BoolFlag{
Name: "decode, d",
Usage: "decode secret",
},
}
app.Action = func(context *cli.Context) error {
filepath := context.Args().Get(0)
writeOpt := context.GlobalBool("write")
encodeOpt := context.GlobalBool("encode")
decodeOpt := context.GlobalBool("decode")
editor := os.ExpandEnv("$EDITOR")
if editor == "" {
editor = "vim"
}
secret := &DecodedSecret{}
if filepath != "" {
readSecretYml(filepath, secret)
} else {
readSecretYmlFromStdin(secret)
}
if encodeOpt == true {
encodeSecret(secret)
} else if decodeOpt == true {
decodeSecret(secret)
} else {
decodeSecret(secret)
editSecretWithEditor(secret, editor)
encodeSecret(secret)
}
bytes, err := yaml.Marshal(secret)
if err != nil {
panic(err)
}
if writeOpt == true && filepath != "" {
info, err := os.Stat(filepath)
if err != nil {
panic(err)
}
ioutil.WriteFile(filepath, bytes, info.Mode())
return nil
}
fmt.Println(string(bytes))
return nil
}
app.Run(os.Args)
}