Skip to content

Commit

Permalink
Bean configuration via HashiCorp Vault example
Browse files Browse the repository at this point in the history
  • Loading branch information
Croway committed Jun 12, 2024
1 parent 41d61f5 commit 1d3c772
Show file tree
Hide file tree
Showing 10 changed files with 361 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
|===
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<module>widget-gadget</module>
<module>azure</module>
<module>variables</module>
<module>vault</module>
</modules>

<properties>
Expand Down
Binary file added vault/img/secret-database.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
128 changes: 128 additions & 0 deletions vault/pom.xml
Original file line number Diff line number Diff line change
@@ -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>
49 changes: 49 additions & 0 deletions vault/readme.adoc
Original file line number Diff line number Diff line change
@@ -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!
Original file line number Diff line number Diff line change
@@ -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);
}
}
27 changes: 27 additions & 0 deletions vault/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions vault/src/main/resources/camel/cars-api.yaml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions vault/src/main/resources/camel/cars-datasource.yaml
Original file line number Diff line number Diff line change
@@ -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}}'
71 changes: 71 additions & 0 deletions vault/src/main/resources/camel/cars-routes.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1d3c772

Please sign in to comment.