This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
forked from kubernetes-sigs/aws-iam-authenticator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
196 lines (170 loc) · 5.48 KB
/
add.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
/*
Copyright 2021 by the contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"os"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmd_api "k8s.io/client-go/tools/clientcmd/api"
"github.com/LF-Certification/aws-iam-authenticator/pkg/config"
"github.com/LF-Certification/aws-iam-authenticator/pkg/mapper/configmap/client"
"sigs.k8s.io/yaml"
)
var addCmd = &cobra.Command{
Use: "add",
Short: "add IAM entity to an existing aws-auth configmap",
}
var addUserCmd = &cobra.Command{
Use: "user",
Short: "add a user entity to an existing aws-auth configmap, not for CRD/file backends",
Long: "NOTE: this does not currently support the CRD and file backends",
Run: func(cmd *cobra.Command, args []string) {
if userARN == "" || userName == "" || len(groups) == 0 {
fmt.Printf("invalid empty value in userARN %q, username %q, groups %q", userARN, userName, groups)
os.Exit(1)
}
checkPrompt(fmt.Sprintf("add userarn %s, username %s, groups %s", userARN, userName, groups))
cli := createClient()
cm, err := cli.AddUser(&config.UserMapping{
UserARN: userARN,
Username: userName,
Groups: groups,
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
b, err := yaml.Marshal(cm)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("updated configmap:\n\n%s\n", string(b))
},
}
var addRoleCmd = &cobra.Command{
Use: "role",
Short: "add a role entity to an existing aws-auth configmap, not for CRD/file backends",
Long: "NOTE: this does not currently support the CRD and file backends",
Run: func(cmd *cobra.Command, args []string) {
if roleARN == "" || userName == "" || len(groups) == 0 {
fmt.Printf("invalid empty value in rolearn %q, username %q, groups %q", roleARN, userName, groups)
os.Exit(1)
}
checkPrompt(fmt.Sprintf("add rolearn %s, username %s, groups %s", roleARN, userName, groups))
cli := createClient()
cm, err := cli.AddRole(&config.RoleMapping{
RoleARN: roleARN,
Username: userName,
Groups: groups,
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
b, err := yaml.Marshal(cm)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("updated configmap:\n\n%s\n", string(b))
},
}
func checkPrompt(action string) {
if !prompt {
return
}
msg := fmt.Sprintf("Ready to add %q, should we continue?", action)
prompt := promptui.Select{
Label: msg,
Items: []string{
"No, cancel it!",
fmt.Sprintf("Yes, let's add %q!", action),
},
}
idx, answer, err := prompt.Run()
if err != nil {
panic(err)
}
if idx != 1 {
fmt.Printf("cancelled %q [index %d, answer %q]\n", action, idx, answer)
os.Exit(0)
}
}
func createClient() client.Client {
if kubeconfigPath == "" {
fmt.Println("empty kubeconfig")
os.Exit(1)
}
var kcfg *restclient.Config
var err error
if kubeconfigContext != "" {
kcfg, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{
ExplicitPath: kubeconfigPath,
},
&clientcmd.ConfigOverrides{
CurrentContext: kubeconfigContext,
ClusterInfo: clientcmd_api.Cluster{Server: masterURL},
},
).ClientConfig()
} else {
kcfg, err = clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
}
if err != nil {
fmt.Println(err)
}
if kcfg == nil {
defaultConfig := clientcmd.DefaultClientConfig
kcfg, err = defaultConfig.ClientConfig()
if kcfg == nil || err != nil {
fmt.Printf("failed to create config from defaults %v\n", err)
os.Exit(1)
}
}
clientset, err := kubernetes.NewForConfig(kcfg)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return client.New(clientset.CoreV1().ConfigMaps("kube-system"))
}
var (
prompt bool
masterURL string
kubeconfigPath string
kubeconfigContext string
userARN string
userName string
groups []string
roleARN string
)
func init() {
rootCmd.AddCommand(addCmd)
addCmd.AddCommand(addUserCmd)
addCmd.AddCommand(addRoleCmd)
addCmd.PersistentFlags().BoolVar(&prompt, "prompt", true, "'false' to disable prompt'")
addCmd.PersistentFlags().StringVar(&masterURL, "master-url", "", "kube-apiserver URL for creating Kubernetes client")
addCmd.PersistentFlags().StringVar(&kubeconfigPath, "kubeconfig", "", "kubeconfig file path, if empty, it loads the default config")
addCmd.PersistentFlags().StringVar(&kubeconfigContext, "kubeconfig-context", "", "kubeconfig context, if empty, it uses the default context")
addUserCmd.PersistentFlags().StringVar(&userARN, "userarn", "", "A new user ARN")
addUserCmd.PersistentFlags().StringVar(&userName, "username", "", "A new user name")
addUserCmd.PersistentFlags().StringSliceVar(&groups, "groups", nil, "A new user groups")
addRoleCmd.PersistentFlags().StringVar(&roleARN, "rolearn", "", "A new role ARN")
addRoleCmd.PersistentFlags().StringVar(&userName, "username", "", "A new user name")
addRoleCmd.PersistentFlags().StringSliceVar(&groups, "groups", nil, "A new role groups")
}