Skip to main content

Agents in Kubernetes and the cloud

Use the identity that a platform already gives a workload: a Kubernetes service account, AWS, Google Cloud, Azure or SPIFFE.

RecipeDraftView source
This page is a draft. The code exists, and not all of it has been run for this page yet. The page says what was checked.
Works with
JavaScript / TypeScriptPythonGoPlain HTTPKubernetes

What it shows​

  1. You register an agent with the identity type workload and the provider k8s. The binding names the issuer of the cluster, a namespace and a service account.
  2. The manifest gives the pod a second service account token. Its audience is the address of the agent, and it expires after 10 minutes.
  3. The program reads that token from a file and sends it to the attestation endpoint of the agent.
  4. LumoAuth checks the signature of the cluster, the audience, the namespace and the service account. It answers with an access token of the agent that is valid for 15 minutes.
  5. The program uses that token. The pod holds no LumoAuth secret.

The same program runs on the other platforms. Only the source of the platform token changes:

PlatformProviderWhere the workload gets its token
Kubernetesk8sA projected service account token with the audience of the agent, as in the manifest.
AWS EKSawsThe same projected token. LumoAuth verifies it with the OIDC issuer of the EKS cluster.
Google CloudgcpThe metadata server: the identity token of the service account, with audience and format=full.
AzureazureThe endpoint for managed identities, with the audience as resource.
SPIFFEspiffespire-agent api fetch jwt -audience with the audience of the agent.

Before you start​

  • A Kubernetes cluster whose OIDC issuer LumoAuth can reach over https.
  • An API key with the scopes admin:agents:read and admin:agents:write, for the setup script.
  • Check that your plan includes workload identity for your platform in the plans matrix.
VariableValue
LUMOAUTH_URLThe address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region.
LUMOAUTH_ORG_IDYour organization, for example acme-corp.
LUMOAUTH_API_KEYFor the setup script only: an API key with the scopes admin:agents:read and admin:agents:write. The workload needs no API key and no secret.
K8S_ISSUERFor the setup script only: the OIDC issuer of your cluster, an https address that LumoAuth can reach.
K8S_NAMESPACEFor the setup script only, and optional: the namespace of the pod. Default: agents.
K8S_SERVICE_ACCOUNTFor the setup script only, and optional: the service account of the pod. Default: order-sync.
AGENT_IDThe agent ID (agt_…). The setup script prints it.
WORKLOAD_TOKEN_FILEThe file that holds the token of the platform. Default: /var/run/secrets/lumoauth/token, the path of the manifest.

The setup script registers the agent with the identity type workload, bound to one service account of one cluster. It prints the audience for the manifest.

K8S_ISSUER=https://oidc.cluster.example.com node agents/kubernetes-and-cloud-workloads/setup.mjs
setup.mjs
// Registers an agent that proves its identity with the service account token
// of a Kubernetes pod. Run it again at any time, also to change the binding.
//
// K8S_ISSUER=https://oidc.cluster.example.com node agents/kubernetes-and-cloud-workloads/setup.mjs
//
// The API key needs the scopes admin:agents:read and admin:agents:write.
import { call, ensure, printEnv } from '../../_lib/admin.mjs';

if (!process.env.K8S_ISSUER) {
console.error('Set K8S_ISSUER to the OIDC issuer of your cluster, an https address.');
process.exit(1);
}

// Only a pod that runs as this service account, in this namespace of this
// cluster, can act as the agent.
const binding = {
workloadIdentityProvider: 'k8s',
workloadIdentityConfig: {
issuer: process.env.K8S_ISSUER,
namespace: process.env.K8S_NAMESPACE ?? 'agents',
service_account: process.env.K8S_SERVICE_ACCOUNT ?? 'order-sync',
},
};

const found = await ensure('/admin/agents', (a) => a.name === 'Order sync agent', {
name: 'Order sync agent',
description: 'Copies new orders from the shop to the warehouse system.',
identityType: 'workload',
capabilities: ['orders:read', 'orders:sync'],
...binding,
});

// The update applies the binding to an agent that an earlier run registered.
const { data: agent } = await call('PATCH', `/admin/agents/${found.agentId}`, binding);

console.log(`Agent "${agent.name}" accepts the workload ${agent.workloadIdentity}.`);
console.log(`Put this audience in the manifest: ${agent.workloadIdentityConfig.audience}`);
printEnv({ AGENT_ID: agent.agentId });

Then put the agent ID and the audience into the manifest, build an image with the program, and apply the manifest.

To try the program from your own computer first, ask the cluster for a token of the service account and run the program with that file:

kubectl create token order-sync --namespace agents --duration 10m \
--audience "https://app.lumoauth.dev/orgs/acme-corp/api/v1/agents/$AGENT_ID" > workload-token
export WORKLOAD_TOKEN_FILE="$PWD/workload-token"

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. The setup script and the refusal of a wrong token were run against an organization. The accepted exchange was not run: it needs a cluster. The manifest was not applied to a cluster. The output below follows the response format of the docs.

agents/kubernetes-and-cloud-workloads/manifest.yaml
# The service account of the agent, and a pod that runs as it. The pod gets a
# second service account token, made for the audience of the agent.
#
# kubectl apply -f manifest.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: order-sync
namespace: agents
---
apiVersion: v1
kind: Pod
metadata:
name: order-sync
namespace: agents
spec:
serviceAccountName: order-sync
restartPolicy: Never
containers:
- name: agent
image: registry.example.com/order-sync:1.0 # your image, with the program of this recipe
env:
- name: LUMOAUTH_URL
value: https://app.lumoauth.dev
- name: LUMOAUTH_ORG_ID
value: acme-corp
- name: AGENT_ID
value: agt_3f9a51c07be24d18 # the agent ID that the setup script printed
- name: WORKLOAD_TOKEN_FILE
value: /var/run/secrets/lumoauth/token
volumeMounts:
- name: lumoauth-token
mountPath: /var/run/secrets/lumoauth
readOnly: true
volumes:
- name: lumoauth-token
projected:
sources:
- serviceAccountToken:
# The audience that the setup script printed: the address of the agent.
audience: https://app.lumoauth.dev/orgs/acme-corp/api/v1/agents/agt_3f9a51c07be24d18
expirationSeconds: 600
path: token
agents/kubernetes-and-cloud-workloads/js/agent.mjs
// A workload exchanges the token of its platform for a LumoAuth token of the
// agent. In a pod, the manifest sets the variables and mounts the token file.
//
// node agents/kubernetes-and-cloud-workloads/js/agent.mjs
import { readFile } from 'node:fs/promises';
import { attestWorkload } from '@lumoauth/agent';

const agentId = process.env.AGENT_ID;
const api = `${process.env.LUMOAUTH_URL}/orgs/${process.env.LUMOAUTH_ORG_ID}/api/v1`;

// 1. Read the token of the platform. On Kubernetes it is a file that the
// kubelet renews. On another platform, only this step changes.
const tokenFile = process.env.WORKLOAD_TOKEN_FILE ?? '/var/run/secrets/lumoauth/token';
const platformToken = (await readFile(tokenFile, 'utf8')).trim();

// 2. Send it to the attestation endpoint of the agent. LumoAuth checks the
// signature of the cluster, the audience, the namespace and the service account.
let token;
try {
token = await attestWorkload({ agentId, attestationToken: platformToken });
} catch (error) {
// The answer never says which check failed. The audit log of the organization does.
console.log(`refused: ${error.code}`);
process.exit(1);
}
console.log(`workload: ${token.identity.subject}`);
console.log(`token: ${token.tokenType}, valid for ${token.expiresIn} seconds`);
console.log(`scope: ${token.scope}`);

// 3. Use the token like the token of any other agent.
const me = await fetch(`${api}/agents/me`, { headers: { Authorization: `Bearer ${token.accessToken}` } });
const { identity } = await me.json();
console.log(`agent: ${identity.agent_id} (${identity.status})`);
node agents/kubernetes-and-cloud-workloads/js/agent.mjs

Compiles. Not run against an organization. The program ran against an organization with a token from an unknown issuer, and LumoAuth refused it with attestation_rejected. The accepted exchange needs a Kubernetes cluster.

Check the result​

Output
workload: k8s:agents/order-sync
token: Bearer, valid for 900 seconds
scope: orders:read orders:sync
agent: agt_3f9a51c07be24d18 (active)
  • Run a pod with another service account, or in another namespace. The program prints refused: attestation_rejected.
  • The answer never says which check failed. In the portal, open Audit logs under Security and filter for the event agent.attestation.rejected. The reason of the entry names the check.
  • Run the program twice with the same token file. The second run is refused, because LumoAuth accepts each platform token once.

Limits​

  • LumoAuth accepts a platform token once. Read the file again only after the kubelet renewed it, or ask for a new token for each exchange.
  • A platform token that expires more than 24 hours from now is refused. Ask for 10 minutes, as the manifest does.
  • On AWS, only EKS pods with a service account token are supported. A workload on EC2, Lambda or ECS has no OIDC token: use the provider custom or SPIFFE there.
  • The Python and Go SDKs have no call for the attestation endpoint, so those two programs send a plain HTTP request.
  • A workload that does not hold its agent ID can send the same token to the token endpoint, with the token-exchange grant and subject_token_type=urn:ietf:params:oauth:token-type:jwt. LumoAuth then finds the agent by the subject of the token.