forked from sl1pm4t/gcp-exec-creds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (60 loc) · 1.44 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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"golang.org/x/oauth2/google"
)
// ExecCredential is the Kubernetes ExecCredential API object
// Example:
// {
// "apiVersion": "client.authentication.k8s.io/v1beta1",
// "kind": "ExecCredential",
// "status": {
// "token": "my-bearer-token"
// }
// }
type ExecCredential struct {
APIVersion string `json:"apiVersion"`
Kind string `json:"ExecCredential"`
Status ExecCredentialStatus `json:"status"`
}
type ExecCredentialStatus struct {
Token string `json:"token"`
}
func NewExecCredential(token string) ExecCredential {
ec := ExecCredential{
APIVersion: "client.authentication.k8s.io/v1beta1",
Kind: "ExecCredential",
Status: ExecCredentialStatus{
Token: token,
},
}
return ec
}
func (ec ExecCredential) String() string {
b, err := json.MarshalIndent(&ec, "", " ")
if err != nil {
fmt.Printf("could not marshal ExecCredentials: %s", err)
os.Exit(1)
}
return string(b)
}
func main() {
clientScopes := []string{
"https://www.googleapis.com/auth/cloud-platform",
}
tokenSource, err := google.DefaultTokenSource(context.Background(), clientScopes...)
if err != nil {
fmt.Printf("could not get tokenSource: %s", err)
os.Exit(1)
}
token, err := tokenSource.Token()
if err != nil {
fmt.Printf("could not get token: %s", err)
os.Exit(1)
}
ec := NewExecCredential(token.AccessToken)
fmt.Println(ec.String())
}