Skip to content

Commit 5db74ad

Browse files
committed
improved error handling and added docs to readme
1 parent d5d2a88 commit 5db74ad

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ There are five command line parameters :-
1414
* `--output-file` - Filename for the output kubeconfig file. Default is [username].config (OPTIONAL)
1515
* `--expirationSeconds` - Number of seconds for the certificate to be valid. If not specified this will take the server's default setting. (OPTIONAL)
1616

17+
## Known Limitations
18+
19+
- This won't work on EKS clusters because they don't issue certificates for Client authentication.
20+
- This won't work with clusters earlier than 1.19 as we're using v1 of the CSR API which was issued then.

main.go

+13
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,12 @@ type KubeConfig struct {
7777
func connectToCluster(kubeconfig string) *kubernetes.Clientset {
7878
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
7979
if err != nil {
80+
fmt.Println("Error creating config object")
8081
log.Fatal(err)
8182
}
8283
clientset, err := kubernetes.NewForConfig(config)
8384
if err != nil {
85+
fmt.Println("Error creating clientset")
8486
log.Fatal(err)
8587
}
8688
return clientset
@@ -108,6 +110,7 @@ func main() {
108110

109111
key, err := rsa.GenerateKey(rand.Reader, 1024)
110112
if err != nil {
113+
fmt.Println("Error Generating key")
111114
log.Fatal(fmt.Printf("Error %s", err))
112115
}
113116
keyDer := x509.MarshalPKCS1PrivateKey(key)
@@ -118,6 +121,7 @@ func main() {
118121
}
119122
asn1, err := asn1.Marshal(subject.ToRDNSequence())
120123
if err != nil {
124+
fmt.Println("Error marshalling ASN")
121125
log.Fatal(fmt.Printf("Error %s", err))
122126
}
123127
csrReq := x509.CertificateRequest{
@@ -126,6 +130,7 @@ func main() {
126130
}
127131
bytes, err := x509.CreateCertificateRequest(rand.Reader, &csrReq, key)
128132
if err != nil {
133+
fmt.Println("Error Creating Certificate Request")
129134
log.Fatal(fmt.Printf("Error %s", err))
130135
}
131136

@@ -151,6 +156,7 @@ func main() {
151156
}
152157
_, err = clientset.CertificatesV1().CertificateSigningRequests().Create(context.TODO(), csr, v1.CreateOptions{})
153158
if err != nil {
159+
fmt.Println("Error Creating CSR Object. Are you running on a cluste < 1.19? This only works with 1.19+")
154160
log.Fatal(fmt.Printf("Error %s", err))
155161
}
156162
csr.Status.Conditions = append(csr.Status.Conditions, certificates.CertificateSigningRequestCondition{
@@ -162,18 +168,21 @@ func main() {
162168
})
163169
csr, err = clientset.CertificatesV1().CertificateSigningRequests().UpdateApproval(context.Background(), "tempcsr", csr, v1.UpdateOptions{})
164170
if err != nil {
171+
fmt.Println("Error Approving Certificate")
165172
log.Fatal(fmt.Printf("Error %s", err))
166173
}
167174
// Give the API server a couple of seconds to issue the cert.
168175
time.Sleep(2 * time.Second)
169176
csr, _ = clientset.CertificatesV1().CertificateSigningRequests().Get(context.TODO(), csr.GetName(), v1.GetOptions{})
170177
pb, _ := pem.Decode(csr.Status.Certificate)
171178
if pb == nil {
179+
fmt.Println("Error issuing cert, are you trying this with EKS?")
172180
_ = clientset.CertificatesV1().CertificateSigningRequests().Delete(context.TODO(), csr.GetName(), v1.DeleteOptions{})
173181
log.Fatal(err)
174182
}
175183
issued_cert, err := x509.ParseCertificate(pb.Bytes)
176184
if err != nil {
185+
fmt.Println("Error Parsing Certificate")
177186
log.Fatal(err)
178187
}
179188
issued_group := "none"
@@ -224,20 +233,24 @@ func main() {
224233

225234
dir, err := os.Getwd()
226235
if err != nil {
236+
fmt.Println("Error Getting working directory")
227237
log.Fatal(err)
228238
}
229239
_, err = os.Create(filepath.Join(dir, *outputFile))
230240
if err != nil {
241+
fmt.Println("Error Creating output file")
231242
log.Fatal(err)
232243
}
233244
file, err := os.OpenFile(*outputFile, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
234245
if err != nil {
246+
fmt.Println("Error opening output file")
235247
log.Fatal(err)
236248
}
237249
defer file.Close()
238250
e := yaml.NewEncoder(file)
239251
err = e.Encode(kc)
240252
if err != nil {
253+
fmt.Println("Error encoding Kubeconfig YAML")
241254
log.Fatal(err)
242255
}
243256
clientset.CertificatesV1().CertificateSigningRequests().Delete(context.TODO(), csr.GetName(), v1.DeleteOptions{})

0 commit comments

Comments
 (0)