diff --git a/.gitignore b/.gitignore index 3b735ec..29ee186 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,7 @@ # Go workspace file go.work + +# up project build artifacts and generated model cache for the e2e harness +e2e/_output/ +e2e/.up/ diff --git a/e2e/README.md b/e2e/README.md new file mode 100644 index 0000000..efa61ad --- /dev/null +++ b/e2e/README.md @@ -0,0 +1,81 @@ +# function-msgraph end-to-end tests + +This directory is a small Upbound project used only to run **real** end-to-end +tests for `function-msgraph`: `up test run tests/* --e2e` installs it on a +throwaway control plane, together with the function built from the pull request, +and checks that the function answers a live Microsoft Graph query. It is never +published to production. + +## Layout + +``` +e2e/ +├── upbound.yaml # test configuration; depends on the function under test (pinned by CI) +├── apis/xqueries/ # XRD + Composition that call the function +├── functions/.gitkeep # must exist for `up` +└── tests/ + └── e2etest-queries/ + └── test.yaml # E2ETest: default DRC audience + identity Secret, applies an XQuery, waits for Ready +``` + +The query reads the details of the e2e app's own service principal +(`ServicePrincipalDetails` for `solutions-e2e-function`). It reads no user or group +data and changes nothing. Any sign-in or query error (including a missing Graph +permission) makes the function return a fatal result, so the XR never becomes +`Ready` and the test fails. A service principal that isn't found gives an empty +result, not an error, so the XRD requires at least one entry in +`status.servicePrincipals`: an empty result is rejected and the XR never becomes +`Ready` either. (Checked: querying a name that doesn't exist fails the test.) + +## Credentials (no Azure secrets in CI) + +The function signs in with Azure workload identity, using the Upbound OIDC token +of its own pod: + +- `test.yaml` patches the control plane's `default` `DeploymentRuntimeConfig` with + `proidc.cloud-spaces.upbound.io/audience: api://AzureADTokenExchange`. Upbound + then mounts a token for that audience at + `/var/run/secrets/upbound.io/provider/token` (function pods too). +- `test.yaml` also creates the `azure-e2e-identity` Secret the Composition passes + to the function. It holds only non-secret values: the Entra app (client) ID, the + tenant ID and the token path. + +### Entra setup + +App registration `solutions-e2e-function` (client ID +`b7b332bc-677c-4bae-a3ba-0aa7e6b2f24a`), shared with the function-azresourcegraph +e2e test. It needs **no Microsoft Graph permission**: a service principal can +read its own application and service principal details without one (see +"Permissions for specific scenarios" on Microsoft's *List servicePrincipals* +page). It only needs a federated credential: + +| Field | Value | +|----------|-------| +| Issuer | `https://proidc.upbound.io` | +| Audience | `api://AzureADTokenExchange` | +| Subject | `mcp:solutions/function-msgraph-e2e-wrapper-uptest-e2e-msgraph:function:solutions-function-msgraph` | + +Entra matches the subject exactly. It is `mcp:/:function:`: +`up test` names the control plane `-uptest-`, and a function +installed as a dependency is named `-`. Renaming the project, the test +or the org changes the subject, and the federated credential must follow. + +## Two repos, one function + +The function dev build is pushed to `solutions/` and the wrapper project to +**`solutions/-e2e-wrapper`**. They must be separate: Crossplane's package +lock keys dependency-graph nodes by source repo, so a project and a function +sharing one repo collide (`node ... already exists`). The `-e2e-wrapper` +repo is granted to the e2e robot in `solutions-gitops-prod`. + +## What runs in CI + +On a pull request labelled `run-e2e-tests`, `.github/workflows/e2e.yaml` (synced +from `upbound/sa-up`): + +1. builds the function package from the PR head and pushes it to `solutions/` + at a dev tag, +2. pins that tag in `upbound.yaml` (`@FUNCTION_VERSION@`), +3. switches to the e2e Spaces control plane group, and +4. runs `up test run tests/* --e2e`, pushing the wrapper project to + `solutions/-e2e-wrapper`. diff --git a/e2e/apis/xqueries/composition.yaml b/e2e/apis/xqueries/composition.yaml new file mode 100644 index 0000000..370121d --- /dev/null +++ b/e2e/apis/xqueries/composition.yaml @@ -0,0 +1,37 @@ +# One read-only Microsoft Graph query: the details of the e2e app's own service +# principal. A service principal can read its own details without any Graph +# permission, so the app needs no admin consent. No user data. The function +# signs in with Azure workload identity, exchanging the Upbound OIDC token for an +# Entra token. Any sign-in or query error is fatal, and an empty result fails the +# XRD's minItems, so in both cases the XR never goes Ready. +apiVersion: apiextensions.crossplane.io/v1 +kind: Composition +metadata: + name: xqueries.e2e.msgraph.upbound.io +spec: + compositeTypeRef: + apiVersion: e2e.msgraph.upbound.io/v1alpha1 + kind: XQuery + mode: Pipeline + pipeline: + - step: service-principal + functionRef: + name: solutions-function-msgraph + input: + apiVersion: msgraph.fn.crossplane.io/v1alpha1 + kind: Input + identity: + type: AzureWorkloadIdentityCredentials + queryType: ServicePrincipalDetails + servicePrincipals: + - solutions-e2e-function + target: status.servicePrincipals + credentials: + - name: azure-creds + source: Secret + secretRef: + namespace: crossplane-system + name: azure-e2e-identity + - step: auto-ready + functionRef: + name: crossplane-contrib-function-auto-ready diff --git a/e2e/apis/xqueries/definition.yaml b/e2e/apis/xqueries/definition.yaml new file mode 100644 index 0000000..efd63d8 --- /dev/null +++ b/e2e/apis/xqueries/definition.yaml @@ -0,0 +1,31 @@ +apiVersion: apiextensions.crossplane.io/v1 +kind: CompositeResourceDefinition +metadata: + name: xqueries.e2e.msgraph.upbound.io +spec: + group: e2e.msgraph.upbound.io + scope: Namespaced + names: + kind: XQuery + plural: xqueries + versions: + - name: v1alpha1 + served: true + referenceable: true + schema: + openAPIV3Schema: + type: object + properties: + spec: + type: object + status: + type: object + properties: + servicePrincipals: + # At least one result, so "not found" can't pass: the status + # write is rejected and the XR never goes Ready. + type: array + minItems: 1 + items: + type: object + x-kubernetes-preserve-unknown-fields: true diff --git a/e2e/functions/.gitkeep b/e2e/functions/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/e2e/tests/e2etest-queries/test.yaml b/e2e/tests/e2etest-queries/test.yaml new file mode 100644 index 0000000..b7c29d7 --- /dev/null +++ b/e2e/tests/e2etest-queries/test.yaml @@ -0,0 +1,57 @@ +# End-to-end test for function-msgraph. `up test run tests/* --e2e` spins +# up a throwaway Spaces control plane, installs this project (the XRD and +# Composition in apis/, plus the function under test as a dependency), creates the +# XR and waits for it to go Ready. A failed sign-in or query makes the function +# return fatal, the XR never goes Ready, and the test fails. +apiVersion: meta.dev.upbound.io/v1alpha1 +kind: E2ETest +metadata: + name: e2e-msgraph +spec: + crossplane: + autoUpgrade: + channel: Rapid + defaultConditions: + - Ready + initResources: + # Patch the control plane's default DeploymentRuntimeConfig before the + # functions install, so every function pod gets an Upbound OIDC token with + # the Entra token-exchange audience, mounted at + # /var/run/secrets/upbound.io/provider/token (function pods too). Spaces + # policy only allows the audience annotation here. + - apiVersion: pkg.crossplane.io/v1beta1 + kind: DeploymentRuntimeConfig + metadata: + name: default + spec: + deploymentTemplate: + spec: + selector: {} + template: + metadata: + annotations: + proidc.cloud-spaces.upbound.io/audience: api://AzureADTokenExchange + # Workload identity settings for the function. Nothing here is secret: the + # Entra app and tenant IDs, and the path of the Upbound OIDC token. + - apiVersion: v1 + kind: Secret + metadata: + name: azure-e2e-identity + namespace: crossplane-system + type: Opaque + stringData: + credentials: | + { + "clientId": "b7b332bc-677c-4bae-a3ba-0aa7e6b2f24a", + "tenantId": "b9925bc4-8383-4c37-b9d2-fa456d1bb1c7", + "federatedTokenFile": "/var/run/secrets/upbound.io/provider/token" + } + manifests: + - apiVersion: e2e.msgraph.upbound.io/v1alpha1 + kind: XQuery + metadata: + name: e2e-query + namespace: default + spec: {} + skipDelete: false + timeoutSeconds: 900 diff --git a/e2e/upbound.yaml b/e2e/upbound.yaml new file mode 100644 index 0000000..74539e3 --- /dev/null +++ b/e2e/upbound.yaml @@ -0,0 +1,25 @@ +apiVersion: meta.dev.upbound.io/v2alpha1 +kind: Project +metadata: + name: function-msgraph-e2e-wrapper +spec: + # CI rewrites @FUNCTION_VERSION@ to the dev tag it just built and pushed from + # the pull request, so the configuration installs that build as a dependency. + dependsOn: + - apiVersion: pkg.crossplane.io/v1 + kind: Function + package: xpkg.upbound.io/solutions/function-msgraph + version: '@FUNCTION_VERSION@' + - apiVersion: pkg.crossplane.io/v1 + kind: Function + package: xpkg.upbound.io/crossplane-contrib/function-auto-ready + version: '>=v0.0.0' + description: | + Test configuration that runs function-msgraph in end-to-end tests: an XRD and + a Composition that call the function, installed on a throwaway control plane + by `up test run tests/* --e2e`. Never published to production. + license: Apache-2.0 + maintainer: Upbound + readme: | + See e2e/README.md. + repository: xpkg.upbound.io/solutions/function-msgraph-e2e-wrapper