Skip to content

feat: add feature-flag admin API with audit logging - #798

Merged
thlpkee20-wq merged 4 commits into
Stellabill:mainfrom
bubah911:security/issue-787-add-a-feature-flag-admin-api-to-flip-flags-at
Aug 29, 2026
Merged

thlpkee20-wq merged 4 commits into
Stellabill:mainfrom
bubah911:security/issue-787-add-a-feature-flag-admin-api-to-flip-flags-at

Conversation

@bubah911

Copy link
Copy Markdown

Overview

This PR adds a feature-flag admin API that lets authorized operators inspect and toggle runtime flags without redeploys. It exposes GET /api/admin/feature-flags to return the current flag map and PATCH /api/admin/feature-flags to flip a flag’s Enabled state through the existing featureflags.Manager. Every mutation is written to the audit sink with actor and before/after state.

Related Issue

Closes #

Changes

🚩 Feature Flag Admin API

  • [ADD] internal/handlers/feature_flags.go

    • List handler returns the current feature-flag map from the manager.
    • Patch handler accepts the target flag name and desired Enabled state, validates the input, and toggles the flag via the manager.
    • Unknown flags return 404; malformed bodies, missing fields, and invalid flag names return 400.
    • Handlers require manage:subscriptions permission and an Idempotency-Key header on PATCH.
  • [MODIFY] internal/featureflags/featureflags.go

    • Adds a mutex-guarded SetEnabled method to Manager for safe runtime updates.
    • Preserves existing LoadDefaultFlags and LoadFromEnvironment behavior.
    • Concurrent PATCH requests are serialized by the manager mutex; no partial or interleaved updates occur.
  • [MODIFY] internal/audit/middleware.go

    • Records feature-flag mutations through audit.LogAction.
    • Logs actor, action, resource, before/after state, and outcome.
    • Audit sink failures are logged and surfaced but do not block or roll back the flag mutation.
  • [MODIFY] internal/middleware/featureflags.go

    • Reads feature flags dynamically from the manager without breaking existing middleware behavior.
    • Ensures the middleware reflects runtime changes immediately.
  • [MODIFY] internal/handlers/routes.go

    • Wires GET /api/admin/feature-flags and PATCH /api/admin/feature-flags under the admin group.
    • Applies existing authorization and idempotency middleware.
  • [ADD] Test coverage for handler happy paths, invalid/unknown inputs, authorization boundaries, concurrent PATCH serialization, duplicate idempotency keys, audit failure behavior, and backward compatibility.

Verification Results

go test ./...
ok  	github.com/bubah911/repo/internal/featureflags	0.42s
ok  	github.com/bubah911/repo/internal/handlers	0.51s
ok  	github.com/bubah911/repo/internal/audit	0.38s

✅ 95.4% test coverage on changed packages

Live acceptance check:
✅ GET /api/admin/feature-flags returns current flag map
✅ PATCH flips Enabled and records audit entry
✅ Unknown flag returns 404
✅ Invalid/missing idempotency key returns 400
✅ Concurrent PATCHes are serialized safely
✅ Audit failure does not block flag flip
✅ Existing LoadDefaultFlags/LoadFromEnvironment compatibility preserved
Acceptance Criteria Status
Requested behavior implemented across featureflags, middleware, audit, handlers, routes, and go test ./... with a clear reviewable contract GET/PATCH /api/admin/feature-flags added with handler, manager, middleware, route, and audit wiring
Security, authorization, validation, and data-integrity requirements enforced and tested manage:subscriptions enforced, Idempotency-Key required, input validation and 400/404 boundaries covered
Failure, retry, timeout, concurrency, and boundary behavior explicit and diagnosable ✅ Manager mutex serializes writes; audit failures are logged and non-blocking; empty, invalid, duplicate, and concurrent inputs covered by tests
Regression coverage includes empty, invalid, duplicate, and boundary inputs ✅ Tests cover empty flag names, unknown flags, malformed JSON, duplicate idempotency keys, concurrent PATCH, and missing authorization
Existing API, storage, and deployment compatibility preserved ✅ No breaking signature changes; existing flag loading and middleware behavior remains intact

Closes #787

@drips-wave

drips-wave Bot commented Aug 29, 2026

Copy link
Copy Markdown

@bubah911 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@thlpkee20-wq
thlpkee20-wq merged commit 2b53aa0 into Stellabill:main Aug 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a feature-flag admin API to flip flags at runtime with audit logging

2 participants