forked from mintel/dex-k8s-authenticator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
356 lines (293 loc) · 8.69 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
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
package main
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"strings"
"github.com/coreos/go-oidc"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
config_file string
debug bool
)
type debugTransport struct {
t http.RoundTripper
}
func (d debugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
reqDump, err := httputil.DumpRequest(req, true)
if err != nil {
return nil, err
}
log.Printf("%s", reqDump)
resp, err := d.t.RoundTrip(req)
if err != nil {
return nil, err
}
respDump, err := httputil.DumpResponse(resp, true)
if err != nil {
resp.Body.Close()
return nil, err
}
log.Printf("%s", respDump)
return resp, nil
}
// Define each cluster
type Cluster struct {
Name string
Short_Description string
Description string
Issuer string
Client_Secret string
Client_ID string
K8s_Master_URI string
K8s_Ca_URI string
K8s_Ca_Pem string
Verifier *oidc.IDTokenVerifier
Provider *oidc.Provider
OfflineAsScope bool
Client *http.Client
Redirect_URI string
Config Config
}
// Define our configuration
type Config struct {
Clusters []Cluster
Listen string
Web_Path_Prefix string
TLS_Cert string
TLS_Key string
IDP_Ca_URI string
IDP_Ca_Pem string
Logo_Uri string
Trusted_Root_Ca []string
}
func substituteEnvVars(text string) string {
re := regexp.MustCompile("\\${([a-zA-Z0-9\\-_]+)}")
matches := re.FindAllStringSubmatch(text, -1)
for _, val := range matches {
envVar := os.Getenv(val[1])
text = strings.Replace(text, val[0], envVar, -1)
}
return text
}
// Start the app
// Do some config parsing
// Setup http-clients and oidc providers
// Define per-cluster handlers
func start_app(config Config) {
// Config validation
listenURL, err := url.Parse(config.Listen)
if err != nil {
log.Fatalf("parse listen address: %v", err)
}
var s struct {
ScopesSupported []string `json:"scopes_supported"`
}
certp, err := x509.SystemCertPool()
for _, cert := range config.Trusted_Root_Ca {
ok := certp.AppendCertsFromPEM([]byte(cert))
if !ok {
log.Fatalf("Failed to parse a trusted cert, pem format expected")
}
}
mTlsConfig := &tls.Config{}
mTlsConfig.PreferServerCipherSuites = true
mTlsConfig.MinVersion = tls.VersionTLS10
mTlsConfig.MaxVersion = tls.VersionTLS12
mTlsConfig.RootCAs = certp
tr := &http.Transport{
TLSClientConfig: mTlsConfig,
}
// Ensure trailing slash on web-path-prefix
web_path_prefix := config.Web_Path_Prefix
if web_path_prefix != "/" {
web_path_prefix = fmt.Sprintf("%s/", path.Clean(web_path_prefix))
config.Web_Path_Prefix = web_path_prefix
}
// Generate handlers for each cluster
for i, _ := range config.Clusters {
cluster := config.Clusters[i]
if debug {
if cluster.Client == nil {
cluster.Client = &http.Client{
Transport: debugTransport{tr},
}
} else {
cluster.Client.Transport = debugTransport{tr}
}
} else {
cluster.Client = &http.Client{Transport: tr}
}
ctx := oidc.ClientContext(context.Background(), cluster.Client)
log.Printf("Creating new provider %s", cluster.Issuer)
provider, err := oidc.NewProvider(ctx, cluster.Issuer)
if err != nil {
log.Fatalf("Failed to query provider %q: %v\n", cluster.Issuer, err)
}
cluster.Provider = provider
log.Printf("Verifying client %s", cluster.Client_ID)
verifier := provider.Verifier(&oidc.Config{ClientID: cluster.Client_ID})
cluster.Verifier = verifier
if err := provider.Claims(&s); err != nil {
log.Fatalf("Failed to parse provider scopes_supported: %v", err)
}
if len(s.ScopesSupported) == 0 {
// scopes_supported is a "RECOMMENDED" discovery claim, not a required
// one. If missing, assume that the provider follows the spec and has
// an "offline_access" scope.
cluster.OfflineAsScope = true
} else {
// See if scopes_supported has the "offline_access" scope.
cluster.OfflineAsScope = func() bool {
for _, scope := range s.ScopesSupported {
if scope == oidc.ScopeOfflineAccess {
return true
}
}
return false
}()
}
cluster.Config = config
base_redirect_uri, err := url.Parse(cluster.Redirect_URI)
if err != nil {
fmt.Errorf("Parsing redirect_uri address: %v", err)
os.Exit(1)
}
// Each cluster gets a different login and callback URL
http.HandleFunc(base_redirect_uri.Path, cluster.handleCallback)
log.Printf("Registered callback handler at: %s", base_redirect_uri.Path)
login_uri := path.Join(config.Web_Path_Prefix, "login", cluster.Name)
http.HandleFunc(login_uri, cluster.handleLogin)
log.Printf("Registered login handler at: %s", login_uri)
}
// Index page
http.HandleFunc(config.Web_Path_Prefix, config.handleIndex)
// Serve static html assets
fs := http.FileServer(http.Dir("html/static/"))
static_uri := path.Join(config.Web_Path_Prefix, "static") + "/"
log.Printf("Registered static assets handler at: %s", static_uri)
http.Handle(static_uri, http.StripPrefix(static_uri, fs))
// Determine whether to use TLS or not
switch listenURL.Scheme {
case "http":
log.Printf("Listening on %s", config.Listen)
err := http.ListenAndServe(listenURL.Host, nil)
log.Fatal(err)
case "https":
log.Printf("Listening on %s", config.Listen)
err := http.ListenAndServeTLS(listenURL.Host, config.TLS_Cert, config.TLS_Key, nil)
log.Fatal(err)
default:
log.Fatalf("Listen address %q is not using http or https", config.Listen)
}
}
func substituteEnvVarsRecursive(copy, original reflect.Value) {
switch original.Kind() {
case reflect.Ptr:
originalValue := original.Elem()
if !originalValue.IsValid() {
return
}
copy.Set(reflect.New(originalValue.Type()))
substituteEnvVarsRecursive(copy.Elem(), originalValue)
case reflect.Interface:
originalValue := original.Elem()
copyValue := reflect.New(originalValue.Type()).Elem()
substituteEnvVarsRecursive(copyValue, originalValue)
copy.Set(copyValue)
case reflect.Struct:
for i := 0; i < original.NumField(); i += 1 {
substituteEnvVarsRecursive(copy.Field(i), original.Field(i))
}
case reflect.Slice:
copy.Set(reflect.MakeSlice(original.Type(), original.Len(), original.Cap()))
for i := 0; i < original.Len(); i += 1 {
substituteEnvVarsRecursive(copy.Index(i), original.Index(i))
}
case reflect.Map:
copy.Set(reflect.MakeMap(original.Type()))
for _, key := range original.MapKeys() {
originalValue := original.MapIndex(key)
copyValue := reflect.New(originalValue.Type()).Elem()
substituteEnvVarsRecursive(copyValue, originalValue)
copy.SetMapIndex(key, copyValue)
}
case reflect.String:
replacedString := substituteEnvVars(original.Interface().(string))
copy.SetString(replacedString)
default:
copy.Set(original)
}
}
var RootCmd = &cobra.Command{
Use: "dex-k8s-authenticator",
Short: "Dex Kubernetes Authenticator",
Long: `Dex Kubernetes Authenticator provides a web-interface to generate a kubeconfig file based on a selected Kubernetes cluster. One or more clusters can be defined in the configuration file.`,
Run: func(cmd *cobra.Command, args []string) {
var config Config
err := viper.Unmarshal(&config)
if err != nil {
log.Fatalf("Unable to decode configuration into struct, %v", err)
}
original := reflect.ValueOf(config)
copy := reflect.New(original.Type()).Elem()
substituteEnvVarsRecursive(copy, original)
// Start the app
start_app(copy.Interface().(Config))
// Fallback if no args specified
cmd.HelpFunc()(cmd, args)
},
}
// Read in config file
func initConfig() {
if config_file != "" {
//viper.SetConfigFile(config_file)
// get the filepath
abs, err := filepath.Abs(config_file)
if err != nil {
log.Fatalf("Error reading config file, %s", err)
}
// get the config name
base := filepath.Base(abs)
// get the path
path := filepath.Dir(abs)
viper.SetConfigName(strings.Split(base, ".")[0])
viper.AddConfigPath(path)
viper.SetDefault("web_path_prefix", "/")
config, err := ioutil.ReadFile(config_file)
if err != nil {
log.Fatalf("Error reading config file, %s", err)
}
origConfigStr := bytes.NewBuffer(config).String()
viper.ReadConfig(bytes.NewBufferString(origConfigStr))
log.Printf("Using config file:", viper.ConfigFileUsed())
}
}
// Initialization
func init() {
cobra.OnInitialize(initConfig)
viper.BindPFlags(RootCmd.Flags())
RootCmd.Flags().StringVar(&config_file, "config", "", "./config.yml")
RootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "Enable debug logging")
}
// Let's go!
func main() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}