-
Notifications
You must be signed in to change notification settings - Fork 8
/
federation.go
126 lines (101 loc) · 3.35 KB
/
federation.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package aws
import (
"context"
"fmt"
"os"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/internal/fnerrors"
)
const (
idTokenVersion = 1
)
func newAssumeRoleCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "assume-role",
Short: "Uses Workload Federation to assume a particular AWS role.",
Args: cobra.NoArgs,
Hidden: true,
}
roleArn := cmd.Flags().String("role_arn", "", "The ARN of the role to assume.")
awsProfile := cmd.Flags().String("aws_profile", "", "Use the specified AWS profile.")
envFile := cmd.Flags().String("write_env", "", "Instead of outputting, write the environment variables to the specified file.")
duration := cmd.Flags().Duration("duration", time.Hour, "For how long the resulting AWS credentials should be valid for.")
return fncobra.Cmd(cmd).Do(func(ctx context.Context) error {
if *roleArn == "" {
return fnerrors.New("--role_arn is required")
}
token, err := fnapi.FetchToken(ctx)
if err != nil {
return err
}
claims, err := token.Claims(ctx)
if err != nil {
return err
}
var out = console.Stdout(ctx)
if *envFile != "" {
f, err := os.Create(*envFile)
if err != nil {
return err
}
defer f.Close()
out = f
}
t := time.Now()
resp, err := fnapi.IssueIdToken(ctx, "sts.amazonaws.com", idTokenVersion)
if err != nil {
return err
}
fmt.Fprintf(console.Stderr(ctx), "Obtained credentials from Namespace (took %v).\n", time.Since(t))
client, err := newSTSClient(ctx, *awsProfile)
if err != nil {
return err
}
sessionName := ""
if claims.InstanceID != "" {
sessionName = fmt.Sprintf("nsc.instance=%s", claims.InstanceID)
} else {
sessionName = fmt.Sprintf("nsc.tenant=%s", claims.TenantID)
}
t = time.Now()
arole, err := client.AssumeRoleWithWebIdentity(ctx, &sts.AssumeRoleWithWebIdentityInput{
RoleArn: aws.String(*roleArn),
RoleSessionName: aws.String(sessionName),
WebIdentityToken: aws.String(resp.IdToken),
DurationSeconds: aws.Int32(int32(duration.Seconds())),
})
if err != nil {
return err
}
fmt.Fprintf(console.Stderr(ctx), "Obtained credentials from AWS (took=%v).\n", time.Since(t))
fmt.Fprintf(out, "export AWS_ACCESS_KEY_ID=%s\n", *arole.Credentials.AccessKeyId)
fmt.Fprintf(out, "export AWS_SECRET_ACCESS_KEY=%s\n", *arole.Credentials.SecretAccessKey)
fmt.Fprintf(out, "export AWS_SESSION_TOKEN=%s\n", *arole.Credentials.SessionToken)
if *envFile != "" {
fmt.Fprintf(console.Stderr(ctx), "Wrote %s.\n", *envFile)
}
return nil
})
}
func newSTSClient(ctx context.Context, awsProfile string) (*sts.Client, error) {
var opts []func(*config.LoadOptions) error
opts = append(opts, config.WithRegion("us-east-1"))
if awsProfile != "" {
opts = append(opts, config.WithSharedConfigProfile(awsProfile))
}
cfg, err := config.LoadDefaultConfig(ctx, opts...)
if err != nil {
return nil, err
}
return sts.NewFromConfig(cfg), nil
}