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 710637f4d3f875c9186da6f51660ab961afeafb6
Author: Andrea Cosentino <[email protected]>
AuthorDate: Tue Mar 1 14:30:25 2022 +0100

    CAMEL-17684 - Support ability to load properties from Vault/Secrets cloud 
services - GCP Secrets Manager
---
 .../org/apache/camel/properties-function/gcp       |   2 +
 .../GoogleSecretManagerPropertiesFunction.java     | 189 +++++++++++++++++++++
 .../GoogleSecretManagerPropertiesSourceTestIT.java | 130 ++++++++++++++
 3 files changed, 321 insertions(+)

diff --git 
a/components/camel-google/camel-google-secret-manager/src/generated/resources/META-INF/services/org/apache/camel/properties-function/gcp
 
b/components/camel-google/camel-google-secret-manager/src/generated/resources/META-INF/services/org/apache/camel/properties-function/gcp
new file mode 100644
index 0000000..b8e8b3a
--- /dev/null
+++ 
b/components/camel-google/camel-google-secret-manager/src/generated/resources/META-INF/services/org/apache/camel/properties-function/gcp
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.google.secret.manager.GoogleSecretManagerPropertiesFunction
diff --git 
a/components/camel-google/camel-google-secret-manager/src/main/java/org/apache/camel/component/google/secret/manager/GoogleSecretManagerPropertiesFunction.java
 
b/components/camel-google/camel-google-secret-manager/src/main/java/org/apache/camel/component/google/secret/manager/GoogleSecretManagerPropertiesFunction.java
new file mode 100644
index 0000000..07973fe
--- /dev/null
+++ 
b/components/camel-google/camel-google-secret-manager/src/main/java/org/apache/camel/component/google/secret/manager/GoogleSecretManagerPropertiesFunction.java
@@ -0,0 +1,189 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *      http://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 org.apache.camel.component.google.secret.manager;
+
+import java.io.InputStream;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.api.gax.core.FixedCredentialsProvider;
+import com.google.api.gax.rpc.NotFoundException;
+import com.google.auth.Credentials;
+import com.google.auth.oauth2.ServiceAccountCredentials;
+import com.google.cloud.secretmanager.v1.AccessSecretVersionResponse;
+import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
+import com.google.cloud.secretmanager.v1.SecretManagerServiceSettings;
+import com.google.cloud.secretmanager.v1.SecretVersionName;
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.spi.PropertiesFunction;
+import org.apache.camel.support.ResourceHelper;
+import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.StringHelper;
+import org.apache.camel.vault.GcpVaultConfiguration;
+
+/**
+ * A {@link PropertiesFunction} that lookup the property value from GCP 
Secrets Manager service.
+ * <p/>
+ * The credentials to access Secrets Manager is defined using three 
environment variables representing the static
+ * credentials:
+ * <ul>
+ * <li><tt>CAMEL_VAULT_GCP_SERVICE_ACCOUNT_KEY</tt></li>
+ * <li><tt>CAMEL_VAULT_GCP_PROJECT_ID</tt></li>
+ * </ul>
+ * <p/>
+ *
+ * Otherwise it is possible to specify the credentials as properties:
+ *
+ * <ul>
+ * <li><tt>camel.vault.aws.serviceAccountKey</tt></li>
+ * <li><tt>camel.vault.aws.projectId</tt></li>
+ * </ul>
+ * <p/>
+ *
+ * This implementation is to return the secret value associated with a key. 
The properties related to this kind of
+ * Properties Function are all prefixed with <tt>gcp:</tt>. For example asking 
for <tt>gcp:token</tt>, will return the
+ * secret value associated to the secret named token on GCP Secrets Manager.
+ *
+ * Another way of retrieving a secret value is using the following notation 
<tt>gcp:database/username</tt>: in this case
+ * the field username of the secret database will be returned. As a fallback, 
the user could provide a default value,
+ * which will be returned in case the secret doesn't exist, the secret has 
been marked for deletion or, for example, if
+ * a particular field of the secret doesn't exist. For using this feature, the 
user could use the following notation
+ * <tt>gcp:database/username:admin</tt>. The admin value will be returned as 
default value, if the conditions above were
+ * all met.
+ */
+
[email protected]("gcp")
+public class GoogleSecretManagerPropertiesFunction extends ServiceSupport 
implements PropertiesFunction, CamelContextAware {
+
+    private static final String CAMEL_VAULT_GCP_SERVICE_ACCOUNT_KEY = 
"CAMEL_VAULT_GCP_SERVICE_ACCOUNT_KEY";
+    private static final String CAMEL_VAULT_GCP_PROJECT_ID = 
"CAMEL_VAULT_GCP_PROJECT_ID";
+    private CamelContext camelContext;
+    private SecretManagerServiceClient client;
+    private String projectId;
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        String serviceAccountKey = 
System.getenv(CAMEL_VAULT_GCP_SERVICE_ACCOUNT_KEY);
+        projectId = System.getenv(CAMEL_VAULT_GCP_PROJECT_ID);
+        if (ObjectHelper.isEmpty(serviceAccountKey) && 
ObjectHelper.isEmpty(projectId)) {
+            GcpVaultConfiguration gcpVaultConfiguration = 
getCamelContext().getVaultConfiguration().gcp();
+            if (ObjectHelper.isNotEmpty(gcpVaultConfiguration)) {
+                serviceAccountKey = 
gcpVaultConfiguration.getServiceAccountKey();
+                projectId = gcpVaultConfiguration.getProjectId();
+            }
+        }
+        if (ObjectHelper.isNotEmpty(serviceAccountKey) && 
ObjectHelper.isNotEmpty(projectId)) {
+            InputStream resolveMandatoryResourceAsInputStream
+                    = 
ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), 
serviceAccountKey);
+            Credentials myCredentials = ServiceAccountCredentials
+                    .fromStream(resolveMandatoryResourceAsInputStream);
+            SecretManagerServiceSettings settings = 
SecretManagerServiceSettings.newBuilder()
+                    
.setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)).build();
+            client = SecretManagerServiceClient.create(settings);
+        } else {
+            throw new RuntimeCamelException(
+                    "Using the AWS Secrets Manager Properties Function 
requires setting AWS credentials as application properties or environment 
variables");
+        }
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        if (client != null) {
+            client.close();
+        }
+        super.doStop();
+    }
+
+    @Override
+    public String getName() {
+        return "aws";
+    }
+
+    @Override
+    public String apply(String remainder) {
+        String key = remainder;
+        String subkey = null;
+        String returnValue = null;
+        String defaultValue = null;
+        if (remainder.contains("/")) {
+            key = StringHelper.before(remainder, "/");
+            subkey = StringHelper.after(remainder, "/");
+            defaultValue = StringHelper.after(subkey, ":");
+        } else if (remainder.contains(":")) {
+            key = StringHelper.before(remainder, ":");
+            defaultValue = StringHelper.after(remainder, ":");
+        }
+
+        if (key != null) {
+            try {
+                returnValue = getSecretFromSource(key, subkey, defaultValue);
+            } catch (JsonProcessingException e) {
+                throw new RuntimeCamelException("Something went wrong while 
recovering " + key + " from vault");
+            }
+        }
+
+        return returnValue;
+    }
+
+    private String getSecretFromSource(
+            String key, String subkey, String defaultValue)
+            throws JsonProcessingException {
+        String returnValue = null;
+        try {
+            SecretVersionName secretVersionName = 
SecretVersionName.of(projectId, key, "latest");
+            AccessSecretVersionResponse response = 
client.accessSecretVersion(secretVersionName);
+            if (ObjectHelper.isNotEmpty(response)) {
+                returnValue = response.getPayload().getData().toStringUtf8();
+            }
+            if (ObjectHelper.isNotEmpty(subkey) && 
ObjectHelper.isNotEmpty(returnValue)) {
+                ObjectMapper mapper = new ObjectMapper();
+                JsonNode actualObj = mapper.readTree(returnValue);
+                JsonNode field = actualObj.get(subkey);
+                if (ObjectHelper.isNotEmpty(field)) {
+                    returnValue = field.textValue();
+                } else {
+                    returnValue = null;
+                }
+            }
+            if (ObjectHelper.isEmpty(returnValue)) {
+                returnValue = defaultValue;
+            }
+        } catch (NotFoundException ex) {
+            if (ObjectHelper.isNotEmpty(defaultValue)) {
+                returnValue = defaultValue;
+            } else {
+                throw ex;
+            }
+        }
+        return returnValue;
+    }
+
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+}
diff --git 
a/components/camel-google/camel-google-secret-manager/src/test/java/org/apache/camel/component/google/secret/manager/integration/GoogleSecretManagerPropertiesSourceTestIT.java
 
b/components/camel-google/camel-google-secret-manager/src/test/java/org/apache/camel/component/google/secret/manager/integration/GoogleSecretManagerPropertiesSourceTestIT.java
new file mode 100644
index 0000000..473cbbd
--- /dev/null
+++ 
b/components/camel-google/camel-google-secret-manager/src/test/java/org/apache/camel/component/google/secret/manager/integration/GoogleSecretManagerPropertiesSourceTestIT.java
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *      http://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 org.apache.camel.component.google.secret.manager.integration;
+
+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;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+public class GoogleSecretManagerPropertiesSourceTestIT extends 
CamelTestSupport {
+
+    @EnabledIfEnvironmentVariable(named = 
"CAMEL_VAULT_GCP_SERVICE_ACCOUNT_KEY", matches = ".*")
+    @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_GCP_PROJECT_ID", 
matches = ".*")
+    @Test
+    public void testFunction() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                
from("direct:start").setBody(simple("{{gcp:hello}}")).to("mock:bar");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:bar").expectedBodiesReceived("hello");
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @EnabledIfEnvironmentVariable(named = 
"CAMEL_VAULT_GCP_SERVICE_ACCOUNT_KEY", matches = ".*")
+    @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_GCP_PROJECT_ID", 
matches = ".*")
+    @Test
+    public void testSecretNotFoundFunction() throws Exception {
+        Exception exception = assertThrows(FailedToCreateRouteException.class, 
() -> {
+            context.addRoutes(new RouteBuilder() {
+                @Override
+                public void configure() throws Exception {
+                    
from("direct:start").setBody(simple("{{gcp:testExample}}")).to("mock:bar");
+                }
+            });
+            context.start();
+
+            getMockEndpoint("mock:bar").expectedBodiesReceived("hello");
+
+            template.sendBody("direct:start", "Hello World");
+
+            assertMockEndpointsSatisfied();
+        });
+    }
+
+    @EnabledIfEnvironmentVariable(named = 
"CAMEL_VAULT_GCP_SERVICE_ACCOUNT_KEY", matches = ".*")
+    @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_GCP_PROJECT_ID", 
matches = ".*")
+    @Test
+    public void testComplexCustomPropertiesDefaultValueFunction() throws 
Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                
from("direct:username").setBody(simple("{{gcp:postgresql/additional1:admin}}")).to("mock:bar");
+                
from("direct:password").setBody(simple("{{gcp:postgresql/additional2:secret}}")).to("mock:bar");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:bar").expectedBodiesReceived("admin", "secret");
+
+        template.sendBody("direct:username", "Hello World");
+        template.sendBody("direct:password", "Hello World");
+        assertMockEndpointsSatisfied();
+    }
+
+    @EnabledIfEnvironmentVariable(named = 
"CAMEL_VAULT_GCP_SERVICE_ACCOUNT_KEY", matches = ".*")
+    @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_GCP_PROJECT_ID", 
matches = ".*")
+    @Test
+    public void testComplexCustomPropertiesDefaultValueExceptionFunction() 
throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                
from("direct:username").setBody(simple("{{gcp:test-3/additional1:admin}}")).to("mock:bar");
+                
from("direct:password").setBody(simple("{{gcp:test-3/additional2:secret}}")).to("mock:bar");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:bar").expectedBodiesReceived("admin", "secret");
+
+        template.sendBody("direct:username", "Hello World");
+        template.sendBody("direct:password", "Hello World");
+        assertMockEndpointsSatisfied();
+    }
+
+    @EnabledIfEnvironmentVariable(named = 
"CAMEL_VAULT_GCP_SERVICE_ACCOUNT_KEY", matches = ".*")
+    @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_GCP_PROJECT_ID", 
matches = ".*")
+    @Test
+    public void testComplexCustomPropertiesExceptionFunction() throws 
Exception {
+        Exception exception = assertThrows(FailedToCreateRouteException.class, 
() -> {
+            context.addRoutes(new RouteBuilder() {
+                @Override
+                public void configure() throws Exception {
+                    
from("direct:username").setBody(simple("{{gcp:test-3/additional1}}")).to("mock:bar");
+                    
from("direct:password").setBody(simple("{{gcp:test-3/additional2}}")).to("mock:bar");
+                }
+            });
+            context.start();
+
+            getMockEndpoint("mock:bar").expectedBodiesReceived("admin", 
"secret");
+
+            template.sendBody("direct:username", "Hello World");
+            template.sendBody("direct:password", "Hello World");
+            assertMockEndpointsSatisfied();
+        });
+    }
+}

Reply via email to