Skip to content

Commit

Permalink
Handle auth files with BearerToken sections.
Browse files Browse the repository at this point in the history
  • Loading branch information
erictune committed Oct 21, 2014
1 parent 71c6f8e commit 21dae01
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 14 deletions.
22 changes: 20 additions & 2 deletions cluster/gce/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ function get-password {
KUBE_USER=admin
KUBE_PASSWORD=$(python -c 'import string,random; print "".join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(16))')

# Store password for reuse.
# Remove this code, since in all use cases I can see, we are overwriting this
# at cluster creation time.
cat << EOF > "$file"
{
"User": "$KUBE_USER",
Expand All @@ -203,6 +204,20 @@ EOF
chmod 0600 "$file"
}

# Generate authentication token for admin user. Will
# read from $HOME/.kubernetes_auth if available.
#
# Vars set:
# KUBE_ADMIN_TOKEN
function get-admin-token {
local file="$HOME/.kubernetes_auth"
if [[ -r "$file" ]]; then
KUBE_ADMIN_TOKEN=$(cat "$file" | python -c 'import json,sys;print json.load(sys.stdin)["BearerToken"]')
return
fi
KUBE_ADMIN_TOKEN=$(python -c 'import string,random; print "".join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(32))')
}

# Instantiate a kubernetes cluster
#
# Assumed vars
Expand Down Expand Up @@ -375,6 +390,8 @@ function kube-up {
local kube_key=".kubecfg.key"
local ca_cert=".kubernetes.ca.crt"

# TODO: generate ADMIN (and KUBELET) tokens and put those in the master's
# config file. Distribute the same way the htpasswd is done.
(umask 077
gcutil ssh "${MASTER_NAME}" sudo cat /usr/share/nginx/kubecfg.crt >"${HOME}/${kube_cert}" 2>/dev/null
gcutil ssh "${MASTER_NAME}" sudo cat /usr/share/nginx/kubecfg.key >"${HOME}/${kube_key}" 2>/dev/null
Expand All @@ -386,7 +403,8 @@ function kube-up {
"Password": "$KUBE_PASSWORD",
"CAFile": "$HOME/$ca_cert",
"CertFile": "$HOME/$kube_cert",
"KeyFile": "$HOME/$kube_key"
"KeyFile": "$HOME/$kube_key",
"BearerToken": "$KUBE_ADMIN_TOKEN"
}
EOF

Expand Down
1 change: 1 addition & 0 deletions cmd/e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func loadClientOrDie() *client.Client {
config.CAFile = auth.CAFile
config.CertFile = auth.CertFile
config.KeyFile = auth.KeyFile
config.BearerToken = auth.BearerToken
if auth.Insecure != nil {
config.Insecure = *auth.Insecure
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/kubecfg/kubecfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ func main() {
if auth.KeyFile != "" {
clientConfig.KeyFile = auth.KeyFile
}
if auth.BearerToken != "" {
clientConfig.BearerToken = auth.BearerToken
}
if auth.Insecure != nil {
clientConfig.Insecure = *auth.Insecure
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/kubecfg/kubecfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ func promptForString(field string, r io.Reader) string {
}

type AuthInfo struct {
User string
Password string
CAFile string
CertFile string
KeyFile string
Insecure *bool
User string
Password string
CAFile string
CertFile string
KeyFile string
BearerToken string
Insecure *bool
}

type NamespaceInfo struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/kubectl/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func getKubeClient(cmd *cobra.Command) *client.Client {
config.CAFile = firstNonEmptyString(getFlagString(cmd, "certificate-authority"), authInfo.CAFile)
config.CertFile = firstNonEmptyString(getFlagString(cmd, "client-certificate"), authInfo.CertFile)
config.KeyFile = firstNonEmptyString(getFlagString(cmd, "client-key"), authInfo.KeyFile)
config.BearerToken = authInfo.BearerToken
// For config.Insecure, the command line ALWAYS overrides the authInfo
// file, regardless of its setting.
if insecureFlag := getFlagBoolPtr(cmd, "insecure-skip-tls-verify"); insecureFlag != nil {
Expand Down
13 changes: 7 additions & 6 deletions pkg/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ func GetKubeClient(config *client.Config, matchVersion bool) (*client.Client, er
}

type AuthInfo struct {
User string
Password string
CAFile string
CertFile string
KeyFile string
Insecure *bool
User string
Password string
CAFile string
CertFile string
KeyFile string
BearerToken string
Insecure *bool
}

// LoadAuthInfo parses an AuthInfo object from a file path. It prompts user and creates file if it doesn't exist.
Expand Down

0 comments on commit 21dae01

Please sign in to comment.