-
Notifications
You must be signed in to change notification settings - Fork 63
397 - HashiCorp Vault support for database secrets #398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4d7e96c
0c92afb
0833339
fda8398
13205c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // Copyright (c) 2025, Oracle and/or its affiliates. | ||
| // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. | ||
|
|
||
| package hashivault | ||
|
|
||
| import ( | ||
| "context" | ||
| "strings" | ||
| "errors" | ||
| "net" | ||
| "net/http" | ||
| "time" | ||
| "github.com/oracle/oci-go-sdk/v65/example/helpers" | ||
|
|
||
| "log/slog" | ||
| vault "github.com/hashicorp/vault/api" | ||
| ) | ||
|
|
||
| var UnsupportedMountType = errors.New("Unsupported HashiCorp Vault mount type") | ||
| var RequiredKeyMissing = errors.New("Required key missing from HashiCorp Vault secret") | ||
|
|
||
| type HashicorpVaultClient struct { | ||
| client *vault.Client | ||
| logger *slog.Logger | ||
| } | ||
|
|
||
| // newUnixSocketVaultClient creates a custom HTTP client using a Unix socket | ||
| func newUnixSocketVaultClient(socketPath string) (*vault.Client, error) { | ||
| httpClient := &http.Client{ | ||
| Transport: &http.Transport{ | ||
| DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { | ||
| return net.Dial("unix", socketPath) | ||
| }, | ||
| }, | ||
| Timeout: 10 * time.Second, | ||
| } | ||
|
|
||
| // Configure the Vault client | ||
| config := &vault.Config{ | ||
| Address: "http://unix", | ||
| HttpClient: httpClient, | ||
| Timeout: 10 * time.Second, | ||
| MinRetryWait: time.Millisecond * 1000, | ||
| MaxRetryWait: time.Millisecond * 1500, | ||
| MaxRetries: 2, | ||
| } | ||
|
|
||
| return vault.NewClient(config) | ||
| } | ||
|
|
||
| // createVaultClient connects to a vault client, using connection method specified with the parameters. Returns error if fails. | ||
| func createVaultClient(logger *slog.Logger, socketPath string) (HashicorpVaultClient,error) { | ||
| var vaultClient HashicorpVaultClient | ||
| var err error | ||
|
|
||
| if socketPath != "" { | ||
| // Create Vault client that uses Unix Socket | ||
| vaultClient.client, err = newUnixSocketVaultClient(socketPath) | ||
| } | ||
| if err != nil { | ||
| logger.Error("Failed to connect to HashiCorp Vault", "err", err) | ||
| } | ||
| vaultClient.logger = logger | ||
| return vaultClient,err | ||
| } | ||
|
|
||
| // CreateVaultClient connects to a vault client, using connection method specified with the parameters. Fatal if fails. | ||
| func CreateVaultClient(logger *slog.Logger, socketPath string) HashicorpVaultClient { | ||
| c,err := createVaultClient(logger, socketPath) | ||
| helpers.FatalIfError(err) | ||
| return c | ||
| } | ||
|
|
||
| // getVaultSecret fetches secret from vault using specified mount type. Returns error on failure. | ||
| func (c HashicorpVaultClient) getVaultSecret(mountType string, mount string, path string, requiredKeys []string) (map[string]string,error) { | ||
| result := map[string]string{} | ||
| var err error | ||
| if mountType == "kvv2" || mountType == "kvv1" { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a vault const for these? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, these are just strings I made up. Vault has multiple plugins to handle secrets, "kvv1" and "kvv2" are the simplest key-value secret stores. I'll also soon work on adding the "database" secret store type. They all need slightly different SDK calls. |
||
| // Handle simple key-value secrets | ||
| var secret *vault.KVSecret | ||
| c.logger.Info("Making call to HashiCorp Vault", "mountType", mountType, "mountName", mount, "secretPath", path, "expectedKeys", requiredKeys) | ||
| if mountType == "kvv2" { | ||
| secret, err = c.client.KVv2(mount).Get(context.TODO(), path) | ||
| } else { | ||
| secret, err = c.client.KVv1(mount).Get(context.TODO(), path) | ||
| } | ||
| if err != nil { | ||
| c.logger.Error("Failed to fetch secret from HashiCorp Vault", "err", err) | ||
| return result, err | ||
| } | ||
| // Expect simple one-level JSON, remap interface{} straight to string | ||
| for key,val := range secret.Data { | ||
| result[key] = strings.TrimRight(val.(string), "\r\n") // make sure a \r and/or \n didn't make it into the secret | ||
| } | ||
| } else { | ||
| c.logger.Error(UnsupportedMountType.Error()) | ||
| return result, UnsupportedMountType | ||
| } | ||
| // Check that we have all required keys present | ||
| for _, key := range requiredKeys { | ||
| val, keyExists := result[key] | ||
| if !keyExists || val == "" { | ||
| c.logger.Error(RequiredKeyMissing.Error(), "key", key) | ||
| return result, RequiredKeyMissing | ||
| } | ||
| } | ||
| return result, nil | ||
| } | ||
|
|
||
| // GetVaultSecret fetches secret from vault using specified mount type. Fatal on failure. | ||
| func (c HashicorpVaultClient) GetVaultSecret(mountType string, mount string, path string, requiredKeys []string) map[string]string { | ||
| // Public callable function that does not return an error, just exits instead. Like other vault code in this project. | ||
| res,err := c.getVaultSecret(mountType, mount, path, requiredKeys) | ||
| helpers.FatalIfError(err) | ||
| return res | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| --- | ||
| title: HashiCorp Vault | ||
| sidebar_position: 8 | ||
| --- | ||
|
|
||
| # HashiCorp Vault | ||
|
|
||
| Securely load database credentials from HashiCorp Vault. | ||
|
|
||
| Each database in the config file may be configured to use HashiCorp Vault. To load the database username and/or password from HashiCorp Vault, set the `vault.hashicorp` property to contain the following information: | ||
|
|
||
| ```yaml | ||
| databases: | ||
| mydb: | ||
| vault: | ||
| hashicorp: | ||
| proxySocket: /var/run/vault/vault.sock | ||
| mountType: secret engine type, currently either "kvv1" or "kvv2" | ||
| mountName: secret engine mount path | ||
| secretPath: path of the secret | ||
| usernameAttribute: name of the JSON attribute, where to read the database username, if ommitted defaults to "username" | ||
| passwordAttribute: name of the JSON attribute, where to read the database password, if ommitted defaults to "password" | ||
| ``` | ||
|
|
||
| Example | ||
|
|
||
| ```yaml | ||
| databases: | ||
| mydb: | ||
| vault: | ||
| hashicorp: | ||
| proxySocket: /var/run/vault/vault.sock | ||
| mountType: kvv2 | ||
| mountName: dev | ||
| secretPath: oracle/mydb/monitoring | ||
| ``` | ||
|
|
||
| ### Authentication | ||
|
|
||
| In this first version it currently only supports queries via HashiCorp Vault Proxy configured to run on the local host and listening on a Unix socket. Currently also required use_auto_auth_token option to be set. | ||
| Will expand the support for other methods in the future. | ||
|
|
||
| Example Vault Proxy configuration snippet: | ||
|
|
||
| ``` | ||
| listener "unix" { | ||
| address = "/var/run/vault/vault.sock" | ||
| socket_mode = "0660" | ||
| socket_user = "vault" | ||
| socket_group = "vaultaccess" | ||
| tls_disable = true | ||
| } | ||
|
|
||
| api_proxy { | ||
| # This always uses the auto_auth token when communicating with Vault server, even if client does not send a token | ||
| use_auto_auth_token = true | ||
| } | ||
| ``` |
Uh oh!
There was an error while loading. Please reload this page.