-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
137 lines (118 loc) · 2.87 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
package main
import (
"bufio"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"gopkg.in/yaml.v2"
)
type secret map[string]interface{}
type decodedSecret struct {
Key string
Value string
}
var version string
func main() {
if len(os.Args) == 2 && os.Args[1] == "version" {
_, _ = fmt.Fprintf(os.Stdout, "ksd version %s\n", version)
return
}
info, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
if (info.Mode()&os.ModeCharDevice) != 0 || info.Size() < 0 {
_, _ = fmt.Fprintln(os.Stderr, "the command is intended to work with pipes.")
_, _ = fmt.Fprintln(os.Stderr, "usage: kubectl get secret <secret-name> -o <yaml|json> |", os.Args[0])
_, _ = fmt.Fprintln(os.Stderr, "usage:", os.Args[0], "< secret.<yaml|json>")
os.Exit(1)
}
stdin := read(os.Stdin)
output, err := parse(stdin)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "could not decode secret: %v\n", err)
os.Exit(1)
}
_, _ = fmt.Fprint(os.Stdout, string(output))
}
func cast(data interface{}, isJSON bool) (map[string]interface{}, bool) {
if isJSON {
d, ok := data.(map[string]interface{})
return d, ok
}
parsed, ok := data.(map[interface{}]interface{})
if !ok {
return nil, false
}
d := make(map[string]interface{}, len(parsed))
for key, value := range parsed {
d[key.(string)] = value
}
return d, true
}
func parse(in []byte) ([]byte, error) {
isJSON := isJSONString(in)
var s secret
if err := unmarshal(in, &s, isJSON); err != nil {
return nil, err
}
data, ok := cast(s["data"], isJSON)
if !ok || len(data) == 0 {
return in, nil
}
s["stringData"] = decode(data)
delete(s, "data")
return marshal(s, isJSON)
}
func read(rd io.Reader) []byte {
var output []byte
reader := bufio.NewReader(rd)
for {
input, err := reader.ReadByte()
if err != nil && err == io.EOF {
break
}
output = append(output, input)
}
return output
}
func unmarshal(in []byte, out interface{}, asJSON bool) error {
if asJSON {
return json.Unmarshal(in, out)
}
return yaml.Unmarshal(in, out)
}
func marshal(d interface{}, asJSON bool) ([]byte, error) {
if asJSON {
return json.MarshalIndent(d, "", " ")
}
return yaml.Marshal(d)
}
func decodeSecret(key, secret string, secrets chan decodedSecret) {
var value string
// avoid wrong encoded secrets
if decoded, err := base64.StdEncoding.DecodeString(secret); err == nil {
value = string(decoded)
} else {
value = secret
}
secrets <- decodedSecret{Key: key, Value: value}
}
func decode(data map[string]interface{}) map[string]string {
length := len(data)
secrets := make(chan decodedSecret, length)
decoded := make(map[string]string, length)
for key, encoded := range data {
go decodeSecret(key, encoded.(string), secrets)
}
for i := 0; i < length; i++ {
secret := <-secrets
decoded[secret.Key] = secret.Value
}
return decoded
}
func isJSONString(s []byte) bool {
return json.Unmarshal(s, &json.RawMessage{}) == nil
}