2024-12-11 15:59:55 +01:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2025-01-05 22:41:15 +01:00
|
|
|
"encoding/json"
|
2024-12-21 21:51:24 +01:00
|
|
|
"fmt"
|
|
|
|
"golang.org/x/oauth2/clientcredentials"
|
2025-01-05 22:41:15 +01:00
|
|
|
"net/http"
|
2024-12-11 15:59:55 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault-client-go"
|
|
|
|
"github.com/hashicorp/vault-client-go/schema"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2024-12-21 21:51:24 +01:00
|
|
|
// GetJWTToken fetches the raw token using client credentials and returns the access token as a string.
|
|
|
|
func GetJWTToken(clientID string, clientSecret string, tokenURL string) string {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
clientCredsConfig := clientcredentials.Config{
|
|
|
|
ClientID: clientID,
|
|
|
|
ClientSecret: clientSecret,
|
|
|
|
TokenURL: tokenURL,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the token using the client credentials
|
|
|
|
token, err := clientCredsConfig.TokenSource(ctx).Token()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error getting token: %v\n", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
// Return the raw access token
|
|
|
|
return token.AccessToken
|
|
|
|
}
|
|
|
|
|
2025-01-05 22:41:15 +01:00
|
|
|
func VaultWithJWT(ctx context.Context, roles string, jwt string) *vault.Client {
|
2024-12-21 21:51:24 +01:00
|
|
|
|
|
|
|
// prepare a client with the given base address
|
|
|
|
client, err := vault.New(
|
|
|
|
vault.WithAddress("http://localhost:8200"),
|
|
|
|
vault.WithRequestTimeout(30*time.Second),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2025-01-05 22:41:15 +01:00
|
|
|
log.Panic(err)
|
2024-12-21 21:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
request := schema.JwtLoginRequest{
|
|
|
|
Jwt: jwt,
|
|
|
|
Role: roles,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Auth.JwtLogin(ctx, request)
|
|
|
|
if err != nil {
|
2025-01-05 22:41:15 +01:00
|
|
|
log.Panic(resp, err)
|
2024-12-21 21:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = client.SetToken(resp.Auth.ClientToken)
|
|
|
|
if err != nil {
|
2025-01-05 22:41:15 +01:00
|
|
|
log.Panic(err)
|
2024-12-21 21:51:24 +01:00
|
|
|
}
|
2025-01-05 22:41:15 +01:00
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func VaultWithUserPass(ctx context.Context, username, password string) *vault.Client {
|
|
|
|
|
|
|
|
// prepare a client with the given base address
|
|
|
|
client, err := vault.New(
|
|
|
|
vault.WithAddress("http://localhost:8200"),
|
|
|
|
vault.WithRequestTimeout(30*time.Second),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client.SetRequestCallbacks(func(req *http.Request) {
|
|
|
|
log.Println("REQUEST:", *req.URL)
|
|
|
|
})
|
|
|
|
|
|
|
|
request := schema.UserpassLoginRequest{
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
|
|
|
|
// authenticate with a root token (insecure)
|
|
|
|
|
|
|
|
resp, err := client.Auth.UserpassLogin(ctx, username, request)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Panic("Problem logging in with username and password")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.SetToken(resp.Auth.ClientToken)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return client
|
2024-12-21 21:51:24 +01:00
|
|
|
}
|
|
|
|
|
2025-01-05 22:41:15 +01:00
|
|
|
func VaultWithToken(token string) *vault.Client {
|
2024-12-11 15:59:55 +01:00
|
|
|
|
|
|
|
// prepare a client with the given base address
|
|
|
|
client, err := vault.New(
|
|
|
|
vault.WithAddress("http://localhost:8200"),
|
|
|
|
vault.WithRequestTimeout(30*time.Second),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2025-01-05 22:41:15 +01:00
|
|
|
log.Panic(err)
|
2024-12-11 15:59:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//client.SetRequestCallbacks(func(req *http.Request) {
|
|
|
|
// log.Println("REQUEST:", *req.URL)
|
|
|
|
//})
|
|
|
|
|
|
|
|
// authenticate with a root token (insecure)
|
2025-01-05 22:41:15 +01:00
|
|
|
if err := client.SetToken(token); err != nil {
|
|
|
|
log.Panic(err)
|
2024-12-11 15:59:55 +01:00
|
|
|
}
|
2025-01-05 22:41:15 +01:00
|
|
|
|
|
|
|
return client
|
2024-12-11 15:59:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// WritingAPIKeyToVault writes the API key to a Vault using the given secret data.
|
|
|
|
//
|
|
|
|
// The function converts the secret data into a map and writes it to the Vault.
|
|
|
|
// If any error occurs during this process, it returns the error.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - ctx: The context for the operation.
|
|
|
|
// - client: The Vault client used to perform the write operation.
|
|
|
|
// - secretData: The secret data to be written to the Vault.
|
|
|
|
//
|
|
|
|
// Returns an error if the operation fails.
|
|
|
|
func WritingAPIKeyToVault[T SecretDataTypes](ctx context.Context, client *vault.Client, secretData SecretData[T]) error {
|
|
|
|
secret, err := SecretToMap(secretData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
request := schema.KvV2WriteRequest{
|
|
|
|
Data: secret,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = client.Secrets.KvV2Write(ctx, secretData.UserSourceID, request, vault.WithMountPath(secretData.Secret.GetMountPath()))
|
|
|
|
if err != nil {
|
2025-01-05 22:41:15 +01:00
|
|
|
daaata, _ := json.Marshal(client)
|
|
|
|
log.Trace(string(daaata))
|
|
|
|
|
|
|
|
log.WithField("mount_path", secretData.Secret.GetMountPath()).Panic(err)
|
2024-12-11 15:59:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadingApiKeyFromVault reads the API key from a Vault and populates the given secret data.
|
|
|
|
//
|
|
|
|
// The function reads the data from the Vault, converts it back into the appropriate secret type,
|
|
|
|
// and assigns it to the provided secret data structure.
|
|
|
|
// If any error occurs during this process, it returns the error.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - ctx: The context for the operation.
|
|
|
|
// - client: The Vault client used to perform the read operation.
|
|
|
|
// - secretData: A pointer to the secret data structure to be populated with the read data.
|
|
|
|
//
|
|
|
|
// Returns an error if the operation fails.
|
|
|
|
func ReadingApiKeyFromVault[T SecretDataTypes](ctx context.Context, client *vault.Client, secretData *SecretData[T]) error {
|
|
|
|
data, err := client.Secrets.KvV2Read(ctx, secretData.UserSourceID, vault.WithMountPath(secretData.Secret.GetMountPath()))
|
|
|
|
if err != nil {
|
2025-01-05 22:41:15 +01:00
|
|
|
log.Panic(err)
|
2024-12-11 15:59:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := MapToSecret[T](data.Data.Data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
secretData.Secret = secret
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|