Using AWS SDK for C++ credential providers (original) (raw)
To make requests to AWS using the AWS SDK for C++, the SDK uses cryptographically-signed credentials issued by AWS. At runtime, the SDK retrieves configuration values for credentials by checking several locations.
Authentication with AWS can be handled outside of your codebase. Many authentication methods can be automatically detected, used, and refreshed by the SDK using the credential provider chain.
For guided options for getting started on AWS authentication for your project, see Authentication and access in the AWS SDKs and Tools Reference Guide.
The credential provider chain
If you don't explicitly specify a credential provider when constructing a client, the SDK for C++ uses a credential provider chain that checks a series of places where you can supply credentials. Once the SDK finds credentials in one of these locations, the search stops.
Credential retrieval order
All SDKs have a series of places (or sources) that they check in order to get valid credentials to use to make a request to an AWS service. After valid credentials are found, the search is stopped. This systematic search is called the credential provider chain.
For each step in the chain, there are different ways to set the values. Setting values directly in code always takes precedence, followed by setting as environment variables, and then in the shared AWS config
file. For more information, see Precedence of settings in the_AWS SDKs and Tools Reference Guide_.
The SDK attempts to load credentials from the [default]
profile in the shared AWS config
andcredentials
files. You can use the AWS_PROFILE
environment variable to choose a named profile you want the SDK to load instead of using [default]
. The config
andcredentials
files are shared by AWS SDKs and tools. The AWS SDKs and Tools Reference Guide has information on SDK configuration settings used by all AWS SDKs and the AWS CLI. To learn more about how to configure the SDK through the shared AWS config
file, see Shared config and credentials files. To learn more about how to configure the SDK through setting environment variables, see Environment variables support.
To authenticate with AWS, the SDK for C++ checks the credential providers in the following order.
- AWS access keys (temporary and long-term credentials)
The SDK attempts to load credentials from theAWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
, andAWS_SESSION_TOKEN
environment variables, or from the shared AWScredentials
file.- For guidance on configuring this provider, see AWS access keys in the AWS SDKs and Tools Reference Guide.
- For details on SDK configuration properties for this provider, see AWS access keys in the_AWS SDKs and Tools Reference Guide_.
- AWS STS web identity
When creating mobile applications or client-based web applications that require access to AWS, AWS Security Token Service (AWS STS) returns a set of temporary security credentials for federated users who are authenticated through a public identity provider (IdP).- When you specify this in a profile, the SDK or tool attempts to retrieve temporary credentials using AWS STS
AssumeRoleWithWebIdentity
API method. For details on this method, see AssumeRoleWithWebIdentity in the_AWS Security Token Service API Reference_. - For guidance on configuring this provider, see Federate with web identity or OpenID Connect in the AWS SDKs and Tools Reference Guide.
- For details on SDK configuration properties for this provider, see Assume role credential provider in the_AWS SDKs and Tools Reference Guide_.
- When you specify this in a profile, the SDK or tool attempts to retrieve temporary credentials using AWS STS
- IAM Identity Center
If you use IAM Identity Center to authenticate, this is when the SDK for C++ uses the single sign-on token that was set up by running AWS CLI commandaws sso login
. The SDK uses the temporary credentials that the IAM Identity Center exchanged for a valid token. The SDK then uses the temporary credentials when it calls AWS services. For detailed information about this process, see Understand SDK credential resolution for AWS services in the AWS SDKs and Tools Reference Guide.- For guidance on configuring this provider, see IAM Identity Center authentication in the AWS SDKs and Tools Reference Guide.
- For details on SDK configuration properties for this provider, see IAM Identity Center credential provider in the_AWS SDKs and Tools Reference Guide_.
- External process provider
This provider can be used to provide custom implementations, such as retrieving credentials from an on-premises credentials store or integrating with your on-premises identify provider.- For guidance on one way to configure this provider, see IAM Roles Anywhere in the AWS SDKs and Tools Reference Guide.
- For details on SDK configuration properties for this provider, see Process credential provider in the_AWS SDKs and Tools Reference Guide_.
- Amazon ECS and Amazon EKS container credentials
Your Amazon Elastic Container Service tasks and Kubernetes service accounts can have an IAM role associated with them. The permissions granted in the IAM role are assumed by the containers running in the task or containers of the pod. This role allows your SDK for C++ application code (on the container) to use other AWS services.
The SDK attempts to retrieve credentials from theAWS_CONTAINER_CREDENTIALS_RELATIVE_URI
orAWS_CONTAINER_CREDENTIALS_FULL_URI
environment variables, which can be set automatically by Amazon ECS and Amazon EKS.- For details on setting up this role for Amazon ECS, see Amazon ECS task IAM role in the Amazon Elastic Container Service Developer Guide.
- For Amazon EKS setup information, see Setting up the Amazon EKS Pod Identity Agent in the _Amazon EKS User Guide.
- For details on SDK configuration properties for this provider, see Container credential provider in the_AWS SDKs and Tools Reference Guide_.
- Amazon EC2 Instance Metadata Service
Create an IAM role and attach it to your instance. The SDK for C++ application on the instance attempts to retrieve the credentials provided by the role from the instance metadata.- For details on setting up this role and using metadata, IAM roles for Amazon EC2 and Work with instance metadata in the Amazon EC2 User Guide.
- For details on SDK configuration properties for this provider, see IMDS credential provider in the_AWS SDKs and Tools Reference Guide_.
The credential provider chain can be reviewed at AWSCredentialsProviderChain in the AWS SDK for C++ source code on GitHub.
If you followed the recommended approach for new users to get started, you set up AWS IAM Identity Center authentication during Authenticating the AWS SDK for C++ with AWS of the Getting started topic. Other authentication methods are useful for different situations. To avoid security risks, we recommend always using short-term credentials. For other authentication method procedures, see Authentication and access in the_AWS SDKs and Tools Reference Guide_.
Explicit credential provider
Instead of relying on the credential provider chain to detect your authentication method, you can specify a specific credential provider that the SDK should use. You can do this by providing credentials in your service client's constructor.
The following example creates an Amazon Simple Storage Service client by directly providing temporary access credentials instead of using the chain.
SDKOptions options;
Aws::InitAPI(options);
{
const auto cred_provider = Aws::MakeShared<Auth::SimpleAWSCredentialsProvider>("TestAllocationTag",
"awsAccessKeyId",
"awsSecretKey",
"sessionToken");
S3Client client{cred_provider};
}
Aws::ShutdownAPI(options);
Identity caching
The SDK will cache credentials and other identity types such as SSO tokens. By default, the SDK uses a lazy cache implementation that loads credentials upon first request, caches them, and then attempts to refresh them during another request when they are close to expiring. Clients created from the same Aws::Client::ClientConfiguration share a cache.