Developer quick start
This quick start will explore how to use Vault client libraries inside your application code to store and retrieve your first secret value. Vault takes the security burden away from developers by providing a secure, centralized secret store for an application’s sensitive data: credentials, certificates, encryption keys, and more.
The complete code samples for the steps below are available here:
For an out-of-the-box runnable demo application showcasing these concepts and more, see the hello-vault repositories (Go, C# and Java/Spring Boot).
Prerequisites
- Docker or a local installation of the Vault binary
- A development environment applicable to one of the languages in this quick start (currently Go, Ruby, C#, Python, Java (Spring), and Bash (curl))
Note: Make sure you are using the latest version of Docker. Older versions may not work. As of 1.12.0, the recommended version of Docker is 20.10.17 or higher.
Step 1: start Vault
Warning: This in-memory “dev” server is useful for practicing with Vault locally for the first time, but is insecure and should never be used in production. For developers who need to manage their own production Vault installations, this page provides some guidance on how to make your setup more production-friendly.
Run the Vault server in a non-production "dev" mode in one of the following ways:
For Docker users, run this command:
$ docker run -p 8200:8200 -e 'VAULT_DEV_ROOT_TOKEN_ID=dev-only-token' hashicorp/vault
For non-Docker users, run this command:
$ vault server -dev -dev-root-token-id="dev-only-token"
The -dev-root-token-id
flag for dev servers tells the Vault server to allow full root access to anyone who presents a token with the specified value (in this case "dev-only-token").
Warning: The root token is useful for development, but allows full access to all data and functionality of Vault, so it must be carefully guarded in production. Ideally, even an administrator of Vault would use their own token with limited privileges instead of the root token.
Vault is now listening over HTTP on port 8200. With all the setup out of the way, it's time to get coding!
Step 2: install a client library
To read and write secrets in your application, you need to first configure a client to connect to Vault. Let's install the Vault client library for your language of choice.
Note: Some of these libraries are currently community-maintained.
Go (official) client library:
$ go get github.com/hashicorp/vault/api
Now, let's add the import statements for the client library to the top of the file.
import statements for client library
import vault "github.com/hashicorp/vault/api"
Step 3: authenticate to Vault
A variety of authentication methods can be used to prove your application's identity to the Vault server. To explore more secure authentication methods, such as via Kubernetes or your cloud provider, see the auth code snippets in the vault-examples repository.
To keep things simple for our example, we'll just use the root token created in Step 1. Paste the following code to initialize a new Vault client that will use token-based authentication for all its requests:
config := vault.DefaultConfig()Â config.Address = "http://127.0.0.1:8200"Â client, err := vault.NewClient(config)if err != nil { log.Fatalf("unable to initialize Vault client: %v", err)}Â client.SetToken("dev-only-token")
Step 4: store a secret
Secrets are sensitive data like API keys and passwords that we shouldn’t be storing in our code or configuration files. Instead, we want to store values like this in Vault.
We'll use the Vault client we just initialized to write a secret to Vault, like so:
secretData := map[string]interface{}{ "password": "Hashi123",}Â Â _, err = client.KVv2("secret").Put(context.Background(), "my-secret-password", secretData)if err != nil { log.Fatalf("unable to write secret: %v", err)}Â fmt.Println("Secret written successfully.")
A common way of storing secrets is as key-value pairs using the KV secrets engine (v2). In the code we've just added, password
is the key in the key-value pair, and Hashi123
is the value.
We also provided the path to our secret in Vault. We will reference this path in a moment when we learn how to retrieve our secret.
Run the code now, and you should see Secret written successfully
. If not, check that you've used the correct value for the root token and Vault server address.
Step 5: retrieve a secret
Now that we know how to write a secret, let's practice reading one.
Underneath the line where you wrote a secret to Vault, let's add a few more lines, where we will be retrieving the secret and unpacking the value:
secret, err := client.KVv2("secret").Get(context.Background(), "my-secret-password")if err != nil { log.Fatalf("unable to read secret: %v", err)}Â value, ok := secret.Data["password"].(string)if !ok {log.Fatalf("value type assertion failed: %T %#v", secret.Data["password"], secret.Data["password"])}
Last, confirm that the value we unpacked from the read response is correct:
if value != "Hashi123" { log.Fatalf("unexpected password value %q retrieved from vault", value)}Â fmt.Println("Access granted!")
If the secret was fetched successfully, you should see the Access granted!
message after you run the code. If not, check to see if you provided the correct path to your secret.
That's it! You've just written and retrieved your first Vault secret!
Additional examples
For more secure examples of client authentication, see the auth snippets in the vault-examples repo.
For a runnable demo app that demonstrates more features, for example, how to keep your connection to Vault alive and how to connect to a database using Vault's dynamic database credentials, see the sample application hello-vault (Go, C#).
To learn how to integrate applications with Vault without needing to always change your application code, see the Vault Agent documentation.