From 023a0d99ab221027dc1b99a2d47735926ce97ce8 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Thu, 13 Jun 2024 07:20:23 +0200 Subject: [PATCH] Fix: Kubernetes native auth --- k8-operator/api/v1alpha1/infisicalsecret_types.go | 11 +++++++++-- k8-operator/controllers/infisicalsecret_auth.go | 14 +++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/k8-operator/api/v1alpha1/infisicalsecret_types.go b/k8-operator/api/v1alpha1/infisicalsecret_types.go index b0cc49a8f..6ea7e4c7b 100644 --- a/k8-operator/api/v1alpha1/infisicalsecret_types.go +++ b/k8-operator/api/v1alpha1/infisicalsecret_types.go @@ -33,13 +33,20 @@ type UniversalAuthDetails struct { type KubernetesAuthDetails struct { // +kubebuilder:validation:Required IdentityID string `json:"identityId"` - // +kubebuilder:validation:Optional - ServiceAccountTokenPath string `json:"serviceAccountTokenPath"` + // +kubebuilder:validation:Required + ServiceAccountRef KubernetesServiceAccountRef `json:"serviceAccountRef"` // +kubebuilder:validation:Required SecretsScope MachineIdentityScopeInWorkspace `json:"secretsScope"` } +type KubernetesServiceAccountRef struct { + // +kubebuilder:validation:Required + Name string `json:"name"` + // +kubebuilder:validation:Required + Namespace string `json:"namespace"` +} + type AWSIamAuthDetails struct { // +kubebuilder:validation:Required IdentityID string `json:"identityId"` diff --git a/k8-operator/controllers/infisicalsecret_auth.go b/k8-operator/controllers/infisicalsecret_auth.go index a2930b330..44861df87 100644 --- a/k8-operator/controllers/infisicalsecret_auth.go +++ b/k8-operator/controllers/infisicalsecret_auth.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/Infisical/infisical/k8-operator/api/v1alpha1" + "github.com/Infisical/infisical/k8-operator/packages/util" infisicalSdk "github.com/infisical/go-sdk" ) @@ -31,6 +32,12 @@ var AuthStrategy = struct { GCP_IAM_MACHINE_IDENTITY: "GCP_IAM_MACHINE_IDENTITY", } +type AuthenticationDetails struct { + authStrategy AuthStrategyType + machineIdentityScope v1alpha1.MachineIdentityScopeInWorkspace // This will only be set if a machine identity auth method is used (e.g. UniversalAuth or KubernetesAuth, etc.) + isMachineIdentityAuth bool +} + var ErrAuthNotApplicable = errors.New("authentication not applicable") func (r *InfisicalSecretReconciler) handleUniversalAuth(ctx context.Context, infisicalSecret v1alpha1.InfisicalSecret, infisicalClient infisicalSdk.InfisicalClientInterface) (AuthenticationDetails, error) { @@ -65,7 +72,12 @@ func (r *InfisicalSecretReconciler) handleKubernetesAuth(ctx context.Context, in return AuthenticationDetails{}, ErrAuthNotApplicable } - _, err := infisicalClient.Auth().KubernetesAuthLogin(kubernetesAuthSpec.IdentityID, kubernetesAuthSpec.ServiceAccountTokenPath) + serviceAccountToken, err := util.GetServiceAccountToken(r.Client, kubernetesAuthSpec.ServiceAccountRef.Namespace, kubernetesAuthSpec.ServiceAccountRef.Name) + if err != nil { + return AuthenticationDetails{}, fmt.Errorf("unable to get service account token [err=%s]", err) + } + + _, err = infisicalClient.Auth().KubernetesRawServiceAccountTokenLogin(kubernetesAuthSpec.IdentityID, serviceAccountToken) if err != nil { return AuthenticationDetails{}, fmt.Errorf("unable to login with Kubernetes native auth [err=%s]", err) }