Skip to content
This repository has been archived by the owner on May 10, 2019. It is now read-only.

Commit

Permalink
Add cluster CA certificate into kubeconfig if one exists
Browse files Browse the repository at this point in the history
  • Loading branch information
ripta committed May 15, 2018
1 parent e921993 commit 3763b73
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
28 changes: 27 additions & 1 deletion kuberos.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"

"github.com/negz/kuberos/extractor"

Expand All @@ -14,6 +17,7 @@ import (
"github.com/pkg/errors"
"go.uber.org/zap"
"golang.org/x/oauth2"
"k8s.io/api/core/v1"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)
Expand All @@ -23,6 +27,9 @@ const (
// be redirected after authentication.
DefaultKubeCfgEndpoint = "ui"

// DefaultAPITokenMountPath is the default mount path for API tokens
DefaultAPITokenMountPath = "/var/run/secrets/kubernetes.io/serviceaccount"

schemeHTTP = "http"
schemeHTTPS = "https"

Expand Down Expand Up @@ -358,9 +365,28 @@ func populateUser(cfg *api.Config, p *extractor.OIDCAuthenticationParams) api.Co
},
},
}

for name, cluster := range cfg.Clusters {
// If the cluster definition does not come with certificate-authority-data nor
// certificate-authority, then check if kuberos has access to the cluster's CA
// certificate and include it when possible. Assume all errors are non-fatal.
if len(cluster.CertificateAuthorityData) == 0 && cluster.CertificateAuthority == "" {
caPath := filepath.Join(DefaultAPITokenMountPath, v1.ServiceAccountRootCAKey)
if caFile, err := os.Open(caPath); err == nil {
if caCert, err := ioutil.ReadAll(caFile); err == nil {
cluster.CertificateAuthorityData = caCert
}
}
}
c.Clusters[name] = cluster
c.Contexts[name] = &api.Context{Cluster: name, AuthInfo: p.Username}
c.Contexts[name] = &api.Context{
Cluster: name,
AuthInfo: p.Username,
}
// Use the first context as the current context
if c.CurrentContext == "" {
c.CurrentContext = name
}
}
return c
}
1 change: 1 addition & 0 deletions kuberos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func TestPopulateUser(t *testing.T) {
},
},
},
CurrentContext: "a",
},
},
{
Expand Down

0 comments on commit 3763b73

Please sign in to comment.