Skip to content

Commit

Permalink
Merge pull request mintel#41 from mintel/feat-path-prefix
Browse files Browse the repository at this point in the history
Feat path prefix
  • Loading branch information
nabadger authored Aug 8, 2018
2 parents e1e37e0 + 674654d commit 12c54fd
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 48 deletions.
1 change: 1 addition & 0 deletions charts/dex-k8s-authenticator/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ data:
config.yaml: |-
{{- with .Values.dexK8sAuthenticator }}
listen: http://0.0.0.0:{{ default "5555" .port }}
web_path_prefix: {{ default "/" .web_path_prefix }}
debug: {{ default "false" .debug }}
{{- if .logoUrl }}
logo_uri: {{ .logoUrl }}
Expand Down
4 changes: 2 additions & 2 deletions charts/dex-k8s-authenticator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ spec:
protocol: TCP
livenessProbe:
httpGet:
path: /
path: {{ .Values.dexK8sAuthenticator.web_path_prefix }}
port: http
readinessProbe:
httpGet:
path: /
path: {{ .Values.dexK8sAuthenticator.web_path_prefix }}
port: http
volumeMounts:
- name: config
Expand Down
1 change: 1 addition & 0 deletions charts/dex-k8s-authenticator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ image:
dexK8sAuthenticator:
port: 5555
debug: false
web_path_prefix: /
#logoUrl: http://<path-to-your-logo.png>
#tlsCert: /path/to/dex-client.crt
#tlsKey: /path/to/dex-client.key
Expand Down
7 changes: 4 additions & 3 deletions dex-auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (cluster *Cluster) handleLogin(w http.ResponseWriter, r *http.Request) {

scopes = append(scopes, "openid", "profile", "email", "offline_access", "groups")

log.Printf("Handling /login for: %s", cluster.Name)
log.Printf("Handling login-uri for: %s", cluster.Name)
authCodeURL := cluster.oauth2Config(scopes).AuthCodeURL(exampleAppState, oauth2.AccessTypeOffline)
log.Printf("Redirecting post-loginto: %s", authCodeURL)
http.Redirect(w, r, authCodeURL, http.StatusSeeOther)
Expand Down Expand Up @@ -107,8 +107,9 @@ func (cluster *Cluster) handleCallback(w http.ResponseWriter, r *http.Request) {
json.Indent(buff, []byte(claims), "", " ")

cluster.renderToken(w, rawIDToken, token.RefreshToken,
viper.GetString("idp_ca_uri"),
viper.GetString("logo_uri"),
cluster.Config.IDP_Ca_URI,
cluster.Config.Logo_Uri,
cluster.Config.Web_Path_Prefix,
viper.GetString("kubectl_version"),
buff.Bytes())
}
1 change: 1 addition & 0 deletions examples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ clusters:
#tls_cert: /path/to/dex-client.crt
#tls_key: /path/to/dex-client.key

web_path_prefix: /
listen: http://127.0.0.1:5555
#logo_uri: http://<path-to-your-logo.png>
kubectl_version: v1.10.2
Expand Down
2 changes: 1 addition & 1 deletion html/static/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pre {

.dex-btn-icon--local {
background-color: #84B6EF;
background-image: url(/static/button.svg);
background-image: url({{ .Web_Path_Prefix }}static/button.svg);
}

.dex-btn-text {
Expand Down
37 changes: 25 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ type Cluster struct {
OfflineAsScope bool
Client *http.Client
Redirect_URI string
Config Config
}

// Define our configuration
type Config struct {
Clusters []Cluster
Listen string

Clusters []Cluster
Listen string
Web_Path_Prefix string
TLS_Cert string
TLS_Key string
IDP_Ca_URI string
Expand Down Expand Up @@ -128,6 +129,13 @@ func start_app(config Config) {
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]
Expand Down Expand Up @@ -180,6 +188,8 @@ func start_app(config Config) {
}()
}

cluster.Config = config

base_redirect_uri, err := url.Parse(cluster.Redirect_URI)

if err != nil {
Expand All @@ -188,21 +198,23 @@ func start_app(config Config) {
}

// Each cluster gets a different login and callback URL
callback_uri := path.Join(base_redirect_uri.Path)
http.HandleFunc(callback_uri, cluster.handleCallback)
log.Printf("Registered callback handler at: %s", callback_uri)
http.HandleFunc(base_redirect_uri.Path, cluster.handleCallback)
log.Printf("Registered callback handler at: %s", base_redirect_uri.Path)

login_uri := path.Join("/login", cluster.Name)
login_uri := path.Join(config.Web_Path_Prefix, "login", cluster.Name)
http.HandleFunc(login_uri, cluster.handleLogin)
log.Printf("Registered login handler at: /login/%s", cluster.Name)
log.Printf("Registered login handler at: %s", login_uri)
}

// Index page
http.HandleFunc("/", config.handleIndex)
http.HandleFunc(config.Web_Path_Prefix, config.handleIndex)

// Serve static html assets
fs := http.FileServer(http.Dir("html/static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
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 {
Expand All @@ -216,7 +228,7 @@ func start_app(config Config) {
log.Fatal(err)

default:
fmt.Errorf("Listen address %q is not using http or https", config.Listen)
log.Fatalf("Listen address %q is not using http or https", config.Listen)
}
}

Expand Down Expand Up @@ -311,6 +323,7 @@ func initConfig() {

viper.SetConfigName(strings.Split(base, ".")[0])
viper.AddConfigPath(path)
viper.SetDefault("web_path_prefix", "/")

config, err := ioutil.ReadFile(config_file)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type templateData struct {
K8sCaPem string
IDPCaURI string
LogoURI string
Web_Path_Prefix string
KubectlVersion string
}

Expand All @@ -46,6 +47,7 @@ func (cluster *Cluster) renderToken(w http.ResponseWriter,
refreshToken string,
idpCaURI string,
logoURI string,
webPathPrefix string,
kubectlVersion string,
claims []byte) {

Expand Down Expand Up @@ -74,6 +76,7 @@ func (cluster *Cluster) renderToken(w http.ResponseWriter,
K8sCaPem: cluster.K8s_Ca_Pem,
IDPCaURI: idpCaURI,
LogoURI: logoURI,
Web_Path_Prefix: webPathPrefix,
KubectlVersion: kubectlVersion}

err = templates.ExecuteTemplate(w, "kubeconfig.html", token_data)
Expand Down
10 changes: 5 additions & 5 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

<title>Generate Kubernetes Token</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/static/main.css" rel="stylesheet" type="text/css">
<link href="/static/styles.css" rel="stylesheet" type="text/css">
<link rel="icon" href="/static/favicon.png">
<link href="{{ .Web_Path_Prefix }}static/main.css" rel="stylesheet" type="text/css">
<link href="{{ .Web_Path_Prefix }}static/styles.css" rel="stylesheet" type="text/css">
<link rel="icon" href="{{ .Web_Path_Prefix }}static/favicon.png">
</head>

<body class="theme-body">
<div class="theme-navbar">
{{ if .Logo_Uri }}
<div class="theme-navbar__logo-wrap">
<img class="theme-navbar__logo" src="{{ .Logo_Uri}}"/>
<img class="theme-navbar__logo" src="{{ .Logo_Uri }}"/>
</div>
{{ end }}
</div>
Expand All @@ -35,7 +35,7 @@ <h2 class="theme-heading">Generate Kubernetes Token</h2>

<div class="theme-form-row">
<p class="theme-form-description">{{$cluster.Description}}</p>
<a href="/login/{{$cluster.Name}}" target="_self">
<a href="{{ $.Web_Path_Prefix }}login/{{$cluster.Name}}" target="_self">
<button class="dex-btn theme-btn-provider">
<span class="dex-btn-icon dex-btn-icon--local"></span>
<span class="dex-btn-text">{{$cluster.Short_Description}}</span>
Expand Down
20 changes: 9 additions & 11 deletions templates/kubeconfig.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


<!DOCTYPE html>
<html>
<head>
Expand All @@ -10,10 +8,10 @@

<title>Kubernetes Configuration</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/static/main.css" rel="stylesheet" type="text/css">
<link href="/static/styles.css" rel="stylesheet" type="text/css">
<link href="/static/tabs.css" rel="stylesheet" type="text/css">
<link rel="icon" href="/static/favicon.png">
<link href="{{ .Web_Path_Prefix }}static/main.css" rel="stylesheet" type="text/css">
<link href="{{ .Web_Path_Prefix }}static/styles.css" rel="stylesheet" type="text/css">
<link href="{{ .Web_Path_Prefix }}static/tabs.css" rel="stylesheet" type="text/css">
<link rel="icon" href="{{ .Web_Path_Prefix }}static/favicon.png">
</head>

<body class="theme-body">
Expand All @@ -28,7 +26,7 @@
<div class="dex-kubeconfig-container">
<div class="theme-panel">
<div style="float:right">
<a href="/">Login Again</a>
<a href="{{ .Web_Path_Prefix }}">Login Again</a>
</div>
<h2 class="theme-heading">Generated Kubernetes Token - {{ .ShortDescription }}</h2>

Expand Down Expand Up @@ -62,10 +60,10 @@ <h2 class="theme-heading">Generated Kubernetes Token - {{ .ShortDescription }}</
</div>
</div>

<script src="/static/highlight.pack.min.js"></script>
<script src="/static/clipboard.min.js"></script>
<script src="/static/snippets.js"></script>
<script src="/static/tooltips.js"></script>
<script src="{{ .Web_Path_Prefix }}static/highlight.pack.min.js"></script>
<script src="{{ .Web_Path_Prefix }}static/clipboard.min.js"></script>
<script src="{{ .Web_Path_Prefix }}static/snippets.js"></script>
<script src="{{ .Web_Path_Prefix }}static/tooltips.js"></script>
<script>
var clipboard = new ClipboardJS('.btn');
clipboard.on('success', function(e) {
Expand Down
14 changes: 7 additions & 7 deletions templates/linux-mac-common.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ <h3>Copy IDP CA Certificate</h3>
<div class="command">

<button class="btn" style="float:right" data-clipboard-snippet="">
<img class="clippy" width="13" src="/static/clippy.svg" alt=""/>
<img class="clippy" width="13" src="{{ .Web_Path_Prefix }}static/clippy.svg" alt=""/>
</button>
<pre><code>curl --create-dirs -s {{ .IDPCaURI }} -o ${HOME}/.kube/certs/{{ .ClusterName }}/idp-ca.crt</code></pre>
</div>
Expand All @@ -19,7 +19,7 @@ <h3>Copy Kubernetes CA Certificate From URL</h3>
<div class="command">

<button class="btn" style="float: right" data-clipboard-snippet="">
<img class="clippy" width="13" src="/static/clippy.svg" alt=""/>
<img class="clippy" width="13" src="{{ .Web_Path_Prefix }}static/clippy.svg" alt=""/>
</button>
<pre><code>curl --create-dirs -s {{ .K8sCaURI }} -o ${HOME}/.kube/certs/{{ .ClusterName }}/k8s-ca.crt</code></pre>
</div>
Expand All @@ -33,7 +33,7 @@ <h3>Copy Kubernetes CA Certificate From PEM</h3>
<div class="command">

<button class="btn" style="float:right" data-clipboard-snippet="">
<img class="clippy" width="13" src="/static/clippy.svg" alt=""/>
<img class="clippy" width="13" src="{{ .Web_Path_Prefix }}static/clippy.svg" alt=""/>
</button>
<pre><code>mkdir -p ${HOME}/.kube/certs/{{ .ClusterName }}/ &amp;&amp; cat &lt;&lt; EOF &gt; ${HOME}/.kube/certs/{{ .ClusterName }}/k8s-ca.crt
{{ .K8sCaPem }}
Expand All @@ -48,7 +48,7 @@ <h3>Run configuration commands</h3>
<div class="command">

<button class="btn" style="float:right" data-clipboard-snippet="">
<img class="clippy" width="13" src="/static/clippy.svg" alt="">
<img class="clippy" width="13" src="{{ .Web_Path_Prefix }}static/clippy.svg" alt="">
</button>
<pre><code>kubectl config set-cluster {{ .ClientID }} \
--certificate-authority=${HOME}/.kube/certs/{{ .ClusterName}}/k8s-ca.crt \
Expand All @@ -58,7 +58,7 @@ <h3>Run configuration commands</h3>
<div class="command">

<button class="btn" style="float:right" data-clipboard-snippet="">
<img class="clippy" width="13" src="/static/clippy.svg" alt=""/>
<img class="clippy" width="13" src="{{ .Web_Path_Prefix }}static/clippy.svg" alt=""/>
</button>
<pre><code>kubectl config set-credentials {{ .Username }}-{{ .ClientID }} \
--auth-provider=oidc \
Expand All @@ -75,7 +75,7 @@ <h3>Run configuration commands</h3>
<div class="command">

<button class="btn" style="float:right" data-clipboard-snippet="">
<img class="clippy" width="13" src="/static/clippy.svg" alt="">
<img class="clippy" width="13" src="{{ .Web_Path_Prefix }}static/clippy.svg" alt="">
</button>
<pre><code class="hljs">kubectl config set-context {{ .Username }}-{{ .ClientID }} \
--cluster={{ .ClientID }} \
Expand All @@ -85,7 +85,7 @@ <h3>Run configuration commands</h3>
<div class="command">

<button class="btn" style="float:right" data-clipboard-snippet="">
<img class="clippy" width="13" src="/static/clippy.svg" alt=""/>
<img class="clippy" width="13" src="{{ .Web_Path_Prefix }}static/clippy.svg" alt=""/>
</button>
<pre><code class="hljs">kubectl config use-context {{ .Username }}-{{ .ClientID}}</code></pre>
</div>
Expand Down
14 changes: 7 additions & 7 deletions templates/windows-tab.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h3>Copy IDP CA Certificate</h3>
<div class="command">

<button class="btn" style="float:right" data-clipboard-snippet="">
<img class="clippy" width="13" src="/static/clippy.svg" alt=""/>
<img class="clippy" width="13" src="{{ .Web_Path_Prefix }}static/clippy.svg" alt=""/>
</button>

<pre><code>curl --create-dirs -s {{ .IDPCaURI }} -o ${HOME}/.kube/certs/{{ .ClusterName }}/idp-ca.crt</code></pre>
Expand All @@ -37,7 +37,7 @@ <h3>Copy Kubernetes CA Certificate From URL</h3>
<div class="command">

<button class="btn" style="float: right" data-clipboard-snippet="">
<img class="clippy" width="13" src="/static/clippy.svg" alt=""/>
<img class="clippy" width="13" src="{{ .Web_Path_Prefix }}static/clippy.svg" alt=""/>
</button>
<pre><code>curl --create-dirs -s {{ .K8sCaURI }} -o ${HOME}/.kube/certs/{{ .ClusterName }}/k8s-ca.crt</code></pre>
</div>
Expand All @@ -51,7 +51,7 @@ <h3>Copy Kubernetes CA Certificate From PEM</h3>
<div class="command">

<button class="btn" style="float:right" data-clipboard-snippet="">
<img class="clippy" width="13" src="/static/clippy.svg" alt=""/>
<img class="clippy" width="13" src="{{ .Web_Path_Prefix }}static/clippy.svg" alt=""/>
</button>
<pre><code>mkdir -p ${HOME}/.kube/certs/{{ .ClusterName }}/ &amp;&amp; cat &lt;&lt; EOF &gt; ${HOME}/.kube/certs/{{ .ClusterName }}/k8s-ca.crt
{{ .K8sCaPem }}
Expand All @@ -66,15 +66,15 @@ <h3>Run configuration commands</h3>
<div class="command">

<button class="btn" style="float:right" data-clipboard-snippet="">
<img class="clippy" width="13" src="/static/clippy.svg" alt="">
<img class="clippy" width="13" src="{{ .Web_Path_Prefix }}static/clippy.svg" alt="">
</button>
<pre><code>kubectl config set-cluster {{ .ClientID }} --certificate-authority=${HOME}/.kube/certs/{{ .ClusterName}}/k8s-ca.crt --server={{ .K8sMasterURI }}</code></pre>
</div>

<div class="command">

<button class="btn" style="float:right" data-clipboard-snippet="">
<img class="clippy" width="13" src="/static/clippy.svg" alt=""/>
<img class="clippy" width="13" src="{{ .Web_Path_Prefix }}static/clippy.svg" alt=""/>
</button>
<pre><code>kubectl config set-credentials {{ .Username }}-{{ .ClientID }} --auth-provider=oidc --auth-provider-arg=idp-issuer-url={{ .Issuer }} --auth-provider-arg=client-id={{ .ClientID }} --auth-provider-arg=client-secret={{ .ClientSecret }} --auth-provider-arg=refresh-token={{ .RefreshToken }} --auth-provider-arg=id-token={{ .IDToken }}
{{- if .IDPCaURI }} --auth-provider-arg=idp-certificate-authority=${HOME}/.kube/certs/{{ .ClusterName }}/idp-ca.crt
Expand All @@ -84,14 +84,14 @@ <h3>Run configuration commands</h3>
<div class="command">

<button class="btn" style="float:right" data-clipboard-snippet="">
<img class="clippy" width="13" src="/static/clippy.svg" alt="">
<img class="clippy" width="13" src="{{ .Web_Path_Prefix }}static/clippy.svg" alt="">
</button>
<pre><code class="hljs">kubectl config set-context {{ .Username }}-{{ .ClientID }} --cluster={{ .ClientID }} --user={{ .Username}}-{{.ClientID }}</code></pre>
</div>

<div class="command">
<button class="btn" style="float:right" data-clipboard-snippet="">
<img class="clippy" width="13" src="/static/clippy.svg" alt=""/>
<img class="clippy" width="13" src="{{ .Web_Path_Prefix }}static/clippy.svg" alt=""/>
</button>
<pre><code class="hljs">kubectl config use-context {{ .Username }}-{{ .ClientID}}</code></pre>
</div>
Expand Down

0 comments on commit 12c54fd

Please sign in to comment.