Home > Backend Development > Golang > Vault integration guide in Golang projects

Vault integration guide in Golang projects

WBOY
Release: 2023-07-17 14:16:36
Original
1563 people have browsed it

Vault Integration Guide in Golang Projects

Overview:
Vault is a tool for managing and protecting sensitive data. It can securely store passwords, API keys, access tokens, etc. Sensitive information. In Golang projects, we can protect our sensitive information by integrating Vault to prevent leakage or access by unauthorized people. This article will introduce how to integrate Vault in a Golang project and provide code examples.

Step 1: Install and configure Vault

First, we need to install Vault and make basic settings.

1.1 Install Vault
You can download the Vault binary suitable for your operating system from Vault’s official website (https://www.vaultproject.io/) and add it to your system path middle.

1.2 Start the Vault server
Run the following command in the terminal to start the Vault server:

vault server -dev

This will start a development mode Vault server locally .

1.3 Configure Vault
Set the address of Vault by running the following command:

export VAULT_ADDR='http://127.0.0.1:8200'

Step 2: Authentication using Vault API

Next, we will use Vault's API to authenticate and obtain an access token.

2.1 Import the Vault library
In your Golang project, import the vault library:

import (
"github.com/hashicorp/vault/api"
)

2.2 Create a Vault client
Use the following code snippet to create a Vault client:

func CreateVaultClient() (*api.Client, error) {
client, err := api.NewClient(&api.Config{

Address: os.Getenv("VAULT_ADDR"),
Copy after login

})

if err != nil {

return nil, err
Copy after login

}

return client, nil
}

2.3 Log in and get the access token
Use the following code to log in and get the access token:

func GetVaultToken(client *api.Client) (string, error) {
options := map[string]interface{}{

"role_id":   os.Getenv("VAULT_ROLE_ID"),
"secret_id": os.Getenv("VAULT_SECRET_ID"),
Copy after login

}

response, err := client.Logical().Write("auth/approle/login", options)
if err != nil {

return "", err
Copy after login
Copy after login

}

token, ok := response.Auth.ClientToken
if !ok {

return "", errors.New("failed to retrieve token from Vault")
Copy after login

}

return token, nil
}

2.4 Using access token
Use the access token obtained in the previous step to access sensitive information in Vault:

func GetSecretFromVault(client *api.Client, secretPath string) (string, error) {
secret, err := client.Logical().Read(secretPath)
if err != nil {

return "", err
Copy after login
Copy after login

}

if secret == nil {

return "", errors.New("secret not found")
Copy after login

}

data, ok := secret.Data["data"].(map[string]interface{ })
if !ok {

return "", errors.New("invalid secret format")
Copy after login

}

key, ok := data["key"].(string)
if !ok {

return "", errors.New("key not found in secret")
Copy after login

}

return key, nil
}

Step 3: Use Vault in code

Through the above steps, we already have a tool that can be used with Vault Golang client to authenticate and access sensitive information. Now we can use it in our code.

func main() {
// Create Vault client
client, err := CreateVaultClient()
if err != nil {

log.Fatalf("failed to create Vault client: %v", err)
Copy after login

}

// Get access token
token, err := GetVaultToken(client)
if err != nil {

log.Fatalf("failed to get Vault token: %v", err)
Copy after login

}

// Use access token Get sensitive information from Vault
secretKey, err := GetSecretFromVault(client, "secret/my-secret-key")
if err != nil {

log.Fatalf("failed to get secret key from Vault: %v", err)
Copy after login

}

// Use the acquired sensitive information for business logic processing
// ...
}

In the above code, we first create a Vault client, and then use the Vault API Log in and get an access token. Next, we use the access token to obtain sensitive information from the Vault and use it for our business logic processing.

Conclusion:
By integrating Vault, protecting and managing sensitive information in Golang projects becomes simpler and more secure. By using Vault’s API, we can easily authenticate and access sensitive information stored in Vault. We hope that the integration guide and sample code provided in this article will guide you in using Vault in your Golang project.

The above is the detailed content of Vault integration guide in Golang projects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template