-
Notifications
You must be signed in to change notification settings - Fork 5
/
exec.go
103 lines (87 loc) · 2.44 KB
/
exec.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
package cmd
import (
"os"
"time"
"github.com/chanzuckerberg/aws-oidc/pkg/aws_config_client"
"github.com/chanzuckerberg/aws-oidc/pkg/getter"
"github.com/chanzuckerberg/go-misc/oidc_cli/oidc_impl"
oidc_client "github.com/chanzuckerberg/go-misc/oidc_cli/oidc_impl/client"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type AWSDefaultEnvironment struct {
DEFAULT_OUTPUT string
DEFAULT_REGION string
}
func loadAWSDefaultEnv() (*AWSDefaultEnvironment, error) {
env := &AWSDefaultEnvironment{}
err := envconfig.Process("AWS", env)
if err != nil {
return env, errors.Wrap(err, "Unable to load all the aws environment variables")
}
return env, nil
}
func init() {
execCmd.Flags().StringVar(
&flagProfileName,
aws_config_client.FlagProfile,
"",
"AWS Profile to fetch credentials from. Can set AWS_PROFILE instead.")
execCmd.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(execCmd)
}
var execCmd = &cobra.Command{
Use: "exec",
Short: "aws-oidc exec",
Long: `exec takes in the command after the -- and executes it as a shell command`,
Args: parseArgs,
RunE: execRun,
}
func parseArgs(cmd *cobra.Command, args []string) error {
dashIndex := cmd.ArgsLenAtDash()
if dashIndex == -1 {
return errors.New("please separate services and command with '--'.")
}
return nil
}
func execRun(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
dashIndex := cmd.ArgsLenAtDash()
command := args[dashIndex]
commandArgs := args[dashIndex+1:]
awsOIDCConfig, err := aws_config_client.FetchParamsFromAWSConfig(
cmd,
aws_config_client.DefaultAWSConfigPath)
if err != nil {
return err
}
token, err := oidc_impl.GetToken(
ctx,
awsOIDCConfig.ClientID,
awsOIDCConfig.IssuerURL,
oidc_client.SetSuccessMessage(successMessage),
)
if err != nil {
return errors.Wrap(err, "Unable to obtain token from clientID and issuerURL")
}
assumeRoleOutput, err := getter.GetAWSAssumeIdentity(
ctx,
token,
awsOIDCConfig.RoleARN,
sessionDuration)
if err != nil {
return errors.Wrap(err, "Unable to extract right token output from AWS Assume Web identity")
}
envVars := append(
getAWSEnvVars(assumeRoleOutput, awsOIDCConfig),
os.Environ()...,
)
return exec(ctx, command, commandArgs, envVars)
}