-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
217 lines (202 loc) · 5.74 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
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
package main
import (
"context"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"io"
"log"
"os"
"github.com/DavidGamba/go-getoptions"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
var Logger = log.New(os.Stderr, "", log.LstdFlags)
func main() {
os.Exit(program(os.Args))
}
func program(args []string) int {
opt := getoptions.New()
opt.SetUnknownMode(getoptions.Pass)
opt.Self("", `Decodes the given secret.
If a secret is not given, lists all secrets in the current namespace.
Source: https://github.com/DavidGamba/dgtools`)
opt.SetCommandFn(Run)
opt.Bool("quiet", false, opt.GetEnv("QUIET"))
opt.Bool("pem", false, opt.Description("Parse the secret as a PEM encoded certificate"))
opt.String("namespace", "")
opt.String("key", "", opt.Description("limit output to the given key"))
opt.String("cluster", "", opt.Description(`Allows targeting a different cluster from the current context.
NOTE: This only works when you are not in a subshell
that sets a KUBECONFIG subset like 'kubie'.
`))
opt.HelpSynopsisArg("[<secret>]", "secret name")
opt.HelpCommand("help", opt.Alias("?"))
remaining, err := opt.Parse(args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
return 1
}
if opt.Called("quiet") {
Logger.SetOutput(io.Discard)
}
ctx, cancel, done := getoptions.InterruptContext()
defer func() { cancel(); <-done }()
err = opt.Dispatch(ctx, remaining)
if err != nil {
if errors.Is(err, getoptions.ErrorHelpCalled) {
return 1
}
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
return 1
}
return 0
}
func Run(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
namespace := opt.Value("namespace").(string)
cluster := opt.Value("cluster").(string)
key := opt.Value("key").(string)
pem := opt.Value("pem").(bool)
secret := ""
if len(args) >= 1 {
secret = args[0]
}
kubeConfig := GetKubeConfig()
Logger.Printf("kubeConfig: %s", kubeConfig)
config, err := NewRestConfig(kubeConfig, cluster)
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return fmt.Errorf("failed to create clientset: %w", err)
}
if namespace == "" {
namespace, _, _ = GetNamespace(kubeConfig, cluster)
Logger.Printf("namespace: %s", namespace)
}
if secret == "" {
list, err := clientset.CoreV1().Secrets(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to list secrets: %w", err)
}
for _, s := range list.Items {
fmt.Println(s.Name)
}
return nil
}
k8sSecret, err := clientset.CoreV1().Secrets(namespace).Get(ctx, secret, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to get secret: %w", err)
}
for k, v := range k8sSecret.Data {
if (key != "" && k == key) || key == "" {
fmt.Printf("%s=", k)
if pem {
info, err := ParseCert(string(v))
if err != nil {
fmt.Printf("%s\n", string(v))
Logger.Printf("%s\n", err)
} else {
fmt.Printf("\n%s\n", info)
}
} else {
fmt.Printf("%s\n", string(v))
}
}
}
for k, v := range k8sSecret.StringData {
if (key != "" && k == key) || key == "" {
fmt.Printf("%s=", k)
if pem {
info, err := ParseCert(v)
if err != nil {
fmt.Printf("%s\n", v)
Logger.Printf("%s\n", err)
} else {
fmt.Printf("\n%s\n", info)
}
} else {
fmt.Printf("%s\n", v)
}
}
}
return nil
}
func ParseCert(data string) (string, error) {
info := ""
blocks := []*pem.Block{}
rest := []byte(data)
for {
block, newRest := pem.Decode(rest)
if block == nil {
return info, fmt.Errorf("failed to parse certificate PEM")
}
blocks = append(blocks, block)
if len(newRest) == 0 {
break
}
rest = newRest
}
for i, block := range blocks {
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return info, fmt.Errorf("failed to parse certificate: %w", err)
}
info += fmt.Sprintf("Certificate %d\n", i)
info += fmt.Sprintf("\tIssuer: %s\n", cert.Issuer)
info += fmt.Sprintf("\tSubject: %s\n", cert.Subject)
info += fmt.Sprintf("\tNotBefore: %s\n", cert.NotBefore)
info += fmt.Sprintf("\tNotAfter: %s\n", cert.NotAfter)
info += fmt.Sprintf("\tSignatureAlgorithm: %s\n", cert.SignatureAlgorithm)
info += fmt.Sprintf("\tPublicKeyAlgorithm: %s\n", cert.PublicKeyAlgorithm)
info += fmt.Sprintf("\tSerialNumber: %s\n", cert.SerialNumber)
info += fmt.Sprintf("\tKeyUsage: %s\n", KeyUsage(cert.KeyUsage))
if len(cert.OCSPServer) > 0 {
info += fmt.Sprintf("\tOCSPServer: %v\n", cert.OCSPServer)
}
if len(cert.IssuingCertificateURL) > 0 {
info += fmt.Sprintf("\tIssuingCertificateURL: %v\n", cert.IssuingCertificateURL)
}
if len(cert.DNSNames) > 0 {
info += fmt.Sprintf("\tDNSNames: %v\n", cert.DNSNames)
}
if len(cert.EmailAddresses) > 0 {
info += fmt.Sprintf("\tEmailAddresses: %v\n", cert.EmailAddresses)
}
if len(cert.IPAddresses) > 0 {
info += fmt.Sprintf("\tIPAddresses: %v\n", cert.IPAddresses)
}
if len(cert.URIs) > 0 {
info += fmt.Sprintf("\tURIs: %v\n", cert.URIs)
}
if len(cert.CRLDistributionPoints) > 0 {
info += fmt.Sprintf("\tCRLDistributionPoints: %v\n", cert.CRLDistributionPoints)
}
}
return info, nil
}
func KeyUsage(k x509.KeyUsage) string {
switch k {
case x509.KeyUsageDigitalSignature:
return "DigitalSignature"
case x509.KeyUsageContentCommitment:
return "ContentCommitment"
case x509.KeyUsageKeyEncipherment:
return "KeyEncipherment"
case x509.KeyUsageDataEncipherment:
return "DataEncipherment"
case x509.KeyUsageKeyAgreement:
return "KeyAgreement"
case x509.KeyUsageCertSign:
return "CertSign"
case x509.KeyUsageCRLSign:
return "CRLSign"
case x509.KeyUsageEncipherOnly:
return "EncipherOnly"
case x509.KeyUsageDecipherOnly:
return "DecipherOnly"
}
return ""
}