This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit cfa5189d83e1eff0afc738988f827f0603598f63 Author: Andrea Cosentino <[email protected]> AuthorDate: Tue Feb 15 13:43:25 2022 +0100 CAMEL-17644 - Support ability to load properties from Vault/Secrets cloud services - aws secrets manager --- .../org/apache/camel/properties-function/aws | 2 + .../SecretsManagerPropertiesFunction.java | 65 +++++++++++++--------- .../SecretsManagerPropertiesSourceTestIT.java | 13 ++--- core/camel-base/pom.xml | 7 --- .../component/properties/PropertiesComponent.java | 1 - .../properties/PropertiesFunctionResolver.java | 2 - 6 files changed, 44 insertions(+), 46 deletions(-) diff --git a/components/camel-aws/camel-aws-secrets-manager/src/generated/resources/META-INF/services/org/apache/camel/properties-function/aws b/components/camel-aws/camel-aws-secrets-manager/src/generated/resources/META-INF/services/org/apache/camel/properties-function/aws new file mode 100644 index 0000000..5ed0bc2 --- /dev/null +++ b/components/camel-aws/camel-aws-secrets-manager/src/generated/resources/META-INF/services/org/apache/camel/properties-function/aws @@ -0,0 +1,2 @@ +# Generated by camel build tools - do NOT edit this file! +class=org.apache.camel.component.aws.secretsmanager.SecretsManagerPropertiesFunction diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/AWSSecretsManagerPropertiesFunction.java b/components/camel-aws/camel-aws-secrets-manager/src/main/java/org/apache/camel/component/aws/secretsmanager/SecretsManagerPropertiesFunction.java similarity index 65% rename from core/camel-base/src/main/java/org/apache/camel/component/properties/AWSSecretsManagerPropertiesFunction.java rename to components/camel-aws/camel-aws-secrets-manager/src/main/java/org/apache/camel/component/aws/secretsmanager/SecretsManagerPropertiesFunction.java index 91c4a14..8cae227 100644 --- a/core/camel-base/src/main/java/org/apache/camel/component/properties/AWSSecretsManagerPropertiesFunction.java +++ b/components/camel-aws/camel-aws-secrets-manager/src/main/java/org/apache/camel/component/aws/secretsmanager/SecretsManagerPropertiesFunction.java @@ -14,11 +14,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.component.properties; +package org.apache.camel.component.aws.secretsmanager; import java.util.Base64; import org.apache.camel.spi.PropertiesFunction; +import org.apache.camel.support.service.ServiceSupport; import org.apache.camel.util.StringHelper; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; @@ -43,11 +44,35 @@ import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRespon * Properties Function are all prefixed with <tt>aws:</tt>. For example asking for <tt>aws:token</tt>, will return the * secret value associated the secret named token on AWS Secrets Manager */ -public class AWSSecretsManagerPropertiesFunction implements PropertiesFunction { + [email protected]("aws") +public class SecretsManagerPropertiesFunction extends ServiceSupport implements PropertiesFunction { private static final String ACCESS_KEY = "AWS_ACCESS_KEY"; private static final String SECRET_KEY = "AWS_SECRET_KEY"; private static final String REGION = "AWS_REGION"; + private SecretsManagerClient client = null; + + @Override + protected void doStart() throws Exception { + super.doStart(); + String accessKey = System.getenv(ACCESS_KEY); + String secretKey = System.getenv(SECRET_KEY); + String region = System.getenv(REGION); + SecretsManagerClientBuilder clientBuilder = SecretsManagerClient.builder(); + AwsBasicCredentials cred = AwsBasicCredentials.create(accessKey, secretKey); + clientBuilder = clientBuilder.credentialsProvider(StaticCredentialsProvider.create(cred)); + clientBuilder.region(Region.of(region)); + client = clientBuilder.build(); + } + + @Override + protected void doStop() throws Exception { + if (client != null) { + client.close(); + } + super.doStop(); + } @Override public String getName() { @@ -59,45 +84,31 @@ public class AWSSecretsManagerPropertiesFunction implements PropertiesFunction { String key = remainder; String returnValue = null; - SecretsManagerClient client = null; - SecretsManagerClientBuilder clientBuilder = SecretsManagerClient.builder(); - if (remainder.contains(":")) { key = StringHelper.before(remainder, ":"); } // make sure to use upper case if (key != null) { - // a service should have both the host and port defined - String accessKey = System.getenv(ACCESS_KEY); - String secretKey = System.getenv(SECRET_KEY); - String region = System.getenv(REGION); - returnValue = getSecretFromSource(key, returnValue, clientBuilder, accessKey, secretKey, region); + returnValue = getSecretFromSource(key); } return returnValue; } private String getSecretFromSource( - String key, String returnValue, SecretsManagerClientBuilder clientBuilder, String accessKey, String secretKey, - String region) { + String key) { + String returnValue; GetSecretValueRequest request; - SecretsManagerClient client; - if (accessKey != null && secretKey != null && region != null) { - AwsBasicCredentials cred = AwsBasicCredentials.create(accessKey, secretKey); - clientBuilder = clientBuilder.credentialsProvider(StaticCredentialsProvider.create(cred)); - clientBuilder.region(Region.of(region)); - client = clientBuilder.build(); - GetSecretValueRequest.Builder builder = GetSecretValueRequest.builder(); - builder.secretId(key); - request = builder.build(); - GetSecretValueResponse secret = client.getSecretValue(request); + GetSecretValueRequest.Builder builder = GetSecretValueRequest.builder(); + builder.secretId(key); + request = builder.build(); + GetSecretValueResponse secret = client.getSecretValue(request); + returnValue = secret.secretString(); + if (secret.secretString() != null) { returnValue = secret.secretString(); - if (secret.secretString() != null) { - returnValue = secret.secretString(); - } else { - returnValue = new String(Base64.getDecoder().decode(secret.secretBinary().asByteBuffer()).array()); - } + } else { + returnValue = new String(Base64.getDecoder().decode(secret.secretBinary().asByteBuffer()).array()); } return returnValue; } diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentAWSSecretsManagerTestIT.java b/components/camel-aws/camel-aws-secrets-manager/src/test/java/org/apache/camel/component/aws/secretsmanager/integration/SecretsManagerPropertiesSourceTestIT.java similarity index 87% rename from core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentAWSSecretsManagerTestIT.java rename to components/camel-aws/camel-aws-secrets-manager/src/test/java/org/apache/camel/component/aws/secretsmanager/integration/SecretsManagerPropertiesSourceTestIT.java index f1bc020..5f3d4ad 100644 --- a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentAWSSecretsManagerTestIT.java +++ b/components/camel-aws/camel-aws-secrets-manager/src/test/java/org/apache/camel/component/aws/secretsmanager/integration/SecretsManagerPropertiesSourceTestIT.java @@ -14,11 +14,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.component.properties; +package org.apache.camel.component.aws.secretsmanager.integration; -import org.apache.camel.ContextTestSupport; import org.apache.camel.FailedToCreateRouteException; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit5.CamelTestSupport; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; @@ -27,19 +27,14 @@ import static org.junit.jupiter.api.Assertions.assertThrows; @EnabledIfEnvironmentVariable(named = "AWS_ACCESS_KEY", matches = ".*") @EnabledIfEnvironmentVariable(named = "AWS_SECRET_KEY", matches = ".*") @EnabledIfEnvironmentVariable(named = "AWS_REGION", matches = ".*") -public class PropertiesComponentAWSSecretsManagerTestIT extends ContextTestSupport { - - @Override - public boolean isUseRouteBuilder() { - return false; - } +public class SecretsManagerPropertiesSourceTestIT extends CamelTestSupport { @Test public void testFunction() throws Exception { context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { - from("direct:start").setBody(simple("{{aws:test}}")).to("mock:bar"); + from("direct:start").setBody(simple("{{aws:hello}}")).to("mock:bar"); } }); context.start(); diff --git a/core/camel-base/pom.xml b/core/camel-base/pom.xml index 6365b33..faeea8b 100644 --- a/core/camel-base/pom.xml +++ b/core/camel-base/pom.xml @@ -56,13 +56,6 @@ <artifactId>camel-util</artifactId> </dependency> - <!-- required dependencies for AWS Secrets Manager --> - <dependency> - <groupId>software.amazon.awssdk</groupId> - <artifactId>secretsmanager</artifactId> - <version>${aws-java-sdk2-version}</version> - </dependency> - <!-- required logging api dependency by camel-base --> <dependency> <groupId>org.slf4j</groupId> diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java index 0eb3bf4..22c757f 100644 --- a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java +++ b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java @@ -126,7 +126,6 @@ public class PropertiesComponent extends ServiceSupport addPropertiesFunction(new ServicePropertiesFunction()); addPropertiesFunction(new ServiceHostPropertiesFunction()); addPropertiesFunction(new ServicePortPropertiesFunction()); - addPropertiesFunction(new AWSSecretsManagerPropertiesFunction()); } /** diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesFunctionResolver.java b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesFunctionResolver.java index 46155b9..c977508 100644 --- a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesFunctionResolver.java +++ b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesFunctionResolver.java @@ -52,8 +52,6 @@ public final class PropertiesFunctionResolver extends ServiceSupport addPropertiesFunction(new ServicePropertiesFunction()); addPropertiesFunction(new ServiceHostPropertiesFunction()); addPropertiesFunction(new ServicePortPropertiesFunction()); - // TODO: Move AWSSecretsManagerPropertiesFunction to camel-aws-secrets-manager - addPropertiesFunction(new AWSSecretsManagerPropertiesFunction()); } @Override
