Skip to content

Merge credentials reloading feature branch to mainline #3712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AWSSDKforJavav2-688d30e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "AWS SDK for Java v2",
"contributor": "",
"type": "feature",
"description": "- ProfileCredentialsProvider and ProfileTokenProvider can reload credentials when disk profile changes\n- Updated DefaultCredentialsProvider chain for reloading credentials\n- Service support classes store ProfileFile as a Supplier interface\n- SdkClientOption and SdkExecutionAttributes constants for PROFILE_FILE have been deprecated in favor of PROFILE_FILE_SUPPLIER"
}
Original file line number Diff line number Diff line change
Expand Up @@ -365,15 +365,13 @@ private MethodSpec finalizeServiceConfigurationMethod() {

private void mergeServiceConfiguration(MethodSpec.Builder builder, String clientConfigClassName) {
ClassName clientConfigClass = ClassName.bestGuess(clientConfigClassName);
builder.addCode("$1T.Builder serviceConfigBuilder = (($1T) config.option($2T.SERVICE_CONFIGURATION)).toBuilder();" +
"serviceConfigBuilder.profileFile(serviceConfigBuilder.profileFile() "
+ "!= null ? serviceConfigBuilder.profileFile() : config.option($2T.PROFILE_FILE));" +
"serviceConfigBuilder.profileName(serviceConfigBuilder.profileName() "
builder.addCode("$1T.Builder serviceConfigBuilder = (($1T) config.option($2T.SERVICE_CONFIGURATION)).toBuilder();"
+ "serviceConfigBuilder.profileFile(serviceConfigBuilder.profileFileSupplier() != null ? "
+ "serviceConfigBuilder.profileFileSupplier() : config.option($2T.PROFILE_FILE_SUPPLIER));"
+ "serviceConfigBuilder.profileName(serviceConfigBuilder.profileName() "
+ "!= null ? serviceConfigBuilder.profileName() : config.option($2T.PROFILE_NAME));",
clientConfigClass, SdkClientOption.class);



if (model.getCustomizationConfig().getServiceConfig().hasDualstackProperty()) {
builder.addCode("if (serviceConfigBuilder.dualstackEnabled() != null) {")
.addCode(" $T.validState(config.option($T.DUALSTACK_ENDPOINT_ENABLED) == null, \"Dualstack has been "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ protected final SdkClientConfiguration finalizeServiceConfiguration(SdkClientCon
interceptors = CollectionUtils.mergeLists(interceptors, config.option(SdkClientOption.EXECUTION_INTERCEPTORS));
ServiceConfiguration.Builder serviceConfigBuilder = ((ServiceConfiguration) config
.option(SdkClientOption.SERVICE_CONFIGURATION)).toBuilder();
serviceConfigBuilder.profileFile(serviceConfigBuilder.profileFile() != null ? serviceConfigBuilder.profileFile() : config
.option(SdkClientOption.PROFILE_FILE));
serviceConfigBuilder.profileFile(serviceConfigBuilder.profileFileSupplier() != null ?
serviceConfigBuilder.profileFileSupplier() :
config.option(SdkClientOption.PROFILE_FILE_SUPPLIER));
serviceConfigBuilder.profileName(serviceConfigBuilder.profileName() != null ? serviceConfigBuilder.profileName() : config
.option(SdkClientOption.PROFILE_NAME));
if (serviceConfigBuilder.dualstackEnabled() != null) {
Expand Down
6 changes: 6 additions & 0 deletions core/auth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<version>${jimfs.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
public interface AwsCredentialsProvider {
/**
* Returns {@link AwsCredentials} that can be used to authorize an AWS request. Each implementation of AWSCredentialsProvider
* can chose its own strategy for loading credentials. For example, an implementation might load credentials from an existing
* can choose its own strategy for loading credentials. For example, an implementation might load credentials from an existing
* key management system, or load new credentials when credentials are rotated.
*
* <p>If an error occurs during the loading of credentials or credentials could not be found, a runtime exception will be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@

package software.amazon.awssdk.auth.credentials;

import java.util.Optional;
import java.util.function.Supplier;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.auth.credentials.internal.LazyAwsCredentialsProvider;
import software.amazon.awssdk.profiles.ProfileFile;
import software.amazon.awssdk.profiles.ProfileFileSupplier;
import software.amazon.awssdk.utils.SdkAutoCloseable;
import software.amazon.awssdk.utils.ToString;
import software.amazon.awssdk.utils.builder.CopyableBuilder;
Expand All @@ -26,8 +29,8 @@
/**
* AWS credentials provider chain that looks for credentials in this order:
* <ol>
* <li>Java System Properties - <code>aws.accessKeyId</code> and <code>aws.secretAccessKey</code></li>
* <li>Environment Variables - <code>AWS_ACCESS_KEY_ID</code> and <code>AWS_SECRET_ACCESS_KEY</code></li>
* <li>Java System Properties - {@code aws.accessKeyId} and {@code aws.secretAccessKey}</li>
* <li>Environment Variables - {@code AWS_ACCESS_KEY_ID} and {@code AWS_SECRET_ACCESS_KEY}</li>
* <li>Web Identity Token credentials from system properties or environment variables</li>
* <li>Credential profiles file at the default location (~/.aws/credentials) shared by all AWS SDKs and the AWS CLI</li>
* <li>Credentials delivered through the Amazon EC2 container service if AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" environment
Expand All @@ -51,7 +54,7 @@ public final class DefaultCredentialsProvider

private final LazyAwsCredentialsProvider providerChain;

private final ProfileFile profileFile;
private final Supplier<ProfileFile> profileFile;

private final String profileName;

Expand Down Expand Up @@ -87,21 +90,21 @@ private static LazyAwsCredentialsProvider createChain(Builder builder) {

return LazyAwsCredentialsProvider.create(() -> {
AwsCredentialsProvider[] credentialsProviders = new AwsCredentialsProvider[] {
SystemPropertyCredentialsProvider.create(),
EnvironmentVariableCredentialsProvider.create(),
WebIdentityTokenFileCredentialsProvider.create(),
ProfileCredentialsProvider.builder()
.profileFile(builder.profileFile)
.profileName(builder.profileName)
.build(),
ContainerCredentialsProvider.builder()
.asyncCredentialUpdateEnabled(asyncCredentialUpdateEnabled)
.build(),
InstanceProfileCredentialsProvider.builder()
.asyncCredentialUpdateEnabled(asyncCredentialUpdateEnabled)
.profileFile(builder.profileFile)
.profileName(builder.profileName)
.build()
SystemPropertyCredentialsProvider.create(),
EnvironmentVariableCredentialsProvider.create(),
WebIdentityTokenFileCredentialsProvider.create(),
ProfileCredentialsProvider.builder()
.profileFile(builder.profileFile)
.profileName(builder.profileName)
.build(),
ContainerCredentialsProvider.builder()
.asyncCredentialUpdateEnabled(asyncCredentialUpdateEnabled)
.build(),
InstanceProfileCredentialsProvider.builder()
.asyncCredentialUpdateEnabled(asyncCredentialUpdateEnabled)
.profileFile(builder.profileFile)
.profileName(builder.profileName)
.build()
};

return AwsCredentialsProviderChain.builder()
Expand Down Expand Up @@ -144,7 +147,7 @@ public Builder toBuilder() {
* Configuration that defines the {@link DefaultCredentialsProvider}'s behavior.
*/
public static final class Builder implements CopyableBuilder<Builder, DefaultCredentialsProvider> {
private ProfileFile profileFile;
private Supplier<ProfileFile> profileFile;
private String profileName;
private Boolean reuseLastProviderEnabled = true;
private Boolean asyncCredentialUpdateEnabled = false;
Expand All @@ -163,7 +166,13 @@ private Builder(DefaultCredentialsProvider credentialsProvider) {
}

public Builder profileFile(ProfileFile profileFile) {
this.profileFile = profileFile;
return profileFile(Optional.ofNullable(profileFile)
.map(ProfileFileSupplier::fixedProfileFile)
.orElse(null));
}

public Builder profileFile(Supplier<ProfileFile> profileFileSupplier) {
this.profileFile = profileFileSupplier;
return this;
}

Expand Down Expand Up @@ -198,6 +207,7 @@ public Builder asyncCredentialUpdateEnabled(Boolean asyncCredentialUpdateEnabled
/**
* Create a {@link DefaultCredentialsProvider} using the configuration defined in this builder.
*/
@Override
public DefaultCredentialsProvider build() {
return new DefaultCredentialsProvider(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.time.Instant;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.annotations.SdkTestInternalApi;
import software.amazon.awssdk.auth.credentials.internal.Ec2MetadataConfigProvider;
Expand All @@ -36,6 +38,7 @@
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.exception.SdkServiceException;
import software.amazon.awssdk.profiles.ProfileFile;
import software.amazon.awssdk.profiles.ProfileFileSupplier;
import software.amazon.awssdk.profiles.ProfileFileSystemSetting;
import software.amazon.awssdk.regions.util.HttpResourcesUtils;
import software.amazon.awssdk.regions.util.ResourcesEndpointProvider;
Expand Down Expand Up @@ -77,7 +80,7 @@ public final class InstanceProfileCredentialsProvider

private final String asyncThreadName;

private final ProfileFile profileFile;
private final Supplier<ProfileFile> profileFile;

private final String profileName;

Expand All @@ -95,8 +98,8 @@ private InstanceProfileCredentialsProvider(BuilderImpl builder) {
this.httpCredentialsLoader = HttpCredentialsLoader.create();
this.configProvider =
Ec2MetadataConfigProvider.builder()
.profileFile(builder.profileFile == null ? null : () -> builder.profileFile)
.profileName(builder.profileName == null ? null : builder.profileName)
.profileFile(builder.profileFile)
.profileName(builder.profileName)
.build();

if (Boolean.TRUE.equals(builder.asyncCredentialUpdateEnabled)) {
Expand Down Expand Up @@ -279,9 +282,19 @@ public interface Builder extends HttpCredentialsProvider.Builder<InstanceProfile
* Configure the profile file used for loading IMDS-related configuration, like the endpoint mode (IPv4 vs IPv6).
*
* <p>By default, {@link ProfileFile#defaultProfileFile()} is used.
*
* @see #profileFile(Supplier)
*/
Builder profileFile(ProfileFile profileFile);

/**
* Define the mechanism for loading profile files.
*
* @param profileFileSupplier Supplier interface for generating a ProfileFile instance.
* @see #profileFile(ProfileFile)
*/
Builder profileFile(Supplier<ProfileFile> profileFileSupplier);

/**
* Configure the profile name used for loading IMDS-related configuration, like the endpoint mode (IPv4 vs IPv6).
*
Expand All @@ -292,6 +305,7 @@ public interface Builder extends HttpCredentialsProvider.Builder<InstanceProfile
/**
* Build a {@link InstanceProfileCredentialsProvider} from the provided configuration.
*/
@Override
InstanceProfileCredentialsProvider build();
}

Expand All @@ -301,7 +315,7 @@ static final class BuilderImpl implements Builder {
private String endpoint;
private Boolean asyncCredentialUpdateEnabled;
private String asyncThreadName;
private ProfileFile profileFile;
private Supplier<ProfileFile> profileFile;
private String profileName;

private BuilderImpl() {
Expand Down Expand Up @@ -354,14 +368,25 @@ public void setAsyncThreadName(String asyncThreadName) {

@Override
public Builder profileFile(ProfileFile profileFile) {
this.profileFile = profileFile;
return this;
return profileFile(Optional.ofNullable(profileFile)
.map(ProfileFileSupplier::fixedProfileFile)
.orElse(null));
}

public void setProfileFile(ProfileFile profileFile) {
profileFile(profileFile);
}

@Override
public Builder profileFile(Supplier<ProfileFile> profileFileSupplier) {
this.profileFile = profileFileSupplier;
return this;
}

public void setProfileFile(Supplier<ProfileFile> profileFileSupplier) {
profileFile(profileFileSupplier);
}

@Override
public Builder profileName(String profileName) {
this.profileName = profileName;
Expand Down
Loading