Pete Freitag Pete Freitag

Using Hashicorp Vault with ColdFusion


Hashicorp Vault is an open source, enterprise grade security vault. It is designed to grant secure access to the secrets that it stores. It can also act as an encryption as a service API. Vault is very powerful, and there are lots of resources and videos describing how it works.

Using Vault is something that I often cover in my ColdFusion security training course, however I'd like to give an example of how you can use vault in your ColdFusion or CFML / Lucee applications.

Start a vault server

For this example to keep things simple we will just run vault locally in dev mode. After you have downloaded the vault binary, start a dev mode server like this:

vault server -dev

Use the vault CLI to store a secret

Now that we have a vault dev server running, we can store a secret in it. First we have to set an environment variable to tell the vault CLI where the server is located, in our case vault started the server on port 8200:

export VAULT_ADDR=''

The export command will only work on linux or mac, if you are running vault on Windows you'll need to set an environment variable and restart your Command Prompt.

Now that vault knows where the server is, we can use the vault key value store to store (or PUT) a secret:

vault kv put secret/hello first=pete last=freitag

Our secret is now securely stored in vault. To test that we really stored it, we can retrieve it from the command line like this:

vault kv get secret/hello

You'll see some metadata as well as your secret values output.

Creating a vault token

Now since we are in dev mode, vault is just spewing secrets to anyone that can connect to it. In a production configuration vault will require a VAULT_TOKEN to connect to it. So let's create the token and we'll use that when we write out code.

vault token create

Copy the token that is output and store it as an environment variable named VAULT_TOKEN.

Connecting to Vault from ColdFusion

Now that we have a vault server, and a vault token - let's see if we can connect to vault from our ColdFusion server. Here, we're going to hit the /v1/sys/init endpoint to see if the server is initialized:

<cfhttp url="#server.system.environment.VAULT_ADDR#/v1/sys/init" method="GET" result="vaultResponse">
    <cfhttpparam type="header" name="X-Vault-Token" value="#server.system.environment.VAULT_TOKEN#">

If the vault server is initialized we should get a JSON response like this:


If it is initialized, we can retrieve the secret we restored via the endpoint /v1/secret/data/hello

<cfhttp url="#server.system.environment.VAULT_ADDR#/v1/secret/data/hello" method="GET" result="vaultResponse">
    <cfhttpparam type="header" name="X-Vault-Token" value="#server.system.environment.VAULT_TOKEN#">

Our response will be something like this:


We can retrieve our secret in CFML by calling deserializeJSON, eg:

<cfset secrets = deserializeJSON(vaultResponse.fileContent)>
  First Name:
  Last Name:

All if this is just scratching the surface of what Hashicorp Vault can do, but hopefully it helps you jumpstart your implementation.

Like this? Follow me ↯

Using Hashicorp Vault with ColdFusion was first published on January 29, 2021.

If you like reading about vault, secrets, hashicorp, coldfusion, or cfml then you might also like:

FuseGuard Web App Firewall for ColdFusion

The FuseGuard Web Application Firewall for ColdFusion & CFML is a high performance, customizable engine that blocks various attacks against your ColdFusion applications.

Post a Comment