You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by fm...@apache.org on 2024/06/12 13:10:42 UTC
(camel-spring-boot-examples) 02/02: Bean configuration via HashiCorp Vault example
This is an automated email from the ASF dual-hosted git repository.
fmariani pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot-examples.git
commit 1d3c77291b7b5d6e436dc4f41aece2e6044e0122
Author: Croway <fe...@gmail.com>
AuthorDate: Wed Jun 12 13:05:18 2024 +0200
Bean configuration via HashiCorp Vault example
---
README.adoc | 4 +-
pom.xml | 1 +
vault/img/secret-database.png | Bin 0 -> 48269 bytes
vault/pom.xml | 128 +++++++++++++++++++++
vault/readme.adoc | 49 ++++++++
.../spring/vault/CamelSpringVaultApplication.java | 29 +++++
vault/src/main/resources/application.properties | 27 +++++
vault/src/main/resources/camel/cars-api.yaml | 28 +++++
.../src/main/resources/camel/cars-datasource.yaml | 25 ++++
vault/src/main/resources/camel/cars-routes.yaml | 71 ++++++++++++
10 files changed, 361 insertions(+), 1 deletion(-)
diff --git a/README.adoc b/README.adoc
index 05af9df..aebb215 100644
--- a/README.adoc
+++ b/README.adoc
@@ -27,7 +27,7 @@ readme's instructions.
=== Examples
// examples: START
-Number of Examples: 57 (0 deprecated)
+Number of Examples: 58 (0 deprecated)
[width="100%",cols="4,2,4",options="header"]
|===
@@ -150,6 +150,8 @@ Number of Examples: 57 (0 deprecated)
| link:jira/README.adoc[Jira] (jira) | SaaS | An example that uses Jira Camel API
| link:twitter-salesforce/README.adoc[Twitter Salesforce] (twitter-salesforce) | SaaS | Twitter mentions is created as contacts in Salesforce
+
+| link:vault/README.adoc[HashiCorp Vault] (vault) | Bean Configuration | Configure a DataSource using HashiCorp Vault
|===
// examples: END
diff --git a/pom.xml b/pom.xml
index 2c6661d..55716ae 100644
--- a/pom.xml
+++ b/pom.xml
@@ -92,6 +92,7 @@
<module>widget-gadget</module>
<module>azure</module>
<module>variables</module>
+ <module>vault</module>
</modules>
<properties>
diff --git a/vault/img/secret-database.png b/vault/img/secret-database.png
new file mode 100644
index 0000000..20e93d2
Binary files /dev/null and b/vault/img/secret-database.png differ
diff --git a/vault/pom.xml b/vault/pom.xml
new file mode 100644
index 0000000..03b80d3
--- /dev/null
+++ b/vault/pom.xml
@@ -0,0 +1,128 @@
+<?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 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.camel.springboot.example</groupId>
+ <artifactId>examples</artifactId>
+ <version>4.7.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>camel-example-spring-boot-vault</artifactId>
+ <name>Camel SB Examples :: HashiCorp Vault</name>
+ <description>Camel bean definition, JDBC DataSource connection, using HashiCorp Vault</description>
+
+ <properties>
+ <category>Bean Configuration</category>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+ </properties>
+
+
+ <dependencyManagement>
+ <dependencies>
+ <!-- Camel BOM -->
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-spring-boot-bom</artifactId>
+ <version>${project.version}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ <!-- Spring Boot BOM -->
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-dependencies</artifactId>
+ <version>${spring-boot-version}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+
+
+ <dependencies>
+
+ <!-- Spring Boot -->
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-web</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-actuator</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-jdbc</artifactId>
+ </dependency>
+
+ <!-- Camel -->
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-spring-boot-starter</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-platform-http-starter</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-spring-jdbc-starter</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-hashicorp-vault-starter</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-yaml-dsl-starter</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-jsonpath-starter</artifactId>
+ </dependency>
+
+ <!-- db driver -->
+ <dependency>
+ <groupId>org.postgresql</groupId>
+ <artifactId>postgresql</artifactId>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <defaultGoal>spring-boot:run</defaultGoal>
+ <plugins>
+ <plugin>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-maven-plugin</artifactId>
+ <version>${spring-boot-version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>repackage</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/vault/readme.adoc b/vault/readme.adoc
new file mode 100644
index 0000000..265e9b0
--- /dev/null
+++ b/vault/readme.adoc
@@ -0,0 +1,49 @@
+== Camel Example HashiCorp Vault
+
+This example shows how `camel-hashicorp-vault` can be used to configure beans using data from HashiCorp Vault
+
+=== How to run
+Run HashiCorp Vault
+
+ docker run --cap-add=IPC_LOCK -e 'VAULT_DEV_ROOT_TOKEN_ID=myroot' -e 'VAULT_DEV_LISTEN_ADDRESS=0.0.0.0:8200' -p 8200:8200 hashicorp/vault:1.16.1`
+
+Go to `http://localhost:8200/ui/vault/secrets/secret/kv/list` login with method Token and token `myroot` and create the following secrets:
+
+* a secret named `myDatabase`
+* a secret data `myPassword` with value `mysecretpassword`
+* a secret data `myUsername` with value `postgres`
+* a secret data `myJdbcURL` with value `jdbc:postgresql://localhost:5432/postgres`
+
+image::img/secret-database.png[]
+
+Run the database container
+
+ docker run -p 5432:5432 --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword postgres
+
+You can run this example using
+
+ mvn spring-boot:run
+
+To execute the routes:
+
+1. verify the table is empty (no data on response)
+
+ curl http://localhost:8080/api/cars
+
+2. insert data
+
+ curl --header "Content-Type: application/json" --request POST --data '{"brand": "Ford", "model": "Mustang", "year": 2024}' http://localhost:8080/api/cars
+
+3. verify the data has been persisted
+
+ curl http://localhost:8080/api/cars
+
+=== 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/vault/src/main/java/org/apache/camel/example/spring/vault/CamelSpringVaultApplication.java b/vault/src/main/java/org/apache/camel/example/spring/vault/CamelSpringVaultApplication.java
new file mode 100644
index 0000000..8dc0929
--- /dev/null
+++ b/vault/src/main/java/org/apache/camel/example/spring/vault/CamelSpringVaultApplication.java
@@ -0,0 +1,29 @@
+/*
+ * 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.spring.vault;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+
+@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
+public class CamelSpringVaultApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(CamelSpringVaultApplication.class, args);
+ }
+}
diff --git a/vault/src/main/resources/application.properties b/vault/src/main/resources/application.properties
new file mode 100644
index 0000000..36539e8
--- /dev/null
+++ b/vault/src/main/resources/application.properties
@@ -0,0 +1,27 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+camel.main.name = camel-spring-boot-vault-example
+
+# Camel Vault properties configuration
+camel.vault.hashicorp.token = myroot
+camel.vault.hashicorp.host = localhost
+camel.vault.hashicorp.port = 8200
+camel.vault.hashicorp.scheme = http
+
+# YAML DSL configuration
+camel.main.routes-include-pattern=camel/*.yaml
diff --git a/vault/src/main/resources/camel/cars-api.yaml b/vault/src/main/resources/camel/cars-api.yaml
new file mode 100644
index 0000000..2a3c64d
--- /dev/null
+++ b/vault/src/main/resources/camel/cars-api.yaml
@@ -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.
+
+- rest:
+ path: api
+ get:
+ - path: cars
+ to: direct:get
+ produces: text/plain
+ post:
+ - path: cars
+ to: direct:create
+ consumes: application/json
+ produces: text/plain
diff --git a/vault/src/main/resources/camel/cars-datasource.yaml b/vault/src/main/resources/camel/cars-datasource.yaml
new file mode 100644
index 0000000..746c969
--- /dev/null
+++ b/vault/src/main/resources/camel/cars-datasource.yaml
@@ -0,0 +1,25 @@
+# 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.
+
+- beans:
+ - name: datasource
+ type: org.springframework.jdbc.datasource.DriverManagerDataSource
+ properties:
+ driverClassName: 'org.postgresql.Driver'
+ url: '{{hashicorp:secret:database/myJdbcURL}}'
+ username: '{{hashicorp:secret:database/myUsername}}'
+ password: '{{hashicorp:secret:database/myPassword}}'
diff --git a/vault/src/main/resources/camel/cars-routes.yaml b/vault/src/main/resources/camel/cars-routes.yaml
new file mode 100644
index 0000000..8d7e207
--- /dev/null
+++ b/vault/src/main/resources/camel/cars-routes.yaml
@@ -0,0 +1,71 @@
+# 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.
+
+# Create database route at startup
+- route:
+ from:
+ uri: timer
+ parameters:
+ timerName: create-database
+ fixedRate: false
+ repeatCount: "1"
+ steps:
+ - setBody:
+ expression:
+ constant:
+ expression: |-
+ CREATE TABLE IF NOT EXISTS cars (
+ brand VARCHAR(255),
+ model VARCHAR(255),
+ year INT
+ );
+ - to:
+ uri: jdbc
+ parameters:
+ dataSourceName: datasource
+# Add car
+- route:
+ from:
+ uri: direct
+ parameters:
+ name: create
+ steps:
+ - setBody:
+ expression:
+ simple:
+ expression: >-
+ INSERT INTO cars (brand, model, year)
+ VALUES ('${jsonpath($.brand)}', '${jsonpath($.model)}', ${jsonpath($.year)});
+ - to:
+ uri: jdbc
+ parameters:
+ dataSourceName: datasource
+# Get all cars
+- route:
+ from:
+ uri: direct
+ parameters:
+ name: get
+ steps:
+ - setBody:
+ expression:
+ simple:
+ expression: SELECT * FROM cars
+ - to:
+ uri: jdbc
+ parameters:
+ dataSourceName: datasource