You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2022/03/04 07:48:47 UTC

[camel] 02/06: CAMEL-17739 - Camel Google Secret Manager Properties Source: Support the usage of client default instance

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 bb7edec1a23d1a7d85683b309ae12b5511ed51b8
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Fri Mar 4 06:46:25 2022 +0100

    CAMEL-17739 - Camel Google Secret Manager Properties Source: Support the usage of client default instance
---
 .../GoogleSecretManagerPropertiesFunction.java      |  6 ++++++
 .../GoogleSecretManagerPropertiesSourceTestIT.java  | 21 +++++++++++++++++++++
 2 files changed, 27 insertions(+)

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
index e562cfa..9059a16 100644
--- 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
@@ -75,6 +75,7 @@ public class GoogleSecretManagerPropertiesFunction extends ServiceSupport implem
 
     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 static final String CAMEL_VAULT_GCP_USE_DEFAULT_INSTANCE = "CAMEL_VAULT_GCP_USE_DEFAULT_INSTANCE";
     private CamelContext camelContext;
     private SecretManagerServiceClient client;
     private String projectId;
@@ -83,12 +84,14 @@ public class GoogleSecretManagerPropertiesFunction extends ServiceSupport implem
     protected void doStart() throws Exception {
         super.doStart();
         String serviceAccountKey = System.getenv(CAMEL_VAULT_GCP_SERVICE_ACCOUNT_KEY);
+        boolean useDefaultInstance = Boolean.parseBoolean(System.getenv(CAMEL_VAULT_GCP_USE_DEFAULT_INSTANCE));
         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();
+                useDefaultInstance = gcpVaultConfiguration.isUseDefaultInstance();
             }
         }
         if (ObjectHelper.isNotEmpty(serviceAccountKey) && ObjectHelper.isNotEmpty(projectId)) {
@@ -99,6 +102,9 @@ public class GoogleSecretManagerPropertiesFunction extends ServiceSupport implem
             SecretManagerServiceSettings settings = SecretManagerServiceSettings.newBuilder()
                     .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)).build();
             client = SecretManagerServiceClient.create(settings);
+        } else if ( useDefaultInstance && ObjectHelper.isNotEmpty(projectId)) {
+            SecretManagerServiceSettings settings = SecretManagerServiceSettings.newBuilder().build();
+            client = SecretManagerServiceClient.create(settings);
         } else {
             throw new RuntimeCamelException(
                     "Using the GCP Secret Manager Properties Function requires setting GCP service account key and project Id as application properties or environment variables");
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
index 76972dc..0e85630 100644
--- 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
@@ -187,4 +187,25 @@ public class GoogleSecretManagerPropertiesSourceTestIT extends CamelTestSupport
         template.sendBody("direct:password", "Hello World");
         assertMockEndpointsSatisfied();
     }
+
+    @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_GCP_USE_DEFAULT_INSTACE", matches = ".*")
+    @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_GCP_PROJECT_ID", matches = ".*")
+    @EnabledIfEnvironmentVariable(named = "GOOGLE_APPLICATION_CREDENTIALS", matches = ".*")
+    @Test
+    public void testComplexPropertiesDefaultInstanceFunction() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:username").setBody(simple("{{gcp:test-3:admin}}")).to("mock:bar");
+                from("direct:password").setBody(simple("{{gcp:test-1: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();
+    }
 }