-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
265 lines (252 loc) · 8.88 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/base64"
"encoding/pem"
"flag"
"fmt"
"log"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v2"
certificates "k8s.io/api/certificates/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
// Cluster holds the cluster data
type Cluster struct {
CertificateAuthorityData string `yaml:"certificate-authority-data"`
Server string `yaml:"server"`
}
//Clusters hold an array of the clusters that would exist in the config file
type Clusters []struct {
Cluster Cluster `yaml:"cluster"`
Name string `yaml:"name"`
}
//Context holds the cluster context
type Context struct {
Cluster string `yaml:"cluster"`
User string `yaml:"user"`
}
//Contexts holds an array of the contexts
type Contexts []struct {
Context Context `yaml:"context"`
Name string `yaml:"name"`
}
//Users holds an array of the users that would exist in the config file
type Users []struct {
User User `yaml:"user"`
Name string `yaml:"name"`
}
//User holds the user authentication data
type User struct {
ClientCertificateData string `yaml:"client-certificate-data"`
ClientKeyData string `yaml:"client-key-data"`
}
//KubeConfig holds the necessary data for creating a new KubeConfig file
type KubeConfig struct {
APIVersion string `yaml:"apiVersion"`
Clusters Clusters `yaml:"clusters"`
Contexts Contexts `yaml:"contexts"`
CurrentContext string `yaml:"current-context"`
Kind string `yaml:"kind"`
Preferences struct{} `yaml:"preferences"`
Users Users `yaml:"users"`
}
func findKubeConfig() (string, error) {
env := os.Getenv("KUBECONFIG")
if env != "" {
return env, nil
}
path, err := homedir.Expand("~/.kube/config")
if err != nil {
return "", err
}
return path, nil
}
func check(msg string, err error) {
if err != nil {
log.Fatal(fmt.Sprintf("%s: %s", msg, err))
}
}
func main() {
outDirPtr := flag.String("outdir", "", "[Optional] The directory to write the generated KUBECONFIG file to")
usernamePtr := flag.String("username", "", "[Required] The username")
emailPtr := flag.String("email", "", "[Required] The user's email")
clusterPtr := flag.String("cluster", "", "[Required] The cluster's name")
countryPtr := flag.String("country", "", "[Optional] The user's country")
cityPtr := flag.String("city", "", "[Optional] The user's city")
orgazniationPtr := flag.String("orgazniation", "", "[Optional] The user's orgazniation")
orgUnitPtr := flag.String("orgUnit", "", "[Optional] The user's organizational unit")
provincePtr := flag.String("province", "", "[Optional] The user's province")
flag.Usage = func() {
flagSet := flag.CommandLine
fmt.Printf("Usage: %s [Requied flags] [Optional flags] \n", path.Base(os.Args[0]))
order := []string{"username", "email", "cluster", "country", "city", "orgazniation", "orgUnit", "province"}
for _, name := range order {
flag := flagSet.Lookup(name)
fmt.Printf("--%s\n", flag.Name)
fmt.Printf(" %s\n", flag.Usage)
}
os.Exit(2)
}
flag.Parse()
if *usernamePtr == "" {
fmt.Println("The username is required")
flag.Usage()
}
if *emailPtr == "" {
fmt.Println("The user's email is required")
flag.Usage()
}
if *clusterPtr == "" {
fmt.Println("The cluster name is required")
flag.Usage()
}
if *countryPtr != "" && len(*countryPtr) > 2 {
log.Fatal("Invalid country. The name must be two letters. Example: US, AU, EG")
}
if *outDirPtr != "" {
dir, err := os.Stat(*outDirPtr)
if err != nil {
log.Fatalf("failed to open out directory, error: %s\n", err)
}
if !dir.IsDir() {
log.Fatalf("%q is not a directory\n", dir.Name())
}
} else {
*outDirPtr, _ = os.Getwd()
}
key, err := rsa.GenerateKey(rand.Reader, 1024)
check("The following error occured while creating the RSA key", err)
keyDer := x509.MarshalPKCS1PrivateKey(key)
commonName := *usernamePtr
emailAddress := *emailPtr
org := strings.ToUpper(*orgazniationPtr)
orgUnit := strings.ToUpper(*orgUnitPtr)
city := strings.ToUpper(*cityPtr)
state := strings.ToUpper(*provincePtr)
country := strings.ToUpper(*countryPtr)
subject := pkix.Name{
CommonName: commonName,
Country: []string{country},
Locality: []string{city},
Organization: []string{org},
OrganizationalUnit: []string{orgUnit},
Province: []string{state},
}
asn1, err := asn1.Marshal(subject.ToRDNSequence())
check("The following error occured while creating the asn1 DNS sequence", err)
csrReq := x509.CertificateRequest{
RawSubject: asn1,
EmailAddresses: []string{emailAddress},
SignatureAlgorithm: x509.SHA256WithRSA,
}
bytes, err := x509.CreateCertificateRequest(rand.Reader, &csrReq, key)
check("The following error occured while Creating the CSR", err)
kubeconfig, err := findKubeConfig()
check("The following error occured while getting the Kube Config file", err)
config, _ := clientcmd.BuildConfigFromFlags("", kubeconfig)
clientset, err := kubernetes.NewForConfig(config)
check("The following error occured while loading the Kube Config file", err)
csr := &certificates.CertificateSigningRequest{
ObjectMeta: v1.ObjectMeta{
Name: "tempcsr",
},
Spec: certificates.CertificateSigningRequestSpec{
Groups: []string{
"system:authenticated",
},
Request: pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE REQUEST", Bytes: bytes}),
Usages: []certificates.KeyUsage{
certificates.UsageKeyEncipherment,
certificates.UsageDigitalSignature,
certificates.UsageClientAuth,
},
SignerName: "kubernetes.io/kube-apiserver-client",
},
}
_, err = clientset.CertificatesV1().CertificateSigningRequests().Create(context.TODO(), csr, v1.CreateOptions{})
check("The following error occured while sending the Certificate Signing Request", err)
csr.Status.Conditions = append(csr.Status.Conditions, certificates.CertificateSigningRequestCondition{
Type: certificates.CertificateApproved,
Reason: "User activation",
Message: "This CSR was approved",
LastUpdateTime: v1.Now(),
})
csr, err = clientset.CertificatesV1().CertificateSigningRequests().Get(context.Background(), "tempcsr", v1.GetOptions{})
check("The following error occured while getting the Certificate Signing Request", err)
if len(csr.Status.Conditions) == 0 {
csr.Status.Conditions = append(csr.Status.Conditions, certificates.CertificateSigningRequestCondition{
Type: certificates.CertificateApproved,
Status: corev1.ConditionTrue,
})
csr, err = clientset.CertificatesV1().CertificateSigningRequests().UpdateApproval(context.Background(), "tempcsr", csr, v1.UpdateOptions{})
check("The following error occured while approving the Certificate Signing Request", err)
}
csr, _ = clientset.CertificatesV1().CertificateSigningRequests().Get(context.TODO(), csr.GetName(), v1.GetOptions{})
clientset.CertificatesV1().CertificateSigningRequests().Delete(context.TODO(), csr.GetName(), v1.DeleteOptions{})
kubeConfig, err := clientcmd.LoadFromFile(kubeconfig)
check("The following error occured while loading the KubeConfig file", err)
if _, v := kubeConfig.Clusters[*clusterPtr]; !v {
log.Fatal(fmt.Sprintf("Cluster \"%s\" was not found in the current Kube Config file", *clusterPtr))
}
for len(csr.Status.Certificate) == 0 {
csr, _ = clientset.CertificatesV1().CertificateSigningRequests().Get(context.TODO(), csr.GetName(), v1.GetOptions{})
time.Sleep(2 * time.Second)
}
kc := &KubeConfig{
APIVersion: "v1",
Clusters: Clusters{
0: {
Cluster{
base64.StdEncoding.EncodeToString([]byte(kubeConfig.Clusters[*clusterPtr].CertificateAuthorityData)),
kubeConfig.Clusters[*clusterPtr].Server,
},
*clusterPtr,
},
},
Contexts: Contexts{
0: {
Context{
Cluster: *clusterPtr,
User: *usernamePtr,
},
*clusterPtr,
},
},
CurrentContext: *clusterPtr,
Kind: "Config",
Users: Users{
0: {
User{
ClientCertificateData: base64.StdEncoding.EncodeToString(csr.Status.Certificate),
ClientKeyData: base64.StdEncoding.EncodeToString(pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: keyDer})),
},
*usernamePtr,
},
},
}
// dir, err := os.Getwd()
outFile := filepath.Join(*outDirPtr, strings.Replace(*usernamePtr, ":", "_", -1))
check("The following error occured while getting the current working directory %s", err)
_, err = os.Create(outFile)
check("The following error occured while creating the target file %s", err)
file, err := os.OpenFile(outFile, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
check("The following error occured while creating the target Kube Config file", err)
defer file.Close()
e := yaml.NewEncoder(file)
err = e.Encode(kc)
check("The following error occured while writing YAML to the target Kube Config file", err)
}