Spring Cloud's Vault practice stepping on the pit record

1. Introduction to Vault

1.1 Overview

Vault is an open source project developed by HashiCorp to manage confidential information such as API keys, passwords, certificates, etc. It provides a centralized management method to protect sensitive data, and also provides access control and encryption services.

Using Vault enables sensitive information to be managed more securely and abstracted from application code, simplifying management and reducing the risk of exposure.

1.2 Relationship between Vault and Spring Cloud

Spring Cloud provides various tools and frameworks for building Spring Boot-based microservice applications, including configuration management, service registration and discovery, load balancing, circuit breakers, and more. Vault works with Spring Cloud for secure configuration management.

Two Vault practice

2.1 Install and configure Vault

2.1.1 Vault installation steps

To install Vault under the Linux system, you can follow the steps below:

  • Download and extract the Vault package
  $ wget https://releases.hashicorp.com/vault/1.8.1/vault_1.8.1_linux_amd64.zip
  $ unzip vault_1.8.1_linux_amd64.zip
  • Move the binaries to the /usr/local/bin directory

    $ sudo mv vault /usr/local/bin/
    
  • Check if the installation is successful

    $ vault --version
    

2.1.2 How to configure the Vault configuration file

The Vault configuration file is a text file in HCL (HashiCorp Configuration Language) format, generally named config.hcl.

Here is an example of a simple configuration file:

listener "tcp" {
    
    
  address = "127.0.0.1:8200"
  tls_disable = 1
}

storage "file" {
    
    
  path = "/var/lib/vault/data"
}

The configuration file specifies the listening address and storage method. Among them, listener defines the listening address and protocol of Vault, where TCP protocol and port number are specified as 8200; storage defines the storage method of Vault, and file system storage is specified here.

2.2 Using Vault to store sensitive information

2.2.1 How to use Vault to store sensitive information

First you need to start the Vault service and authenticate. The key can then be stored in Vault using the following command:

$ vault kv put secret/myapp/database username=dbuser password=dbpass

The above command stores the database username and password in Vault at secret/myapp/database.

To retrieve passwords stored in Vault, use the following command:

$ vault kv get secret/myapp/database

The above command will display the database username and password stored in Vault.

2.2.2 Encryption mechanism of Vault

Vault provides several encryption mechanisms to protect sensitive data. The most commonly used method is encryption based on the Transit engine.

Using the Transit engine, data can be encrypted and decrypted, and an encryption policy defined once and reused across multiple applications. This can effectively ensure data security and reduce the developer's workload in implementing encryption logic in the code.

2.3 Integrating Vault with Spring Cloud

2.3.1 How to use Vault in Spring Cloud

Spring Cloud provides two libraries, spring-cloud-vault and spring-cloud-starter-vault, for integrating Vault. In a Spring Boot application, integration with Vault can be enabled by adding these dependencies.

For example, in a Maven project the following dependencies can be added to the pom.xml file:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-vault-config</artifactId>
  <version>3.0.4</version>
</dependency>

2.3.2 Configure the connection between Spring Cloud and Vault

Configuring the connection between Spring Cloud and Vault requires setting Vault's address, authentication method, and access authorization. Here is a simple application.yml example:

spring:
  cloud:
    vault:
      uri: http://127.0.0.1:8200/
      authentication: TOKEN
      token: {
    
    VAULT_TOKEN}
      ssl:
        key-store:
          location: file:/path/to/vault.keystore
          password: {
    
    KEYSTORE_PASSWORD}
          key-alias: {
    
    KEY_ALIAS}

Among them, uri specifies the address of the Vault service; authentication specifies the Vault authentication method, TOKEN is used here; token is the certificate for accessing the Vault; ssl defines the settings of the Secure Sockets Layer (SSL), including the location and password of the trust store, and key aliases etc.

3. Vault practice stepping on the pit record

3.1 Common problems and solutions encountered when using Vault

  1. Unable to start Vault service

    • Problem description: The Vault service cannot be started normally, and the error message is "failed to start:";

    • Possible Causes:

      1. Port conflict, you can modify the Vault service port by modifying the configuration file "config.hcl";

      2. Insufficient data directory permissions, you can modify the permissions of the data directory or change the user running the Vault service;

      3. HashiCorp Vault is not installed on the system;

    • solution:

      1. Check whether HashiCorp Vault is installed in the system, and make sure it has been added to the PATH environment variable;

      2. Confirm that the port is not occupied, and modify the Vault service port through the configuration file;

      3. Modify data directory permissions or change the user the Vault service runs as.

  2. Forward proxy usage exception

    • Problem description: When using a forward proxy to access the Vault service, a 503 error is returned;

    • Possible Causes:

      1. The forwarding address of the proxy is wrong, resulting in failure to access normally;

      2. The Vault service is not properly configured for SSL;

      3. The "listener" section of the configuration file is incorrectly set.

    • solution:

      1. Check the proxy forwarding address and make sure it is correct and valid;

      2. Confirm that the Vault service is properly configured for SSL;

      3. Check that the "listener" section of the configuration file is set correctly.

3.2 Possible problems and solutions in the integration of Vault and Spring Cloud

  1. No automatic lease renewal

    • Problem description: After using Vault to integrate with Spring Cloud, automatic lease renewal cannot be performed;

    • Possible Causes:

      1. Vault Token has insufficient permissions;

      2. The secret reference passed to Spring Cloud is invalid;

      3. Vault is not properly configured.

    • solution:

      1. Confirm that the Vault Token used has the right to renew the lease;

      2. Confirm that the secret reference passed to Spring Cloud is valid and can be parsed correctly;

      3. Confirm that Vault is properly configured and assigned the correct secret reference.

  2. Vault key error

    • Problem description: After using Vault to integrate with Spring Cloud, the Vault key cannot be obtained correctly;

    • Possible Causes:

      1. Not properly connecting to the Vault server when configuring Vault;

      2. Vault Token has insufficient permissions;

      3. The credentials path is set incorrectly.

    • solution:

      1. Confirm that the Vault connection is configured correctly and can connect to the Vault server normally;

      2. Make sure that the Vault Token used has sufficient authority to obtain the key;

      3. Check that the credentials path is set correctly.

4. Summary and review

This article mainly introduces some common problems that may be encountered when using Vault, and gives corresponding solutions. Corresponding introductions are also made for the problems that arise in the integration of Vault and Spring Cloud. In general, you need to pay attention to the correct configuration and related permission settings when using Vault.

Guess you like

Origin blog.csdn.net/u010349629/article/details/130857414