Spring Cloud Vault
Spring Cloud Vault
Spring Cloud Vault
Table of Contents
1. Quick Start
3. Authentication methods
3.1. Token authentication
3.2. AppId authentication
3.2.1. Custom UserId
4. Secret Backends
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 1/29
2/4/2018 Spring Cloud Vault
5. Database backends
5.1. Database
5.2. Apache Cassandra
5.3. MongoDB
5.4. MySQL
5.5. PostgreSQL
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 2/29
2/4/2018 Spring Cloud Vault
Copies of this document may be made for your own use and for distribution to
others, provided that you do not charge any fee for such copies and further
provided that each copy contains this Copyright Notice, whether distributed in
print or electronically.
Spring Cloud Vault Config provides client-side support for externalized configuration in a
distributed system. With HashiCorp’s Vault you have a central place to manage external secret
properties for applications across all environments. Vault can manage static and dynamic
secrets such as username/password for remote applications/resources and provide credentials
for external services such as MySQL, PostgreSQL, Apache Cassandra, MongoDB, Consul,
AWS and more.
1. Quick Start
Prerequisites
To get started with Vault and this guide you need a *NIX-like operating systems that provides:
Install Vault
$ src/test/bash/install_vault.sh
$ src/test/bash/create_certificates.sh
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 3/29
2/4/2018 Spring Cloud Vault
spring.cloud.vault.ssl.trust-store property to
file:work/keystore.jks .
$ src/test/bash/local_run_vault.sh
Vault is started listening on 0.0.0.0:8200 using the inmem storage and https . Vault is
sealed and not initialized when starting up.
If you want to run tests, leave Vault uninitialized. The tests will initialize Vault and
create a root token 00000000-0000-0000-0000-000000000000 .
If you want to use Vault for your application or give it a try then you need to initialize it first.
$ export VAULT_ADDR="https://localhost:8200"
$ export VAULT_SKIP_VERIFY=true # Don't do this for production
$ vault init
Key 1: 7149c6a2e16b8833f6eb1e76df03e47f6113a3288b3093faf5033d44f0e70fe701
Key 2: 901c534c7988c18c20435a85213c683bdcf0efcd82e38e2893779f152978c18c02
Key 3: 03ff3948575b1165a20c20ee7c3e6edf04f4cdbe0e82dbff5be49c63f98bc03a03
Key 4: 216ae5cc3ddaf93ceb8e1d15bb9fc3176653f5b738f5f3d1ee00cd7dccbe926e04
Key 5: b2898fc8130929d569c1677ee69dc5f3be57d7c4b494a6062693ce0b1c4d93d805
Initial Root Token: 19aefa97-cccc-bbbb-aaaa-225940e63d76
Vault does not store the master key. Without at least 3 keys,
your Vault will remain permanently sealed.
Vault will initialize and return a set of unsealing keys and the root token. Pick 3 keys and unseal
Vault. Store the Vault token in the VAULT_TOKEN environment variable.
Spring Cloud Vault accesses different resources. By default, the secret backend is enabled
which accesses secret config settings via JSON endpoints.
/secret/{application}/{profile}
/secret/{application}
/secret/{defaultContext}/{profile}
/secret/{defaultContext}
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-vault-config</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 5/29
2/4/2018 Spring Cloud Vault
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Then you can create a standard Spring Boot application, like this simple HTTP server:
@SpringBootApplication
@RestController
public class Application {
@RequestMapping("/")
public String home() {
return "Hello World!";
}
When it runs it will pick up the external configuration from the default local Vault server on port
8200 if it is running. To modify the startup behavior you can change the location of the Vault
server using bootstrap.properties (like application.properties but for the bootstrap
phase of an application context), e.g.
spring.cloud.vault:
host: localhost
port: 8200
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 6/29
2/4/2018 Spring Cloud Vault
scheme: https
uri: https://localhost:8200
connection-timeout: 5000
read-timeout: 15000
config:
order: -10
host sets the hostname of the Vault host. The host name will be used for SSL certificate
validation
port sets the Vault port
scheme setting the scheme to http will use plain HTTP. Supported schemes are http
and https .
uri configure the Vault endpoint with an URI. Takes precedence over host/port/scheme
configuration
connection-timeout sets the connection timeout in milliseconds
read-timeout sets the read timeout in milliseconds
config.order sets the order for the property source
If the application imports the spring-boot-starter-actuator project, the status of the vault
server will be available via the /health endpoint.
The vault health indicator can be enabled or disabled through the property
health.vault.enabled (default true ).
2.1 Authentication
Vault requires an authentication mechanism to authorize client requests.
For a quickstart, use the root token printed by the Vault initialization.
spring.cloud.vault:
token: 19aefa97-cccc-bbbb-aaaa-225940e63d76
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 7/29
2/4/2018 Spring Cloud Vault
3. Authentication methods
Different organizations have different requirements for security and authentication. Vault reflects
that need by shipping multiple authentication methods. Spring Cloud Vault supports token and
AppId authentication.
spring.cloud.vault:
authentication: TOKEN
token: 00000000-0000-0000-0000-000000000000
authentication setting this value to TOKEN selects the Token authentication method
token sets the static token to use
spring.cloud.vault:
authentication: APPID
app-id:
user-id: IP_ADDRESS
authentication setting this value to APPID selects the AppId authentication method
app-id-path sets the path of the AppId mount to use
user-id sets the UserId method. Possible values are IP_ADDRESS , MAC_ADDRESS or a
class name implementing a custom AppIdUserIdMechanism
The corresponding command to generate the IP address UserId from a command line is:
Including the line break of echo leads to a different hash value so make sure to
include the -n flag.
Mac address-based UserId’s obtain their network device from the localhost-bound device. The
configuration also allows specifying a network-interface hint to pick the right device. The
value of network-interface is optional and can be either an interface name or interface
index (0-based).
spring.cloud.vault:
authentication: APPID
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 9/29
2/4/2018 Spring Cloud Vault
app-id:
user-id: MAC_ADDRESS
network-interface: eth0
The corresponding command to generate the IP address UserId from a command line is:
The Mac address is specified uppercase and without colons. Including the line
break of echo leads to a different hash value so make sure to include the -n
flag.
spring.cloud.vault:
authentication: APPID
app-id:
user-id: com.examlple.MyUserIdMechanism
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 10/29
2/4/2018 Spring Cloud Vault
@Override
public String createUserId() {
String userId = ...
return userId;
}
}
Spring Vault supports AppRole authentication by providing either RoleId only or together with a
provided SecretId (push or pull mode).
RoleId and optionally SecretId must be provided by configuration, Spring Vault will not look up
these or create a custom SecretId.
spring.cloud.vault:
authentication: APPROLE
app-role:
role-id: bde2076b-cccb-3cf0-d57e-bca7b1e83a52
spring.cloud.vault:
authentication: APPROLE
app-role:
role-id: bde2076b-cccb-3cf0-d57e-bca7b1e83a52
secret-id: 1696536f-1976-73b1-b241-0b4213908d39
app-auth-path: approle
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 11/29
2/4/2018 Spring Cloud Vault
spring.cloud.vault:
authentication: AWS_EC2
AWS-EC2 authentication enables nonce by default to follow the Trust On First Use (TOFU)
principle. Any unintended party that gains access to the PKCS#7 identity metadata can
authenticate against Vault.
During the first login, Spring Cloud Vault generates a nonce that is stored in the auth backend
aside the instance Id. Re-authentication requires the same nonce to be sent. Any other party
does not have the nonce and can raise an alert in Vault for further investigation.
The nonce is kept in memory and is lost during application restart. You can configure a static
nonce with spring.cloud.vault.aws-ec2.nonce .
AWS-EC2 authentication roles are optional and default to the AMI. You can configure the
authentication role by setting the spring.cloud.vault.aws-ec2.role property.
spring.cloud.vault:
authentication: AWS_EC2
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 12/29
2/4/2018 Spring Cloud Vault
aws-ec2:
role: application-server
spring.cloud.vault:
authentication: AWS_EC2
aws-ec2:
role: application-server
aws-ec2-path: aws-ec2
identity-document: http://...
nonce: my-static-nonce
authentication setting this value to AWS_EC2 selects the AWS EC2 authentication
method
role sets the name of the role against which the login is being attempted.
aws-ec2-path sets the path of the AWS EC2 mount to use
identity-document sets URL of the PKCS#7 AWS EC2 identity document
nonce used for AWS-EC2 authentication. An empty nonce defaults to nonce generation
The current IAM role the application is running in is automatically calculated. If you are running
your application on AWS ECS then the application will use the IAM role assigned to the ECS
task of the running container. If you are running your application naked on top of an EC2
instance then the IAM role used will be the one assigned to the EC2 instance.
When using the AWS-IAM authentication you must create a role in Vault and assign it to your
IAM role. An empty role defaults to the friendly name the current IAM role.
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 13/29
2/4/2018 Spring Cloud Vault
spring.cloud.vault:
authentication: AWS_IAM
spring.cloud.vault:
authentication: AWS_IAM
aws-iam:
role: my-dev-role
aws-path: aws
server-id: some.server.name
role sets the name of the role against which the login is being attempted. This should be
bound to your IAM role. If one is not supplied then the friendly name of the current IAM user
will be used as the vault role.
aws-path sets the path of the AWS mount to use
server-id sets the value to use for the X-Vault-AWS-IAM-Server-ID header
preventing certain types of replay attacks.
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 14/29
2/4/2018 Spring Cloud Vault
spring.cloud.vault:
authentication: CERT
ssl:
key-store: classpath:keystore.jks
key-store-password: changeit
cert-auth-path: cert
spring.cloud.vault:
authentication: CUBBYHOLE
token: 397ccb93-ff6c-b17b-9389-380b01ca2645
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 15/29
2/4/2018 Spring Cloud Vault
See also:
A file containing a JWT token for a pod’s service account is automatically mounted at
/var/run/secrets/kubernetes.io/serviceaccount/token .
spring.cloud.vault:
authentication: KUBERNETES
kubernetes:
role: my-dev-role
service-account-token-file: /var/run/secrets/kubernetes.io/serviceaccount
See also:
4. Secret Backends
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 16/29
2/4/2018 Spring Cloud Vault
/secret/{application}/{profile}
/secret/{application}
/secret/{default-context}/{profile}
/secret/{default-context}
spring.cloud.vault.generic.application-name
spring.cloud.vault.application-name
spring.application.name
Secrets can be obtained from other folders within the generic backend by adding their paths to
the application name, separated by commas. For example, given the application name
usefulapp,mysql1,projectx/aws , each of these folders will be used:
/secret/usefulapp
/secret/mysql1
/secret/projectx/aws
Spring Cloud Vault adds all active profiles to the list of possible context paths. No active profiles
will skip accessing contexts with a profile name.
Properties are exposed like they are stored (i.e. without additional prefixes).
spring.cloud.vault:
generic:
enabled: true
backend: secret
profile-separator: '/'
default-context: application
application-name: my-app
enabled setting this value to false disables the secret backend config usage
backend sets the path of the secret mount to use
default-context sets the context name used by all applications
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 17/29
2/4/2018 Spring Cloud Vault
application-name overrides the application name for use in the generic backend
profile-separator separates the profile name from the context in property sources with
profiles
4.2 Consul
Spring Cloud Vault can obtain credentials for HashiCorp Consul. The Consul integration
requires the spring-cloud-vault-config-consul dependency.
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-vault-config-consul</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</dependency>
</dependencies>
spring.cloud.vault:
consul:
enabled: true
role: readonly
backend: consul
token-property: spring.cloud.consul.token
enabled setting this value to true enables the Consul backend config usage
role sets the role name of the Consul role definition
backend sets the path of the Consul mount to use
token-property sets the property name in which the Consul ACL token is stored
4.3 RabbitMQ
Spring Cloud Vault can obtain credentials for RabbitMQ.
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-vault-config-rabbitmq</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</dependency>
</dependencies>
spring.cloud.vault:
rabbitmq:
enabled: true
role: readonly
backend: rabbitmq
username-property: spring.rabbitmq.username
password-property: spring.rabbitmq.password
enabled setting this value to true enables the RabbitMQ backend config usage
role sets the role name of the RabbitMQ role definition
backend sets the path of the RabbitMQ mount to use
username-property sets the property name in which the RabbitMQ username is stored
password-property sets the property name in which the RabbitMQ password is stored
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 19/29
2/4/2018 Spring Cloud Vault
4.4 AWS
Spring Cloud Vault can obtain credentials for AWS.
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-vault-config-aws</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</dependency>
</dependencies>
The access key and secret key are stored in cloud.aws.credentials.accessKey and
cloud.aws.credentials.secretKey so using Spring Cloud AWS will pick up the generated
credentials without further configuration. You can configure the property names by setting
spring.cloud.vault.aws.access-key-property and
spring.cloud.vault.aws.secret-key-property .
spring.cloud.vault:
aws:
enabled: true
role: readonly
backend: aws
access-key-property: cloud.aws.credentials.accessKey
secret-key-property: cloud.aws.credentials.secretKey
enabled setting this value to true enables the AWS backend config usage
role sets the role name of the AWS role definition
backend sets the path of the AWS mount to use
access-key-property sets the property name in which the AWS access key is stored
secret-key-property sets the property name in which the AWS secret key is stored
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 20/29
2/4/2018 Spring Cloud Vault
5. Database backends
Vault supports several database secret backends to generate database credentials dynamically
based on configured roles. This means services that need to access a database no longer need
to configure credentials: they can request them from Vault, and use Vault’s leasing mechanism
to more easily roll keys.
Using a database secret backend requires to enable the backend in the configuration and the
spring-cloud-vault-config-databases dependency.
Vault ships since 0.7.1 with a dedicated database secret backend that allows database
integration via plugins. You can use that specific backend by using the generic database
backend. Make sure to specify the appropriate backend path, e.g.
spring.cloud.vault.mysql.role.backend=database .
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-vault-config-databases</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</dependency>
</dependencies>
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 21/29
2/4/2018 Spring Cloud Vault
5.1 Database
Spring Cloud Vault can obtain credentials for any database listed at
https://www.vaultproject.io/api/secret/databases/index.html. The integration can be enabled by
setting spring.cloud.vault.database.enabled=true (default false ) and providing the
role name with spring.cloud.vault.database.role=… .
spring.cloud.vault:
database:
enabled: true
role: readonly
backend: database
username-property: spring.datasource.username
password-property: spring.datasource.username
enabled setting this value to true enables the Database backend config usage
role sets the role name of the Database role definition
backend sets the path of the Database mount to use
username-property sets the property name in which the Database username is stored
password-property sets the property name in which the Database password is stored
Spring Cloud Vault can obtain credentials for Apache Cassandra. The integration can be
enabled by setting spring.cloud.vault.cassandra.enabled=true (default false ) and
providing the role name with spring.cloud.vault.cassandra.role=… .
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 22/29
2/4/2018 Spring Cloud Vault
spring.cloud.vault:
cassandra:
enabled: true
role: readonly
backend: cassandra
username-property: spring.data.cassandra.username
password-property: spring.data.cassandra.username
enabled setting this value to true enables the Cassandra backend config usage
role sets the role name of the Cassandra role definition
backend sets the path of the Cassandra mount to use
username-property sets the property name in which the Cassandra username is stored
password-property sets the property name in which the Cassandra password is stored
5.3 MongoDB
Spring Cloud Vault can obtain credentials for MongoDB. The integration can be enabled by
setting spring.cloud.vault.mongodb.enabled=true (default false ) and providing the
role name with spring.cloud.vault.mongodb.role=… .
spring.cloud.vault:
mongodb:
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 23/29
2/4/2018 Spring Cloud Vault
enabled: true
role: readonly
backend: mongodb
username-property: spring.data.mongodb.username
password-property: spring.data.mongodb.password
enabled setting this value to true enables the MongodB backend config usage
role sets the role name of the MongoDB role definition
backend sets the path of the MongoDB mount to use
username-property sets the property name in which the MongoDB username is stored
password-property sets the property name in which the MongoDB password is stored
5.4 MySQL
The mysql backend has been deprecated in Vault 0.7.1 and it is recommended
to use the database backend and mount it as mysql . Configuration for
spring.cloud.vault.mysql will be removed in a future version.
Spring Cloud Vault can obtain credentials for MySQL. The integration can be enabled by setting
spring.cloud.vault.mysql.enabled=true (default false ) and providing the role name
with spring.cloud.vault.mysql.role=… .
spring.cloud.vault:
mysql:
enabled: true
role: readonly
backend: mysql
username-property: spring.datasource.username
password-property: spring.datasource.username
enabled setting this value to true enables the MySQL backend config usage
role sets the role name of the MySQL role definition
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 24/29
2/4/2018 Spring Cloud Vault
5.5 PostgreSQL
Spring Cloud Vault can obtain credentials for PostgreSQL. The integration can be enabled by
setting spring.cloud.vault.postgresql.enabled=true (default false ) and providing the
role name with spring.cloud.vault.postgresql.role=… .
spring.cloud.vault:
postgresql:
enabled: true
role: readonly
backend: postgresql
username-property: spring.datasource.username
password-property: spring.datasource.username
enabled setting this value to true enables the PostgreSQL backend config usage
role sets the role name of the PostgreSQL role definition
backend sets the path of the PostgreSQL mount to use
username-property sets the property name in which the PostgreSQL username is stored
password-property sets the property name in which the PostgreSQL password is stored
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 25/29
2/4/2018 Spring Cloud Vault
@Override
public void addSecretBackends(SecretBackendConfigurer configurer) {
configurer.add("secret/my-application");
configurer.registerDefaultGenericSecretBackends(false);
configurer.registerDefaultDiscoveredSecretBackends(true);
}
}
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 26/29
2/4/2018 Spring Cloud Vault
The discovery client implementations all support some kind of metadata map (e.g. for Eureka
we have eureka.instance.metadataMap). Some additional properties of the service may need to
be configured in its service registration metadata so that clients can connect correctly. Service
registries that do not provide details about transport layer security need to provide a scheme
metadata entry to be set either to https or http . If no scheme is configured and the service
is not exposed as secure service, then configuration defaults to spring.cloud.vault.scheme
which is https when it’s not set.
spring.cloud.vault.discovery:
enabled: true
service-id: my-vault-service
spring.cloud.vault:
fail-fast: true
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 27/29
2/4/2018 Spring Cloud Vault
spring.cloud.vault:
ssl:
trust-store: classpath:keystore.jks
trust-store-password: changeit
trust-store sets the resource for the trust-store. SSL-secured Vault communication will
validate the Vault SSL certificate with the specified trust-store.
trust-store-password sets the trust-store password
Please note that configuring spring.cloud.vault.ssl.* can be only applied when either
Apache Http Components or the OkHttp client is on your class-path.
Vault promises that the data will be valid for the given duration, or Time To Live (TTL). Once the
lease is expired, Vault can revoke the data, and the consumer of the secret can no longer be
certain that it is valid.
Spring Cloud Vault maintains a lease lifecycle beyond the creation of login tokens and secrets.
That said, login tokens and secrets associated with a lease are scheduled for renewal just
before the lease expires until terminal expiry. Application shutdown revokes obtained login
tokens and renewable leases.
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 28/29
2/4/2018 Spring Cloud Vault
Secret service and database backends (such as MongoDB or MySQL) usually generate a
renewable lease so generated credentials will be disabled on application shutdown.
Lease renewal and revocation is enabled by default and can be disabled by setting
spring.cloud.vault.config.lifecycle.enabled to false . This is not recommended as
leases can expire and Spring Cloud Vault cannot longer access Vault or services using
generated credentials and valid credentials remain active after application shutdown.
spring.cloud.vault:
config.lifecycle.enabled: true
http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html 29/29