-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
65 changed files
with
5,472 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
subprojects/micronaut-amazon-awssdk-core/micronaut-amazon-awssdk-core.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
58 changes: 58 additions & 0 deletions
58
...ssdk-core/src/main/java/com/agorapulse/micronaut/amazon/awssdk/core/AwsConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...ore/src/main/java/com/agorapulse/micronaut/amazon/awssdk/core/BasicAwsRegionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...va/com/agorapulse/micronaut/amazon/awssdk/core/DefaultRegionAndEndpointConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
97 changes: 97 additions & 0 deletions
97
...n/java/com/agorapulse/micronaut/amazon/awssdk/core/EnvironmentAwsCredentialsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...c/main/java/com/agorapulse/micronaut/amazon/awssdk/core/EnvironmentAwsRegionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...main/java/com/agorapulse/micronaut/amazon/awssdk/core/RegionAndEndpointConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.