Skip to content

Commit 8671c31

Browse files
committed
Use bearer token
1 parent 3e27ada commit 8671c31

4 files changed

Lines changed: 29 additions & 14 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,10 @@ Usage:
332332
gotoaws eks get-token [flags]
333333
334334
Flags:
335-
--cluster string arn or name of the cluster (required)
335+
--cluster string arn or name of the cluster
336336
-h, --help help for get-token
337337
--role string arn or name of the role
338+
--token-only Return only the token for use with Bearer token based tools
338339
339340
Global Flags:
340341
--config string config file (default "$HOME/.config/configstore/gotoaws.json")

cmd/eks/get_token.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import (
1010
)
1111

1212
type getTokenOptions struct {
13-
cluster string
14-
role string
13+
cluster string
14+
role string
15+
tokenOnly bool
1516
}
1617

1718
func newGetTokenCmd() *cobra.Command {
@@ -43,21 +44,21 @@ func newGetTokenCmd() *cobra.Command {
4344
}
4445
}
4546

46-
out := gen.FormatJSON(*t)
47+
if opts.tokenOnly {
48+
fmt.Fprintln(os.Stdout, t.Token)
49+
} else {
50+
out := gen.FormatJSON(*t)
4751

48-
fmt.Fprintln(os.Stdout, out)
52+
fmt.Fprintln(os.Stdout, out)
53+
}
4954

5055
return nil
5156
},
5257
}
5358

54-
cmd.Flags().StringVarP(&opts.cluster, "cluster", "", "", "arn or name of the cluster (required)")
55-
56-
if err := cmd.MarkFlagRequired("cluster"); err != nil {
57-
panic(err)
58-
}
59-
59+
cmd.Flags().StringVarP(&opts.cluster, "cluster", "", "", "arn or name of the cluster")
6060
cmd.Flags().StringVarP(&opts.role, "role", "", "", "arn or name of the role")
61+
cmd.Flags().BoolVarP(&opts.tokenOnly, "token-only", "", false, "Return only the token for use with Bearer token based tools")
6162

6263
return cmd
6364
}

pkg/eks/kubeclient.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ type Kubeclient struct {
2020
}
2121

2222
func NewKubeclient(cfg *config.Config, cluster *Cluster, role string) (*Kubeclient, error) {
23-
execConfig := NewExecConfig(cfg, cluster.Name, role)
23+
token, err := getToken(cfg, cluster.Name, role)
24+
if err != nil {
25+
return nil, err
26+
}
2427

2528
config := &rest.Config{
26-
Host: cluster.Endpoint,
29+
Host: cluster.Endpoint,
30+
BearerToken: token.Token,
2731
TLSClientConfig: rest.TLSClientConfig{
2832
CAData: cluster.CAData,
2933
},
30-
ExecProvider: execConfig,
3134
}
3235

3336
clientset, err := kubernetes.NewForConfig(config)

pkg/eks/token.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,13 @@ func (t *tokenGen) FormatJSON(token Token) string {
131131

132132
return string(enc)
133133
}
134+
135+
func getToken(cfg *config.Config, clusterName, role string) (*Token, error) {
136+
gen := NewTokenGen(cfg)
137+
138+
if role != "" {
139+
return gen.GetWithRole(clusterName, role)
140+
}
141+
142+
return gen.Get(clusterName)
143+
}

0 commit comments

Comments
 (0)