This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-examples.git
commit adb1a506cc54661a1817f8778f2f8b6d65537153 Author: Andrea Cosentino <anco...@gmail.com> AuthorDate: Thu Mar 18 07:33:48 2021 +0100 Added a little Camel-AWS-Secrets-Manager example --- examples/aws-secrets-manager/README.adoc | 62 +++++++++++ examples/aws-secrets-manager/pom.xml | 113 +++++++++++++++++++++ .../org/apache/camel/example/MyApplication.java | 38 +++++++ .../org/apache/camel/example/MyRouteBuilder.java | 33 ++++++ .../src/main/resources/application.properties | 33 ++++++ .../src/main/resources/logback.xml | 30 ++++++ 6 files changed, 309 insertions(+) diff --git a/examples/aws-secrets-manager/README.adoc b/examples/aws-secrets-manager/README.adoc new file mode 100644 index 0000000..d6c4dac --- /dev/null +++ b/examples/aws-secrets-manager/README.adoc @@ -0,0 +1,62 @@ +== Camel Example AWS Secrets Manager + +This example shows how to use AWS Secrets Manager to retrieve an API key and call a secured rest service. + +Also notice how you can configure Camel in the `application.properties` file. + +=== Setup + +- Store the secret + +Login into your AWS Account and create a secret called "finnhub_token" with the Finnhub.io token value, you retrieved from finnhub_token. + +- Setting up the AWS credentials + +Open the `application.properties` file and set the correct values for accessKey, secretKey and region + +=== 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] +---- +07:28:51.140 [org.apache.camel.example.MyApplication.main()] INFO o.apache.camel.main.BaseMainSupport - Auto-configuration summary +07:28:51.142 [org.apache.camel.example.MyApplication.main()] INFO o.apache.camel.main.BaseMainSupport - camel.main.name=AWS-secrets-manager +07:28:51.143 [org.apache.camel.example.MyApplication.main()] INFO o.apache.camel.main.BaseMainSupport - camel.main.jmxEnabled=false +07:28:51.143 [org.apache.camel.example.MyApplication.main()] INFO o.apache.camel.main.BaseMainSupport - camel.main.beanIntrospectionLoggingLevel=INFO +07:28:51.143 [org.apache.camel.example.MyApplication.main()] INFO o.apache.camel.main.BaseMainSupport - camel.main.fileConfigurations=src/main/data/*.properties +07:28:51.143 [org.apache.camel.example.MyApplication.main()] INFO o.apache.camel.main.BaseMainSupport - camel.component.aws-secrets-manager.accessKey=xxxxxx +07:28:51.143 [org.apache.camel.example.MyApplication.main()] INFO o.apache.camel.main.BaseMainSupport - camel.component.aws-secrets-manager.secretKey=xxxxxx +07:28:51.143 [org.apache.camel.example.MyApplication.main()] INFO o.apache.camel.main.BaseMainSupport - camel.component.aws-secrets-manager.region=eu-west-1 +07:28:51.699 [org.apache.camel.example.MyApplication.main()] INFO o.a.c.i.engine.AbstractCamelContext - Routes startup summary (total:1 started:1) +07:28:51.699 [org.apache.camel.example.MyApplication.main()] INFO o.a.c.i.engine.AbstractCamelContext - Started route1 (timer://myTimer) +07:28:51.699 [org.apache.camel.example.MyApplication.main()] INFO o.a.c.i.engine.AbstractCamelContext - Apache Camel 3.9.0-SNAPSHOT (AWS-secrets-manager) started in 537ms (build:27ms init:112ms start:398ms) +07:28:53.429 [Camel (AWS-secrets-manager) thread #0 - timer://myTimer] INFO o.a.c.component.http.HttpComponent - Created ClientConnectionManager org.apache.http.impl.conn.PoolingHttpClientConnectionManager@7d5604cb +07:28:54.128 [Camel (AWS-secrets-manager) thread #0 - timer://myTimer] INFO route1 - Current Amazon price: 3135.73 $ +---- + +=== Help and contributions + +If you hit any problem using Camel or have some feedback, then please +https://camel.apache.org/support.html[let us know]. + +We also love contributors, so +https://camel.apache.org/contributing.html[get involved] :-) + +The Camel riders! diff --git a/examples/aws-secrets-manager/pom.xml b/examples/aws-secrets-manager/pom.xml new file mode 100644 index 0000000..1fb19fb --- /dev/null +++ b/examples/aws-secrets-manager/pom.xml @@ -0,0 +1,113 @@ +<?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>examples</artifactId> + <version>3.9.0-SNAPSHOT</version> + </parent> + + <artifactId>camel-example-aws-secrets-manager</artifactId> + <packaging>jar</packaging> + <name>Camel :: Example :: AWS Secrets Manager</name> + <description>An example for showing AWS Secrets Manager Camel component</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-aws-secrets-manager</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-http</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/aws-secrets-manager/src/main/java/org/apache/camel/example/MyApplication.java b/examples/aws-secrets-manager/src/main/java/org/apache/camel/example/MyApplication.java new file mode 100644 index 0000000..496b37d --- /dev/null +++ b/examples/aws-secrets-manager/src/main/java/org/apache/camel/example/MyApplication.java @@ -0,0 +1,38 @@ +/* + * 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.main.Main; + +/** + * Main class that boot the Camel application + */ +public final class MyApplication { + + private MyApplication() { + } + + public static void main(String[] args) throws Exception { + // use Camels Main class + Main main = new Main(); + // and add the routes (you can specify multiple classes) + main.configure().addRoutesBuilder(MyRouteBuilder.class); + // now keep the application running until the JVM is terminated (ctrl + c or sigterm) + main.run(args); + } + +} diff --git a/examples/aws-secrets-manager/src/main/java/org/apache/camel/example/MyRouteBuilder.java b/examples/aws-secrets-manager/src/main/java/org/apache/camel/example/MyRouteBuilder.java new file mode 100644 index 0000000..9540c91 --- /dev/null +++ b/examples/aws-secrets-manager/src/main/java/org/apache/camel/example/MyRouteBuilder.java @@ -0,0 +1,33 @@ +/* + * 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; +import org.apache.camel.component.aws.secretsmanager.SecretsManagerConstants; + +public class MyRouteBuilder extends RouteBuilder { + + @Override + public void configure() throws Exception { + from("timer://myTimer?fixedRate=true&period=10000") + .setHeader(SecretsManagerConstants.SECRET_ID, constant("finnhub_token")) + .to("aws-secrets-manager://test?operation=getSecret") + .toD("https://finnhub.io/api/v1/quote?symbol={{stock}}&token=${body}") + .setBody().jsonpath("$.c", true) + .log("Current {{stockName}} price: ${body} $"); + } +} diff --git a/examples/aws-secrets-manager/src/main/resources/application.properties b/examples/aws-secrets-manager/src/main/resources/application.properties new file mode 100644 index 0000000..df43c2c --- /dev/null +++ b/examples/aws-secrets-manager/src/main/resources/application.properties @@ -0,0 +1,33 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- + +# to configure camel main +# here you can configure options on camel main (see MainConfigurationProperties class) +camel.main.name = AWS-secrets-manager +camel.main.jmx-enabled = false + +# extended runtime statistics about bean introspection usage (java reflection) +camel.main.bean-introspection-logging-level=INFO + +# Camel AWS Secrets Manager +camel.component.aws-secrets-manager.accessKey=xxxx +camel.component.aws-secrets-manager.secretKey=yyyy +camel.component.aws-secrets-manager.region=eu-west-1 + +# Generic properties +stock = AMZN +stockName = Amazon diff --git a/examples/aws-secrets-manager/src/main/resources/logback.xml b/examples/aws-secrets-manager/src/main/resources/logback.xml new file mode 100644 index 0000000..a798d0b --- /dev/null +++ b/examples/aws-secrets-manager/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>