Search in sources :

Example 1 with ProfileFile

use of software.amazon.awssdk.profiles.ProfileFile in project openhab-addons by openhab.

the class DynamoDBConfig method fromConfig.

/**
 * @param config persistence service configuration
 * @return DynamoDB configuration. Returns null in case of configuration errors
 */
@Nullable
public static DynamoDBConfig fromConfig(Map<String, Object> config) {
    ExpectedTableSchema tableRevision;
    try {
        String regionName = (String) config.get("region");
        if (regionName == null) {
            return null;
        }
        final Region region;
        if (Region.regions().stream().noneMatch(r -> r.toString().equals(regionName))) {
            LOGGER.warn("Region {} is not matching known regions: {}. The region might not be supported.", regionName, Region.regions().stream().map(r -> r.toString()).collect(Collectors.joining(", ")));
        }
        region = Region.of(regionName);
        Optional<RetryMode> retryMode = Optional.empty();
        AwsCredentials credentials;
        String accessKey = (String) config.get("accessKey");
        String secretKey = (String) config.get("secretKey");
        if (accessKey != null && !accessKey.isBlank() && secretKey != null && !secretKey.isBlank()) {
            LOGGER.debug("accessKey and secretKey specified. Using those.");
            credentials = AwsBasicCredentials.create(accessKey, secretKey);
        } else {
            LOGGER.debug("accessKey and/or secretKey blank. Checking profilesConfigFile and profile.");
            String profilesConfigFile = (String) config.get("profilesConfigFile");
            String profile = (String) config.get("profile");
            if (profilesConfigFile == null || profilesConfigFile.isBlank() || profile == null || profile.isBlank()) {
                LOGGER.error("Specify either 1) accessKey and secretKey; or 2) profilesConfigFile and " + "profile for providing AWS credentials");
                return null;
            }
            ProfileFile profileFile = ProfileFile.builder().content(Path.of(profilesConfigFile)).type(Type.CREDENTIALS).build();
            credentials = ProfileCredentialsProvider.builder().profileFile(profileFile).profileName(profile).build().resolveCredentials();
            retryMode = profileFile.profile(profile).flatMap(p -> p.property(ProfileProperty.RETRY_MODE)).flatMap(retry_mode -> {
                for (RetryMode value : RetryMode.values()) {
                    if (retry_mode.equalsIgnoreCase(value.name())) {
                        return Optional.of(value);
                    }
                }
                LOGGER.warn("Unknown retry_mode '{}' in profile. Ignoring and using SDK default retry mode.", retry_mode);
                return Optional.empty();
            });
            LOGGER.debug("Retry mode {}", retryMode);
        }
        String table = (String) config.get("table");
        String tablePrefixLegacy;
        if (table == null || table.isBlank()) {
            // the new parameter 'table' has not been set. Check whether the legacy parameter 'tablePrefix' is set
            table = DEFAULT_TABLE_NAME;
            tablePrefixLegacy = (String) config.get("tablePrefix");
            if (tablePrefixLegacy == null || tablePrefixLegacy.isBlank()) {
                LOGGER.debug("Using default table prefix {}", DEFAULT_TABLE_PREFIX);
                // No explicit value has been specified for tablePrefix, user could be still using the legacy setup
                tableRevision = ExpectedTableSchema.MAYBE_LEGACY;
                tablePrefixLegacy = DEFAULT_TABLE_PREFIX;
            } else {
                // Explicit value for tablePrefix, user certainly prefers LEGACY
                tableRevision = ExpectedTableSchema.LEGACY;
            }
        } else {
            tableRevision = ExpectedTableSchema.NEW;
            tablePrefixLegacy = DEFAULT_TABLE_PREFIX;
        }
        final long readCapacityUnits;
        String readCapacityUnitsParam = (String) config.get("readCapacityUnits");
        if (readCapacityUnitsParam == null || readCapacityUnitsParam.isBlank()) {
            readCapacityUnits = DEFAULT_READ_CAPACITY_UNITS;
        } else {
            readCapacityUnits = Long.parseLong(readCapacityUnitsParam);
        }
        final long writeCapacityUnits;
        String writeCapacityUnitsParam = (String) config.get("writeCapacityUnits");
        if (writeCapacityUnitsParam == null || writeCapacityUnitsParam.isBlank()) {
            writeCapacityUnits = DEFAULT_WRITE_CAPACITY_UNITS;
        } else {
            writeCapacityUnits = Long.parseLong(writeCapacityUnitsParam);
        }
        @Nullable final Integer expireDays;
        String expireDaysString = (String) config.get("expireDays");
        if (expireDaysString == null || expireDaysString.isBlank()) {
            expireDays = null;
        } else {
            expireDays = Integer.parseInt(expireDaysString);
            if (expireDays <= 0) {
                LOGGER.error("expireDays should be positive integer or null");
                return null;
            }
        }
        switch(tableRevision) {
            case NEW:
                LOGGER.debug("Using new DynamoDB table schema");
                return DynamoDBConfig.newSchema(region, credentials, retryMode.map(AwsRetryPolicy::forRetryMode), table, readCapacityUnits, writeCapacityUnits, expireDays);
            case LEGACY:
                LOGGER.warn("Using legacy DynamoDB table schema. It is recommended to transition to new schema by defining 'table' parameter and not configuring 'tablePrefix'");
                return DynamoDBConfig.legacySchema(region, credentials, retryMode.map(AwsRetryPolicy::forRetryMode), tablePrefixLegacy, readCapacityUnits, writeCapacityUnits);
            case MAYBE_LEGACY:
                LOGGER.debug("Unclear whether we should use new legacy DynamoDB table schema. It is recommended to explicitly define new 'table' parameter. The correct table schema will be detected at runtime.");
                return DynamoDBConfig.maybeLegacySchema(region, credentials, retryMode.map(AwsRetryPolicy::forRetryMode), table, tablePrefixLegacy, readCapacityUnits, writeCapacityUnits, expireDays);
            default:
                throw new IllegalStateException("Unhandled enum. Bug");
        }
    } catch (Exception e) {
        LOGGER.error("Error with configuration: {} {}", e.getClass().getSimpleName(), e.getMessage());
        return null;
    }
}
Also used : RetryMode(software.amazon.awssdk.core.retry.RetryMode) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) AwsRetryPolicy(software.amazon.awssdk.awscore.retry.AwsRetryPolicy) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Logger(org.slf4j.Logger) Type(software.amazon.awssdk.profiles.ProfileFile.Type) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) LoggerFactory(org.slf4j.LoggerFactory) ProfileCredentialsProvider(software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider) Collectors(java.util.stream.Collectors) RetryPolicy(software.amazon.awssdk.core.retry.RetryPolicy) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) Optional(java.util.Optional) ProfileProperty(software.amazon.awssdk.profiles.ProfileProperty) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) RetryMode(software.amazon.awssdk.core.retry.RetryMode) Region(software.amazon.awssdk.regions.Region) Path(java.nio.file.Path) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) Region(software.amazon.awssdk.regions.Region) Nullable(org.eclipse.jdt.annotation.Nullable) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 2 with ProfileFile

use of software.amazon.awssdk.profiles.ProfileFile in project aws-sdk-java-v2 by aws.

the class ClientRetryModeTestSuite method retryModeCanBeSetByProfileFile.

@Test
public void retryModeCanBeSetByProfileFile() {
    ProfileFile profileFile = ProfileFile.builder().content(new StringInputStream("[profile foo]\n" + "retry_mode = standard")).type(ProfileFile.Type.CONFIGURATION).build();
    stubThrottlingResponse();
    ClientT client = clientBuilder().overrideConfiguration(o -> o.defaultProfileFile(profileFile).defaultProfileName("foo")).build();
    assertThatThrownBy(() -> callAllTypes(client)).isInstanceOf(SdkException.class);
    verifyRequestCount(3);
}
Also used : ProfileFile(software.amazon.awssdk.profiles.ProfileFile) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SdkException(software.amazon.awssdk.core.exception.SdkException) Test(org.junit.Test) AllTypesResponse(software.amazon.awssdk.services.protocolrestjson.model.AllTypesResponse) WireMock.verify(com.github.tomakehurst.wiremock.client.WireMock.verify) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) WireMock.anyRequestedFor(com.github.tomakehurst.wiremock.client.WireMock.anyRequestedFor) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) WireMockRule(com.github.tomakehurst.wiremock.junit.WireMockRule) AwsClientBuilder(software.amazon.awssdk.awscore.client.builder.AwsClientBuilder) StringInputStream(software.amazon.awssdk.utils.StringInputStream) Rule(org.junit.Rule) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) WireMock.anyUrl(com.github.tomakehurst.wiremock.client.WireMock.anyUrl) URI(java.net.URI) WireMock.post(com.github.tomakehurst.wiremock.client.WireMock.post) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) RetryMode(software.amazon.awssdk.core.retry.RetryMode) Region(software.amazon.awssdk.regions.Region) ExecutorService(java.util.concurrent.ExecutorService) StringInputStream(software.amazon.awssdk.utils.StringInputStream) ProfileFile(software.amazon.awssdk.profiles.ProfileFile) Test(org.junit.Test)

Example 3 with ProfileFile

use of software.amazon.awssdk.profiles.ProfileFile in project aws-sdk-java-v2 by aws.

the class AwsDefaultClientBuilder method resolveUseDualstackFromDefaultProvider.

/**
 * Load the dualstack endpoint setting from the default provider logic.
 */
private Boolean resolveUseDualstackFromDefaultProvider(SdkClientConfiguration config) {
    ProfileFile profileFile = config.option(SdkClientOption.PROFILE_FILE);
    String profileName = config.option(SdkClientOption.PROFILE_NAME);
    return DualstackEnabledProvider.builder().profileFile(() -> profileFile).profileName(profileName).build().isDualstackEnabled().orElse(null);
}
Also used : ProfileFile(software.amazon.awssdk.profiles.ProfileFile)

Example 4 with ProfileFile

use of software.amazon.awssdk.profiles.ProfileFile in project aws-sdk-java-v2 by aws.

the class DefaultServiceEndpointBuilder method getServiceEndpoint.

public URI getServiceEndpoint() {
    if (profileFile == null) {
        profileFile = new Lazy<>(ProfileFile::defaultProfileFile)::getValue;
    }
    if (profileName == null) {
        profileName = ProfileFileSystemSetting.AWS_PROFILE.getStringValueOrThrow();
    }
    if (dualstackEnabled == null) {
        dualstackEnabled = DualstackEnabledProvider.builder().profileFile(profileFile).profileName(profileName).build().isDualstackEnabled().orElse(false);
    }
    if (fipsEnabled == null) {
        fipsEnabled = FipsEnabledProvider.builder().profileFile(profileFile).profileName(profileName).build().isFipsEnabled().orElse(false);
    }
    List<EndpointTag> endpointTags = new ArrayList<>();
    if (dualstackEnabled) {
        endpointTags.add(EndpointTag.DUALSTACK);
    }
    if (fipsEnabled) {
        endpointTags.add(EndpointTag.FIPS);
    }
    ServiceMetadata serviceMetadata = ServiceMetadata.of(serviceName).reconfigure(c -> c.profileFile(profileFile).profileName(profileName).advancedOptions(advancedOptions));
    URI endpoint = addProtocolToServiceEndpoint(serviceMetadata.endpointFor(ServiceEndpointKey.builder().region(region).tags(endpointTags).build()));
    if (endpoint.getHost() == null) {
        String error = "Configured region (" + region + ") and tags (" + endpointTags + ") resulted in an invalid URI: " + endpoint + ". This is usually caused by an invalid region configuration.";
        List<Region> exampleRegions = serviceMetadata.regions();
        if (!exampleRegions.isEmpty()) {
            error += " Valid regions: " + exampleRegions;
        }
        throw SdkClientException.create(error);
    }
    return endpoint;
}
Also used : Lazy(software.amazon.awssdk.utils.Lazy) EndpointTag(software.amazon.awssdk.regions.EndpointTag) ArrayList(java.util.ArrayList) Region(software.amazon.awssdk.regions.Region) URI(java.net.URI) ServiceMetadata(software.amazon.awssdk.regions.ServiceMetadata) ProfileFile(software.amazon.awssdk.profiles.ProfileFile)

Example 5 with ProfileFile

use of software.amazon.awssdk.profiles.ProfileFile in project aws-sdk-java-v2 by aws.

the class DualstackEnabledProvider method isDualstackEnabled.

/**
 * Returns true when dualstack should be used, false when dualstack should not be used, and empty when there is no global
 * dualstack configuration.
 */
public Optional<Boolean> isDualstackEnabled() {
    Optional<Boolean> setting = SdkSystemSetting.AWS_USE_DUALSTACK_ENDPOINT.getBooleanValue();
    if (setting.isPresent()) {
        return setting;
    }
    ProfileFile profileFile = this.profileFile.get();
    Optional<Profile> profile = profileFile.profile(profileName());
    return profile.flatMap(p -> p.booleanProperty(ProfileProperty.USE_DUALSTACK_ENDPOINT));
}
Also used : Profile(software.amazon.awssdk.profiles.Profile) ProfileFile(software.amazon.awssdk.profiles.ProfileFile)

Aggregations

ProfileFile (software.amazon.awssdk.profiles.ProfileFile)47 Test (org.junit.jupiter.api.Test)22 StringInputStream (software.amazon.awssdk.utils.StringInputStream)20 Test (org.junit.Test)14 ProcessCredentialsProviderTest (software.amazon.awssdk.auth.credentials.ProcessCredentialsProviderTest)11 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 ProfileCredentialsUtils (software.amazon.awssdk.auth.credentials.internal.ProfileCredentialsUtils)9 Region (software.amazon.awssdk.regions.Region)9 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)8 URI (java.net.URI)7 Optional (java.util.Optional)6 Arrays (java.util.Arrays)5 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)5 AwsCredentials (software.amazon.awssdk.auth.credentials.AwsCredentials)5 ExecutionInterceptor (software.amazon.awssdk.core.interceptor.ExecutionInterceptor)5 Profile (software.amazon.awssdk.profiles.Profile)5 SdkAutoCloseable (software.amazon.awssdk.utils.SdkAutoCloseable)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 AwsBasicCredentials (software.amazon.awssdk.auth.credentials.AwsBasicCredentials)4