Skip to content

Commit f7c833a

Browse files
committed
extend the app role cred provider to use direct secredID
1 parent 3e6bef8 commit f7c833a

File tree

8 files changed

+44
-6
lines changed

8 files changed

+44
-6
lines changed

api/v1beta1/vaultauth_types.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ type VaultAuthConfigAppRole struct {
127127
// RoleID of the AppRole Role to use for authenticating to Vault.
128128
RoleID string `json:"roleId,omitempty"`
129129

130+
// SecretID of the AppRole Role to use for authenticating to Vault.
131+
// If both SecretID and SecretRef are specified, SecretID takes precedence.
132+
SecretID string `json:"secretID,omitempty"`
133+
130134
// SecretRef is the name of a Kubernetes secret in the consumer's (VDS/VSS/PKI) namespace which
131135
// provides the AppRole Role's SecretID. The secret must have a key named `id` which holds the
132136
// AppRole Role's secretID.
@@ -159,8 +163,8 @@ func (a *VaultAuthConfigAppRole) Validate() error {
159163
errs = errors.Join(fmt.Errorf("empty roleID"))
160164
}
161165

162-
if a.SecretRef == "" {
163-
errs = errors.Join(fmt.Errorf("empty secretRef"))
166+
if a.SecretRef == "" && a.SecretID == "" {
167+
errs = errors.Join(fmt.Errorf("empty secretRef and seecretID"))
164168
}
165169

166170
return errs

chart/crds/secrets.hashicorp.com_vaultauthglobals.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ spec:
7979
description: RoleID of the AppRole Role to use for authenticating
8080
to Vault.
8181
type: string
82+
secretID:
83+
description: SecretID of the AppRole Role to use for authenticating
84+
to Vault.
85+
type: string
8286
secretRef:
8387
description: |-
8488
SecretRef is the name of a Kubernetes secret in the consumer's (VDS/VSS/PKI) namespace which

chart/crds/secrets.hashicorp.com_vaultauths.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ spec:
6767
description: RoleID of the AppRole Role to use for authenticating
6868
to Vault.
6969
type: string
70+
secretID:
71+
description: SecretID of the AppRole Role to use for authenticating
72+
to Vault.
73+
type: string
7074
secretRef:
7175
description: |-
7276
SecretRef is the name of a Kubernetes secret in the consumer's (VDS/VSS/PKI) namespace which

config/crd/bases/secrets.hashicorp.com_vaultauthglobals.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ spec:
7979
description: RoleID of the AppRole Role to use for authenticating
8080
to Vault.
8181
type: string
82+
secretID:
83+
description: SecretID of the AppRole Role to use for authenticating
84+
to Vault.
85+
type: string
8286
secretRef:
8387
description: |-
8488
SecretRef is the name of a Kubernetes secret in the consumer's (VDS/VSS/PKI) namespace which

config/crd/bases/secrets.hashicorp.com_vaultauths.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ spec:
6767
description: RoleID of the AppRole Role to use for authenticating
6868
to Vault.
6969
type: string
70+
secretID:
71+
description: SecretID of the AppRole Role to use for authenticating
72+
to Vault.
73+
type: string
7074
secretRef:
7175
description: |-
7276
SecretRef is the name of a Kubernetes secret in the consumer's (VDS/VSS/PKI) namespace which

credentials/vault/approle.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99

1010
"k8s.io/apimachinery/pkg/types"
11+
"k8s.io/apimachinery/pkg/util/uuid"
1112
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1213
"sigs.k8s.io/controller-runtime/pkg/log"
1314

@@ -45,6 +46,12 @@ func (l *AppRoleCredentialProvider) Init(ctx context.Context, client ctrlclient.
4546
l.authObj = authObj
4647
l.providerNamespace = providerNamespace
4748

49+
// If SecretID is provided directly in the spec, use a new UUID for this provider instance
50+
if authObj.Spec.AppRole.SecretID != "" {
51+
l.uid = uuid.NewUUID()
52+
return nil
53+
}
54+
4855
// We use the UID of the secret which holds the AppRole Role's secret_id for the provider UID
4956
key := ctrlclient.ObjectKey{
5057
Namespace: l.providerNamespace,
@@ -61,6 +68,15 @@ func (l *AppRoleCredentialProvider) Init(ctx context.Context, client ctrlclient.
6168

6269
func (l *AppRoleCredentialProvider) GetCreds(ctx context.Context, client ctrlclient.Client) (map[string]interface{}, error) {
6370
logger := log.FromContext(ctx)
71+
72+
// If SecretID is provided directly in the spec, return the spec's role_id and secret_id
73+
if l.authObj.Spec.AppRole.SecretID != "" {
74+
return map[string]interface{}{
75+
"role_id": l.authObj.Spec.AppRole.RoleID,
76+
"secret_id": l.authObj.Spec.AppRole.SecretID,
77+
}, nil
78+
}
79+
6480
// Fetch the AppRole Role's SecretID from the Kubernetes Secret each time there is a call to
6581
// GetCreds in case the SecretID has changed since the last time the client token was
6682
// generated. In the case of AppRole this is assumed to be common.

docs/api/api-reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ _Appears in:_
703703
| Field | Description | Default | Validation |
704704
| --- | --- | --- | --- |
705705
| `roleId` _string_ | RoleID of the AppRole Role to use for authenticating to Vault. | | |
706+
| `secretID` _string_ | SecretID of the AppRole Role to use for authenticating to Vault. | | |
706707
| `secretRef` _string_ | SecretRef is the name of a Kubernetes secret in the consumer's (VDS/VSS/PKI) namespace which<br />provides the AppRole Role's SecretID. The secret must have a key named `id` which holds the<br />AppRole Role's secretID. | | |
707708

708709

@@ -829,6 +830,7 @@ _Appears in:_
829830
| Field | Description | Default | Validation |
830831
| --- | --- | --- | --- |
831832
| `roleId` _string_ | RoleID of the AppRole Role to use for authenticating to Vault. | | |
833+
| `secretID` _string_ | SecretID of the AppRole Role to use for authenticating to Vault. | | |
832834
| `secretRef` _string_ | SecretRef is the name of a Kubernetes secret in the consumer's (VDS/VSS/PKI) namespace which<br />provides the AppRole Role's SecretID. The secret must have a key named `id` which holds the<br />AppRole Role's secretID. | | |
833835
| `namespace` _string_ | Namespace to auth to in Vault | | |
834836
| `mount` _string_ | Mount to use when authenticating to auth method. | | |

vault/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ func MakeVaultClient(ctx context.Context, cfg *ClientConfig, client ctrlclient.C
5252
return nil, fmt.Errorf("ClientConfig was nil")
5353
}
5454

55-
if client == nil {
56-
return nil, fmt.Errorf("ctrl-runtime Client was nil")
57-
}
58-
5955
var b []byte
6056
if cfg.CACertSecretRef != "" {
57+
if client == nil {
58+
return nil, fmt.Errorf("ctrl-runtime Client was nil and CCACertSecretRef was provided")
59+
}
60+
6161
objKey := ctrlclient.ObjectKey{
6262
Namespace: cfg.K8sNamespace,
6363
Name: cfg.CACertSecretRef,

0 commit comments

Comments
 (0)