In breve
- Il ledger ha registrato alice come richiedente, perché il token gli è arrivato passando da payments.
- Senza un claim purpose, payments ha rifiutato la richiesta, ma il ledger ha accettato lo stesso token quando l'ho chiamato direttamente.
- Un importo modificato è stato rifiutato da entrambi i servizi e un token emesso solo per payments è stato rifiutato dal ledger.
- I limiti sono un segreto condiviso, nessun accesso utente e nomi di claim che sono quelli di questo lab.
Ripeti questo lab
Ho scritto questo lab perché tu possa rifarlo per conto tuo. Ogni passo ha i comandi da copiare e l'output che ho ottenuto io, e puoi segnarlo come fatto. Le spunte restano solo nel tuo browser.
Copia un prompt già pronto per Claude Code, Cursor, Codex o un altro agente di codice. Solo per ambienti di test.
Mostra il prompt
I want to run a hands-on lab from iamops.dev, and I would like your help to do it safely. Lab: Transaction token in una catena di chiamate: tre servizi locali, un token Read the whole lab here (Markdown): https://iamops.dev/it/transaction-token-call-chain-lab.md What the lab needs before the first step: - Python 3, curl, jq and openssl How I want you to work: 1. Read the lab from start to finish first (each step lists its own prerequisites). Then check that the prerequisites are in place and that the tool versions match what the lab expects. Tell me what is missing before you run anything. 2. Run one step at a time. Before each command, show it to me and say in a sentence what it does. 3. Ask me before anything that costs money, creates cloud resources, touches a real tenant or account, or deletes anything. Wait for my answer. 4. Use the placeholders from the lab. Never paste real secrets, tokens or production identities into a command, a file or this chat. If a value is needed, tell me where to set it myself. 5. After each step, compare the output with the expected output in the lab. If it does not match, stop and tell me what differs. Do not carry on or invent a fix without asking me. 6. At the end, report what passed, what failed and which cleanup you did. Then offer to tick the steps off for me. The lab page keeps ticks only in my browser, so if you cannot do that, list the steps I should tick. This lab is for test environments only. Do not run it against production, or against a tenant or account that holds real data or real users. You may answer in my language.
Un transaction token porta il richiedente, la finalità e la fattura attraverso una catena di chiamate tra servizi. In questa esecuzione il ledger ha registrato alice, perché il token gli è arrivato passando da payments. Il controllo sulla finalità stava solo in payments. Quando il token non aveva il claim purpose, payments l’ha rifiutato, ma il ledger ha accettato lo stesso token quando l’ho chiamato direttamente. Un importo modificato è stato rifiutato in entrambi i salti, e un token emesso solo per payments è stato rifiutato dal ledger.
La guida sui transaction token spiega l’idea. Questo lab costruisce una piccola versione della catena e invia richieste vere attraverso di essa.
Preparazione e avvio#
Ti servono Python 3 senza pacchetti extra, più curl, jq e openssl. I tre servizi ascoltano su 127.0.0.1, sulle porte da 4701 a 4703. Non serve nessun account né un tenant reale, e nulla lascia il computer dopo il download.
I servizi sono un unico file Python, chain_service.py. Ogni ruolo gira come processo separato, e tutti e tre leggono lo stesso file della chiave.
mkdir -p txn-lab && cd txn-lab
curl -s -O https://iamops-dev.github.io/lab-files/transaction-token-call-chain-lab/chain_service.py
mkdir -p run
openssl rand -hex 32 > key.hex && chmod 600 key.hex
export ISSUER=http://127.0.0.1:4701 PAYMENTS=http://127.0.0.1:4702 LEDGER=http://127.0.0.1:4703
python3 -I chain_service.py --role ledger --port 4703 --key-file key.hex --log run/ledger.log &
python3 -I chain_service.py --role payments --port 4702 --key-file key.hex --downstream "$LEDGER" --log run/payments.log &
python3 -I chain_service.py --role issuer --port 4701 --key-file key.hex --log run/issuer.log &Un controllo di stato conferma che ogni servizio risponde prima che venga inviato qualsiasi token.
curl -s "$ISSUER/healthz" | jq -c .
curl -s "$PAYMENTS/healthz" | jq -c .
curl -s "$LEDGER/healthz" | jq -c .
cat run/*.out
Passo 2: un token di servizio semplice si ferma al ledger#
L’agente chiede all’issuer un token di servizio. Il token nomina un solo workload, agent-1, e nessun utente. L’agente lo invia a payments con una richiesta per la fattura 1042.
PLAIN=$(curl -s -X POST "$ISSUER/token/service" -d 'sub=agent-1' | jq -r .token)
echo "$PLAIN" | jq -Rr 'split(".") | .[1] | gsub("-";"+") | gsub("_";"/") | . + "==" | @base64d | fromjson'
curl -s -w '\nHTTP %{http_code}\n' -X POST "$PAYMENTS/pay" -H "Authorization: Bearer $PLAIN" \
-H 'Content-Type: application/json' -d '{"invoice":"1042","amount":"120.00"}'
tail -n 1 run/payments.log | jq -c '{role, decision, status, actor, requester, own_checks, downstream_status}'
tail -n 1 run/ledger.log | jq -c '{role, decision, status, actor, requester, reason}'
Payments verifica la firma, l’issuer e l’audience, poi inoltra la chiamata. Il ledger la rifiuta perché nel token non c’è nessun richiedente. Il log di payments mostra che i suoi controlli sono passati, e il log del ledger mostra il rifiuto.
Passo 3: un token di transazione arriva al ledger#
L’issuer emette un token di transazione per alice. Il token indica il richiedente, il workload che agisce, la finalità, la fattura e l’importo. Payments controlla la finalità e il corpo della richiesta, poi passa lo stesso token al ledger, che registra alice.
TXN=$(curl -s -X POST "$ISSUER/token/transaction" -d 'requester=alice' -d 'actor=agent-1' \
-d 'purpose=pay_invoice' -d 'invoice=1042' -d 'amount=120.00' | jq -r .token)
echo "$TXN" | jq -Rr 'split(".") | .[1] | gsub("-";"+") | gsub("_";"/") | . + "==" | @base64d | fromjson'
curl -s -w '\nHTTP %{http_code}\n' -X POST "$PAYMENTS/pay" -H "Authorization: Bearer $TXN" \
-H 'Content-Type: application/json' -d '{"invoice":"1042","amount":"120.00"}'
tail -n 1 run/payments.log | jq -c '{role, decision, status, actor, requester, purpose, txn, own_checks, downstream_status}'
tail -n 1 run/ledger.log | jq -c '{role, decision, status, actor, requester, purpose, txn, aud: .claims.aud, result: .decision}'
Lo stesso valore txn compare in entrambi i log. È il token in ciascun salto.
Passo 4: senza il claim purpose#
Stavolta il token non ha il claim purpose. Payments lo rifiuta con insufficient_purpose. Poi ho inviato lo stesso token direttamente al ledger, che ha registrato la voce. In questo lab solo payments controlla la finalità, quindi un chiamante che raggiunge direttamente il ledger supera quel controllo.
TXN=$(curl -s -X POST "$ISSUER/token/transaction" -d 'requester=alice' -d 'actor=agent-1' \
-d 'invoice=1042' -d 'amount=120.00' | jq -r .token)
curl -s -w '\nHTTP %{http_code}\n' -X POST "$PAYMENTS/pay" -H "Authorization: Bearer $TXN" \
-H 'Content-Type: application/json' -d '{"invoice":"1042","amount":"120.00"}'
curl -s -w '\nHTTP %{http_code}\n' -X POST "$LEDGER/entries" -H "Authorization: Bearer $TXN" \
-H 'Content-Type: application/json' -d '{"invoice":"1042","amount":"120.00"}'
tail -n 1 run/payments.log
tail -n 1 run/ledger.log
Passo 5: un importo modificato viene rifiutato#
Il token dice 120.00. Ho cambiato il corpo della richiesta in 1200.00. Payments l’ha rifiutata, e anche il ledger l’ha rifiutata quando l’ho chiamato direttamente. Entrambi hanno risposto request_mismatch, quindi ogni servizio confronta il corpo con il token.
curl -s -w '\nHTTP %{http_code}\n' -X POST "$PAYMENTS/pay" -H "Authorization: Bearer $TXN" \
-H 'Content-Type: application/json' -d '{"invoice":"1042","amount":"1200.00"}'
curl -s -w '\nHTTP %{http_code}\n' -X POST "$LEDGER/entries" -H "Authorization: Bearer $TXN" \
-H 'Content-Type: application/json' -d '{"invoice":"1042","amount":"1200.00"}'
Passo 6: un token solo per payments#
Ho emesso un token di transazione con l’audience impostata solo su payments, poi l’ho inviato al ledger. Il ledger l’ha rifiutato con invalid_token, perché l’audience del token è payments e non ledger.
TXN=$(curl -s -X POST "$ISSUER/token/transaction" -d 'requester=alice' -d 'actor=agent-1' \
-d 'purpose=pay_invoice' -d 'invoice=1042' -d 'amount=120.00' -d 'aud=payments' | jq -r .token)
curl -s -w '\nHTTP %{http_code}\n' -X POST "$LEDGER/entries" -H "Authorization: Bearer $TXN" \
-H 'Content-Type: application/json' -d '{"invoice":"1042","amount":"120.00"}'
Passo 7: un token che scade#
Un token con durata di un secondo è stato rifiutato dopo due secondi con token expired.
TXN=$(curl -s -X POST "$ISSUER/token/transaction" -d 'requester=alice' -d 'actor=agent-1' \
-d 'purpose=pay_invoice' -d 'invoice=1042' -d 'amount=120.00' -d 'ttl=1' | jq -r .token)
sleep 2
curl -s -w '\nHTTP %{http_code}\n' -X POST "$PAYMENTS/pay" -H "Authorization: Bearer $TXN" \
-H 'Content-Type: application/json' -d '{"invoice":"1042","amount":"120.00"}'
Passo 8: cosa contano i log#
Ogni servizio scrive una riga di log per decisione, con i claim che ha letto. Questa esecuzione ha emesso sei token. Payments ha registrato cinque richieste, una accettata e quattro rifiutate. Il ledger ne ha registrate cinque, due accettate e tre rifiutate. Alcune richieste sono andate direttamente al ledger, quindi il ledger ha registrato richieste che payments non ha mai visto.
cat run/payments.log run/ledger.log | jq -sc 'group_by([.role, .decision]) | map({role: .[0].role, decision: .[0].decision, count: length})'
Cosa questo non copre#
- L’issuer prende il nome del richiedente dal modulo della richiesta. Nessun utente accede, quindi alice è solo un’etichetta. Una distribuzione reale verifica l’accesso prima di emettere un token.
- Un unico segreto condiviso firma e controlla ogni token, quindi ogni servizio che lo possiede può anche emetterne uno. Le distribuzioni reali usano chiavi asimmetriche che l’issuer pubblica.
- I nomi dei claim (requester, actor, purpose, txn) sono quelli di questo lab. Confrontali con la bozza IETF (Internet Engineering Task Force) prima di riusarli in un sistema reale.
- Payments inoltra lo stesso token al ledger. Scambiare il token a ogni salto è un disegno diverso, e questo lab non lo prova.
- Nessuna piattaforma di agenti gira qui. Il lab prova i controlli nei servizi, non un agente.
Pulizia#
Ferma i servizi, elimina la chiave e la copia del file del servizio, e controlla che non resti nessun processo né listener.
pkill -f chain_service.py
rm -f key.hex chain_service.py run/*.log run/*.out run/pids && rm -rf __pycache__
pgrep -fl chain_service.py || echo "no chain_service.py process left"
lsof -nP -iTCP:4701-4703 -sTCP:LISTEN || echo "no listener on 4701-4703"
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:4701/healthz
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:4702/healthz
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:4703/healthz
find . -name key.hex -o -name '*.jwt' -o -name chain_service.py -o -name '*.log' | grep -v /raw/ || echo none
L’esecuzione è avvenuta il 30 novembre 2026, su un Mac, con i servizi solo su localhost. Data, orari e valori txn cambiano a ogni esecuzione.
Ripeti questo lab#
Ti servono Python 3 (senza pacchetti extra), curl, jq e openssl. Tutto gira sul tuo computer sulle porte da 4701 a 4703, senza account e senza accesso a internet dopo il download. Prevedi circa 10 minuti.
Ogni valore tra parentesi angolari è un segnaposto per il tuo ambiente di test. Nulla in questa pagina è un identificativo o un segreto reale.
Imposta le variabili una sola volta
export ISSUER=http://127.0.0.1:4701 PAYMENTS=http://127.0.0.1:4702 LEDGER=http://127.0.0.1:4703 0 di 10 passi fatti
Passo 1Scarica il servizio e crea una chiave
Obiettivo. Crea una cartella di lavoro, scarica l'unico file del servizio e crea una chiave nuova di 32 byte che solo tu puoi leggere.
Prerequisiti
- Python 3, curl, jq e openssl
mkdir -p txn-lab && cd txn-lab
curl -s -O https://iamops-dev.github.io/lab-files/transaction-token-call-chain-lab/chain_service.py
mkdir -p run
openssl rand -hex 32 > key.hex && chmod 600 key.hex Output atteso
(no output) Passo 2Avvia ledger, payments e issuer
Obiettivo. Avvia i tre servizi su 127.0.0.1. Payments chiama il ledger, quindi il ledger parte per primo.
Prerequisiti
- Il passo di preparazione è completato
- Le porte 4701, 4702 e 4703 sono libere
python3 -I chain_service.py --role ledger --port 4703 --key-file key.hex --log run/ledger.log &
python3 -I chain_service.py --role payments --port 4702 --key-file key.hex --downstream "$LEDGER" --log run/payments.log &
python3 -I chain_service.py --role issuer --port 4701 --key-file key.hex --log run/issuer.log &
sleep 1 Output atteso
ledger listening on 127.0.0.1:4703
payments listening on 127.0.0.1:4702
issuer listening on 127.0.0.1:4701 Passo 3Controlla i tre endpoint di stato
Obiettivo. Verifica che ogni servizio risponda prima di inviare qualsiasi token.
Prerequisiti
- Il passo di avvio è completato
curl -s "$ISSUER/healthz" | jq -c .
curl -s "$PAYMENTS/healthz" | jq -c .
curl -s "$LEDGER/healthz" | jq -c . Output atteso
{"role":"issuer","ok":true}
{"role":"payments","ok":true}
{"role":"ledger","ok":true} Passo 4Un token di servizio semplice si ferma al ledger
Obiettivo. Invia il token dell'agente a payments. Payments supera i propri controlli, il ledger rifiuta perché nel token non c'è nessun utente.
Prerequisiti
- Il controllo di stato è completato
PLAIN=$(curl -s -X POST "$ISSUER/token/service" -d 'sub=agent-1' | jq -r .token)
echo "$PLAIN" | jq -Rr 'split(".") | .[1] | gsub("-";"+") | gsub("_";"/") | . + "==" | @base64d | fromjson'
curl -s -w '\nHTTP %{http_code}\n' -X POST "$PAYMENTS/pay" -H "Authorization: Bearer $PLAIN" -H 'Content-Type: application/json' -d '{"invoice":"1042","amount":"120.00"}'
tail -n 1 run/payments.log | jq -c '{role, decision, status, actor, requester, own_checks, downstream_status}'
tail -n 1 run/ledger.log | jq -c '{role, decision, status, actor, requester, reason}' Output atteso
{
"aud": [
"payments",
"ledger"
],
"exp": 1796072679,
"iat": 1796072379,
"iss": "chain-lab-issuer",
"sub": "agent-1"
}
{
"error": "no_requester",
"reason": "token has no requester claim, so no user is recorded"
}
HTTP 403
{"role":"payments","decision":"reject","status":403,"actor":"agent-1","requester":null,"own_checks":"pass","downstream_status":403}
{"role":"ledger","decision":"reject","status":403,"actor":"agent-1","requester":null,"reason":"token has no requester claim, so no user is recorded"} Passo 5Un token di transazione arriva al ledger tramite payments
Obiettivo. Emetti un token che indica alice come richiedente e include la finalità, poi invialo a payments. Payments passa lo stesso token al ledger.
Prerequisiti
- Il controllo di stato è completato
TXN=$(curl -s -X POST "$ISSUER/token/transaction" -d 'requester=alice' -d 'actor=agent-1' -d 'purpose=pay_invoice' -d 'invoice=1042' -d 'amount=120.00' | jq -r .token)
echo "$TXN" | jq -Rr 'split(".") | .[1] | gsub("-";"+") | gsub("_";"/") | . + "==" | @base64d | fromjson'
curl -s -w '\nHTTP %{http_code}\n' -X POST "$PAYMENTS/pay" -H "Authorization: Bearer $TXN" -H 'Content-Type: application/json' -d '{"invoice":"1042","amount":"120.00"}'
tail -n 1 run/payments.log | jq -c '{role, decision, status, actor, requester, purpose, txn, own_checks, downstream_status}'
tail -n 1 run/ledger.log | jq -c '{role, decision, status, actor, requester, purpose, txn, aud: .claims.aud, result: .decision}' Output atteso
{
"actor": "agent-1",
"amount": "120.00",
"aud": [
"payments",
"ledger"
],
"exp": 1796072681,
"iat": 1796072381,
"invoice": "1042",
"iss": "chain-lab-issuer",
"purpose": "pay_invoice",
"requester": "alice",
"txn": "1c68d6b87787107f"
}
{
"result": "paid",
"actor": "agent-1",
"ledger": {
"result": "recorded",
"requester": "alice",
"invoice": "1042"
}
}
HTTP 200
{"role":"payments","decision":"accept","status":200,"actor":"agent-1","requester":"alice","purpose":"pay_invoice","txn":"1c68d6b87787107f","own_checks":"pass","downstream_status":201}
{"role":"ledger","decision":"accept","status":201,"actor":"agent-1","requester":"alice","purpose":"pay_invoice","txn":"1c68d6b87787107f","aud":["payments","ledger"],"result":"accept"} Passo 6Senza il claim purpose
Obiettivo. Invia un token dello stesso tipo senza purpose. Payments lo rifiuta. Il ledger, chiamato direttamente, lo accetta.
Prerequisiti
- Il controllo di stato è completato
TXN=$(curl -s -X POST "$ISSUER/token/transaction" -d 'requester=alice' -d 'actor=agent-1' -d 'invoice=1042' -d 'amount=120.00' | jq -r .token)
curl -s -w '\nHTTP %{http_code}\n' -X POST "$PAYMENTS/pay" -H "Authorization: Bearer $TXN" -H 'Content-Type: application/json' -d '{"invoice":"1042","amount":"120.00"}'
curl -s -w '\nHTTP %{http_code}\n' -X POST "$LEDGER/entries" -H "Authorization: Bearer $TXN" -H 'Content-Type: application/json' -d '{"invoice":"1042","amount":"120.00"}' Output atteso
{
"error": "insufficient_purpose",
"reason": "purpose claim missing"
}
HTTP 403
{
"result": "recorded",
"requester": "alice",
"invoice": "1042"
}
HTTP 201 Passo 7Un importo modificato viene rifiutato
Obiettivo. Tieni il token a 120.00 e invia 1200.00 nel corpo. Entrambi i servizi lo rifiutano.
Prerequisiti
- Il controllo di stato è completato
TXN=$(curl -s -X POST "$ISSUER/token/transaction" -d 'requester=alice' -d 'actor=agent-1' -d 'purpose=pay_invoice' -d 'invoice=1042' -d 'amount=120.00' | jq -r .token)
curl -s -w '\nHTTP %{http_code}\n' -X POST "$PAYMENTS/pay" -H "Authorization: Bearer $TXN" -H 'Content-Type: application/json' -d '{"invoice":"1042","amount":"1200.00"}'
curl -s -w '\nHTTP %{http_code}\n' -X POST "$LEDGER/entries" -H "Authorization: Bearer $TXN" -H 'Content-Type: application/json' -d '{"invoice":"1042","amount":"1200.00"}' Output atteso
{
"error": "request_mismatch",
"reason": "invoice or amount differs from the token"
}
HTTP 403
{
"error": "request_mismatch",
"reason": "invoice or amount differs from the token"
}
HTTP 403 Passo 8Un token solo per payments viene rifiutato dal ledger
Obiettivo. Emetti un token con audience impostata su payments, poi invialo al ledger.
Prerequisiti
- Il controllo di stato è completato
TXN=$(curl -s -X POST "$ISSUER/token/transaction" -d 'requester=alice' -d 'actor=agent-1' -d 'purpose=pay_invoice' -d 'invoice=1042' -d 'amount=120.00' -d 'aud=payments' | jq -r .token)
curl -s -w '\nHTTP %{http_code}\n' -X POST "$LEDGER/entries" -H "Authorization: Bearer $TXN" -H 'Content-Type: application/json' -d '{"invoice":"1042","amount":"120.00"}' Output atteso
{
"error": "invalid_token",
"reason": "token audience is payments, not ledger"
}
HTTP 401 Passo 9Un token da un secondo viene rifiutato dopo due secondi
Obiettivo. Emetti un token che dura un secondo, attendi due secondi, poi invialo a payments.
Prerequisiti
- Il controllo di stato è completato
TXN=$(curl -s -X POST "$ISSUER/token/transaction" -d 'requester=alice' -d 'actor=agent-1' -d 'purpose=pay_invoice' -d 'invoice=1042' -d 'amount=120.00' -d 'ttl=1' | jq -r .token)
sleep 2
curl -s -w '\nHTTP %{http_code}\n' -X POST "$PAYMENTS/pay" -H "Authorization: Bearer $TXN" -H 'Content-Type: application/json' -d '{"invoice":"1042","amount":"120.00"}' Output atteso
{
"error": "invalid_token",
"reason": "token expired"
}
HTTP 401 Passo 10Pulizia
Obiettivo. Ferma i tre servizi, elimina la cartella di lavoro e verifica che nessuno ascolti sulle tre porte.
Prerequisiti
- I passi precedenti sono completati
pkill -f chain_service.py
cd .. && rm -rf txn-lab
lsof -nP -iTCP:4701-4703 -sTCP:LISTEN || echo "no listener on 4701-4703"
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:4701/healthz Output atteso
no listener on 4701-4703
000 Fonti#
Questo articolo in formato Markdown · RSS · JSON feed
Nuovi articoli via email
Una email a settimana con nuovi articoli e lab. Disiscrizione in qualsiasi momento.
Il tuo indirizzo è salvato su Kit, il servizio che invia le email. Dettagli