-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
288 lines (245 loc) · 8.1 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
"github.com/coreos/go-oidc"
"golang.org/x/oauth2"
"encoding/json"
"io/ioutil"
"runtime"
. "github.com/logrusorgru/aurora"
)
var logger = log.New(os.Stdout, "", log.LUTC)
const (
clientID = "kubectl-login"
configFile = ".kubectl-login.json"
state = "csrf-protection-state"
oidcProvider = "oidc"
tokensSeparator = ";"
)
type configuration struct {
Issuer string `json:"issuer"`
RedirectURL string `json:"redirectUrl"`
LoginSecret string `json:"loginSecret"`
Aliases []string `json:"aliases"`
}
func main() {
rawConfig := getRawConfig()
alias := getAlias(os.Args[1:])
config, cluster := getConfigByAlias(alias, rawConfig)
currentKubeconfig := os.Getenv("KUBECONFIG")
masterKubeconfig := currentKubeconfig
if !isMasterConfig(currentKubeconfig) {
masterKubeconfig = strings.Split(currentKubeconfig, "_")[0]
}
newKubeconfig := getClusterConfig(masterKubeconfig, cluster)
if isLoggedIn(newKubeconfig) {
logger.Printf(newKubeconfig)
os.Exit(0)
}
switchConfig(masterKubeconfig, cluster)
kubeLogin := getKubeLogin(config)
ctx := context.Background()
// Initialize a provider by specifying dex's issuer URL.
provider, err := oidc.NewProvider(ctx, config.Issuer)
if err != nil {
logger.Fatalf("error: cannot initialize OIDC provider for issuer %s:%v", config.Issuer, err)
}
// Configure the OAuth2 config with the client values.
oauth2Config := oauth2.Config{
ClientID: clientID,
ClientSecret: kubeLogin,
RedirectURL: config.RedirectURL,
Endpoint: provider.Endpoint(), // Discovery returns the OAuth2 endpoints.
Scopes: []string{oidc.ScopeOpenID, "profile", "email", "groups", "offline_access"}, // "openid" is a required scope for OpenID Connect flows.
}
redirectUrl := oauth2Config.AuthCodeURL(state)
logger.Println(redirectUrl)
browserErr := openBrowser(redirectUrl)
if browserErr != nil {
if !strings.Contains(browserErr.Error(), "executable file not found in $PATH") {
logger.Fatalf("error: cannot open browser: %v", browserErr)
}
}
idTokenVerifier := provider.Verifier(&oidc.Config{ClientID: clientID})
tokensInput := readTokens()
rawIdToken, refreshToken := extractTokens(tokensInput)
if _, err = idTokenVerifier.Verify(ctx, rawIdToken); err != nil {
logger.Fatalf("error: token is invalid: %v", err)
}
if len(refreshToken) == 0 {
setIdTokenCreds(rawIdToken, newKubeconfig)
} else {
setOIDCAuth(kubeLogin, rawIdToken, refreshToken, config.Issuer, newKubeconfig)
}
switchContext(cluster, newKubeconfig)
if !isLoggedIn(newKubeconfig) {
logger.Fatal("error: kubectl command didn't work, even after login!")
}
//output the new kubeconfig path, used in the wrapper to set the env variable
logger.Printf(newKubeconfig)
}
func isMasterConfig(kubeconfigPath string) bool {
return len(kubeconfigPath) > 0 && !strings.Contains(kubeconfigPath, "_")
}
func switchConfig(masterConfig, cluster string) string {
clusterKubeconfig := getClusterConfig(masterConfig, cluster)
copyConfig(masterConfig, clusterKubeconfig)
return clusterKubeconfig
}
func getClusterConfig(masterConfig string, cluster string) string {
return masterConfig + "_" + cluster
}
func copyConfig(srcPath string, dstPath string) {
src, err := os.Open(srcPath)
if err != nil {
logger.Fatalf("error: could not open kubeconfig %s: %v", srcPath, err)
}
defer src.Close()
dst, err := os.Create(dstPath)
if err != nil {
logger.Fatalf("error: could not create kubeconfig %s: %v", dstPath, err)
}
defer dst.Close()
if _, err := io.Copy(dst, src); err != nil {
logger.Fatalf("error: could not copy kubeconfig %s to %s: %v", srcPath, dstPath, err)
}
}
func openBrowser(url string) error {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform: %v", runtime.GOOS)
}
return err
}
func getRawConfig() map[string]*configuration {
configPath := os.Getenv("HOME") + string(os.PathSeparator) + configFile
file, err := os.Open(configPath)
if err != nil {
logger.Fatalf("error: cannot open config file at %s: %v", configPath, err)
}
data, err := ioutil.ReadAll(file)
if err != nil {
closeFile(file)
logger.Fatalf("error: cannot read config file at %s: %v", configPath, err)
}
var cfg map[string]*configuration
err = json.Unmarshal(data, &cfg)
if err != nil {
closeFile(file)
logger.Fatalf("error: cannot unmarshal contents of config file at %s: %v", configPath, err)
}
closeFile(file)
return cfg
}
func getAlias(args []string) string {
if len(args) == 0 {
logger.Fatalf("Alias is mandatory i.e %s. try '%s' to get this value.",
Bold(Cyan("kubectl-login <ALIAS>")), Bold(Cyan("cat $HOME/"+configFile)))
}
return args[0]
}
func closeFile(f *os.File) {
if err := f.Close(); err != nil {
logger.Printf("warning: couldn't close config file: %v", err)
}
}
func getConfigByAlias(alias string, rawConfig map[string]*configuration) (*configuration, string) {
for k, v := range rawConfig {
if containsAlias(v, alias) {
return v, k
}
}
logger.Fatalf("Alias \"%s\" not found. Try '%s' to get this value.",
Bold(Cyan(alias)), Bold(Cyan("cat $HOME/"+configFile)))
return nil, ""
}
func getKubeLogin(config *configuration) string {
if os.Getenv("KUBELOGIN") != "" {
return os.Getenv("KUBELOGIN")
} else if config.LoginSecret != "" {
return config.LoginSecret
} else {
logger.Fatal("KUBELOGIN is not set. You Can also set this in your ~/" + configFile + " file.")
return ""
}
}
func containsAlias(c *configuration, s string) bool {
for _, val := range c.Aliases {
if val == s {
return true
}
}
return false
}
func extractTokens(combTkns string) (string, string) {
/* dex-redirect will combine the id token with the refresh token into a single string separated by ";" */
tkns := strings.Split(combTkns, tokensSeparator)
if len(tkns) == 1 {
return tkns[0], ""
} else {
return tkns[0], tkns[1]
}
}
func setIdTokenCreds(token, config string) {
tstr := fmt.Sprintf("--token=%s", token)
cfg := fmt.Sprintf("--kubeconfig=%s", config)
cmd := exec.Command("kubectl", "config", "set-credentials", clientID, tstr, cfg)
err := cmd.Run()
if err != nil {
logger.Fatalf("error: cannot set kubectl credentials: %v", err)
}
}
func setOIDCAuth(clientSecret, idToken, refreshToken, idpIssuerUrl, config string) {
cfg := fmt.Sprintf("--kubeconfig=%s", config)
authProv := fmt.Sprintf("--auth-provider=%s", oidcProvider)
authProvIdp := fmt.Sprintf("--auth-provider-arg=idp-issuer-url=%s", idpIssuerUrl)
authProvClientId := fmt.Sprintf("--auth-provider-arg=client-id=%s", clientID)
authProvClientSecret := fmt.Sprintf("--auth-provider-arg=client-secret=%s", clientSecret)
authProvIdToken := fmt.Sprintf("--auth-provider-arg=id-token=%s", idToken)
authProvRefreshToken := fmt.Sprintf("--auth-provider-arg=refresh-token=%s", refreshToken)
cmd := exec.Command("kubectl", "config", "set-credentials", clientID,
cfg,
authProv,
authProvIdp,
authProvClientId,
authProvClientSecret,
authProvIdToken,
authProvRefreshToken)
err := cmd.Run()
if err != nil {
logger.Fatalf("error: cannot set kubectl OIDC credentials: %v", err)
}
}
func switchContext(cluster, config string) {
clusterArg := fmt.Sprintf("--cluster=%s", cluster)
user := fmt.Sprintf("--user=%s", clientID)
cfg := fmt.Sprintf("--kubeconfig=%s", config)
cmd := exec.Command("kubectl", "config", "set-context", cluster, user, clusterArg, "--namespace=default", cfg)
err := cmd.Run()
if err != nil {
logger.Fatalf("error: cannot set kubectl login context: %v", err)
}
cmd = exec.Command("kubectl", "config", "use-context", cluster, cfg)
err = cmd.Run()
if err != nil {
logger.Fatalf("error: cannot switch to kubectl login context: %v", err)
}
}
func isLoggedIn(config string) bool {
cfg := fmt.Sprintf("--kubeconfig=%s", config)
err := exec.Command("kubectl", "get", "namespace", cfg).Run()
return err == nil
}