-
Notifications
You must be signed in to change notification settings - Fork 5
/
env.go
64 lines (53 loc) · 1.48 KB
/
env.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
package cmd
import (
"fmt"
"os"
"strings"
"time"
"github.com/chanzuckerberg/aws-oidc/pkg/aws_config_client"
"github.com/honeycombio/beeline-go"
"github.com/spf13/cobra"
)
var flagProfileName string
var sessionDuration time.Duration
func init() {
envCmd.Flags().StringVar(
&flagProfileName,
aws_config_client.FlagProfile,
"",
"AWS Profile to fetch credentials from.")
envCmd.Flags().DurationVar(
&sessionDuration,
"session-duration",
time.Hour,
`The duration, of the role session. "1h" means 1 hour.
Must be between 1-12 hours and must be <= the target role's max session duration.`,
)
rootCmd.AddCommand(envCmd)
}
var envCmd = &cobra.Command{
Use: "env",
Short: "aws-oidc env",
Long: `Env will output relevant AWS credentials to environment variables.
Useful when running docker such "docker run -it --env-file <(aws-oidc env --profile foobar) amazon/aws-cli sts get-caller-identity"
`,
RunE: envRun,
}
func envRun(cmd *cobra.Command, args []string) error {
ctx, span := beeline.StartSpan(cmd.Context(), "env_command")
defer span.Send()
awsOIDCConfig, err := aws_config_client.FetchParamsFromAWSConfig(
cmd,
aws_config_client.DefaultAWSConfigPath)
if err != nil {
return err
}
assumeRoleOutput, err := assumeRole(ctx, awsOIDCConfig, sessionDuration)
if err != nil {
return err
}
// output in the appropriate format for docker
envVars := getAWSEnvVars(assumeRoleOutput, awsOIDCConfig)
fmt.Fprintln(os.Stdout, strings.Join(envVars, "\n"))
return nil
}