| 1 | //go:build hostedlive |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "errors" |
| 8 | |
| 9 | "github.com/aws/aws-sdk-go-v2/aws" |
| 10 | "github.com/aws/aws-sdk-go-v2/config" |
| 11 | ) |
| 12 | |
| 13 | // Controller ruling R41: livectl resolves credentials once, at startup, |
| 14 | // before the account check, and takes only static keys (an access key, a |
| 15 | // secret and an optional session token) from the environment or from the |
| 16 | // selected profile in the shared config and credentials files. Every other |
| 17 | // provider is refused with exit 4 and a fixed message naming its kind: |
| 18 | // none of them is ever built, so no credential service is called, nothing |
| 19 | // refreshes, and every later call signs with exactly the keys the account |
| 20 | // check saw. Resolution reads only the environment and the two files. |
| 21 | |
| 22 | // fixedCredentials hands out the same keys for the life of the process. |
| 23 | type fixedCredentials struct{ c aws.Credentials } |
| 24 | |
| 25 | func (f fixedCredentials) Retrieve(context.Context) (aws.Credentials, error) { |
| 26 | c := f.c |
| 27 | c.CanExpire, c.Source = false, "livectl static keys" |
| 28 | return c, nil |
| 29 | } |
| 30 | |
| 31 | // refusedKind is the guard failure for a credential provider livectl will |
| 32 | // not use. |
| 33 | func refusedKind(kind string) *failure { |
| 34 | return guard("credentials from %s are refused; livectl takes only static keys (R41)", kind) |
| 35 | } |
| 36 | |
| 37 | // profileKind names the non-static provider a profile asks for, or "". |
| 38 | func profileKind(sc config.SharedConfig) string { |
| 39 | switch { |
| 40 | case sc.WebIdentityTokenFile != "": |
| 41 | return "web identity" |
| 42 | case sc.SSOSessionName != "" || sc.SSOSession != nil || sc.SSOStartURL != "" || sc.SSOAccountID != "" || sc.SSORoleName != "": |
| 43 | return "SSO" |
| 44 | case sc.CredentialProcess != "": |
| 45 | return "credential_process" |
| 46 | case sc.CredentialSource == "EcsContainer": |
| 47 | return "container credentials" |
| 48 | case sc.CredentialSource == "Ec2InstanceMetadata": |
| 49 | return "instance metadata" |
| 50 | case sc.CredentialSource != "": |
| 51 | return "credential_source" |
| 52 | case sc.RoleARN != "" || sc.SourceProfileName != "" || sc.Source != nil: |
| 53 | return "assume role" |
| 54 | case sc.LoginSession != "": |
| 55 | return "login session" |
| 56 | } |
| 57 | return "" |
| 58 | } |
| 59 | |
| 60 | // resolveCredentials returns the region and the static keys to use, in the |
| 61 | // SDK's own order: keys in the environment; else the selected profile, |
| 62 | // refused if it names any other provider; else a refusal naming what the |
| 63 | // SDK would have fallen back to (web identity, the container endpoint or |
| 64 | // instance metadata). |
| 65 | func resolveCredentials(ctx context.Context) (string, aws.Credentials, *failure) { |
| 66 | ec, err := config.NewEnvConfig() |
| 67 | if err != nil { |
| 68 | return "", aws.Credentials{}, &failure{exit: exitOther, Kind: "other", Message: "the AWS environment settings did not parse"} |
| 69 | } |
| 70 | profile := ec.SharedConfigProfile |
| 71 | if profile == "" { |
| 72 | profile = "default" |
| 73 | } |
| 74 | cfgFile, credFile := ec.SharedConfigFile, ec.SharedCredentialsFile |
| 75 | if cfgFile == "" { |
| 76 | cfgFile = config.DefaultSharedConfigFilename() |
| 77 | } |
| 78 | if credFile == "" { |
| 79 | credFile = config.DefaultSharedCredentialsFilename() |
| 80 | } |
| 81 | sc, err := config.LoadSharedConfigProfile(ctx, profile, func(o *config.LoadSharedConfigOptions) { |
| 82 | o.ConfigFiles, o.CredentialsFiles = []string{cfgFile}, []string{credFile} |
| 83 | }) |
| 84 | var missing config.SharedConfigProfileNotExistError |
| 85 | if err != nil && !errors.As(err, &missing) { |
| 86 | return "", aws.Credentials{}, &failure{exit: exitOther, Kind: "other", Message: "the shared config or credentials file did not parse"} |
| 87 | } |
| 88 | region := ec.Region |
| 89 | if region == "" { |
| 90 | region = sc.Region |
| 91 | } |
| 92 | switch { |
| 93 | case ec.Credentials.HasKeys(): |
| 94 | return region, ec.Credentials, nil |
| 95 | case profileKind(sc) != "": |
| 96 | return "", aws.Credentials{}, refusedKind(profileKind(sc)) |
| 97 | case sc.Credentials.HasKeys(): |
| 98 | return region, sc.Credentials, nil |
| 99 | case ec.WebIdentityTokenFilePath != "" || ec.RoleARN != "": |
| 100 | return "", aws.Credentials{}, refusedKind("web identity") |
| 101 | case ec.ContainerCredentialsEndpoint != "" || ec.ContainerCredentialsRelativePath != "": |
| 102 | return "", aws.Credentials{}, refusedKind("container credentials") |
| 103 | } |
| 104 | return "", aws.Credentials{}, refusedKind("instance metadata") |
| 105 | } |
| 106 | |
| 107 | // staticConfig is the only aws.Config livectl builds: the region and the |
| 108 | // resolved keys. No credential chain is ever created. |
| 109 | func staticConfig(region string, c aws.Credentials) aws.Config { |
| 110 | if region == "" { |
| 111 | region = wantRegion |
| 112 | } |
| 113 | return aws.Config{Region: region, Credentials: fixedCredentials{c}} |
| 114 | } |
| 115 | |