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 2024/10/16 14:15:14 UTC

(camel-spring-boot) branch CAMEL-21265-gcp created (now 269f34a98bb)

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a change to branch CAMEL-21265-gcp
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git


      at 269f34a98bb CAMEL-21265 - Create ApplicationEnvironmentPreparedEvent for Vault components - Google Secret Manager

This branch includes the following new commits:

     new 269f34a98bb CAMEL-21265 - Create ApplicationEnvironmentPreparedEvent for Vault components - Google Secret Manager

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(camel-spring-boot) 01/01: CAMEL-21265 - Create ApplicationEnvironmentPreparedEvent for Vault components - Google Secret Manager

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch CAMEL-21265-gcp
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git

commit 269f34a98bbef6294c982b9830f8d16bd3734a10
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Wed Oct 16 16:13:57 2024 +0200

    CAMEL-21265 - Create ApplicationEnvironmentPreparedEvent for Vault components - Google Secret Manager
    
    Signed-off-by: Andrea Cosentino <an...@gmail.com>
---
 .../camel-google-secret-manager-starter/pom.xml    |  12 +++
 ...ingBootGoogleSecretManagerPropertiesParser.java |  76 ++++++++++++++
 .../src/main/resources/META-INF/spring.factories   |   2 +
 .../springboot/EarlyResolvedPropertiesTest.java    | 111 +++++++++++++++++++++
 .../src/test/resources/application.properties      |   2 +
 5 files changed, 203 insertions(+)

diff --git a/components-starter/camel-google-secret-manager-starter/pom.xml b/components-starter/camel-google-secret-manager-starter/pom.xml
index f7234b80895..684ad144fb2 100644
--- a/components-starter/camel-google-secret-manager-starter/pom.xml
+++ b/components-starter/camel-google-secret-manager-starter/pom.xml
@@ -38,6 +38,18 @@
       <artifactId>camel-google-secret-manager</artifactId>
       <version>${camel-version}</version>
     </dependency>
+    <!-- for testing -->
+    <dependency>
+      <groupId>org.awaitility</groupId>
+      <artifactId>awaitility</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter-test</artifactId>
+      <version>${spring-boot-version}</version>
+      <scope>test</scope>
+    </dependency>
     <!--START OF GENERATED CODE-->
     <dependency>
       <groupId>org.apache.camel.springboot</groupId>
diff --git a/components-starter/camel-google-secret-manager-starter/src/main/java/org/apache/camel/component/google/secret/manager/springboot/SpringBootGoogleSecretManagerPropertiesParser.java b/components-starter/camel-google-secret-manager-starter/src/main/java/org/apache/camel/component/google/secret/manager/springboot/SpringBootGoogleSecretManagerPropertiesParser.java
new file mode 100644
index 00000000000..1b9c6079a82
--- /dev/null
+++ b/components-starter/camel-google-secret-manager-starter/src/main/java/org/apache/camel/component/google/secret/manager/springboot/SpringBootGoogleSecretManagerPropertiesParser.java
@@ -0,0 +1,76 @@
+package org.apache.camel.component.google.secret.manager.springboot;
+
+import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
+import com.google.cloud.secretmanager.v1.SecretManagerServiceSettings;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.component.google.secret.manager.GoogleSecretManagerPropertiesFunction;
+import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
+import org.springframework.boot.origin.OriginTrackedValue;
+import org.springframework.context.ApplicationListener;
+import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.core.env.MapPropertySource;
+import org.springframework.core.env.PropertiesPropertySource;
+import org.springframework.core.env.PropertySource;
+
+import java.io.IOException;
+import java.util.Properties;
+
+public class SpringBootGoogleSecretManagerPropertiesParser implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
+    private static final Logger LOG = LoggerFactory.getLogger(SpringBootGoogleSecretManagerPropertiesParser.class);
+
+    @Override
+    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
+        SecretManagerServiceClient client;
+        ConfigurableEnvironment environment = event.getEnvironment();
+        String projectId;
+        if (Boolean.parseBoolean(environment.getProperty("camel.component.google-secret-manager.early-resolve-properties"))) {
+            projectId = environment.getProperty("camel.vault.gcp.projectId");
+            boolean useDefaultInstance = Boolean.parseBoolean(environment.getProperty("camel.vault.gcp.useDefaultInstance"));
+            if (useDefaultInstance && ObjectHelper.isNotEmpty(projectId)) {
+                SecretManagerServiceSettings settings = null;
+                try {
+                    settings = SecretManagerServiceSettings.newBuilder().build();
+                    client = SecretManagerServiceClient.create(settings);
+                } catch (IOException e) {
+                    throw new RuntimeException(e);
+                }
+            } else {
+                throw new RuntimeCamelException(
+                        "Using the GCP Secret Manager Properties Function in Spring Boot early resolver mode requires setting GCP project Id as application properties and use default instance option to true");
+            }
+            GoogleSecretManagerPropertiesFunction secretsManagerPropertiesFunction = new GoogleSecretManagerPropertiesFunction(client, projectId);
+            final Properties props = new Properties();
+            for (PropertySource mutablePropertySources : event.getEnvironment().getPropertySources()) {
+                if (mutablePropertySources instanceof MapPropertySource mapPropertySource) {
+                    mapPropertySource.getSource().forEach((key, value) -> {
+                        String stringValue = null;
+                        if ((value instanceof OriginTrackedValue originTrackedValue &&
+                                originTrackedValue.getValue() instanceof String v)) {
+                            stringValue = v;
+                        } else if (value instanceof String v) {
+                            stringValue = v;
+                        }
+                        if (stringValue != null &&
+                                stringValue.startsWith("{{gcp:") &&
+                                stringValue.endsWith("}}")) {
+                            LOG.debug("decrypting and overriding property {}", key);
+                            try {
+                                String element = secretsManagerPropertiesFunction.apply(stringValue
+                                        .replace("{{gcp:", "")
+                                        .replace("}}", ""));
+                                props.put(key, element);
+                            } catch (Exception e) {
+                                // Log and do nothing
+                                LOG.debug("failed to parse property {}. This exception is ignored.", key, e);
+                            }
+                        }
+                    });
+                }
+            }
+            environment.getPropertySources().addFirst(new PropertiesPropertySource("overridden-camel-google-secret-manager-properties", props));
+        }
+    }
+}
diff --git a/components-starter/camel-google-secret-manager-starter/src/main/resources/META-INF/spring.factories b/components-starter/camel-google-secret-manager-starter/src/main/resources/META-INF/spring.factories
new file mode 100644
index 00000000000..055b6b93c35
--- /dev/null
+++ b/components-starter/camel-google-secret-manager-starter/src/main/resources/META-INF/spring.factories
@@ -0,0 +1,2 @@
+org.springframework.context.ApplicationListener=\
+  org.apache.camel.component.google.secret.manager.springboot.SpringBootGoogleSecretManagerPropertiesParser
\ No newline at end of file
diff --git a/components-starter/camel-google-secret-manager-starter/src/test/java/org/apache/camel/component/google/secret/manager/springboot/EarlyResolvedPropertiesTest.java b/components-starter/camel-google-secret-manager-starter/src/test/java/org/apache/camel/component/google/secret/manager/springboot/EarlyResolvedPropertiesTest.java
new file mode 100644
index 00000000000..edb99913231
--- /dev/null
+++ b/components-starter/camel-google-secret-manager-starter/src/test/java/org/apache/camel/component/google/secret/manager/springboot/EarlyResolvedPropertiesTest.java
@@ -0,0 +1,111 @@
+/*
+ * 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.springboot;
+
+import com.google.cloud.secretmanager.v1.*;
+import com.google.protobuf.ByteString;
+import org.apache.camel.spring.boot.CamelAutoConfiguration;
+import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariables;
+import org.junit.jupiter.api.condition.EnabledIfSystemProperties;
+import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.autoconfigure.AutoConfigureBefore;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.test.annotation.DirtiesContext;
+
+import java.io.IOException;
+
+@CamelSpringBootTest
+@DirtiesContext
+@SpringBootApplication
+@SpringBootTest(
+        classes = { EarlyResolvedPropertiesTest.TestConfiguration.class },
+        properties = {
+                "camel.component.google-secret-manager.early-resolve-properties=true",
+                "early.resolved.property.simple={{gcp:databaseTestPassword}}"
+        })
+
+// Must be manually tested. Provide your own projectId using -Dcamel.vault.test.gcp.projectId and -Dcamel.vault.test.gcp.useDefaultInstance
+@EnabledIfSystemProperties({
+        @EnabledIfSystemProperty(named = "camel.vault.test.gcp.projectId", matches = ".*",
+                disabledReason = "Access key not provided"),
+        @EnabledIfSystemProperty(named = "camel.vault.test.gcp.useDefaultInstance", matches = ".*",
+                disabledReason = "Secret key not provided"),
+})
+@EnabledIfEnvironmentVariables({
+    @EnabledIfEnvironmentVariable(named="GOOGLE_APPLICATION_CREDENTIALS", matches = ".*", disabledReason = "No environment variables for google credentials set")
+})
+public class EarlyResolvedPropertiesTest {
+
+    static SecretManagerServiceClient client;
+    static String secretId;
+
+    @BeforeAll
+    public static void setup() throws IOException {
+        String projectId = System.getProperty("camel.vault.test.gcp.projectId");
+        String useDefaultInstance = System.getProperty("camel.vault.test.gcp.useDefaultInstance");
+        System.setProperty("camel.vault.gcp.projectId", projectId);
+        System.setProperty("camel.vault.gcp.useDefaultInstance", useDefaultInstance);
+
+        SecretManagerServiceSettings settings = SecretManagerServiceSettings.newBuilder().build();
+        client = SecretManagerServiceClient.create(settings);
+        Secret secret = Secret.newBuilder()
+                .setReplication(
+                        Replication.newBuilder()
+                                .setAutomatic(Replication.Automatic.newBuilder().build())
+                                .build())
+                .build();
+
+        Secret createdSecret = client.createSecret(ProjectName.of(projectId), "databaseTestPassword", secret);
+
+        SecretPayload payload = SecretPayload.newBuilder()
+                .setData(ByteString.copyFromUtf8("string")).build();
+        client.addSecretVersion(createdSecret.getName(), payload);
+    }
+
+    @AfterAll
+    public static void teardown() throws IOException {
+        String projectId = System.getProperty("camel.vault.test.gcp.projectId");
+
+        client.deleteSecret(SecretName.of(projectId, "databaseTestPassword"));
+    }
+
+    @Value("${early.resolved.property}")
+    private String earlyResolvedProperty;
+
+    @Value("${early.resolved.property.simple}")
+    private String earlyResolvedPropertySimple;
+
+    @Test
+    public void testEarlyResolvedProperties() {
+        Assertions.assertThat(earlyResolvedProperty).isEqualTo("string");
+        Assertions.assertThat(earlyResolvedPropertySimple).isEqualTo("string");
+    }
+
+    @Configuration
+    @AutoConfigureBefore(CamelAutoConfiguration.class)
+    public static class TestConfiguration {
+    }
+}
diff --git a/components-starter/camel-google-secret-manager-starter/src/test/resources/application.properties b/components-starter/camel-google-secret-manager-starter/src/test/resources/application.properties
new file mode 100644
index 00000000000..a71fcaf48fa
--- /dev/null
+++ b/components-starter/camel-google-secret-manager-starter/src/test/resources/application.properties
@@ -0,0 +1,2 @@
+# Needed by EarlyResolvedPropertiesTest
+early.resolved.property = {{gcp:databaseTestPassword}}
\ No newline at end of file