5. 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-doc.cn

5.1. Token authentication

Tokens are the core method for authentication within Vault. Token authentication requires a static token to be provided using the configuration. As a fallback, the token may also be retrieved from ~/.vault-token which is the default location used by the Vault CLI to cache tokens.spring-doc.cn

Token authentication is the default authentication method. If a token is disclosed an unintended party gains access to Vault and can access secrets for the intended client.
Example 8. application.yml
spring.cloud.vault:
    authentication: TOKEN
    token: 00000000-0000-0000-0000-000000000000
  • authentication setting this value to TOKEN selects the Token authentication methodspring-doc.cn

  • token sets the static token to use. If missing or empty, then an attempt will be made to retrieve a token from ~/.vault-token.spring-doc.cn

See also:spring-doc.cn

5.2. Vault Agent authentication

Vault ships a sidecar utility with Vault Agent since version 0.11.0. Vault Agent implements the functionality of Spring Vault’s SessionManager with its Auto-Auth feature. Applications can reuse cached session credentials by relying on Vault Agent running on localhost. Spring Vault can send requests without the X-Vault-Token header. Disable Spring Vault’s authentication infrastructure to disable client authentication and session management.spring-doc.cn

Example 9. application.yml
spring.cloud.vault:
    authentication: NONE
  • authentication setting this value to NONE disables ClientAuthentication and SessionManager.spring-doc.cn

5.3. AppId authentication

Vault supports AppId authentication that consists of two hard to guess tokens. The AppId defaults to spring.application.name that is statically configured. The second token is the UserId which is a part determined by the application, usually related to the runtime environment. IP address, Mac address or a Docker container name are good examples. Spring Cloud Vault Config supports IP address, Mac address and static UserId’s (e.g. supplied via System properties). The IP and Mac address are represented as Hex-encoded SHA256 hash.spring-doc.cn

IP address-based UserId’s use the local host’s IP address.spring-doc.cn

Example 10. application.yml using SHA256 IP-Address UserId’s
spring.cloud.vault:
    authentication: APPID
    app-id:
        user-id: IP_ADDRESS
  • authentication setting this value to APPID selects the AppId authentication methodspring-doc.cn

  • app-id-path sets the path of the AppId mount to usespring-doc.cn

  • user-id sets the UserId method. Possible values are IP_ADDRESS, MAC_ADDRESS or a class name implementing a custom AppIdUserIdMechanismspring-doc.cn

The corresponding command to generate the IP address UserId from a command line is:spring-doc.cn

$ echo -n 192.168.99.1 | sha256sum
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-doc.cn

Example 11. application.yml using SHA256 Mac-Address UserId’s
spring.cloud.vault:
    authentication: APPID
    app-id:
        user-id: MAC_ADDRESS
        network-interface: eth0
  • network-interface sets network interface to obtain the physical addressspring-doc.cn

The corresponding command to generate the IP address UserId from a command line is:spring-doc.cn

$ echo -n 0AFEDE1234AC | sha256sum
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.

5.3.1. Custom UserId

The UserId generation is an open mechanism. You can set spring.cloud.vault.app-id.user-id to any string and the configured value will be used as static UserId.spring-doc.cn

A more advanced approach lets you set spring.cloud.vault.app-id.user-id to a classname. This class must be on your classpath and must implement the org.springframework.cloud.vault.AppIdUserIdMechanism interface and the createUserId method. Spring Cloud Vault will obtain the UserId by calling createUserId each time it authenticates using AppId to obtain a token.spring-doc.cn

Example 12. application.yml
spring.cloud.vault:
    authentication: APPID
    app-id:
        user-id: com.examlple.MyUserIdMechanism
Example 13. MyUserIdMechanism.java
public class MyUserIdMechanism implements AppIdUserIdMechanism {

  @Override
  public String createUserId() {
    String userId = ...
    return userId;
  }
}

5.4. AppRole authentication

AppRole is intended for machine authentication, like the deprecated (since Vault 0.6.1) AppId authentication. AppRole authentication consists of two hard to guess (secret) tokens: RoleId and SecretId.spring-doc.cn

Spring Vault supports various AppRole scenarios (push/pull mode and wrapped).spring-doc.cn

RoleId and optionally SecretId must be provided by configuration, Spring Vault will not look up these or create a custom SecretId.spring-doc.cn

Example 14. application.yml with AppRole authentication properties
spring.cloud.vault:
    authentication: APPROLE
    app-role:
        role-id: bde2076b-cccb-3cf0-d57e-bca7b1e83a52

The following scenarios are supported along the required configuration details:spring-doc.cn

Table 1. Configuration

Methodspring-doc.cn

RoleIdspring-doc.cn

SecretIdspring-doc.cn

RoleNamespring-doc.cn

Tokenspring-doc.cn

Provided RoleId/SecretIdspring-doc.cn

Providedspring-doc.cn

Providedspring-doc.cn

Provided RoleId without SecretIdspring-doc.cn

Providedspring-doc.cn

Provided RoleId, Pull SecretIdspring-doc.cn

Providedspring-doc.cn

Providedspring-doc.cn

Providedspring-doc.cn

Providedspring-doc.cn

Pull RoleId, provided SecretIdspring-doc.cn

Providedspring-doc.cn

Providedspring-doc.cn

Providedspring-doc.cn

Full Pull Modespring-doc.cn

Providedspring-doc.cn

Providedspring-doc.cn

Wrappedspring-doc.cn

Providedspring-doc.cn

Wrapped RoleId, provided SecretIdspring-doc.cn

Providedspring-doc.cn

Providedspring-doc.cn

Provided RoleId, wrapped SecretIdspring-doc.cn

Providedspring-doc.cn

Providedspring-doc.cn

Table 2. Pull/Push/Wrapped Matrix

RoleIdspring-doc.cn

SecretIdspring-doc.cn

Supportedspring-doc.cn

Providedspring-doc.cn

Providedspring-doc.cn

spring-doc.cn

Providedspring-doc.cn

Pullspring-doc.cn

spring-doc.cn

Providedspring-doc.cn

Wrappedspring-doc.cn

spring-doc.cn

Providedspring-doc.cn

Absentspring-doc.cn

spring-doc.cn

Pullspring-doc.cn

Providedspring-doc.cn

spring-doc.cn

Pullspring-doc.cn

Pullspring-doc.cn

spring-doc.cn

Pullspring-doc.cn

Wrappedspring-doc.cn

spring-doc.cn

Pullspring-doc.cn

Absentspring-doc.cn

spring-doc.cn

Wrappedspring-doc.cn

Providedspring-doc.cn

spring-doc.cn

Wrappedspring-doc.cn

Pullspring-doc.cn

spring-doc.cn

Wrappedspring-doc.cn

Wrappedspring-doc.cn

spring-doc.cn

Wrappedspring-doc.cn

Absentspring-doc.cn

spring-doc.cn

You can use still all combinations of push/pull/wrapped modes by providing a configured AppRoleAuthentication bean within the context. Spring Cloud Vault cannot derive all possible AppRole combinations from the configuration properties.
AppRole authentication is limited to simple pull mode using reactive infrastructure. Full pull mode is not yet supported. Using Spring Cloud Vault with the Spring WebFlux stack enables Vault’s reactive auto-configuration which can be disabled by setting spring.cloud.vault.reactive.enabled=false.
Example 15. application.yml with all AppRole authentication properties
spring.cloud.vault:
    authentication: APPROLE
    app-role:
        role-id: bde2076b-cccb-3cf0-d57e-bca7b1e83a52
        secret-id: 1696536f-1976-73b1-b241-0b4213908d39
        role: my-role
        app-role-path: approle
  • role-id sets the RoleId.spring-doc.cn

  • secret-id sets the SecretId. SecretId can be omitted if AppRole is configured without requiring SecretId (See bind_secret_id).spring-doc.cn

  • role: sets the AppRole name for pull mode.spring-doc.cn

  • app-role-path sets the path of the approle authentication mount to use.spring-doc.cn

5.5. AWS-EC2 authentication

The aws-ec2 auth backend provides a secure introduction mechanism for AWS EC2 instances, allowing automated retrieval of a Vault token. Unlike most Vault authentication backends, this backend does not require first-deploying, or provisioning security-sensitive credentials (tokens, username/password, client certificates, etc.). Instead, it treats AWS as a Trusted Third Party and uses the cryptographically signed dynamic metadata information that uniquely represents each EC2 instance.spring-doc.cn

Example 16. application.yml using AWS-EC2 Authentication
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.spring-doc.cn

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.spring-doc.cn

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.spring-doc.cn

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-doc.cn

Example 17. application.yml with configured role
spring.cloud.vault:
    authentication: AWS_EC2
    aws-ec2:
        role: application-server
Example 18. application.yml with all AWS EC2 authentication properties
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 methodspring-doc.cn

  • role sets the name of the role against which the login is being attempted.spring-doc.cn

  • aws-ec2-path sets the path of the AWS EC2 mount to usespring-doc.cn

  • identity-document sets URL of the PKCS#7 AWS EC2 identity documentspring-doc.cn

  • nonce used for AWS-EC2 authentication. An empty nonce defaults to nonce generationspring-doc.cn

5.6. AWS-IAM authentication

The aws backend provides a secure authentication mechanism for AWS IAM roles, allowing the automatic authentication with vault based on the current IAM role of the running application. Unlike most Vault authentication backends, this backend does not require first-deploying, or provisioning security-sensitive credentials (tokens, username/password, client certificates, etc.). Instead, it treats AWS as a Trusted Third Party and uses the 4 pieces of information signed by the caller with their IAM credentials to verify that the caller is indeed using that IAM role.spring-doc.cn

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.spring-doc.cn

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.spring-doc.cn

Example 19. application.yml with required AWS-IAM Authentication properties
spring.cloud.vault:
    authentication: AWS_IAM
Example 20. application.yml with all AWS-IAM Authentication properties
spring.cloud.vault:
    authentication: AWS_IAM
    aws-iam:
        role: my-dev-role
        aws-path: aws
        server-name: some.server.name
        endpoint-uri: https://sts.eu-central-1.amazonaws.com
  • 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.spring-doc.cn

  • aws-path sets the path of the AWS mount to usespring-doc.cn

  • server-name sets the value to use for the X-Vault-AWS-IAM-Server-ID header preventing certain types of replay attacks.spring-doc.cn

  • endpoint-uri sets the value to use for the AWS STS API used for the iam_request_url parameter.spring-doc.cn

AWS-IAM requires the AWS Java SDK dependency (com.amazonaws:aws-java-sdk-core) as the authentication implementation uses AWS SDK types for credentials and request signing.spring-doc.cn

5.7. Azure MSI authentication

The azure auth backend provides a secure introduction mechanism for Azure VM instances, allowing automated retrieval of a Vault token. Unlike most Vault authentication backends, this backend does not require first-deploying, or provisioning security-sensitive credentials (tokens, username/password, client certificates, etc.). Instead, it treats Azure as a Trusted Third Party and uses the managed service identity and instance metadata information that can be bound to a VM instance.spring-doc.cn

Example 21. application.yml with required Azure Authentication properties
spring.cloud.vault:
    authentication: AZURE_MSI
    azure-msi:
        role: my-dev-role
Example 22. application.yml with all Azure Authentication properties
spring.cloud.vault:
    authentication: AZURE_MSI
    azure-msi:
        role: my-dev-role
        azure-path: azure
        metadata-service: http://169.254.169.254/metadata/instance…
        identity-token-service: http://169.254.169.254/metadata/identity…
  • role sets the name of the role against which the login is being attempted.spring-doc.cn

  • azure-path sets the path of the Azure mount to usespring-doc.cn

  • metadata-service sets the URI at which to access the instance metadata servicespring-doc.cn

  • identity-token-service sets the URI at which to access the identity token servicespring-doc.cn

Azure MSI authentication obtains environmental details about the virtual machine (subscription Id, resource group, VM name) from the instance metadata service. The Vault server has Resource Id defaults to vault.hashicorp.com. To change this, set spring.cloud.vault.azure-msi.identity-token-service accordingly.spring-doc.cn

See also:spring-doc.cn

5.8. TLS certificate authentication

The cert auth backend allows authentication using SSL/TLS client certificates that are either signed by a CA or self-signed.spring-doc.cn

To enable cert authentication you need to:spring-doc.cn

  1. Use SSL, see Vault Client SSL configurationspring-doc.cn

  2. Configure a Java Keystore that contains the client certificate and the private keyspring-doc.cn

  3. Set the spring.cloud.vault.authentication to CERTspring-doc.cn

Example 23. application.yml
spring.cloud.vault:
    authentication: CERT
    ssl:
        key-store: classpath:keystore.jks
        key-store-password: changeit
        key-store-type: JKS
        cert-auth-path: cert

5.9. Cubbyhole authentication

Cubbyhole authentication uses Vault primitives to provide a secured authentication workflow. Cubbyhole authentication uses tokens as primary login method. An ephemeral token is used to obtain a second, login VaultToken from Vault’s Cubbyhole secret backend. The login token is usually longer-lived and used to interact with Vault. The login token will be retrieved from a wrapped response stored at /cubbyhole/response.spring-doc.cn

Creating a wrapped tokenspring-doc.cn

Response Wrapping for token creation requires Vault 0.6.0 or higher.
Example 24. Creating and storing tokens
$ vault token-create -wrap-ttl="10m"
Key                            Value
---                            -----
wrapping_token:                397ccb93-ff6c-b17b-9389-380b01ca2645
wrapping_token_ttl:            0h10m0s
wrapping_token_creation_time:  2016-09-18 20:29:48.652957077 +0200 CEST
wrapped_accessor:              46b6aebb-187f-932a-26d7-4f3d86a68319
Example 25. application.yml
spring.cloud.vault:
    authentication: CUBBYHOLE
    token: 397ccb93-ff6c-b17b-9389-380b01ca2645

See also:spring-doc.cn

5.10. GCP-GCE authentication

The gcp auth backend allows Vault login by using existing GCP (Google Cloud Platform) IAM and GCE credentials.spring-doc.cn

GCP GCE (Google Compute Engine) authentication creates a signature in the form of a JSON Web Token (JWT) for a service account. A JWT for a Compute Engine instance is obtained from the GCE metadata service using Instance identification. This API creates a JSON Web Token that can be used to confirm the instance identity.spring-doc.cn

Unlike most Vault authentication backends, this backend does not require first-deploying, or provisioning security-sensitive credentials (tokens, username/password, client certificates, etc.). Instead, it treats GCP as a Trusted Third Party and uses the cryptographically signed dynamic metadata information that uniquely represents each GCP service account.spring-doc.cn

Example 26. application.yml with required GCP-GCE Authentication properties
spring.cloud.vault:
    authentication: GCP_GCE
    gcp-gce:
        role: my-dev-role
Example 27. application.yml with all GCP-GCE Authentication properties
spring.cloud.vault:
    authentication: GCP_GCE
    gcp-gce:
        gcp-path: gcp
        role: my-dev-role
        service-account: [email protected]
  • role sets the name of the role against which the login is being attempted.spring-doc.cn

  • gcp-path sets the path of the GCP mount to usespring-doc.cn

  • service-account allows overriding the service account Id to a specific value. Defaults to the default service account.spring-doc.cn

See also:spring-doc.cn

5.11. GCP-IAM authentication

The gcp auth backend allows Vault login by using existing GCP (Google Cloud Platform) IAM and GCE credentials.spring-doc.cn

GCP IAM authentication creates a signature in the form of a JSON Web Token (JWT) for a service account. A JWT for a service account is obtained by calling GCP IAM’s projects.serviceAccounts.signJwt API. The caller authenticates against GCP IAM and proves thereby its identity. This Vault backend treats GCP as a Trusted Third Party.spring-doc.cn

IAM credentials can be obtained from either the runtime environment , specifically the GOOGLE_APPLICATION_CREDENTIALS environment variable, the Google Compute metadata service, or supplied externally as e.g. JSON or base64 encoded. JSON is the preferred form as it carries the project id and service account identifier required for calling projects.serviceAccounts.signJwt.spring-doc.cn

Example 28. application.yml with required GCP-IAM Authentication properties
spring.cloud.vault:
    authentication: GCP_IAM
    gcp-iam:
        role: my-dev-role
Example 29. application.yml with all GCP-IAM Authentication properties
spring.cloud.vault:
    authentication: GCP_IAM
    gcp-iam:
        credentials:
            location: classpath:credentials.json
            encoded-key: e+KApn0=
        gcp-path: gcp
        jwt-validity: 15m
        project-id: my-project-id
        role: my-dev-role
        service-account-id: [email protected]
  • role sets the name of the role against which the login is being attempted.spring-doc.cn

  • credentials.location path to the credentials resource that contains Google credentials in JSON format.spring-doc.cn

  • credentials.encoded-key the base64 encoded contents of an OAuth2 account private key in the JSON format.spring-doc.cn

  • gcp-path sets the path of the GCP mount to usespring-doc.cn

  • jwt-validity configures the JWT token validity. Defaults to 15 minutes.spring-doc.cn

  • project-id allows overriding the project Id to a specific value. Defaults to the project Id from the obtained credential.spring-doc.cn

  • service-account allows overriding the service account Id to a specific value. Defaults to the service account from the obtained credential.spring-doc.cn

GCP IAM authentication requires the Google Cloud Java SDK dependency (com.google.apis:google-api-services-iam and com.google.auth:google-auth-library-oauth2-http) as the authentication implementation uses Google APIs for credentials and JWT signing.spring-doc.cn

Google credentials require an OAuth 2 token maintaining the token lifecycle. All API is synchronous therefore, GcpIamAuthentication does not support AuthenticationSteps which is required for reactive usage.

See also:spring-doc.cn

5.12. Kubernetes authentication

Kubernetes authentication mechanism (since Vault 0.8.3) allows to authenticate with Vault using a Kubernetes Service Account Token. The authentication is role based and the role is bound to a service account name and a namespace.spring-doc.cn

A file containing a JWT token for a pod’s service account is automatically mounted at /var/run/secrets/kubernetes.io/serviceaccount/token.spring-doc.cn

Example 30. application.yml with all Kubernetes authentication properties
spring.cloud.vault:
    authentication: KUBERNETES
    kubernetes:
        role: my-dev-role
        kubernetes-path: kubernetes
        service-account-token-file: /var/run/secrets/kubernetes.io/serviceaccount/token
  • role sets the Role.spring-doc.cn

  • kubernetes-path sets the path of the Kubernetes mount to use.spring-doc.cn

  • service-account-token-file sets the location of the file containing the Kubernetes Service Account Token. Defaults to /var/run/secrets/kubernetes.io/serviceaccount/token.spring-doc.cn

See also:spring-doc.cn

5.13. Pivotal CloudFoundry authentication

The pcf auth backend provides a secure introduction mechanism for applications running within Pivotal’s CloudFoundry instances allowing automated retrieval of a Vault token. Unlike most Vault authentication backends, this backend does not require first-deploying, or provisioning security-sensitive credentials (tokens, username/password, client certificates, etc.) as identity provisioning is handled by PCF itself. Instead, it treats PCF as a Trusted Third Party and uses the managed instance identity.spring-doc.cn

Example 31. application.yml with required PCF Authentication properties
spring.cloud.vault:
    authentication: PCF
    pcf:
        role: my-dev-role
Example 32. application.yml with all PCF Authentication properties
spring.cloud.vault:
    authentication: PCF
    pcf:
        role: my-dev-role
        pcf-path: path
        instance-certificate: /etc/cf-instance-credentials/instance.crt
        instance-key: /etc/cf-instance-credentials/instance.key
  • role sets the name of the role against which the login is being attempted.spring-doc.cn

  • pcf-path sets the path of the PCF mount to use.spring-doc.cn

  • instance-certificate sets the path to the PCF instance identity certificate. Defaults to ${CF_INSTANCE_CERT} env variable.spring-doc.cn

  • instance-key sets the path to the PCF instance identity key. Defaults to ${CF_INSTANCE_KEY} env variable.spring-doc.cn

PCF authentication requires BouncyCastle (bcpkix-jdk15on) to be on the classpath for RSA PSS signing.