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-examples.git
commit 53b23ae8fb376ff10859753dd408e6db68b0307a Author: Andrea Cosentino <anco...@gmail.com> AuthorDate: Tue Sep 20 15:02:59 2022 +0200 Added an example of Azure Key Vault Camel context reloading on Secret refresh Signed-off-by: Andrea Cosentino <anco...@gmail.com> --- .../vault/azure-key-vault-reloading/README.adoc | 106 ++++++++++++++++++++ examples/vault/azure-key-vault-reloading/pom.xml | 109 +++++++++++++++++++++ .../org/apache/camel/example/MyApplication.java | 46 +++++++++ .../org/apache/camel/example/MyRouteBuilder.java | 28 ++++++ .../src/main/resources/application.properties | 19 ++-- .../src/main/resources/logback.xml | 30 ++++++ .../src/main/resources/application.properties | 2 +- 7 files changed, 332 insertions(+), 8 deletions(-) diff --git a/examples/vault/azure-key-vault-reloading/README.adoc b/examples/vault/azure-key-vault-reloading/README.adoc new file mode 100644 index 00000000..eb8eef7d --- /dev/null +++ b/examples/vault/azure-key-vault-reloading/README.adoc @@ -0,0 +1,106 @@ +== Camel Example Azure Key Vault Reloading + +This example shows how to use Azure Key Vault to retrieve a secret, update the secret and trigger a reload of the camel context. + +Also notice how you can configure Camel in the `application.properties` file. + +=== Setup + +You'll need to have a key vault as first step. + +Then you'll need to create an event grid subscription to Eventhubs with a Blob Account and container for storing the checkpoint. + +It's not totally easy to do through the az cli, but everything could be done through the Azure UI. We're planning to improve this example by having all the instructions exposed as Azure CLI commands. + +Set all the credentials in the application.properties file correctly + +=== Build + +First compile the example by executing: + +[source,sh] +---- +$ mvn compile +---- + +=== How to run + +Then you can run this example using + +[source,sh] +---- +$ mvn camel:run +---- + +At this point you should see: + +[source,sh] +---- +14:52:59.371 [org.apache.camel.example.MyApplication.main()] INFO org.apache.camel.main.MainSupport - Apache Camel (Main) 3.19.0-SNAPSHOT is starting +14:52:59.449 [org.apache.camel.example.MyApplication.main()] INFO o.apache.camel.main.BaseMainSupport - Classpath scanning enabled from base package: org.apache.camel.example +14:52:59.526 [org.apache.camel.example.MyApplication.main()] INFO o.apache.camel.main.BaseMainSupport - Auto-configuration summary +. +. +. +. +14:53:02.301 [org.apache.camel.example.MyApplication.main()] INFO o.a.c.i.engine.AbstractCamelContext - Apache Camel 3.19.0-SNAPSHOT (azure-key-vault) is starting +14:53:02.325 [org.apache.camel.example.MyApplication.main()] INFO o.a.c.i.engine.AbstractCamelContext - Routes startup (started:1) +14:53:02.325 [org.apache.camel.example.MyApplication.main()] INFO o.a.c.i.engine.AbstractCamelContext - Started route1 (timer://myTimer) +14:53:02.326 [org.apache.camel.example.MyApplication.main()] INFO o.a.c.i.engine.AbstractCamelContext - Apache Camel 3.19.0-SNAPSHOT (azure-key-vault) started in 2s84ms (build:24ms init:2s36ms start:24ms JVM-uptime:5s) +14:54:03.325 [Camel (azure-key-vault) thread #2 - timer://myTimer] INFO route1 - Secret value is: Camel rocks! +14:54:13.324 [Camel (azure-key-vault) thread #2 - timer://myTimer] INFO route1 - Secret value is: Camel rocks! +14:54:23.324 [Camel (azure-key-vault) thread #2 - timer://myTimer] INFO route1 - Secret value is: Camel rocks! + +---- + +The example is running and it is using the original secret value. Now, in a different terminal, run the following Az CLI command: + +[source,sh] +---- +az keyvault secret set --name hello --vault-name test12345678910 --value 'Camel Rocks reloaded!' +---- + +This will create a new secret version. + +Now, get back, to the running Camel application and in the log you should see: + +[source,sh] +---- +. +. +. +14:55:28.646 [partition-pump-0-3] INFO o.a.c.c.a.k.v.EventhubsReloadTriggerTask - Update for Azure secret: hello detected, triggering CamelContext reload +14:55:28.646 [partition-pump-0-3] INFO o.a.c.s.DefaultContextReloadStrategy - Reloading CamelContext (azure-key-vault) triggered by: Azure Secrets Refresh Task +14:55:30.574 [Camel (azure-key-vault) thread #5 - timer://myTimer] INFO route1 - Secret value is: Camel Rocks reloaded! +. +. +. +. +---- + +The Camel context has been reloaded after we noticed a `Microsoft.KeyVault.SecretNewVersionCreated` event for this specific secret, in the Eventgrid topic. + +The example will work even if you remove the property `camel.azure.vault.secrets`, because the azure related properties will be taken into account automatically. + +Now, stop the application. + +=== Cleanup + +- Delete the secret + +Simply run + +[source,sh] +---- +az keyvault secret delete --name hello --vault-name test12345678910 +---- + +=== Help and contributions + +If you hit any problem using Camel or have some feedback, then please +https://camel.apache.org/community/support/[let us know]. + +We also love contributors, so +https://camel.apache.org/community/contributing/[get involved] :-) + +The Camel riders! diff --git a/examples/vault/azure-key-vault-reloading/pom.xml b/examples/vault/azure-key-vault-reloading/pom.xml new file mode 100644 index 00000000..d084a3e3 --- /dev/null +++ b/examples/vault/azure-key-vault-reloading/pom.xml @@ -0,0 +1,109 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.camel.example</groupId> + <artifactId>camel-examples-vault-parent</artifactId> + <version>3.19.0-SNAPSHOT</version> + </parent> + + <artifactId>camel-example-azure-key-vault-reloading</artifactId> + <packaging>jar</packaging> + <name>Camel :: Example :: Azure Key Vault Reloading</name> + <description>An example for showing Azure Key Vault Camel component with reloading</description> + + <properties> + <category>Beginner</category> + </properties> + + <dependencyManagement> + <dependencies> + <!-- Add Camel BOM --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-bom</artifactId> + <version>${camel.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-core</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-main</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-azure-key-vault</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-timer</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-jsonpath</artifactId> + </dependency> + + <!-- logging --> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + <version>${log4j2-version}</version> + <scope>runtime</scope> + </dependency> + <dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-core</artifactId> + <version>${logback-version}</version> + </dependency> + <dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-classic</artifactId> + <version>${logback-version}</version> + </dependency> + + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.camel</groupId> + <artifactId>camel-maven-plugin</artifactId> + <version>${camel.version}</version> + <configuration> + <logClasspath>false</logClasspath> + <mainClass>org.apache.camel.example.MyApplication</mainClass> + </configuration> + </plugin> + </plugins> + </build> + +</project> diff --git a/examples/vault/azure-key-vault-reloading/src/main/java/org/apache/camel/example/MyApplication.java b/examples/vault/azure-key-vault-reloading/src/main/java/org/apache/camel/example/MyApplication.java new file mode 100644 index 00000000..95c29298 --- /dev/null +++ b/examples/vault/azure-key-vault-reloading/src/main/java/org/apache/camel/example/MyApplication.java @@ -0,0 +1,46 @@ +/* + * 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.example; + +import java.time.Instant; + +import org.apache.camel.main.Main; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Main class that boot the Camel application + */ +public final class MyApplication { + + private static final Logger LOG = LoggerFactory.getLogger(MyApplication.class); + + public static Instant lastTime = null; + + private MyApplication() { + } + + public static void main(String[] args) throws Exception { + // use Camels Main class + Main main = new Main(MyApplication.class); + + // now keep the application running until the JVM is terminated (ctrl + c or + // sigterm) + main.run(args); + } + +} diff --git a/examples/vault/azure-key-vault-reloading/src/main/java/org/apache/camel/example/MyRouteBuilder.java b/examples/vault/azure-key-vault-reloading/src/main/java/org/apache/camel/example/MyRouteBuilder.java new file mode 100644 index 00000000..ca2f031e --- /dev/null +++ b/examples/vault/azure-key-vault-reloading/src/main/java/org/apache/camel/example/MyRouteBuilder.java @@ -0,0 +1,28 @@ +/* + * 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.example; + +import org.apache.camel.builder.RouteBuilder; + +public class MyRouteBuilder extends RouteBuilder { + + @Override + public void configure() throws Exception { + from("timer://myTimer?fixedRate=true&period=10000") + .log("Secret value is: {{azure:hello}}"); + } +} diff --git a/examples/vault/google-secret-manager-reloading/src/main/resources/application.properties b/examples/vault/azure-key-vault-reloading/src/main/resources/application.properties similarity index 69% copy from examples/vault/google-secret-manager-reloading/src/main/resources/application.properties copy to examples/vault/azure-key-vault-reloading/src/main/resources/application.properties index 0b2eb247..50406aa8 100644 --- a/examples/vault/google-secret-manager-reloading/src/main/resources/application.properties +++ b/examples/vault/azure-key-vault-reloading/src/main/resources/application.properties @@ -17,16 +17,21 @@ # here you can configure options on camel main # https://camel.apache.org/components/next/others/main.html -camel.main.name = gcp-secrets-manager +camel.main.name = azure-key-vault camel.main.jmx-enabled = false # extended runtime statistics about bean introspection usage (java reflection) camel.main.bean-introspection-logging-level=INFO -camel.vault.gcp.projectId=gcp-sec-refresh -camel.vault.gcp.serviceAccountKey = <path_to_service_account_key_file> -camel.vault.gcp.refreshEnabled=true -camel.vault.gcp.refreshPeriod=60000 -camel.vault.gcp.secrets=hello* -camel.vault.gcp.subscriptionName=sub-gcp-sec-refresh +camel.vault.azure.tenantId = <tenant_id> +camel.vault.azure.clientId = <client_id> +camel.vault.azure.clientSecret = <client_secret> +camel.vault.azure.vaultName = <vault_name> +camel.vault.azure.refreshEnabled=true +camel.vault.azure.refreshPeriod=15000 +camel.vault.azure.secrets=hello* +camel.vault.azure.eventhubConnectionString=<eventhub_conn_string> +camel.vault.azure.blobAccountName=<blob_account_name> +camel.vault.azure.blobContainerName=<blob_container_name> +camel.vault.azure.blobAccessKey=<blob_access_key> camel.main.context-reload-enabled = true diff --git a/examples/vault/azure-key-vault-reloading/src/main/resources/logback.xml b/examples/vault/azure-key-vault-reloading/src/main/resources/logback.xml new file mode 100644 index 00000000..a798d0b3 --- /dev/null +++ b/examples/vault/azure-key-vault-reloading/src/main/resources/logback.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +--> +<configuration> + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> + </encoder> + </appender> + + <root level="INFO"> + <appender-ref ref="STDOUT" /> + </root> +</configuration> diff --git a/examples/vault/google-secret-manager-reloading/src/main/resources/application.properties b/examples/vault/google-secret-manager-reloading/src/main/resources/application.properties index 0b2eb247..cec4c9d9 100644 --- a/examples/vault/google-secret-manager-reloading/src/main/resources/application.properties +++ b/examples/vault/google-secret-manager-reloading/src/main/resources/application.properties @@ -24,7 +24,7 @@ camel.main.jmx-enabled = false camel.main.bean-introspection-logging-level=INFO camel.vault.gcp.projectId=gcp-sec-refresh -camel.vault.gcp.serviceAccountKey = <path_to_service_account_key_file> +camel.vault.gcp.serviceAccountKey = file:////home/oscerd/Desktop/gcp-sec-refresh.json camel.vault.gcp.refreshEnabled=true camel.vault.gcp.refreshPeriod=60000 camel.vault.gcp.secrets=hello*