-
Notifications
You must be signed in to change notification settings - Fork 29
/
auth.go
472 lines (384 loc) · 14.7 KB
/
auth.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package cmd
import (
"context"
"errors"
"fmt"
"github.com/coreos/go-oidc"
"github.com/ghodss/yaml"
"github.com/gini/dexter/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/microsoft"
"gopkg.in/square/go-jose.v2/jwt"
"io/ioutil"
k8sRuntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/clientcmd"
clientCmdApi "k8s.io/client-go/tools/clientcmd/api"
clientCmdLatest "k8s.io/client-go/tools/clientcmd/api/latest"
"net/http"
"net/url"
"os"
"os/signal"
"os/user"
"path/filepath"
"regexp"
"strings"
"sync"
"syscall"
"time"
)
// dexterOIDC: struct to store the required data and provide methods to
// authenticate with Googles OpenID implementation
type dexterOIDC struct {
endpoint string // azure or google
azureTenant string // azure tenant
clientID string // clientID commandline flag
clientSecret string // clientSecret commandline flag
callback string // callback URL commandline flag
state string // CSRF protection
kubeConfig string // location of the kube config file
dryRun bool // don't update the kubectl config
Oauth2Config *oauth2.Config // oauth2 configuration
k8sMutex sync.RWMutex // mutex to prevent simultaneous write to kubectl config
httpClient *http.Client // http client
httpServer http.Server // http server
quitChan chan struct{} // signal for a clean shutdown
signalChan chan os.Signal // react on signals from the outside world
}
// initialize the struct, parse commandline flags and install a signal handler
func (d *dexterOIDC) initialize() error {
// install signal handler
signal.Notify(
oidcData.signalChan,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
// get active user (to get the homedirectory)
usr, err := user.Current()
if err != nil {
return errors.New(fmt.Sprintf("failed to determine current user: %s", err))
}
// construct the path to the users .kube/config file as a default
kubeConfigDefaultPath := filepath.Join(usr.HomeDir, ".kube", "config")
// setup commandline flags
AuthCmd.PersistentFlags().StringVarP(&d.endpoint, "endpoint", "e", "google", "OIDC-providers: google or azure")
AuthCmd.PersistentFlags().StringVarP(&d.azureTenant, "tenant", "t", "common", "Your azure tenant")
AuthCmd.PersistentFlags().StringVarP(&d.clientID, "client-id", "i", "REDACTED", "Google clientID")
AuthCmd.PersistentFlags().StringVarP(&d.clientSecret, "client-secret", "s", "REDACTED", "Google clientSecret")
AuthCmd.PersistentFlags().StringVarP(&d.callback, "callback", "c", "http://127.0.0.1:64464/callback", "Callback URL. The listen address is dreived from that.")
AuthCmd.PersistentFlags().StringVarP(&d.kubeConfig, "kube-config", "k", kubeConfigDefaultPath, "Overwrite the default location of kube config (~/.kube/config)")
AuthCmd.PersistentFlags().BoolVarP(&d.dryRun, "dry-run", "d", false, "Toggle config overwrite")
// create random string as CSRF protection for the oauth2 flow
d.state = utils.RandomString()
return nil
}
// setup and populate the OAuth2 config
func (d *dexterOIDC) createOauth2Config() error {
// no commandline client credentials supplied
if d.clientID == "REDACTED" && d.clientSecret == "REDACTED" {
// no builtin defaults - let's try auto-configuration
if defaultClientID == "" && defaultClientSecret == "" {
log.Info("Autopilot mode - no credentials set")
if err := d.autoConfigureOauth2Config(); err != nil {
return errors.New(fmt.Sprintf("failed to extract oidc configuration from the kube config: %s", err))
}
} else if defaultClientID != "" && defaultClientSecret != "" {
// use build-time defaults if no clientId & clientSecret was provided
log.Info("Using builtin credentials - no credentials set")
d.clientID = defaultClientID
d.clientSecret = defaultClientSecret
}
}
// setup oidc client context
oidc.ClientContext(context.Background(), d.httpClient)
// populate oauth2 config
d.Oauth2Config.ClientID = oidcData.clientID
d.Oauth2Config.ClientSecret = oidcData.clientSecret
d.Oauth2Config.RedirectURL = oidcData.callback
switch oidcData.endpoint {
case "azure":
d.Oauth2Config.Endpoint = microsoft.AzureADEndpoint(oidcData.azureTenant)
d.Oauth2Config.Scopes = []string{oidc.ScopeOpenID, oidc.ScopeOfflineAccess, "email"}
case "google":
d.Oauth2Config.Endpoint = google.Endpoint
d.Oauth2Config.Scopes = []string{oidc.ScopeOpenID, "profile", "email"}
default:
return errors.New(fmt.Sprintf("unsupported endpoint: %s", oidcData.endpoint))
}
return nil
}
// populate the Oauth2Config object from the kube config. Return an error when the operation failed
func (d *dexterOIDC) autoConfigureOauth2Config() error {
// initialize the clientConfig and error variables
var clientCfg *clientCmdApi.Config
var err error
// try to load the credentials from the kubeconfig specified on the commandline
if d.kubeConfig != "" {
clientCfg, err = clientcmd.LoadFromFile(d.kubeConfig)
if err != nil {
return errors.New(fmt.Sprintf("failed to load kubeconfig from %s: %s", d.kubeConfig, err))
}
} else {
// try to load credentials from CurrentContext
clientCfg, err = clientcmd.NewDefaultClientConfigLoadingRules().Load()
if err != nil {
return errors.New(fmt.Sprintf("failed to load kubeconfig from the default locations: %s", err))
}
}
// loop through all the contexts until we find the current context
for contextName, context := range clientCfg.Contexts {
// find the context definition that matches the current active context
if contextName == clientCfg.CurrentContext {
// loop through the global authentication definitions
for authName, authInfo := range clientCfg.AuthInfos {
// find the authentication definition that is in use in the current context
if authName == context.AuthInfo {
// ensure it is oidc based
if authInfo.AuthProvider != nil && authInfo.AuthProvider.Name == "oidc" {
// verify that relevant keys exist
for _, key := range []string{"client-id", "client-secret", "idp-issuer-url"} {
if _, ok := authInfo.AuthProvider.Config[key]; !ok {
return errors.New(fmt.Sprintf("%s is missing in kubeconfig: %s", key, err))
}
}
// set client credentials and idp url based on the kubeconfig definition
d.clientSecret = authInfo.AuthProvider.Config["client-secret"]
d.clientID = authInfo.AuthProvider.Config["client-id"]
idp := authInfo.AuthProvider.Config["idp-issuer-url"]
// set endpoint based on a match on the issuer URL
if strings.Contains(idp, "google") {
oidcData.endpoint = "google"
} else if strings.Contains(idp, "microsoft") {
oidcData.endpoint = "azure"
re, err := regexp.Compile(`[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}`) //find uuid, this is tenant
if err != nil {
// failed to extract the azure tenant, use default "common
oidcData.azureTenant = "common"
return nil
}
res := re.FindStringSubmatch(idp)
if len(res) == 1 {
oidcData.azureTenant = res[0] // found tenant
} else {
// failed to find tenant, use common
oidcData.azureTenant = "common"
}
}
return nil
}
}
}
}
}
return errors.New("failed to auto-configure OIDC from kubeconfig")
}
func (d *dexterOIDC) authUrl() string {
return d.Oauth2Config.AuthCodeURL(d.state, oauth2.AccessTypeOffline, oauth2.SetAuthURLParam("prompt", "consent"))
}
// start HTTP server to receive callbacks. This has to be run in a go routine
func (d *dexterOIDC) startHttpServer() {
// set HTTP server listen address from callback URL
parsedURL, err := url.Parse(d.callback)
if err != nil {
log.Errorf("Failed to parse callback URL: %s", err)
d.quitChan <- struct{}{}
}
d.httpServer.Addr = parsedURL.Host
http.HandleFunc("/callback", d.callbackHandler)
d.httpServer.ListenAndServe()
}
// accept callbacks from your browser
func (d *dexterOIDC) callbackHandler(w http.ResponseWriter, r *http.Request) {
log.Info("callback received")
// Get code and state from the passed form value
code := r.FormValue("code")
callbackState := r.FormValue("state")
// verify code AND state are defined
if code == "" || callbackState == "" {
log.Errorf("no code or state in request: %q", r.Form)
http.Error(w, "no code or state found in your request", http.StatusBadRequest)
return
}
// compare callback state and initial state
if callbackState != oidcData.state {
log.Error("state mismatch! Someone could be tampering with your connection!")
http.Error(w, "state mismatch! Someone could be tampering with your connection!", http.StatusBadRequest)
return
}
log.Info("authCode and state verification passed. Fetching JWT")
// create context and exchange authCode for token
ctx := oidc.ClientContext(r.Context(), d.httpClient)
token, err := oidcData.Oauth2Config.Exchange(ctx, code)
if err != nil {
log.Errorf("Failed to exchange auth code: %s", err)
http.Error(w, "Failed to exchange auth code!", http.StatusInternalServerError)
return
}
log.Info("exchanged authCode for JWT token. Refresh token was supplied")
log.Infof("writing credentials to %s", d.kubeConfig)
if err := d.writeK8sConfig(token); err != nil {
log.Errorf("Failed to write k8s config: %s", err)
http.Error(w, fmt.Sprintf("Failed to write k8s config: %s", err), http.StatusInternalServerError)
return
}
// We're done here
oidcData.quitChan <- struct{}{}
w.WriteHeader(http.StatusOK)
w.Write([]byte("<html><body>Authentication completed. It's safe to close this window now ;-)</body></html>"))
return
}
type CustomClaim struct {
Email string `json:"email"`
}
// write the k8s config
func (d *dexterOIDC) writeK8sConfig(token *oauth2.Token) error {
// acquire lock
d.k8sMutex.Lock()
defer d.k8sMutex.Unlock()
idToken := token.Extra("id_token").(string)
parsed, err := jwt.ParseSigned(idToken)
if err != nil {
return errors.New(fmt.Sprintf("Failed to parse token: %s", err))
}
customClaim := &CustomClaim{}
claims := &jwt.Claims{}
err = parsed.UnsafeClaimsWithoutVerification(claims, customClaim)
if err != nil {
return errors.New(fmt.Sprintf("failed to get user details from token: %s", err))
}
email := customClaim.Email
// construct the authinfo struct
authInfo := &clientCmdApi.AuthInfo{
AuthProvider: &clientCmdApi.AuthProviderConfig{
Name: "oidc",
Config: map[string]string{
"client-id": d.clientID,
"client-secret": d.clientSecret,
"id-token": idToken,
"idp-issuer-url": claims.Issuer,
"refresh-token": token.RefreshToken,
},
},
}
// contruct the config snippet
config := &clientCmdApi.Config{
AuthInfos: map[string]*clientCmdApi.AuthInfo{email: authInfo},
}
// write the rendered config snipped when dry-run is enabled
if oidcData.dryRun {
// create a JSON representation
json, err := k8sRuntime.Encode(clientCmdLatest.Codec, config)
if err != nil {
return errors.New(fmt.Sprintf("failed to runtime encode config: %s", err))
}
// convert JSON to YAML
output, err := yaml.JSONToYAML(json)
if err != nil {
return errors.New(fmt.Sprintf("failed to convert JSON to YAML: %s", err))
}
// show the result
log.Infof("Here's the config snippet that would be merged with your config: \n%v", string(output))
return nil
}
// write the config
tempKubeConfig, err := ioutil.TempFile("", "")
defer os.Remove(tempKubeConfig.Name())
if err != nil {
return errors.New(fmt.Sprintf("failed to create tempfile: %s", err))
}
// write snipped to temporary file
clientcmd.WriteToFile(*config, tempKubeConfig.Name())
// setup the order for the file load
loadingRules := clientcmd.ClientConfigLoadingRules{
Precedence: []string{tempKubeConfig.Name(), d.kubeConfig},
}
// merge the configs
mergedConfig, err := loadingRules.Load()
if err != nil {
return errors.New(fmt.Sprintf("failed to merge configurations: %s", err))
}
// write the merged data to the k8s config
err = clientcmd.WriteToFile(*mergedConfig, d.kubeConfig)
if err != nil {
return errors.New(fmt.Sprintf("failed to write merged configuration: %s", err))
}
return nil
}
var (
// default injected at build time. This is optional
defaultClientID string
defaultClientSecret string
// initialize dexter OIDC config
oidcData = dexterOIDC{
Oauth2Config: &oauth2.Config{},
httpClient: &http.Client{Timeout: 2 * time.Second},
quitChan: make(chan struct{}),
signalChan: make(chan os.Signal, 1),
}
// Cobra command
AuthCmd = &cobra.Command{
Use: "auth",
Short: "Authenticate with OIDC provider",
Long: `Use your Google login to get a JWT (JSON Web Token) and update your
local k8s config accordingly. A refresh token is added and automatically refreshed
by kubectl. Existing token configurations are overwritten.
For details go to: https://blog.gini.net/
dexters authentication flow
===========================
1. Open a browser window/tab and redirect you to Google (https://accounts.google.com)
2. You login with your Google credentials
3. You will be redirected to dexters builtin webserver and can now close the browser tab
4. dexter extracts the token from the callback and patches your ~/.kube/config
➜ Unless you have a good reason to do so please use the built-in google credentials (if they were added at build time)!
`,
Run: authCommand,
}
)
// initialize the command
func init() {
// add the auth command
rootCmd.AddCommand(AuthCmd)
// parse commandline flags
if err := oidcData.initialize(); err != nil {
log.Errorf("Failed to initialize OIDC provider: %s", err)
os.Exit(1)
}
}
// the command to run
func authCommand(cmd *cobra.Command, args []string) {
if oidcData.clientID == "" || oidcData.clientSecret == "" {
log.Error("clientID and clientSecret cannot be empty!")
return
}
// setup oauth2 object
if err := oidcData.createOauth2Config(); err != nil {
log.Errorf("oauth2 configuration failed: %s", err)
return
}
log.Info("Starting auth browser session. Please check your browser instances...")
if err := utils.OpenURL(oidcData.authUrl()); err != nil {
log.Errorf("Failed to open browser session: %s", err)
return
}
log.Infof("Spawning http server to receive callbacks (%s)", oidcData.callback)
// spawn HTTP server
go oidcData.startHttpServer()
for {
select {
// flow was completed or error occured
case <-oidcData.quitChan:
log.Debugf("Shutdown signal received. We're done here")
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
oidcData.httpServer.Shutdown(ctx)
log.Infof("Shutdown completed")
os.Exit(0)
// OS signal was received
case sig := <-oidcData.signalChan:
log.Infof("Signal %d (%s) received. Initiating shutdown", sig, sig)
close(oidcData.quitChan)
default:
}
}
}