Skip to content

Commit 1d3c772

Browse files
committed
Bean configuration via HashiCorp Vault example
1 parent 41d61f5 commit 1d3c772

File tree

10 files changed

+361
-1
lines changed

10 files changed

+361
-1
lines changed

README.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ readme's instructions.
2727
=== Examples
2828

2929
// examples: START
30-
Number of Examples: 57 (0 deprecated)
30+
Number of Examples: 58 (0 deprecated)
3131

3232
[width="100%",cols="4,2,4",options="header"]
3333
|===
@@ -150,6 +150,8 @@ Number of Examples: 57 (0 deprecated)
150150
| link:jira/README.adoc[Jira] (jira) | SaaS | An example that uses Jira Camel API
151151

152152
| link:twitter-salesforce/README.adoc[Twitter Salesforce] (twitter-salesforce) | SaaS | Twitter mentions is created as contacts in Salesforce
153+
154+
| link:vault/README.adoc[HashiCorp Vault] (vault) | Bean Configuration | Configure a DataSource using HashiCorp Vault
153155
|===
154156
// examples: END
155157

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
<module>widget-gadget</module>
9393
<module>azure</module>
9494
<module>variables</module>
95+
<module>vault</module>
9596
</modules>
9697

9798
<properties>

vault/img/secret-database.png

47.1 KB
Loading

vault/pom.xml

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Licensed to the Apache Software Foundation (ASF) under one or more
5+
contributor license agreements. See the NOTICE file distributed with
6+
this work for additional information regarding copyright ownership.
7+
The ASF licenses this file to You under the Apache License, Version 2.0
8+
(the "License"); you may not use this file except in compliance with
9+
the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
<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">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<parent>
24+
<groupId>org.apache.camel.springboot.example</groupId>
25+
<artifactId>examples</artifactId>
26+
<version>4.7.0-SNAPSHOT</version>
27+
</parent>
28+
29+
<artifactId>camel-example-spring-boot-vault</artifactId>
30+
<name>Camel SB Examples :: HashiCorp Vault</name>
31+
<description>Camel bean definition, JDBC DataSource connection, using HashiCorp Vault</description>
32+
33+
<properties>
34+
<category>Bean Configuration</category>
35+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
36+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
37+
</properties>
38+
39+
40+
<dependencyManagement>
41+
<dependencies>
42+
<!-- Camel BOM -->
43+
<dependency>
44+
<groupId>org.apache.camel.springboot</groupId>
45+
<artifactId>camel-spring-boot-bom</artifactId>
46+
<version>${project.version}</version>
47+
<type>pom</type>
48+
<scope>import</scope>
49+
</dependency>
50+
<!-- Spring Boot BOM -->
51+
<dependency>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-dependencies</artifactId>
54+
<version>${spring-boot-version}</version>
55+
<type>pom</type>
56+
<scope>import</scope>
57+
</dependency>
58+
</dependencies>
59+
</dependencyManagement>
60+
61+
62+
<dependencies>
63+
64+
<!-- Spring Boot -->
65+
<dependency>
66+
<groupId>org.springframework.boot</groupId>
67+
<artifactId>spring-boot-starter-web</artifactId>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.springframework.boot</groupId>
71+
<artifactId>spring-boot-starter-actuator</artifactId>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.springframework.boot</groupId>
75+
<artifactId>spring-boot-starter-jdbc</artifactId>
76+
</dependency>
77+
78+
<!-- Camel -->
79+
<dependency>
80+
<groupId>org.apache.camel.springboot</groupId>
81+
<artifactId>camel-spring-boot-starter</artifactId>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.apache.camel.springboot</groupId>
85+
<artifactId>camel-platform-http-starter</artifactId>
86+
</dependency>
87+
<dependency>
88+
<groupId>org.apache.camel.springboot</groupId>
89+
<artifactId>camel-spring-jdbc-starter</artifactId>
90+
</dependency>
91+
<dependency>
92+
<groupId>org.apache.camel.springboot</groupId>
93+
<artifactId>camel-hashicorp-vault-starter</artifactId>
94+
</dependency>
95+
<dependency>
96+
<groupId>org.apache.camel.springboot</groupId>
97+
<artifactId>camel-yaml-dsl-starter</artifactId>
98+
</dependency>
99+
<dependency>
100+
<groupId>org.apache.camel.springboot</groupId>
101+
<artifactId>camel-jsonpath-starter</artifactId>
102+
</dependency>
103+
104+
<!-- db driver -->
105+
<dependency>
106+
<groupId>org.postgresql</groupId>
107+
<artifactId>postgresql</artifactId>
108+
</dependency>
109+
</dependencies>
110+
111+
<build>
112+
<defaultGoal>spring-boot:run</defaultGoal>
113+
<plugins>
114+
<plugin>
115+
<groupId>org.springframework.boot</groupId>
116+
<artifactId>spring-boot-maven-plugin</artifactId>
117+
<version>${spring-boot-version}</version>
118+
<executions>
119+
<execution>
120+
<goals>
121+
<goal>repackage</goal>
122+
</goals>
123+
</execution>
124+
</executions>
125+
</plugin>
126+
</plugins>
127+
</build>
128+
</project>

vault/readme.adoc

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
== Camel Example HashiCorp Vault
2+
3+
This example shows how `camel-hashicorp-vault` can be used to configure beans using data from HashiCorp Vault
4+
5+
=== How to run
6+
Run HashiCorp Vault
7+
8+
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`
9+
10+
Go to `http://localhost:8200/ui/vault/secrets/secret/kv/list` login with method Token and token `myroot` and create the following secrets:
11+
12+
* a secret named `myDatabase`
13+
* a secret data `myPassword` with value `mysecretpassword`
14+
* a secret data `myUsername` with value `postgres`
15+
* a secret data `myJdbcURL` with value `jdbc:postgresql://localhost:5432/postgres`
16+
17+
image::img/secret-database.png[]
18+
19+
Run the database container
20+
21+
docker run -p 5432:5432 --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword postgres
22+
23+
You can run this example using
24+
25+
mvn spring-boot:run
26+
27+
To execute the routes:
28+
29+
1. verify the table is empty (no data on response)
30+
31+
curl http://localhost:8080/api/cars
32+
33+
2. insert data
34+
35+
curl --header "Content-Type: application/json" --request POST --data '{"brand": "Ford", "model": "Mustang", "year": 2024}' http://localhost:8080/api/cars
36+
37+
3. verify the data has been persisted
38+
39+
curl http://localhost:8080/api/cars
40+
41+
=== Help and contributions
42+
43+
If you hit any problem using Camel or have some feedback, then please
44+
https://camel.apache.org/support.html[let us know].
45+
46+
We also love contributors, so
47+
https://camel.apache.org/contributing.html[get involved] :-)
48+
49+
The Camel riders!
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.example.spring.vault;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
22+
23+
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
24+
public class CamelSpringVaultApplication {
25+
26+
public static void main(String[] args) {
27+
SpringApplication.run(CamelSpringVaultApplication.class, args);
28+
}
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## ---------------------------------------------------------------------------
2+
## Licensed to the Apache Software Foundation (ASF) under one or more
3+
## contributor license agreements. See the NOTICE file distributed with
4+
## this work for additional information regarding copyright ownership.
5+
## The ASF licenses this file to You under the Apache License, Version 2.0
6+
## (the "License"); you may not use this file except in compliance with
7+
## the License. You may obtain a copy of the License at
8+
##
9+
## http://www.apache.org/licenses/LICENSE-2.0
10+
##
11+
## Unless required by applicable law or agreed to in writing, software
12+
## distributed under the License is distributed on an "AS IS" BASIS,
13+
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
## See the License for the specific language governing permissions and
15+
## limitations under the License.
16+
## ---------------------------------------------------------------------------
17+
18+
camel.main.name = camel-spring-boot-vault-example
19+
20+
# Camel Vault properties configuration
21+
camel.vault.hashicorp.token = myroot
22+
camel.vault.hashicorp.host = localhost
23+
camel.vault.hashicorp.port = 8200
24+
camel.vault.hashicorp.scheme = http
25+
26+
# YAML DSL configuration
27+
camel.main.routes-include-pattern=camel/*.yaml
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
- rest:
19+
path: api
20+
get:
21+
- path: cars
22+
to: direct:get
23+
produces: text/plain
24+
post:
25+
- path: cars
26+
to: direct:create
27+
consumes: application/json
28+
produces: text/plain
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
- beans:
19+
- name: datasource
20+
type: org.springframework.jdbc.datasource.DriverManagerDataSource
21+
properties:
22+
driverClassName: 'org.postgresql.Driver'
23+
url: '{{hashicorp:secret:database/myJdbcURL}}'
24+
username: '{{hashicorp:secret:database/myUsername}}'
25+
password: '{{hashicorp:secret:database/myPassword}}'
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# Create database route at startup
19+
- route:
20+
from:
21+
uri: timer
22+
parameters:
23+
timerName: create-database
24+
fixedRate: false
25+
repeatCount: "1"
26+
steps:
27+
- setBody:
28+
expression:
29+
constant:
30+
expression: |-
31+
CREATE TABLE IF NOT EXISTS cars (
32+
brand VARCHAR(255),
33+
model VARCHAR(255),
34+
year INT
35+
);
36+
- to:
37+
uri: jdbc
38+
parameters:
39+
dataSourceName: datasource
40+
# Add car
41+
- route:
42+
from:
43+
uri: direct
44+
parameters:
45+
name: create
46+
steps:
47+
- setBody:
48+
expression:
49+
simple:
50+
expression: >-
51+
INSERT INTO cars (brand, model, year)
52+
VALUES ('${jsonpath($.brand)}', '${jsonpath($.model)}', ${jsonpath($.year)});
53+
- to:
54+
uri: jdbc
55+
parameters:
56+
dataSourceName: datasource
57+
# Get all cars
58+
- route:
59+
from:
60+
uri: direct
61+
parameters:
62+
name: get
63+
steps:
64+
- setBody:
65+
expression:
66+
simple:
67+
expression: SELECT * FROM cars
68+
- to:
69+
uri: jdbc
70+
parameters:
71+
dataSourceName: datasource

0 commit comments

Comments
 (0)