Skip to content

Commit

Permalink
Merge 40ab424 into 0414d2f
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Apr 8, 2020
2 parents 0414d2f + 40ab424 commit f1bba42
Show file tree
Hide file tree
Showing 65 changed files with 5,472 additions and 14 deletions.
15 changes: 8 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@

slug=agorapulse/micronaut-aws-sdk
group=com.agorapulse
micronautVersion = 1.3.2
gruVersion = 0.8.2
micronautVersion = 1.3.3
gruVersion = 0.8.3
druVersion = 0.6.0
groovyVersion = 2.5.6
groovyVersion = 2.5.10
spockVersion = 1.3-groovy-2.5
awsSdkVersion = 1.11.714
awsSdk2Version = 2.10.56
testcontainersVersion = 1.12.1
kordampVersion=0.31.2
awsSdkVersion = 1.11.754
awsSdk2Version = 2.11.10
testcontainersVersion = 1.12.5
kordampVersion=0.32.0
closureSupportVersion=0.6.0

# this should be aligned to Micronaut version
# required for AWS CBOR marshalling
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2020 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
config {
bintray {
enabled = true
}
}

dependencies {
compile "software.amazon.awssdk:aws-core"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2020 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.agorapulse.micronaut.amazon.awssdk.core;

import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.env.Environment;
import software.amazon.awssdk.auth.credentials.*;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.regions.providers.*;

import javax.inject.Singleton;

@Factory
public class AwsConfiguration {

private static final Region DEFAULT_REGION = Region.EU_WEST_1;

@Bean
@Singleton
AwsCredentialsProvider defaultAwsCredentialsProvider(Environment environment) {
return AwsCredentialsProviderChain.of(
EnvironmentAwsCredentialsProvider.create(environment),
EnvironmentVariableCredentialsProvider.create(),
SystemPropertyCredentialsProvider.create(),
ProfileCredentialsProvider.create(),
InstanceProfileCredentialsProvider.create()
);
}

@Bean
@Singleton
AwsRegionProvider defaultAwsRegionProvider(Environment environment) {
return new AwsRegionProviderChain(
new EnvironmentAwsRegionProvider(environment),
new SystemSettingsRegionProvider(),
new AwsProfileRegionProvider(),
new InstanceProfileRegionProvider(),
new BasicAwsRegionProvider(DEFAULT_REGION)
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2020 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.agorapulse.micronaut.amazon.awssdk.core;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.regions.providers.AwsRegionProvider;


public class BasicAwsRegionProvider implements AwsRegionProvider {

private final Region region;

public BasicAwsRegionProvider(Region region) {
this.region = region;
}

@Override
public Region getRegion() {
return region;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2020 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.agorapulse.micronaut.amazon.awssdk.core;

import javax.annotation.Nullable;

/**
* Default region and endpoint configuration.
*/
public class DefaultRegionAndEndpointConfiguration implements RegionAndEndpointConfiguration {

public String getRegion() {
return region;
}

public void setRegion(String region) {
this.region = region;
}

public String getEndpoint() {
return endpoint;
}

public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}

@Nullable private String region;
@Nullable private String endpoint;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2020 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.agorapulse.micronaut.amazon.awssdk.core;

import io.micronaut.context.env.Environment;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.utils.StringUtils;

/**
* A {@link AwsCredentialsProvider} that reads from the {@link Environment}.
*/
public class EnvironmentAwsCredentialsProvider implements AwsCredentialsProvider {

/**
* Environment variable name for the AWS access key ID.
*/
public static final String ACCESS_KEY_ENV_VAR = "aws.access-key-id";

/**
* Alternate environment variable name for the AWS access key ID.
*/
public static final String ALTERNATE_ACCESS_KEY_ENV_VAR = "aws.access-key";

/**
* Environment variable name for the AWS secret key.
*/
public static final String SECRET_KEY_ENV_VAR = "aws.secret-key";

/**
* Alternate environment variable name for the AWS secret key.
*/
public static final String ALTERNATE_SECRET_KEY_ENV_VAR = "aws.secret-access-key";

/**
* Environment variable name for the AWS session token.
*/
public static final String AWS_SESSION_TOKEN_ENV_VAR = "aws.session-token";

public static AwsCredentialsProvider create(Environment environment) {
return new EnvironmentAwsCredentialsProvider(environment);
}

private final Environment environment;

/**
* Constructor.
* @param environment environment
*/
private EnvironmentAwsCredentialsProvider(Environment environment) {
this.environment = environment;
}

@Override
public AwsCredentials resolveCredentials() {
String accessKey = environment.getProperty(ACCESS_KEY_ENV_VAR, String.class, environment.getProperty(ALTERNATE_ACCESS_KEY_ENV_VAR, String.class, (String) null));

String secretKey = environment.getProperty(SECRET_KEY_ENV_VAR, String.class, environment.getProperty(ALTERNATE_SECRET_KEY_ENV_VAR, String.class, (String) null));
accessKey = StringUtils.trim(accessKey);
secretKey = StringUtils.trim(secretKey);
String sessionToken = StringUtils.trim(environment.getProperty(AWS_SESSION_TOKEN_ENV_VAR, String.class, (String) null));

if (StringUtils.isNotBlank(accessKey) || StringUtils.isNotBlank(secretKey)) {
throw SdkClientException.create(
"Unable to load AWS credentials from environment "
+ "(" + ACCESS_KEY_ENV_VAR + " (or " + ALTERNATE_ACCESS_KEY_ENV_VAR + ") and "
+ SECRET_KEY_ENV_VAR + " (or " + ALTERNATE_SECRET_KEY_ENV_VAR + "))");
}

return sessionToken == null
? AwsBasicCredentials.create(accessKey, secretKey)
: AwsSessionCredentials.create(accessKey, secretKey, sessionToken);
}

@Override
public String toString() {
return getClass().getSimpleName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2020 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.agorapulse.micronaut.amazon.awssdk.core;

import io.micronaut.context.env.Environment;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.regions.providers.AwsRegionProvider;

/**
* A {@link AwsRegionProvider} that reads from the {@link Environment}.
*
* @since 1.0.0
*/
public class EnvironmentAwsRegionProvider implements AwsRegionProvider {

/**
* Environment variable name for the AWS access key ID.
*/
public static final String REGION_ENV_VAR = "aws.region";

private final Environment environment;

/**
* Constructor.
* @param environment environment
*/
public EnvironmentAwsRegionProvider(Environment environment) {
this.environment = environment;
}

@Override
public Region getRegion() {
return environment.getProperty(REGION_ENV_VAR, String.class).map(Region::of).orElse(null);
}

@Override
public String toString() {
return getClass().getSimpleName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2020 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.agorapulse.micronaut.amazon.awssdk.core;

import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.regions.providers.AwsRegionProvider;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Optional;

public interface RegionAndEndpointConfiguration {

String getRegion();

String getEndpoint();

default <C, B extends AwsClientBuilder<B, C>> B configure(B builder, AwsRegionProvider awsRegionProvider) {
Region region = Optional.ofNullable(getRegion()).map(Region::of).orElseGet(awsRegionProvider::getRegion);
if (getEndpoint() != null) {
try {
builder.endpointOverride(new URI(getEndpoint()));
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Endpoint configured for " + getClass() + " is not a valid URI!", e);
}
} else {
builder.region(region);
}
return builder;
}

}

0 comments on commit f1bba42

Please sign in to comment.