Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type SecretProperties struct {
PasswordField string
SecretName string
UserName string
KVVersion int
}

type Vault struct {
Expand Down Expand Up @@ -115,7 +116,9 @@ func (v *Vault) login(ctx context.Context) (*vault.Secret, error) {
return authInfo, nil
}

// GetKVSecret fetches the latest version of secret api key from kv-v1 or kv-v2
// GetKVSecret retrieves a secret from Vault using KV v1 or KV v2.
// MountPath is the actual Vault mount (e.g., "testing-path"),
// while KVVersion controls which API (v1/v2) is used.
func (v *Vault) GetKVSecret(ctx context.Context, props *SecretProperties, secret string) (*vault.KVSecret, error) {
var kvSecret *vault.KVSecret
var err error
Expand All @@ -134,12 +137,11 @@ func (v *Vault) GetKVSecret(ctx context.Context, props *SecretProperties, secret
secretPath = secret
}
}

// perform more checks based on profile
if props.MountPath != "kv2" {
kvSecret, err = v.client.KVv1(props.MountPath).Get(ctx, secretPath)
} else {
switch props.KVVersion {
case 2:
kvSecret, err = v.client.KVv2(props.MountPath).Get(ctx, secretPath)
default:
kvSecret, err = v.client.KVv1(props.MountPath).Get(ctx, secretPath)
}

if err != nil {
Expand Down
42 changes: 42 additions & 0 deletions vault/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ func createVaultTestCluster(t *testing.T) (*docker.DockerCluster, string, string
t.Fatal(err)
}

// create KV V2 mount with a custom mount name
if err := client.Sys().Mount("custom-kv2", &vaultapi.MountInput{
Type: "kv",
Options: map[string]string{
"version": "2",
},
}); err != nil {
t.Fatal(err)
}

// enable approle
if err := client.Sys().EnableAuthWithOptions("approle", &vaultapi.EnableAuthOptions{
Type: "approle",
Expand Down Expand Up @@ -124,6 +134,15 @@ func createVaultTestCluster(t *testing.T) (*docker.DockerCluster, string, string
t.Fatal(err)
}

// Create KV2 secret under custom mount
if _, err := client.Logical().Write("custom-kv2/data/ipmi/testkv2secret", map[string]interface{}{
"data": map[string]interface{}{
"value": "testkv2value",
},
}); err != nil {
t.Fatal(err)
}

// Create KV1 secret
if _, err := client.Logical().Write("secret/testkv1secret", map[string]interface{}{
"data": map[string]interface{}{
Expand Down Expand Up @@ -274,6 +293,7 @@ func Test_Vault_Auth(t *testing.T) {
loginFunc: login,
secretProps: &SecretProperties{
MountPath: "kv2",
KVVersion: 2,
SecretName: "missing",
},
getSecretFunc: getSecret,
Expand All @@ -289,6 +309,7 @@ func Test_Vault_Auth(t *testing.T) {
secretProps: &SecretProperties{
MountPath: "kv2",
Path: "morepath",
KVVersion: 2,
SecretName: "testkv2secret",
},
getSecretFunc: getSecret,
Expand All @@ -303,6 +324,7 @@ func Test_Vault_Auth(t *testing.T) {
loginFunc: login,
secretProps: &SecretProperties{
MountPath: "kv2",
KVVersion: 2,
Path: "morepath",
},
getSecretFunc: getSecret,
Expand All @@ -317,6 +339,7 @@ func Test_Vault_Auth(t *testing.T) {
loginFunc: login,
secretProps: &SecretProperties{
MountPath: "kv2",
KVVersion: 2,
SecretName: "testkv2secret",
},
getSecretFunc: getSecret,
Expand All @@ -331,6 +354,7 @@ func Test_Vault_Auth(t *testing.T) {
loginFunc: login,
secretProps: &SecretProperties{
MountPath: "kv2",
KVVersion: 2,
},
getSecretFunc: getSecret,
cleanUpFunc: cleanUp,
Expand All @@ -350,13 +374,30 @@ func Test_Vault_Auth(t *testing.T) {
cleanUpFunc: cleanUp,
expectErr: false,
},
{
name: "Get KVv2 Secret Custom Mount",
ctx: ctx,
vaultParams: goodParams,
appRoleClientFunc: createAppRoleClient,
loginFunc: login,
secretProps: &SecretProperties{
MountPath: "custom-kv2",
Path: "ipmi",
KVVersion: 2,
SecretName: "testkv2secret",
},
getSecretFunc: getSecret,
cleanUpFunc: cleanUp,
expectErr: false,
},
{
name: "Token Renewal",
ctx: ctx,
vaultParams: goodParams,
appRoleClientFunc: createAppRoleClient,
secretProps: &SecretProperties{
MountPath: "kv2",
KVVersion: 2,
},
validateFunc: func(t *testing.T, tc testcase) error {
var wg = sync.WaitGroup{}
Expand Down Expand Up @@ -388,6 +429,7 @@ func Test_Vault_Auth(t *testing.T) {
appRoleClientFunc: createAppRoleClient,
secretProps: &SecretProperties{
MountPath: "kv2",
KVVersion: 2,
},
validateFunc: func(t *testing.T, tc testcase) error {
var wg = sync.WaitGroup{}
Expand Down
Loading