Documentation
¶
Overview ¶
Package awsauth gives the toolkit its AWS access: the base configuration and the settings of every client made from it; the Provider, which gives out configurations for environments, assuming the roles stack artifacts name; and the Access to stacks' environments built on it.
The base configuration is the Go SDK's own default resolution, config.LoadDefaultConfig, as it is (Epic D's revised D.1). It doesn't re-create the CDK CLI's extras: the AMAZON_* aliases, the EC2 check before using instance metadata, and the CLI's region fallbacks. Its one addition is the CLI's last resort: when no region is found, the region is us-east-1.
Resolving credentials reads the process's environment and the shared AWS files, as the SDK does; ADR 0001 names this as one of the two places the library reads process globals. A caller who wants none of it passes its own aws.Config.
Every base configuration, a caller's included, gets the toolkit's client settings, as upstream applies them over custom credentials too:
- retries as upstream's: 7 attempts with capped, equal-jitter exponential backoff from 300 ms, or from 1 s for CloudFormation through CloudFormationOptions;
- the user agent asteroid-computing-aws-cdk-toolkit/<version>, which callers add to per call with awsmiddleware.AddUserAgentKey, as upstream adds to it for deployments;
- a CDK_SDK_I0100 trace of each API call, without its input, and of the SDK's warnings.
Upstream's method tracing, which logs every SDK and provider method with a global indent, is not ported (Epic D's D.3-D4).
When a profile's role needs MFA, its mfa_serial, the token is asked of the host with a CDK_SDK_I1100 request, as upstream asks it (D.4); an empty answer, which a host that doesn't answer requests gives, goes on to STS, which refuses it with its own message.
S3 clients take S3Options, which uses path-style addressing when CDK_S3_FORCE_PATH_STYLE is set, or when AWS_ENDPOINT_URL_S3, or else AWS_ENDPOINT_URL, names a loopback host, as upstream's forceS3PathStyle decides (H.2-D7).
Role sessions are named from CDK_ROLE_SESSION_NAME or the OS user name, as upstream names them; these and the S3 variables are the other process globals the package reads.
Index ¶
- Constants
- func BaseConfig(ctx context.Context, io *iohelper.Helper, opts Options) (aws.Config, error)
- func CloudFormationOptions(o *cloudformation.Options)
- func S3Options(o *s3.Options)
- type Access
- func (a *Access) ForDestination(ctx context.Context, env cloudassembly.Environment, ...) (Target, error)
- func (a *Access) ForLookup(ctx context.Context, s *cloudassembly.Stack) (Target, error)
- func (a *Access) ForLookupBestEffort(ctx context.Context, s *cloudassembly.Stack) (Target, error)
- func (a *Access) ForStack(ctx context.Context, s *cloudassembly.Stack, mode Mode) (Target, error)
- func (a *Access) Placeholders(env cloudassembly.Environment) cloudassembly.PlaceholderResolver
- func (a *Access) Provider() *Provider
- func (a *Access) ResolveStackEnvironment(ctx context.Context, s *cloudassembly.Stack) (cloudassembly.Environment, error)
- type Account
- type BootstrapVersions
- type CredentialPlugin
- type Mode
- type Options
- type Provider
- func (p *Provider) AppEnv(ctx context.Context) (map[string]string, error)
- func (p *Provider) BaseCredentialsPartition(ctx context.Context, env cloudassembly.Environment, mode Mode) (string, bool, error)
- func (p *Provider) CurrentAccount(ctx context.Context, cfg aws.Config) (Account, error)
- func (p *Provider) DefaultAccount(ctx context.Context) (Account, bool, error)
- func (p *Provider) DefaultRegion() string
- func (p *Provider) ForEnvironment(ctx context.Context, env cloudassembly.Environment, mode Mode, role *Role) (aws.Config, bool, error)
- func (p *Provider) Lookups() contextprovider.AWS
- func (p *Provider) ResolveEnvironment(ctx context.Context, env cloudassembly.Environment) (cloudassembly.Environment, error)
- type Role
- type Target
Constants ¶
const DefaultRegion = "us-east-1"
DefaultRegion is the region used when no other is configured, as the CDK CLI defaults.
const ForcePathStyleEnv = "CDK_S3_FORCE_PATH_STYLE"
ForcePathStyleEnv names the environment variable that makes S3 clients use path-style addressing, for an S3 emulator, such as LocalStack or MinIO, reached by a host name: upstream's CDK_S3_FORCE_PATH_STYLE. It is one of the process globals the library reads (H.2-D7).
Variables ¶
This section is empty.
Functions ¶
func BaseConfig ¶
BaseConfig returns the base AWS configuration: opts.Config, or the SDK's default resolution with opts applied, with the region defaulting to DefaultRegion and the toolkit's client settings applied.
It fails with a cdkerr.Error of Kind cdkerr.KindAuthentication when the shared configuration cannot be loaded, such as when the profile does not exist or AWS_CA_BUNDLE names no file.
func CloudFormationOptions ¶
func CloudFormationOptions(o *cloudformation.Options)
CloudFormationOptions sets a CloudFormation client's retries, which wait longer than other clients' do, as upstream's do. Callers pass it to cloudformation.NewFromConfig.
func S3Options ¶
S3Options sets an S3 client's addressing as upstream's forceS3PathStyle does: path style when ForcePathStyleEnv is set to anything, or when the S3 endpoint the environment names is a loopback host, for the emulators that serve only path-style URLs (H.2-D7). Otherwise it leaves the SDK's choice, which is path style for an IP address endpoint already. Callers pass it to s3.NewFromConfig.
Types ¶
type Access ¶
type Access struct {
// contains filtered or unexported fields
}
Access gives access to stacks' environments, through the roles their artifacts name: upstream's EnvironmentAccess. It caches the access to each environment, role and mode, and concurrent requests for the same one share the work (Epic D's ED-4).
func NewAccess ¶
func NewAccess(p *Provider, io *iohelper.Helper, versions BootstrapVersions) *Access
NewAccess returns the access p gives, which reads bootstrap stack versions with versions.
func (*Access) ForDestination ¶
func (a *Access) ForDestination(ctx context.Context, env cloudassembly.Environment, d cloudassembly.AWSDestination) (Target, error)
ForDestination returns the access for publishing an asset to d, through d's role, in d's region or else env's: upstream's PublishingAws.sdk, which asks forEnvironment for writing with the destination's role. env is the asset's stack's environment, and d has its placeholders resolved already. The access shares the cache of every other, so a destination whose role is the stack's deploy role, or another destination's, assumes it once.
func (*Access) ForLookup ¶
ForLookup returns the access for looking things up in s's environment, through its lookup role: upstream's accessStackForLookup.
When the lookup role was assumed, the environment's bootstrap stack must be at least the version the role needs. When it was not, because assuming it failed but the base credentials are for the right account, a warning says so. It fails with a cdkerr.Error of Kind cdkerr.KindToolkit and Code "InsufficientBootstrapStackVersion" when the bootstrap stack is too old.
func (*Access) ForLookupBestEffort ¶
ForLookupBestEffort returns the access for looking things up in s's environment through its lookup role, or, when that fails, a warning with the failure's message and the access for reading s through its deploy role: upstream's accessStackForLookupBestEffort (M.9-D6). The access it gives may have only the lookup role's read-only permissions, so it is for reading the stack, not for anything only the deploy role may do.
Upstream first fails a stack without an environment; a stack can't have none here, because cloudassembly.Load refuses one. When the lookup fails and ctx is done, the lookup's error is returned without the warning or the fallback, which could only fail the same way. It fails with the errors of Access.ForStack when the fallback fails too.
func (*Access) ForStack ¶
ForStack returns the access for deploying s, or reading it when mode is ForReading, through its deploy role: upstream's accessStackForStackOperations.
func (*Access) Placeholders ¶
func (a *Access) Placeholders(env cloudassembly.Environment) cloudassembly.PlaceholderResolver
Placeholders returns the resolver of the placeholders in a stack's role ARNs for env, the stack's environment, resolved: ${AWS::AccountId} and ${AWS::Region} from env, and ${AWS::Partition} from env's base credentials, as upstream's replacePlaceholders does.
func (*Access) ResolveStackEnvironment ¶
func (a *Access) ResolveStackEnvironment(ctx context.Context, s *cloudassembly.Stack) (cloudassembly.Environment, error)
ResolveStackEnvironment returns s's environment, resolved.
type BootstrapVersions ¶
type BootstrapVersions interface {
VersionFromSSMParameter(ctx context.Context, t Target, parameter string) (int, error)
}
BootstrapVersions reads the version of an environment's bootstrap stack from the SSM parameter that holds it, with a target's configuration. The deployment epics implement it; Access.ForLookup needs it to check that a lookup role can do what the stack needs.
type CredentialPlugin ¶
type CredentialPlugin interface {
// Name names the plugin in messages.
Name() string
// Available reports whether the plugin can be used at all.
Available(ctx context.Context) (bool, error)
// CanProvide reports whether the plugin has credentials for the account.
CanProvide(ctx context.Context, account string) (bool, error)
// Credentials returns the credentials for the account, for mode; they are cached until they expire.
Credentials(ctx context.Context, account string, mode Mode) (aws.CredentialsProvider, error)
}
CredentialPlugin supplies credentials for accounts the default credentials aren't for: upstream's CredentialProviderSource (Epic D's D.5). The toolkit's own interface, toolkit.CredentialPlugin, is adapted to it.
type Mode ¶
type Mode int
Mode is what credentials are wanted for, which credential plugins are told: upstream's Mode.
type Options ¶
type Options struct {
// Profile is the shared config profile to use, which beats credentials in the environment; empty means the SDK's default resolution, which honours AWS_PROFILE.
Profile string
// Region overrides the resolved region.
Region string
// Config, when set, is used instead of the default resolution: upstream's BaseCredentials.custom.
// The toolkit's client settings still apply, replacing its Retryer, RetryMaxAttempts and Logger.
Config *aws.Config
// HTTPClient is the client every AWS call goes through, for proxies, CA bundles and timeouts; nil means the SDK's default, which honours HTTPS_PROXY, NO_PROXY and AWS_CA_BUNDLE.
HTTPClient aws.HTTPClient
// AccountCacheFile, when set, caches the default account by access key ID, in upstream's format, so a short-lived process saves an STS call; the CDK CLI uses ~/.cdk/cache/accounts_partitions.json.
// The toolkit reaches into no home directory on its own (Epic D's D.2-D3).
AccountCacheFile string
// CredentialPlugins supply credentials for accounts the default credentials aren't for, asked in order (Epic D's D.5).
CredentialPlugins []CredentialPlugin
}
Options are the options for BaseConfig.
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider gives out AWS configurations for environments: upstream's SdkProvider.
It picks base credentials for an environment's account: the default credentials, when they are for that account, or the default credentials anyway, noting they may be for another. Credential plugins come between the two: the first that is available and can provide credentials for the account gives them (Epic D's D.5). It then assumes the role the caller names, if any, and falls back to the base credentials when that fails but they are for the right account, with a warning, as upstream does for setups that predate bootstrap roles.
It is safe for concurrent use; concurrent requests for the default account share one STS call.
func NewProvider ¶
NewProvider returns a provider with the default credentials and region of BaseConfig. When opts.AccountCacheFile is set, the default account is cached in that file by access key ID.
func (*Provider) AppEnv ¶
AppEnv returns the environment variables that give a CDK app the default region and, when there is one, the default account: upstream's prepareDefaultEnvironment. An app with no default account still synthesizes; stacks that need one fail later.
func (*Provider) BaseCredentialsPartition ¶
func (p *Provider) BaseCredentialsPartition(ctx context.Context, env cloudassembly.Environment, mode Mode) (string, bool, error)
BaseCredentialsPartition returns the partition of the base credentials for env, and false when there are none: upstream's baseCredentialsPartition.
func (*Provider) CurrentAccount ¶
CurrentAccount returns the account cfg's credentials are for, such as a configuration Access gave out: upstream's SDK.currentAccount. It reads the account cache first, when there is one, and asks STS otherwise.
func (*Provider) DefaultAccount ¶
DefaultAccount returns the account of the default credentials, found with STS, and false when there are none or they don't work.
Expired credentials give a warning; any other failure gives a debug message. Unlike upstream, a failure is not remembered, so a long-lived toolkit picks up credentials the user refreshes (Epic D's ED-4).
func (*Provider) DefaultRegion ¶
DefaultRegion returns the region of the default configuration.
func (*Provider) ForEnvironment ¶
func (p *Provider) ForEnvironment(ctx context.Context, env cloudassembly.Environment, mode Mode, role *Role) (aws.Config, bool, error)
ForEnvironment returns the configuration for working in env, resolved, through role when it is not nil, and whether the role was assumed: upstream's forEnvironment.
Without a role, the base credentials must be for env's account, and are checked with STS before they are used. When assuming the role fails, the base credentials are used instead if they are for env's account, with a warning, and the result reports that no role was assumed.
It fails with a cdkerr.Error of Kind cdkerr.KindAuthentication and Code "NoCredentials", "IncorrectDefaultCredentials", "AssumeRoleFailed" or "AssumeRoleExpiredToken".
func (*Provider) Lookups ¶
func (p *Provider) Lookups() contextprovider.AWS
Lookups returns the AWS access p gives context providers.
func (*Provider) ResolveEnvironment ¶
func (p *Provider) ResolveEnvironment(ctx context.Context, env cloudassembly.Environment) (cloudassembly.Environment, error)
ResolveEnvironment replaces the unknown account or region of an environment-agnostic stack with the default account or region. It fails with a cdkerr.Error of Kind cdkerr.KindAuthentication and Code "UnresolvedAccount" when the account is unknown and there is no default account.
type Role ¶
type Role struct {
ARN string
ExternalID string
// AdditionalOptions are further STS AssumeRole parameters, as a JSON object with upstream's names, such as {"Tags": [{"Key": "k", "Value": "v"}]}.
AdditionalOptions jsontext.Value
}
Role is a role to assume in an environment, as a stack artifact describes its deploy or lookup role.
type Target ¶
type Target struct {
// Config is the configuration for working in the environment.
Config aws.Config
// Environment is the stack's environment, resolved.
Environment cloudassembly.Environment
// AssumedRole reports whether the stack's role was assumed.
AssumedRole bool
// FallbackCredentials reports that the stack names a role, assuming it failed, and the base credentials, which are for the right account, are used instead.
FallbackCredentials bool
}
Target is the access to a stack's environment that Access gives: upstream's TargetEnvironment.