Chapter DFeature flags labPage 7 of 8

Feature flags lab

Constrain access and rehearse rollback

Build a FastAPI RAG endpoint whose new index can be dark-launched, exposed to 5%, or disabled without a deploy as an operable release, not a slide-deck example.

~14 minSecurity and rollback

1Try it yourself

Playground

Feature flag lab

Flags control who sees what — without redeploying code.

New RAG index for 5% of traffic

Before you start

Why this matters

Before changing code, write the single production outcome this chapter must prove and the signal that would stop you. For this lab, the service boundary is FlagContext(flag_key, user_id, tenant_id, environment) returning FlagDecision(enabled, variant, reason, config_version). Record one request identifier you can follow from ingress through the final decision. If you cannot name the owner of the stop decision, the rollout is not yet controlled.

The source lesson separates release from exposure and requires every trace to carry the evaluated variant. This chapter turns that compact lesson into implementation evidence. The running scenario is a FastAPI RAG endpoint whose new index can be dark-launched, exposed to 5%, or disabled without a deploy. You will keep the same scenario across all eight chapters so setup decisions, tests, telemetry, and rollback controls accumulate into one coherent system rather than eight disconnected exercises.

2Learn the idea

Read

Draw the trust boundaries

Mark user input, application runtime, configuration service, external provider, durable store, and operator console. At each crossing, specify authentication, authorization, validation, encryption, and audit evidence. The system’s concrete policy is: restrict flag writes with RBAC, require review for production changes, sign audit records, and never treat a client-side flag as authorization. Test that policy with a negative request, not only a configuration screenshot.

Secrets enter at runtime through a secrets manager or workload identity. They never appear in source, fixtures, process arguments, telemetry, or exception bodies. Scope credentials to the smallest resource and operation. Production configuration writes require role-based access, review for high-risk changes, and an immutable record with actor and reason.

Read

Defend the data path

Set body and field-size limits before parsing expensive input. Normalize only after signature or identity checks where raw bytes are security-relevant. Treat retrieved documents, webhook payloads, and tool responses as untrusted data. Apply output schemas and explicit allowlists before any side effect. For customer data, minimize collection, define retention, and verify deletion reaches logs or indexes where promised.

Build abuse tests for replay, cross-tenant identifiers, prompt injection, forged configuration, stale credentials, and denial-of-service through expensive requests. Assert both response and absence of side effects. A 403 that still performed a provider call is a security and cost bug.

Read

Rehearse rollback safely

Execute: activate the server-side kill switch to serve control, verify decision logs, then revert the configuration revision. Use a two-person checklist for production-like drills: announce start, snapshot current revision and metrics, apply the control, verify safe behavior, reconcile in-flight work, and announce completion. Ensure rollback permissions are available to on-call staff without granting broad routine admin access.

Distinguish rollback from evidence destruction. Keep the failed artifact, configuration digest, audit events, and redacted traces under retention policy. If compromise is possible, rotate credentials and isolate the revision before analysis. Never reactivate a known-compromised secret merely because it restores service.

Read

Operational handoff

The runbook must name owner, escalation path, safe-mode behavior, exact control, verification query, and conditions for re-enabling. Run it with someone who did not write the code. Measure completion time and note any undocumented access. The chapter passes only when least privilege, negative tests, audited changes, and a timed rollback drill all produce evidence.

Read

Test least privilege and denial

Separate runtime and operator permissions. The application receives one capability, while the operator role owns the audited recovery action. Security invariant: Client flag values never grant access to the v2 index.

roles:
  runtime:
    principal: flag-release-manager
    allow:
      - flags:write-production
    deny:
      - admin:*
  break_glass_operator:
    allow:
      - flags:write-production
    conditions:
      reason_required: true
      approval_count: 2
      ttl_minutes: 30

Run a negative probe before the positive path. The exact status may be 401 or 403 by contract, but no downstream call or durable mutation may occur. Audit output names the decision, never credentials or payload contents.

$ curl -si -H 'Authorization: Bearer invalid-fixture' http://localhost:8000/answer
HTTP/1.1 401 Unauthorized
$ printf '%s\n' '{"decision":"deny","principal":"unknown","side_effects":0}'
{"decision":"deny","principal":"unknown","side_effects":0}

If side_effects is nonzero, move authorization ahead of retrieval, queue publication, or provider calls; changing only the status code is not a fix. If runtime unexpectedly has admin rights, inspect role inheritance and workload identity binding. Record actor, reason, approvals, prior flag-config-184, and expiry during rollback. A break-glass token still valid after the drill is a failed security test.

Checking tutor…

Continue learning · glossary & guides