Skip to main content

Read what an agent did

Query the audit log for one agent, read why an action was refused, and export the rows as JSON, from the terminal.

RecipeReadyView source
Packages
lumo
Works with
lumo CLIPlain HTTP

What it shows​

  1. The setup script registers an agent and lets it act: it gets a token, asks for one capability that it holds and one that it does not hold.
  2. The script lists the rows in which the agent is the actor, from a given time on.
  3. It reads the refused action in full. The action and the reason are in the context of the row.
  4. It finds the row that registered the agent. When an API key made the change, the row names the person who created that key.
  5. It exports the rows of the agent as a JSON file.

Before you start​

  • An API key with the scopes admin:audit:read and admin:users:read. Create it in the portal under Applications & developer tools, API keys. The person who creates the key needs the permissions audit.view and audit.export.
  • Check that your plan includes the Admin API and audit log export in the plans matrix.
  • The lumo CLI for the first version, curl for the second, and jq for both.
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_KEYAn API key with the scopes admin:audit:read and admin:users:read. Its creator needs the permissions audit.view and audit.export.
AGENT_IDThe ID of the agent whose rows you read. The setup script prints it.
SINCEThe time from which the rows are read, in ISO 8601. The setup script prints the time before the agent acted.
AUDIT_FILEOptional. The file for the export. Default: expense-report-agent-audit.json in the current folder.

The setup script registers the agent and lets it act: it gets a token, asks for expenses:read, which it holds, and asks for expenses:approve, which it does not hold. Its API key also needs admin:agents:read and admin:agents:write.

node agents/audit-trail/setup.mjs
setup.mjs
// Registers the agent of this recipe, "Expense report agent", and lets it act,
// so that the audit log has rows to read. The agent gets a token, asks for a
// capability that it holds, and asks for one that it does not hold. Run it
// again at any time.
//
// node agents/audit-trail/setup.mjs
//
// The API key needs the scopes admin:agents:read and admin:agents:write.
import { LumoAgent } from '@lumoauth/agent';
import { call, ensure, printEnv } from '../../_lib/admin.mjs';

// The recipe reads the rows from this moment on.
const since = new Date(Date.now() - 1000).toISOString();

const agent = await ensure('/admin/agents', (a) => a.name === 'Expense report agent', {
name: 'Expense report agent',
description: 'Reads expense reports and prepares them for approval. It may not approve them.',
identityType: 'client_secret',
capabilities: ['expenses:read'],
});

// LumoAuth shows a secret once, in the answer that creates it. On a second
// run the script therefore asks for a new secret, and the old one stops.
const secret =
agent.clientSecret ??
(await call('POST', `/admin/agents/${agent.agentId}/rotate-credentials`)).data.clientSecret;

// The agent acts with its own credentials. LumoAuth writes a row for the
// token and for the refusal. An allowed question writes no row.
const expenseReportAgent = new LumoAgent({ clientId: agent.agentId, clientSecret: secret });
for (const action of ['expenses:read', 'expenses:approve']) {
const answer = await expenseReportAgent.ask(action, { report: 'EXP-2291', amount: 640 });
console.log(`The agent asked for ${action}: ${answer.allowed ? 'allowed' : 'denied'}`);
}

printEnv({ AGENT_ID: agent.agentId, SINCE: since });

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. Every version below was run against a LumoAuth organization.

agents/audit-trail/cli/trail.sh
#!/bin/sh
# Read what one agent did, with the lumo CLI: list its rows of the audit log,
# read the refused action, find the person behind its registration, and
# export the rows as JSON.
#
# sh agents/audit-trail/cli/trail.sh
#
# The script needs the lumo CLI and jq.
set -eu

# The CLI has its own names for the three values of the recipes.
export LUMO_BASE_URL="$LUMOAUTH_URL" LUMO_ORG="$LUMOAUTH_ORG_ID" LUMO_API_KEY="$LUMOAUTH_API_KEY"
FILE="${AUDIT_FILE:-expense-report-agent-audit.json}"

# 1. The rows in which the agent is the actor, from SINCE on, oldest first.
ROWS=$(lumo logs list --user "$AGENT_ID" --from "$SINCE" --limit 100 -o json)
echo "what the agent did:"
printf '%s\n' "$ROWS" | jq -r '.data | sort_by(.id)[] | " \(.eventType) \(.status)"'

# 2. A refusal names the action and the reason in its context. Only the
# single row has the context.
REFUSED=$(printf '%s\n' "$ROWS" | jq -r '[.data[] | select(.eventType == "agent.capability_check_denied") | .id][0] // empty')
if [ -n "$REFUSED" ]; then
lumo logs get "$REFUSED" -o json |
jq -r '.data.context | "refused: \(.requested_action), reason \(.reason), decided by \(.decided_by)"'
fi

# 3. The row that registered the agent names the actor. For an API key, the
# context names the person who created the key.
CREATED=$(lumo api GET /audit-logs --query targetId="$AGENT_ID" --query eventType=agent.created -o json | jq -r '.data[0].id // empty')
if [ -n "$CREATED" ]; then
ENTRY=$(lumo logs get "$CREATED" -o json)
PERSON=$(printf '%s\n' "$ENTRY" | jq -r '.data.context.actor_owner_id // .data.actorId')
EMAIL=$(lumo users get "$PERSON" -o json | jq -r '.data.email')
printf '%s\n' "$ENTRY" | jq -r --arg email "$EMAIL" '.data | "registered by: \(.actorType) \"\(.actorName)\", person \($email)"'
fi

# 4. The same rows as a file, with the context of each row. The export takes
# the filters of the list.
lumo api GET /audit-logs/export --query format=json --query actorId="$AGENT_ID" --query from="$SINCE" -o json > "$FILE"
echo "exported: $(jq '.data | length' "$FILE") rows to $FILE"
sh agents/audit-trail/cli/trail.sh

Check the result​

Output
what the agent did:
oauth.agent_token_issued success
abac_check failure
permission_check failure
abac_check failure
permission_check failure
agent.capability_check_denied success
refused: expenses:approve, reason capability_mismatch, decided by capability
registered by: api_key "Recipes key", person maya.chen@example.com
exported: 6 rows to expense-report-agent-audit.json
  • For each question of the agent, LumoAuth also writes the checks of its policy engine, abac_check and permission_check. Their result failure means that no attribute rule and no permission matched. The capabilities of the agent decided the answers: expenses:read was allowed, and only expenses:approve was refused.
  • An allowed question writes no row of its own. A refused one writes agent.capability_check_denied, with the result success. Read the refusal from the event type and the context. The context holds the same audit_id that the agent received in the answer.
  • In the portal, open Audit logs under Security. Choose the agent under Actor and click Apply filters. Export logs downloads the same rows as CSV.

Limits​

  • The CLI has no command that follows the log live. To keep every row outside LumoAuth, send the log to a log destination.
  • The rows of the actions of an agent name the agent, not a person. Only rows that a person or an API key wrote, such as agent.created, lead to a person.
  • lumo logs export takes no filter for the actor, so the CLI version calls the export endpoint through lumo api.
  • The API returns only the entries inside the audit retention of your plan. One export holds at most 10,000 rows.