//go:build hostedlive package main import ( "context" "errors" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" ) // Controller ruling R41: livectl resolves credentials once, at startup, // before the account check, and takes only static keys (an access key, a // secret and an optional session token) from the environment or from the // selected profile in the shared config and credentials files. Every other // provider is refused with exit 4 and a fixed message naming its kind: // none of them is ever built, so no credential service is called, nothing // refreshes, and every later call signs with exactly the keys the account // check saw. Resolution reads only the environment and the two files. // fixedCredentials hands out the same keys for the life of the process. type fixedCredentials struct{ c aws.Credentials } func (f fixedCredentials) Retrieve(context.Context) (aws.Credentials, error) { c := f.c c.CanExpire, c.Source = false, "livectl static keys" return c, nil } // refusedKind is the guard failure for a credential provider livectl will // not use. func refusedKind(kind string) *failure { return guard("credentials from %s are refused; livectl takes only static keys (R41)", kind) } // profileKind names the non-static provider a profile asks for, or "". func profileKind(sc config.SharedConfig) string { switch { case sc.WebIdentityTokenFile != "": return "web identity" case sc.SSOSessionName != "" || sc.SSOSession != nil || sc.SSOStartURL != "" || sc.SSOAccountID != "" || sc.SSORoleName != "": return "SSO" case sc.CredentialProcess != "": return "credential_process" case sc.CredentialSource == "EcsContainer": return "container credentials" case sc.CredentialSource == "Ec2InstanceMetadata": return "instance metadata" case sc.CredentialSource != "": return "credential_source" case sc.RoleARN != "" || sc.SourceProfileName != "" || sc.Source != nil: return "assume role" case sc.LoginSession != "": return "login session" } return "" } // resolveCredentials returns the region and the static keys to use, in the // SDK's own order: keys in the environment; else the selected profile, // refused if it names any other provider; else a refusal naming what the // SDK would have fallen back to (web identity, the container endpoint or // instance metadata). func resolveCredentials(ctx context.Context) (string, aws.Credentials, *failure) { ec, err := config.NewEnvConfig() if err != nil { return "", aws.Credentials{}, &failure{exit: exitOther, Kind: "other", Message: "the AWS environment settings did not parse"} } profile := ec.SharedConfigProfile if profile == "" { profile = "default" } cfgFile, credFile := ec.SharedConfigFile, ec.SharedCredentialsFile if cfgFile == "" { cfgFile = config.DefaultSharedConfigFilename() } if credFile == "" { credFile = config.DefaultSharedCredentialsFilename() } sc, err := config.LoadSharedConfigProfile(ctx, profile, func(o *config.LoadSharedConfigOptions) { o.ConfigFiles, o.CredentialsFiles = []string{cfgFile}, []string{credFile} }) var missing config.SharedConfigProfileNotExistError if err != nil && !errors.As(err, &missing) { return "", aws.Credentials{}, &failure{exit: exitOther, Kind: "other", Message: "the shared config or credentials file did not parse"} } region := ec.Region if region == "" { region = sc.Region } switch { case ec.Credentials.HasKeys(): return region, ec.Credentials, nil case profileKind(sc) != "": return "", aws.Credentials{}, refusedKind(profileKind(sc)) case sc.Credentials.HasKeys(): return region, sc.Credentials, nil case ec.WebIdentityTokenFilePath != "" || ec.RoleARN != "": return "", aws.Credentials{}, refusedKind("web identity") case ec.ContainerCredentialsEndpoint != "" || ec.ContainerCredentialsRelativePath != "": return "", aws.Credentials{}, refusedKind("container credentials") } return "", aws.Credentials{}, refusedKind("instance metadata") } // staticConfig is the only aws.Config livectl builds: the region and the // resolved keys. No credential chain is ever created. func staticConfig(region string, c aws.Credentials) aws.Config { if region == "" { region = wantRegion } return aws.Config{Region: region, Credentials: fixedCredentials{c}} }