-
Notifications
You must be signed in to change notification settings - Fork 0
/
refresh.go
47 lines (41 loc) · 1.37 KB
/
refresh.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
package commands
import (
"context"
"github.com/alecthomas/kong"
"github.com/tb0hdan/aws-k8s/pkg/auth"
"github.com/tb0hdan/aws-k8s/pkg/config"
"github.com/tb0hdan/aws-k8s/pkg/external"
"github.com/tb0hdan/aws-k8s/pkg/utils"
"github.com/aws/aws-sdk-go-v2/service/sts"
log "github.com/sirupsen/logrus"
)
type RefreshCmd struct {
Token string `cmd:"token" help:"MFA token"`
}
func (r *RefreshCmd) Run(ctx *CLIContext) error {
appConfig := config.New("~/.aws/aws-k8s.ini")
appConfig.Load()
stsClient := &external.STSClient{User: ctx.User, AppConfig: appConfig}
client := stsClient.Get()
tokenInfo := &sts.GetSessionTokenInput{
DurationSeconds: utils.Int32Ptr(3600),
SerialNumber: utils.StrPtr(client.Role),
TokenCode: utils.StrPtr(r.Token),
}
output, err := client.Client.GetSessionToken(context.Background(), tokenInfo)
if err != nil {
log.Fatalf("GetSessionToken failed with: %+v\n", err)
}
absolutePath := kong.ExpandPath("~/.aws/aws-k8s.json")
credentials := auth.NewCredentials(absolutePath)
err = credentials.Save(&auth.CredentialsCache{
AccessKeyId: *output.Credentials.AccessKeyId,
Expiration: *output.Credentials.Expiration,
SecretAccessKey: *output.Credentials.SecretAccessKey,
SessionToken: *output.Credentials.SessionToken,
})
if err != nil {
log.Fatalf("Could not save credentials to cache: %+v\n", err)
}
return nil
}