diff --git a/.github/workflows/on-pr.yaml b/.github/workflows/on-pr.yaml index cc2852e..bf5922f 100644 --- a/.github/workflows/on-pr.yaml +++ b/.github/workflows/on-pr.yaml @@ -33,6 +33,7 @@ jobs: examples: | [ { "example": "examples/secretstacks/minimal.yaml" }, + { "example": "examples/secretstacks/import-identity.yaml" }, { "example": "examples/secretstacks/standard.yaml" }, { "example": "examples/secretstacks/vault.yaml" }, { "example": "examples/secretstacks/vault-external.yaml" }, diff --git a/.github/workflows/on-push-main.yaml b/.github/workflows/on-push-main.yaml index 268402d..abe2722 100644 --- a/.github/workflows/on-push-main.yaml +++ b/.github/workflows/on-push-main.yaml @@ -28,6 +28,7 @@ jobs: examples: | [ { "example": "examples/secretstacks/minimal.yaml" }, + { "example": "examples/secretstacks/import-identity.yaml" }, { "example": "examples/secretstacks/standard.yaml" }, { "example": "examples/secretstacks/vault.yaml" }, { "example": "examples/secretstacks/vault-external.yaml" }, diff --git a/Makefile b/Makefile index 3e4ba70..2dae5ed 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,7 @@ generate-configuration: # Format: example_path::observed_resources_path (observed_resources_path is optional) EXAMPLES := \ examples/secretstacks/minimal.yaml:: \ + examples/secretstacks/import-identity.yaml:: \ examples/secretstacks/standard.yaml:: \ examples/secretstacks/vault.yaml:: \ examples/secretstacks/vault-external.yaml:: \ diff --git a/README.md b/README.md index 00e724e..8ccef4a 100644 --- a/README.md +++ b/README.md @@ -117,3 +117,9 @@ make test # run KCL unit tests hops config install --path xrs/stacks/aws/secret kubectl apply -f local/secretstack.yaml ``` + +## Import an existing pod identity + +Use `spec.aws.podIdentity` to set the existing PodIdentity composite name, IAM role name and EKS association ID. Start with identity management policies set to Observe. Supply the complete inline policies and managed policy ARNs that the role should have when you enable Update. An explicit empty list replaces that policy set. Omitting either list retains the stack's defaults. Pin the current controller chart with `spec.chartVersion` to test the chart upgrade separately. + +Match the cluster, namespace and service account to the existing association. Transfer the existing writer before enabling updates and verify the same role and association IDs after reconciliation. The stack keeps managing its PodIdentity and its deletion dependency. [Import example](examples/secretstacks/import-identity.yaml) diff --git a/apis/secretstacks/definition.yaml b/apis/secretstacks/definition.yaml index d198392..ddefbd9 100644 --- a/apis/secretstacks/definition.yaml +++ b/apis/secretstacks/definition.yaml @@ -97,6 +97,10 @@ spec: description: external-secrets Helm values merged with defaults. type: object x-kubernetes-preserve-unknown-fields: true + chartVersion: + description: Pin the External Secrets Operator Helm chart version during import or a staged upgrade. Defaults to the version bundled with this stack. + type: string + minLength: 1 overrideAllValues: description: external-secrets Helm values that replace all defaults. type: object @@ -121,6 +125,58 @@ spec: description: AWS configuration. Required when backend=aws (region). type: object properties: + podIdentity: + type: object + description: Import and policy settings for the composed PodIdentity. Omitted policies retain the stack defaults. + properties: + name: + type: string + minLength: 1 + maxLength: 253 + pattern: '^[a-z0-9]([-a-z0-9]*[a-z0-9])?([.][a-z0-9]([-a-z0-9]*[a-z0-9])?)*$' + description: Name of the PodIdentity composite in the stack namespace. + role: + type: object + required: [externalName] + properties: + externalName: + type: string + minLength: 1 + description: Existing IAM role name, as recorded on the Role managed resource. + association: + type: object + required: [externalName] + properties: + externalName: + type: string + minLength: 1 + description: Existing EKS Pod Identity association ID for this cluster and service account. + managementPolicies: + type: array + minItems: 1 + description: Management actions for the identity leaves. Defaults to the stack management policies. + items: + type: string + enum: ["*", Observe, Create, Update, Delete, LateInitialize] + inlinePolicy: + type: array + description: Complete replacement for the default IAM inline policies, including an explicit empty list. + items: + type: object + required: [name, policy] + properties: + name: + type: string + minLength: 1 + policy: + type: string + minLength: 1 + managedPolicyArns: + type: array + description: Complete replacement for the default IAM managed policy ARNs, including an explicit empty list. + items: + type: string + minLength: 1 region: description: AWS region for the EKS cluster and Secrets Manager. type: string diff --git a/examples/secretstacks/import-identity.yaml b/examples/secretstacks/import-identity.yaml new file mode 100644 index 0000000..2f80235 --- /dev/null +++ b/examples/secretstacks/import-identity.yaml @@ -0,0 +1,25 @@ +apiVersion: hops.ops.com.ai/v1alpha1 +kind: SecretStack +metadata: + name: existing + namespace: default +spec: + clusterName: example + aws: + region: us-east-2 + podIdentity: + name: existing-controller + role: + externalName: example-controller + association: + externalName: a-0123456789abcdef0 + managementPolicies: + - Observe + inlinePolicy: [] + managedPolicyArns: [] + backend: aws + secretStore: + enabled: false + chartVersion: 2.2.0 + managementPolicies: + - Observe diff --git a/functions/render/000-state-init.yaml.gotmpl b/functions/render/000-state-init.yaml.gotmpl index 7afb800..c99a073 100644 --- a/functions/render/000-state-init.yaml.gotmpl +++ b/functions/render/000-state-init.yaml.gotmpl @@ -104,6 +104,7 @@ "labels" $labels "backend" $backend "helm" (dict + "chartVersion" ($spec.chartVersion | default "") "providerConfigRef" $helmProviderConfigRef "releaseName" ($spec.name | default "external-secrets") "namespace" $namespace @@ -112,6 +113,7 @@ ) "kubernetesProviderConfigRef" $k8sProviderConfigRef "aws" (dict + "podIdentity" ($aws.podIdentity | default dict) "enabled" $awsEnabled "region" $awsRegion "providerConfigRef" $awsProviderConfigRef @@ -146,7 +148,7 @@ "name" ($secretStoreSpec.name | default "default") ) "podIdentity" (dict - "name" (printf "%s-external-secrets" $name) + "name" (dig "podIdentity" "name" (printf "%s-external-secrets" $name) $aws) ) "serviceAccount" (dict "name" "external-secrets" diff --git a/functions/render/200-helm-release-external-secrets.yaml.gotmpl b/functions/render/200-helm-release-external-secrets.yaml.gotmpl index e6943eb..2e2245d 100644 --- a/functions/render/200-helm-release-external-secrets.yaml.gotmpl +++ b/functions/render/200-helm-release-external-secrets.yaml.gotmpl @@ -34,8 +34,12 @@ spec: chart: name: external-secrets repository: https://charts.external-secrets.io + {{- if $state.helm.chartVersion }} + version: {{ $state.helm.chartVersion | quote }} + {{- else }} # renovate: datasource=helm depName=external-secrets registryUrl=https://charts.external-secrets.io version: "2.15.0" + {{- end }} namespace: {{ $state.helm.namespace }} values: {{- if $state.helm.overrideAllValues }} diff --git a/functions/render/210-aws-pod-identity.yaml.gotmpl b/functions/render/210-aws-pod-identity.yaml.gotmpl index 74f9a1c..73abb2a 100644 --- a/functions/render/210-aws-pod-identity.yaml.gotmpl +++ b/functions/render/210-aws-pod-identity.yaml.gotmpl @@ -6,6 +6,7 @@ {{- if $state.aws.enabled }} +{{- $identity := $state.aws.podIdentity }} --- apiVersion: aws.hops.ops.com.ai/v1alpha1 kind: PodIdentity @@ -15,7 +16,7 @@ metadata: {{ setResourceNameAnnotation "pod-identity" }} labels: {{ $state.labels | toJson }} spec: - managementPolicies: {{ $state.managementPolicies | toJson }} + managementPolicies: {{ dig "managementPolicies" $state.managementPolicies $identity | toJson }} clusterName: {{ $state.clusterName }} region: {{ $state.aws.region }} providerConfigRef: @@ -27,9 +28,24 @@ spec: {{- if $state.aws.permissionsBoundaryArn }} permissionsBoundaryArn: {{ $state.aws.permissionsBoundaryArn }} {{- end }} + {{- with $identity.role }} + role: + externalName: {{ .externalName | quote }} + roleNameOverride: {{ .externalName | quote }} + {{- end }} + {{- with $identity.association }} + association: + externalName: {{ .externalName | quote }} + {{- end }} serviceAccount: name: {{ $state.serviceAccount.name }} namespace: {{ $state.serviceAccount.namespace }} + {{- if hasKey $identity "managedPolicyArns" }} + managedPolicyArns: {{ $identity.managedPolicyArns | toJson }} + {{- end }} + {{- if hasKey $identity "inlinePolicy" }} + inlinePolicy: {{ $identity.inlinePolicy | toJson }} + {{- else }} inlinePolicy: - name: external-secrets policy: | @@ -71,6 +87,7 @@ spec: } ] } + {{- end }} tags: {{ $state.aws.tags | toJson }} # Usage: Protect PodIdentity from deletion until Helm release is deleted diff --git a/tests/test-render/main.k b/tests/test-render/main.k index 5e9bf9d..f3aef02 100644 --- a/tests/test-render/main.k +++ b/tests/test-render/main.k @@ -357,6 +357,37 @@ _items = [ } } + metav1alpha1.CompositionTest { + metadata.name = "import-existing-identity" + spec = { + compositionPath = "apis/secretstacks/composition.yaml" + xrdPath = "apis/secretstacks/definition.yaml" + xrPath = "examples/secretstacks/import-identity.yaml" + timeoutSeconds = 60 + validate = False + assertResources = [ + { + apiVersion = "aws.hops.ops.com.ai/v1alpha1" + kind = "PodIdentity" + metadata.name = "existing-controller" + spec = { + managementPolicies = ["Observe"] + roleNameOverride = "example-controller" + role.externalName = "example-controller" + association.externalName = "a-0123456789abcdef0" + inlinePolicy = [] + managedPolicyArns = [] + } + } + { + apiVersion = "helm.m.crossplane.io/v1beta1" + kind = "Release" + metadata.name = "external-secrets" + spec.forProvider.chart.version = "2.2.0" + } + ] + } + } ] items = _items