Skip to content
This repository was archived by the owner on Mar 6, 2023. It is now read-only.

Commit 44ac497

Browse files
author
Matt Morrison
committed
Initial commit
0 parents  commit 44ac497

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

README.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Kubernetes client-go credential helper for Google Cloud
2+
3+
`client-go` credential helper that generates Kubernetes `ExecCredentials` objects from the GCloud SDK account.
4+
5+
## Overview
6+
7+
Kubernetes provides a pluggable mechanism for getting user credentials for authenticating with the API server.
8+
`k8s.io/client-go` client libraries and tools using it such as `kubectl` and `kubelet` are able to execute an external command to receive user credentials.
9+
This tool reads the client credentials from the pre-authenticated GCloud SDK account on the local system and generates the appropriate `ExecCredential` API object that can be used by the above tools to authenticate.
10+
11+
See (https://kubernetes.io/docs/reference/access-authn-authz/authentication/#client-go-credential-plugins)
12+
13+
## Usage
14+
15+
Build and run `gcp-exec-creds`.
16+
The command does not take any flags or arguments.
17+
18+
**Example**
19+
20+
```
21+
gcp-exec-creds
22+
{
23+
"apiVersion": "client.authentication.k8s.io/v1beta1",
24+
"ExecCredential": "ExecCredential",
25+
"status": {
26+
"token": "ya29.Gl2MBsw3YWqsD5......OqgeJE5LciQ"
27+
}
28+
}
29+
```
30+
31+
## Building
32+
33+
**Prerequisites**
34+
35+
- Golang 1.11
36+
37+
`go get -v github.com/sl1pm4t/gcp-exec-creds`
38+
39+
### How is this different from 'gcloud config config-helper' command?
40+
41+
This tool outputs the credentials in the `ExecCredential` API document format which may be necessary for some tools.
42+
The `gcloud config config-helper` tool outputs in a non-standard json document.

go.mod

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module github.com/sl1pm4t/gcp-exec-creds
2+
3+
require (
4+
cloud.google.com/go v0.34.0 // indirect
5+
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e // indirect
6+
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890
7+
)

go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cloud.google.com/go v0.34.0 h1:eOI3/cP2VTU6uZLDYAoic+eyzzB9YyGmJ7eIjl8rOPg=
2+
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
3+
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg=
4+
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
5+
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890 h1:uESlIz09WIHT2I+pasSXcpLYqYK8wHcdCetU3VuMBJE=
6+
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=

main.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"os"
8+
9+
"golang.org/x/oauth2/google"
10+
)
11+
12+
// ExecCredential is the Kubernetes ExecCredential API object
13+
// Example:
14+
// {
15+
// "apiVersion": "client.authentication.k8s.io/v1beta1",
16+
// "kind": "ExecCredential",
17+
// "status": {
18+
// "token": "my-bearer-token"
19+
// }
20+
// }
21+
type ExecCredential struct {
22+
APIVersion string `json:"apiVersion"`
23+
Kind string `json:"ExecCredential"`
24+
Status ExecCredentialStatus `json:"status"`
25+
}
26+
27+
type ExecCredentialStatus struct {
28+
Token string `json:"token"`
29+
}
30+
31+
func NewExecCredential(token string) ExecCredential {
32+
ec := ExecCredential{
33+
APIVersion: "client.authentication.k8s.io/v1beta1",
34+
Kind: "ExecCredential",
35+
Status: ExecCredentialStatus{
36+
Token: token,
37+
},
38+
}
39+
return ec
40+
}
41+
42+
func (ec ExecCredential) String() string {
43+
b, err := json.MarshalIndent(&ec, "", " ")
44+
if err != nil {
45+
fmt.Printf("could not marshal ExecCredentials: %s", err)
46+
os.Exit(1)
47+
}
48+
return string(b)
49+
}
50+
51+
func main() {
52+
clientScopes := []string{
53+
"https://www.googleapis.com/auth/cloud-platform",
54+
}
55+
56+
tokenSource, err := google.DefaultTokenSource(context.Background(), clientScopes...)
57+
if err != nil {
58+
fmt.Printf("could not get tokenSource: %s", err)
59+
os.Exit(1)
60+
}
61+
62+
token, err := tokenSource.Token()
63+
if err != nil {
64+
fmt.Printf("could not get token: %s", err)
65+
os.Exit(1)
66+
}
67+
68+
ec := NewExecCredential(token.AccessToken)
69+
fmt.Println(ec.String())
70+
}

0 commit comments

Comments
 (0)