Vault Secrets in the Pipeline
Accessing and Referring to Vault Secrets in the Pipeline
Codefresh offers a Vault plugin you may use from the Step Marketplace. The plugin imports key-value pairs from the Vault server, and exports them into the pipeline.
- A free Codefresh account
- An existing Vault server already setup
- A secret stored in said Vault server with a key of “password”
- A Vault authorization token
The Example Java Application
You can find the example project on GitHub.
The example application retrieves the system variable “password,” from the pipeline and uses it to authenticate to a Redis database, but you are free to use any type of database of your choosing.
String password = System.getenv("password"); String host = System.getProperty("server.host"); RedisClient redisClient = new RedisClient( RedisURI.create("redis://" + password + "@" + host + ":6379")); RedisConnection<String, String> connection = redisClient.connect();
Also in the example application is a simple unit test that ensures we are able to read and write data to the database.
You cannot run the application locally, as it needs to run in the pipeline in order to use our environment variables to connect.
Create the Pipeline
You should be able to copy and paste this YAML in the in-line editor of the Codefresh UI. It will automatically clone the project for you.
Note that you need to change the
VAULT_AUTH_TOKEN arguments under the first step to your respective values.
version: "1.0" stages: - "vault" - "clone" - "package" steps: vault: title: Importing vault values... stage: "vault" type: vault arguments: VAULT_ADDR: 'http://<YOUR_VAULT_SERVER_IP>:<PORT>' VAULT_PATH: 'path/to/secret' VAULT_AUTH_TOKEN: '<YOUR_VAULT_AUTH_TOKEN>' clone: title: Cloning main repository... type: git-clone arguments: repo: 'codefresh-contrib/vault-sample-app' git: github stage: clone package_jar: title: Packaging jar and running unit tests... stage: package working_directory: $ arguments: image: maven:3.5.2-jdk-8-alpine commands: - mvn -Dmaven.repo.local=/codefresh/volume/m2_repository -Dserver.host=my-redis-db-host clean package services: composition: my-redis-db-host: image: 'redis:4-alpine' command: 'redis-server --requirepass $password' ports: - 6379
The above pipeline does the following:
- Imports the key-value pairs from the Vault server and exports them into the pipeline under
- Clones the main repository (note the special use of naming the step
main_clone). This ensures that all subsequent commands are run inside the project that was checked out.
- The last step,
package_jar, does a few special things to take note of:
- Spins up a Service Container running Redis on port 6379 , and sets the password to the database using our exported environment variable
maven.repo.localto cache Maven dependencies into the local codefresh volume to speed up builds
- Runs unit tests and packages the jar. Note how you can directly refer to the service container’s name (
my-redis-db-host) when we set
You will see that the variable was correctly exported to the pipeline by running a simple