Skip to content

Using kubelet identity to access ACR OCI charts #1071

@gldraphael

Description

@gldraphael

I created a test cluster exp-aks-02 with the following configuration:

Kubernetes Version: 1.25.6
Authentication and Authorization: Azure AD authentication with Kubernetes RBAC
Network Plugin: Azure CNI

(The cluster does not use the ACR integration.)

I then went ahead and bootstrapped flux, and assigned ACR Pull and Reader permissions to the User Assigned Managed Identity exp-aks-02-agentpool on a ACR instance.

At this point, I expected it to just work, but flux get sources would show this error:

unknown build error: failed to get credential from azure: DefaultAzureCredential: failed to acquire a token.
Attempted credentials:
        EnvironmentCredential: missing environment variable AZURE_TENANT_ID
        ManagedIdentityCredential: no default identity is assigned to this resource
        AzureCLICredential: Azure CLI not found on path

Ideas?


Other Observations

Fetching token by specifying the UAI to use

I followed the thread at #898 and concluded the reason this happens is because I have two UAIs (User Assigned managed Identities) attached to this cluster (exp-aks-02-agentpool and aciconnectorlinux-exp-aks-02).

So I tried patching the flux-system kustomization to add AZURE_CLIENT_ID:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - gotk-components.yaml
  - gotk-sync.yaml
labels:
  - pairs:
      toolkit.fluxcd.io/tenant: sre-team
patches:
  - patch: |
      - op: add
        path: /spec/template/spec/containers/0/args/-
        value: --concurrent=20
      - op: add
        path: /spec/template/spec/containers/0/args/-
        value: --requeue-dependency=5s
    target:
      kind: Deployment
      name: "(kustomize-controller|helm-controller|source-controller)"
  - patch: |
      - op: add
        path: /spec/template/spec/containers/0/env/-
        value:
          name: AZURE_CLIENT_ID
          value: --client-id--
    target:
      kind: Deployment
      name: "(helm-controller|source-controller)"

But I now see this error (which almost feels like a bug):

unknown build error: failed to get credential from azure: error exchanging token: failed to decode the response: invalid character '<' looking for beginning of value

However hitting the token API directly works as long as I include the client_id parameter:

$ kubectl exec -it source-controller-59b5c97495-htrtb -n flux-system -- /bin/sh
$ wget -q -O - "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://management.azure.com/&client_id=$AZURE_CLIENT_ID" --header "Metadata: true"
{"access_token":"--redacted--","client_id":"--client-id--","expires_in":"84928","expires_on":"1681412609","ext_expires_in":"86399","not_before":"1681325909","resource":"https://management.azure.com/","token_type":"Bearer"}

akv2k8s works ok

I am able to consume secrets from azure keyvault using the akv2k8s project which appears to use the userAssignedIdentityID value from /etc/kubernetes/azure.json:

apiVersion: v1
kind: Namespace
metadata:
  name: akv2k8s
  labels:
    toolkit.fluxcd.io/tenant: sre-team
---
apiVersion: source.toolkit.fluxcd.io/v1beta2
kind: HelmRepository
metadata:
  name: akv2k8s
  namespace: akv2k8s
spec:
  interval: 60m0s
  url: https://charts.spvapi.no
---
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
  name: akv2k8s
  namespace: akv2k8s
spec:
  interval: 60m
  chart:
    spec:
      chart: akv2k8s
      version: "2.*"
      sourceRef:
        kind: HelmRepository
        name: akv2k8s
      interval: 12h
  values:
    global:
      metrics:
        enabled: true
---
apiVersion: spv.no/v1alpha1
kind: AzureKeyVaultSecret
metadata:
  name: test-credentials
  namespace: monitoring
spec:
  vault:
    name: vault-name
    object:
      type: multi-key-value-secret
      name: test-credentials
      contentType: application/x-json
  output:
    secret:
      name: test-credentials

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions