Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ jobs:
run: |
set -euo pipefail
head=$(git rev-parse --short=12 HEAD)
services=$(python scripts/deploy_plan.py "$head" < running.json | tr '\n' ' ')
# xargs joins the lines and trims: "nothing to deploy" must be truly empty, not a space.
services=$(python scripts/deploy_plan.py "$head" < running.json | xargs)
echo "head=$head" >> "$GITHUB_OUTPUT"
echo "services=$services" >> "$GITHUB_OUTPUT"
echo "Deploying commit $head: ${services:-nothing changed}"
Expand Down
5 changes: 4 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ history, monolith dropped). The full agreed plan is in [`docs/PLAN.md`](docs/PLA
- Name prefix: `ms12p` (parent uses `flaskms`). Teardown deletes the whole resource group, so a
shared name would delete the other project.

## Current state (M3b: all 12 patterns live locally; Azure run is #11)
## Current state (complete: 12 of 12 live; one Azure run done and torn down, #11)
- Folders: `services/{user,catalog,order,payment,inventory}/` (Flask, one DB each), `gateway/`
(FastAPI), `registry/` (FastAPI), `shared/` (`shop_common`), `contracts/`, `frontend/`, `infra/`,
`scripts/`, `tests/`.
Expand Down Expand Up @@ -86,6 +86,9 @@ API, reused 5 min then revalidated by ETag (304s are free), last good answer kep
- `make run` sets `REGISTRY_URL`, `REDIS_URL`, `CHAOS_ENABLED=1`, `INSTANCE_ID`, and restarts a stopped
service after 20 s. Inventory seeds stock for all 24 catalog products; `POST /api/inventory/<id>/restock`
(chaos only) keeps smoke runs from running out. Registry TTL 10 s, heartbeat 2 s. Azure uses Container Apps DNS (no registry yet).
- Azure run gotchas: GitHub's OIDC subject may be `repo:<owner>@<id>/<repo>@<id>:ref:...`, so the
identity trusts both forms; `make verify` can time out while an empty Container Apps environment
is "ScheduledForDelete" (no cost) - re-run it.
- Gotchas solved: FastAPI tracing middleware must be added AFTER the request-ID middleware; Flask-
SQLAlchemy's engine is instrumented per engine; Pact's `Verifier` needs `host="127.0.0.1"`; Pact state
handlers run on another thread, so end the test's DB transaction first or SQLite deadlocks; two
Expand Down
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ A small Contoso e-commerce system that shows **12 microservices patterns working
Each pattern gets its own page with an explanation, when *not* to use it, and a live demo you
can click.

> **Status: M3b, delivery.** All 12 patterns are live locally, each with a working demo. The
> first real Azure run is the last step (issue #11). The foundation is copied from
> **Status: complete.** All 12 patterns are live, each with a working demo, locally and in one
> real Azure run (issue #11): provision, Deploy through GitHub Actions with OIDC, smoke tests,
> screenshots, teardown. The foundation is copied from
> [Azure-Ecommerce-Monolith-Microservices-Migration](https://github.com/MAOFILHO/Azure-Ecommerce-Monolith-Microservices-Migration).
> See [docs/PLAN.md](docs/PLAN.md) and the [M3b issues](https://github.com/MAOFILHO/Microservices-12-Patterns/issues).

Expand Down Expand Up @@ -152,8 +153,25 @@ Azure Container Apps (one app per service plus the gateway, all scale-to-zero; R
always-on internal bus) + MySQL Flexible Server. About $0.21 for an 8-hour session; see
[cost_estimate.md](cost_estimate.md). In Azure, discovery uses Container Apps DNS (no registry), and
the demo switches stay off. Redis is reachable only from apps inside the environment and has no
password, which is fine for a demo where every app is ours. Nothing has been deployed yet: the first
Azure run is issue #11.
password, which is fine for a demo where every app is ours.

### The Azure run (#11)
`make provision` built 6 images in the cloud (67-69 MB each, tagged with the commit), deployed them
plus Redis, and created the $25 budget alert. The Azure smoke tests
(`tests/smoke/test_post_provision_azure.py`) passed 4 of 4: a sale settled through Redis, demo
switches answered 403, every container ran as uid 10001 on the deployed commit, and the CI-CD page
saw every service on main. The Deploy button then signed in with OIDC and correctly rebuilt
nothing. Everything was removed with `make teardown` the same evening.

<img width="100%" alt="Containerization page in Azure: 6 containers, uid 10001, linux/x86_64, built from one commit, 67-69 MB" src="docs/screenshots/azure-p09-containerization.jpg" />

<img width="100%" alt="CI-CD page in Azure: every service on the latest commit of main" src="docs/screenshots/azure-p03-ci-cd.jpg" />

<img width="100%" alt="Event-Driven page in Azure: 3 events published within a second; inventory sold the reservation" src="docs/screenshots/azure-p07-event-driven.jpg" />

Lessons from the run: GitHub now presents an OIDC subject with account and repo ids, so the
identity trusts both forms (PR #14); `make verify` can time out while Azure removes an empty
Container Apps environment, which takes longer than everything else and bills nothing.

```bash
make github-identity # once: the Azure identity the Deploy workflow uses (OIDC, no secret)
Expand Down
Binary file added docs/screenshots/azure-p03-ci-cd.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/azure-p07-event-driven.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/azure-p09-containerization.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 25 additions & 13 deletions infra/github_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

Creates, only where missing:
1. an app registration named APP_NAME, and its service principal;
2. a federated credential (OIDC) trusting only this repo's main branch, so GitHub signs in with a
short-lived token and no client secret exists anywhere;
2. federated credentials (OIDC) trusting only this repo's main branch, in both subject forms
GitHub uses, so it signs in with a short-lived token and no client secret exists anywhere;
3. the Contributor role on the project resource group only, never the subscription.

Then stores the three ids (client, tenant, subscription) as GitHub Actions secrets. They are not
Expand Down Expand Up @@ -36,14 +36,22 @@
GITHUB_SECRETS = ["AZURE_CLIENT_ID", "AZURE_TENANT_ID", "AZURE_SUBSCRIPTION_ID"]


def federated_credential(repo: str) -> dict:
return {
"name": CREDENTIAL_NAME,
"issuer": ISSUER,
"subject": f"repo:{repo}:ref:refs/heads/main",
"audiences": ["api://AzureADTokenExchange"],
"description": "GitHub Actions on main of this repo only (Deploy workflow).",
def federated_credentials(repo: str, owner_id: int, repo_id: int) -> list[dict]:
"""The subjects GitHub may present for a run on main of this repo: the name-only form, and the
form with account and repo ids that GitHub sends for newer repos (seen on the first real
Deploy run). The id form also survives a rename and rejects a re-created repo of the same name."""
owner, name = repo.split("/")
subjects = {
CREDENTIAL_NAME: f"repo:{repo}:ref:refs/heads/main",
f"{CREDENTIAL_NAME}-ids": f"repo:{owner}@{owner_id}/{name}@{repo_id}:ref:refs/heads/main",
}
return [{"name": cred_name, "issuer": ISSUER, "subject": subject, "audiences": ["api://AzureADTokenExchange"],
"description": "GitHub Actions on main of this repo only (Deploy workflow)."}
for cred_name, subject in subjects.items()]


def missing_credentials(wanted: list[dict], existing_names: list[str]) -> list[dict]:
return [cred for cred in wanted if cred["name"] not in existing_names]


def role_scope(subscription_id: str, resource_group: str) -> str:
Expand Down Expand Up @@ -115,12 +123,14 @@ def main() -> int:

client_id = app_id()
object_id = sp_object_id(client_id) if client_id else None
owner_id, repo_id = (int(v) for v in _run(["gh", "api", f"repos/{repo}", "--jq", ".owner.id, .id"]).split())
wanted = federated_credentials(repo, owner_id, repo_id)
secret_names = {item["name"] for item in json.loads(_run(["gh", "secret", "list", "--repo", repo, "--json", "name"]) or "[]")}
found = {
"app": client_id is not None,
"service_principal": object_id is not None,
"credential": bool(client_id and any(c["name"] == CREDENTIAL_NAME for c in
_az_json(["ad", "app", "federated-credential", "list", "--id", client_id]))),
"credential": bool(client_id) and not missing_credentials(
wanted, [c["name"] for c in _az_json(["ad", "app", "federated-credential", "list", "--id", client_id])]),
"resource_group": _run(["az", "group", "exists", "--name", RESOURCE_GROUP]) == "true",
"role": False,
"github_secrets": set(GITHUB_SECRETS) <= secret_names,
Expand All @@ -135,8 +145,10 @@ def main() -> int:
if "service_principal" in todo:
object_id = _az_json(["ad", "sp", "create", "--id", client_id])["id"]
if "credential" in todo:
_run(["az", "ad", "app", "federated-credential", "create", "--id", client_id,
"--parameters", json.dumps(federated_credential(repo))])
existing = [c["name"] for c in _az_json(["ad", "app", "federated-credential", "list", "--id", client_id]) or []]
for cred in missing_credentials(wanted, existing):
_run(["az", "ad", "app", "federated-credential", "create", "--id", client_id,
"--parameters", json.dumps(cred)])
if "role" in todo:
_run(["az", *role_assignment_args(object_id, scope)])
if not found["resource_group"]:
Expand Down
97 changes: 97 additions & 0 deletions tests/smoke/test_post_provision_azure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"""Post-provision smoke tests against Azure (issue #11). Run after `make provision`:

.venv/bin/python -m pytest tests/smoke/test_post_provision_azure.py -v

Reads the gateway address from infra/.state.json and skips when nothing is provisioned. Checks
what only a real Azure run can prove: Redis inside the environment carries events, the signing
key secret works, containers run as uid 10001 on the commit they were tagged with, demo switches
are off, and the CI-CD page reads GitHub. Apps scale to zero, so the first calls wake them.
"""
import json
import time
import uuid
from pathlib import Path

import httpx
import pytest

TIMEOUT = httpx.Timeout(60.0)


@pytest.fixture(scope="module")
def azure():
state_file = Path(__file__).resolve().parents[2] / "infra" / ".state.json"
if not state_file.exists():
pytest.skip("nothing provisioned (no infra/.state.json)")
state = json.loads(state_file.read_text())
gateway = f"https://{state['outputs']['gatewayFqdn']}"
deadline = time.monotonic() + 180 # cold start of the gateway and every service it calls
while time.monotonic() < deadline:
try:
if httpx.get(f"{gateway}/health", timeout=TIMEOUT).status_code == 200:
break
except httpx.HTTPError:
pass
time.sleep(5)
return {"gateway": gateway, "tag": state["image_tag"]}


def _wait_for(condition, seconds=60.0):
deadline = time.monotonic() + seconds
while time.monotonic() < deadline:
if condition():
return True
time.sleep(2)
return False


def _login_with_cart(shop: str, qty: int = 1) -> dict:
tag = uuid.uuid4().hex[:8]
httpx.post(f"{shop}/user/create", timeout=TIMEOUT,
data={"username": f"az{tag}", "email": f"az{tag}@example.com", "password": "secret123"})
r = httpx.post(f"{shop}/user/login", data={"username": f"az{tag}", "password": "secret123"}, timeout=TIMEOUT)
assert r.status_code == 200, r.text
headers = {"Authorization": f"Bearer {r.json()['token']}"}
assert httpx.post(f"{shop}/order/add-item", data={"product_id": 1, "qty": qty}, headers=headers,
timeout=TIMEOUT).status_code == 200
return headers


def _mug(shop: str) -> dict:
return next(s for s in httpx.get(f"{shop}/inventory", timeout=TIMEOUT).json()["results"] if s["product_id"] == 1)


def test_every_process_runs_in_a_container_as_uid_10001_on_the_deployed_commit(azure):
processes = httpx.get(f"{azure['gateway']}/api/patterns/runtime", timeout=TIMEOUT).json()["processes"]
assert len(processes) == 6, processes # gateway + 5 services, one replica each in Azure
for p in processes:
assert p.get("in_container") is True and p.get("uid") == 10001 and p.get("root") is False, p
assert p.get("git_sha") == azure["tag"], p
assert p.get("arch") == "x86_64", p


def test_a_sale_settles_through_redis_inside_azure(azure):
shop = f"{azure['gateway']}/api/shop"
headers = _login_with_cart(shop, qty=1)
before = _mug(shop)
r = httpx.post(f"{shop}/order/checkout", headers=headers, json={}, timeout=TIMEOUT)
assert r.status_code == 200, r.text
# Order's outbox -> Redis Container App -> inventory's consumer turns the hold into a sale.
assert _wait_for(lambda: _mug(shop)["on_hand"] == before["on_hand"] - 1), _mug(shop)
assert _mug(shop)["reserved"] == before["reserved"]


def test_demo_switches_are_off_in_azure(azure):
shop = f"{azure['gateway']}/api/shop"
headers = _login_with_cart(shop)
assert httpx.post(f"{shop}/order/checkout", headers=headers, json={"simulate_decline": True},
timeout=TIMEOUT).status_code == 403
api = f"{azure['gateway']}/api/patterns"
assert httpx.post(f"{api}/breaker/payment-mode", json={"mode": "failing"}, timeout=TIMEOUT).status_code == 403
assert httpx.post(f"{api}/events/bus", json={"connected": False}, timeout=TIMEOUT).status_code == 403


def test_the_ci_cd_page_sees_every_service_on_main(azure):
body = httpx.get(f"{azure['gateway']}/api/patterns/delivery", timeout=TIMEOUT).json()
assert body["main"] is not None, "GitHub unreachable from Azure"
assert {s["status"] for s in body["services"]} <= {"current", "unchanged"}, body["services"]
22 changes: 18 additions & 4 deletions tests/unit/test_github_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@


def test_only_this_repos_main_branch_can_sign_in_as_it():
cred = gi.federated_credential("MAOFILHO/Microservices-12-Patterns")
assert cred["issuer"] == "https://token.actions.githubusercontent.com"
assert cred["subject"] == "repo:MAOFILHO/Microservices-12-Patterns:ref:refs/heads/main"
assert cred["audiences"] == ["api://AzureADTokenExchange"]
"""GitHub may send the name-only subject or the one with account and repo ids (newer repos;
seen on the first real Deploy run). Both are trusted, both for main of this repo only."""
creds = gi.federated_credentials("MAOFILHO/Microservices-12-Patterns", owner_id=69284062, repo_id=1458985608)
assert [c["subject"] for c in creds] == [
"repo:MAOFILHO/Microservices-12-Patterns:ref:refs/heads/main",
"repo:MAOFILHO@69284062/Microservices-12-Patterns@1458985608:ref:refs/heads/main",
]
assert len({c["name"] for c in creds}) == 2
for cred in creds:
assert cred["issuer"] == "https://token.actions.githubusercontent.com"
assert cred["audiences"] == ["api://AzureADTokenExchange"]


def test_a_missing_second_credential_is_added_on_the_next_run():
creds = gi.federated_credentials("o/r", owner_id=1, repo_id=2)
existing = [creds[0]["name"]]
assert gi.missing_credentials(creds, existing) == [creds[1]]
assert gi.missing_credentials(creds, [c["name"] for c in creds]) == []


def test_access_is_this_resource_group_only_never_the_subscription():
Expand Down
Loading