Skip to content

Commit

Permalink
feat: Add ability to add namespace as a vault role suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
mkilchhofer committed Apr 9, 2024
1 parent 45ebf91 commit 46ed703
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions api/v1beta1/vaultauth_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
type VaultAuthConfigKubernetes struct {
// Role to use for authenticating to Vault.
Role string `json:"role"`
// Add the consuming secret's namespace as a suffix to the role name (e.g. "<role>-kube-system").
// Defaults to false.
// +kubebuilder:default:=false
RoleNamespaceSuffix bool `json:"roleNamespaceSuffix,omitempty"`
// ServiceAccount to use when authenticating to Vault's
// authentication backend. This must reside in the consuming secret's (VDS/VSS/PKI) namespace.
ServiceAccount string `json:"serviceAccount"`
Expand Down
5 changes: 5 additions & 0 deletions chart/crds/secrets.hashicorp.com_vaultauths.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ spec:
role:
description: Role to use for authenticating to Vault.
type: string
roleNamespaceSuffix:
default: false
description: Add the consuming secret's namespace as a suffix
to the role name (e.g. "<role>-kube-system"). Defaults to false.
type: boolean
serviceAccount:
description: ServiceAccount to use when authenticating to Vault's
authentication backend. This must reside in the consuming secret's
Expand Down
3 changes: 3 additions & 0 deletions chart/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ VaultAuthMethod Spec
{{- if eq $cur.method "kubernetes" }}
kubernetes:
role: {{ $cur.kubernetes.role }}
{{- if ne (toString $cur.kubernetes.roleNamespaceSuffix) "<nil>" }}
roleNamespaceSuffix: {{ $cur.kubernetes.roleNamespaceSuffix }}
{{- end }}
serviceAccount: {{ $serviceAccount }}
{{- if $cur.kubernetes.tokenAudiences }}
audiences: {{ $cur.kubernetes.tokenAudiences | toJson }}
Expand Down
4 changes: 4 additions & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,10 @@ defaultAuthMethod:
# @type: string
role: ""

# Add the consuming secret's namespace as a suffix to the role name (e.g. "<role>-kube-system"). Defaults to false.
# @type: boolean
roleNamespaceSuffix: ~

# Kubernetes ServiceAccount associated with the default Vault Auth Role
# @type: string
serviceAccount: default
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/secrets.hashicorp.com_vaultauths.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ spec:
role:
description: Role to use for authenticating to Vault.
type: string
roleNamespaceSuffix:
default: false
description: Add the consuming secret's namespace as a suffix
to the role name (e.g. "<role>-kube-system"). Defaults to false.
type: boolean
serviceAccount:
description: ServiceAccount to use when authenticating to Vault's
authentication backend. This must reside in the consuming secret's
Expand Down
1 change: 1 addition & 0 deletions docs/api/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ _Appears in:_
| Field | Description |
| --- | --- |
| `role` _string_ | Role to use for authenticating to Vault. |
| `roleNamespaceSuffix` _boolean_ | Add the consuming secret's namespace as a suffix to the role name (e.g. "<role>-kube-system"). Defaults to false. |
| `serviceAccount` _string_ | ServiceAccount to use when authenticating to Vault's authentication backend. This must reside in the consuming secret's (VDS/VSS/PKI) namespace. |
| `audiences` _string array_ | TokenAudiences to include in the ServiceAccount token. |
| `tokenExpirationSeconds` _integer_ | TokenExpirationSeconds to set the ServiceAccount token. |
Expand Down
7 changes: 6 additions & 1 deletion internal/credentials/vault/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package vault

import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -82,9 +83,13 @@ func (l *KubernetesCredentialProvider) GetCreds(ctx context.Context, client ctrl
return nil, err
}

role := l.authObj.Spec.Kubernetes.Role
if l.authObj.Spec.Kubernetes.RoleNamespaceSuffix {
role = fmt.Sprintf("%s-%s", role, l.providerNamespace)
}
// credentials needed for Kubernetes auth
return map[string]interface{}{
"role": l.authObj.Spec.Kubernetes.Role,
"role": role,
"jwt": tr.Status.Token,
}, nil
}

0 comments on commit 46ed703

Please sign in to comment.