Compare commits
37 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9657f18784 | |||
| d4e8ef90ee | |||
| d6cc82755d | |||
| 0d38bbae00 | |||
| 23a27be57e | |||
| b1b52000c4 | |||
| d87b4b4042 | |||
| 5c242f8abb | |||
| 1121b7d4fd | |||
| 2a85d4bf2b | |||
| 8f09e0e81a | |||
| 7e5fc9f111 | |||
| cf02b8e2d8 | |||
| fe780bf873 | |||
| cb96650e59 | |||
| bef5a3269d | |||
| 6191c5e00a | |||
| fa45836612 | |||
| 0bc7943d8f | |||
| 97ba7ab74c | |||
| 582645f9d6 | |||
| 8840317cce | |||
| 482300b8b1 | |||
| 8545473703 | |||
| 3b17f7f70b | |||
| eef7d1bc1a | |||
| d7d7ba21d9 | |||
| 4d3d438253 | |||
| 7cc2c86300 | |||
| 51a5277062 | |||
| d3b63b1f87 | |||
| 70e4f715f7 | |||
| 3f09d5eb4f | |||
| 036a0b8be4 | |||
| 30fc3470fa | |||
| 586d4e3355 | |||
| 394a9fb754 |
@@ -11,7 +11,8 @@
|
||||
"Bash(sqlite3 /Users/kyle/src/mcias/run/mcias.db \"PRAGMA table_info\\(policy_rules\\);\" 2>&1)",
|
||||
"Bash(sqlite3 /Users/kyle/src/mcias/run/mcias.db \"SELECT * FROM schema_version;\" 2>&1; sqlite3 /Users/kyle/src/mcias/run/mcias.db \"SELECT * FROM schema_migrations;\" 2>&1)",
|
||||
"Bash(go run:*)",
|
||||
"Bash(go list:*)"
|
||||
"Bash(go list:*)",
|
||||
"Bash(go vet:*)"
|
||||
]
|
||||
},
|
||||
"hooks": {
|
||||
|
||||
BIN
.claude/skills/checkpoint.zip
Normal file
BIN
.claude/skills/checkpoint.zip
Normal file
Binary file not shown.
134
ARCHITECTURE.md
134
ARCHITECTURE.md
@@ -15,36 +15,46 @@ parties that delegate authentication decisions to it.
|
||||
### Components
|
||||
|
||||
```
|
||||
┌────────────────────────────────────────────────────┐
|
||||
│ MCIAS Server (mciassrv) │
|
||||
│ ┌──────────┐ ┌──────────┐ ┌───────────────────┐ │
|
||||
│ │ Auth │ │ Token │ │ Account / Role │ │
|
||||
│ │ Handler │ │ Manager │ │ Manager │ │
|
||||
│ └────┬─────┘ └────┬─────┘ └─────────┬─────────┘ │
|
||||
│ └─────────────┴─────────────────┘ │
|
||||
│ │ │
|
||||
│ ┌─────────▼──────────┐ │
|
||||
│ │ SQLite Database │ │
|
||||
│ └────────────────────┘ │
|
||||
└────────────────────────────────────────────────────┘
|
||||
▲ ▲ ▲
|
||||
│ HTTPS/REST │ HTTPS/REST │ direct file I/O
|
||||
│ │ │
|
||||
┌──────┴──────┐ ┌────┴─────┐ ┌──────┴──────┐
|
||||
│ Personal │ │ mciasctl │ │ mciasdb │
|
||||
│ Apps │ │ (admin │ │ (DB tool) │
|
||||
└─────────────┘ │ CLI) │ └─────────────┘
|
||||
└──────────┘
|
||||
┌──────────────────────────────────────────────────────────┐
|
||||
│ MCIAS Server (mciassrv) │
|
||||
│ ┌──────────┐ ┌──────────┐ ┌───────────────────┐ │
|
||||
│ │ Auth │ │ Token │ │ Account / Role │ │
|
||||
│ │ Handler │ │ Manager │ │ Manager │ │
|
||||
│ └────┬─────┘ └────┬─────┘ └─────────┬─────────┘ │
|
||||
│ └─────────────┴─────────────────┘ │
|
||||
│ │ │
|
||||
│ ┌─────────▼──────────┐ │
|
||||
│ │ SQLite Database │ │
|
||||
│ └────────────────────┘ │
|
||||
│ │
|
||||
│ ┌──────────────────┐ ┌──────────────────────┐ │
|
||||
│ │ REST listener │ │ gRPC listener │ │
|
||||
│ │ (net/http) │ │ (google.golang.org/ │ │
|
||||
│ │ :8443 │ │ grpc) :9443 │ │
|
||||
│ └──────────────────┘ └──────────────────────┘ │
|
||||
└──────────────────────────────────────────────────────────┘
|
||||
▲ ▲ ▲ ▲
|
||||
│ HTTPS/REST │ HTTPS/REST │ gRPC/TLS │ direct file I/O
|
||||
│ │ │ │
|
||||
┌────┴──────┐ ┌────┴─────┐ ┌─────┴────────┐ ┌───┴────────┐
|
||||
│ Personal │ │ mciasctl │ │ mciasgrpcctl │ │ mciasdb │
|
||||
│ Apps │ │ (admin │ │ (gRPC admin │ │ (DB tool) │
|
||||
└───────────┘ │ CLI) │ │ CLI) │ └────────────┘
|
||||
└──────────┘ └──────────────┘
|
||||
```
|
||||
|
||||
**mciassrv** — The authentication server. Exposes a REST API over HTTPS/TLS.
|
||||
Handles login, token issuance, token validation, token renewal, and token
|
||||
revocation.
|
||||
**mciassrv** — The authentication server. Exposes a REST API and gRPC API over
|
||||
HTTPS/TLS (dual-stack; see §17). Handles login, token issuance, token
|
||||
validation, token renewal, and token revocation.
|
||||
|
||||
**mciasctl** — The administrator CLI. Communicates with mciassrv's REST API
|
||||
using an admin JWT. Creates/manages human accounts, system accounts, roles,
|
||||
and Postgres credential records.
|
||||
|
||||
**mciasgrpcctl** — The gRPC administrator CLI. Mirrors mciasctl's subcommands
|
||||
but communicates over gRPC/TLS instead of REST. Both CLIs can coexist; neither
|
||||
depends on the other.
|
||||
|
||||
**mciasdb** — The database maintenance tool. Operates directly on the SQLite
|
||||
file, bypassing the server API. Intended for break-glass recovery, offline
|
||||
inspection, schema verification, and maintenance tasks that cannot be
|
||||
@@ -127,13 +137,21 @@ mciassrv (passphrase or keyfile) to decrypt secrets at rest.
|
||||
|
||||
### Roles
|
||||
|
||||
Roles are simple string labels stored in the `account_roles` table.
|
||||
Roles are simple string labels stored in the `account_roles` table. Only
|
||||
compile-time allowlisted role names are accepted; attempting to grant an
|
||||
unknown role returns an error (prevents typos like "admim" from silently
|
||||
creating a useless role).
|
||||
|
||||
Reserved roles:
|
||||
Compile-time allowlisted roles:
|
||||
- `admin` — superuser; can manage all accounts, tokens, and credentials
|
||||
- `user` — standard user role
|
||||
- `guest` — limited read-only access
|
||||
- `viewer` — read-only access
|
||||
- `editor` — create/modify access
|
||||
- `commenter` — comment/annotate access
|
||||
- Any role named identically to a system account — grants that human account
|
||||
the ability to issue/revoke tokens and retrieve Postgres credentials for that
|
||||
system account
|
||||
system account (via policy rules, not the allowlist)
|
||||
|
||||
Role assignment requires admin privileges.
|
||||
|
||||
@@ -340,7 +358,6 @@ All endpoints use JSON request/response bodies. All responses include a
|
||||
| POST | `/v1/auth/login` | none | Username/password (+TOTP) login → JWT |
|
||||
| POST | `/v1/auth/logout` | bearer JWT | Revoke current token |
|
||||
| POST | `/v1/auth/renew` | bearer JWT | Exchange token for new token |
|
||||
| PUT | `/v1/auth/password` | bearer JWT | Self-service password change (requires current password) |
|
||||
|
||||
### Token Endpoints
|
||||
|
||||
@@ -372,7 +389,9 @@ All endpoints use JSON request/response bodies. All responses include a
|
||||
| Method | Path | Auth required | Description |
|
||||
|---|---|---|---|
|
||||
| GET | `/v1/accounts/{id}/roles` | admin JWT | List roles for account |
|
||||
| PUT | `/v1/accounts/{id}/roles` | admin JWT | Replace role set |
|
||||
| PUT | `/v1/accounts/{id}/roles` | admin JWT | Replace role set (atomic) |
|
||||
| POST | `/v1/accounts/{id}/roles` | admin JWT | Grant a single role |
|
||||
| DELETE | `/v1/accounts/{id}/roles/{role}` | admin JWT | Revoke a single role |
|
||||
|
||||
### TOTP Endpoints
|
||||
|
||||
@@ -412,11 +431,23 @@ All endpoints use JSON request/response bodies. All responses include a
|
||||
|---|---|---|---|
|
||||
| GET | `/v1/audit` | admin JWT | List audit log events |
|
||||
|
||||
### Vault Endpoints
|
||||
|
||||
| Method | Path | Auth required | Description |
|
||||
|---|---|---|---|
|
||||
| GET | `/v1/vault/status` | none | Returns `{"sealed": bool}`; always accessible |
|
||||
| POST | `/v1/vault/unseal` | none | Accept passphrase, derive key, unseal (rate-limited 3/s burst 5) |
|
||||
| POST | `/v1/vault/seal` | admin JWT | Zero key material and seal the vault; invalidates all JWTs |
|
||||
|
||||
When the vault is sealed, all endpoints except health, vault status, and unseal
|
||||
return 503 with `{"error":"vault is sealed","code":"vault_sealed"}`. The UI
|
||||
redirects non-exempt paths to `/unseal`.
|
||||
|
||||
### Admin / Server Endpoints
|
||||
|
||||
| Method | Path | Auth required | Description |
|
||||
|---|---|---|---|
|
||||
| GET | `/v1/health` | none | Health check |
|
||||
| GET | `/v1/health` | none | Health check — returns `{"status":"ok"}` or `{"status":"sealed"}` |
|
||||
| GET | `/v1/keys/public` | none | Ed25519 public key (JWK format) |
|
||||
|
||||
### Web Management UI
|
||||
@@ -439,6 +470,7 @@ cookie pattern (`mcias_csrf`).
|
||||
|
||||
| Path | Description |
|
||||
|---|---|
|
||||
| `/unseal` | Passphrase form to unseal the vault; shown for all paths when sealed |
|
||||
| `/login` | Username/password login with optional TOTP step |
|
||||
| `/` | Dashboard (account summary) |
|
||||
| `/accounts` | Account list |
|
||||
@@ -446,6 +478,7 @@ cookie pattern (`mcias_csrf`).
|
||||
| `/pgcreds` | Postgres credentials list (owned + granted) with create form |
|
||||
| `/policies` | Policy rules management — create, enable/disable, delete |
|
||||
| `/audit` | Audit log viewer |
|
||||
| `/profile` | User profile — self-service password change (any authenticated user) |
|
||||
|
||||
**HTMX fragments:** Mutating operations (role updates, tag edits, credential
|
||||
saves, policy toggles, access grants) use HTMX partial-page updates for a
|
||||
@@ -490,6 +523,9 @@ CREATE TABLE accounts (
|
||||
-- AES-256-GCM encrypted TOTP secret; NULL if not enrolled
|
||||
totp_secret_enc BLOB,
|
||||
totp_secret_nonce BLOB,
|
||||
-- Last accepted TOTP counter value; prevents replay attacks within the
|
||||
-- ±1 time-step window (RFC 6238 §5.2). NULL = no code accepted yet.
|
||||
last_totp_counter INTEGER DEFAULT NULL,
|
||||
created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ','now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ','now')),
|
||||
deleted_at TEXT
|
||||
@@ -661,17 +697,20 @@ or a keyfile path — never inline in the config file.
|
||||
|
||||
```toml
|
||||
[server]
|
||||
listen_addr = "0.0.0.0:8443"
|
||||
grpc_addr = "0.0.0.0:9443" # optional; omit to disable gRPC
|
||||
tls_cert = "/etc/mcias/server.crt"
|
||||
tls_key = "/etc/mcias/server.key"
|
||||
listen_addr = "0.0.0.0:8443"
|
||||
grpc_addr = "0.0.0.0:9443" # optional; omit to disable gRPC
|
||||
tls_cert = "/etc/mcias/server.crt"
|
||||
tls_key = "/etc/mcias/server.key"
|
||||
# trusted_proxy = "127.0.0.1" # optional; IP of reverse proxy — when set,
|
||||
# X-Forwarded-For is trusted only from this IP
|
||||
# for rate limiting and audit log IP extraction
|
||||
|
||||
[database]
|
||||
path = "/var/lib/mcias/mcias.db"
|
||||
|
||||
[tokens]
|
||||
issuer = "https://auth.example.com"
|
||||
default_expiry = "720h" # 30 days
|
||||
default_expiry = "168h" # 7 days
|
||||
admin_expiry = "8h"
|
||||
service_expiry = "8760h" # 365 days
|
||||
|
||||
@@ -711,7 +750,8 @@ mcias/
|
||||
│ ├── policy/ # in-process authorization policy engine (§20)
|
||||
│ ├── server/ # HTTP handlers, router setup
|
||||
│ ├── token/ # JWT issuance, validation, revocation
|
||||
│ └── ui/ # web UI context, CSRF, session, template handlers
|
||||
│ ├── ui/ # web UI context, CSRF, session, template handlers
|
||||
│ └── validate/ # input validation helpers (username, password strength)
|
||||
├── web/
|
||||
│ ├── static/ # CSS and static assets
|
||||
│ └── templates/ # HTML templates (base layout, pages, HTMX fragments)
|
||||
@@ -761,12 +801,17 @@ The `cmd/` packages are thin wrappers that wire dependencies and call into
|
||||
| `totp_removed` | TOTP removed from account |
|
||||
| `pgcred_accessed` | Postgres credentials retrieved |
|
||||
| `pgcred_updated` | Postgres credentials stored/updated |
|
||||
| `pgcred_access_granted` | Read access to PG credentials granted to another account |
|
||||
| `pgcred_access_revoked` | Read access to PG credentials revoked from an account |
|
||||
| `password_changed` | Account password changed (self-service or admin reset) |
|
||||
| `tag_added` | Tag added to account |
|
||||
| `tag_removed` | Tag removed from account |
|
||||
| `policy_rule_created` | Policy rule created |
|
||||
| `policy_rule_updated` | Policy rule updated (priority, enabled, description) |
|
||||
| `policy_rule_deleted` | Policy rule deleted |
|
||||
| `policy_deny` | Policy engine denied a request (logged for every explicit deny) |
|
||||
| `vault_unsealed` | Vault unsealed via REST API or web UI; details include `source` (api\|ui) and `ip` |
|
||||
| `vault_sealed` | Vault sealed via REST API; details include actor ID, `source`, and `ip` |
|
||||
|
||||
---
|
||||
|
||||
@@ -838,6 +883,7 @@ mciasdb --config PATH <subcommand> [flags]
|
||||
|---|---|
|
||||
| `mciasdb schema verify` | Open DB, run migrations in dry-run mode, report version |
|
||||
| `mciasdb schema migrate` | Apply any pending migrations and exit |
|
||||
| `mciasdb schema force --version N` | Force schema version (clears dirty state); break-glass recovery |
|
||||
| `mciasdb prune tokens` | Delete expired rows from `token_revocation` and `system_tokens` |
|
||||
|
||||
**Account management (offline):**
|
||||
@@ -943,7 +989,7 @@ in `proto/generate.go` using `protoc-gen-go` and `protoc-gen-go-grpc`.
|
||||
|---|---|
|
||||
| `AuthService` | `Login`, `Logout`, `RenewToken`, `EnrollTOTP`, `ConfirmTOTP`, `RemoveTOTP` |
|
||||
| `TokenService` | `ValidateToken`, `IssueServiceToken`, `RevokeToken` |
|
||||
| `AccountService` | `ListAccounts`, `CreateAccount`, `GetAccount`, `UpdateAccount`, `DeleteAccount`, `GetRoles`, `SetRoles` |
|
||||
| `AccountService` | `ListAccounts`, `CreateAccount`, `GetAccount`, `UpdateAccount`, `DeleteAccount`, `GetRoles`, `SetRoles`, `GrantRole`, `RevokeRole` |
|
||||
| `CredentialService` | `GetPGCreds`, `SetPGCreds` |
|
||||
| `AdminService` | `Health`, `GetPublicKey` |
|
||||
|
||||
@@ -979,9 +1025,12 @@ details.
|
||||
### Interceptor Chain
|
||||
|
||||
```
|
||||
[Request Logger] → [Auth Interceptor] → [Rate Limiter] → [Handler]
|
||||
[Sealed Interceptor] → [Request Logger] → [Auth Interceptor] → [Rate Limiter] → [Handler]
|
||||
```
|
||||
|
||||
- **Sealed Interceptor**: first in chain; blocks all RPCs with
|
||||
`codes.Unavailable` ("vault sealed") when the vault is sealed, except
|
||||
`AdminService/Health` which returns the sealed status.
|
||||
- **Request Logger**: logs method, peer IP, status code, duration; never logs
|
||||
the `authorization` metadata value.
|
||||
- **Auth Interceptor**: validates Bearer JWT, injects claims. Public RPCs
|
||||
@@ -1374,9 +1423,10 @@ const (
|
||||
ActionReadAudit Action = "audit:read"
|
||||
ActionEnrollTOTP Action = "totp:enroll" // self-service
|
||||
ActionRemoveTOTP Action = "totp:remove" // admin
|
||||
ActionLogin Action = "auth:login" // public
|
||||
ActionLogout Action = "auth:logout" // self-service
|
||||
ActionListRules Action = "policy:list"
|
||||
ActionLogin Action = "auth:login" // public
|
||||
ActionLogout Action = "auth:logout" // self-service
|
||||
ActionChangePassword Action = "auth:change_password" // self-service
|
||||
ActionListRules Action = "policy:list"
|
||||
ActionManageRules Action = "policy:manage"
|
||||
|
||||
// Resource types
|
||||
@@ -1476,8 +1526,10 @@ at the same priority level.
|
||||
|
||||
```
|
||||
Priority 0, Allow: roles=[admin], actions=<all> — admin wildcard
|
||||
Priority 0, Allow: actions=[tokens:renew, auth:logout] — self-service logout/renew
|
||||
Priority 0, Allow: actions=[auth:logout, tokens:renew] — self-service logout/renew
|
||||
Priority 0, Allow: actions=[totp:enroll] — self-service TOTP enrollment
|
||||
Priority 0, Allow: accountTypes=[human], actions=[auth:change_password]
|
||||
— self-service password change
|
||||
Priority 0, Allow: accountTypes=[system], actions=[pgcreds:read],
|
||||
resourceType=pgcreds, ownerMatchesSubject=true
|
||||
— system account reads own creds
|
||||
|
||||
359
AUDIT.md
359
AUDIT.md
@@ -1,202 +1,349 @@
|
||||
# MCIAS Security Audit Report
|
||||
|
||||
**Date:** 2026-03-12
|
||||
**Scope:** Full codebase — authentication flows, token lifecycle, cryptography, database layer, REST/gRPC/UI servers, authorization, and operational security.
|
||||
**Methodology:** Static code analysis of all source files with adversarial focus on auth flows, crypto usage, input handling, and inter-component trust boundaries.
|
||||
**Date:** 2026-03-14 (updated — penetration test round 4)
|
||||
**Original audit date:** 2026-03-13
|
||||
**Auditor role:** Penetration tester (code review + live instance probing)
|
||||
**Scope:** Full codebase and running instance at mcias.metacircular.net:8443 — authentication flows, token lifecycle, cryptography, database layer, REST/gRPC/UI servers, authorization, headers, and operational security.
|
||||
**Methodology:** Static code analysis, live HTTP probing, architectural review.
|
||||
|
||||
---
|
||||
|
||||
## Executive Summary
|
||||
|
||||
MCIAS demonstrates strong security awareness throughout. The cryptographic foundations are sound, credential handling is careful, and the most common web/API authentication vulnerabilities have been explicitly addressed. The codebase shows consistent attention to defense-in-depth: constant-time comparisons, dummy Argon2 operations for unknown users, algorithm-confusion prevention in JWT validation, parameterized SQL, audit logging, and CSRF protection with HMAC-signed double-submit.
|
||||
MCIAS has a strong security posture. All findings from the first three audit rounds (CRIT-01/CRIT-02, DEF-01 through DEF-10, and SEC-01 through SEC-12) have been remediated. The cryptographic foundations are sound, JWT validation is correct, SQL injection is not possible, XSS is prevented by Go's html/template auto-escaping, and CSRF protection is well-implemented.
|
||||
|
||||
**Two confirmed bugs with real security impact were found**, along with several defense-in-depth gaps that should be addressed before production deployment. The overall security posture is well above average for this class of system.
|
||||
A fourth-round penetration test (PEN-01 through PEN-07) against the live instance at `mcias.metacircular.net:8443` identified 7 new findings: 2 medium, 2 low, and 3 informational. **Unauthorized access was not achieved** — the system's defense-in-depth held. See the open findings table below for details.
|
||||
|
||||
---
|
||||
|
||||
## Confirmed Vulnerabilities
|
||||
## Open Findings (PEN-01 through PEN-07)
|
||||
|
||||
### CRIT-01 — TOTP Replay Attack (Medium-High)
|
||||
Identified during the fourth-round penetration test on 2026-03-14 against the live instance at `mcias.metacircular.net:8443` and the source code at the same commit.
|
||||
|
||||
**File:** `internal/auth/auth.go:208-230`, `internal/grpcserver/auth.go:84`, `internal/ui/handlers_auth.go:152`
|
||||
| ID | Severity | Finding | Status |
|
||||
|----|----------|---------|--------|
|
||||
| PEN-01 | Medium | `extractBearerFromRequest` does not validate "Bearer " prefix | **Fixed** — uses `strings.SplitN` + `strings.EqualFold` prefix validation, matching middleware implementation |
|
||||
| PEN-02 | Medium | Security headers missing from live instance responses | **Fixed** — redeployed; all headers confirmed present on live instance 2026-03-15 |
|
||||
| PEN-03 | Low | CSP `unsafe-inline` on `/docs` Swagger UI endpoint | **Accepted** — self-hosting Swagger UI (1.7 MB) to enable nonces adds complexity disproportionate to the risk; inline script is static, no user-controlled input |
|
||||
| PEN-04 | Info | OpenAPI spec publicly accessible without authentication | **Accepted** — intentional; public access required for agents and external developers |
|
||||
| PEN-05 | Info | gRPC port 9443 publicly accessible | **Accepted** — intentional; required for server-to-server access by external systems |
|
||||
| PEN-06 | Low | REST login increments lockout counter for missing TOTP code | **Fixed** — `RecordLoginFailure` removed from TOTP-missing branch; `TestTOTPMissingDoesNotIncrementLockout` added |
|
||||
| PEN-07 | Info | Rate limiter is per-IP only, no per-account limiting | **Accepted** — per-account hard lockout (10 failures/15 min) already covers distributed brute-force; per-account rate limiting adds marginal benefit at this scale |
|
||||
|
||||
`ValidateTOTP` accepts any code falling in the current ±1 time-step window (±30 seconds, so a given code is valid for ~90 seconds) but **never records which codes have already been used**. The same valid TOTP code can be submitted an unlimited number of times within that window. There is no `last_used_totp_counter` or `last_used_totp_at` field in the schema.
|
||||
<details>
|
||||
<summary>Finding descriptions (click to expand)</summary>
|
||||
|
||||
**Attack scenario:** An attacker who has observed a valid TOTP code (e.g. from a compromised session, shoulder surfing, or a MITM that delayed delivery) can reuse that code to authenticate within its validity window.
|
||||
### PEN-01 — `extractBearerFromRequest` Does Not Validate "Bearer " Prefix (Medium)
|
||||
|
||||
**Fix:** Track the last accepted TOTP counter per account in the database. Reject any counter ≤ the last accepted one. This requires a new column (`last_totp_counter INTEGER`) on the `accounts` table and a check-and-update in `ValidateTOTP`'s callers (or within it, with a DB reference passed in).
|
||||
**File:** `internal/server/server.go` (lines 1414–1425)
|
||||
|
||||
---
|
||||
|
||||
### CRIT-02 — gRPC `EnrollTOTP` Enables TOTP Before Confirmation (Medium)
|
||||
|
||||
**File:** `internal/grpcserver/auth.go:202` vs `internal/server/server.go:724-728`
|
||||
|
||||
The REST `EnrollTOTP` handler explicitly uses `StorePendingTOTP` (which keeps `totp_required=0`) and a comment at line 724 explains why:
|
||||
The server-level `extractBearerFromRequest` function extracts the token by slicing the `Authorization` header at offset 7 (`len("Bearer ")`) without first verifying that the header actually starts with `"Bearer "`. Any 8+ character `Authorization` value is accepted — e.g., `Authorization: XXXXXXXX` would extract `X` as the token string.
|
||||
|
||||
```go
|
||||
// Security: use StorePendingTOTP (not SetTOTP) so that totp_required
|
||||
// is not enabled until the user confirms the code.
|
||||
// Current (vulnerable):
|
||||
if len(auth) <= len(prefix) {
|
||||
return "", fmt.Errorf("malformed Authorization header")
|
||||
}
|
||||
return auth[len(prefix):], nil // no prefix check
|
||||
```
|
||||
|
||||
The gRPC `EnrollTOTP` handler at line 202 calls `SetTOTP` directly, which immediately sets `totp_required=1`. Any user who initiates TOTP enrollment over gRPC but does not immediately confirm will have their account locked out — they cannot log in because TOTP is required, but no working TOTP secret is confirmed.
|
||||
The middleware-level `extractBearerToken` in `internal/middleware/middleware.go` (lines 303–316) correctly uses `strings.SplitN` and `strings.EqualFold` to validate the prefix. The server-level function should be replaced with a call to the middleware version, or the same validation logic should be applied.
|
||||
|
||||
**Fix:** Change `grpcserver/auth.go:202` from `a.s.db.SetTOTP(...)` to `a.s.db.StorePendingTOTP(...)`, matching the REST server's behavior and the documented intent of those two DB methods.
|
||||
**Impact:** Low in practice because the extracted garbage is then passed to JWT validation which will reject it. However, it violates defense-in-depth: a future change to token validation could widen the attack surface, and the inconsistency between the two extraction functions is a maintenance hazard.
|
||||
|
||||
**Recommendation:** Replace `extractBearerFromRequest` with a call to `middleware.extractBearerToken` (after exporting it or moving the function), or replicate the prefix validation.
|
||||
|
||||
**Fix:** `extractBearerFromRequest` now uses `strings.SplitN` and `strings.EqualFold` to validate the `"Bearer"` prefix before extracting the token, matching the middleware implementation. Test `TestExtractBearerFromRequest` covers valid tokens, missing headers, non-Bearer schemes (Token, Basic), empty tokens, case-insensitive matching, and the previously-accepted garbage input.
|
||||
|
||||
---
|
||||
|
||||
## Defense-in-Depth Gaps
|
||||
### PEN-02 — Security Headers Missing from Live Instance Responses (Medium)
|
||||
|
||||
### DEF-01 — No Rate Limiting on the UI Login Endpoint (Medium)
|
||||
**Live probe:** `https://mcias.metacircular.net:8443/login`
|
||||
|
||||
**File:** `internal/ui/ui.go:264`
|
||||
The live instance's `/login` response did not include the security headers (`X-Content-Type-Options`, `Strict-Transport-Security`, `Cache-Control`, `Permissions-Policy`) that the source code's `globalSecurityHeaders` and UI `securityHeaders` middleware should be applying (SEC-04 and SEC-10 fixes).
|
||||
|
||||
This is likely a code/deployment discrepancy — the deployed binary may predate the SEC-04/SEC-10 fixes, or the middleware may not be wired into the route chain correctly for all paths.
|
||||
|
||||
**Impact:** Without HSTS, browsers will not enforce HTTPS-only access. Without `X-Content-Type-Options: nosniff`, MIME-type sniffing attacks are possible. Without `Cache-Control: no-store`, authenticated responses may be cached by proxies or browsers.
|
||||
|
||||
**Recommendation:** Redeploy the current source to the live instance and verify headers with `curl -I`.
|
||||
|
||||
**Fix:** Redeployed 2026-03-15. Live probe confirms all headers present on `/login`, `/v1/health`, and `/`:
|
||||
`cache-control: no-store`, `content-security-policy`, `permissions-policy`, `referrer-policy`, `strict-transport-security: max-age=63072000; includeSubDomains`, `x-content-type-options: nosniff`, `x-frame-options: DENY`.
|
||||
|
||||
---
|
||||
|
||||
### PEN-03 — CSP `unsafe-inline` on `/docs` Swagger UI Endpoint (Low)
|
||||
|
||||
**File:** `internal/server/server.go` (lines 1450–1452)
|
||||
|
||||
The `docsSecurityHeaders` wrapper sets a Content-Security-Policy that includes `script-src 'self' 'unsafe-inline'` and `style-src 'self' 'unsafe-inline'`. This is required by Swagger UI's rendering approach, but it weakens CSP protection on the docs endpoint.
|
||||
|
||||
**Impact:** If an attacker can inject content into the Swagger UI page (e.g., via a reflected parameter in the OpenAPI spec URL), inline scripts would execute. The blast radius is limited to the `/docs` path, which requires no authentication (see PEN-04).
|
||||
|
||||
**Recommendation:** Consider serving Swagger UI from a separate subdomain or using CSP nonces instead of `unsafe-inline`. Alternatively, accept the risk given the limited scope.
|
||||
|
||||
---
|
||||
|
||||
### PEN-04 — OpenAPI Spec Publicly Accessible Without Authentication (Informational)
|
||||
|
||||
**Live probe:** `GET /openapi.yaml` returns the full API specification without authentication.
|
||||
|
||||
The OpenAPI spec reveals all API endpoints, request/response schemas, authentication flows, and error codes. While security-through-obscurity is not a defense, exposing the full API surface to unauthenticated users provides a roadmap for attackers.
|
||||
|
||||
**Recommendation:** Consider requiring authentication for `/openapi.yaml` and `/docs`, or accept the risk if the API surface is intended to be public.
|
||||
|
||||
---
|
||||
|
||||
### PEN-05 — gRPC Port 9443 Publicly Accessible (Informational)
|
||||
|
||||
**Live probe:** Port 9443 accepts TLS connections and serves gRPC.
|
||||
|
||||
The gRPC interface is accessible from the public internet. While it requires authentication for all RPCs, exposing it increases the attack surface (gRPC-specific vulnerabilities, protocol-level attacks).
|
||||
|
||||
**Recommendation:** If gRPC is only used for server-to-server communication, restrict access at the firewall/network level. If it must be public, ensure gRPC-specific rate limiting and monitoring are in place (SEC-06 fix applies here).
|
||||
|
||||
---
|
||||
|
||||
### PEN-06 — REST Login Increments Lockout Counter for Missing TOTP Code (Low)
|
||||
|
||||
**File:** `internal/server/server.go` (lines 271–277)
|
||||
|
||||
When a TOTP-enrolled account submits a login request without a TOTP code, the REST handler calls `s.db.RecordLoginFailure(acct.ID)` before returning the `"TOTP code required"` error. This increments the lockout counter even though the user has not actually failed authentication — they simply omitted the TOTP field.
|
||||
|
||||
The gRPC handler was fixed for this exact issue in DEF-08, but the REST handler was not updated to match.
|
||||
|
||||
```go
|
||||
uiMux.HandleFunc("POST /login", u.handleLoginPost)
|
||||
// Current (REST — increments lockout for missing TOTP):
|
||||
if acct.TOTPRequired {
|
||||
if req.TOTPCode == "" {
|
||||
s.writeAudit(r, model.EventLoginFail, &acct.ID, nil, `{"reason":"totp_missing"}`)
|
||||
_ = s.db.RecordLoginFailure(acct.ID) // should not increment
|
||||
middleware.WriteError(w, http.StatusUnauthorized, "TOTP code required", "totp_required")
|
||||
return
|
||||
}
|
||||
```
|
||||
|
||||
The REST `/v1/auth/login` endpoint is wrapped with `loginRateLimit` (10 req/s per IP). The UI `/login` endpoint has no equivalent middleware. Account lockout (10 failures per 15 minutes) partially mitigates brute force, but an attacker can still enumerate whether accounts exist at full network speed before triggering lockout, and can trigger lockout against many accounts in parallel with no rate friction.
|
||||
**Impact:** An attacker who knows a username with TOTP enabled can lock the account by sending 10 login requests with a valid password but no TOTP code. The password must be correct (wrong passwords also increment the counter), but this lowers the bar from "must guess TOTP" to "must omit TOTP." More practically, legitimate users with buggy clients that forget the TOTP field could self-lock.
|
||||
|
||||
**Fix:** Apply the same `middleware.RateLimit(10, 10)` to `POST /login` in the UI mux. A simpler option is to wrap the entire `uiMux` with the rate limiter since the UI is also a sensitive surface.
|
||||
**Recommendation:** Remove the `RecordLoginFailure` call from the TOTP-missing branch, matching the gRPC handler's behavior after the DEF-08 fix.
|
||||
|
||||
**Fix:** `RecordLoginFailure` removed from the TOTP-missing branch in `internal/server/server.go`. The branch now matches the gRPC handler exactly, including the rationale comment. `TestTOTPMissingDoesNotIncrementLockout` verifies the fix: it fully enrolls TOTP via the HTTP API, sets `LockoutThreshold=1`, issues a TOTP-missing login, and asserts the account is not locked.
|
||||
|
||||
---
|
||||
|
||||
### DEF-02 — `pendingLogins` Map Has No Expiry Cleanup (Low)
|
||||
### PEN-07 — Rate Limiter Is Per-IP Only, No Per-Account Limiting (Informational)
|
||||
|
||||
**File:** `internal/ui/ui.go:57`
|
||||
The rate limiter uses a per-IP token bucket. An attacker with access to multiple IP addresses (botnet, cloud instances, rotating proxies) can distribute brute-force attempts across IPs to bypass the per-IP limit.
|
||||
|
||||
The `pendingLogins sync.Map` stores short-lived TOTP nonces (90-second TTL). When consumed via `consumeTOTPNonce`, entries are deleted via `LoadAndDelete`. However, entries that are created but never consumed (user abandons login at the TOTP step, closes browser) **accumulate indefinitely** — they are checked for expiry on read but never proactively deleted.
|
||||
The account lockout mechanism (10 failures in 15 minutes) provides a secondary defense, but it is a blunt instrument — it locks out the legitimate user as well.
|
||||
|
||||
In normal operation this is a minor memory leak. Under adversarial conditions — an attacker repeatedly sending username+password to step 1 without proceeding to step 2 — the map grows without bound. At scale this could be used for memory exhaustion.
|
||||
**Recommendation:** Consider adding per-account rate limiting as a complement to per-IP limiting. This would cap login attempts per username regardless of source IP, without affecting other users. The account lockout already partially serves this role, but a softer rate limit (e.g., 1 req/s per username) would slow distributed attacks without locking out the user.
|
||||
|
||||
**Fix:** Add a background goroutine (matching the pattern in `middleware.RateLimit`) that periodically iterates the map and deletes expired entries. A 5-minute cleanup interval is sufficient given the 90-second TTL.
|
||||
</details>
|
||||
|
||||
---
|
||||
|
||||
### DEF-03 — Rate Limiter Uses `RemoteAddr`, Not `X-Forwarded-For` (Low)
|
||||
## Remediated Findings (SEC-01 through SEC-12)
|
||||
|
||||
**File:** `internal/middleware/middleware.go:200`
|
||||
All findings from the SEC audit round have been remediated. The original descriptions are preserved below for reference.
|
||||
|
||||
The comment already acknowledges this: the rate limiter extracts the client IP from `r.RemoteAddr`. When the server is deployed behind a reverse proxy (nginx, Caddy, a load balancer), `RemoteAddr` will be the proxy's IP for all requests, collapsing all clients into a single rate-limit bucket. This effectively disables per-IP rate limiting in proxy deployments.
|
||||
| ID | Severity | Finding | Status |
|
||||
|----|----------|---------|--------|
|
||||
| SEC-01 | Medium | TOTP enrollment did not require password re-authentication | **Fixed** — both REST and gRPC now require current password, with lockout counter on failure |
|
||||
| SEC-02 | Medium | Account lockout response leaked account existence | **Fixed** — locked accounts now return same 401 `"invalid credentials"` as wrong password, with dummy Argon2 for timing uniformity |
|
||||
| SEC-03 | Medium | Token renewal had no proximity or re-auth check | **Fixed** — renewal requires token to have consumed ≥50% of its lifetime |
|
||||
| SEC-04 | Low-Med | REST API responses lacked security headers | **Fixed** — `globalSecurityHeaders` middleware applies `X-Content-Type-Options`, HSTS, and `Cache-Control: no-store` to all routes |
|
||||
| SEC-05 | Low | No request body size limit on REST API | **Fixed** — `decodeJSON` wraps body with `http.MaxBytesReader` (1 MiB); max password length enforced |
|
||||
| SEC-06 | Low | gRPC rate limiter ignored TrustedProxy | **Fixed** — `grpcClientIP` extracts real client IP via metadata when peer matches trusted proxy |
|
||||
| SEC-07 | Low | Static file directory listing enabled | **Fixed** — `noDirListing` wrapper returns 404 for directory requests |
|
||||
| SEC-08 | Low | System token issuance was not atomic | **Fixed** — `IssueSystemToken` wraps revoke+track in a single SQLite transaction |
|
||||
| SEC-09 | Info | Navigation bar exposed admin UI structure to non-admin users | **Fixed** — nav links conditionally rendered with `{{if .IsAdmin}}` |
|
||||
| SEC-10 | Info | No `Permissions-Policy` header | **Fixed** — `Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=()` added |
|
||||
| SEC-11 | Info | Audit log details used `fmt.Sprintf` instead of `json.Marshal` | **Fixed** — `audit.JSON` and `audit.JSONWithRoles` helpers use `json.Marshal` |
|
||||
| SEC-12 | Info | Default token expiry was 30 days | **Fixed** — default reduced to 7 days (168h); renewal proximity check (SEC-03) further limits exposure |
|
||||
|
||||
**Fix:** Add a configurable `TrustedProxy` setting. When set, extract the real client IP from `X-Forwarded-For` or `X-Real-IP` headers only for requests coming from that proxy address. Never trust those headers unconditionally — doing so allows IP spoofing.
|
||||
<details>
|
||||
<summary>Original finding descriptions (click to expand)</summary>
|
||||
|
||||
### SEC-01 — TOTP Enrollment Does Not Require Password Re-authentication (Medium)
|
||||
|
||||
**Files:** `internal/server/server.go`, `internal/grpcserver/auth.go`
|
||||
|
||||
`POST /v1/auth/totp/enroll` and the gRPC `EnrollTOTP` RPC originally required only a valid JWT — no password confirmation. If an attacker stole a session token, they could enroll TOTP on the victim's account.
|
||||
|
||||
**Fix:** Both endpoints now require the current password, with lockout counter incremented on failure.
|
||||
|
||||
---
|
||||
|
||||
### DEF-04 — Missing `nbf` (Not Before) Claim on Issued Tokens (Low)
|
||||
### SEC-02 — Account Lockout Response Leaks Account Existence (Medium)
|
||||
|
||||
**File:** `internal/token/token.go:73-82`
|
||||
Locked accounts originally returned HTTP 429 / gRPC `ResourceExhausted` with `"account temporarily locked"`, distinguishable from the HTTP 401 `"invalid credentials"` returned for wrong passwords.
|
||||
|
||||
`IssueToken` sets `iss`, `sub`, `iat`, `exp`, and `jti`, but not `nbf`. Without a not-before constraint, a token is valid from the moment of issuance and a slightly clock-skewed client or intermediate could present it early. This is a defense-in-depth measure, not a practical attack at the moment, but it costs nothing to add.
|
||||
|
||||
**Fix:** Add `NotBefore: jwt.NewNumericDate(now)` to the `RegisteredClaims` struct. Add the corresponding validation step in `ValidateToken` (using `jwt.WithNotBefore()` or a manual check).
|
||||
**Fix:** All login paths now return the same `"invalid credentials"` response for locked accounts, with dummy Argon2 to maintain timing uniformity.
|
||||
|
||||
---
|
||||
|
||||
### DEF-05 — No Maximum Token Expiry Ceiling in Config Validation (Low)
|
||||
### SEC-03 — Token Renewal Has No Proximity or Re-auth Check (Medium)
|
||||
|
||||
**File:** `internal/config/config.go:150-158`
|
||||
`POST /v1/auth/renew` originally accepted any valid token regardless of remaining lifetime.
|
||||
|
||||
The config validator enforces that expiry durations are positive but not that they are bounded above. An operator misconfiguration (e.g. `service_expiry = "876000h"`) would issue tokens valid for 100 years. For human sessions (`default_expiry`, `admin_expiry`) this is a significant risk in the event of token theft.
|
||||
|
||||
**Fix:** Add upper-bound checks in `validate()`. Suggested maximums: 30 days for `default_expiry`, 24 hours for `admin_expiry`, 5 years for `service_expiry`. At minimum, log a warning when values exceed reasonable thresholds.
|
||||
**Fix:** Renewal now requires the token to have consumed ≥50% of its lifetime before it can be renewed.
|
||||
|
||||
---
|
||||
|
||||
### DEF-06 — `GetAccountByUsername` Comment Incorrect re: Case Sensitivity (Informational)
|
||||
### SEC-04 — REST API Responses Lack Security Headers (Low-Medium)
|
||||
|
||||
**File:** `internal/db/accounts.go:73`
|
||||
API endpoints originally returned only `Content-Type` — no `Cache-Control`, `X-Content-Type-Options`, or HSTS.
|
||||
|
||||
The comment reads "case-insensitive" but the query uses `WHERE username = ?` with SQLite's default BINARY collation, which is **case-sensitive**. This means `admin` and `Admin` would be treated as distinct accounts. This is not a security bug by itself, but it contradicts the comment and could mask confusion.
|
||||
|
||||
**Fix:** If case-insensitive matching is intended, add `COLLATE NOCASE` to the column definition or the query. If case-sensitive is correct (more common for SSO systems), remove the word "case-insensitive" from the comment.
|
||||
**Fix:** `globalSecurityHeaders` middleware applies these headers to all routes (API and UI).
|
||||
|
||||
---
|
||||
|
||||
### DEF-07 — SQLite `synchronous=NORMAL` in WAL Mode (Low)
|
||||
### SEC-05 — No Request Body Size Limit on REST API Endpoints (Low)
|
||||
|
||||
**File:** `internal/db/db.go:68`
|
||||
`decodeJSON` originally read from `r.Body` without any size limit.
|
||||
|
||||
With `PRAGMA synchronous=NORMAL` and `journal_mode=WAL`, SQLite syncs the WAL file on checkpoints but not on every write. A power failure between a write and the next checkpoint could lose the most recent transactions. For an authentication system — where token issuance and revocation records must be durable — this is a meaningful risk.
|
||||
|
||||
**Fix:** Change to `PRAGMA synchronous=FULL`. For a single-node personal SSO the performance impact is negligible; durability of token revocations is worth it.
|
||||
**Fix:** `http.MaxBytesReader` with 1 MiB limit added to `decodeJSON`. Maximum password length also enforced.
|
||||
|
||||
---
|
||||
|
||||
### DEF-08 — gRPC `Login` Counts TOTP-Missing as a Login Failure (Low)
|
||||
### SEC-06 — gRPC Rate Limiter Ignores TrustedProxy (Low)
|
||||
|
||||
**File:** `internal/grpcserver/auth.go:76-77`
|
||||
The gRPC rate limiter originally used `peer.FromContext` directly, always getting the proxy IP behind a reverse proxy.
|
||||
|
||||
When TOTP is required but no code is provided (`req.TotpCode == ""`), the gRPC handler calls `RecordLoginFailure`. In the two-step UI flow this is defensible, but via the gRPC single-step `Login` RPC, a well-behaved client that has not yet obtained the TOTP code (not an attacker) will increment the failure counter. Repeated retries could trigger account lockout unintentionally.
|
||||
|
||||
**Fix:** Either document that gRPC clients must always include the TOTP code and treat its omission as a deliberate attempt, or do not count "TOTP code required" as a failure (since the password was verified successfully at that point).
|
||||
**Fix:** `grpcClientIP` now reads from gRPC metadata headers when the peer matches the trusted proxy.
|
||||
|
||||
---
|
||||
|
||||
### DEF-09 — Security Headers Missing on REST API Docs Endpoints (Informational)
|
||||
### SEC-07 — Static File Directory Listing Enabled (Low)
|
||||
|
||||
**File:** `internal/server/server.go:85-94`
|
||||
`http.FileServerFS` served directory listings by default.
|
||||
|
||||
The `/docs` and `/docs/openapi.yaml` endpoints are served from the parent `mux` and therefore do not receive the `securityHeaders` middleware applied to the UI sub-mux. The Swagger UI page at `/docs` is served without `X-Frame-Options`, `Content-Security-Policy`, etc.
|
||||
|
||||
**Fix:** Apply a security-headers middleware to the docs handlers, or move them into the UI sub-mux.
|
||||
**Fix:** `noDirListing` wrapper returns 404 for directory requests.
|
||||
|
||||
---
|
||||
|
||||
### DEF-10 — Role Strings Not Validated Against an Allowlist (Low)
|
||||
### SEC-08 — System Token Issuance Is Not Atomic (Low)
|
||||
|
||||
**File:** `internal/db/accounts.go:302-311` (`GrantRole`)
|
||||
`handleTokenIssue` originally performed three sequential non-transactional operations.
|
||||
|
||||
There is no allowlist for role strings written to the `account_roles` table. Any string can be stored. While the admin-only constraint prevents non-admins from calling these endpoints, a typo by an admin (e.g. `"admim"`) would silently create an unknown role that silently grants nothing. The `RequireRole` check would never match it, causing a confusing failure mode.
|
||||
|
||||
**Fix:** Maintain a compile-time allowlist of valid roles (e.g. `"admin"`, `"user"`) and reject unknown role names at the handler layer before writing to the database.
|
||||
**Fix:** `IssueSystemToken` wraps all operations in a single SQLite transaction.
|
||||
|
||||
---
|
||||
|
||||
## Positive Findings
|
||||
### SEC-09 — Navigation Bar Exposes Admin UI Structure to Non-Admin Users (Informational)
|
||||
|
||||
The following implementation details are exemplary and should be preserved:
|
||||
Nav links were rendered for all authenticated users.
|
||||
|
||||
**Fix:** Admin nav links wrapped in `{{if .IsAdmin}}` conditional.
|
||||
|
||||
---
|
||||
|
||||
### SEC-10 — No `Permissions-Policy` Header (Informational)
|
||||
|
||||
The security headers middleware did not include `Permissions-Policy`.
|
||||
|
||||
**Fix:** `Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=()` added.
|
||||
|
||||
---
|
||||
|
||||
### SEC-11 — Audit Log Details Use `fmt.Sprintf` Instead of `json.Marshal` (Informational)
|
||||
|
||||
Audit details were constructed with `fmt.Sprintf` and `%q`, which is fragile for JSON.
|
||||
|
||||
**Fix:** `audit.JSON` and `audit.JSONWithRoles` helpers use `json.Marshal`.
|
||||
|
||||
---
|
||||
|
||||
### SEC-12 — Default Token Expiry Is 30 Days (Informational / Configuration)
|
||||
|
||||
Default expiry was 720h (30 days).
|
||||
|
||||
**Fix:** Reduced to 168h (7 days). Combined with SEC-03's renewal proximity check, exposure window is significantly reduced.
|
||||
|
||||
</details>
|
||||
|
||||
---
|
||||
|
||||
## Previously Remediated Findings (CRIT/DEF series)
|
||||
|
||||
The following findings from the initial audit (2026-03-12) were confirmed fixed in the 2026-03-13 audit:
|
||||
|
||||
| ID | Finding | Status |
|
||||
|----|---------|--------|
|
||||
| CRIT-01 | TOTP replay attack — no counter tracking | **Fixed** — `CheckAndUpdateTOTPCounter` with atomic SQL, migration 000007 |
|
||||
| CRIT-02 | gRPC `EnrollTOTP` called `SetTOTP` instead of `StorePendingTOTP` | **Fixed** — now calls `StorePendingTOTP` |
|
||||
| DEF-01 | No rate limiting on UI login | **Fixed** — `loginRateLimit` applied to `POST /login` |
|
||||
| DEF-02 | `pendingLogins` map had no expiry cleanup | **Fixed** — `cleanupPendingLogins` goroutine runs every 5 minutes |
|
||||
| DEF-03 | Rate limiter ignored `X-Forwarded-For` | **Fixed** — `ClientIP()` respects `TrustedProxy` config |
|
||||
| DEF-04 | Missing `nbf` claim on tokens | **Fixed** — `NotBefore: jwt.NewNumericDate(now)` added |
|
||||
| DEF-05 | No max token expiry ceiling | **Fixed** — upper bounds enforced in config validation |
|
||||
| DEF-06 | Incorrect case-sensitivity comment | **Fixed** — comment corrected |
|
||||
| DEF-07 | SQLite `synchronous=NORMAL` | **Fixed** — changed to `PRAGMA synchronous=FULL` |
|
||||
| DEF-08 | gRPC counted TOTP-missing as failure | **Fixed** — no longer increments lockout counter |
|
||||
| DEF-09 | Security headers missing on docs endpoints | **Fixed** — `docsSecurityHeaders` wrapper added |
|
||||
| DEF-10 | Role strings not validated | **Fixed** — `model.ValidateRole()` with compile-time allowlist |
|
||||
|
||||
---
|
||||
|
||||
## Positive Findings (Preserved)
|
||||
|
||||
These implementation details are exemplary and should be maintained:
|
||||
|
||||
| Area | Detail |
|
||||
|------|--------|
|
||||
| JWT alg confusion | `ValidateToken` enforces `alg=EdDSA` in the key function, before signature verification — the only correct place |
|
||||
| Constant-time comparisons | `crypto/subtle.ConstantTimeCompare` used consistently for password hashes, TOTP codes, and CSRF tokens |
|
||||
| Timing uniformity | Dummy Argon2 computed (once, with full production parameters via `sync.Once`) for unknown/inactive users on both REST and gRPC paths |
|
||||
| Token revocation | Every token is tracked by JTI; unknown tokens are rejected (fail-closed) rather than silently accepted |
|
||||
| Token renewal atomicity | `RenewToken` wraps revocation + insertion in a single SQLite transaction |
|
||||
| TOTP nonce design | Two-step UI login uses a 128-bit single-use server-side nonce to avoid transmitting the password twice |
|
||||
| CSRF protection | HMAC-SHA256 signed double-submit cookie with `SameSite=Strict` and constant-time validation |
|
||||
| Credential exclusion | `json:"-"` tags on all credential fields; proto messages omit them too |
|
||||
| Security headers | All UI responses receive CSP, `X-Content-Type-Options`, `X-Frame-Options`, HSTS, and `Referrer-Policy` |
|
||||
| Account lockout | 10-attempt, 15-minute rolling lockout checked before Argon2 to prevent timing oracle |
|
||||
| Argon2id parameters | Config validator enforces OWASP 2023 minimums and rejects weakening |
|
||||
| SQL injection | All queries use parameterized statements; no string concatenation anywhere |
|
||||
| Audit log | Append-only with actor/target/IP; no delete path provided |
|
||||
| Master key handling | Env var cleared after reading; signing key zeroed on shutdown |
|
||||
| JWT alg confusion | `ValidateToken` enforces `alg=EdDSA` in the key function before signature verification |
|
||||
| Constant-time operations | `crypto/subtle.ConstantTimeCompare` for password hashes, CSRF tokens; all three TOTP windows evaluated without early exit |
|
||||
| Timing uniformity | Dummy Argon2 via `sync.Once` for unknown/inactive users on all login paths |
|
||||
| Token revocation | Fail-closed: untracked tokens are rejected, not silently accepted |
|
||||
| Token renewal atomicity | `RenewToken` wraps revoke+track in a single SQLite transaction |
|
||||
| TOTP replay prevention | Counter-based replay detection with atomic SQL UPDATE/WHERE |
|
||||
| TOTP nonce design | 128-bit single-use server-side nonce; password never retransmitted in step 2 |
|
||||
| CSRF protection | HMAC-SHA256 double-submit cookie, domain-separated key derivation, SameSite=Strict, constant-time validation |
|
||||
| Credential exclusion | `json:"-"` on all credential fields; password hash never in API responses |
|
||||
| Security headers (UI) | CSP (no unsafe-inline), X-Content-Type-Options, X-Frame-Options DENY, HSTS 2yr, Referrer-Policy no-referrer |
|
||||
| Cookie hardening | HttpOnly + Secure + SameSite=Strict on session cookie |
|
||||
| Account lockout | 10-attempt rolling window, checked before Argon2, with timing-safe dummy hash |
|
||||
| Argon2id parameters | Config validator enforces OWASP 2023 minimums; rejects weakening |
|
||||
| SQL injection | Zero string concatenation — all queries parameterized |
|
||||
| Input validation | Username regex + length, password min length, account type enum, role allowlist, JSON strict decoder |
|
||||
| Audit logging | Append-only, no delete path, credentials never logged, actor/target/IP captured |
|
||||
| Master key hygiene | Env var cleared after read, key zeroed on shutdown, AES-256-GCM at rest |
|
||||
| TLS | MinVersion TLS 1.2, X25519 preferred, no plaintext listener, read/write/idle timeouts set |
|
||||
|
||||
---
|
||||
|
||||
## Remediation Priority
|
||||
## Penetration Test — Attacks That Failed (2026-03-14)
|
||||
|
||||
| Fixed | Priority | ID | Severity | Action |
|
||||
|-------|----------|----|----------|--------|
|
||||
| Yes | 1 | CRIT-02 | Medium | Change `grpcserver/auth.go:202` to call `StorePendingTOTP` instead of `SetTOTP` |
|
||||
| Yes | 2 | CRIT-01 | Medium | Add `last_totp_counter` tracking to prevent TOTP replay within the validity window |
|
||||
| Yes | 3 | DEF-01 | Medium | Apply IP rate limiting to the UI `POST /login` endpoint |
|
||||
| Yes | 4 | DEF-02 | Low | Add background cleanup goroutine for the `pendingLogins` map |
|
||||
| Yes | 5 | DEF-03 | Low | Support trusted-proxy IP extraction for accurate per-client rate limiting |
|
||||
| Yes | 6 | DEF-04 | Low | Add `nbf` claim to issued tokens and validate it on receipt |
|
||||
| Yes | 7 | DEF-05 | Low | Add upper-bound caps on token expiry durations in config validation |
|
||||
| Yes | 8 | DEF-07 | Low | Change SQLite to `PRAGMA synchronous=FULL` |
|
||||
| Yes | 9 | DEF-08 | Low | Do not count gRPC TOTP-missing as a login failure |
|
||||
| Yes | 10 | DEF-10 | Low | Validate role strings against an allowlist before writing to the DB |
|
||||
| Yes | 11 | DEF-09 | Info | Apply security headers to `/docs` endpoints |
|
||||
| Yes | 12 | DEF-06 | Info | Correct the misleading "case-insensitive" comment in `GetAccountByUsername` |
|
||||
The following attacks were attempted against the live instance and failed, confirming the effectiveness of existing defenses:
|
||||
|
||||
| Attack | Result |
|
||||
|--------|--------|
|
||||
| JWT `alg:none` bypass | Rejected — `ValidateToken` enforces `alg=EdDSA` |
|
||||
| JWT `alg:HS256` key-confusion | Rejected — only EdDSA accepted |
|
||||
| Forged JWT with random Ed25519 key | Rejected — signature verification failed |
|
||||
| Username enumeration via timing | Not possible — ~355ms for both existing and non-existing users (dummy Argon2 working) |
|
||||
| Username enumeration via error messages | Not possible — identical `"invalid credentials"` for all failure modes |
|
||||
| Account lockout enumeration | Not possible — locked accounts return same response as wrong password (SEC-02 fix confirmed) |
|
||||
| SQL injection via login fields | Not possible — parameterized queries throughout |
|
||||
| JSON body bomb (oversized payload) | Rejected — `http.MaxBytesReader` returns 413 (SEC-05 fix confirmed) |
|
||||
| Unknown JSON fields | Rejected — `DisallowUnknownFields` active on decoder |
|
||||
| Rate limit bypass | Working correctly — 429 after burst exhaustion, `Retry-After` header present |
|
||||
| Admin endpoint access without auth | Properly returns 401 |
|
||||
| Directory traversal on static files | Not possible — `noDirListing` wrapper returns 404 (SEC-07 fix confirmed) |
|
||||
| Public key endpoint | Returns Ed25519 PKIX key (expected; public by design) |
|
||||
|
||||
---
|
||||
|
||||
## Schema Observations
|
||||
## Remediation Status
|
||||
|
||||
The migration chain (migrations 001–006) is sound. Foreign key cascades are appropriate. Indexes are present on all commonly-queried columns. The `failed_logins` table uses a rolling window query approach which is correct.
|
||||
**CRIT/DEF/SEC series:** All 24 findings remediated. No open items.
|
||||
|
||||
One note: the `accounts` table has no unique index enforcing `COLLATE NOCASE` on `username`. This is consistent with treating usernames as case-sensitive but should be documented explicitly to avoid future ambiguity.
|
||||
**PEN series (2026-03-14):** All 7 findings resolved — 4 fixed, 3 accepted by design. Unauthorized access was not achieved. No open items remain.
|
||||
|
||||
Next audit should focus on:
|
||||
- Any new features added since 2026-03-15
|
||||
- Dependency updates and CVE review
|
||||
- Re-evaluate PEN-03 if Swagger UI self-hosting becomes desirable
|
||||
|
||||
140
PROGRESS.md
140
PROGRESS.md
@@ -2,7 +2,145 @@
|
||||
|
||||
Source of truth for current development state.
|
||||
---
|
||||
All phases complete. **v1.0.0 tagged.** All packages pass `go test ./...`; `golangci-lint run ./...` clean.
|
||||
All phases complete. **v1.0.0 tagged.** All packages pass `go test ./...`; `golangci-lint run ./...` clean (pre-existing warnings only).
|
||||
|
||||
### 2026-03-15 — Service account token delegation and download
|
||||
|
||||
**Problem:** Only admins could issue tokens for service accounts, and the only way to retrieve the token was a flash message (copy-paste). There was no delegation mechanism for non-admin users.
|
||||
|
||||
**Solution:** Added token-issue delegation and a one-time secure file download flow.
|
||||
|
||||
**DB (`internal/db/`):**
|
||||
- Migration `000008`: new `service_account_delegates` table — tracks which human accounts may issue tokens for a given system account
|
||||
- `GrantTokenIssueAccess`, `RevokeTokenIssueAccess`, `ListTokenIssueDelegates`, `HasTokenIssueAccess`, `ListDelegatedServiceAccounts` functions
|
||||
|
||||
**Model (`internal/model/`):**
|
||||
- New `ServiceAccountDelegate` type
|
||||
- New audit event constants: `EventTokenDelegateGranted`, `EventTokenDelegateRevoked`
|
||||
|
||||
**UI (`internal/ui/`):**
|
||||
- `handleIssueSystemToken`: now allows admins and delegates (not just admins); after issuance stores token in a short-lived (5 min) single-use download nonce; returns download link in the HTMX fragment
|
||||
- `handleDownloadToken`: serves the token as `Content-Disposition: attachment` via the one-time nonce; nonce deleted on first use to prevent replay
|
||||
- `handleGrantTokenDelegate` / `handleRevokeTokenDelegate`: admin-only endpoints to manage delegate access for a system account
|
||||
- `handleServiceAccountsPage`: new `/service-accounts` page for non-admin delegates to see their assigned service accounts and issue tokens
|
||||
- New `tokenDownloads sync.Map` in `UIServer` with background cleanup goroutine
|
||||
|
||||
**Routes:**
|
||||
- `POST /accounts/{id}/token` — changed from admin-only to authed+CSRF, authorization checked in handler
|
||||
- `GET /token/download/{nonce}` — new, authed
|
||||
- `POST /accounts/{id}/token/delegates` — new, admin-only
|
||||
- `DELETE /accounts/{id}/token/delegates/{grantee}` — new, admin-only
|
||||
- `GET /service-accounts` — new, authed (delegates' token management page)
|
||||
|
||||
**Templates:**
|
||||
- `token_list.html`: shows download link after issuance
|
||||
- `token_delegates.html`: new fragment for admin delegate management
|
||||
- `account_detail.html`: added "Token Issue Access" section for system accounts
|
||||
- `service_accounts.html`: new page listing delegated service accounts with issue button
|
||||
- `base.html`: non-admin nav now shows "Service Accounts" link
|
||||
|
||||
### 2026-03-14 — Vault seal/unseal lifecycle
|
||||
|
||||
**Problem:** `mciassrv` required the master passphrase at startup and refused to start without it. Operators needed a way to start the server in a degraded state and provide the passphrase at runtime, plus the ability to re-seal at runtime.
|
||||
|
||||
**Solution:** Implemented a `Vault` abstraction that manages key material lifecycle with seal/unseal state transitions.
|
||||
|
||||
**New package: `internal/vault/`**
|
||||
- `vault.go`: Thread-safe `Vault` struct with `sync.RWMutex`-protected state. Methods: `IsSealed()`, `Unseal()`, `Seal()`, `MasterKey()`, `PrivKey()`, `PubKey()`. `Seal()` zeroes all key material before nilling.
|
||||
- `derive.go`: Extracted `DeriveFromPassphrase()` and `DecryptSigningKey()` from `cmd/mciassrv/main.go` for reuse by unseal handlers.
|
||||
- `vault_test.go`: Tests for state transitions, key zeroing, concurrent access.
|
||||
|
||||
**REST API (`internal/server/`):**
|
||||
- `POST /v1/vault/unseal`: Accept passphrase, derive key, unseal (rate-limited 3/s burst 5)
|
||||
- `POST /v1/vault/seal`: Admin-only, seals vault and zeroes key material
|
||||
- `GET /v1/vault/status`: Returns `{"sealed": bool}`
|
||||
- `GET /v1/health`: Now returns `{"status":"sealed"}` when sealed
|
||||
- All other `/v1/*` endpoints return 503 `vault_sealed` when sealed
|
||||
|
||||
**Web UI (`internal/ui/`):**
|
||||
- New unseal page at `/unseal` with passphrase form (same styling as login)
|
||||
- All UI routes redirect to `/unseal` when sealed (except `/static/`)
|
||||
- CSRF manager now derives key lazily from vault
|
||||
|
||||
**gRPC (`internal/grpcserver/`):**
|
||||
- New `sealedInterceptor` first in interceptor chain — returns `codes.Unavailable` for all RPCs except Health
|
||||
- Health RPC returns `status: "sealed"` when sealed
|
||||
|
||||
**Startup (`cmd/mciassrv/main.go`):**
|
||||
- When passphrase env var is empty/unset (and not first run): starts in sealed state
|
||||
- When passphrase is available: backward-compatible unsealed startup
|
||||
- First run still requires passphrase to generate signing key
|
||||
|
||||
**Refactoring:**
|
||||
- All three servers (REST, UI, gRPC) share a single `*vault.Vault` by pointer
|
||||
- Replaced static `privKey`, `pubKey`, `masterKey` fields with vault accessor calls
|
||||
- `middleware.RequireAuth` now reads pubkey from vault at request time
|
||||
- New `middleware.RequireUnsealed` middleware wired before request logger
|
||||
|
||||
**Audit events:** Added `vault_sealed` and `vault_unsealed` event types.
|
||||
|
||||
**OpenAPI:** Updated `openapi.yaml` with vault endpoints and sealed health response.
|
||||
|
||||
**Files changed:** 19 files (3 new packages, 3 new handlers, 1 new template, extensive refactoring across all server packages and tests).
|
||||
|
||||
### 2026-03-13 — Make pgcreds discoverable via CLI and UI
|
||||
|
||||
**Problem:** Users had no way to discover which pgcreds were available to them or what their credential IDs were, making it functionally impossible to use the system without manual database inspection.
|
||||
|
||||
**Solution:** Added two complementary discovery paths:
|
||||
|
||||
**REST API:**
|
||||
- New `GET /v1/pgcreds` endpoint (requires authentication) returns all accessible credentials (owned + explicitly granted) with their IDs, host, port, database, username, and timestamps
|
||||
- Response includes `id` field so users can then fetch full credentials via `GET /v1/accounts/{id}/pgcreds`
|
||||
|
||||
**CLI (`cmd/mciasctl/main.go`):**
|
||||
- New `pgcreds list` subcommand calls `GET /v1/pgcreds` and displays accessible credentials with IDs
|
||||
- Updated usage documentation to include `pgcreds list`
|
||||
|
||||
**Web UI (`web/templates/pgcreds.html`):**
|
||||
- Credential ID now displayed in a `<code>` element at the top of each credential's metadata block
|
||||
- Styled with monospace font for easy copying and reference
|
||||
|
||||
**Files modified:**
|
||||
- `internal/server/server.go`: Added route `GET /v1/pgcreds` (requires auth, not admin) + handler `handleListAccessiblePGCreds`
|
||||
- `cmd/mciasctl/main.go`: Added `pgCredsList` function and switch case
|
||||
- `web/templates/pgcreds.html`: Display credential ID in the credentials list
|
||||
- Struct field alignment fixed in `pgCredResponse` to pass `go vet`
|
||||
|
||||
All tests pass; `go vet ./...` clean.
|
||||
|
||||
### 2026-03-12 — Update web UI and model for all compile-time roles
|
||||
|
||||
- `internal/model/model.go`: added `RoleGuest`, `RoleViewer`, `RoleEditor`, and
|
||||
`RoleCommenter` constants; updated `allowedRoles` map and `ValidateRole` error
|
||||
message to include the full set of recognised roles.
|
||||
- `internal/ui/`: updated `knownRoles` to include guest, viewer, editor, and
|
||||
commenter; replaced hardcoded role strings with model constants; removed
|
||||
obsolete "service" role from UI dropdowns.
|
||||
- All tests pass; build verified.
|
||||
|
||||
### 2026-03-12 — Fix UI privilege escalation vulnerability
|
||||
|
||||
**internal/ui/ui.go**
|
||||
- Added `requireAdminRole` middleware that checks `claims.HasRole("admin")`
|
||||
and returns 403 if absent
|
||||
- Updated `admin` and `adminGet` middleware wrappers to include
|
||||
`requireAdminRole` in the chain — previously only `requireCookieAuth`
|
||||
was applied, allowing any authenticated user to access admin endpoints
|
||||
- Profile routes correctly use only `requireCookieAuth` (not admin-gated)
|
||||
|
||||
**internal/ui/handlers_accounts.go**
|
||||
- Removed redundant inline admin check from `handleAdminResetPassword`
|
||||
(now handled by route-level middleware)
|
||||
|
||||
**Full audit performed across all three API surfaces:**
|
||||
- REST (`internal/server/server.go`): all admin routes use
|
||||
`requireAuth → RequireRole("admin")` — correct
|
||||
- gRPC (all service files): every admin RPC calls `requireAdmin(ctx)` as
|
||||
first statement — correct
|
||||
- UI: was vulnerable, now fixed with `requireAdminRole` middleware
|
||||
|
||||
All tests pass; `go vet ./...` clean.
|
||||
|
||||
### 2026-03-12 — Checkpoint: password change UI enforcement + migration recovery
|
||||
|
||||
|
||||
@@ -165,18 +165,27 @@ See ARCHITECTURE.md for design rationale.
|
||||
### Step 4.1: `cmd/mciasctl` — admin CLI
|
||||
**Acceptance criteria:**
|
||||
- Subcommands:
|
||||
- `mciasctl account create --username NAME --type human|system`
|
||||
- `mciasctl account create -username NAME -type human|system`
|
||||
- `mciasctl account list`
|
||||
- `mciasctl account suspend --id UUID`
|
||||
- `mciasctl account delete --id UUID`
|
||||
- `mciasctl role grant --account UUID --role ROLE`
|
||||
- `mciasctl role revoke --account UUID --role ROLE`
|
||||
- `mciasctl token issue --account UUID` (system accounts)
|
||||
- `mciasctl token revoke --jti JTI`
|
||||
- `mciasctl pgcreds set --account UUID --host H --port P --db D --user U --password P`
|
||||
- `mciasctl pgcreds get --account UUID`
|
||||
- CLI reads admin JWT from `MCIAS_ADMIN_TOKEN` env var or `--token` flag
|
||||
- All commands make HTTPS requests to mciassrv (base URL from `--server` flag
|
||||
- `mciasctl account update -id UUID -status active|inactive`
|
||||
- `mciasctl account delete -id UUID`
|
||||
- `mciasctl account get -id UUID`
|
||||
- `mciasctl account set-password -id UUID`
|
||||
- `mciasctl role list -id UUID`
|
||||
- `mciasctl role set -id UUID -roles role1,role2`
|
||||
- `mciasctl role grant -id UUID -role ROLE`
|
||||
- `mciasctl role revoke -id UUID -role ROLE`
|
||||
- `mciasctl token issue -id UUID` (system accounts)
|
||||
- `mciasctl token revoke -jti JTI`
|
||||
- `mciasctl pgcreds set -id UUID -host H -port P -db D -user U`
|
||||
- `mciasctl pgcreds get -id UUID`
|
||||
- `mciasctl auth login`
|
||||
- `mciasctl auth change-password`
|
||||
- `mciasctl tag list -id UUID`
|
||||
- `mciasctl tag set -id UUID -tags tag1,tag2`
|
||||
- `mciasctl policy list|create|get|update|delete`
|
||||
- CLI reads admin JWT from `MCIAS_TOKEN` env var or `-token` flag
|
||||
- All commands make HTTPS requests to mciassrv (base URL from `-server` flag
|
||||
or `MCIAS_SERVER` env var)
|
||||
- Tests: flag parsing; missing required flags → error; help text complete
|
||||
|
||||
|
||||
65
README.md
65
README.md
@@ -64,10 +64,10 @@ EOF
|
||||
|
||||
Generate the certificate:
|
||||
```sh
|
||||
cert genkey -a ec -s 521 > /etc/mcias/server.key
|
||||
cert selfsign -p /etc/mcias/server.key -f /tmp/request.yaml > /etc/mcias/server.crt
|
||||
chmod 0640 /etc/mcias/server.key
|
||||
chown root:mcias /etc/mcias/server.key
|
||||
cert genkey -a ec -s 521 > /srv/mcias/server.key
|
||||
cert selfsign -p /srv/mcias/server.key -f /tmp/request.yaml > /srv/mcias/server.crt
|
||||
chmod 0640 /srv/mcias/server.key
|
||||
chown mcias:mcias /srv/mcias/server.key /srv/mcias/server.crt
|
||||
rm /tmp/request.yaml
|
||||
```
|
||||
|
||||
@@ -75,21 +75,21 @@ rm /tmp/request.yaml
|
||||
|
||||
```sh
|
||||
openssl req -x509 -newkey ed25519 -days 3650 \
|
||||
-keyout /etc/mcias/server.key \
|
||||
-out /etc/mcias/server.crt \
|
||||
-keyout /srv/mcias/server.key \
|
||||
-out /srv/mcias/server.crt \
|
||||
-subj "/CN=auth.example.com" \
|
||||
-nodes
|
||||
chmod 0640 /etc/mcias/server.key
|
||||
chown root:mcias /etc/mcias/server.key
|
||||
chmod 0640 /srv/mcias/server.key
|
||||
chown mcias:mcias /srv/mcias/server.key /srv/mcias/server.crt
|
||||
```
|
||||
|
||||
### 2. Configure the server
|
||||
|
||||
```sh
|
||||
cp dist/mcias.conf.example /etc/mcias/mcias.conf
|
||||
$EDITOR /etc/mcias/mcias.conf
|
||||
chmod 0640 /etc/mcias/mcias.conf
|
||||
chown root:mcias /etc/mcias/mcias.conf
|
||||
cp dist/mcias.conf.example /srv/mcias/mcias.toml
|
||||
$EDITOR /srv/mcias/mcias.toml
|
||||
chmod 0640 /srv/mcias/mcias.toml
|
||||
chown mcias:mcias /srv/mcias/mcias.toml
|
||||
```
|
||||
|
||||
Minimum required fields:
|
||||
@@ -97,11 +97,11 @@ Minimum required fields:
|
||||
```toml
|
||||
[server]
|
||||
listen_addr = "0.0.0.0:8443"
|
||||
tls_cert = "/etc/mcias/server.crt"
|
||||
tls_key = "/etc/mcias/server.key"
|
||||
tls_cert = "/srv/mcias/server.crt"
|
||||
tls_key = "/srv/mcias/server.key"
|
||||
|
||||
[database]
|
||||
path = "/var/lib/mcias/mcias.db"
|
||||
path = "/srv/mcias/mcias.db"
|
||||
|
||||
[tokens]
|
||||
issuer = "https://auth.example.com"
|
||||
@@ -116,10 +116,10 @@ For local development, use `dist/mcias-dev.conf.example`.
|
||||
### 3. Set the master key passphrase
|
||||
|
||||
```sh
|
||||
cp dist/mcias.env.example /etc/mcias/env
|
||||
$EDITOR /etc/mcias/env # replace the placeholder passphrase
|
||||
chmod 0640 /etc/mcias/env
|
||||
chown root:mcias /etc/mcias/env
|
||||
cp dist/mcias.env.example /srv/mcias/env
|
||||
$EDITOR /srv/mcias/env # replace the placeholder passphrase
|
||||
chmod 0640 /srv/mcias/env
|
||||
chown mcias:mcias /srv/mcias/env
|
||||
```
|
||||
|
||||
> **Important:** Back up the passphrase to a secure offline location.
|
||||
@@ -130,10 +130,10 @@ chown root:mcias /etc/mcias/env
|
||||
```sh
|
||||
export MCIAS_MASTER_PASSPHRASE=your-passphrase
|
||||
|
||||
mciasdb --config /etc/mcias/mcias.conf account create \
|
||||
mciasdb --config /srv/mcias/mcias.toml account create \
|
||||
--username admin --type human
|
||||
mciasdb --config /etc/mcias/mcias.conf account set-password --id <UUID>
|
||||
mciasdb --config /etc/mcias/mcias.conf role grant --id <UUID> --role admin
|
||||
mciasdb --config /srv/mcias/mcias.toml account set-password --id <UUID>
|
||||
mciasdb --config /srv/mcias/mcias.toml role grant --id <UUID> --role admin
|
||||
```
|
||||
|
||||
### 5. Start the server
|
||||
@@ -143,7 +143,7 @@ mciasdb --config /etc/mcias/mcias.conf role grant --id <UUID> --role admin
|
||||
systemctl enable --now mcias
|
||||
|
||||
# manual
|
||||
MCIAS_MASTER_PASSPHRASE=your-passphrase mciassrv -config /etc/mcias/mcias.conf
|
||||
MCIAS_MASTER_PASSPHRASE=your-passphrase mciassrv -config /srv/mcias/mcias.toml
|
||||
```
|
||||
|
||||
### 6. Verify
|
||||
@@ -193,7 +193,7 @@ See `man mciasctl` for the full reference.
|
||||
|
||||
```sh
|
||||
export MCIAS_MASTER_PASSPHRASE=your-passphrase
|
||||
CONF="--config /etc/mcias/mcias.conf"
|
||||
CONF="--config /srv/mcias/mcias.toml"
|
||||
|
||||
mciasdb $CONF schema verify
|
||||
mciasdb $CONF account list
|
||||
@@ -217,22 +217,22 @@ Enable the gRPC listener in config:
|
||||
[server]
|
||||
listen_addr = "0.0.0.0:8443"
|
||||
grpc_addr = "0.0.0.0:9443"
|
||||
tls_cert = "/etc/mcias/server.crt"
|
||||
tls_key = "/etc/mcias/server.key"
|
||||
tls_cert = "/srv/mcias/server.crt"
|
||||
tls_key = "/srv/mcias/server.key"
|
||||
```
|
||||
|
||||
Using mciasgrpcctl:
|
||||
|
||||
```sh
|
||||
export MCIAS_TOKEN=$ADMIN_JWT
|
||||
mciasgrpcctl -server auth.example.com:9443 -cacert /etc/mcias/server.crt health
|
||||
mciasgrpcctl -server auth.example.com:9443 -cacert /srv/mcias/server.crt health
|
||||
mciasgrpcctl account list
|
||||
```
|
||||
|
||||
Using grpcurl:
|
||||
|
||||
```sh
|
||||
grpcurl -cacert /etc/mcias/server.crt \
|
||||
grpcurl -cacert /srv/mcias/server.crt \
|
||||
-H "authorization: Bearer $ADMIN_JWT" \
|
||||
auth.example.com:9443 \
|
||||
mcias.v1.AdminService/Health
|
||||
@@ -265,14 +265,13 @@ See [ARCHITECTURE.md](ARCHITECTURE.md) §8 (Web Management UI) for design detail
|
||||
```sh
|
||||
make docker
|
||||
|
||||
mkdir -p /srv/mcias/config
|
||||
cp dist/mcias.conf.docker.example /srv/mcias/config/mcias.conf
|
||||
$EDITOR /srv/mcias/config/mcias.conf
|
||||
mkdir -p /srv/mcias
|
||||
cp dist/mcias.conf.docker.example /srv/mcias/mcias.toml
|
||||
$EDITOR /srv/mcias/mcias.toml
|
||||
|
||||
docker run -d \
|
||||
--name mcias \
|
||||
-v /srv/mcias/config:/etc/mcias:ro \
|
||||
-v mcias-data:/data \
|
||||
-v /srv/mcias:/srv/mcias \
|
||||
-e MCIAS_MASTER_PASSPHRASE=your-passphrase \
|
||||
-p 8443:8443 \
|
||||
-p 9443:9443 \
|
||||
|
||||
464
RUNBOOK.md
Normal file
464
RUNBOOK.md
Normal file
@@ -0,0 +1,464 @@
|
||||
# MCIAS Runbook
|
||||
|
||||
Operational procedures for running and maintaining the MCIAS authentication
|
||||
server. All required files live under `/srv/mcias`.
|
||||
|
||||
---
|
||||
|
||||
## Directory Layout
|
||||
|
||||
```
|
||||
/srv/mcias/
|
||||
mcias.toml — server configuration (TOML)
|
||||
server.crt — TLS certificate (PEM)
|
||||
server.key — TLS private key (PEM, mode 0640)
|
||||
mcias.db — SQLite database (WAL mode creates .db-wal and .db-shm)
|
||||
env — environment file: MCIAS_MASTER_PASSPHRASE (mode 0640)
|
||||
master.key — optional raw AES-256 key file (mode 0640, alternative to env)
|
||||
```
|
||||
|
||||
All files are owned by the `mcias` system user and group (`mcias:mcias`).
|
||||
The directory itself is mode `0750`.
|
||||
|
||||
---
|
||||
|
||||
## Installation
|
||||
|
||||
Run as root from the repository root after `make build`:
|
||||
|
||||
```sh
|
||||
sh dist/install.sh
|
||||
```
|
||||
|
||||
This script is idempotent. It:
|
||||
1. Creates the `mcias` system user and group if they do not exist.
|
||||
2. Installs binaries to `/usr/local/bin/`.
|
||||
3. Creates `/srv/mcias/` with correct ownership and permissions.
|
||||
4. Installs the systemd service unit to `/etc/systemd/system/mcias.service`.
|
||||
5. Installs example config files to `/srv/mcias/` (will not overwrite existing files).
|
||||
|
||||
After installation, complete the steps below before starting the service.
|
||||
|
||||
---
|
||||
|
||||
## First-Run Setup
|
||||
|
||||
### 1. Generate a TLS certificate
|
||||
|
||||
**Self-signed (personal/development use):**
|
||||
|
||||
```sh
|
||||
openssl req -x509 -newkey ed25519 -days 3650 \
|
||||
-keyout /srv/mcias/server.key \
|
||||
-out /srv/mcias/server.crt \
|
||||
-subj "/CN=auth.example.com" \
|
||||
-nodes
|
||||
chmod 0640 /srv/mcias/server.key
|
||||
chown mcias:mcias /srv/mcias/server.key /srv/mcias/server.crt
|
||||
```
|
||||
|
||||
**Using the `cert` tool:**
|
||||
|
||||
```sh
|
||||
go install github.com/kisom/cert@latest
|
||||
|
||||
cat > /tmp/request.yaml <<EOF
|
||||
subject:
|
||||
common_name: auth.example.com
|
||||
hosts:
|
||||
- auth.example.com
|
||||
key:
|
||||
algo: ecdsa
|
||||
size: 521
|
||||
ca:
|
||||
expiry: 87600h
|
||||
EOF
|
||||
|
||||
cert genkey -a ec -s 521 > /srv/mcias/server.key
|
||||
cert selfsign -p /srv/mcias/server.key -f /tmp/request.yaml > /srv/mcias/server.crt
|
||||
chmod 0640 /srv/mcias/server.key
|
||||
chown mcias:mcias /srv/mcias/server.key /srv/mcias/server.crt
|
||||
rm /tmp/request.yaml
|
||||
```
|
||||
|
||||
### 2. Write the configuration file
|
||||
|
||||
```sh
|
||||
cp /srv/mcias/mcias.conf.example /srv/mcias/mcias.toml
|
||||
$EDITOR /srv/mcias/mcias.toml
|
||||
chmod 0640 /srv/mcias/mcias.toml
|
||||
chown mcias:mcias /srv/mcias/mcias.toml
|
||||
```
|
||||
|
||||
Minimum required settings:
|
||||
|
||||
```toml
|
||||
[server]
|
||||
listen_addr = "0.0.0.0:8443"
|
||||
tls_cert = "/srv/mcias/server.crt"
|
||||
tls_key = "/srv/mcias/server.key"
|
||||
|
||||
[database]
|
||||
path = "/srv/mcias/mcias.db"
|
||||
|
||||
[tokens]
|
||||
issuer = "https://auth.example.com"
|
||||
|
||||
[master_key]
|
||||
passphrase_env = "MCIAS_MASTER_PASSPHRASE"
|
||||
```
|
||||
|
||||
See `dist/mcias.conf.example` for the full annotated reference.
|
||||
|
||||
### 3. Set the master key passphrase
|
||||
|
||||
```sh
|
||||
cp /srv/mcias/mcias.env.example /srv/mcias/env
|
||||
$EDITOR /srv/mcias/env # set MCIAS_MASTER_PASSPHRASE to a long random value
|
||||
chmod 0640 /srv/mcias/env
|
||||
chown mcias:mcias /srv/mcias/env
|
||||
```
|
||||
|
||||
Generate a strong passphrase:
|
||||
|
||||
```sh
|
||||
openssl rand -base64 32
|
||||
```
|
||||
|
||||
> **IMPORTANT:** Back up the passphrase to a secure offline location.
|
||||
> Losing it permanently destroys access to all encrypted data in the database.
|
||||
|
||||
### 4. Create the first admin account
|
||||
|
||||
```sh
|
||||
export MCIAS_MASTER_PASSPHRASE=your-passphrase
|
||||
|
||||
mciasdb --config /srv/mcias/mcias.toml account create \
|
||||
--username admin --type human
|
||||
# note the UUID printed
|
||||
|
||||
mciasdb --config /srv/mcias/mcias.toml account set-password --id <UUID>
|
||||
mciasdb --config /srv/mcias/mcias.toml role grant --id <UUID> --role admin
|
||||
```
|
||||
|
||||
### 5. Enable and start the service
|
||||
|
||||
```sh
|
||||
systemctl enable mcias
|
||||
systemctl start mcias
|
||||
systemctl status mcias
|
||||
```
|
||||
|
||||
### 6. Verify
|
||||
|
||||
```sh
|
||||
curl -k https://auth.example.com:8443/v1/health
|
||||
# {"status":"ok"}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Routine Operations
|
||||
|
||||
### Start / stop / restart
|
||||
|
||||
```sh
|
||||
systemctl start mcias
|
||||
systemctl stop mcias
|
||||
systemctl restart mcias
|
||||
```
|
||||
|
||||
### View logs
|
||||
|
||||
```sh
|
||||
journalctl -u mcias -f
|
||||
journalctl -u mcias --since "1 hour ago"
|
||||
```
|
||||
|
||||
### Check service status
|
||||
|
||||
```sh
|
||||
systemctl status mcias
|
||||
```
|
||||
|
||||
### Reload configuration
|
||||
|
||||
The server reads its configuration at startup only. To apply config changes:
|
||||
|
||||
```sh
|
||||
systemctl restart mcias
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Account Management
|
||||
|
||||
All account management can be done via `mciasctl` (REST API) when the server
|
||||
is running, or `mciasdb` for offline/break-glass operations.
|
||||
|
||||
```sh
|
||||
# Set env for offline tool
|
||||
export MCIAS_MASTER_PASSPHRASE=your-passphrase
|
||||
CONF="--config /srv/mcias/mcias.toml"
|
||||
|
||||
# List accounts
|
||||
mciasdb $CONF account list
|
||||
|
||||
# Create account
|
||||
mciasdb $CONF account create --username alice --type human
|
||||
|
||||
# Set password (prompts interactively)
|
||||
mciasdb $CONF account set-password --id <UUID>
|
||||
|
||||
# Grant or revoke a role
|
||||
mciasdb $CONF role grant --id <UUID> --role admin
|
||||
mciasdb $CONF role revoke --id <UUID> --role admin
|
||||
|
||||
# Disable account
|
||||
mciasdb $CONF account set-status --id <UUID> --status inactive
|
||||
|
||||
# Delete account
|
||||
mciasdb $CONF account set-status --id <UUID> --status deleted
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Token Management
|
||||
|
||||
```sh
|
||||
CONF="--config /srv/mcias/mcias.toml"
|
||||
|
||||
# List active tokens for an account
|
||||
mciasdb $CONF token list --id <UUID>
|
||||
|
||||
# Revoke a specific token by JTI
|
||||
mciasdb $CONF token revoke --jti <JTI>
|
||||
|
||||
# Revoke all tokens for an account (e.g., suspected compromise)
|
||||
mciasdb $CONF token revoke-all --id <UUID>
|
||||
|
||||
# Prune expired tokens from the database
|
||||
mciasdb $CONF prune tokens
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Database Maintenance
|
||||
|
||||
### Verify schema
|
||||
|
||||
```sh
|
||||
mciasdb --config /srv/mcias/mcias.toml schema verify
|
||||
```
|
||||
|
||||
### Run pending migrations
|
||||
|
||||
```sh
|
||||
mciasdb --config /srv/mcias/mcias.toml schema migrate
|
||||
```
|
||||
|
||||
### Force schema version (break-glass)
|
||||
|
||||
```sh
|
||||
mciasdb --config /srv/mcias/mcias.toml schema force --version N
|
||||
```
|
||||
|
||||
Use only when `schema migrate` reports a dirty version after a failed migration.
|
||||
|
||||
### Backup the database
|
||||
|
||||
SQLite WAL mode creates three files. Back up all three atomically using the
|
||||
SQLite backup API or by stopping the server first:
|
||||
|
||||
```sh
|
||||
# Online backup (preferred — no downtime):
|
||||
sqlite3 /srv/mcias/mcias.db ".backup /path/to/backup/mcias-$(date +%F).db"
|
||||
|
||||
# Offline backup:
|
||||
systemctl stop mcias
|
||||
cp /srv/mcias/mcias.db /path/to/backup/mcias-$(date +%F).db
|
||||
systemctl start mcias
|
||||
```
|
||||
|
||||
Store backups alongside a copy of the master key passphrase in a secure
|
||||
offline location. A database backup without the passphrase is unrecoverable.
|
||||
|
||||
---
|
||||
|
||||
## Audit Log
|
||||
|
||||
```sh
|
||||
CONF="--config /srv/mcias/mcias.toml"
|
||||
|
||||
# Show last 50 audit events
|
||||
mciasdb $CONF audit tail --n 50
|
||||
|
||||
# Query by account
|
||||
mciasdb $CONF audit query --account <UUID>
|
||||
|
||||
# Query by event type since a given time
|
||||
mciasdb $CONF audit query --type login_failure --since 2026-01-01T00:00:00Z
|
||||
|
||||
# Output as JSON (for log shipping)
|
||||
mciasdb $CONF audit query --json
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Upgrading
|
||||
|
||||
1. Build the new binaries: `make build`
|
||||
2. Stop the service: `systemctl stop mcias`
|
||||
3. Install new binaries: `sh dist/install.sh`
|
||||
- The script will not overwrite existing config files.
|
||||
- New example files are placed with a `.new` suffix for review.
|
||||
4. Review any `.new` config files in `/srv/mcias/` and merge changes manually.
|
||||
5. Run schema migrations if required:
|
||||
```sh
|
||||
mciasdb --config /srv/mcias/mcias.toml schema migrate
|
||||
```
|
||||
6. Start the service: `systemctl start mcias`
|
||||
7. Verify: `curl -k https://auth.example.com:8443/v1/health`
|
||||
|
||||
---
|
||||
|
||||
## Master Key Rotation
|
||||
|
||||
> This operation is not yet automated. Until a rotation command is
|
||||
> implemented, rotation requires a full re-encryption of the database.
|
||||
> Contact the project maintainer for the current procedure.
|
||||
|
||||
---
|
||||
|
||||
## TLS Certificate Renewal
|
||||
|
||||
Replace the certificate and key files, then restart the server:
|
||||
|
||||
```sh
|
||||
# Generate or obtain new cert/key, then:
|
||||
cp new-server.crt /srv/mcias/server.crt
|
||||
cp new-server.key /srv/mcias/server.key
|
||||
chmod 0640 /srv/mcias/server.key
|
||||
chown mcias:mcias /srv/mcias/server.crt /srv/mcias/server.key
|
||||
systemctl restart mcias
|
||||
```
|
||||
|
||||
For Let's Encrypt with Certbot, add a deploy hook:
|
||||
|
||||
```sh
|
||||
# /etc/letsencrypt/renewal-hooks/deploy/mcias.sh
|
||||
#!/bin/sh
|
||||
cp /etc/letsencrypt/live/auth.example.com/fullchain.pem /srv/mcias/server.crt
|
||||
cp /etc/letsencrypt/live/auth.example.com/privkey.pem /srv/mcias/server.key
|
||||
chmod 0640 /srv/mcias/server.key
|
||||
chown mcias:mcias /srv/mcias/server.crt /srv/mcias/server.key
|
||||
systemctl restart mcias
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Docker Deployment
|
||||
|
||||
```sh
|
||||
make docker
|
||||
|
||||
mkdir -p /srv/mcias
|
||||
cp dist/mcias.conf.docker.example /srv/mcias/mcias.toml
|
||||
$EDITOR /srv/mcias/mcias.toml
|
||||
|
||||
# Place TLS cert and key under /srv/mcias/
|
||||
# Set ownership so uid 10001 (container mcias user) can read them.
|
||||
chown -R 10001:10001 /srv/mcias
|
||||
|
||||
docker run -d \
|
||||
--name mcias \
|
||||
-v /srv/mcias:/srv/mcias \
|
||||
-e MCIAS_MASTER_PASSPHRASE=your-passphrase \
|
||||
-p 8443:8443 \
|
||||
-p 9443:9443 \
|
||||
--restart unless-stopped \
|
||||
mcias:latest
|
||||
```
|
||||
|
||||
See `dist/mcias.conf.docker.example` for the full annotated Docker config.
|
||||
|
||||
---
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Server fails to start: "open database"
|
||||
|
||||
Check that `/srv/mcias/` is writable by the `mcias` user:
|
||||
|
||||
```sh
|
||||
ls -la /srv/mcias/
|
||||
stat /srv/mcias/mcias.db # if it already exists
|
||||
```
|
||||
|
||||
Fix: `chown mcias:mcias /srv/mcias`
|
||||
|
||||
### Server fails to start: "environment variable ... is not set"
|
||||
|
||||
The `MCIAS_MASTER_PASSPHRASE` env var is missing. Ensure `/srv/mcias/env`
|
||||
exists, is readable by the mcias user, and contains the correct variable:
|
||||
|
||||
```sh
|
||||
grep MCIAS_MASTER_PASSPHRASE /srv/mcias/env
|
||||
```
|
||||
|
||||
Also confirm the systemd unit loads it:
|
||||
|
||||
```sh
|
||||
systemctl cat mcias | grep EnvironmentFile
|
||||
```
|
||||
|
||||
### Server fails to start: "decrypt signing key"
|
||||
|
||||
The master key passphrase has changed or is wrong. The passphrase must match
|
||||
the one used when the database was first initialized (the KDF salt is stored
|
||||
in the database). Restore the correct passphrase from your offline backup.
|
||||
|
||||
### TLS errors in client connections
|
||||
|
||||
Verify the certificate is valid and covers the correct hostname:
|
||||
|
||||
```sh
|
||||
openssl x509 -in /srv/mcias/server.crt -noout -text | grep -E "Subject|DNS"
|
||||
openssl x509 -in /srv/mcias/server.crt -noout -dates
|
||||
```
|
||||
|
||||
### Database locked / WAL not cleaning up
|
||||
|
||||
Check for lingering `mcias.db-wal` and `mcias.db-shm` files after an unclean
|
||||
shutdown. These are safe to leave in place — SQLite will recover on next open.
|
||||
Do not delete them while the server is running.
|
||||
|
||||
### Schema dirty after failed migration
|
||||
|
||||
```sh
|
||||
mciasdb --config /srv/mcias/mcias.toml schema verify
|
||||
mciasdb --config /srv/mcias/mcias.toml schema force --version N
|
||||
mciasdb --config /srv/mcias/mcias.toml schema migrate
|
||||
```
|
||||
|
||||
Replace `N` with the last successfully applied version number.
|
||||
|
||||
---
|
||||
|
||||
## File Permissions Reference
|
||||
|
||||
| Path | Mode | Owner |
|
||||
|------|------|-------|
|
||||
| `/srv/mcias/` | `0750` | `mcias:mcias` |
|
||||
| `/srv/mcias/mcias.toml` | `0640` | `mcias:mcias` |
|
||||
| `/srv/mcias/server.crt` | `0644` | `mcias:mcias` |
|
||||
| `/srv/mcias/server.key` | `0640` | `mcias:mcias` |
|
||||
| `/srv/mcias/mcias.db` | `0640` | `mcias:mcias` |
|
||||
| `/srv/mcias/env` | `0640` | `mcias:mcias` |
|
||||
| `/srv/mcias/master.key` | `0640` | `mcias:mcias` |
|
||||
|
||||
Verify permissions:
|
||||
|
||||
```sh
|
||||
ls -la /srv/mcias/
|
||||
```
|
||||
@@ -15,10 +15,10 @@ go get git.wntrmute.dev/kyle/mcias/clients/go
|
||||
## Quick Start
|
||||
|
||||
```go
|
||||
import mciasgoclient "git.wntrmute.dev/kyle/mcias/clients/go"
|
||||
import "git.wntrmute.dev/kyle/mcias/clients/go/mcias"
|
||||
|
||||
// Connect to the MCIAS server.
|
||||
client, err := mciasgoclient.New("https://auth.example.com", mciasgoclient.Options{})
|
||||
client, err := mcias.New("https://auth.example.com", mcias.Options{})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -43,7 +43,7 @@ if err := client.Logout(); err != nil {
|
||||
## Custom CA Certificate
|
||||
|
||||
```go
|
||||
client, err := mciasgoclient.New("https://auth.example.com", mciasgoclient.Options{
|
||||
client, err := mcias.New("https://auth.example.com", mcias.Options{
|
||||
CACertPath: "/etc/mcias/ca.pem",
|
||||
})
|
||||
```
|
||||
@@ -55,17 +55,17 @@ All methods return typed errors:
|
||||
```go
|
||||
_, _, err := client.Login("alice", "wrongpass", "")
|
||||
switch {
|
||||
case errors.Is(err, new(mciasgoclient.MciasAuthError)):
|
||||
case errors.Is(err, new(mcias.MciasAuthError)):
|
||||
// 401 — wrong credentials or token invalid
|
||||
case errors.Is(err, new(mciasgoclient.MciasForbiddenError)):
|
||||
case errors.Is(err, new(mcias.MciasForbiddenError)):
|
||||
// 403 — insufficient role
|
||||
case errors.Is(err, new(mciasgoclient.MciasNotFoundError)):
|
||||
case errors.Is(err, new(mcias.MciasNotFoundError)):
|
||||
// 404 — resource not found
|
||||
case errors.Is(err, new(mciasgoclient.MciasInputError)):
|
||||
case errors.Is(err, new(mcias.MciasInputError)):
|
||||
// 400 — malformed request
|
||||
case errors.Is(err, new(mciasgoclient.MciasConflictError)):
|
||||
case errors.Is(err, new(mcias.MciasConflictError)):
|
||||
// 409 — conflict (e.g. duplicate username)
|
||||
case errors.Is(err, new(mciasgoclient.MciasServerError)):
|
||||
case errors.Is(err, new(mcias.MciasServerError)):
|
||||
// 5xx — unexpected server error
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// Package mciasgoclient provides a thread-safe Go client for the MCIAS REST API.
|
||||
// Package mcias provides a thread-safe Go client for the MCIAS REST API.
|
||||
//
|
||||
// Security: bearer tokens are stored under a sync.RWMutex and are never written
|
||||
// to logs or included in error messages anywhere in this package.
|
||||
package mciasgoclient
|
||||
package mcias
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -28,7 +28,7 @@ type MciasError struct {
|
||||
}
|
||||
|
||||
func (e *MciasError) Error() string {
|
||||
return fmt.Sprintf("mciasgoclient: HTTP %d: %s", e.StatusCode, e.Message)
|
||||
return fmt.Sprintf("mcias: HTTP %d: %s", e.StatusCode, e.Message)
|
||||
}
|
||||
|
||||
// MciasAuthError is returned for 401 Unauthorized responses.
|
||||
@@ -77,6 +77,7 @@ type PublicKey struct {
|
||||
type TokenClaims struct {
|
||||
Valid bool `json:"valid"`
|
||||
Sub string `json:"sub,omitempty"`
|
||||
Username string `json:"username,omitempty"`
|
||||
Roles []string `json:"roles,omitempty"`
|
||||
ExpiresAt string `json:"expires_at,omitempty"`
|
||||
}
|
||||
@@ -401,9 +402,15 @@ func (c *Client) RenewToken() (token, expiresAt string, err error) {
|
||||
// Returns a base32 secret and an otpauth:// URI for QR-code generation.
|
||||
// The secret is shown once; it is not retrievable after this call.
|
||||
// TOTP is not enforced until confirmed via ConfirmTOTP.
|
||||
func (c *Client) EnrollTOTP() (*TOTPEnrollResponse, error) {
|
||||
//
|
||||
// Security (SEC-01): the current password is required to prevent a stolen
|
||||
// session token from being used to enroll attacker-controlled TOTP.
|
||||
func (c *Client) EnrollTOTP(password string) (*TOTPEnrollResponse, error) {
|
||||
var resp TOTPEnrollResponse
|
||||
if err := c.do(http.MethodPost, "/v1/auth/totp/enroll", nil, &resp); err != nil {
|
||||
body := struct {
|
||||
Password string `json:"password"`
|
||||
}{Password: password}
|
||||
if err := c.do(http.MethodPost, "/v1/auth/totp/enroll", body, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Package mciasgoclient_test provides tests for the MCIAS Go client.
|
||||
// Package mcias_test provides tests for the MCIAS Go client.
|
||||
// All tests use inline httptest.NewServer mocks to keep this module
|
||||
// self-contained (no cross-module imports).
|
||||
package mciasgoclient_test
|
||||
package mcias_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -11,16 +11,16 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
mciasgoclient "git.wntrmute.dev/kyle/mcias/clients/go"
|
||||
mcias "git.wntrmute.dev/kyle/mcias/clients/go"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func newTestClient(t *testing.T, serverURL string) *mciasgoclient.Client {
|
||||
func newTestClient(t *testing.T, serverURL string) *mcias.Client {
|
||||
t.Helper()
|
||||
c, err := mciasgoclient.New(serverURL, mciasgoclient.Options{})
|
||||
c, err := mcias.New(serverURL, mcias.Options{})
|
||||
if err != nil {
|
||||
t.Fatalf("New: %v", err)
|
||||
}
|
||||
@@ -42,7 +42,7 @@ func writeError(w http.ResponseWriter, status int, msg string) {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
c, err := mciasgoclient.New("https://example.com", mciasgoclient.Options{})
|
||||
c, err := mcias.New("https://example.com", mcias.Options{})
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
@@ -52,7 +52,7 @@ func TestNew(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNewWithPresetToken(t *testing.T) {
|
||||
c, err := mciasgoclient.New("https://example.com", mciasgoclient.Options{Token: "preset-tok"})
|
||||
c, err := mcias.New("https://example.com", mcias.Options{Token: "preset-tok"})
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
@@ -62,7 +62,7 @@ func TestNewWithPresetToken(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNewBadCACert(t *testing.T) {
|
||||
_, err := mciasgoclient.New("https://example.com", mciasgoclient.Options{CACertPath: "/nonexistent/ca.pem"})
|
||||
_, err := mcias.New("https://example.com", mcias.Options{CACertPath: "/nonexistent/ca.pem"})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing CA cert file")
|
||||
}
|
||||
@@ -97,7 +97,7 @@ func TestHealthError(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("expected error for 503")
|
||||
}
|
||||
var srvErr *mciasgoclient.MciasServerError
|
||||
var srvErr *mcias.MciasServerError
|
||||
if !errors.As(err, &srvErr) {
|
||||
t.Errorf("expected MciasServerError, got %T: %v", err, err)
|
||||
}
|
||||
@@ -183,7 +183,7 @@ func TestLoginUnauthorized(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("expected error for 401")
|
||||
}
|
||||
var authErr *mciasgoclient.MciasAuthError
|
||||
var authErr *mcias.MciasAuthError
|
||||
if !errors.As(err, &authErr) {
|
||||
t.Errorf("expected MciasAuthError, got %T: %v", err, err)
|
||||
}
|
||||
@@ -275,7 +275,7 @@ func TestEnrollTOTP(t *testing.T) {
|
||||
}))
|
||||
defer srv.Close()
|
||||
c := newTestClient(t, srv.URL)
|
||||
resp, err := c.EnrollTOTP()
|
||||
resp, err := c.EnrollTOTP("testpass123")
|
||||
if err != nil {
|
||||
t.Fatalf("EnrollTOTP: %v", err)
|
||||
}
|
||||
@@ -312,7 +312,7 @@ func TestConfirmTOTPBadCode(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("expected error for bad TOTP code")
|
||||
}
|
||||
var inputErr *mciasgoclient.MciasInputError
|
||||
var inputErr *mcias.MciasInputError
|
||||
if !errors.As(err, &inputErr) {
|
||||
t.Errorf("expected MciasInputError, got %T: %v", err, err)
|
||||
}
|
||||
@@ -347,7 +347,7 @@ func TestChangePasswordWrongCurrent(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("expected error for wrong current password")
|
||||
}
|
||||
var authErr *mciasgoclient.MciasAuthError
|
||||
var authErr *mcias.MciasAuthError
|
||||
if !errors.As(err, &authErr) {
|
||||
t.Errorf("expected MciasAuthError, got %T: %v", err, err)
|
||||
}
|
||||
@@ -456,7 +456,7 @@ func TestCreateAccountConflict(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("expected error for 409")
|
||||
}
|
||||
var conflictErr *mciasgoclient.MciasConflictError
|
||||
var conflictErr *mcias.MciasConflictError
|
||||
if !errors.As(err, &conflictErr) {
|
||||
t.Errorf("expected MciasConflictError, got %T: %v", err, err)
|
||||
}
|
||||
@@ -801,7 +801,7 @@ func TestListAudit(t *testing.T) {
|
||||
}))
|
||||
defer srv.Close()
|
||||
c := newTestClient(t, srv.URL)
|
||||
resp, err := c.ListAudit(mciasgoclient.AuditFilter{})
|
||||
resp, err := c.ListAudit(mcias.AuditFilter{})
|
||||
if err != nil {
|
||||
t.Fatalf("ListAudit: %v", err)
|
||||
}
|
||||
@@ -827,7 +827,7 @@ func TestListAuditWithFilter(t *testing.T) {
|
||||
}))
|
||||
defer srv.Close()
|
||||
c := newTestClient(t, srv.URL)
|
||||
_, err := c.ListAudit(mciasgoclient.AuditFilter{
|
||||
_, err := c.ListAudit(mcias.AuditFilter{
|
||||
Limit: 10, Offset: 5, EventType: "login_fail", ActorID: "acct-uuid-1",
|
||||
})
|
||||
if err != nil {
|
||||
@@ -896,10 +896,10 @@ func TestCreatePolicyRule(t *testing.T) {
|
||||
}))
|
||||
defer srv.Close()
|
||||
c := newTestClient(t, srv.URL)
|
||||
rule, err := c.CreatePolicyRule(mciasgoclient.CreatePolicyRuleRequest{
|
||||
rule, err := c.CreatePolicyRule(mcias.CreatePolicyRuleRequest{
|
||||
Description: "Test rule",
|
||||
Priority: 50,
|
||||
Rule: mciasgoclient.PolicyRuleBody{Effect: "deny"},
|
||||
Rule: mcias.PolicyRuleBody{Effect: "deny"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreatePolicyRule: %v", err)
|
||||
@@ -950,7 +950,7 @@ func TestGetPolicyRuleNotFound(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("expected error for 404")
|
||||
}
|
||||
var notFoundErr *mciasgoclient.MciasNotFoundError
|
||||
var notFoundErr *mcias.MciasNotFoundError
|
||||
if !errors.As(err, ¬FoundErr) {
|
||||
t.Errorf("expected MciasNotFoundError, got %T: %v", err, err)
|
||||
}
|
||||
@@ -976,7 +976,7 @@ func TestUpdatePolicyRule(t *testing.T) {
|
||||
}))
|
||||
defer srv.Close()
|
||||
c := newTestClient(t, srv.URL)
|
||||
rule, err := c.UpdatePolicyRule(7, mciasgoclient.UpdatePolicyRuleRequest{Enabled: &enabled})
|
||||
rule, err := c.UpdatePolicyRule(7, mcias.UpdatePolicyRuleRequest{Enabled: &enabled})
|
||||
if err != nil {
|
||||
t.Fatalf("UpdatePolicyRule: %v", err)
|
||||
}
|
||||
@@ -1073,7 +1073,7 @@ func TestIntegration(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("expected error for wrong credentials")
|
||||
}
|
||||
var authErr *mciasgoclient.MciasAuthError
|
||||
var authErr *mcias.MciasAuthError
|
||||
if !errors.As(err, &authErr) {
|
||||
t.Errorf("expected MciasAuthError, got %T", err)
|
||||
}
|
||||
|
||||
@@ -148,11 +148,15 @@ class Client:
|
||||
expires_at = str(data["expires_at"])
|
||||
self.token = token
|
||||
return token, expires_at
|
||||
def enroll_totp(self) -> tuple[str, str]:
|
||||
def enroll_totp(self, password: str) -> tuple[str, str]:
|
||||
"""POST /v1/auth/totp/enroll — begin TOTP enrollment.
|
||||
|
||||
Security (SEC-01): current password is required to prevent session-theft
|
||||
escalation to persistent account takeover.
|
||||
|
||||
Returns (secret, otpauth_uri). The secret is shown only once.
|
||||
"""
|
||||
data = self._request("POST", "/v1/auth/totp/enroll")
|
||||
data = self._request("POST", "/v1/auth/totp/enroll", json={"password": password})
|
||||
assert data is not None
|
||||
return str(data["secret"]), str(data["otpauth_uri"])
|
||||
def confirm_totp(self, code: str) -> None:
|
||||
|
||||
@@ -191,7 +191,7 @@ def test_enroll_totp(admin_client: Client) -> None:
|
||||
json={"secret": "JBSWY3DPEHPK3PXP", "otpauth_uri": "otpauth://totp/MCIAS:alice?secret=JBSWY3DPEHPK3PXP&issuer=MCIAS"},
|
||||
)
|
||||
)
|
||||
secret, uri = admin_client.enroll_totp()
|
||||
secret, uri = admin_client.enroll_totp("testpass123")
|
||||
assert secret == "JBSWY3DPEHPK3PXP"
|
||||
assert "otpauth://totp/" in uri
|
||||
@respx.mock
|
||||
|
||||
@@ -484,9 +484,12 @@ impl Client {
|
||||
|
||||
/// Begin TOTP enrollment. Returns `(secret, otpauth_uri)`.
|
||||
/// The secret is shown once; store it in an authenticator app immediately.
|
||||
pub async fn enroll_totp(&self) -> Result<(String, String), MciasError> {
|
||||
///
|
||||
/// Security (SEC-01): current password is required to prevent session-theft
|
||||
/// escalation to persistent account takeover.
|
||||
pub async fn enroll_totp(&self, password: &str) -> Result<(String, String), MciasError> {
|
||||
let resp: TotpEnrollResponse =
|
||||
self.post("/v1/auth/totp/enroll", &serde_json::json!({})).await?;
|
||||
self.post("/v1/auth/totp/enroll", &serde_json::json!({"password": password})).await?;
|
||||
Ok((resp.secret, resp.otpauth_uri))
|
||||
}
|
||||
|
||||
|
||||
@@ -449,7 +449,7 @@ async fn test_enroll_totp() {
|
||||
.await;
|
||||
|
||||
let c = admin_client(&server).await;
|
||||
let (secret, uri) = c.enroll_totp().await.unwrap();
|
||||
let (secret, uri) = c.enroll_totp("testpass123").await.unwrap();
|
||||
assert_eq!(secret, "JBSWY3DPEHPK3PXP");
|
||||
assert!(uri.starts_with("otpauth://totp/"));
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
// token issue -id UUID
|
||||
// token revoke -jti JTI
|
||||
//
|
||||
// pgcreds list
|
||||
// pgcreds set -id UUID -host HOST [-port PORT] -db DB -user USER [-password PASS]
|
||||
// pgcreds get -id UUID
|
||||
//
|
||||
@@ -526,9 +527,11 @@ func (c *controller) tokenRevoke(args []string) {
|
||||
|
||||
func (c *controller) runPGCreds(args []string) {
|
||||
if len(args) == 0 {
|
||||
fatalf("pgcreds requires a subcommand: get, set")
|
||||
fatalf("pgcreds requires a subcommand: list, get, set")
|
||||
}
|
||||
switch args[0] {
|
||||
case "list":
|
||||
c.pgCredsList(args[1:])
|
||||
case "get":
|
||||
c.pgCredsGet(args[1:])
|
||||
case "set":
|
||||
@@ -538,6 +541,15 @@ func (c *controller) runPGCreds(args []string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *controller) pgCredsList(args []string) {
|
||||
fs := flag.NewFlagSet("pgcreds list", flag.ExitOnError)
|
||||
_ = fs.Parse(args)
|
||||
|
||||
var result json.RawMessage
|
||||
c.doRequest("GET", "/v1/pgcreds", nil, &result)
|
||||
printJSON(result)
|
||||
}
|
||||
|
||||
func (c *controller) pgCredsGet(args []string) {
|
||||
fs := flag.NewFlagSet("pgcreds get", flag.ExitOnError)
|
||||
id := fs.String("id", "", "account UUID (required)")
|
||||
@@ -943,6 +955,7 @@ Commands:
|
||||
token issue -id UUID
|
||||
token revoke -jti JTI
|
||||
|
||||
pgcreds list
|
||||
pgcreds get -id UUID
|
||||
pgcreds set -id UUID -host HOST [-port PORT] -db DB -user USER [-password PASS]
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// mciasdb --config /etc/mcias/mcias.toml <command> [subcommand] [flags]
|
||||
// mciasdb --config /srv/mcias/mcias.toml <command> [subcommand] [flags]
|
||||
//
|
||||
// Commands:
|
||||
//
|
||||
@@ -39,6 +39,8 @@
|
||||
//
|
||||
// pgcreds get --id UUID
|
||||
// pgcreds set --id UUID --host H --port P --db D --user U
|
||||
//
|
||||
// rekey
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -53,7 +55,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
configPath := flag.String("config", "mcias.toml", "path to TOML configuration file")
|
||||
configPath := flag.String("config", "/srv/mcias/mcias.toml", "path to TOML configuration file")
|
||||
flag.Usage = usage
|
||||
flag.Parse()
|
||||
|
||||
@@ -107,6 +109,8 @@ func main() {
|
||||
tool.runAudit(subArgs)
|
||||
case "pgcreds":
|
||||
tool.runPGCreds(subArgs)
|
||||
case "rekey":
|
||||
tool.runRekey(subArgs)
|
||||
default:
|
||||
fatalf("unknown command %q; run with no args for usage", command)
|
||||
}
|
||||
@@ -259,6 +263,9 @@ Commands:
|
||||
pgcreds set --id UUID --host H [--port P] --db D --user U
|
||||
(password is prompted interactively)
|
||||
|
||||
rekey Re-encrypt all secrets under a new master passphrase
|
||||
(prompts interactively; requires server to be stopped)
|
||||
|
||||
NOTE: mciasdb bypasses the mciassrv API and operates directly on the SQLite
|
||||
file. Use it only when the server is unavailable or for break-glass recovery.
|
||||
All write operations are recorded in the audit log.
|
||||
|
||||
@@ -438,3 +438,141 @@ func TestPGCredsGetNotFound(t *testing.T) {
|
||||
t.Fatal("expected ErrNotFound, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
// ---- rekey command tests ----
|
||||
|
||||
// TestRekeyCommandRoundTrip exercises runRekey end-to-end with real AES-256-GCM
|
||||
// encryption and actual Argon2id key derivation. It verifies that all secrets
|
||||
// (signing key, TOTP, pg password) remain accessible after rekey and that the
|
||||
// old master key no longer decrypts the re-encrypted values.
|
||||
//
|
||||
// Note: Argon2id derivation (time=3, memory=128 MiB) makes this test slow (~2 s).
|
||||
func TestRekeyCommandRoundTrip(t *testing.T) {
|
||||
tool := newTestTool(t)
|
||||
|
||||
// ── Setup: signing key encrypted under old master key ──
|
||||
_, privKey, err := crypto.GenerateEd25519KeyPair()
|
||||
if err != nil {
|
||||
t.Fatalf("generate key pair: %v", err)
|
||||
}
|
||||
sigKeyPEM, err := crypto.MarshalPrivateKeyPEM(privKey)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal key: %v", err)
|
||||
}
|
||||
sigEnc, sigNonce, err := crypto.SealAESGCM(tool.masterKey, sigKeyPEM)
|
||||
if err != nil {
|
||||
t.Fatalf("seal signing key: %v", err)
|
||||
}
|
||||
if err := tool.db.WriteServerConfig(sigEnc, sigNonce); err != nil {
|
||||
t.Fatalf("write server config: %v", err)
|
||||
}
|
||||
// WriteMasterKeySalt so ReadServerConfig has a valid salt row.
|
||||
oldSalt, err := crypto.NewSalt()
|
||||
if err != nil {
|
||||
t.Fatalf("gen salt: %v", err)
|
||||
}
|
||||
if err := tool.db.WriteMasterKeySalt(oldSalt); err != nil {
|
||||
t.Fatalf("write salt: %v", err)
|
||||
}
|
||||
|
||||
// ── Setup: account with TOTP ──
|
||||
a, err := tool.db.CreateAccount("rekeyuser", "human", "")
|
||||
if err != nil {
|
||||
t.Fatalf("create account: %v", err)
|
||||
}
|
||||
totpSecret := []byte("JBSWY3DPEHPK3PXP")
|
||||
totpEnc, totpNonce, err := crypto.SealAESGCM(tool.masterKey, totpSecret)
|
||||
if err != nil {
|
||||
t.Fatalf("seal totp: %v", err)
|
||||
}
|
||||
if err := tool.db.SetTOTP(a.ID, totpEnc, totpNonce); err != nil {
|
||||
t.Fatalf("set totp: %v", err)
|
||||
}
|
||||
|
||||
// ── Setup: pg credentials ──
|
||||
pgPass := []byte("pgpassword123")
|
||||
pgEnc, pgNonce, err := crypto.SealAESGCM(tool.masterKey, pgPass)
|
||||
if err != nil {
|
||||
t.Fatalf("seal pg pass: %v", err)
|
||||
}
|
||||
if err := tool.db.WritePGCredentials(a.ID, "localhost", 5432, "mydb", "myuser", pgEnc, pgNonce); err != nil {
|
||||
t.Fatalf("write pg creds: %v", err)
|
||||
}
|
||||
|
||||
// ── Pipe new passphrase twice into stdin ──
|
||||
const newPassphrase = "new-master-passphrase-for-test"
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatalf("create stdin pipe: %v", err)
|
||||
}
|
||||
origStdin := os.Stdin
|
||||
os.Stdin = r
|
||||
t.Cleanup(func() { os.Stdin = origStdin })
|
||||
if _, err := fmt.Fprintf(w, "%s\n%s\n", newPassphrase, newPassphrase); err != nil {
|
||||
t.Fatalf("write stdin: %v", err)
|
||||
}
|
||||
_ = w.Close()
|
||||
|
||||
// ── Execute rekey ──
|
||||
tool.runRekey(nil)
|
||||
|
||||
// ── Derive new key from stored salt + new passphrase ──
|
||||
newSalt, err := tool.db.ReadMasterKeySalt()
|
||||
if err != nil {
|
||||
t.Fatalf("read new salt: %v", err)
|
||||
}
|
||||
newKey, err := crypto.DeriveKey(newPassphrase, newSalt)
|
||||
if err != nil {
|
||||
t.Fatalf("derive new key: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
for i := range newKey {
|
||||
newKey[i] = 0
|
||||
}
|
||||
}()
|
||||
|
||||
// Signing key must decrypt with new key.
|
||||
newSigEnc, newSigNonce, err := tool.db.ReadServerConfig()
|
||||
if err != nil {
|
||||
t.Fatalf("read server config after rekey: %v", err)
|
||||
}
|
||||
decPEM, err := crypto.OpenAESGCM(newKey, newSigNonce, newSigEnc)
|
||||
if err != nil {
|
||||
t.Fatalf("decrypt signing key with new key: %v", err)
|
||||
}
|
||||
if string(decPEM) != string(sigKeyPEM) {
|
||||
t.Error("signing key PEM mismatch after rekey")
|
||||
}
|
||||
|
||||
// Old key must NOT decrypt the re-encrypted signing key.
|
||||
// Security: adversarial check that old key is invalidated.
|
||||
if _, err := crypto.OpenAESGCM(tool.masterKey, newSigNonce, newSigEnc); err == nil {
|
||||
t.Error("old key still decrypts signing key after rekey — ciphertext was not replaced")
|
||||
}
|
||||
|
||||
// TOTP must decrypt with new key.
|
||||
updatedAcct, err := tool.db.GetAccountByUUID(a.UUID)
|
||||
if err != nil {
|
||||
t.Fatalf("get account after rekey: %v", err)
|
||||
}
|
||||
decTOTP, err := crypto.OpenAESGCM(newKey, updatedAcct.TOTPSecretNonce, updatedAcct.TOTPSecretEnc)
|
||||
if err != nil {
|
||||
t.Fatalf("decrypt TOTP with new key: %v", err)
|
||||
}
|
||||
if string(decTOTP) != string(totpSecret) {
|
||||
t.Errorf("TOTP mismatch: got %q, want %q", decTOTP, totpSecret)
|
||||
}
|
||||
|
||||
// pg password must decrypt with new key.
|
||||
updatedCred, err := tool.db.ReadPGCredentials(a.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("read pg creds after rekey: %v", err)
|
||||
}
|
||||
decPG, err := crypto.OpenAESGCM(newKey, updatedCred.PGPasswordNonce, updatedCred.PGPasswordEnc)
|
||||
if err != nil {
|
||||
t.Fatalf("decrypt pg password with new key: %v", err)
|
||||
}
|
||||
if string(decPG) != string(pgPass) {
|
||||
t.Errorf("pg password mismatch: got %q, want %q", decPG, pgPass)
|
||||
}
|
||||
}
|
||||
|
||||
154
cmd/mciasdb/rekey.go
Normal file
154
cmd/mciasdb/rekey.go
Normal file
@@ -0,0 +1,154 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.wntrmute.dev/kyle/mcias/internal/crypto"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/db"
|
||||
)
|
||||
|
||||
// runRekey re-encrypts all secrets under a new passphrase-derived master key.
|
||||
//
|
||||
// The current master key (already loaded in tool.masterKey by openDB) is used
|
||||
// to decrypt every encrypted secret: the Ed25519 signing key, all TOTP secrets,
|
||||
// and all Postgres credential passwords. The operator is then prompted for a
|
||||
// new passphrase (confirmed), a fresh Argon2id salt is generated, a new 256-bit
|
||||
// master key is derived, and all secrets are re-encrypted and written back in a
|
||||
// single atomic SQLite transaction.
|
||||
//
|
||||
// Security: The entire re-encryption happens in memory first; the database is
|
||||
// only updated once all ciphertext has been produced successfully. The new
|
||||
// salt replaces the old salt atomically within the same transaction so the
|
||||
// database is never left in a mixed state. Both the old and new master keys
|
||||
// are zeroed in deferred cleanup. No secret material is logged or printed.
|
||||
func (t *tool) runRekey(_ []string) {
|
||||
// ── 1. Decrypt signing key under old master key ──────────────────────
|
||||
sigKeyEnc, sigKeyNonce, err := t.db.ReadServerConfig()
|
||||
if err != nil {
|
||||
fatalf("read server config: %v", err)
|
||||
}
|
||||
sigKeyPEM, err := crypto.OpenAESGCM(t.masterKey, sigKeyNonce, sigKeyEnc)
|
||||
if err != nil {
|
||||
fatalf("decrypt signing key: %v", err)
|
||||
}
|
||||
|
||||
// ── 2. Decrypt all TOTP secrets under old master key ─────────────────
|
||||
totpAccounts, err := t.db.ListAccountsWithTOTP()
|
||||
if err != nil {
|
||||
fatalf("list accounts with TOTP: %v", err)
|
||||
}
|
||||
type totpPlain struct {
|
||||
secret []byte
|
||||
accountID int64
|
||||
}
|
||||
totpPlaintexts := make([]totpPlain, 0, len(totpAccounts))
|
||||
for _, a := range totpAccounts {
|
||||
pt, err := crypto.OpenAESGCM(t.masterKey, a.TOTPSecretNonce, a.TOTPSecretEnc)
|
||||
if err != nil {
|
||||
fatalf("decrypt TOTP secret for account %s: %v", a.Username, err)
|
||||
}
|
||||
totpPlaintexts = append(totpPlaintexts, totpPlain{accountID: a.ID, secret: pt})
|
||||
}
|
||||
|
||||
// ── 3. Decrypt all pg_credentials passwords under old master key ──────
|
||||
pgCreds, err := t.db.ListAllPGCredentials()
|
||||
if err != nil {
|
||||
fatalf("list pg credentials: %v", err)
|
||||
}
|
||||
type pgPlain struct {
|
||||
password []byte
|
||||
credID int64
|
||||
}
|
||||
pgPlaintexts := make([]pgPlain, 0, len(pgCreds))
|
||||
for _, c := range pgCreds {
|
||||
pt, err := crypto.OpenAESGCM(t.masterKey, c.PGPasswordNonce, c.PGPasswordEnc)
|
||||
if err != nil {
|
||||
fatalf("decrypt pg password for credential %d: %v", c.ID, err)
|
||||
}
|
||||
pgPlaintexts = append(pgPlaintexts, pgPlain{credID: c.ID, password: pt})
|
||||
}
|
||||
|
||||
// ── 4. Prompt for new passphrase (confirmed) ──────────────────────────
|
||||
fmt.Fprintln(os.Stderr, "Enter new master passphrase (will not echo):")
|
||||
newPassphrase, err := readPassword("New passphrase: ")
|
||||
if err != nil {
|
||||
fatalf("read passphrase: %v", err)
|
||||
}
|
||||
if newPassphrase == "" {
|
||||
fatalf("passphrase must not be empty")
|
||||
}
|
||||
confirm, err := readPassword("Confirm passphrase: ")
|
||||
if err != nil {
|
||||
fatalf("read passphrase confirmation: %v", err)
|
||||
}
|
||||
if newPassphrase != confirm {
|
||||
fatalf("passphrases do not match")
|
||||
}
|
||||
|
||||
// ── 5. Derive new master key ──────────────────────────────────────────
|
||||
// Security: a fresh random salt is generated for every rekey so that the
|
||||
// new key is independent of the old key even if the same passphrase is
|
||||
// reused. The new salt is stored atomically with the re-encrypted secrets.
|
||||
newSalt, err := crypto.NewSalt()
|
||||
if err != nil {
|
||||
fatalf("generate new salt: %v", err)
|
||||
}
|
||||
newKey, err := crypto.DeriveKey(newPassphrase, newSalt)
|
||||
if err != nil {
|
||||
fatalf("derive new master key: %v", err)
|
||||
}
|
||||
// Zero both keys when done, regardless of outcome.
|
||||
defer func() {
|
||||
for i := range newKey {
|
||||
newKey[i] = 0
|
||||
}
|
||||
}()
|
||||
|
||||
// ── 6. Re-encrypt signing key ─────────────────────────────────────────
|
||||
newSigKeyEnc, newSigKeyNonce, err := crypto.SealAESGCM(newKey, sigKeyPEM)
|
||||
if err != nil {
|
||||
fatalf("re-encrypt signing key: %v", err)
|
||||
}
|
||||
|
||||
// ── 7. Re-encrypt TOTP secrets ────────────────────────────────────────
|
||||
totpRows := make([]db.TOTPRekeyRow, 0, len(totpPlaintexts))
|
||||
for _, tp := range totpPlaintexts {
|
||||
enc, nonce, err := crypto.SealAESGCM(newKey, tp.secret)
|
||||
if err != nil {
|
||||
fatalf("re-encrypt TOTP secret for account %d: %v", tp.accountID, err)
|
||||
}
|
||||
totpRows = append(totpRows, db.TOTPRekeyRow{
|
||||
AccountID: tp.accountID,
|
||||
Enc: enc,
|
||||
Nonce: nonce,
|
||||
})
|
||||
}
|
||||
|
||||
// ── 8. Re-encrypt pg_credentials passwords ────────────────────────────
|
||||
pgRows := make([]db.PGRekeyRow, 0, len(pgPlaintexts))
|
||||
for _, pp := range pgPlaintexts {
|
||||
enc, nonce, err := crypto.SealAESGCM(newKey, pp.password)
|
||||
if err != nil {
|
||||
fatalf("re-encrypt pg password for credential %d: %v", pp.credID, err)
|
||||
}
|
||||
pgRows = append(pgRows, db.PGRekeyRow{
|
||||
CredentialID: pp.credID,
|
||||
Enc: enc,
|
||||
Nonce: nonce,
|
||||
})
|
||||
}
|
||||
|
||||
// ── 9. Atomic commit ──────────────────────────────────────────────────
|
||||
if err := t.db.Rekey(newSalt, newSigKeyEnc, newSigKeyNonce, totpRows, pgRows); err != nil {
|
||||
fatalf("rekey database: %v", err)
|
||||
}
|
||||
|
||||
if err := t.db.WriteAuditEvent("master_key_rekeyed", nil, nil, "", `{"actor":"mciasdb"}`); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "warning: write audit event: %v\n", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Rekey complete: %d TOTP secrets and %d pg credentials re-encrypted.\n",
|
||||
len(totpRows), len(pgRows))
|
||||
fmt.Fprintln(os.Stderr, "Update your mcias.toml or passphrase environment variable to use the new passphrase.")
|
||||
}
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// mciassrv -config /etc/mcias/mcias.toml
|
||||
// mciassrv -config /srv/mcias/mcias.toml
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -36,10 +36,11 @@ import (
|
||||
"git.wntrmute.dev/kyle/mcias/internal/db"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/grpcserver"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/server"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/vault"
|
||||
)
|
||||
|
||||
func main() {
|
||||
configPath := flag.String("config", "mcias.toml", "path to TOML configuration file")
|
||||
configPath := flag.String("config", "/srv/mcias/mcias.toml", "path to TOML configuration file")
|
||||
flag.Parse()
|
||||
|
||||
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
|
||||
@@ -72,29 +73,46 @@ func run(configPath string, logger *slog.Logger) error {
|
||||
}
|
||||
logger.Info("database ready", "path", cfg.Database.Path)
|
||||
|
||||
// Derive or load the master encryption key.
|
||||
// Derive or load the master encryption key and build the vault.
|
||||
// Security: The master key encrypts TOTP secrets, Postgres passwords, and
|
||||
// the signing key at rest. It is derived from a passphrase via Argon2id
|
||||
// (or loaded directly from a key file). The KDF salt is stored in the DB
|
||||
// for stability across restarts. The passphrase env var is cleared after use.
|
||||
masterKey, err := loadMasterKey(cfg, database)
|
||||
if err != nil {
|
||||
return fmt.Errorf("load master key: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
// Zero the master key when done — reduces the window of exposure.
|
||||
for i := range masterKey {
|
||||
masterKey[i] = 0
|
||||
//
|
||||
// When the passphrase is not available (empty env var in passphrase mode
|
||||
// with no key file), the server starts in sealed state. The operator must
|
||||
// provide the passphrase via the /v1/vault/unseal API or the /unseal UI page.
|
||||
// First run (no signing key in DB) still requires the passphrase at startup.
|
||||
var v *vault.Vault
|
||||
masterKey, mkErr := loadMasterKey(cfg, database)
|
||||
if mkErr != nil {
|
||||
// Check if we can start sealed (passphrase mode, empty env var).
|
||||
if cfg.MasterKey.KeyFile == "" && os.Getenv(cfg.MasterKey.PassphraseEnv) == "" {
|
||||
// Verify that this is not a first run — the signing key must already exist.
|
||||
enc, nonce, scErr := database.ReadServerConfig()
|
||||
if scErr != nil || enc == nil || nonce == nil {
|
||||
return fmt.Errorf("first run requires passphrase: %w", mkErr)
|
||||
}
|
||||
v = vault.NewSealed()
|
||||
logger.Info("vault starting in sealed state")
|
||||
} else {
|
||||
return fmt.Errorf("load master key: %w", mkErr)
|
||||
}
|
||||
}()
|
||||
|
||||
// Load or generate the Ed25519 signing key.
|
||||
// Security: The private signing key is stored AES-256-GCM encrypted in the
|
||||
// database. On first run it is generated and stored. The key is decrypted
|
||||
// with the master key each startup.
|
||||
privKey, pubKey, err := loadOrGenerateSigningKey(database, masterKey, logger)
|
||||
if err != nil {
|
||||
return fmt.Errorf("signing key: %w", err)
|
||||
} else {
|
||||
// Load or generate the Ed25519 signing key.
|
||||
// Security: The private signing key is stored AES-256-GCM encrypted in the
|
||||
// database. On first run it is generated and stored. The key is decrypted
|
||||
// with the master key each startup.
|
||||
privKey, pubKey, err := loadOrGenerateSigningKey(database, masterKey, logger)
|
||||
if err != nil {
|
||||
// Zero master key on failure.
|
||||
for i := range masterKey {
|
||||
masterKey[i] = 0
|
||||
}
|
||||
return fmt.Errorf("signing key: %w", err)
|
||||
}
|
||||
v = vault.NewUnsealed(masterKey, privKey, pubKey)
|
||||
logger.Info("vault unsealed at startup")
|
||||
}
|
||||
|
||||
// Configure TLS. We require TLS 1.2+ and prefer TLS 1.3.
|
||||
@@ -108,8 +126,8 @@ func run(configPath string, logger *slog.Logger) error {
|
||||
},
|
||||
}
|
||||
|
||||
// Build the REST handler.
|
||||
restSrv := server.New(database, cfg, privKey, pubKey, masterKey, logger)
|
||||
// Build the REST handler. All servers share the same vault by pointer.
|
||||
restSrv := server.New(database, cfg, v, logger)
|
||||
httpServer := &http.Server{
|
||||
Addr: cfg.Server.ListenAddr,
|
||||
Handler: restSrv.Handler(),
|
||||
@@ -131,7 +149,7 @@ func run(configPath string, logger *slog.Logger) error {
|
||||
return fmt.Errorf("load gRPC TLS credentials: %w", err)
|
||||
}
|
||||
|
||||
grpcSrvImpl := grpcserver.New(database, cfg, privKey, pubKey, masterKey, logger)
|
||||
grpcSrvImpl := grpcserver.New(database, cfg, v, logger)
|
||||
// Build server directly with TLS credentials. GRPCServerWithCreds builds
|
||||
// the server with transport credentials at construction time per gRPC idiom.
|
||||
grpcSrv = rebuildGRPCServerWithTLS(grpcSrvImpl, grpcTLSCreds)
|
||||
|
||||
51
dist/install.sh
vendored
51
dist/install.sh
vendored
@@ -6,7 +6,7 @@
|
||||
# This script must be run as root. It:
|
||||
# 1. Creates the mcias system user and group (idempotent).
|
||||
# 2. Copies binaries to /usr/local/bin/.
|
||||
# 3. Creates /etc/mcias/ and /var/lib/mcias/ with correct permissions.
|
||||
# 3. Creates /srv/mcias/ with correct permissions.
|
||||
# 4. Installs the systemd service unit.
|
||||
# 5. Prints post-install instructions.
|
||||
#
|
||||
@@ -25,8 +25,7 @@ set -eu
|
||||
# Configuration
|
||||
# ---------------------------------------------------------------------------
|
||||
BIN_DIR="/usr/local/bin"
|
||||
CONF_DIR="/etc/mcias"
|
||||
DATA_DIR="/var/lib/mcias"
|
||||
SRV_DIR="/srv/mcias"
|
||||
MAN_DIR="/usr/share/man/man1"
|
||||
SYSTEMD_DIR="/etc/systemd/system"
|
||||
SERVICE_USER="mcias"
|
||||
@@ -114,23 +113,19 @@ for bin in mciassrv mciasctl mciasdb mciasgrpcctl; do
|
||||
install -m 0755 -o root -g root "$src" "$BIN_DIR/$bin"
|
||||
done
|
||||
|
||||
# Step 3: Create configuration directory.
|
||||
info "Creating $CONF_DIR"
|
||||
install -d -m 0750 -o root -g "$SERVICE_GROUP" "$CONF_DIR"
|
||||
# Step 3: Create service directory.
|
||||
info "Creating $SRV_DIR"
|
||||
install -d -m 0750 -o "$SERVICE_USER" -g "$SERVICE_GROUP" "$SRV_DIR"
|
||||
|
||||
# Install example config files; never overwrite existing configs.
|
||||
for f in mcias.conf.example mcias.env.example; do
|
||||
src="$SCRIPT_DIR/$f"
|
||||
dst="$CONF_DIR/$f"
|
||||
dst="$SRV_DIR/$f"
|
||||
if [ -f "$src" ]; then
|
||||
install -m 0640 -o root -g "$SERVICE_GROUP" "$src" "$dst" 2>/dev/null || true
|
||||
install -m 0640 -o "$SERVICE_USER" -g "$SERVICE_GROUP" "$src" "$dst" 2>/dev/null || true
|
||||
fi
|
||||
done
|
||||
|
||||
# Step 4: Create data directory.
|
||||
info "Creating $DATA_DIR"
|
||||
install -d -m 0750 -o "$SERVICE_USER" -g "$SERVICE_GROUP" "$DATA_DIR"
|
||||
|
||||
# Step 5: Install systemd service unit.
|
||||
if [ -d "$SYSTEMD_DIR" ]; then
|
||||
info "Installing systemd service unit to $SYSTEMD_DIR"
|
||||
@@ -175,26 +170,26 @@ Next steps:
|
||||
|
||||
# Self-signed (development / personal use):
|
||||
openssl req -x509 -newkey ed25519 -days 3650 \\
|
||||
-keyout /etc/mcias/server.key \\
|
||||
-out /etc/mcias/server.crt \\
|
||||
-keyout /srv/mcias/server.key \\
|
||||
-out /srv/mcias/server.crt \\
|
||||
-subj "/CN=auth.example.com" \\
|
||||
-nodes
|
||||
chmod 0640 /etc/mcias/server.key
|
||||
chown root:mcias /etc/mcias/server.key
|
||||
chmod 0640 /srv/mcias/server.key
|
||||
chown mcias:mcias /srv/mcias/server.key /srv/mcias/server.crt
|
||||
|
||||
2. Copy and edit the configuration file:
|
||||
|
||||
cp /etc/mcias/mcias.conf.example /etc/mcias/mcias.conf
|
||||
\$EDITOR /etc/mcias/mcias.conf
|
||||
chmod 0640 /etc/mcias/mcias.conf
|
||||
chown root:mcias /etc/mcias/mcias.conf
|
||||
cp /srv/mcias/mcias.conf.example /srv/mcias/mcias.toml
|
||||
\$EDITOR /srv/mcias/mcias.toml
|
||||
chmod 0640 /srv/mcias/mcias.toml
|
||||
chown mcias:mcias /srv/mcias/mcias.toml
|
||||
|
||||
3. Set the master key passphrase:
|
||||
|
||||
cp /etc/mcias/mcias.env.example /etc/mcias/env
|
||||
\$EDITOR /etc/mcias/env # replace the placeholder passphrase
|
||||
chmod 0640 /etc/mcias/env
|
||||
chown root:mcias /etc/mcias/env
|
||||
cp /srv/mcias/mcias.env.example /srv/mcias/env
|
||||
\$EDITOR /srv/mcias/env # replace the placeholder passphrase
|
||||
chmod 0640 /srv/mcias/env
|
||||
chown mcias:mcias /srv/mcias/env
|
||||
|
||||
IMPORTANT: Back up the passphrase to a secure offline location.
|
||||
Losing it means losing access to all encrypted data in the database.
|
||||
@@ -208,16 +203,16 @@ Next steps:
|
||||
5. Create the first admin account using mciasdb (while the server is
|
||||
running, or before first start):
|
||||
|
||||
MCIAS_MASTER_PASSPHRASE=\$(grep MCIAS_MASTER_PASSPHRASE /etc/mcias/env | cut -d= -f2) \\
|
||||
mciasdb --config /etc/mcias/mcias.conf account create \\
|
||||
MCIAS_MASTER_PASSPHRASE=\$(grep MCIAS_MASTER_PASSPHRASE /srv/mcias/env | cut -d= -f2) \\
|
||||
mciasdb --config /srv/mcias/mcias.toml account create \\
|
||||
--username admin --type human
|
||||
|
||||
Then set a password:
|
||||
MCIAS_MASTER_PASSPHRASE=... mciasdb --config /etc/mcias/mcias.conf \\
|
||||
MCIAS_MASTER_PASSPHRASE=... mciasdb --config /srv/mcias/mcias.toml \\
|
||||
account set-password --id <UUID>
|
||||
|
||||
And grant the admin role:
|
||||
MCIAS_MASTER_PASSPHRASE=... mciasdb --config /etc/mcias/mcias.conf \\
|
||||
MCIAS_MASTER_PASSPHRASE=... mciasdb --config /srv/mcias/mcias.toml \\
|
||||
role grant --id <UUID> --role admin
|
||||
|
||||
For full documentation, see: man mciassrv
|
||||
|
||||
2
dist/mcias-dev.conf.example
vendored
2
dist/mcias-dev.conf.example
vendored
@@ -15,7 +15,7 @@
|
||||
# export MCIAS_MASTER_PASSPHRASE=devpassphrase
|
||||
#
|
||||
# Start the server:
|
||||
# mciassrv -config /path/to/mcias-dev.conf
|
||||
# mciassrv -config /path/to/mcias-dev.toml
|
||||
|
||||
[server]
|
||||
listen_addr = "127.0.0.1:8443"
|
||||
|
||||
20
dist/mcias.conf.docker.example
vendored
20
dist/mcias.conf.docker.example
vendored
@@ -1,42 +1,40 @@
|
||||
# mcias.conf.docker.example — Config template for container deployment
|
||||
#
|
||||
# Mount this file into the container at /etc/mcias/mcias.conf:
|
||||
# Mount this file into the container at /srv/mcias/mcias.toml:
|
||||
#
|
||||
# docker run -d \
|
||||
# --name mcias \
|
||||
# -v /path/to/mcias.conf:/etc/mcias/mcias.conf:ro \
|
||||
# -v /path/to/certs:/etc/mcias:ro \
|
||||
# -v mcias-data:/data \
|
||||
# -v /srv/mcias:/srv/mcias \
|
||||
# -e MCIAS_MASTER_PASSPHRASE=your-passphrase \
|
||||
# -p 8443:8443 \
|
||||
# -p 9443:9443 \
|
||||
# mcias:latest
|
||||
#
|
||||
# The container runs as uid 10001 (mcias). Ensure that:
|
||||
# - /data volume is writable by uid 10001
|
||||
# - /srv/mcias is writable by uid 10001
|
||||
# - TLS cert and key are readable by uid 10001
|
||||
#
|
||||
# TLS: The server performs TLS termination inside the container; there is no
|
||||
# plain-text mode. Mount your certificate and key under /etc/mcias/.
|
||||
# plain-text mode. Place your certificate and key under /srv/mcias/.
|
||||
# For Let's Encrypt certificates, mount the live/ directory read-only.
|
||||
|
||||
[server]
|
||||
listen_addr = "0.0.0.0:8443"
|
||||
grpc_addr = "0.0.0.0:9443"
|
||||
tls_cert = "/etc/mcias/server.crt"
|
||||
tls_key = "/etc/mcias/server.key"
|
||||
tls_cert = "/srv/mcias/server.crt"
|
||||
tls_key = "/srv/mcias/server.key"
|
||||
# If a reverse proxy (nginx, Caddy, Traefik) sits in front of this container,
|
||||
# set trusted_proxy to its container IP so real client IPs are used for rate
|
||||
# limiting and audit logging. Leave commented out for direct exposure.
|
||||
# trusted_proxy = "172.17.0.1"
|
||||
|
||||
[database]
|
||||
# VOLUME /data is declared in the Dockerfile; map a named volume here.
|
||||
path = "/data/mcias.db"
|
||||
# All data lives under /srv/mcias for a single-volume deployment.
|
||||
path = "/srv/mcias/mcias.db"
|
||||
|
||||
[tokens]
|
||||
issuer = "https://auth.example.com"
|
||||
default_expiry = "720h"
|
||||
default_expiry = "168h"
|
||||
admin_expiry = "8h"
|
||||
service_expiry = "8760h"
|
||||
|
||||
|
||||
22
dist/mcias.conf.example
vendored
22
dist/mcias.conf.example
vendored
@@ -1,12 +1,12 @@
|
||||
# mcias.conf — Reference configuration for mciassrv
|
||||
#
|
||||
# Copy this file to /etc/mcias/mcias.conf and adjust the values for your
|
||||
# Copy this file to /srv/mcias/mcias.toml and adjust the values for your
|
||||
# deployment. All fields marked REQUIRED must be set before the server will
|
||||
# start. Fields marked OPTIONAL can be omitted to use defaults.
|
||||
#
|
||||
# File permissions: mode 0640, owner root:mcias.
|
||||
# chmod 0640 /etc/mcias/mcias.conf
|
||||
# chown root:mcias /etc/mcias/mcias.conf
|
||||
# chmod 0640 /srv/mcias/mcias.toml
|
||||
# chown root:mcias /srv/mcias/mcias.toml
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# [server] — Network listener configuration
|
||||
@@ -26,11 +26,11 @@ listen_addr = "0.0.0.0:8443"
|
||||
# REQUIRED. Path to the TLS certificate (PEM format).
|
||||
# Self-signed certificates work fine for personal deployments; for
|
||||
# public-facing deployments consider a certificate from Let's Encrypt.
|
||||
tls_cert = "/etc/mcias/server.crt"
|
||||
tls_cert = "/srv/mcias/server.crt"
|
||||
|
||||
# REQUIRED. Path to the TLS private key (PEM format).
|
||||
# Permissions: mode 0640, owner root:mcias.
|
||||
tls_key = "/etc/mcias/server.key"
|
||||
tls_key = "/srv/mcias/server.key"
|
||||
|
||||
# OPTIONAL. IP address of a trusted reverse proxy (e.g. nginx, Caddy, HAProxy).
|
||||
# When set, the rate limiter and audit log extract the real client IP from the
|
||||
@@ -55,7 +55,7 @@ tls_key = "/etc/mcias/server.key"
|
||||
# REQUIRED. Path to the SQLite database file.
|
||||
# The directory must be writable by the mcias user. WAL mode is enabled
|
||||
# automatically; expect three files: mcias.db, mcias.db-wal, mcias.db-shm.
|
||||
path = "/var/lib/mcias/mcias.db"
|
||||
path = "/srv/mcias/mcias.db"
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# [tokens] — JWT issuance policy
|
||||
@@ -69,8 +69,8 @@ issuer = "https://auth.example.com"
|
||||
|
||||
# OPTIONAL. Default token expiry for interactive (human) logins.
|
||||
# Go duration string: "h" hours, "m" minutes, "s" seconds.
|
||||
# Default: 720h (30 days). Reduce for higher-security deployments.
|
||||
default_expiry = "720h"
|
||||
# Default: 168h (7 days). The maximum allowed value is 720h (30 days).
|
||||
default_expiry = "168h"
|
||||
|
||||
# OPTIONAL. Expiry for admin tokens (tokens with the "admin" role).
|
||||
# Should be shorter than default_expiry to limit the blast radius of
|
||||
@@ -113,13 +113,13 @@ threads = 4
|
||||
# database on first run and reused on subsequent runs so the same passphrase
|
||||
# always produces the same master key.
|
||||
#
|
||||
# Set the passphrase in /etc/mcias/env (loaded by the systemd EnvironmentFile
|
||||
# Set the passphrase in /srv/mcias/env (loaded by the systemd EnvironmentFile
|
||||
# directive). See dist/mcias.env.example for the template.
|
||||
passphrase_env = "MCIAS_MASTER_PASSPHRASE"
|
||||
|
||||
# Option B: Key file mode. The file must contain exactly 32 bytes of raw key
|
||||
# material (AES-256). Generate with: openssl rand -out /etc/mcias/master.key 32
|
||||
# material (AES-256). Generate with: openssl rand -out /srv/mcias/master.key 32
|
||||
# Permissions: mode 0640, owner root:mcias.
|
||||
#
|
||||
# Uncomment and comment out passphrase_env to switch modes.
|
||||
# keyfile = "/etc/mcias/master.key"
|
||||
# keyfile = "/srv/mcias/master.key"
|
||||
|
||||
6
dist/mcias.env.example
vendored
6
dist/mcias.env.example
vendored
@@ -1,10 +1,10 @@
|
||||
# /etc/mcias/env — Environment file for mciassrv (systemd EnvironmentFile).
|
||||
# /srv/mcias/env — Environment file for mciassrv (systemd EnvironmentFile).
|
||||
#
|
||||
# This file is loaded by the mcias.service unit before the server starts.
|
||||
# It must be readable only by root and the mcias service account:
|
||||
#
|
||||
# chmod 0640 /etc/mcias/env
|
||||
# chown root:mcias /etc/mcias/env
|
||||
# chmod 0640 /srv/mcias/env
|
||||
# chown root:mcias /srv/mcias/env
|
||||
#
|
||||
# SECURITY: This file contains the master key passphrase. Treat it with
|
||||
# the same care as a private key. Do not commit it to version control.
|
||||
|
||||
10
dist/mcias.service
vendored
10
dist/mcias.service
vendored
@@ -11,11 +11,11 @@ User=mcias
|
||||
Group=mcias
|
||||
|
||||
# Configuration and secrets.
|
||||
# /etc/mcias/env must contain MCIAS_MASTER_PASSPHRASE=<passphrase>
|
||||
# /srv/mcias/env must contain MCIAS_MASTER_PASSPHRASE=<passphrase>
|
||||
# See dist/mcias.env.example for the template.
|
||||
EnvironmentFile=/etc/mcias/env
|
||||
EnvironmentFile=/srv/mcias/env
|
||||
|
||||
ExecStart=/usr/local/bin/mciassrv -config /etc/mcias/mcias.conf
|
||||
ExecStart=/usr/local/bin/mciassrv -config /srv/mcias/mcias.toml
|
||||
Restart=on-failure
|
||||
RestartSec=5
|
||||
|
||||
@@ -30,11 +30,11 @@ LimitNOFILE=65536
|
||||
CapabilityBoundingSet=
|
||||
|
||||
# Filesystem restrictions.
|
||||
# mciassrv reads /etc/mcias (config, TLS cert/key) and writes /var/lib/mcias (DB).
|
||||
# mciassrv reads and writes /srv/mcias (config, TLS cert/key, database).
|
||||
ProtectSystem=strict
|
||||
ProtectHome=true
|
||||
PrivateTmp=true
|
||||
ReadWritePaths=/var/lib/mcias
|
||||
ReadWritePaths=/srv/mcias
|
||||
|
||||
# Additional hardening.
|
||||
NoNewPrivileges=true
|
||||
|
||||
@@ -304,9 +304,12 @@ func (x *RenewTokenResponse) GetExpiresAt() *timestamppb.Timestamp {
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnrollTOTPRequest carries no body; the acting account is from the JWT.
|
||||
// EnrollTOTPRequest carries the current password for re-authentication.
|
||||
// Security (SEC-01): password is required to prevent a stolen session token
|
||||
// from being used to enroll attacker-controlled TOTP on the victim's account.
|
||||
type EnrollTOTPRequest struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Password string `protobuf:"bytes,1,opt,name=password,proto3" json:"password,omitempty"` // security: current password required; never logged
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
@@ -341,6 +344,13 @@ func (*EnrollTOTPRequest) Descriptor() ([]byte, []int) {
|
||||
return file_mcias_v1_auth_proto_rawDescGZIP(), []int{6}
|
||||
}
|
||||
|
||||
func (x *EnrollTOTPRequest) GetPassword() string {
|
||||
if x != nil {
|
||||
return x.Password
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// EnrollTOTPResponse returns the TOTP secret and otpauth URI for display.
|
||||
// Security: the secret is shown once; it is stored only in encrypted form.
|
||||
type EnrollTOTPResponse struct {
|
||||
@@ -578,8 +588,9 @@ const file_mcias_v1_auth_proto_rawDesc = "" +
|
||||
"\x12RenewTokenResponse\x12\x14\n" +
|
||||
"\x05token\x18\x01 \x01(\tR\x05token\x129\n" +
|
||||
"\n" +
|
||||
"expires_at\x18\x02 \x01(\v2\x1a.google.protobuf.TimestampR\texpiresAt\"\x13\n" +
|
||||
"\x11EnrollTOTPRequest\"M\n" +
|
||||
"expires_at\x18\x02 \x01(\v2\x1a.google.protobuf.TimestampR\texpiresAt\"/\n" +
|
||||
"\x11EnrollTOTPRequest\x12\x1a\n" +
|
||||
"\bpassword\x18\x01 \x01(\tR\bpassword\"M\n" +
|
||||
"\x12EnrollTOTPResponse\x12\x16\n" +
|
||||
"\x06secret\x18\x01 \x01(\tR\x06secret\x12\x1f\n" +
|
||||
"\votpauth_uri\x18\x02 \x01(\tR\n" +
|
||||
|
||||
33
internal/audit/detail.go
Normal file
33
internal/audit/detail.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// Package audit provides helpers for constructing audit log detail strings.
|
||||
package audit
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// JSON builds a JSON details string from key-value pairs for audit logging.
|
||||
// Uses json.Marshal for safe encoding rather than fmt.Sprintf with %q,
|
||||
// which is fragile for edge-case Unicode.
|
||||
func JSON(pairs ...string) string {
|
||||
if len(pairs)%2 != 0 {
|
||||
return "{}"
|
||||
}
|
||||
m := make(map[string]string, len(pairs)/2)
|
||||
for i := 0; i < len(pairs); i += 2 {
|
||||
m[pairs[i]] = pairs[i+1]
|
||||
}
|
||||
b, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return "{}"
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// JSONWithRoles builds a JSON details string that includes a "roles" key
|
||||
// mapped to a string slice. This produces a proper JSON array for the value.
|
||||
func JSONWithRoles(roles []string) string {
|
||||
m := map[string][]string{"roles": roles}
|
||||
b, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return "{}"
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
163
internal/audit/detail_test.go
Normal file
163
internal/audit/detail_test.go
Normal file
@@ -0,0 +1,163 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
pairs []string
|
||||
verify func(t *testing.T, result string)
|
||||
}{
|
||||
{
|
||||
name: "single pair",
|
||||
pairs: []string{"username", "alice"},
|
||||
verify: func(t *testing.T, result string) {
|
||||
var m map[string]string
|
||||
if err := json.Unmarshal([]byte(result), &m); err != nil {
|
||||
t.Fatalf("invalid JSON: %v", err)
|
||||
}
|
||||
if m["username"] != "alice" {
|
||||
t.Fatalf("expected alice, got %s", m["username"])
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "multiple pairs",
|
||||
pairs: []string{"jti", "abc-123", "reason", "logout"},
|
||||
verify: func(t *testing.T, result string) {
|
||||
var m map[string]string
|
||||
if err := json.Unmarshal([]byte(result), &m); err != nil {
|
||||
t.Fatalf("invalid JSON: %v", err)
|
||||
}
|
||||
if m["jti"] != "abc-123" {
|
||||
t.Fatalf("expected abc-123, got %s", m["jti"])
|
||||
}
|
||||
if m["reason"] != "logout" {
|
||||
t.Fatalf("expected logout, got %s", m["reason"])
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "special characters in values",
|
||||
pairs: []string{"username", "user\"with\\quotes"},
|
||||
verify: func(t *testing.T, result string) {
|
||||
var m map[string]string
|
||||
if err := json.Unmarshal([]byte(result), &m); err != nil {
|
||||
t.Fatalf("invalid JSON for special chars: %v", err)
|
||||
}
|
||||
if m["username"] != "user\"with\\quotes" {
|
||||
t.Fatalf("unexpected value: %s", m["username"])
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "unicode edge cases",
|
||||
pairs: []string{"username", "user\u2028line\u2029sep"},
|
||||
verify: func(t *testing.T, result string) {
|
||||
var m map[string]string
|
||||
if err := json.Unmarshal([]byte(result), &m); err != nil {
|
||||
t.Fatalf("invalid JSON for unicode: %v", err)
|
||||
}
|
||||
if m["username"] != "user\u2028line\u2029sep" {
|
||||
t.Fatalf("unexpected value: %s", m["username"])
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "null bytes in value",
|
||||
pairs: []string{"data", "before\x00after"},
|
||||
verify: func(t *testing.T, result string) {
|
||||
var m map[string]string
|
||||
if err := json.Unmarshal([]byte(result), &m); err != nil {
|
||||
t.Fatalf("invalid JSON for null bytes: %v", err)
|
||||
}
|
||||
if m["data"] != "before\x00after" {
|
||||
t.Fatalf("unexpected value: %q", m["data"])
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "odd number of args returns empty object",
|
||||
pairs: []string{"key"},
|
||||
verify: func(t *testing.T, result string) {
|
||||
if result != "{}" {
|
||||
t.Fatalf("expected {}, got %s", result)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no args returns empty object",
|
||||
pairs: nil,
|
||||
verify: func(t *testing.T, result string) {
|
||||
if result != "{}" {
|
||||
t.Fatalf("expected {}, got %s", result)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := JSON(tc.pairs...)
|
||||
tc.verify(t, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONWithRoles(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
roles []string
|
||||
verify func(t *testing.T, result string)
|
||||
}{
|
||||
{
|
||||
name: "multiple roles",
|
||||
roles: []string{"admin", "editor"},
|
||||
verify: func(t *testing.T, result string) {
|
||||
var m map[string][]string
|
||||
if err := json.Unmarshal([]byte(result), &m); err != nil {
|
||||
t.Fatalf("invalid JSON: %v", err)
|
||||
}
|
||||
if len(m["roles"]) != 2 || m["roles"][0] != "admin" || m["roles"][1] != "editor" {
|
||||
t.Fatalf("unexpected roles: %v", m["roles"])
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "empty roles",
|
||||
roles: []string{},
|
||||
verify: func(t *testing.T, result string) {
|
||||
var m map[string][]string
|
||||
if err := json.Unmarshal([]byte(result), &m); err != nil {
|
||||
t.Fatalf("invalid JSON: %v", err)
|
||||
}
|
||||
if len(m["roles"]) != 0 {
|
||||
t.Fatalf("expected empty roles, got %v", m["roles"])
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "roles with special characters",
|
||||
roles: []string{"role\"special"},
|
||||
verify: func(t *testing.T, result string) {
|
||||
var m map[string][]string
|
||||
if err := json.Unmarshal([]byte(result), &m); err != nil {
|
||||
t.Fatalf("invalid JSON: %v", err)
|
||||
}
|
||||
if m["roles"][0] != "role\"special" {
|
||||
t.Fatalf("unexpected role: %s", m["roles"][0])
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := JSONWithRoles(tc.roles)
|
||||
tc.verify(t, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -75,7 +75,7 @@ type MasterKeyConfig struct {
|
||||
}
|
||||
|
||||
// duration is a wrapper around time.Duration that supports TOML string parsing
|
||||
// (e.g. "720h", "8h").
|
||||
// (e.g. "168h", "8h").
|
||||
type duration struct {
|
||||
time.Duration
|
||||
}
|
||||
|
||||
@@ -12,11 +12,11 @@ func validConfig() string {
|
||||
return `
|
||||
[server]
|
||||
listen_addr = "0.0.0.0:8443"
|
||||
tls_cert = "/etc/mcias/server.crt"
|
||||
tls_key = "/etc/mcias/server.key"
|
||||
tls_cert = "/srv/mcias/server.crt"
|
||||
tls_key = "/srv/mcias/server.key"
|
||||
|
||||
[database]
|
||||
path = "/var/lib/mcias/mcias.db"
|
||||
path = "/srv/mcias/mcias.db"
|
||||
|
||||
[tokens]
|
||||
issuer = "https://auth.example.com"
|
||||
@@ -154,11 +154,11 @@ func TestValidateMasterKeyBothSet(t *testing.T) {
|
||||
path := writeTempConfig(t, `
|
||||
[server]
|
||||
listen_addr = "0.0.0.0:8443"
|
||||
tls_cert = "/etc/mcias/server.crt"
|
||||
tls_key = "/etc/mcias/server.key"
|
||||
tls_cert = "/srv/mcias/server.crt"
|
||||
tls_key = "/srv/mcias/server.key"
|
||||
|
||||
[database]
|
||||
path = "/var/lib/mcias/mcias.db"
|
||||
path = "/srv/mcias/mcias.db"
|
||||
|
||||
[tokens]
|
||||
issuer = "https://auth.example.com"
|
||||
@@ -173,7 +173,7 @@ threads = 4
|
||||
|
||||
[master_key]
|
||||
passphrase_env = "MCIAS_MASTER_PASSPHRASE"
|
||||
keyfile = "/etc/mcias/master.key"
|
||||
keyfile = "/srv/mcias/master.key"
|
||||
`)
|
||||
_, err := Load(path)
|
||||
if err == nil {
|
||||
@@ -185,11 +185,11 @@ func TestValidateMasterKeyNoneSet(t *testing.T) {
|
||||
path := writeTempConfig(t, `
|
||||
[server]
|
||||
listen_addr = "0.0.0.0:8443"
|
||||
tls_cert = "/etc/mcias/server.crt"
|
||||
tls_key = "/etc/mcias/server.key"
|
||||
tls_cert = "/srv/mcias/server.crt"
|
||||
tls_key = "/srv/mcias/server.key"
|
||||
|
||||
[database]
|
||||
path = "/var/lib/mcias/mcias.db"
|
||||
path = "/srv/mcias/mcias.db"
|
||||
|
||||
[tokens]
|
||||
issuer = "https://auth.example.com"
|
||||
|
||||
@@ -692,6 +692,70 @@ func (db *DB) RenewToken(oldJTI, reason, newJTI string, accountID int64, issuedA
|
||||
return nil
|
||||
}
|
||||
|
||||
// IssueSystemToken atomically revokes an existing system token (if oldJTI is
|
||||
// non-empty), tracks the new token in token_revocation, and upserts the
|
||||
// system_tokens table — all within a single SQLite transaction.
|
||||
//
|
||||
// Security: these three operations must be atomic so that a crash between them
|
||||
// cannot leave the database in an inconsistent state (e.g., old token revoked
|
||||
// but new token not tracked, or token tracked but system_tokens not updated).
|
||||
// With MaxOpenConns(1) and SQLite's serialised write path, BEGIN IMMEDIATE
|
||||
// acquires the write lock immediately and prevents any other writer from
|
||||
// interleaving.
|
||||
func (db *DB) IssueSystemToken(oldJTI, newJTI string, accountID int64, issuedAt, expiresAt time.Time) error {
|
||||
tx, err := db.sql.Begin()
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: issue system token begin tx: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
n := now()
|
||||
|
||||
// If there is an existing token, revoke it.
|
||||
if oldJTI != "" {
|
||||
_, err := tx.Exec(`
|
||||
UPDATE token_revocation
|
||||
SET revoked_at = ?, revoke_reason = ?
|
||||
WHERE jti = ? AND revoked_at IS NULL
|
||||
`, n, nullString("rotated"), oldJTI)
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: issue system token revoke old %q: %w", oldJTI, err)
|
||||
}
|
||||
// We do not require rows affected > 0 because the old token may
|
||||
// already be revoked or expired; the important thing is that we
|
||||
// proceed to track the new token regardless.
|
||||
}
|
||||
|
||||
// Track the new token in token_revocation.
|
||||
_, err = tx.Exec(`
|
||||
INSERT INTO token_revocation (jti, account_id, issued_at, expires_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`, newJTI, accountID,
|
||||
issuedAt.UTC().Format(time.RFC3339),
|
||||
expiresAt.UTC().Format(time.RFC3339))
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: issue system token track new %q: %w", newJTI, err)
|
||||
}
|
||||
|
||||
// Upsert the system_tokens table so GetSystemToken returns the new JTI.
|
||||
_, err = tx.Exec(`
|
||||
INSERT INTO system_tokens (account_id, jti, expires_at, created_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT(account_id) DO UPDATE SET
|
||||
jti = excluded.jti,
|
||||
expires_at = excluded.expires_at,
|
||||
created_at = excluded.created_at
|
||||
`, accountID, newJTI, expiresAt.UTC().Format(time.RFC3339), n)
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: issue system token set system token for account %d: %w", accountID, err)
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return fmt.Errorf("db: issue system token commit: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RevokeAllUserTokens revokes all non-expired, non-revoked tokens for an account.
|
||||
func (db *DB) RevokeAllUserTokens(accountID int64, reason string) error {
|
||||
n := now()
|
||||
@@ -1181,3 +1245,268 @@ func (db *DB) ClearLoginFailures(accountID int64) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListAccountsWithTOTP returns all accounts (including deleted) that have a
|
||||
// non-null TOTP secret stored, so that rekey can re-encrypt every secret even
|
||||
// for inactive or deleted accounts.
|
||||
func (db *DB) ListAccountsWithTOTP() ([]*model.Account, error) {
|
||||
rows, err := db.sql.Query(`
|
||||
SELECT id, uuid, username, account_type, COALESCE(password_hash,''),
|
||||
status, totp_required,
|
||||
totp_secret_enc, totp_secret_nonce,
|
||||
created_at, updated_at, deleted_at
|
||||
FROM accounts
|
||||
WHERE totp_secret_enc IS NOT NULL
|
||||
ORDER BY id ASC
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("db: list accounts with TOTP: %w", err)
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
var accounts []*model.Account
|
||||
for rows.Next() {
|
||||
a, err := db.scanAccountRow(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
accounts = append(accounts, a)
|
||||
}
|
||||
return accounts, rows.Err()
|
||||
}
|
||||
|
||||
// ListAllPGCredentials returns every row in pg_credentials. Used by rekey
|
||||
// to re-encrypt all stored passwords under a new master key.
|
||||
func (db *DB) ListAllPGCredentials() ([]*model.PGCredential, error) {
|
||||
rows, err := db.sql.Query(`
|
||||
SELECT id, account_id, pg_host, pg_port, pg_database, pg_username,
|
||||
pg_password_enc, pg_password_nonce, created_at, updated_at, owner_id
|
||||
FROM pg_credentials
|
||||
ORDER BY id ASC
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("db: list all pg credentials: %w", err)
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
var creds []*model.PGCredential
|
||||
for rows.Next() {
|
||||
var cred model.PGCredential
|
||||
var createdAtStr, updatedAtStr string
|
||||
var ownerID sql.NullInt64
|
||||
|
||||
if err := rows.Scan(
|
||||
&cred.ID, &cred.AccountID, &cred.PGHost, &cred.PGPort,
|
||||
&cred.PGDatabase, &cred.PGUsername,
|
||||
&cred.PGPasswordEnc, &cred.PGPasswordNonce,
|
||||
&createdAtStr, &updatedAtStr, &ownerID,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("db: scan pg credential: %w", err)
|
||||
}
|
||||
var parseErr error
|
||||
cred.CreatedAt, parseErr = parseTime(createdAtStr)
|
||||
if parseErr != nil {
|
||||
return nil, parseErr
|
||||
}
|
||||
cred.UpdatedAt, parseErr = parseTime(updatedAtStr)
|
||||
if parseErr != nil {
|
||||
return nil, parseErr
|
||||
}
|
||||
if ownerID.Valid {
|
||||
v := ownerID.Int64
|
||||
cred.OwnerID = &v
|
||||
}
|
||||
creds = append(creds, &cred)
|
||||
}
|
||||
return creds, rows.Err()
|
||||
}
|
||||
|
||||
// TOTPRekeyRow carries a re-encrypted TOTP secret for a single account.
|
||||
type TOTPRekeyRow struct {
|
||||
Enc []byte
|
||||
Nonce []byte
|
||||
AccountID int64
|
||||
}
|
||||
|
||||
// PGRekeyRow carries a re-encrypted Postgres password for a single credential row.
|
||||
type PGRekeyRow struct {
|
||||
Enc []byte
|
||||
Nonce []byte
|
||||
CredentialID int64
|
||||
}
|
||||
|
||||
// Rekey atomically replaces the master-key salt and all secrets encrypted
|
||||
// under the old master key with values encrypted under the new master key.
|
||||
//
|
||||
// Security: The entire replacement is performed inside a single SQLite
|
||||
// transaction so that a crash mid-way leaves the database either fully on the
|
||||
// old key or fully on the new key — never in a mixed state. The caller is
|
||||
// responsible for zeroing the old and new master keys after this call returns.
|
||||
func (db *DB) Rekey(newSalt, newSigningKeyEnc, newSigningKeyNonce []byte, totpRows []TOTPRekeyRow, pgRows []PGRekeyRow) error {
|
||||
tx, err := db.sql.Begin()
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: rekey begin tx: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
n := now()
|
||||
|
||||
// Replace master key salt and signing key atomically.
|
||||
_, err = tx.Exec(`
|
||||
UPDATE server_config
|
||||
SET master_key_salt = ?,
|
||||
signing_key_enc = ?,
|
||||
signing_key_nonce = ?,
|
||||
updated_at = ?
|
||||
WHERE id = 1
|
||||
`, newSalt, newSigningKeyEnc, newSigningKeyNonce, n)
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: rekey update server_config: %w", err)
|
||||
}
|
||||
|
||||
// Re-encrypt each TOTP secret.
|
||||
for _, row := range totpRows {
|
||||
_, err = tx.Exec(`
|
||||
UPDATE accounts
|
||||
SET totp_secret_enc = ?,
|
||||
totp_secret_nonce = ?,
|
||||
updated_at = ?
|
||||
WHERE id = ?
|
||||
`, row.Enc, row.Nonce, n, row.AccountID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: rekey update TOTP for account %d: %w", row.AccountID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Re-encrypt each pg_credentials password.
|
||||
for _, row := range pgRows {
|
||||
_, err = tx.Exec(`
|
||||
UPDATE pg_credentials
|
||||
SET pg_password_enc = ?,
|
||||
pg_password_nonce = ?,
|
||||
updated_at = ?
|
||||
WHERE id = ?
|
||||
`, row.Enc, row.Nonce, n, row.CredentialID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: rekey update pg credential %d: %w", row.CredentialID, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return fmt.Errorf("db: rekey commit: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GrantTokenIssueAccess records that granteeID may issue tokens for the system
|
||||
// account identified by accountID. Idempotent: a second call for the same
|
||||
// (account, grantee) pair is silently ignored via INSERT OR IGNORE.
|
||||
func (db *DB) GrantTokenIssueAccess(accountID, granteeID int64, grantedBy *int64) error {
|
||||
_, err := db.sql.Exec(`
|
||||
INSERT OR IGNORE INTO service_account_delegates
|
||||
(account_id, grantee_id, granted_by, granted_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`, accountID, granteeID, grantedBy, now())
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: grant token issue access: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RevokeTokenIssueAccess removes the delegate grant for granteeID on accountID.
|
||||
// Returns ErrNotFound if no such grant exists.
|
||||
func (db *DB) RevokeTokenIssueAccess(accountID, granteeID int64) error {
|
||||
result, err := db.sql.Exec(`
|
||||
DELETE FROM service_account_delegates
|
||||
WHERE account_id = ? AND grantee_id = ?
|
||||
`, accountID, granteeID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: revoke token issue access: %w", err)
|
||||
}
|
||||
n, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: revoke token issue access rows: %w", err)
|
||||
}
|
||||
if n == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListTokenIssueDelegates returns all delegate grants for the given system account.
|
||||
func (db *DB) ListTokenIssueDelegates(accountID int64) ([]*model.ServiceAccountDelegate, error) {
|
||||
rows, err := db.sql.Query(`
|
||||
SELECT d.id, d.account_id, d.grantee_id, d.granted_by, d.granted_at,
|
||||
a.uuid, a.username
|
||||
FROM service_account_delegates d
|
||||
JOIN accounts a ON a.id = d.grantee_id
|
||||
WHERE d.account_id = ?
|
||||
ORDER BY d.granted_at ASC
|
||||
`, accountID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("db: list token issue delegates: %w", err)
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
var out []*model.ServiceAccountDelegate
|
||||
for rows.Next() {
|
||||
var d model.ServiceAccountDelegate
|
||||
var grantedAt string
|
||||
if err := rows.Scan(
|
||||
&d.ID, &d.AccountID, &d.GranteeID, &d.GrantedBy, &grantedAt,
|
||||
&d.GranteeUUID, &d.GranteeName,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("db: scan token issue delegate: %w", err)
|
||||
}
|
||||
t, err := parseTime(grantedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
d.GrantedAt = t
|
||||
out = append(out, &d)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// HasTokenIssueAccess reports whether actorID has been granted permission to
|
||||
// issue tokens for the system account identified by accountID.
|
||||
func (db *DB) HasTokenIssueAccess(accountID, actorID int64) (bool, error) {
|
||||
var count int
|
||||
err := db.sql.QueryRow(`
|
||||
SELECT COUNT(1) FROM service_account_delegates
|
||||
WHERE account_id = ? AND grantee_id = ?
|
||||
`, accountID, actorID).Scan(&count)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("db: has token issue access: %w", err)
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
// ListDelegatedServiceAccounts returns system accounts for which actorID has
|
||||
// been granted token-issue access.
|
||||
func (db *DB) ListDelegatedServiceAccounts(actorID int64) ([]*model.Account, error) {
|
||||
rows, err := db.sql.Query(`
|
||||
SELECT a.id, a.uuid, a.username, a.account_type, COALESCE(a.password_hash,''),
|
||||
a.status, a.totp_required,
|
||||
a.totp_secret_enc, a.totp_secret_nonce,
|
||||
a.created_at, a.updated_at, a.deleted_at
|
||||
FROM service_account_delegates d
|
||||
JOIN accounts a ON a.id = d.account_id
|
||||
WHERE d.grantee_id = ? AND a.status != 'deleted'
|
||||
ORDER BY a.username ASC
|
||||
`, actorID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("db: list delegated service accounts: %w", err)
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
var out []*model.Account
|
||||
for rows.Next() {
|
||||
a, err := db.scanAccountRow(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, a)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
@@ -445,6 +445,79 @@ func TestSystemTokenRotationRevokesOld(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestIssueSystemTokenAtomic verifies that IssueSystemToken atomically
|
||||
// revokes an old token, tracks the new token, and upserts system_tokens.
|
||||
func TestIssueSystemTokenAtomic(t *testing.T) {
|
||||
db := openTestDB(t)
|
||||
acct, err := db.CreateAccount("svc-atomic", model.AccountTypeSystem, "hash")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateAccount: %v", err)
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
exp := now.Add(time.Hour)
|
||||
|
||||
// Issue first system token with no old JTI.
|
||||
jti1 := "atomic-sys-tok-1"
|
||||
if err := db.IssueSystemToken("", jti1, acct.ID, now, exp); err != nil {
|
||||
t.Fatalf("IssueSystemToken first: %v", err)
|
||||
}
|
||||
|
||||
// Verify the first token is tracked and not revoked.
|
||||
rec1, err := db.GetTokenRecord(jti1)
|
||||
if err != nil {
|
||||
t.Fatalf("GetTokenRecord jti1: %v", err)
|
||||
}
|
||||
if rec1.IsRevoked() {
|
||||
t.Error("first token should not be revoked")
|
||||
}
|
||||
|
||||
// Verify system_tokens points to the first token.
|
||||
st1, err := db.GetSystemToken(acct.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetSystemToken after first issue: %v", err)
|
||||
}
|
||||
if st1.JTI != jti1 {
|
||||
t.Errorf("system token JTI = %q, want %q", st1.JTI, jti1)
|
||||
}
|
||||
|
||||
// Issue second token, which should atomically revoke the first.
|
||||
jti2 := "atomic-sys-tok-2"
|
||||
if err := db.IssueSystemToken(jti1, jti2, acct.ID, now, exp); err != nil {
|
||||
t.Fatalf("IssueSystemToken second: %v", err)
|
||||
}
|
||||
|
||||
// First token must be revoked.
|
||||
rec1After, err := db.GetTokenRecord(jti1)
|
||||
if err != nil {
|
||||
t.Fatalf("GetTokenRecord jti1 after rotation: %v", err)
|
||||
}
|
||||
if !rec1After.IsRevoked() {
|
||||
t.Error("first token should be revoked after second issue")
|
||||
}
|
||||
if rec1After.RevokeReason != "rotated" {
|
||||
t.Errorf("revoke reason = %q, want %q", rec1After.RevokeReason, "rotated")
|
||||
}
|
||||
|
||||
// Second token must be tracked and not revoked.
|
||||
rec2, err := db.GetTokenRecord(jti2)
|
||||
if err != nil {
|
||||
t.Fatalf("GetTokenRecord jti2: %v", err)
|
||||
}
|
||||
if rec2.IsRevoked() {
|
||||
t.Error("second token should not be revoked")
|
||||
}
|
||||
|
||||
// system_tokens must point to the second token.
|
||||
st2, err := db.GetSystemToken(acct.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetSystemToken after second issue: %v", err)
|
||||
}
|
||||
if st2.JTI != jti2 {
|
||||
t.Errorf("system token JTI = %q, want %q", st2.JTI, jti2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRevokeAllUserTokens(t *testing.T) {
|
||||
db := openTestDB(t)
|
||||
acct, err := db.CreateAccount("ivan", model.AccountTypeHuman, "hash")
|
||||
|
||||
@@ -194,3 +194,210 @@ func TestListAuditEventsCombinedFilters(t *testing.T) {
|
||||
t.Fatalf("expected 0 events, got %d", len(events))
|
||||
}
|
||||
}
|
||||
|
||||
// ---- rekey helper tests ----
|
||||
|
||||
func TestListAccountsWithTOTP(t *testing.T) {
|
||||
database := openTestDB(t)
|
||||
|
||||
// No accounts with TOTP yet.
|
||||
accounts, err := database.ListAccountsWithTOTP()
|
||||
if err != nil {
|
||||
t.Fatalf("ListAccountsWithTOTP (empty): %v", err)
|
||||
}
|
||||
if len(accounts) != 0 {
|
||||
t.Fatalf("expected 0 accounts, got %d", len(accounts))
|
||||
}
|
||||
|
||||
// Create an account and store a TOTP secret.
|
||||
a, err := database.CreateAccount("totpuser", model.AccountTypeHuman, "hash")
|
||||
if err != nil {
|
||||
t.Fatalf("create account: %v", err)
|
||||
}
|
||||
if err := database.SetTOTP(a.ID, []byte("enc"), []byte("nonce")); err != nil {
|
||||
t.Fatalf("set TOTP: %v", err)
|
||||
}
|
||||
|
||||
// Create another account without TOTP.
|
||||
if _, err := database.CreateAccount("nototp", model.AccountTypeHuman, "hash"); err != nil {
|
||||
t.Fatalf("create account: %v", err)
|
||||
}
|
||||
|
||||
accounts, err = database.ListAccountsWithTOTP()
|
||||
if err != nil {
|
||||
t.Fatalf("ListAccountsWithTOTP: %v", err)
|
||||
}
|
||||
if len(accounts) != 1 {
|
||||
t.Fatalf("expected 1 account with TOTP, got %d", len(accounts))
|
||||
}
|
||||
if accounts[0].ID != a.ID {
|
||||
t.Errorf("expected account ID %d, got %d", a.ID, accounts[0].ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListAllPGCredentials(t *testing.T) {
|
||||
database := openTestDB(t)
|
||||
|
||||
creds, err := database.ListAllPGCredentials()
|
||||
if err != nil {
|
||||
t.Fatalf("ListAllPGCredentials (empty): %v", err)
|
||||
}
|
||||
if len(creds) != 0 {
|
||||
t.Fatalf("expected 0 creds, got %d", len(creds))
|
||||
}
|
||||
|
||||
a, err := database.CreateAccount("pguser", model.AccountTypeSystem, "")
|
||||
if err != nil {
|
||||
t.Fatalf("create account: %v", err)
|
||||
}
|
||||
if err := database.WritePGCredentials(a.ID, "host", 5432, "db", "user", []byte("enc"), []byte("nonce")); err != nil {
|
||||
t.Fatalf("write pg credentials: %v", err)
|
||||
}
|
||||
|
||||
creds, err = database.ListAllPGCredentials()
|
||||
if err != nil {
|
||||
t.Fatalf("ListAllPGCredentials: %v", err)
|
||||
}
|
||||
if len(creds) != 1 {
|
||||
t.Fatalf("expected 1 credential, got %d", len(creds))
|
||||
}
|
||||
if creds[0].AccountID != a.ID {
|
||||
t.Errorf("expected account ID %d, got %d", a.ID, creds[0].AccountID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRekey(t *testing.T) {
|
||||
database := openTestDB(t)
|
||||
|
||||
// Set up: salt + signing key.
|
||||
oldSalt := []byte("oldsaltoldsaltoldsaltoldsaltoldt") // 32 bytes
|
||||
if err := database.WriteMasterKeySalt(oldSalt); err != nil {
|
||||
t.Fatalf("write salt: %v", err)
|
||||
}
|
||||
if err := database.WriteServerConfig([]byte("oldenc"), []byte("oldnonce")); err != nil {
|
||||
t.Fatalf("write server config: %v", err)
|
||||
}
|
||||
|
||||
// Set up: account with TOTP.
|
||||
a, err := database.CreateAccount("rekeyuser", model.AccountTypeHuman, "hash")
|
||||
if err != nil {
|
||||
t.Fatalf("create account: %v", err)
|
||||
}
|
||||
if err := database.SetTOTP(a.ID, []byte("totpenc"), []byte("totpnonce")); err != nil {
|
||||
t.Fatalf("set TOTP: %v", err)
|
||||
}
|
||||
|
||||
// Set up: pg credential.
|
||||
if err := database.WritePGCredentials(a.ID, "host", 5432, "db", "user", []byte("pgenc"), []byte("pgnonce")); err != nil {
|
||||
t.Fatalf("write pg creds: %v", err)
|
||||
}
|
||||
|
||||
// Execute Rekey.
|
||||
newSalt := []byte("newsaltnewsaltnewsaltnewsaltnews") // 32 bytes
|
||||
totpRows := []TOTPRekeyRow{{AccountID: a.ID, Enc: []byte("newtotpenc"), Nonce: []byte("newtotpnonce")}}
|
||||
pgCred, err := database.ReadPGCredentials(a.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("read pg creds: %v", err)
|
||||
}
|
||||
pgRows := []PGRekeyRow{{CredentialID: pgCred.ID, Enc: []byte("newpgenc"), Nonce: []byte("newpgnonce")}}
|
||||
|
||||
if err := database.Rekey(newSalt, []byte("newenc"), []byte("newnonce"), totpRows, pgRows); err != nil {
|
||||
t.Fatalf("Rekey: %v", err)
|
||||
}
|
||||
|
||||
// Verify: salt replaced.
|
||||
gotSalt, err := database.ReadMasterKeySalt()
|
||||
if err != nil {
|
||||
t.Fatalf("read salt after rekey: %v", err)
|
||||
}
|
||||
if string(gotSalt) != string(newSalt) {
|
||||
t.Errorf("salt mismatch: got %q, want %q", gotSalt, newSalt)
|
||||
}
|
||||
|
||||
// Verify: signing key replaced.
|
||||
gotEnc, gotNonce, err := database.ReadServerConfig()
|
||||
if err != nil {
|
||||
t.Fatalf("read server config after rekey: %v", err)
|
||||
}
|
||||
if string(gotEnc) != "newenc" || string(gotNonce) != "newnonce" {
|
||||
t.Errorf("signing key enc/nonce mismatch after rekey")
|
||||
}
|
||||
|
||||
// Verify: TOTP replaced.
|
||||
updatedAcct, err := database.GetAccountByID(a.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get account after rekey: %v", err)
|
||||
}
|
||||
if string(updatedAcct.TOTPSecretEnc) != "newtotpenc" || string(updatedAcct.TOTPSecretNonce) != "newtotpnonce" {
|
||||
t.Errorf("TOTP enc/nonce mismatch after rekey: enc=%q nonce=%q",
|
||||
updatedAcct.TOTPSecretEnc, updatedAcct.TOTPSecretNonce)
|
||||
}
|
||||
|
||||
// Verify: pg credential replaced.
|
||||
updatedCred, err := database.ReadPGCredentials(a.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("read pg creds after rekey: %v", err)
|
||||
}
|
||||
if string(updatedCred.PGPasswordEnc) != "newpgenc" || string(updatedCred.PGPasswordNonce) != "newpgnonce" {
|
||||
t.Errorf("pg enc/nonce mismatch after rekey: enc=%q nonce=%q",
|
||||
updatedCred.PGPasswordEnc, updatedCred.PGPasswordNonce)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRekeyEmptyDatabase(t *testing.T) {
|
||||
database := openTestDB(t)
|
||||
|
||||
// Minimal setup: salt and signing key only; no TOTP, no pg creds.
|
||||
salt := []byte("saltsaltsaltsaltsaltsaltsaltsalt") // 32 bytes
|
||||
if err := database.WriteMasterKeySalt(salt); err != nil {
|
||||
t.Fatalf("write salt: %v", err)
|
||||
}
|
||||
if err := database.WriteServerConfig([]byte("enc"), []byte("nonce")); err != nil {
|
||||
t.Fatalf("write server config: %v", err)
|
||||
}
|
||||
|
||||
newSalt := []byte("newsaltnewsaltnewsaltnewsaltnews") // 32 bytes
|
||||
if err := database.Rekey(newSalt, []byte("newenc"), []byte("newnonce"), nil, nil); err != nil {
|
||||
t.Fatalf("Rekey (empty): %v", err)
|
||||
}
|
||||
|
||||
gotSalt, err := database.ReadMasterKeySalt()
|
||||
if err != nil {
|
||||
t.Fatalf("read salt: %v", err)
|
||||
}
|
||||
if string(gotSalt) != string(newSalt) {
|
||||
t.Errorf("salt mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRekeyOldSaltUnchangedOnQueryError verifies the salt and encrypted data
|
||||
// is only present under the new values after a successful Rekey — the old
|
||||
// values must be gone. Uses the same approach as TestRekey but reads the
|
||||
// stored salt before and confirms it changes.
|
||||
func TestRekeyReplacesSalt(t *testing.T) {
|
||||
database := openTestDB(t)
|
||||
|
||||
oldSalt := []byte("oldsaltoldsaltoldsaltoldsaltoldt") // 32 bytes
|
||||
if err := database.WriteMasterKeySalt(oldSalt); err != nil {
|
||||
t.Fatalf("write salt: %v", err)
|
||||
}
|
||||
if err := database.WriteServerConfig([]byte("enc"), []byte("nonce")); err != nil {
|
||||
t.Fatalf("write server config: %v", err)
|
||||
}
|
||||
|
||||
newSalt := []byte("newsaltnewsaltnewsaltnewsaltnews") // 32 bytes
|
||||
if err := database.Rekey(newSalt, []byte("newenc"), []byte("newnonce"), nil, nil); err != nil {
|
||||
t.Fatalf("Rekey: %v", err)
|
||||
}
|
||||
|
||||
gotSalt, err := database.ReadMasterKeySalt()
|
||||
if err != nil {
|
||||
t.Fatalf("read salt: %v", err)
|
||||
}
|
||||
if string(gotSalt) == string(oldSalt) {
|
||||
t.Error("old salt still present after rekey")
|
||||
}
|
||||
if string(gotSalt) != string(newSalt) {
|
||||
t.Errorf("expected new salt %q, got %q", newSalt, gotSalt)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
-- service_account_delegates tracks which human accounts are permitted to issue
|
||||
-- tokens for a given system account without holding the global admin role.
|
||||
-- Admins manage delegates; delegates can issue/rotate tokens for the specific
|
||||
-- system account only and cannot modify any other account settings.
|
||||
CREATE TABLE IF NOT EXISTS service_account_delegates (
|
||||
id INTEGER PRIMARY KEY,
|
||||
account_id INTEGER NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
|
||||
grantee_id INTEGER NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
|
||||
granted_by INTEGER REFERENCES accounts(id),
|
||||
granted_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ','now')),
|
||||
UNIQUE (account_id, grantee_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sa_delegates_account ON service_account_delegates (account_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_sa_delegates_grantee ON service_account_delegates (grantee_id);
|
||||
@@ -17,8 +17,12 @@ type adminServiceServer struct {
|
||||
s *Server
|
||||
}
|
||||
|
||||
// Health returns {"status":"ok"} to signal the server is operational.
|
||||
// Health returns {"status":"ok"} to signal the server is operational, or
|
||||
// {"status":"sealed"} when the vault is sealed.
|
||||
func (a *adminServiceServer) Health(_ context.Context, _ *mciasv1.HealthRequest) (*mciasv1.HealthResponse, error) {
|
||||
if a.s.vault.IsSealed() {
|
||||
return &mciasv1.HealthResponse{Status: "sealed"}, nil
|
||||
}
|
||||
return &mciasv1.HealthResponse{Status: "ok"}, nil
|
||||
}
|
||||
|
||||
@@ -26,11 +30,12 @@ func (a *adminServiceServer) Health(_ context.Context, _ *mciasv1.HealthRequest)
|
||||
// The "x" field is the raw 32-byte public key base64url-encoded without padding,
|
||||
// matching the REST /v1/keys/public response format.
|
||||
func (a *adminServiceServer) GetPublicKey(_ context.Context, _ *mciasv1.GetPublicKeyRequest) (*mciasv1.GetPublicKeyResponse, error) {
|
||||
if len(a.s.pubKey) == 0 {
|
||||
return nil, status.Error(codes.Internal, "public key not available")
|
||||
pubKey, err := a.s.vault.PubKey()
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Unavailable, "vault sealed")
|
||||
}
|
||||
// Encode as base64url without padding — identical to the REST handler.
|
||||
x := base64.RawURLEncoding.EncodeToString(a.s.pubKey)
|
||||
x := base64.RawURLEncoding.EncodeToString(pubKey)
|
||||
return &mciasv1.GetPublicKeyResponse{
|
||||
Kty: "OKP",
|
||||
Crv: "Ed25519",
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/peer"
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
mciasv1 "git.wntrmute.dev/kyle/mcias/gen/mcias/v1"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/audit"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/auth"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/crypto"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/model"
|
||||
@@ -42,7 +44,7 @@ func (a *authServiceServer) Login(ctx context.Context, req *mciasv1.LoginRequest
|
||||
// Security: run dummy Argon2 to equalise timing for unknown users.
|
||||
_, _ = auth.VerifyPassword("dummy", auth.DummyHash())
|
||||
a.s.db.WriteAuditEvent(model.EventLoginFail, nil, nil, ip, //nolint:errcheck // audit failure is non-fatal
|
||||
fmt.Sprintf(`{"username":%q,"reason":"unknown_user"}`, req.Username))
|
||||
audit.JSON("username", req.Username, "reason", "unknown_user"))
|
||||
return nil, status.Error(codes.Unauthenticated, "invalid credentials")
|
||||
}
|
||||
|
||||
@@ -60,7 +62,9 @@ func (a *authServiceServer) Login(ctx context.Context, req *mciasv1.LoginRequest
|
||||
if locked {
|
||||
_, _ = auth.VerifyPassword("dummy", auth.DummyHash())
|
||||
a.s.db.WriteAuditEvent(model.EventLoginFail, &acct.ID, nil, ip, `{"reason":"account_locked"}`) //nolint:errcheck
|
||||
return nil, status.Error(codes.ResourceExhausted, "account temporarily locked")
|
||||
// Security: return the same Unauthenticated / "invalid credentials" as wrong-password
|
||||
// to prevent user-enumeration via lockout differentiation (SEC-02).
|
||||
return nil, status.Error(codes.Unauthenticated, "invalid credentials")
|
||||
}
|
||||
|
||||
ok, err := auth.VerifyPassword(req.Password, acct.PasswordHash)
|
||||
@@ -82,7 +86,11 @@ func (a *authServiceServer) Login(ctx context.Context, req *mciasv1.LoginRequest
|
||||
a.s.db.WriteAuditEvent(model.EventLoginFail, &acct.ID, nil, ip, `{"reason":"totp_missing"}`) //nolint:errcheck
|
||||
return nil, status.Error(codes.Unauthenticated, "TOTP code required")
|
||||
}
|
||||
secret, err := crypto.OpenAESGCM(a.s.masterKey, acct.TOTPSecretNonce, acct.TOTPSecretEnc)
|
||||
masterKey, mkErr := a.s.vault.MasterKey()
|
||||
if mkErr != nil {
|
||||
return nil, status.Error(codes.Unavailable, "vault sealed")
|
||||
}
|
||||
secret, err := crypto.OpenAESGCM(masterKey, acct.TOTPSecretNonce, acct.TOTPSecretEnc)
|
||||
if err != nil {
|
||||
a.s.logger.Error("decrypt TOTP secret", "error", err, "account_id", acct.ID)
|
||||
return nil, status.Error(codes.Internal, "internal error")
|
||||
@@ -117,7 +125,11 @@ func (a *authServiceServer) Login(ctx context.Context, req *mciasv1.LoginRequest
|
||||
}
|
||||
}
|
||||
|
||||
tokenStr, claims, err := token.IssueToken(a.s.privKey, a.s.cfg.Tokens.Issuer, acct.UUID, roles, expiry)
|
||||
privKey, pkErr := a.s.vault.PrivKey()
|
||||
if pkErr != nil {
|
||||
return nil, status.Error(codes.Unavailable, "vault sealed")
|
||||
}
|
||||
tokenStr, claims, err := token.IssueToken(privKey, a.s.cfg.Tokens.Issuer, acct.UUID, roles, expiry)
|
||||
if err != nil {
|
||||
a.s.logger.Error("issue token", "error", err)
|
||||
return nil, status.Error(codes.Internal, "internal error")
|
||||
@@ -129,7 +141,7 @@ func (a *authServiceServer) Login(ctx context.Context, req *mciasv1.LoginRequest
|
||||
|
||||
a.s.db.WriteAuditEvent(model.EventLoginOK, &acct.ID, nil, ip, "") //nolint:errcheck
|
||||
a.s.db.WriteAuditEvent(model.EventTokenIssued, &acct.ID, nil, ip, //nolint:errcheck
|
||||
fmt.Sprintf(`{"jti":%q}`, claims.JTI))
|
||||
audit.JSON("jti", claims.JTI))
|
||||
|
||||
return &mciasv1.LoginResponse{
|
||||
Token: tokenStr,
|
||||
@@ -145,7 +157,7 @@ func (a *authServiceServer) Logout(ctx context.Context, _ *mciasv1.LogoutRequest
|
||||
return nil, status.Error(codes.Internal, "internal error")
|
||||
}
|
||||
a.s.db.WriteAuditEvent(model.EventTokenRevoked, nil, nil, peerIP(ctx), //nolint:errcheck
|
||||
fmt.Sprintf(`{"jti":%q,"reason":"logout"}`, claims.JTI))
|
||||
audit.JSON("jti", claims.JTI, "reason", "logout"))
|
||||
return &mciasv1.LogoutResponse{}, nil
|
||||
}
|
||||
|
||||
@@ -153,6 +165,14 @@ func (a *authServiceServer) Logout(ctx context.Context, _ *mciasv1.LogoutRequest
|
||||
func (a *authServiceServer) RenewToken(ctx context.Context, _ *mciasv1.RenewTokenRequest) (*mciasv1.RenewTokenResponse, error) {
|
||||
claims := claimsFromContext(ctx)
|
||||
|
||||
// Security: only allow renewal when the token has consumed at least 50% of
|
||||
// its lifetime. This prevents indefinite renewal of stolen tokens (SEC-03).
|
||||
totalLifetime := claims.ExpiresAt.Sub(claims.IssuedAt)
|
||||
elapsed := time.Since(claims.IssuedAt)
|
||||
if elapsed < totalLifetime/2 {
|
||||
return nil, status.Error(codes.InvalidArgument, "token is not yet eligible for renewal")
|
||||
}
|
||||
|
||||
acct, err := a.s.db.GetAccountByUUID(claims.Subject)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Unauthenticated, "account not found")
|
||||
@@ -174,7 +194,11 @@ func (a *authServiceServer) RenewToken(ctx context.Context, _ *mciasv1.RenewToke
|
||||
}
|
||||
}
|
||||
|
||||
newTokenStr, newClaims, err := token.IssueToken(a.s.privKey, a.s.cfg.Tokens.Issuer, acct.UUID, roles, expiry)
|
||||
privKey, pkErr := a.s.vault.PrivKey()
|
||||
if pkErr != nil {
|
||||
return nil, status.Error(codes.Unavailable, "vault sealed")
|
||||
}
|
||||
newTokenStr, newClaims, err := token.IssueToken(privKey, a.s.cfg.Tokens.Issuer, acct.UUID, roles, expiry)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "internal error")
|
||||
}
|
||||
@@ -186,7 +210,7 @@ func (a *authServiceServer) RenewToken(ctx context.Context, _ *mciasv1.RenewToke
|
||||
}
|
||||
|
||||
a.s.db.WriteAuditEvent(model.EventTokenRenewed, &acct.ID, nil, peerIP(ctx), //nolint:errcheck
|
||||
fmt.Sprintf(`{"old_jti":%q,"new_jti":%q}`, claims.JTI, newClaims.JTI))
|
||||
audit.JSON("old_jti", claims.JTI, "new_jti", newClaims.JTI))
|
||||
|
||||
return &mciasv1.RenewTokenResponse{
|
||||
Token: newTokenStr,
|
||||
@@ -195,19 +219,49 @@ func (a *authServiceServer) RenewToken(ctx context.Context, _ *mciasv1.RenewToke
|
||||
}
|
||||
|
||||
// EnrollTOTP begins TOTP enrollment for the calling account.
|
||||
func (a *authServiceServer) EnrollTOTP(ctx context.Context, _ *mciasv1.EnrollTOTPRequest) (*mciasv1.EnrollTOTPResponse, error) {
|
||||
//
|
||||
// Security (SEC-01): the current password is required to prevent a stolen
|
||||
// session token from being used to enroll attacker-controlled TOTP on the
|
||||
// victim's account. Lockout is checked and failures are recorded.
|
||||
func (a *authServiceServer) EnrollTOTP(ctx context.Context, req *mciasv1.EnrollTOTPRequest) (*mciasv1.EnrollTOTPResponse, error) {
|
||||
claims := claimsFromContext(ctx)
|
||||
acct, err := a.s.db.GetAccountByUUID(claims.Subject)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Unauthenticated, "account not found")
|
||||
}
|
||||
|
||||
if req.Password == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "password is required")
|
||||
}
|
||||
|
||||
// Security: check lockout before verifying (same as login flow).
|
||||
locked, lockErr := a.s.db.IsLockedOut(acct.ID)
|
||||
if lockErr != nil {
|
||||
a.s.logger.Error("lockout check (gRPC TOTP enroll)", "error", lockErr)
|
||||
}
|
||||
if locked {
|
||||
a.s.db.WriteAuditEvent(model.EventTOTPEnrolled, &acct.ID, &acct.ID, peerIP(ctx), `{"result":"locked"}`) //nolint:errcheck
|
||||
return nil, status.Error(codes.ResourceExhausted, "account temporarily locked")
|
||||
}
|
||||
|
||||
// Security: verify the current password with Argon2id (constant-time).
|
||||
ok, verifyErr := auth.VerifyPassword(req.Password, acct.PasswordHash)
|
||||
if verifyErr != nil || !ok {
|
||||
_ = a.s.db.RecordLoginFailure(acct.ID)
|
||||
a.s.db.WriteAuditEvent(model.EventTOTPEnrolled, &acct.ID, &acct.ID, peerIP(ctx), `{"result":"wrong_password"}`) //nolint:errcheck
|
||||
return nil, status.Error(codes.Unauthenticated, "password is incorrect")
|
||||
}
|
||||
|
||||
rawSecret, b32Secret, err := auth.GenerateTOTPSecret()
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "internal error")
|
||||
}
|
||||
|
||||
secretEnc, secretNonce, err := crypto.SealAESGCM(a.s.masterKey, rawSecret)
|
||||
masterKey, mkErr := a.s.vault.MasterKey()
|
||||
if mkErr != nil {
|
||||
return nil, status.Error(codes.Unavailable, "vault sealed")
|
||||
}
|
||||
secretEnc, secretNonce, err := crypto.SealAESGCM(masterKey, rawSecret)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "internal error")
|
||||
}
|
||||
@@ -245,7 +299,11 @@ func (a *authServiceServer) ConfirmTOTP(ctx context.Context, req *mciasv1.Confir
|
||||
return nil, status.Error(codes.FailedPrecondition, "TOTP enrollment not started")
|
||||
}
|
||||
|
||||
secret, err := crypto.OpenAESGCM(a.s.masterKey, acct.TOTPSecretNonce, acct.TOTPSecretEnc)
|
||||
masterKey, mkErr := a.s.vault.MasterKey()
|
||||
if mkErr != nil {
|
||||
return nil, status.Error(codes.Unavailable, "vault sealed")
|
||||
}
|
||||
secret, err := crypto.OpenAESGCM(masterKey, acct.TOTPSecretNonce, acct.TOTPSecretEnc)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "internal error")
|
||||
}
|
||||
|
||||
@@ -47,7 +47,11 @@ func (c *credentialServiceServer) GetPGCreds(ctx context.Context, req *mciasv1.G
|
||||
}
|
||||
|
||||
// Decrypt the password for admin retrieval.
|
||||
password, err := crypto.OpenAESGCM(c.s.masterKey, cred.PGPasswordNonce, cred.PGPasswordEnc)
|
||||
masterKey, mkErr := c.s.vault.MasterKey()
|
||||
if mkErr != nil {
|
||||
return nil, status.Error(codes.Unavailable, "vault sealed")
|
||||
}
|
||||
password, err := crypto.OpenAESGCM(masterKey, cred.PGPasswordNonce, cred.PGPasswordEnc)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "internal error")
|
||||
}
|
||||
@@ -94,7 +98,11 @@ func (c *credentialServiceServer) SetPGCreds(ctx context.Context, req *mciasv1.S
|
||||
return nil, status.Error(codes.Internal, "internal error")
|
||||
}
|
||||
|
||||
enc, nonce, err := crypto.SealAESGCM(c.s.masterKey, []byte(cr.Password))
|
||||
masterKey, mkErr := c.s.vault.MasterKey()
|
||||
if mkErr != nil {
|
||||
return nil, status.Error(codes.Unavailable, "vault sealed")
|
||||
}
|
||||
enc, nonce, err := crypto.SealAESGCM(masterKey, []byte(cr.Password))
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "internal error")
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ed25519"
|
||||
"log/slog"
|
||||
"net"
|
||||
"strings"
|
||||
@@ -35,6 +34,7 @@ import (
|
||||
"git.wntrmute.dev/kyle/mcias/internal/config"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/db"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/token"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/vault"
|
||||
)
|
||||
|
||||
// contextKey is the unexported context key type for this package.
|
||||
@@ -57,21 +57,17 @@ type Server struct {
|
||||
cfg *config.Config
|
||||
logger *slog.Logger
|
||||
rateLimiter *grpcRateLimiter
|
||||
privKey ed25519.PrivateKey
|
||||
pubKey ed25519.PublicKey
|
||||
masterKey []byte
|
||||
vault *vault.Vault
|
||||
}
|
||||
|
||||
// New creates a Server with the given dependencies (same as the REST Server).
|
||||
// A fresh per-IP rate limiter (10 req/s, burst 10) is allocated per Server
|
||||
// instance so that tests do not share state across test cases.
|
||||
func New(database *db.DB, cfg *config.Config, priv ed25519.PrivateKey, pub ed25519.PublicKey, masterKey []byte, logger *slog.Logger) *Server {
|
||||
func New(database *db.DB, cfg *config.Config, v *vault.Vault, logger *slog.Logger) *Server {
|
||||
return &Server{
|
||||
db: database,
|
||||
cfg: cfg,
|
||||
privKey: priv,
|
||||
pubKey: pub,
|
||||
masterKey: masterKey,
|
||||
vault: v,
|
||||
logger: logger,
|
||||
rateLimiter: newGRPCRateLimiter(10, 10),
|
||||
}
|
||||
@@ -106,6 +102,7 @@ func (s *Server) buildServer(extra ...grpc.ServerOption) *grpc.Server {
|
||||
[]grpc.ServerOption{
|
||||
grpc.ChainUnaryInterceptor(
|
||||
s.loggingInterceptor,
|
||||
s.sealedInterceptor,
|
||||
s.authInterceptor,
|
||||
s.rateLimitInterceptor,
|
||||
),
|
||||
@@ -162,14 +159,36 @@ func (s *Server) loggingInterceptor(
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// sealedInterceptor rejects all RPCs (except Health) when the vault is sealed.
|
||||
//
|
||||
// Security: This is the first interceptor in the chain (after logging). It
|
||||
// prevents any authenticated or data-serving handler from running while the
|
||||
// vault is sealed and key material is unavailable.
|
||||
func (s *Server) sealedInterceptor(
|
||||
ctx context.Context,
|
||||
req interface{},
|
||||
info *grpc.UnaryServerInfo,
|
||||
handler grpc.UnaryHandler,
|
||||
) (interface{}, error) {
|
||||
if !s.vault.IsSealed() {
|
||||
return handler(ctx, req)
|
||||
}
|
||||
// Health is always allowed — returns sealed status.
|
||||
if info.FullMethod == "/mcias.v1.AdminService/Health" {
|
||||
return handler(ctx, req)
|
||||
}
|
||||
return nil, status.Error(codes.Unavailable, "vault sealed")
|
||||
}
|
||||
|
||||
// authInterceptor validates the Bearer JWT from gRPC metadata and injects
|
||||
// claims into the context. Public methods bypass this check.
|
||||
//
|
||||
// Security: Same validation path as the REST RequireAuth middleware:
|
||||
// 1. Extract "authorization" metadata value (case-insensitive key lookup).
|
||||
// 2. Validate JWT (alg-first, then signature, then expiry/issuer).
|
||||
// 3. Check JTI against revocation table.
|
||||
// 4. Inject claims into context.
|
||||
// 2. Read public key from vault (fail closed if sealed).
|
||||
// 3. Validate JWT (alg-first, then signature, then expiry/issuer).
|
||||
// 4. Check JTI against revocation table.
|
||||
// 5. Inject claims into context.
|
||||
func (s *Server) authInterceptor(
|
||||
ctx context.Context,
|
||||
req interface{},
|
||||
@@ -186,7 +205,13 @@ func (s *Server) authInterceptor(
|
||||
return nil, status.Error(codes.Unauthenticated, "missing or invalid authorization")
|
||||
}
|
||||
|
||||
claims, err := token.ValidateToken(s.pubKey, tokenStr, s.cfg.Tokens.Issuer)
|
||||
// Security: read the public key from vault at request time.
|
||||
pubKey, err := s.vault.PubKey()
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Unavailable, "vault sealed")
|
||||
}
|
||||
|
||||
claims, err := token.ValidateToken(pubKey, tokenStr, s.cfg.Tokens.Issuer)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Unauthenticated, "invalid or expired token")
|
||||
}
|
||||
@@ -289,28 +314,75 @@ func (l *grpcRateLimiter) cleanup() {
|
||||
|
||||
// rateLimitInterceptor applies per-IP rate limiting using the same token-bucket
|
||||
// parameters as the REST rate limiter (10 req/s, burst 10).
|
||||
//
|
||||
// Security (SEC-06): uses grpcClientIP to extract the real client IP when
|
||||
// behind a trusted reverse proxy, matching the REST middleware behaviour.
|
||||
func (s *Server) rateLimitInterceptor(
|
||||
ctx context.Context,
|
||||
req interface{},
|
||||
info *grpc.UnaryServerInfo,
|
||||
handler grpc.UnaryHandler,
|
||||
) (interface{}, error) {
|
||||
ip := ""
|
||||
if p, ok := peer.FromContext(ctx); ok {
|
||||
host, _, err := net.SplitHostPort(p.Addr.String())
|
||||
if err == nil {
|
||||
ip = host
|
||||
} else {
|
||||
ip = p.Addr.String()
|
||||
}
|
||||
var trustedProxy net.IP
|
||||
if s.cfg.Server.TrustedProxy != "" {
|
||||
trustedProxy = net.ParseIP(s.cfg.Server.TrustedProxy)
|
||||
}
|
||||
|
||||
ip := grpcClientIP(ctx, trustedProxy)
|
||||
|
||||
if ip != "" && !s.rateLimiter.allow(ip) {
|
||||
return nil, status.Error(codes.ResourceExhausted, "rate limit exceeded")
|
||||
}
|
||||
return handler(ctx, req)
|
||||
}
|
||||
|
||||
// grpcClientIP extracts the real client IP from gRPC context, optionally
|
||||
// honouring proxy headers when the peer matches the trusted proxy.
|
||||
//
|
||||
// Security (SEC-06): mirrors middleware.ClientIP for the REST server.
|
||||
// X-Forwarded-For and X-Real-IP metadata are only trusted when the immediate
|
||||
// peer address matches trustedProxy exactly, preventing IP-spoofing attacks.
|
||||
// Only the first (leftmost) value in x-forwarded-for is used (original client).
|
||||
// gRPC lowercases all metadata keys, so we look up "x-forwarded-for" and
|
||||
// "x-real-ip".
|
||||
func grpcClientIP(ctx context.Context, trustedProxy net.IP) string {
|
||||
peerIP := ""
|
||||
if p, ok := peer.FromContext(ctx); ok {
|
||||
host, _, err := net.SplitHostPort(p.Addr.String())
|
||||
if err == nil {
|
||||
peerIP = host
|
||||
} else {
|
||||
peerIP = p.Addr.String()
|
||||
}
|
||||
}
|
||||
|
||||
if trustedProxy != nil && peerIP != "" {
|
||||
remoteIP := net.ParseIP(peerIP)
|
||||
if remoteIP != nil && remoteIP.Equal(trustedProxy) {
|
||||
// Peer is the trusted proxy — extract real client IP from metadata.
|
||||
// Prefer x-real-ip (single value) over x-forwarded-for (may be a
|
||||
// comma-separated list when multiple proxies are chained).
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if ok {
|
||||
if vals := md.Get("x-real-ip"); len(vals) > 0 {
|
||||
if ip := net.ParseIP(strings.TrimSpace(vals[0])); ip != nil {
|
||||
return ip.String()
|
||||
}
|
||||
}
|
||||
if vals := md.Get("x-forwarded-for"); len(vals) > 0 {
|
||||
// Take the first (leftmost) address — the original client.
|
||||
first, _, _ := strings.Cut(vals[0], ",")
|
||||
if ip := net.ParseIP(strings.TrimSpace(first)); ip != nil {
|
||||
return ip.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return peerIP
|
||||
}
|
||||
|
||||
// extractBearerFromMD extracts the Bearer token from gRPC metadata.
|
||||
// The key lookup is case-insensitive per gRPC metadata convention (all keys
|
||||
// are lowercased by the framework; we match on "authorization").
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -19,6 +20,7 @@ import (
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/peer"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/grpc/test/bufconn"
|
||||
|
||||
@@ -28,6 +30,7 @@ import (
|
||||
"git.wntrmute.dev/kyle/mcias/internal/db"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/model"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/token"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/vault"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -71,7 +74,8 @@ func newTestEnv(t *testing.T) *testEnv {
|
||||
cfg := config.NewTestConfig(testIssuer)
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
|
||||
srv := New(database, cfg, priv, pub, masterKey, logger)
|
||||
v := vault.NewUnsealed(masterKey, priv, pub)
|
||||
srv := New(database, cfg, v, logger)
|
||||
grpcSrv := srv.GRPCServer()
|
||||
|
||||
lis := bufconn.Listen(bufConnSize)
|
||||
@@ -143,7 +147,12 @@ func (e *testEnv) issueAdminToken(t *testing.T, username string) (string, *model
|
||||
// issueUserToken issues a regular (non-admin) token for an account.
|
||||
func (e *testEnv) issueUserToken(t *testing.T, acct *model.Account) string {
|
||||
t.Helper()
|
||||
tokenStr, claims, err := token.IssueToken(e.priv, testIssuer, acct.UUID, []string{}, time.Hour)
|
||||
return e.issueShortToken(t, acct, time.Hour)
|
||||
}
|
||||
|
||||
func (e *testEnv) issueShortToken(t *testing.T, acct *model.Account, expiry time.Duration) string {
|
||||
t.Helper()
|
||||
tokenStr, claims, err := token.IssueToken(e.priv, testIssuer, acct.UUID, []string{}, expiry)
|
||||
if err != nil {
|
||||
t.Fatalf("issue token: %v", err)
|
||||
}
|
||||
@@ -357,11 +366,17 @@ func TestLogout(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRenewToken verifies that a valid token can be renewed.
|
||||
// TestRenewToken verifies that a valid token can be renewed after 50% of its
|
||||
// lifetime has elapsed (SEC-03).
|
||||
func TestRenewToken(t *testing.T) {
|
||||
e := newTestEnv(t)
|
||||
acct := e.createHumanAccount(t, "renewuser")
|
||||
tok := e.issueUserToken(t, acct)
|
||||
|
||||
// Issue a short-lived token (4s) so we can wait past the 50% threshold.
|
||||
tok := e.issueShortToken(t, acct, 4*time.Second)
|
||||
|
||||
// Wait for >50% of lifetime to elapse.
|
||||
time.Sleep(2100 * time.Millisecond)
|
||||
|
||||
cl := mciasv1.NewAuthServiceClient(e.conn)
|
||||
ctx := authCtx(tok)
|
||||
@@ -377,6 +392,28 @@ func TestRenewToken(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRenewTokenTooEarly verifies that a token cannot be renewed before 50%
|
||||
// of its lifetime has elapsed (SEC-03).
|
||||
func TestRenewTokenTooEarly(t *testing.T) {
|
||||
e := newTestEnv(t)
|
||||
acct := e.createHumanAccount(t, "renewearlyuser")
|
||||
tok := e.issueUserToken(t, acct)
|
||||
|
||||
cl := mciasv1.NewAuthServiceClient(e.conn)
|
||||
ctx := authCtx(tok)
|
||||
_, err := cl.RenewToken(ctx, &mciasv1.RenewTokenRequest{})
|
||||
if err == nil {
|
||||
t.Fatal("RenewToken: expected error for early renewal, got nil")
|
||||
}
|
||||
st, ok := status.FromError(err)
|
||||
if !ok || st.Code() != codes.InvalidArgument {
|
||||
t.Fatalf("RenewToken: expected InvalidArgument, got %v", err)
|
||||
}
|
||||
if !strings.Contains(st.Message(), "not yet eligible for renewal") {
|
||||
t.Errorf("RenewToken: expected eligibility message, got: %s", st.Message())
|
||||
}
|
||||
}
|
||||
|
||||
// ---- TokenService tests ----
|
||||
|
||||
// TestValidateToken verifies the public ValidateToken RPC returns valid=true for
|
||||
@@ -650,3 +687,196 @@ func TestCredentialFieldsAbsentFromAccountResponse(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---- grpcClientIP tests (SEC-06) ----
|
||||
|
||||
// fakeAddr implements net.Addr for testing peer contexts.
|
||||
type fakeAddr struct {
|
||||
addr string
|
||||
network string
|
||||
}
|
||||
|
||||
func (a fakeAddr) String() string { return a.addr }
|
||||
func (a fakeAddr) Network() string { return a.network }
|
||||
|
||||
// TestGRPCClientIP_NoProxy verifies that when no trusted proxy is configured
|
||||
// the function returns the peer IP directly.
|
||||
func TestGRPCClientIP_NoProxy(t *testing.T) {
|
||||
ctx := peer.NewContext(context.Background(), &peer.Peer{
|
||||
Addr: fakeAddr{addr: "10.0.0.5:54321", network: "tcp"},
|
||||
})
|
||||
|
||||
got := grpcClientIP(ctx, nil)
|
||||
if got != "10.0.0.5" {
|
||||
t.Errorf("grpcClientIP(no proxy) = %q, want %q", got, "10.0.0.5")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGRPCClientIP_TrustedProxy_XForwardedFor verifies that when the peer
|
||||
// matches the trusted proxy, the real client IP is extracted from
|
||||
// x-forwarded-for metadata.
|
||||
func TestGRPCClientIP_TrustedProxy_XForwardedFor(t *testing.T) {
|
||||
proxyIP := net.ParseIP("192.168.1.1")
|
||||
|
||||
ctx := peer.NewContext(context.Background(), &peer.Peer{
|
||||
Addr: fakeAddr{addr: "192.168.1.1:12345", network: "tcp"},
|
||||
})
|
||||
md := metadata.Pairs("x-forwarded-for", "203.0.113.50, 10.0.0.1")
|
||||
ctx = metadata.NewIncomingContext(ctx, md)
|
||||
|
||||
got := grpcClientIP(ctx, proxyIP)
|
||||
if got != "203.0.113.50" {
|
||||
t.Errorf("grpcClientIP(xff) = %q, want %q", got, "203.0.113.50")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGRPCClientIP_TrustedProxy_XRealIP verifies that x-real-ip is preferred
|
||||
// over x-forwarded-for when both are present.
|
||||
func TestGRPCClientIP_TrustedProxy_XRealIP(t *testing.T) {
|
||||
proxyIP := net.ParseIP("192.168.1.1")
|
||||
|
||||
ctx := peer.NewContext(context.Background(), &peer.Peer{
|
||||
Addr: fakeAddr{addr: "192.168.1.1:12345", network: "tcp"},
|
||||
})
|
||||
md := metadata.Pairs(
|
||||
"x-real-ip", "198.51.100.10",
|
||||
"x-forwarded-for", "203.0.113.50",
|
||||
)
|
||||
ctx = metadata.NewIncomingContext(ctx, md)
|
||||
|
||||
got := grpcClientIP(ctx, proxyIP)
|
||||
if got != "198.51.100.10" {
|
||||
t.Errorf("grpcClientIP(x-real-ip preferred) = %q, want %q", got, "198.51.100.10")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGRPCClientIP_UntrustedPeer_IgnoresHeaders verifies that forwarded
|
||||
// headers are ignored when the peer does NOT match the trusted proxy.
|
||||
// Security: This prevents IP-spoofing by untrusted clients.
|
||||
func TestGRPCClientIP_UntrustedPeer_IgnoresHeaders(t *testing.T) {
|
||||
proxyIP := net.ParseIP("192.168.1.1")
|
||||
|
||||
// Peer is NOT the trusted proxy.
|
||||
ctx := peer.NewContext(context.Background(), &peer.Peer{
|
||||
Addr: fakeAddr{addr: "10.0.0.99:54321", network: "tcp"},
|
||||
})
|
||||
md := metadata.Pairs(
|
||||
"x-forwarded-for", "203.0.113.50",
|
||||
"x-real-ip", "198.51.100.10",
|
||||
)
|
||||
ctx = metadata.NewIncomingContext(ctx, md)
|
||||
|
||||
got := grpcClientIP(ctx, proxyIP)
|
||||
if got != "10.0.0.99" {
|
||||
t.Errorf("grpcClientIP(untrusted peer) = %q, want %q", got, "10.0.0.99")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGRPCClientIP_TrustedProxy_NoHeaders verifies that when the peer matches
|
||||
// the proxy but no forwarded headers are set, the peer IP is returned as fallback.
|
||||
func TestGRPCClientIP_TrustedProxy_NoHeaders(t *testing.T) {
|
||||
proxyIP := net.ParseIP("192.168.1.1")
|
||||
|
||||
ctx := peer.NewContext(context.Background(), &peer.Peer{
|
||||
Addr: fakeAddr{addr: "192.168.1.1:12345", network: "tcp"},
|
||||
})
|
||||
|
||||
got := grpcClientIP(ctx, proxyIP)
|
||||
if got != "192.168.1.1" {
|
||||
t.Errorf("grpcClientIP(proxy, no headers) = %q, want %q", got, "192.168.1.1")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGRPCClientIP_TrustedProxy_InvalidHeader verifies that invalid IPs in
|
||||
// headers are ignored and the peer IP is returned.
|
||||
func TestGRPCClientIP_TrustedProxy_InvalidHeader(t *testing.T) {
|
||||
proxyIP := net.ParseIP("192.168.1.1")
|
||||
|
||||
ctx := peer.NewContext(context.Background(), &peer.Peer{
|
||||
Addr: fakeAddr{addr: "192.168.1.1:12345", network: "tcp"},
|
||||
})
|
||||
md := metadata.Pairs("x-forwarded-for", "not-an-ip")
|
||||
ctx = metadata.NewIncomingContext(ctx, md)
|
||||
|
||||
got := grpcClientIP(ctx, proxyIP)
|
||||
if got != "192.168.1.1" {
|
||||
t.Errorf("grpcClientIP(invalid header) = %q, want %q", got, "192.168.1.1")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGRPCClientIP_NoPeer verifies that an empty string is returned when
|
||||
// there is no peer in the context.
|
||||
func TestGRPCClientIP_NoPeer(t *testing.T) {
|
||||
got := grpcClientIP(context.Background(), nil)
|
||||
if got != "" {
|
||||
t.Errorf("grpcClientIP(no peer) = %q, want %q", got, "")
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoginLockedAccountReturnsUnauthenticated verifies that a locked-out
|
||||
// account gets the same gRPC Unauthenticated / "invalid credentials" as a
|
||||
// wrong-password attempt, preventing user-enumeration via lockout
|
||||
// differentiation (SEC-02).
|
||||
func TestLoginLockedAccountReturnsUnauthenticated(t *testing.T) {
|
||||
e := newTestEnv(t)
|
||||
acct := e.createHumanAccount(t, "lockgrpc")
|
||||
|
||||
// Lower the lockout threshold so we don't need 10 failures.
|
||||
origThreshold := db.LockoutThreshold
|
||||
db.LockoutThreshold = 3
|
||||
t.Cleanup(func() { db.LockoutThreshold = origThreshold })
|
||||
|
||||
for range db.LockoutThreshold {
|
||||
if err := e.db.RecordLoginFailure(acct.ID); err != nil {
|
||||
t.Fatalf("RecordLoginFailure: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
locked, err := e.db.IsLockedOut(acct.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("IsLockedOut: %v", err)
|
||||
}
|
||||
if !locked {
|
||||
t.Fatal("expected account to be locked out after threshold failures")
|
||||
}
|
||||
|
||||
cl := mciasv1.NewAuthServiceClient(e.conn)
|
||||
|
||||
// Attempt login on the locked account.
|
||||
_, lockedErr := cl.Login(context.Background(), &mciasv1.LoginRequest{
|
||||
Username: "lockgrpc",
|
||||
Password: "testpass123",
|
||||
})
|
||||
if lockedErr == nil {
|
||||
t.Fatal("Login on locked account: expected error, got nil")
|
||||
}
|
||||
|
||||
// Attempt login with wrong password for comparison.
|
||||
_, wrongErr := cl.Login(context.Background(), &mciasv1.LoginRequest{
|
||||
Username: "lockgrpc",
|
||||
Password: "wrongpassword",
|
||||
})
|
||||
if wrongErr == nil {
|
||||
t.Fatal("Login with wrong password: expected error, got nil")
|
||||
}
|
||||
|
||||
lockedSt, _ := status.FromError(lockedErr)
|
||||
wrongSt, _ := status.FromError(wrongErr)
|
||||
|
||||
// Both must return Unauthenticated, not ResourceExhausted.
|
||||
if lockedSt.Code() != codes.Unauthenticated {
|
||||
t.Errorf("locked: got code %v, want Unauthenticated", lockedSt.Code())
|
||||
}
|
||||
if wrongSt.Code() != codes.Unauthenticated {
|
||||
t.Errorf("wrong password: got code %v, want Unauthenticated", wrongSt.Code())
|
||||
}
|
||||
|
||||
// Messages must be identical.
|
||||
if lockedSt.Message() != wrongSt.Message() {
|
||||
t.Errorf("locked message %q differs from wrong-password message %q",
|
||||
lockedSt.Message(), wrongSt.Message())
|
||||
}
|
||||
if lockedSt.Message() != "invalid credentials" {
|
||||
t.Errorf("locked message = %q, want %q", lockedSt.Message(), "invalid credentials")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,11 @@ func (t *tokenServiceServer) ValidateToken(_ context.Context, req *mciasv1.Valid
|
||||
return &mciasv1.ValidateTokenResponse{Valid: false}, nil
|
||||
}
|
||||
|
||||
claims, err := token.ValidateToken(t.s.pubKey, tokenStr, t.s.cfg.Tokens.Issuer)
|
||||
pubKey, pkErr := t.s.vault.PubKey()
|
||||
if pkErr != nil {
|
||||
return &mciasv1.ValidateTokenResponse{Valid: false}, nil
|
||||
}
|
||||
claims, err := token.ValidateToken(pubKey, tokenStr, t.s.cfg.Tokens.Issuer)
|
||||
if err != nil {
|
||||
return &mciasv1.ValidateTokenResponse{Valid: false}, nil
|
||||
}
|
||||
@@ -67,21 +71,24 @@ func (ts *tokenServiceServer) IssueServiceToken(ctx context.Context, req *mciasv
|
||||
return nil, status.Error(codes.InvalidArgument, "token issue is only for system accounts")
|
||||
}
|
||||
|
||||
tokenStr, claims, err := token.IssueToken(ts.s.privKey, ts.s.cfg.Tokens.Issuer, acct.UUID, nil, ts.s.cfg.ServiceExpiry())
|
||||
privKey, pkErr := ts.s.vault.PrivKey()
|
||||
if pkErr != nil {
|
||||
return nil, status.Error(codes.Unavailable, "vault sealed")
|
||||
}
|
||||
tokenStr, claims, err := token.IssueToken(privKey, ts.s.cfg.Tokens.Issuer, acct.UUID, nil, ts.s.cfg.ServiceExpiry())
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "internal error")
|
||||
}
|
||||
|
||||
// Revoke existing system token if any.
|
||||
// Atomically revoke existing system token (if any), track the new token,
|
||||
// and update system_tokens — all in a single transaction.
|
||||
// Security: prevents inconsistent state if a crash occurs mid-operation.
|
||||
var oldJTI string
|
||||
existing, err := ts.s.db.GetSystemToken(acct.ID)
|
||||
if err == nil && existing != nil {
|
||||
_ = ts.s.db.RevokeToken(existing.JTI, "rotated")
|
||||
oldJTI = existing.JTI
|
||||
}
|
||||
|
||||
if err := ts.s.db.TrackToken(claims.JTI, acct.ID, claims.IssuedAt, claims.ExpiresAt); err != nil {
|
||||
return nil, status.Error(codes.Internal, "internal error")
|
||||
}
|
||||
if err := ts.s.db.SetSystemToken(acct.ID, claims.JTI, claims.ExpiresAt); err != nil {
|
||||
if err := ts.s.db.IssueSystemToken(oldJTI, claims.JTI, acct.ID, claims.IssuedAt, claims.ExpiresAt); err != nil {
|
||||
return nil, status.Error(codes.Internal, "internal error")
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ed25519"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -27,6 +26,7 @@ import (
|
||||
"git.wntrmute.dev/kyle/mcias/internal/db"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/policy"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/token"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/vault"
|
||||
)
|
||||
|
||||
// contextKey is the unexported type for context keys in this package, preventing
|
||||
@@ -90,12 +90,18 @@ func (rw *responseWriter) WriteHeader(code int) {
|
||||
// RequireAuth returns middleware that validates a Bearer JWT and injects the
|
||||
// claims into the request context. Returns 401 on any auth failure.
|
||||
//
|
||||
// The public key is read from the vault at request time so that the middleware
|
||||
// works correctly across seal/unseal transitions. When the vault is sealed,
|
||||
// the sealed middleware (RequireUnsealed) prevents reaching this handler, but
|
||||
// the vault check here provides defense in depth (fail closed).
|
||||
//
|
||||
// Security: Token validation order:
|
||||
// 1. Extract Bearer token from Authorization header.
|
||||
// 2. Validate the JWT (alg=EdDSA, signature, expiry, issuer).
|
||||
// 3. Check the JTI against the revocation table in the database.
|
||||
// 4. Inject validated claims into context for downstream handlers.
|
||||
func RequireAuth(pubKey ed25519.PublicKey, database *db.DB, issuer string) func(http.Handler) http.Handler {
|
||||
// 2. Read public key from vault (fail closed if sealed).
|
||||
// 3. Validate the JWT (alg=EdDSA, signature, expiry, issuer).
|
||||
// 4. Check the JTI against the revocation table in the database.
|
||||
// 5. Inject validated claims into context for downstream handlers.
|
||||
func RequireAuth(v *vault.Vault, database *db.DB, issuer string) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
tokenStr, err := extractBearerToken(r)
|
||||
@@ -104,6 +110,14 @@ func RequireAuth(pubKey ed25519.PublicKey, database *db.DB, issuer string) func(
|
||||
return
|
||||
}
|
||||
|
||||
// Security: read the public key from vault at request time.
|
||||
// If the vault is sealed, fail closed with 503.
|
||||
pubKey, err := v.PubKey()
|
||||
if err != nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "vault sealed", "vault_sealed")
|
||||
return
|
||||
}
|
||||
|
||||
claims, err := token.ValidateToken(pubKey, tokenStr, issuer)
|
||||
if err != nil {
|
||||
// Security: Map all token errors to a generic 401; do not
|
||||
@@ -437,3 +451,47 @@ func RequirePolicy(
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// RequireUnsealed returns middleware that blocks requests when the vault is sealed.
|
||||
//
|
||||
// Exempt paths (served normally even when sealed):
|
||||
// - GET /v1/health, GET /v1/vault/status, POST /v1/vault/unseal
|
||||
// - GET /unseal, POST /unseal
|
||||
// - GET /static/* (CSS/JS needed by the unseal page)
|
||||
//
|
||||
// API paths (/v1/*) receive a JSON 503 response. All other paths (UI) receive
|
||||
// a 302 redirect to /unseal.
|
||||
//
|
||||
// Security: This middleware is the first in the chain (after global security
|
||||
// headers). It ensures no authenticated or data-serving handler runs while the
|
||||
// vault is sealed and key material is unavailable.
|
||||
func RequireUnsealed(v *vault.Vault) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !v.IsSealed() {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
path := r.URL.Path
|
||||
|
||||
// Exempt paths that must work while sealed.
|
||||
if path == "/v1/health" || path == "/v1/vault/status" ||
|
||||
path == "/v1/vault/unseal" ||
|
||||
path == "/unseal" ||
|
||||
strings.HasPrefix(path, "/static/") {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// API paths: JSON 503.
|
||||
if strings.HasPrefix(path, "/v1/") {
|
||||
writeError(w, http.StatusServiceUnavailable, "vault sealed", "vault_sealed")
|
||||
return
|
||||
}
|
||||
|
||||
// UI paths: redirect to unseal page.
|
||||
http.Redirect(w, r, "/unseal", http.StatusFound)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"git.wntrmute.dev/kyle/mcias/internal/db"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/model"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/token"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/vault"
|
||||
)
|
||||
|
||||
func generateTestKey(t *testing.T) (ed25519.PublicKey, ed25519.PrivateKey) {
|
||||
@@ -26,6 +27,15 @@ func generateTestKey(t *testing.T) (ed25519.PublicKey, ed25519.PrivateKey) {
|
||||
return pub, priv
|
||||
}
|
||||
|
||||
func testVault(t *testing.T, priv ed25519.PrivateKey, pub ed25519.PublicKey) *vault.Vault {
|
||||
t.Helper()
|
||||
mk := make([]byte, 32)
|
||||
if _, err := rand.Read(mk); err != nil {
|
||||
t.Fatalf("generate master key: %v", err)
|
||||
}
|
||||
return vault.NewUnsealed(mk, priv, pub)
|
||||
}
|
||||
|
||||
func openTestDB(t *testing.T) *db.DB {
|
||||
t.Helper()
|
||||
database, err := db.Open(":memory:")
|
||||
@@ -96,7 +106,7 @@ func TestRequireAuthValid(t *testing.T) {
|
||||
tokenStr := issueAndTrackToken(t, priv, database, acct.ID, []string{"reader"})
|
||||
|
||||
reached := false
|
||||
handler := RequireAuth(pub, database, testIssuer)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
handler := RequireAuth(testVault(t, priv, pub), database, testIssuer)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
reached = true
|
||||
claims := ClaimsFromContext(r.Context())
|
||||
if claims == nil {
|
||||
@@ -123,7 +133,7 @@ func TestRequireAuthMissingHeader(t *testing.T) {
|
||||
_ = priv
|
||||
database := openTestDB(t)
|
||||
|
||||
handler := RequireAuth(pub, database, testIssuer)(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
handler := RequireAuth(testVault(t, priv, pub), database, testIssuer)(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
t.Error("handler should not be reached without auth")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
@@ -138,10 +148,10 @@ func TestRequireAuthMissingHeader(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRequireAuthInvalidToken(t *testing.T) {
|
||||
pub, _ := generateTestKey(t)
|
||||
pub, priv := generateTestKey(t)
|
||||
database := openTestDB(t)
|
||||
|
||||
handler := RequireAuth(pub, database, testIssuer)(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
handler := RequireAuth(testVault(t, priv, pub), database, testIssuer)(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
t.Error("handler should not be reached with invalid token")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
@@ -176,7 +186,7 @@ func TestRequireAuthRevokedToken(t *testing.T) {
|
||||
t.Fatalf("RevokeToken: %v", err)
|
||||
}
|
||||
|
||||
handler := RequireAuth(pub, database, testIssuer)(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
handler := RequireAuth(testVault(t, priv, pub), database, testIssuer)(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
t.Error("handler should not be reached with revoked token")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
@@ -201,7 +211,7 @@ func TestRequireAuthExpiredToken(t *testing.T) {
|
||||
t.Fatalf("IssueToken: %v", err)
|
||||
}
|
||||
|
||||
handler := RequireAuth(pub, database, testIssuer)(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
handler := RequireAuth(testVault(t, priv, pub), database, testIssuer)(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
t.Error("handler should not be reached with expired token")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
@@ -178,6 +178,9 @@ const (
|
||||
EventPGCredAccessed = "pgcred_accessed"
|
||||
EventPGCredUpdated = "pgcred_updated" //nolint:gosec // G101: audit event type string, not a credential
|
||||
|
||||
EventVaultSealed = "vault_sealed"
|
||||
EventVaultUnsealed = "vault_unsealed"
|
||||
|
||||
EventTagAdded = "tag_added"
|
||||
EventTagRemoved = "tag_removed"
|
||||
|
||||
@@ -207,8 +210,25 @@ const (
|
||||
EventPGCredAccessRevoked = "pgcred_access_revoked" //nolint:gosec // G101: audit event type, not a credential
|
||||
|
||||
EventPasswordChanged = "password_changed"
|
||||
|
||||
EventTokenDelegateGranted = "token_delegate_granted"
|
||||
EventTokenDelegateRevoked = "token_delegate_revoked"
|
||||
)
|
||||
|
||||
// ServiceAccountDelegate records that a specific account has been granted
|
||||
// permission to issue tokens for a given system account. Only admins can
|
||||
// add or remove delegates; delegates can issue/rotate tokens for that specific
|
||||
// system account and nothing else.
|
||||
type ServiceAccountDelegate struct {
|
||||
GrantedAt time.Time `json:"granted_at"`
|
||||
GrantedBy *int64 `json:"-"`
|
||||
GranteeUUID string `json:"grantee_id"`
|
||||
GranteeName string `json:"grantee_username"`
|
||||
ID int64 `json:"-"`
|
||||
AccountID int64 `json:"-"`
|
||||
GranteeID int64 `json:"-"`
|
||||
}
|
||||
|
||||
// PolicyRuleRecord is the database representation of a policy rule.
|
||||
// RuleJSON holds a JSON-encoded policy.RuleBody (all match and effect fields).
|
||||
// The ID, Priority, and Description are stored as dedicated columns.
|
||||
|
||||
@@ -217,6 +217,9 @@ func (s *Server) handleCreatePolicyRule(w http.ResponseWriter, r *http.Request)
|
||||
s.writeAudit(r, model.EventPolicyRuleCreated, createdBy, nil,
|
||||
fmt.Sprintf(`{"rule_id":%d,"description":%q}`, rec.ID, rec.Description))
|
||||
|
||||
// Reload the in-memory engine so the new rule takes effect immediately.
|
||||
s.reloadPolicyEngine()
|
||||
|
||||
rv, err := policyRuleToResponse(rec)
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusInternalServerError, "internal error", "internal_error")
|
||||
@@ -325,6 +328,9 @@ func (s *Server) handleUpdatePolicyRule(w http.ResponseWriter, r *http.Request)
|
||||
s.writeAudit(r, model.EventPolicyRuleUpdated, actorID, nil,
|
||||
fmt.Sprintf(`{"rule_id":%d}`, rec.ID))
|
||||
|
||||
// Reload the in-memory engine so rule changes take effect immediately.
|
||||
s.reloadPolicyEngine()
|
||||
|
||||
updated, err := s.db.GetPolicyRule(rec.ID)
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusInternalServerError, "internal error", "internal_error")
|
||||
@@ -358,6 +364,9 @@ func (s *Server) handleDeletePolicyRule(w http.ResponseWriter, r *http.Request)
|
||||
s.writeAudit(r, model.EventPolicyRuleDeleted, actorID, nil,
|
||||
fmt.Sprintf(`{"rule_id":%d,"description":%q}`, rec.ID, rec.Description))
|
||||
|
||||
// Reload the in-memory engine so the deleted rule is removed immediately.
|
||||
s.reloadPolicyEngine()
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
|
||||
@@ -10,46 +10,186 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.wntrmute.dev/kyle/mcias/internal/audit"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/auth"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/config"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/crypto"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/db"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/middleware"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/model"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/policy"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/token"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/ui"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/validate"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/vault"
|
||||
"git.wntrmute.dev/kyle/mcias/web"
|
||||
)
|
||||
|
||||
// Server holds the dependencies injected into all handlers.
|
||||
type Server struct {
|
||||
db *db.DB
|
||||
cfg *config.Config
|
||||
logger *slog.Logger
|
||||
privKey ed25519.PrivateKey
|
||||
pubKey ed25519.PublicKey
|
||||
masterKey []byte
|
||||
db *db.DB
|
||||
cfg *config.Config
|
||||
logger *slog.Logger
|
||||
vault *vault.Vault
|
||||
polEng *policy.Engine
|
||||
}
|
||||
|
||||
// New creates a Server with the given dependencies.
|
||||
func New(database *db.DB, cfg *config.Config, priv ed25519.PrivateKey, pub ed25519.PublicKey, masterKey []byte, logger *slog.Logger) *Server {
|
||||
// The policy engine is initialised and loaded from the database on construction.
|
||||
func New(database *db.DB, cfg *config.Config, v *vault.Vault, logger *slog.Logger) *Server {
|
||||
eng := policy.NewEngine()
|
||||
if err := loadEngineRules(eng, database); err != nil {
|
||||
logger.Warn("policy engine initial load failed; built-in defaults will apply", "error", err)
|
||||
}
|
||||
return &Server{
|
||||
db: database,
|
||||
cfg: cfg,
|
||||
privKey: priv,
|
||||
pubKey: pub,
|
||||
masterKey: masterKey,
|
||||
logger: logger,
|
||||
db: database,
|
||||
cfg: cfg,
|
||||
vault: v,
|
||||
logger: logger,
|
||||
polEng: eng,
|
||||
}
|
||||
}
|
||||
|
||||
// loadEngineRules reads all policy rules from the database and loads them into eng.
|
||||
// Enabled/disabled and validity-window filtering is handled by the engine itself.
|
||||
func loadEngineRules(eng *policy.Engine, database *db.DB) error {
|
||||
records, err := database.ListPolicyRules(false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("list policy rules: %w", err)
|
||||
}
|
||||
prs := make([]policy.PolicyRecord, len(records))
|
||||
for i, r := range records {
|
||||
prs[i] = policy.PolicyRecord{
|
||||
ID: r.ID,
|
||||
Priority: r.Priority,
|
||||
Description: r.Description,
|
||||
RuleJSON: r.RuleJSON,
|
||||
Enabled: r.Enabled,
|
||||
NotBefore: r.NotBefore,
|
||||
ExpiresAt: r.ExpiresAt,
|
||||
}
|
||||
}
|
||||
return eng.SetRules(prs)
|
||||
}
|
||||
|
||||
// reloadPolicyEngine reloads operator rules from the database into the engine.
|
||||
// Called after any create, update, or delete of a policy rule so that the
|
||||
// in-memory cache stays consistent with the database.
|
||||
func (s *Server) reloadPolicyEngine() {
|
||||
if err := loadEngineRules(s.polEng, s.db); err != nil {
|
||||
s.logger.Error("reload policy engine", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
// accountTypeLookup returns an AccountTypeLookup closure that resolves the
|
||||
// account type ("human" or "system") for the given subject UUID. Used by the
|
||||
// RequirePolicy middleware to populate PolicyInput.AccountType.
|
||||
func (s *Server) accountTypeLookup() middleware.AccountTypeLookup {
|
||||
return func(subjectUUID string) string {
|
||||
acct, err := s.db.GetAccountByUUID(subjectUUID)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(acct.AccountType)
|
||||
}
|
||||
}
|
||||
|
||||
// policyDenyLogger returns a PolicyDenyLogger that records policy denials in
|
||||
// the audit log as EventPolicyDeny events.
|
||||
func (s *Server) policyDenyLogger() middleware.PolicyDenyLogger {
|
||||
return func(r *http.Request, claims *token.Claims, action policy.Action, res policy.Resource, matchedRuleID int64) {
|
||||
s.writeAudit(r, model.EventPolicyDeny, nil, nil,
|
||||
fmt.Sprintf(`{"subject":%q,"action":%q,"resource_type":%q,"rule_id":%d}`,
|
||||
claims.Subject, action, res.Type, matchedRuleID))
|
||||
}
|
||||
}
|
||||
|
||||
// buildAccountResource assembles the policy.Resource for endpoints that
|
||||
// target a specific account ({id} path parameter). Looks up the account's
|
||||
// UUID, username (for ServiceName), and tags from the database.
|
||||
// Returns an empty Resource on lookup failure; deny-by-default in the engine
|
||||
// means this safely falls through to a denial for owner-scoped rules.
|
||||
func (s *Server) buildAccountResource(r *http.Request, _ *token.Claims) policy.Resource {
|
||||
id := r.PathValue("id")
|
||||
if id == "" {
|
||||
return policy.Resource{}
|
||||
}
|
||||
acct, err := s.db.GetAccountByUUID(id)
|
||||
if err != nil {
|
||||
return policy.Resource{}
|
||||
}
|
||||
tags, _ := s.db.GetAccountTags(acct.ID)
|
||||
return policy.Resource{
|
||||
OwnerUUID: acct.UUID,
|
||||
ServiceName: acct.Username,
|
||||
Tags: tags,
|
||||
}
|
||||
}
|
||||
|
||||
// buildTokenResource assembles the policy.Resource for token-issue requests.
|
||||
// The request body contains account_id (UUID); the resource owner is that account.
|
||||
// Because this builder reads the body it must be called before the body is
|
||||
// consumed by the handler — the middleware calls it before invoking next.
|
||||
func (s *Server) buildTokenResource(r *http.Request, _ *token.Claims) policy.Resource {
|
||||
// Peek at the account_id without consuming the body.
|
||||
// We read the body into a small wrapper struct to get the target UUID.
|
||||
// The actual handler re-reads the body via decodeJSON, so this is safe
|
||||
// because http.MaxBytesReader is applied by the handler, not here.
|
||||
var peek struct {
|
||||
AccountID string `json:"account_id"`
|
||||
}
|
||||
body, err := io.ReadAll(io.LimitReader(r.Body, maxJSONBytes))
|
||||
if err != nil {
|
||||
return policy.Resource{}
|
||||
}
|
||||
// Restore the body for the downstream handler.
|
||||
r.Body = io.NopCloser(strings.NewReader(string(body)))
|
||||
if err := json.Unmarshal(body, &peek); err != nil || peek.AccountID == "" {
|
||||
return policy.Resource{}
|
||||
}
|
||||
acct, err := s.db.GetAccountByUUID(peek.AccountID)
|
||||
if err != nil {
|
||||
return policy.Resource{}
|
||||
}
|
||||
tags, _ := s.db.GetAccountTags(acct.ID)
|
||||
return policy.Resource{
|
||||
OwnerUUID: acct.UUID,
|
||||
ServiceName: acct.Username,
|
||||
Tags: tags,
|
||||
}
|
||||
}
|
||||
|
||||
// buildJTIResource assembles the policy.Resource for token-revoke requests.
|
||||
// Looks up the token record by {jti} to identify the owning account.
|
||||
func (s *Server) buildJTIResource(r *http.Request, _ *token.Claims) policy.Resource {
|
||||
jti := r.PathValue("jti")
|
||||
if jti == "" {
|
||||
return policy.Resource{}
|
||||
}
|
||||
rec, err := s.db.GetTokenRecord(jti)
|
||||
if err != nil {
|
||||
return policy.Resource{}
|
||||
}
|
||||
acct, err := s.db.GetAccountByID(rec.AccountID)
|
||||
if err != nil {
|
||||
return policy.Resource{}
|
||||
}
|
||||
tags, _ := s.db.GetAccountTags(acct.ID)
|
||||
return policy.Resource{
|
||||
OwnerUUID: acct.UUID,
|
||||
ServiceName: acct.Username,
|
||||
Tags: tags,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,69 +247,146 @@ func (s *Server) Handler() http.Handler {
|
||||
_, _ = w.Write(specYAML)
|
||||
})))
|
||||
|
||||
// Vault endpoints (exempt from sealed middleware and auth).
|
||||
unsealRateLimit := middleware.RateLimit(3, 5, trustedProxy)
|
||||
mux.Handle("POST /v1/vault/unseal", unsealRateLimit(http.HandlerFunc(s.handleUnseal)))
|
||||
mux.HandleFunc("GET /v1/vault/status", s.handleVaultStatus)
|
||||
mux.Handle("POST /v1/vault/seal", middleware.RequireAuth(s.vault, s.db, s.cfg.Tokens.Issuer)(middleware.RequireRole("admin")(http.HandlerFunc(s.handleSeal))))
|
||||
|
||||
// Authenticated endpoints.
|
||||
requireAuth := middleware.RequireAuth(s.pubKey, s.db, s.cfg.Tokens.Issuer)
|
||||
requireAdmin := func(h http.Handler) http.Handler {
|
||||
return requireAuth(middleware.RequireRole("admin")(h))
|
||||
requireAuth := middleware.RequireAuth(s.vault, s.db, s.cfg.Tokens.Issuer)
|
||||
|
||||
// Policy middleware factory: chains requireAuth → RequirePolicy → next.
|
||||
// All protected endpoints use this instead of the old requireAdmin wrapper
|
||||
// so that operator-defined policy rules (not just the admin role) control
|
||||
// access. The built-in admin wildcard rule (ID -1) preserves existing
|
||||
// admin behaviour; additional operator rules can grant non-admin accounts
|
||||
// access to specific actions.
|
||||
//
|
||||
// Security: deny-wins + default-deny in the engine mean that any
|
||||
// misconfiguration results in 403, never silent permit.
|
||||
acctTypeLookup := s.accountTypeLookup()
|
||||
denyLogger := s.policyDenyLogger()
|
||||
requirePolicy := func(
|
||||
action policy.Action,
|
||||
resType policy.ResourceType,
|
||||
builder middleware.ResourceBuilder,
|
||||
) func(http.Handler) http.Handler {
|
||||
pol := middleware.RequirePolicy(s.polEng, action, resType, builder, acctTypeLookup, denyLogger)
|
||||
return func(next http.Handler) http.Handler {
|
||||
return requireAuth(pol(next))
|
||||
}
|
||||
}
|
||||
|
||||
// Auth endpoints (require valid token).
|
||||
// Resource builders for endpoints that target a specific account or token.
|
||||
buildAcct := middleware.ResourceBuilder(s.buildAccountResource)
|
||||
buildToken := middleware.ResourceBuilder(s.buildTokenResource)
|
||||
buildJTI := middleware.ResourceBuilder(s.buildJTIResource)
|
||||
|
||||
// Auth endpoints (require valid token; self-service rules in built-in defaults
|
||||
// allow any authenticated principal to perform these operations).
|
||||
mux.Handle("POST /v1/auth/logout", requireAuth(http.HandlerFunc(s.handleLogout)))
|
||||
mux.Handle("POST /v1/auth/renew", requireAuth(http.HandlerFunc(s.handleRenew)))
|
||||
mux.Handle("POST /v1/auth/totp/enroll", requireAuth(http.HandlerFunc(s.handleTOTPEnroll)))
|
||||
mux.Handle("POST /v1/auth/totp/confirm", requireAuth(http.HandlerFunc(s.handleTOTPConfirm)))
|
||||
|
||||
// Admin-only endpoints.
|
||||
mux.Handle("DELETE /v1/auth/totp", requireAdmin(http.HandlerFunc(s.handleTOTPRemove)))
|
||||
mux.Handle("POST /v1/token/issue", requireAdmin(http.HandlerFunc(s.handleTokenIssue)))
|
||||
mux.Handle("DELETE /v1/token/{jti}", requireAdmin(http.HandlerFunc(s.handleTokenRevoke)))
|
||||
mux.Handle("GET /v1/accounts", requireAdmin(http.HandlerFunc(s.handleListAccounts)))
|
||||
mux.Handle("POST /v1/accounts", requireAdmin(http.HandlerFunc(s.handleCreateAccount)))
|
||||
mux.Handle("GET /v1/accounts/{id}", requireAdmin(http.HandlerFunc(s.handleGetAccount)))
|
||||
mux.Handle("PATCH /v1/accounts/{id}", requireAdmin(http.HandlerFunc(s.handleUpdateAccount)))
|
||||
mux.Handle("DELETE /v1/accounts/{id}", requireAdmin(http.HandlerFunc(s.handleDeleteAccount)))
|
||||
mux.Handle("GET /v1/accounts/{id}/roles", requireAdmin(http.HandlerFunc(s.handleGetRoles)))
|
||||
mux.Handle("PUT /v1/accounts/{id}/roles", requireAdmin(http.HandlerFunc(s.handleSetRoles)))
|
||||
mux.Handle("POST /v1/accounts/{id}/roles", requireAdmin(http.HandlerFunc(s.handleGrantRole)))
|
||||
mux.Handle("DELETE /v1/accounts/{id}/roles/{role}", requireAdmin(http.HandlerFunc(s.handleRevokeRole)))
|
||||
mux.Handle("GET /v1/accounts/{id}/pgcreds", requireAdmin(http.HandlerFunc(s.handleGetPGCreds)))
|
||||
mux.Handle("PUT /v1/accounts/{id}/pgcreds", requireAdmin(http.HandlerFunc(s.handleSetPGCreds)))
|
||||
mux.Handle("GET /v1/audit", requireAdmin(http.HandlerFunc(s.handleListAudit)))
|
||||
mux.Handle("GET /v1/accounts/{id}/tags", requireAdmin(http.HandlerFunc(s.handleGetTags)))
|
||||
mux.Handle("PUT /v1/accounts/{id}/tags", requireAdmin(http.HandlerFunc(s.handleSetTags)))
|
||||
mux.Handle("PUT /v1/accounts/{id}/password", requireAdmin(http.HandlerFunc(s.handleAdminSetPassword)))
|
||||
// Policy-gated endpoints (formerly admin-only; now controlled by the engine).
|
||||
mux.Handle("DELETE /v1/auth/totp",
|
||||
requirePolicy(policy.ActionRemoveTOTP, policy.ResourceTOTP, buildAcct)(http.HandlerFunc(s.handleTOTPRemove)))
|
||||
mux.Handle("POST /v1/token/issue",
|
||||
requirePolicy(policy.ActionIssueToken, policy.ResourceToken, buildToken)(http.HandlerFunc(s.handleTokenIssue)))
|
||||
mux.Handle("DELETE /v1/token/{jti}",
|
||||
requirePolicy(policy.ActionRevokeToken, policy.ResourceToken, buildJTI)(http.HandlerFunc(s.handleTokenRevoke)))
|
||||
mux.Handle("GET /v1/accounts",
|
||||
requirePolicy(policy.ActionListAccounts, policy.ResourceAccount, nil)(http.HandlerFunc(s.handleListAccounts)))
|
||||
mux.Handle("POST /v1/accounts",
|
||||
requirePolicy(policy.ActionCreateAccount, policy.ResourceAccount, nil)(http.HandlerFunc(s.handleCreateAccount)))
|
||||
mux.Handle("GET /v1/accounts/{id}",
|
||||
requirePolicy(policy.ActionReadAccount, policy.ResourceAccount, buildAcct)(http.HandlerFunc(s.handleGetAccount)))
|
||||
mux.Handle("PATCH /v1/accounts/{id}",
|
||||
requirePolicy(policy.ActionUpdateAccount, policy.ResourceAccount, buildAcct)(http.HandlerFunc(s.handleUpdateAccount)))
|
||||
mux.Handle("DELETE /v1/accounts/{id}",
|
||||
requirePolicy(policy.ActionDeleteAccount, policy.ResourceAccount, buildAcct)(http.HandlerFunc(s.handleDeleteAccount)))
|
||||
mux.Handle("GET /v1/accounts/{id}/roles",
|
||||
requirePolicy(policy.ActionReadRoles, policy.ResourceAccount, buildAcct)(http.HandlerFunc(s.handleGetRoles)))
|
||||
mux.Handle("PUT /v1/accounts/{id}/roles",
|
||||
requirePolicy(policy.ActionWriteRoles, policy.ResourceAccount, buildAcct)(http.HandlerFunc(s.handleSetRoles)))
|
||||
mux.Handle("POST /v1/accounts/{id}/roles",
|
||||
requirePolicy(policy.ActionWriteRoles, policy.ResourceAccount, buildAcct)(http.HandlerFunc(s.handleGrantRole)))
|
||||
mux.Handle("DELETE /v1/accounts/{id}/roles/{role}",
|
||||
requirePolicy(policy.ActionWriteRoles, policy.ResourceAccount, buildAcct)(http.HandlerFunc(s.handleRevokeRole)))
|
||||
mux.Handle("GET /v1/pgcreds", requireAuth(http.HandlerFunc(s.handleListAccessiblePGCreds)))
|
||||
mux.Handle("GET /v1/accounts/{id}/pgcreds",
|
||||
requirePolicy(policy.ActionReadPGCreds, policy.ResourcePGCreds, buildAcct)(http.HandlerFunc(s.handleGetPGCreds)))
|
||||
mux.Handle("PUT /v1/accounts/{id}/pgcreds",
|
||||
requirePolicy(policy.ActionWritePGCreds, policy.ResourcePGCreds, buildAcct)(http.HandlerFunc(s.handleSetPGCreds)))
|
||||
mux.Handle("GET /v1/audit",
|
||||
requirePolicy(policy.ActionReadAudit, policy.ResourceAuditLog, nil)(http.HandlerFunc(s.handleListAudit)))
|
||||
mux.Handle("GET /v1/accounts/{id}/tags",
|
||||
requirePolicy(policy.ActionReadTags, policy.ResourceAccount, buildAcct)(http.HandlerFunc(s.handleGetTags)))
|
||||
mux.Handle("PUT /v1/accounts/{id}/tags",
|
||||
requirePolicy(policy.ActionWriteTags, policy.ResourceAccount, buildAcct)(http.HandlerFunc(s.handleSetTags)))
|
||||
mux.Handle("PUT /v1/accounts/{id}/password",
|
||||
requirePolicy(policy.ActionUpdateAccount, policy.ResourceAccount, buildAcct)(http.HandlerFunc(s.handleAdminSetPassword)))
|
||||
|
||||
// Self-service password change (requires valid token; actor must match target account).
|
||||
mux.Handle("PUT /v1/auth/password", requireAuth(http.HandlerFunc(s.handleChangePassword)))
|
||||
mux.Handle("GET /v1/policy/rules", requireAdmin(http.HandlerFunc(s.handleListPolicyRules)))
|
||||
mux.Handle("POST /v1/policy/rules", requireAdmin(http.HandlerFunc(s.handleCreatePolicyRule)))
|
||||
mux.Handle("GET /v1/policy/rules/{id}", requireAdmin(http.HandlerFunc(s.handleGetPolicyRule)))
|
||||
mux.Handle("PATCH /v1/policy/rules/{id}", requireAdmin(http.HandlerFunc(s.handleUpdatePolicyRule)))
|
||||
mux.Handle("DELETE /v1/policy/rules/{id}", requireAdmin(http.HandlerFunc(s.handleDeletePolicyRule)))
|
||||
mux.Handle("GET /v1/policy/rules",
|
||||
requirePolicy(policy.ActionListRules, policy.ResourcePolicy, nil)(http.HandlerFunc(s.handleListPolicyRules)))
|
||||
mux.Handle("POST /v1/policy/rules",
|
||||
requirePolicy(policy.ActionManageRules, policy.ResourcePolicy, nil)(http.HandlerFunc(s.handleCreatePolicyRule)))
|
||||
mux.Handle("GET /v1/policy/rules/{id}",
|
||||
requirePolicy(policy.ActionListRules, policy.ResourcePolicy, nil)(http.HandlerFunc(s.handleGetPolicyRule)))
|
||||
mux.Handle("PATCH /v1/policy/rules/{id}",
|
||||
requirePolicy(policy.ActionManageRules, policy.ResourcePolicy, nil)(http.HandlerFunc(s.handleUpdatePolicyRule)))
|
||||
mux.Handle("DELETE /v1/policy/rules/{id}",
|
||||
requirePolicy(policy.ActionManageRules, policy.ResourcePolicy, nil)(http.HandlerFunc(s.handleDeletePolicyRule)))
|
||||
|
||||
// UI routes (HTMX-based management frontend).
|
||||
uiSrv, err := ui.New(s.db, s.cfg, s.privKey, s.pubKey, s.masterKey, s.logger)
|
||||
uiSrv, err := ui.New(s.db, s.cfg, s.vault, s.logger)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("ui: init failed: %v", err))
|
||||
}
|
||||
uiSrv.Register(mux)
|
||||
|
||||
// Apply global middleware: request logging.
|
||||
// Apply global middleware: request logging, sealed check, and security headers.
|
||||
// Rate limiting is applied per-route above (login, token/validate).
|
||||
var root http.Handler = mux
|
||||
// Security: RequireUnsealed runs after the mux (so exempt routes can be
|
||||
// routed) but before the logger (so sealed-blocked requests are still logged).
|
||||
root = middleware.RequireUnsealed(s.vault)(root)
|
||||
root = middleware.RequestLogger(s.logger)(root)
|
||||
|
||||
// Security (SEC-04): apply baseline security headers to ALL responses
|
||||
// (both API and UI). These headers are safe for every content type:
|
||||
// - X-Content-Type-Options prevents MIME-sniffing attacks.
|
||||
// - Strict-Transport-Security enforces HTTPS for 2 years.
|
||||
// - Cache-Control prevents caching of authenticated responses.
|
||||
// The UI sub-mux already sets these plus CSP/X-Frame-Options/Referrer-Policy
|
||||
// which will override where needed (last Set wins before WriteHeader).
|
||||
root = globalSecurityHeaders(root)
|
||||
|
||||
return root
|
||||
}
|
||||
|
||||
// ---- Public handlers ----
|
||||
|
||||
func (s *Server) handleHealth(w http.ResponseWriter, _ *http.Request) {
|
||||
if s.vault.IsSealed() {
|
||||
writeJSON(w, http.StatusOK, map[string]string{"status": "sealed"})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||||
}
|
||||
|
||||
// handlePublicKey returns the server's Ed25519 public key in JWK format.
|
||||
// This allows relying parties to independently verify JWTs.
|
||||
func (s *Server) handlePublicKey(w http.ResponseWriter, _ *http.Request) {
|
||||
pubKey, err := s.vault.PubKey()
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusServiceUnavailable, "vault sealed", "vault_sealed")
|
||||
return
|
||||
}
|
||||
// Encode the Ed25519 public key as a JWK (RFC 8037).
|
||||
// The "x" parameter is the base64url-encoded public key bytes.
|
||||
jwk := map[string]string{
|
||||
@@ -177,7 +394,7 @@ func (s *Server) handlePublicKey(w http.ResponseWriter, _ *http.Request) {
|
||||
"crv": "Ed25519",
|
||||
"use": "sig",
|
||||
"alg": "EdDSA",
|
||||
"x": encodeBase64URL(s.pubKey),
|
||||
"x": encodeBase64URL(pubKey),
|
||||
}
|
||||
writeJSON(w, http.StatusOK, jwk)
|
||||
}
|
||||
@@ -214,7 +431,7 @@ func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
// Security: return a generic error whether the user exists or not.
|
||||
// Always run a dummy Argon2 check to prevent timing-based user enumeration.
|
||||
_, _ = auth.VerifyPassword("dummy", auth.DummyHash())
|
||||
s.writeAudit(r, model.EventLoginFail, nil, nil, fmt.Sprintf(`{"username":%q,"reason":"unknown_user"}`, req.Username))
|
||||
s.writeAudit(r, model.EventLoginFail, nil, nil, audit.JSON("username", req.Username, "reason", "unknown_user"))
|
||||
middleware.WriteError(w, http.StatusUnauthorized, "invalid credentials", "unauthorized")
|
||||
return
|
||||
}
|
||||
@@ -238,7 +455,9 @@ func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
if locked {
|
||||
_, _ = auth.VerifyPassword("dummy", auth.DummyHash())
|
||||
s.writeAudit(r, model.EventLoginFail, &acct.ID, nil, `{"reason":"account_locked"}`)
|
||||
middleware.WriteError(w, http.StatusTooManyRequests, "account temporarily locked", "account_locked")
|
||||
// Security: return the same 401 "invalid credentials" as wrong-password
|
||||
// to prevent user-enumeration via lockout differentiation (SEC-02).
|
||||
middleware.WriteError(w, http.StatusUnauthorized, "invalid credentials", "unauthorized")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -255,13 +474,23 @@ func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
// TOTP check (if enrolled).
|
||||
if acct.TOTPRequired {
|
||||
if req.TOTPCode == "" {
|
||||
// Security (DEF-08 / PEN-06): do NOT increment the lockout counter
|
||||
// for a missing TOTP code. A missing code means the client needs to
|
||||
// re-prompt the user — it is not a credential failure. Incrementing
|
||||
// here would let an attacker trigger account lockout by omitting the
|
||||
// code after a correct password guess, and would penalise well-behaved
|
||||
// clients that call Login in two steps (password first, TOTP second).
|
||||
s.writeAudit(r, model.EventLoginFail, &acct.ID, nil, `{"reason":"totp_missing"}`)
|
||||
_ = s.db.RecordLoginFailure(acct.ID)
|
||||
middleware.WriteError(w, http.StatusUnauthorized, "TOTP code required", "totp_required")
|
||||
return
|
||||
}
|
||||
// Decrypt the TOTP secret.
|
||||
secret, err := crypto.OpenAESGCM(s.masterKey, acct.TOTPSecretNonce, acct.TOTPSecretEnc)
|
||||
masterKey, err := s.vault.MasterKey()
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusServiceUnavailable, "vault sealed", "vault_sealed")
|
||||
return
|
||||
}
|
||||
secret, err := crypto.OpenAESGCM(masterKey, acct.TOTPSecretNonce, acct.TOTPSecretEnc)
|
||||
if err != nil {
|
||||
s.logger.Error("decrypt TOTP secret", "error", err, "account_id", acct.ID)
|
||||
middleware.WriteError(w, http.StatusInternalServerError, "internal error", "internal_error")
|
||||
@@ -301,7 +530,12 @@ func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
tokenStr, claims, err := token.IssueToken(s.privKey, s.cfg.Tokens.Issuer, acct.UUID, roles, expiry)
|
||||
privKey, err := s.vault.PrivKey()
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusServiceUnavailable, "vault sealed", "vault_sealed")
|
||||
return
|
||||
}
|
||||
tokenStr, claims, err := token.IssueToken(privKey, s.cfg.Tokens.Issuer, acct.UUID, roles, expiry)
|
||||
if err != nil {
|
||||
s.logger.Error("issue token", "error", err)
|
||||
middleware.WriteError(w, http.StatusInternalServerError, "internal error", "internal_error")
|
||||
@@ -315,7 +549,7 @@ func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
s.writeAudit(r, model.EventLoginOK, &acct.ID, nil, "")
|
||||
s.writeAudit(r, model.EventTokenIssued, &acct.ID, nil, fmt.Sprintf(`{"jti":%q}`, claims.JTI))
|
||||
s.writeAudit(r, model.EventTokenIssued, &acct.ID, nil, audit.JSON("jti", claims.JTI))
|
||||
|
||||
writeJSON(w, http.StatusOK, loginResponse{
|
||||
Token: tokenStr,
|
||||
@@ -330,13 +564,22 @@ func (s *Server) handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
middleware.WriteError(w, http.StatusInternalServerError, "internal error", "internal_error")
|
||||
return
|
||||
}
|
||||
s.writeAudit(r, model.EventTokenRevoked, nil, nil, fmt.Sprintf(`{"jti":%q,"reason":"logout"}`, claims.JTI))
|
||||
s.writeAudit(r, model.EventTokenRevoked, nil, nil, audit.JSON("jti", claims.JTI, "reason", "logout"))
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (s *Server) handleRenew(w http.ResponseWriter, r *http.Request) {
|
||||
claims := middleware.ClaimsFromContext(r.Context())
|
||||
|
||||
// Security: only allow renewal when the token has consumed at least 50% of
|
||||
// its lifetime. This prevents indefinite renewal of stolen tokens (SEC-03).
|
||||
totalLifetime := claims.ExpiresAt.Sub(claims.IssuedAt)
|
||||
elapsed := time.Since(claims.IssuedAt)
|
||||
if elapsed < totalLifetime/2 {
|
||||
middleware.WriteError(w, http.StatusBadRequest, "token is not yet eligible for renewal", "renewal_too_early")
|
||||
return
|
||||
}
|
||||
|
||||
// Load account to get current roles (they may have changed since token issuance).
|
||||
acct, err := s.db.GetAccountByUUID(claims.Subject)
|
||||
if err != nil {
|
||||
@@ -362,7 +605,12 @@ func (s *Server) handleRenew(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
newTokenStr, newClaims, err := token.IssueToken(s.privKey, s.cfg.Tokens.Issuer, acct.UUID, roles, expiry)
|
||||
privKey, err := s.vault.PrivKey()
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusServiceUnavailable, "vault sealed", "vault_sealed")
|
||||
return
|
||||
}
|
||||
newTokenStr, newClaims, err := token.IssueToken(privKey, s.cfg.Tokens.Issuer, acct.UUID, roles, expiry)
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusInternalServerError, "internal error", "internal_error")
|
||||
return
|
||||
@@ -376,7 +624,7 @@ func (s *Server) handleRenew(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
s.writeAudit(r, model.EventTokenRenewed, &acct.ID, nil, fmt.Sprintf(`{"old_jti":%q,"new_jti":%q}`, claims.JTI, newClaims.JTI))
|
||||
s.writeAudit(r, model.EventTokenRenewed, &acct.ID, nil, audit.JSON("old_jti", claims.JTI, "new_jti", newClaims.JTI))
|
||||
|
||||
writeJSON(w, http.StatusOK, loginResponse{
|
||||
Token: newTokenStr,
|
||||
@@ -392,6 +640,7 @@ type validateRequest struct {
|
||||
|
||||
type validateResponse struct {
|
||||
Subject string `json:"sub,omitempty"`
|
||||
Username string `json:"username,omitempty"`
|
||||
ExpiresAt string `json:"expires_at,omitempty"`
|
||||
Roles []string `json:"roles,omitempty"`
|
||||
Valid bool `json:"valid"`
|
||||
@@ -414,7 +663,12 @@ func (s *Server) handleTokenValidate(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
claims, err := token.ValidateToken(s.pubKey, tokenStr, s.cfg.Tokens.Issuer)
|
||||
pubKey, err := s.vault.PubKey()
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusServiceUnavailable, "vault sealed", "vault_sealed")
|
||||
return
|
||||
}
|
||||
claims, err := token.ValidateToken(pubKey, tokenStr, s.cfg.Tokens.Issuer)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusOK, validateResponse{Valid: false})
|
||||
return
|
||||
@@ -426,12 +680,16 @@ func (s *Server) handleTokenValidate(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, validateResponse{
|
||||
resp := validateResponse{
|
||||
Valid: true,
|
||||
Subject: claims.Subject,
|
||||
Roles: claims.Roles,
|
||||
ExpiresAt: claims.ExpiresAt.Format("2006-01-02T15:04:05Z"),
|
||||
})
|
||||
}
|
||||
if acct, err := s.db.GetAccountByUUID(claims.Subject); err == nil {
|
||||
resp.Username = acct.Username
|
||||
}
|
||||
writeJSON(w, http.StatusOK, resp)
|
||||
}
|
||||
|
||||
type issueTokenRequest struct {
|
||||
@@ -454,23 +712,26 @@ func (s *Server) handleTokenIssue(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
tokenStr, claims, err := token.IssueToken(s.privKey, s.cfg.Tokens.Issuer, acct.UUID, nil, s.cfg.ServiceExpiry())
|
||||
privKey, err := s.vault.PrivKey()
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusServiceUnavailable, "vault sealed", "vault_sealed")
|
||||
return
|
||||
}
|
||||
tokenStr, claims, err := token.IssueToken(privKey, s.cfg.Tokens.Issuer, acct.UUID, nil, s.cfg.ServiceExpiry())
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusInternalServerError, "internal error", "internal_error")
|
||||
return
|
||||
}
|
||||
|
||||
// Revoke existing system token if any.
|
||||
// Atomically revoke existing system token (if any), track the new token,
|
||||
// and update system_tokens — all in a single transaction.
|
||||
// Security: prevents inconsistent state if a crash occurs mid-operation.
|
||||
var oldJTI string
|
||||
existing, err := s.db.GetSystemToken(acct.ID)
|
||||
if err == nil && existing != nil {
|
||||
_ = s.db.RevokeToken(existing.JTI, "rotated")
|
||||
oldJTI = existing.JTI
|
||||
}
|
||||
|
||||
if err := s.db.TrackToken(claims.JTI, acct.ID, claims.IssuedAt, claims.ExpiresAt); err != nil {
|
||||
middleware.WriteError(w, http.StatusInternalServerError, "internal error", "internal_error")
|
||||
return
|
||||
}
|
||||
if err := s.db.SetSystemToken(acct.ID, claims.JTI, claims.ExpiresAt); err != nil {
|
||||
if err := s.db.IssueSystemToken(oldJTI, claims.JTI, acct.ID, claims.IssuedAt, claims.ExpiresAt); err != nil {
|
||||
middleware.WriteError(w, http.StatusInternalServerError, "internal error", "internal_error")
|
||||
return
|
||||
}
|
||||
@@ -482,7 +743,7 @@ func (s *Server) handleTokenIssue(w http.ResponseWriter, r *http.Request) {
|
||||
actorID = &a.ID
|
||||
}
|
||||
}
|
||||
s.writeAudit(r, model.EventTokenIssued, actorID, &acct.ID, fmt.Sprintf(`{"jti":%q}`, claims.JTI))
|
||||
s.writeAudit(r, model.EventTokenIssued, actorID, &acct.ID, audit.JSON("jti", claims.JTI))
|
||||
|
||||
writeJSON(w, http.StatusOK, loginResponse{
|
||||
Token: tokenStr,
|
||||
@@ -502,7 +763,7 @@ func (s *Server) handleTokenRevoke(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
s.writeAudit(r, model.EventTokenRevoked, nil, nil, fmt.Sprintf(`{"jti":%q}`, jti))
|
||||
s.writeAudit(r, model.EventTokenRevoked, nil, nil, audit.JSON("jti", jti))
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
@@ -597,7 +858,7 @@ func (s *Server) handleCreateAccount(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
s.writeAudit(r, model.EventAccountCreated, nil, &acct.ID, fmt.Sprintf(`{"username":%q}`, acct.Username))
|
||||
s.writeAudit(r, model.EventAccountCreated, nil, &acct.ID, audit.JSON("username", acct.Username))
|
||||
writeJSON(w, http.StatusCreated, accountToResponse(acct))
|
||||
}
|
||||
|
||||
@@ -712,7 +973,7 @@ func (s *Server) handleSetRoles(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
s.writeAudit(r, model.EventRoleGranted, grantedBy, &acct.ID, fmt.Sprintf(`{"roles":%v}`, req.Roles))
|
||||
s.writeAudit(r, model.EventRoleGranted, grantedBy, &acct.ID, audit.JSONWithRoles(req.Roles))
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
@@ -745,7 +1006,7 @@ func (s *Server) handleGrantRole(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
s.writeAudit(r, model.EventRoleGranted, grantedBy, &acct.ID, fmt.Sprintf(`{"role":"%s"}`, req.Role))
|
||||
s.writeAudit(r, model.EventRoleGranted, grantedBy, &acct.ID, audit.JSON("role", req.Role))
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
@@ -774,12 +1035,16 @@ func (s *Server) handleRevokeRole(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
s.writeAudit(r, model.EventRoleRevoked, revokedBy, &acct.ID, fmt.Sprintf(`{"role":"%s"}`, role))
|
||||
s.writeAudit(r, model.EventRoleRevoked, revokedBy, &acct.ID, audit.JSON("role", role))
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// ---- TOTP endpoints ----
|
||||
|
||||
type totpEnrollRequest struct {
|
||||
Password string `json:"password"` // security: current password required to prevent session-theft escalation
|
||||
}
|
||||
|
||||
type totpEnrollResponse struct {
|
||||
Secret string `json:"secret"` // base32-encoded
|
||||
OTPAuthURI string `json:"otpauth_uri"`
|
||||
@@ -789,6 +1054,12 @@ type totpConfirmRequest struct {
|
||||
Code string `json:"code"`
|
||||
}
|
||||
|
||||
// handleTOTPEnroll begins TOTP enrollment for the calling account.
|
||||
//
|
||||
// Security (SEC-01): the current password is required in the request body to
|
||||
// prevent a stolen session token from being used to enroll attacker-controlled
|
||||
// MFA on the victim's account. Lockout is checked and failures are recorded
|
||||
// to prevent brute-force use of this endpoint as a password oracle.
|
||||
func (s *Server) handleTOTPEnroll(w http.ResponseWriter, r *http.Request) {
|
||||
claims := middleware.ClaimsFromContext(r.Context())
|
||||
acct, err := s.db.GetAccountByUUID(claims.Subject)
|
||||
@@ -797,6 +1068,38 @@ func (s *Server) handleTOTPEnroll(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var req totpEnrollRequest
|
||||
if !decodeJSON(w, r, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
if req.Password == "" {
|
||||
middleware.WriteError(w, http.StatusBadRequest, "password is required", "bad_request")
|
||||
return
|
||||
}
|
||||
|
||||
// Security: check lockout before verifying (same as login and password-change flows)
|
||||
// so an attacker cannot use this endpoint to brute-force the current password.
|
||||
locked, lockErr := s.db.IsLockedOut(acct.ID)
|
||||
if lockErr != nil {
|
||||
s.logger.Error("lockout check (TOTP enroll)", "error", lockErr)
|
||||
}
|
||||
if locked {
|
||||
s.writeAudit(r, model.EventTOTPEnrolled, &acct.ID, &acct.ID, `{"result":"locked"}`)
|
||||
middleware.WriteError(w, http.StatusTooManyRequests, "account temporarily locked", "account_locked")
|
||||
return
|
||||
}
|
||||
|
||||
// Security: verify the current password with the same constant-time
|
||||
// Argon2id path used at login to prevent timing oracles.
|
||||
ok, verifyErr := auth.VerifyPassword(req.Password, acct.PasswordHash)
|
||||
if verifyErr != nil || !ok {
|
||||
_ = s.db.RecordLoginFailure(acct.ID)
|
||||
s.writeAudit(r, model.EventTOTPEnrolled, &acct.ID, &acct.ID, `{"result":"wrong_password"}`)
|
||||
middleware.WriteError(w, http.StatusUnauthorized, "password is incorrect", "unauthorized")
|
||||
return
|
||||
}
|
||||
|
||||
rawSecret, b32Secret, err := auth.GenerateTOTPSecret()
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusInternalServerError, "internal error", "internal_error")
|
||||
@@ -805,7 +1108,12 @@ func (s *Server) handleTOTPEnroll(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Encrypt the secret before storing it temporarily.
|
||||
// Note: we store as pending; enrollment is confirmed with /confirm.
|
||||
secretEnc, secretNonce, err := crypto.SealAESGCM(s.masterKey, rawSecret)
|
||||
masterKey, err := s.vault.MasterKey()
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusServiceUnavailable, "vault sealed", "vault_sealed")
|
||||
return
|
||||
}
|
||||
secretEnc, secretNonce, err := crypto.SealAESGCM(masterKey, rawSecret)
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusInternalServerError, "internal error", "internal_error")
|
||||
return
|
||||
@@ -848,7 +1156,12 @@ func (s *Server) handleTOTPConfirm(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
secret, err := crypto.OpenAESGCM(s.masterKey, acct.TOTPSecretNonce, acct.TOTPSecretEnc)
|
||||
masterKey, err := s.vault.MasterKey()
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusServiceUnavailable, "vault sealed", "vault_sealed")
|
||||
return
|
||||
}
|
||||
secret, err := crypto.OpenAESGCM(masterKey, acct.TOTPSecretNonce, acct.TOTPSecretEnc)
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusInternalServerError, "internal error", "internal_error")
|
||||
return
|
||||
@@ -1026,7 +1339,9 @@ func (s *Server) handleChangePassword(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
if locked {
|
||||
s.writeAudit(r, model.EventPasswordChanged, &acct.ID, &acct.ID, `{"result":"locked"}`)
|
||||
middleware.WriteError(w, http.StatusTooManyRequests, "account temporarily locked", "account_locked")
|
||||
// Security: return the same 401 as wrong-password to prevent
|
||||
// user-enumeration via lockout differentiation (SEC-02).
|
||||
middleware.WriteError(w, http.StatusUnauthorized, "invalid credentials", "unauthorized")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1106,7 +1421,12 @@ func (s *Server) handleGetPGCreds(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Decrypt the password to return it to the admin caller.
|
||||
password, err := crypto.OpenAESGCM(s.masterKey, cred.PGPasswordNonce, cred.PGPasswordEnc)
|
||||
masterKey, err := s.vault.MasterKey()
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusServiceUnavailable, "vault sealed", "vault_sealed")
|
||||
return
|
||||
}
|
||||
password, err := crypto.OpenAESGCM(masterKey, cred.PGPasswordNonce, cred.PGPasswordEnc)
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusInternalServerError, "internal error", "internal_error")
|
||||
return
|
||||
@@ -1143,7 +1463,12 @@ func (s *Server) handleSetPGCreds(w http.ResponseWriter, r *http.Request) {
|
||||
req.Port = 5432
|
||||
}
|
||||
|
||||
enc, nonce, err := crypto.SealAESGCM(s.masterKey, []byte(req.Password))
|
||||
masterKey, err := s.vault.MasterKey()
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusServiceUnavailable, "vault sealed", "vault_sealed")
|
||||
return
|
||||
}
|
||||
enc, nonce, err := crypto.SealAESGCM(masterKey, []byte(req.Password))
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusInternalServerError, "internal error", "internal_error")
|
||||
return
|
||||
@@ -1158,6 +1483,58 @@ func (s *Server) handleSetPGCreds(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// handleListAccessiblePGCreds returns all pg_credentials accessible to the
|
||||
// authenticated user: those owned + those explicitly granted. The credential ID
|
||||
// is included so callers can fetch a specific credential via /v1/accounts/{id}/pgcreds.
|
||||
func (s *Server) handleListAccessiblePGCreds(w http.ResponseWriter, r *http.Request) {
|
||||
claims := middleware.ClaimsFromContext(r.Context())
|
||||
if claims == nil {
|
||||
middleware.WriteError(w, http.StatusUnauthorized, "not authenticated", "unauthorized")
|
||||
return
|
||||
}
|
||||
|
||||
acct, err := s.db.GetAccountByUUID(claims.Subject)
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusUnauthorized, "account not found", "unauthorized")
|
||||
return
|
||||
}
|
||||
|
||||
creds, err := s.db.ListAccessiblePGCreds(acct.ID)
|
||||
if err != nil {
|
||||
middleware.WriteError(w, http.StatusInternalServerError, "internal error", "internal_error")
|
||||
return
|
||||
}
|
||||
|
||||
// Convert credentials to response format with credential ID.
|
||||
type pgCredResponse struct {
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Host string `json:"host"`
|
||||
Database string `json:"database"`
|
||||
Username string `json:"username"`
|
||||
ServiceAccountID string `json:"service_account_id"`
|
||||
ServiceAccountName string `json:"service_account_name,omitempty"`
|
||||
ID int64 `json:"id"`
|
||||
Port int `json:"port"`
|
||||
}
|
||||
|
||||
response := make([]pgCredResponse, len(creds))
|
||||
for i, cred := range creds {
|
||||
response[i] = pgCredResponse{
|
||||
ID: cred.ID,
|
||||
ServiceAccountID: cred.ServiceAccountUUID,
|
||||
Host: cred.PGHost,
|
||||
Port: cred.PGPort,
|
||||
Database: cred.PGDatabase,
|
||||
Username: cred.PGUsername,
|
||||
CreatedAt: cred.CreatedAt,
|
||||
UpdatedAt: cred.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, response)
|
||||
}
|
||||
|
||||
// ---- Audit endpoints ----
|
||||
|
||||
// handleListAudit returns paginated audit log entries with resolved usernames.
|
||||
@@ -1269,9 +1646,21 @@ func writeJSON(w http.ResponseWriter, status int, v interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// maxJSONBytes limits the size of JSON request bodies (1 MiB).
|
||||
//
|
||||
// Security (SEC-05): without a size limit an attacker could send a
|
||||
// multi-gigabyte body and exhaust server memory. The UI layer already
|
||||
// applies http.MaxBytesReader; this constant gives the REST API the
|
||||
// same protection.
|
||||
const maxJSONBytes = 1 << 20
|
||||
|
||||
// decodeJSON decodes a JSON request body into v.
|
||||
// Returns false and writes a 400 response if decoding fails.
|
||||
//
|
||||
// Security (SEC-05): the body is wrapped with http.MaxBytesReader so
|
||||
// that oversized payloads are rejected before they are fully read.
|
||||
func decodeJSON(w http.ResponseWriter, r *http.Request, v interface{}) bool {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxJSONBytes)
|
||||
dec := json.NewDecoder(r.Body)
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(v); err != nil {
|
||||
@@ -1282,21 +1671,42 @@ func decodeJSON(w http.ResponseWriter, r *http.Request, v interface{}) bool {
|
||||
}
|
||||
|
||||
// extractBearerFromRequest extracts a Bearer token from the Authorization header.
|
||||
// Security (PEN-01): validates the "Bearer" prefix using case-insensitive
|
||||
// comparison before extracting the token. The previous implementation sliced
|
||||
// at a fixed offset without checking the prefix, accepting any 8+ character
|
||||
// Authorization value.
|
||||
func extractBearerFromRequest(r *http.Request) (string, error) {
|
||||
auth := r.Header.Get("Authorization")
|
||||
if auth == "" {
|
||||
return "", fmt.Errorf("no Authorization header")
|
||||
}
|
||||
const prefix = "Bearer "
|
||||
if len(auth) <= len(prefix) {
|
||||
parts := strings.SplitN(auth, " ", 2)
|
||||
if len(parts) != 2 || !strings.EqualFold(parts[0], "Bearer") {
|
||||
return "", fmt.Errorf("malformed Authorization header")
|
||||
}
|
||||
return auth[len(prefix):], nil
|
||||
if parts[1] == "" {
|
||||
return "", fmt.Errorf("empty Bearer token")
|
||||
}
|
||||
return parts[1], nil
|
||||
}
|
||||
|
||||
// docsSecurityHeaders adds the same defensive HTTP headers as the UI sub-mux
|
||||
// to the /docs and /docs/openapi.yaml endpoints.
|
||||
//
|
||||
// globalSecurityHeaders sets baseline security headers on every response.
|
||||
// Security (SEC-04): API responses previously lacked X-Content-Type-Options,
|
||||
// HSTS, and Cache-Control. These three headers are safe for all content types
|
||||
// and do not interfere with JSON API clients or the HTMX UI.
|
||||
func globalSecurityHeaders(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
h := w.Header()
|
||||
h.Set("X-Content-Type-Options", "nosniff")
|
||||
h.Set("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
|
||||
h.Set("Cache-Control", "no-store")
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// Security (DEF-09): without these headers the Swagger UI HTML page is
|
||||
// served without CSP, X-Frame-Options, or HSTS, leaving it susceptible
|
||||
// to clickjacking and MIME-type confusion in browsers.
|
||||
|
||||
@@ -3,10 +3,15 @@ package server
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/ed25519"
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/sha1" //nolint:gosec // G505: SHA1 required by RFC 6238 TOTP (HMAC-SHA1)
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"math"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
@@ -18,9 +23,31 @@ import (
|
||||
"git.wntrmute.dev/kyle/mcias/internal/config"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/db"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/model"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/policy"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/token"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/vault"
|
||||
)
|
||||
|
||||
// generateTOTPCode computes a valid RFC 6238 TOTP code for the current time
|
||||
// using the given raw secret bytes. Used in tests to confirm TOTP enrollment.
|
||||
func generateTOTPCode(t *testing.T, secret []byte) string {
|
||||
t.Helper()
|
||||
counter := uint64(time.Now().Unix() / 30) //nolint:gosec // G115: always non-negative
|
||||
counterBytes := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(counterBytes, counter)
|
||||
mac := hmac.New(sha1.New, secret)
|
||||
if _, err := mac.Write(counterBytes); err != nil {
|
||||
t.Fatalf("generateTOTPCode: HMAC write: %v", err)
|
||||
}
|
||||
h := mac.Sum(nil)
|
||||
offset := h[len(h)-1] & 0x0F
|
||||
binCode := (int(h[offset]&0x7F)<<24 |
|
||||
int(h[offset+1])<<16 |
|
||||
int(h[offset+2])<<8 |
|
||||
int(h[offset+3])) % int(math.Pow10(6))
|
||||
return fmt.Sprintf("%06d", binCode)
|
||||
}
|
||||
|
||||
const testIssuer = "https://auth.example.com"
|
||||
|
||||
func newTestServer(t *testing.T) (*Server, ed25519.PublicKey, ed25519.PrivateKey, *db.DB) {
|
||||
@@ -47,8 +74,9 @@ func newTestServer(t *testing.T) (*Server, ed25519.PublicKey, ed25519.PrivateKey
|
||||
|
||||
cfg := config.NewTestConfig(testIssuer)
|
||||
|
||||
v := vault.NewUnsealed(masterKey, priv, pub)
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
srv := New(database, cfg, priv, pub, masterKey, logger)
|
||||
srv := New(database, cfg, v, logger)
|
||||
return srv, pub, priv, database
|
||||
}
|
||||
|
||||
@@ -519,8 +547,10 @@ func TestTOTPEnrollDoesNotRequireTOTP(t *testing.T) {
|
||||
t.Fatalf("TrackToken: %v", err)
|
||||
}
|
||||
|
||||
// Start enrollment.
|
||||
rr := doRequest(t, handler, "POST", "/v1/auth/totp/enroll", nil, tokenStr)
|
||||
// Start enrollment (password required since SEC-01 fix).
|
||||
rr := doRequest(t, handler, "POST", "/v1/auth/totp/enroll", totpEnrollRequest{
|
||||
Password: "testpass123",
|
||||
}, tokenStr)
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("enroll status = %d, want 200; body: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
@@ -558,12 +588,69 @@ func TestTOTPEnrollDoesNotRequireTOTP(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestTOTPEnrollRequiresPassword verifies that TOTP enrollment (SEC-01)
|
||||
// requires the current password. A stolen session token alone must not be
|
||||
// sufficient to add attacker-controlled MFA to the victim's account.
|
||||
func TestTOTPEnrollRequiresPassword(t *testing.T) {
|
||||
srv, _, priv, _ := newTestServer(t)
|
||||
acct := createTestHumanAccount(t, srv, "totp-pw-check")
|
||||
handler := srv.Handler()
|
||||
|
||||
tokenStr, claims, err := token.IssueToken(priv, testIssuer, acct.UUID, nil, time.Hour)
|
||||
if err != nil {
|
||||
t.Fatalf("IssueToken: %v", err)
|
||||
}
|
||||
if err := srv.db.TrackToken(claims.JTI, acct.ID, claims.IssuedAt, claims.ExpiresAt); err != nil {
|
||||
t.Fatalf("TrackToken: %v", err)
|
||||
}
|
||||
|
||||
t.Run("no password", func(t *testing.T) {
|
||||
rr := doRequest(t, handler, "POST", "/v1/auth/totp/enroll", totpEnrollRequest{}, tokenStr)
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Errorf("enroll without password: status = %d, want %d; body: %s",
|
||||
rr.Code, http.StatusBadRequest, rr.Body.String())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("wrong password", func(t *testing.T) {
|
||||
rr := doRequest(t, handler, "POST", "/v1/auth/totp/enroll", totpEnrollRequest{
|
||||
Password: "wrong-password",
|
||||
}, tokenStr)
|
||||
if rr.Code != http.StatusUnauthorized {
|
||||
t.Errorf("enroll with wrong password: status = %d, want %d; body: %s",
|
||||
rr.Code, http.StatusUnauthorized, rr.Body.String())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("correct password", func(t *testing.T) {
|
||||
rr := doRequest(t, handler, "POST", "/v1/auth/totp/enroll", totpEnrollRequest{
|
||||
Password: "testpass123",
|
||||
}, tokenStr)
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("enroll with correct password: status = %d, want 200; body: %s",
|
||||
rr.Code, rr.Body.String())
|
||||
}
|
||||
var resp totpEnrollResponse
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if resp.Secret == "" {
|
||||
t.Error("expected non-empty TOTP secret")
|
||||
}
|
||||
if resp.OTPAuthURI == "" {
|
||||
t.Error("expected non-empty otpauth URI")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestRenewToken(t *testing.T) {
|
||||
srv, _, priv, _ := newTestServer(t)
|
||||
acct := createTestHumanAccount(t, srv, "renew-user")
|
||||
handler := srv.Handler()
|
||||
|
||||
oldTokenStr, claims, err := token.IssueToken(priv, testIssuer, acct.UUID, nil, time.Hour)
|
||||
// Issue a short-lived token (4s) so we can wait past the 50% threshold
|
||||
// while leaving enough headroom before expiry to avoid flakiness.
|
||||
oldTokenStr, claims, err := token.IssueToken(priv, testIssuer, acct.UUID, nil, 4*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("IssueToken: %v", err)
|
||||
}
|
||||
@@ -572,6 +659,9 @@ func TestRenewToken(t *testing.T) {
|
||||
t.Fatalf("TrackToken: %v", err)
|
||||
}
|
||||
|
||||
// Wait for >50% of the 4s lifetime to elapse.
|
||||
time.Sleep(2100 * time.Millisecond)
|
||||
|
||||
rr := doRequest(t, handler, "POST", "/v1/auth/renew", nil, oldTokenStr)
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("renew status = %d, want 200; body: %s", rr.Code, rr.Body.String())
|
||||
@@ -594,3 +684,438 @@ func TestRenewToken(t *testing.T) {
|
||||
t.Error("old token should be revoked after renewal")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOversizedJSONBodyRejected(t *testing.T) {
|
||||
srv, _, _, _ := newTestServer(t)
|
||||
handler := srv.Handler()
|
||||
|
||||
// Build a JSON body larger than 1 MiB.
|
||||
oversized := bytes.Repeat([]byte("A"), (1<<20)+1)
|
||||
body := []byte(`{"username":"admin","password":"` + string(oversized) + `"}`)
|
||||
|
||||
req := httptest.NewRequest("POST", "/v1/auth/login", bytes.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rr := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400 for oversized body, got %d", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSecurityHeadersOnAPIResponses verifies that the global security-headers
|
||||
// middleware (SEC-04) sets X-Content-Type-Options, Strict-Transport-Security,
|
||||
// and Cache-Control on all API responses, not just the UI.
|
||||
func TestSecurityHeadersOnAPIResponses(t *testing.T) {
|
||||
srv, _, _, _ := newTestServer(t)
|
||||
handler := srv.Handler()
|
||||
|
||||
wantHeaders := map[string]string{
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
"Strict-Transport-Security": "max-age=63072000; includeSubDomains",
|
||||
"Cache-Control": "no-store",
|
||||
}
|
||||
|
||||
t.Run("GET /v1/health", func(t *testing.T) {
|
||||
rr := doRequest(t, handler, "GET", "/v1/health", nil, "")
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200", rr.Code)
|
||||
}
|
||||
for header, want := range wantHeaders {
|
||||
got := rr.Header().Get(header)
|
||||
if got != want {
|
||||
t.Errorf("%s = %q, want %q", header, got, want)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("POST /v1/auth/login", func(t *testing.T) {
|
||||
createTestHumanAccount(t, srv, "sec04-user")
|
||||
rr := doRequest(t, handler, "POST", "/v1/auth/login", map[string]string{
|
||||
"username": "sec04-user",
|
||||
"password": "testpass123",
|
||||
}, "")
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200; body: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
for header, want := range wantHeaders {
|
||||
got := rr.Header().Get(header)
|
||||
if got != want {
|
||||
t.Errorf("%s = %q, want %q", header, got, want)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestLoginLockedAccountReturns401 verifies that a locked-out account gets the
|
||||
// same HTTP 401 / "invalid credentials" response as a wrong-password attempt,
|
||||
// preventing user-enumeration via lockout differentiation (SEC-02).
|
||||
func TestLoginLockedAccountReturns401(t *testing.T) {
|
||||
srv, _, _, database := newTestServer(t)
|
||||
acct := createTestHumanAccount(t, srv, "lockuser")
|
||||
handler := srv.Handler()
|
||||
|
||||
// Lower the lockout threshold so we don't need 10 failures.
|
||||
origThreshold := db.LockoutThreshold
|
||||
db.LockoutThreshold = 3
|
||||
t.Cleanup(func() { db.LockoutThreshold = origThreshold })
|
||||
|
||||
// Record enough failures to trigger lockout.
|
||||
for range db.LockoutThreshold {
|
||||
if err := database.RecordLoginFailure(acct.ID); err != nil {
|
||||
t.Fatalf("RecordLoginFailure: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Confirm the account is locked.
|
||||
locked, err := database.IsLockedOut(acct.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("IsLockedOut: %v", err)
|
||||
}
|
||||
if !locked {
|
||||
t.Fatal("expected account to be locked out after threshold failures")
|
||||
}
|
||||
|
||||
// Attempt login on the locked account.
|
||||
lockedRR := doRequest(t, handler, "POST", "/v1/auth/login", map[string]string{
|
||||
"username": "lockuser",
|
||||
"password": "testpass123",
|
||||
}, "")
|
||||
|
||||
// Also attempt login with a wrong password (not locked) for comparison.
|
||||
wrongRR := doRequest(t, handler, "POST", "/v1/auth/login", map[string]string{
|
||||
"username": "lockuser",
|
||||
"password": "wrongpassword",
|
||||
}, "")
|
||||
|
||||
// Both must return 401, not 429.
|
||||
if lockedRR.Code != http.StatusUnauthorized {
|
||||
t.Errorf("locked account: status = %d, want %d", lockedRR.Code, http.StatusUnauthorized)
|
||||
}
|
||||
if wrongRR.Code != http.StatusUnauthorized {
|
||||
t.Errorf("wrong password: status = %d, want %d", wrongRR.Code, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
// Parse the JSON bodies and compare — they must be identical.
|
||||
type errResp struct {
|
||||
Error string `json:"error"`
|
||||
Code string `json:"code"`
|
||||
}
|
||||
var lockedBody, wrongBody errResp
|
||||
if err := json.Unmarshal(lockedRR.Body.Bytes(), &lockedBody); err != nil {
|
||||
t.Fatalf("unmarshal locked body: %v", err)
|
||||
}
|
||||
if err := json.Unmarshal(wrongRR.Body.Bytes(), &wrongBody); err != nil {
|
||||
t.Fatalf("unmarshal wrong body: %v", err)
|
||||
}
|
||||
|
||||
if lockedBody != wrongBody {
|
||||
t.Errorf("locked response %+v differs from wrong-password response %+v", lockedBody, wrongBody)
|
||||
}
|
||||
if lockedBody.Code != "unauthorized" {
|
||||
t.Errorf("locked response code = %q, want %q", lockedBody.Code, "unauthorized")
|
||||
}
|
||||
if lockedBody.Error != "invalid credentials" {
|
||||
t.Errorf("locked response error = %q, want %q", lockedBody.Error, "invalid credentials")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRenewTokenTooEarly verifies that a token cannot be renewed before 50%
|
||||
// of its lifetime has elapsed (SEC-03).
|
||||
// TestExtractBearerFromRequest verifies that extractBearerFromRequest correctly
|
||||
// validates the "Bearer" prefix before extracting the token string.
|
||||
// Security (PEN-01): the previous implementation sliced at a fixed offset
|
||||
// without checking the prefix, accepting any 8+ character Authorization value.
|
||||
func TestExtractBearerFromRequest(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
header string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{"valid", "Bearer mytoken123", "mytoken123", false},
|
||||
{"missing header", "", "", true},
|
||||
{"no bearer prefix", "Token mytoken123", "", true},
|
||||
{"basic auth scheme", "Basic dXNlcjpwYXNz", "", true},
|
||||
{"empty token", "Bearer ", "", true},
|
||||
{"bearer only no space", "Bearer", "", true},
|
||||
{"case insensitive", "bearer mytoken123", "mytoken123", false},
|
||||
{"mixed case", "BEARER mytoken123", "mytoken123", false},
|
||||
{"garbage 8 chars", "XXXXXXXX", "", true},
|
||||
{"token with spaces", "Bearer token with spaces", "token with spaces", false},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
if tc.header != "" {
|
||||
req.Header.Set("Authorization", tc.header)
|
||||
}
|
||||
got, err := extractBearerFromRequest(req)
|
||||
if (err != nil) != tc.wantErr {
|
||||
t.Errorf("wantErr=%v, got err=%v", tc.wantErr, err)
|
||||
}
|
||||
if !tc.wantErr && got != tc.want {
|
||||
t.Errorf("token = %q, want %q", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenewTokenTooEarly(t *testing.T) {
|
||||
srv, _, priv, _ := newTestServer(t)
|
||||
acct := createTestHumanAccount(t, srv, "renew-early-user")
|
||||
handler := srv.Handler()
|
||||
|
||||
// Issue a long-lived token so 50% is far in the future.
|
||||
tokStr, claims, err := token.IssueToken(priv, testIssuer, acct.UUID, nil, time.Hour)
|
||||
if err != nil {
|
||||
t.Fatalf("IssueToken: %v", err)
|
||||
}
|
||||
if err := srv.db.TrackToken(claims.JTI, acct.ID, claims.IssuedAt, claims.ExpiresAt); err != nil {
|
||||
t.Fatalf("TrackToken: %v", err)
|
||||
}
|
||||
|
||||
// Immediately try to renew — should be rejected.
|
||||
rr := doRequest(t, handler, "POST", "/v1/auth/renew", nil, tokStr)
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Fatalf("renew status = %d, want 400; body: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
if !strings.Contains(rr.Body.String(), "not yet eligible for renewal") {
|
||||
t.Errorf("expected eligibility message, got: %s", rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestTOTPMissingDoesNotIncrementLockout verifies that a login attempt with
|
||||
// a correct password but missing TOTP code does NOT increment the account
|
||||
// lockout counter (PEN-06 / DEF-08).
|
||||
//
|
||||
// Security: incrementing the lockout counter for a missing TOTP code would
|
||||
// allow an attacker to lock out a TOTP-enrolled account by repeatedly sending
|
||||
// the correct password with no TOTP code — without needing to guess TOTP.
|
||||
// It would also penalise well-behaved two-step clients.
|
||||
func TestTOTPMissingDoesNotIncrementLockout(t *testing.T) {
|
||||
srv, _, priv, database := newTestServer(t)
|
||||
acct := createTestHumanAccount(t, srv, "totp-lockout-user")
|
||||
handler := srv.Handler()
|
||||
|
||||
// Issue a token so we can call the TOTP enroll and confirm endpoints.
|
||||
tokenStr, claims, err := token.IssueToken(priv, testIssuer, acct.UUID, nil, time.Hour)
|
||||
if err != nil {
|
||||
t.Fatalf("IssueToken: %v", err)
|
||||
}
|
||||
if err := srv.db.TrackToken(claims.JTI, acct.ID, claims.IssuedAt, claims.ExpiresAt); err != nil {
|
||||
t.Fatalf("TrackToken: %v", err)
|
||||
}
|
||||
|
||||
// Enroll TOTP — get back the base32 secret.
|
||||
enrollRR := doRequest(t, handler, "POST", "/v1/auth/totp/enroll", totpEnrollRequest{
|
||||
Password: "testpass123",
|
||||
}, tokenStr)
|
||||
if enrollRR.Code != http.StatusOK {
|
||||
t.Fatalf("enroll status = %d, want 200; body: %s", enrollRR.Code, enrollRR.Body.String())
|
||||
}
|
||||
var enrollResp totpEnrollResponse
|
||||
if err := json.Unmarshal(enrollRR.Body.Bytes(), &enrollResp); err != nil {
|
||||
t.Fatalf("unmarshal enroll: %v", err)
|
||||
}
|
||||
|
||||
// Decode the secret and generate a valid TOTP code to confirm enrollment.
|
||||
// We compute the TOTP code inline using the same RFC 6238 algorithm used
|
||||
// by auth.ValidateTOTP, since auth.hotp is not exported.
|
||||
secretBytes, err := auth.DecodeTOTPSecret(enrollResp.Secret)
|
||||
if err != nil {
|
||||
t.Fatalf("DecodeTOTPSecret: %v", err)
|
||||
}
|
||||
currentCode := generateTOTPCode(t, secretBytes)
|
||||
|
||||
// Confirm enrollment.
|
||||
confirmRR := doRequest(t, handler, "POST", "/v1/auth/totp/confirm", map[string]string{
|
||||
"code": currentCode,
|
||||
}, tokenStr)
|
||||
if confirmRR.Code != http.StatusNoContent {
|
||||
t.Fatalf("confirm status = %d, want 204; body: %s", confirmRR.Code, confirmRR.Body.String())
|
||||
}
|
||||
|
||||
// Account should now require TOTP. Lower the lockout threshold to 1 so
|
||||
// that a single RecordLoginFailure call would immediately lock the account.
|
||||
origThreshold := db.LockoutThreshold
|
||||
db.LockoutThreshold = 1
|
||||
t.Cleanup(func() { db.LockoutThreshold = origThreshold })
|
||||
|
||||
// Attempt login with the correct password but no TOTP code.
|
||||
rr := doRequest(t, handler, "POST", "/v1/auth/login", map[string]string{
|
||||
"username": "totp-lockout-user",
|
||||
"password": "testpass123",
|
||||
}, "")
|
||||
if rr.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("expected 401 for missing TOTP, got %d; body: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
// The error code must be totp_required, not unauthorized.
|
||||
var errResp struct {
|
||||
Code string `json:"code"`
|
||||
}
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &errResp); err != nil {
|
||||
t.Fatalf("unmarshal error response: %v", err)
|
||||
}
|
||||
if errResp.Code != "totp_required" {
|
||||
t.Errorf("error code = %q, want %q", errResp.Code, "totp_required")
|
||||
}
|
||||
|
||||
// Security (PEN-06): the lockout counter must NOT have been incremented.
|
||||
// With threshold=1, if it had been incremented the account would now be
|
||||
// locked and a subsequent login with correct credentials would fail.
|
||||
locked, err := database.IsLockedOut(acct.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("IsLockedOut: %v", err)
|
||||
}
|
||||
if locked {
|
||||
t.Error("account was locked after TOTP-missing login — lockout counter was incorrectly incremented (PEN-06)")
|
||||
}
|
||||
}
|
||||
|
||||
// issueSystemToken creates a system account, issues a JWT with the given roles,
|
||||
// tracks it in the database, and returns the token string and account.
|
||||
func issueSystemToken(t *testing.T, srv *Server, priv ed25519.PrivateKey, username string, roles []string) (string, *model.Account) {
|
||||
t.Helper()
|
||||
acct, err := srv.db.CreateAccount(username, model.AccountTypeSystem, "")
|
||||
if err != nil {
|
||||
t.Fatalf("create system account: %v", err)
|
||||
}
|
||||
tokenStr, claims, err := token.IssueToken(priv, testIssuer, acct.UUID, roles, time.Hour)
|
||||
if err != nil {
|
||||
t.Fatalf("issue token: %v", err)
|
||||
}
|
||||
if err := srv.db.TrackToken(claims.JTI, acct.ID, claims.IssuedAt, claims.ExpiresAt); err != nil {
|
||||
t.Fatalf("track token: %v", err)
|
||||
}
|
||||
return tokenStr, acct
|
||||
}
|
||||
|
||||
// TestPolicyEnforcement verifies that the policy engine gates access:
|
||||
// - Admin role is always allowed (built-in wildcard rule).
|
||||
// - Unauthenticated requests are rejected.
|
||||
// - Non-admin accounts are denied by default.
|
||||
// - A non-admin account gains access once an operator policy rule is created.
|
||||
// - Deleting the rule reverts to denial.
|
||||
func TestPolicyEnforcement(t *testing.T) {
|
||||
srv, _, priv, _ := newTestServer(t)
|
||||
handler := srv.Handler()
|
||||
|
||||
adminToken, _ := issueAdminToken(t, srv, priv, "admin-pol")
|
||||
|
||||
// 1. Admin can list accounts (built-in wildcard rule -1).
|
||||
rr := doRequest(t, handler, "GET", "/v1/accounts", nil, adminToken)
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Errorf("admin list accounts status = %d, want 200; body: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
|
||||
// 2. Unauthenticated request is rejected.
|
||||
rr = doRequest(t, handler, "GET", "/v1/accounts", nil, "")
|
||||
if rr.Code != http.StatusUnauthorized {
|
||||
t.Errorf("unauth list accounts status = %d, want 401", rr.Code)
|
||||
}
|
||||
|
||||
// 3. System account with no operator rules is denied by default.
|
||||
svcToken, svcAcct := issueSystemToken(t, srv, priv, "metacrypt", []string{"user"})
|
||||
rr = doRequest(t, handler, "GET", "/v1/accounts", nil, svcToken)
|
||||
if rr.Code != http.StatusForbidden {
|
||||
t.Errorf("system account (no policy) list accounts status = %d, want 403; body: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
|
||||
// 4. Create an operator policy rule granting the system account accounts:list.
|
||||
rule := createPolicyRuleRequest{
|
||||
Description: "allow metacrypt to list accounts",
|
||||
Priority: 50,
|
||||
Rule: policy.RuleBody{
|
||||
SubjectUUID: svcAcct.UUID,
|
||||
AccountTypes: []string{"system"},
|
||||
Actions: []policy.Action{policy.ActionListAccounts},
|
||||
Effect: policy.Allow,
|
||||
},
|
||||
}
|
||||
rr = doRequest(t, handler, "POST", "/v1/policy/rules", rule, adminToken)
|
||||
if rr.Code != http.StatusCreated {
|
||||
t.Fatalf("create policy rule status = %d, want 201; body: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
var created policyRuleResponse
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &created); err != nil {
|
||||
t.Fatalf("unmarshal created rule: %v", err)
|
||||
}
|
||||
|
||||
// 5. The same system account can now list accounts.
|
||||
rr = doRequest(t, handler, "GET", "/v1/accounts", nil, svcToken)
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Errorf("system account (with policy) list accounts status = %d, want 200; body: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
|
||||
// 6. The system account is still denied other actions (accounts:read).
|
||||
rr = doRequest(t, handler, "POST", "/v1/accounts", map[string]string{
|
||||
"username": "newuser", "password": "newpassword123", "account_type": "human",
|
||||
}, svcToken)
|
||||
if rr.Code != http.StatusForbidden {
|
||||
t.Errorf("system account (list-only policy) create account status = %d, want 403", rr.Code)
|
||||
}
|
||||
|
||||
// 7. Delete the rule and verify the account is denied again.
|
||||
rr = doRequest(t, handler, "DELETE", fmt.Sprintf("/v1/policy/rules/%d", created.ID), nil, adminToken)
|
||||
if rr.Code != http.StatusNoContent {
|
||||
t.Fatalf("delete policy rule status = %d, want 204; body: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
rr = doRequest(t, handler, "GET", "/v1/accounts", nil, svcToken)
|
||||
if rr.Code != http.StatusForbidden {
|
||||
t.Errorf("system account (rule deleted) list accounts status = %d, want 403", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPolicyDenyRule verifies that an explicit Deny rule blocks access even
|
||||
// when an Allow rule would otherwise permit it.
|
||||
func TestPolicyDenyRule(t *testing.T) {
|
||||
srv, _, priv, _ := newTestServer(t)
|
||||
handler := srv.Handler()
|
||||
|
||||
adminToken, _ := issueAdminToken(t, srv, priv, "admin-deny")
|
||||
|
||||
// Create an Allow rule for the system account.
|
||||
svcToken, svcAcct := issueSystemToken(t, srv, priv, "svc-deny", []string{"user"})
|
||||
allow := createPolicyRuleRequest{
|
||||
Description: "allow svc-deny to list accounts",
|
||||
Priority: 50,
|
||||
Rule: policy.RuleBody{
|
||||
SubjectUUID: svcAcct.UUID,
|
||||
Actions: []policy.Action{policy.ActionListAccounts},
|
||||
Effect: policy.Allow,
|
||||
},
|
||||
}
|
||||
rr := doRequest(t, handler, "POST", "/v1/policy/rules", allow, adminToken)
|
||||
if rr.Code != http.StatusCreated {
|
||||
t.Fatalf("create allow rule status = %d; body: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
|
||||
// Verify access is granted.
|
||||
rr = doRequest(t, handler, "GET", "/v1/accounts", nil, svcToken)
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("with allow rule, list accounts status = %d, want 200", rr.Code)
|
||||
}
|
||||
|
||||
// Add a higher-priority Deny rule for the same account.
|
||||
deny := createPolicyRuleRequest{
|
||||
Description: "deny svc-deny accounts:list",
|
||||
Priority: 10, // lower number = higher precedence
|
||||
Rule: policy.RuleBody{
|
||||
SubjectUUID: svcAcct.UUID,
|
||||
Actions: []policy.Action{policy.ActionListAccounts},
|
||||
Effect: policy.Deny,
|
||||
},
|
||||
}
|
||||
rr = doRequest(t, handler, "POST", "/v1/policy/rules", deny, adminToken)
|
||||
if rr.Code != http.StatusCreated {
|
||||
t.Fatalf("create deny rule status = %d; body: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
|
||||
// Deny-wins: access must now be blocked despite the Allow rule.
|
||||
rr = doRequest(t, handler, "GET", "/v1/accounts", nil, svcToken)
|
||||
if rr.Code != http.StatusForbidden {
|
||||
t.Errorf("deny-wins: list accounts status = %d, want 403", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
102
internal/server/vault.go
Normal file
102
internal/server/vault.go
Normal file
@@ -0,0 +1,102 @@
|
||||
// Vault seal/unseal REST handlers for MCIAS.
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.wntrmute.dev/kyle/mcias/internal/audit"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/middleware"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/model"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/vault"
|
||||
)
|
||||
|
||||
// unsealRequest is the request body for POST /v1/vault/unseal.
|
||||
type unsealRequest struct {
|
||||
Passphrase string `json:"passphrase"`
|
||||
}
|
||||
|
||||
// handleUnseal accepts a passphrase, derives the master key, decrypts the
|
||||
// signing key, and unseals the vault. Rate-limited to 3/s burst 5.
|
||||
//
|
||||
// Security: The passphrase is never logged. A generic error is returned on
|
||||
// any failure to prevent information leakage about the vault state.
|
||||
func (s *Server) handleUnseal(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.vault.IsSealed() {
|
||||
writeJSON(w, http.StatusOK, map[string]string{"status": "already unsealed"})
|
||||
return
|
||||
}
|
||||
|
||||
var req unsealRequest
|
||||
if !decodeJSON(w, r, &req) {
|
||||
return
|
||||
}
|
||||
if req.Passphrase == "" {
|
||||
middleware.WriteError(w, http.StatusBadRequest, "passphrase is required", "bad_request")
|
||||
return
|
||||
}
|
||||
|
||||
// Derive master key from passphrase.
|
||||
masterKey, err := vault.DeriveFromPassphrase(req.Passphrase, s.db)
|
||||
if err != nil {
|
||||
s.logger.Error("vault unseal: derive key", "error", err)
|
||||
middleware.WriteError(w, http.StatusUnauthorized, "unseal failed", "unauthorized")
|
||||
return
|
||||
}
|
||||
|
||||
// Decrypt the signing key.
|
||||
privKey, pubKey, err := vault.DecryptSigningKey(s.db, masterKey)
|
||||
if err != nil {
|
||||
// Zero derived master key on failure.
|
||||
for i := range masterKey {
|
||||
masterKey[i] = 0
|
||||
}
|
||||
s.logger.Error("vault unseal: decrypt signing key", "error", err)
|
||||
middleware.WriteError(w, http.StatusUnauthorized, "unseal failed", "unauthorized")
|
||||
return
|
||||
}
|
||||
|
||||
if err := s.vault.Unseal(masterKey, privKey, pubKey); err != nil {
|
||||
s.logger.Error("vault unseal: state transition", "error", err)
|
||||
middleware.WriteError(w, http.StatusConflict, "vault is already unsealed", "conflict")
|
||||
return
|
||||
}
|
||||
|
||||
ip := middleware.ClientIP(r, nil)
|
||||
s.writeAudit(r, model.EventVaultUnsealed, nil, nil, audit.JSON("source", "api", "ip", ip))
|
||||
s.logger.Info("vault unsealed via API", "ip", ip)
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]string{"status": "unsealed"})
|
||||
}
|
||||
|
||||
// handleSeal seals the vault, zeroing all key material. Admin-only.
|
||||
//
|
||||
// Security: The caller's token becomes invalid after sealing because the
|
||||
// public key needed to validate it is no longer available.
|
||||
func (s *Server) handleSeal(w http.ResponseWriter, r *http.Request) {
|
||||
if s.vault.IsSealed() {
|
||||
writeJSON(w, http.StatusOK, map[string]string{"status": "already sealed"})
|
||||
return
|
||||
}
|
||||
|
||||
claims := middleware.ClaimsFromContext(r.Context())
|
||||
var actorID *int64
|
||||
if claims != nil {
|
||||
acct, err := s.db.GetAccountByUUID(claims.Subject)
|
||||
if err == nil {
|
||||
actorID = &acct.ID
|
||||
}
|
||||
}
|
||||
|
||||
s.vault.Seal()
|
||||
|
||||
ip := middleware.ClientIP(r, nil)
|
||||
s.writeAudit(r, model.EventVaultSealed, actorID, nil, audit.JSON("source", "api", "ip", ip))
|
||||
s.logger.Info("vault sealed via API", "ip", ip)
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]string{"status": "sealed"})
|
||||
}
|
||||
|
||||
// handleVaultStatus returns the current seal state of the vault.
|
||||
func (s *Server) handleVaultStatus(w http.ResponseWriter, _ *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]bool{"sealed": s.vault.IsSealed()})
|
||||
}
|
||||
171
internal/server/vault_test.go
Normal file
171
internal/server/vault_test.go
Normal file
@@ -0,0 +1,171 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.wntrmute.dev/kyle/mcias/internal/vault"
|
||||
)
|
||||
|
||||
func TestHandleHealthSealed(t *testing.T) {
|
||||
srv, _, _, _ := newTestServer(t)
|
||||
srv.vault.Seal()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/v1/health", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("health status = %d, want 200", rr.Code)
|
||||
}
|
||||
|
||||
var resp map[string]string
|
||||
if err := json.NewDecoder(rr.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode health: %v", err)
|
||||
}
|
||||
if resp["status"] != "sealed" {
|
||||
t.Fatalf("health status = %q, want sealed", resp["status"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleHealthUnsealed(t *testing.T) {
|
||||
srv, _, _, _ := newTestServer(t)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/v1/health", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("health status = %d, want 200", rr.Code)
|
||||
}
|
||||
|
||||
var resp map[string]string
|
||||
if err := json.NewDecoder(rr.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode health: %v", err)
|
||||
}
|
||||
if resp["status"] != "ok" {
|
||||
t.Fatalf("health status = %q, want ok", resp["status"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestVaultStatusEndpoint(t *testing.T) {
|
||||
srv, _, _, _ := newTestServer(t)
|
||||
|
||||
// Unsealed
|
||||
req := httptest.NewRequest(http.MethodGet, "/v1/vault/status", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(rr, req)
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("status code = %d, want 200", rr.Code)
|
||||
}
|
||||
var resp map[string]bool
|
||||
if err := json.NewDecoder(rr.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if resp["sealed"] {
|
||||
t.Fatal("vault should be unsealed")
|
||||
}
|
||||
|
||||
// Seal and check again
|
||||
srv.vault.Seal()
|
||||
req = httptest.NewRequest(http.MethodGet, "/v1/vault/status", nil)
|
||||
rr = httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(rr, req)
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("status code = %d, want 200", rr.Code)
|
||||
}
|
||||
resp = nil
|
||||
if err := json.NewDecoder(rr.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if !resp["sealed"] {
|
||||
t.Fatal("vault should be sealed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSealedMiddlewareAPIReturns503(t *testing.T) {
|
||||
srv, _, _, _ := newTestServer(t)
|
||||
srv.vault.Seal()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/v1/accounts", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("sealed API status = %d, want 503", rr.Code)
|
||||
}
|
||||
|
||||
var resp map[string]string
|
||||
if err := json.NewDecoder(rr.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if resp["code"] != "vault_sealed" {
|
||||
t.Fatalf("error code = %q, want vault_sealed", resp["code"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSealedMiddlewareUIRedirects(t *testing.T) {
|
||||
srv, _, _, _ := newTestServer(t)
|
||||
srv.vault.Seal()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/dashboard", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusFound {
|
||||
t.Fatalf("sealed UI status = %d, want 302", rr.Code)
|
||||
}
|
||||
loc := rr.Header().Get("Location")
|
||||
if loc != "/unseal" {
|
||||
t.Fatalf("redirect location = %q, want /unseal", loc)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnsealBadPassphrase(t *testing.T) {
|
||||
srv, _, _, _ := newTestServer(t)
|
||||
// Start sealed.
|
||||
v := vault.NewSealed()
|
||||
srv.vault = v
|
||||
|
||||
body := `{"passphrase":"wrong-passphrase"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/v1/vault/unseal", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rr := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("unseal with bad passphrase status = %d, want 401", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSealAlreadySealedNoop(t *testing.T) {
|
||||
srv, _, priv, _ := newTestServer(t)
|
||||
|
||||
// Seal via API (needs admin token)
|
||||
adminToken, _ := issueAdminToken(t, srv, priv, "admin")
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/v1/vault/seal", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+adminToken)
|
||||
rr := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("seal status = %d, want 200", rr.Code)
|
||||
}
|
||||
|
||||
var resp map[string]string
|
||||
if err := json.NewDecoder(rr.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if resp["status"] != "sealed" {
|
||||
t.Fatalf("seal response status = %q, want sealed", resp["status"])
|
||||
}
|
||||
|
||||
// Vault should be sealed now
|
||||
if !srv.vault.IsSealed() {
|
||||
t.Fatal("vault should be sealed after seal API call")
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,9 @@ import (
|
||||
"crypto/subtle"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"git.wntrmute.dev/kyle/mcias/internal/vault"
|
||||
)
|
||||
|
||||
// CSRFManager implements HMAC-signed Double-Submit Cookie CSRF protection.
|
||||
@@ -21,17 +24,67 @@ import (
|
||||
// - The form/header value is HMAC-SHA256(key, cookieVal); this is what the
|
||||
// server verifies. An attacker cannot forge the HMAC without the key.
|
||||
// - Comparison uses crypto/subtle.ConstantTimeCompare to prevent timing attacks.
|
||||
// - When backed by a vault, the key is derived lazily on first use after
|
||||
// unseal. When the vault is re-sealed, the key is invalidated and re-derived
|
||||
// on the next unseal. This is safe because sealed middleware prevents
|
||||
// reaching CSRF-protected routes.
|
||||
type CSRFManager struct {
|
||||
key []byte
|
||||
mu sync.Mutex
|
||||
key []byte
|
||||
vault *vault.Vault
|
||||
}
|
||||
|
||||
// newCSRFManager creates a CSRFManager whose key is derived from masterKey.
|
||||
// newCSRFManager creates a CSRFManager with a static key derived from masterKey.
|
||||
// Key derivation: SHA-256("mcias-ui-csrf-v1" || masterKey)
|
||||
func newCSRFManager(masterKey []byte) *CSRFManager {
|
||||
return &CSRFManager{key: deriveCSRFKey(masterKey)}
|
||||
}
|
||||
|
||||
// newCSRFManagerFromVault creates a CSRFManager that derives its key lazily
|
||||
// from the vault's master key. When the vault is sealed, operations fail
|
||||
// gracefully (the sealed middleware prevents reaching CSRF-protected routes).
|
||||
func newCSRFManagerFromVault(v *vault.Vault) *CSRFManager {
|
||||
c := &CSRFManager{vault: v}
|
||||
// If already unsealed, derive immediately.
|
||||
mk, err := v.MasterKey()
|
||||
if err == nil {
|
||||
c.key = deriveCSRFKey(mk)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// deriveCSRFKey computes the HMAC key from a master key.
|
||||
func deriveCSRFKey(masterKey []byte) []byte {
|
||||
h := sha256.New()
|
||||
h.Write([]byte("mcias-ui-csrf-v1"))
|
||||
h.Write(masterKey)
|
||||
return &CSRFManager{key: h.Sum(nil)}
|
||||
return h.Sum(nil)
|
||||
}
|
||||
|
||||
// csrfKey returns the current CSRF key, deriving it from vault if needed.
|
||||
func (c *CSRFManager) csrfKey() ([]byte, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
// If we have a vault, re-derive key when sealed state changes.
|
||||
if c.vault != nil {
|
||||
if c.vault.IsSealed() {
|
||||
c.key = nil
|
||||
return nil, fmt.Errorf("csrf: vault is sealed")
|
||||
}
|
||||
if c.key == nil {
|
||||
mk, err := c.vault.MasterKey()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("csrf: %w", err)
|
||||
}
|
||||
c.key = deriveCSRFKey(mk)
|
||||
}
|
||||
}
|
||||
|
||||
if c.key == nil {
|
||||
return nil, fmt.Errorf("csrf: no key available")
|
||||
}
|
||||
return c.key, nil
|
||||
}
|
||||
|
||||
// NewToken generates a fresh CSRF token pair.
|
||||
@@ -40,12 +93,16 @@ func newCSRFManager(masterKey []byte) *CSRFManager {
|
||||
// - cookieVal: hex(32 random bytes) — stored in the mcias_csrf cookie
|
||||
// - headerVal: hex(HMAC-SHA256(key, cookieVal)) — embedded in forms / X-CSRF-Token header
|
||||
func (c *CSRFManager) NewToken() (cookieVal, headerVal string, err error) {
|
||||
key, err := c.csrfKey()
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
raw := make([]byte, 32)
|
||||
if _, err = rand.Read(raw); err != nil {
|
||||
return "", "", fmt.Errorf("csrf: generate random bytes: %w", err)
|
||||
}
|
||||
cookieVal = hex.EncodeToString(raw)
|
||||
mac := hmac.New(sha256.New, c.key)
|
||||
mac := hmac.New(sha256.New, key)
|
||||
mac.Write([]byte(cookieVal))
|
||||
headerVal = hex.EncodeToString(mac.Sum(nil))
|
||||
return cookieVal, headerVal, nil
|
||||
@@ -57,7 +114,11 @@ func (c *CSRFManager) Validate(cookieVal, headerVal string) bool {
|
||||
if cookieVal == "" || headerVal == "" {
|
||||
return false
|
||||
}
|
||||
mac := hmac.New(sha256.New, c.key)
|
||||
key, err := c.csrfKey()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
mac := hmac.New(sha256.New, key)
|
||||
mac.Write([]byte(cookieVal))
|
||||
expected := hex.EncodeToString(mac.Sum(nil))
|
||||
// Security: constant-time comparison prevents timing oracle attacks.
|
||||
|
||||
@@ -39,7 +39,7 @@ func (u *UIServer) handleAccountsList(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
u.render(w, "accounts", AccountsData{
|
||||
PageData: PageData{CSRFToken: csrfToken, ActorName: u.actorName(r)},
|
||||
PageData: PageData{CSRFToken: csrfToken, ActorName: u.actorName(r), IsAdmin: isAdmin(r)},
|
||||
Accounts: accounts,
|
||||
})
|
||||
}
|
||||
@@ -182,17 +182,35 @@ func (u *UIServer) handleAccountDetail(w http.ResponseWriter, r *http.Request) {
|
||||
tags = nil
|
||||
}
|
||||
|
||||
// For system accounts, load token issue delegates and the full account
|
||||
// list so admins can add new ones.
|
||||
var tokenDelegates []*model.ServiceAccountDelegate
|
||||
var delegatableAccounts []*model.Account
|
||||
if acct.AccountType == model.AccountTypeSystem && isAdmin(r) {
|
||||
tokenDelegates, err = u.db.ListTokenIssueDelegates(acct.ID)
|
||||
if err != nil {
|
||||
u.logger.Warn("list token issue delegates", "error", err)
|
||||
}
|
||||
delegatableAccounts, err = u.db.ListAccounts()
|
||||
if err != nil {
|
||||
u.logger.Warn("list accounts for delegate dropdown", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
u.render(w, "account_detail", AccountDetailData{
|
||||
PageData: PageData{CSRFToken: csrfToken, ActorName: u.actorName(r)},
|
||||
Account: acct,
|
||||
Roles: roles,
|
||||
AllRoles: knownRoles,
|
||||
Tokens: tokens,
|
||||
PGCred: pgCred,
|
||||
PGCredGrants: pgCredGrants,
|
||||
GrantableAccounts: grantableAccounts,
|
||||
ActorID: actorID,
|
||||
Tags: tags,
|
||||
PageData: PageData{CSRFToken: csrfToken, ActorName: u.actorName(r), IsAdmin: isAdmin(r)},
|
||||
Account: acct,
|
||||
Roles: roles,
|
||||
AllRoles: knownRoles,
|
||||
Tokens: tokens,
|
||||
PGCred: pgCred,
|
||||
PGCredGrants: pgCredGrants,
|
||||
GrantableAccounts: grantableAccounts,
|
||||
ActorID: actorID,
|
||||
Tags: tags,
|
||||
TokenDelegates: tokenDelegates,
|
||||
DelegatableAccounts: delegatableAccounts,
|
||||
CanIssueToken: true, // account_detail is admin-only, so admin can always issue
|
||||
})
|
||||
}
|
||||
|
||||
@@ -460,7 +478,12 @@ func (u *UIServer) handleSetPGCreds(w http.ResponseWriter, r *http.Request) {
|
||||
// Security: encrypt the password with AES-256-GCM before storage.
|
||||
// A fresh random nonce is generated per call by SealAESGCM; nonce reuse
|
||||
// is not possible. The plaintext password is not retained after this call.
|
||||
enc, nonce, err := crypto.SealAESGCM(u.masterKey, []byte(password))
|
||||
masterKey, err := u.vault.MasterKey()
|
||||
if err != nil {
|
||||
u.renderError(w, r, http.StatusInternalServerError, "internal error")
|
||||
return
|
||||
}
|
||||
enc, nonce, err := crypto.SealAESGCM(masterKey, []byte(password))
|
||||
if err != nil {
|
||||
u.logger.Error("encrypt pg password", "error", err)
|
||||
u.renderError(w, r, http.StatusInternalServerError, "internal error")
|
||||
@@ -790,7 +813,7 @@ func (u *UIServer) handlePGCredsList(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
u.render(w, "pgcreds", PGCredsData{
|
||||
PageData: PageData{CSRFToken: csrfToken, ActorName: u.actorName(r)},
|
||||
PageData: PageData{CSRFToken: csrfToken, ActorName: u.actorName(r), IsAdmin: isAdmin(r)},
|
||||
Creds: creds,
|
||||
UncredentialedAccounts: uncredentialed,
|
||||
CredGrants: credGrants,
|
||||
@@ -864,7 +887,12 @@ func (u *UIServer) handleCreatePGCreds(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Security: encrypt with AES-256-GCM; fresh nonce per call.
|
||||
enc, nonce, err := crypto.SealAESGCM(u.masterKey, []byte(password))
|
||||
masterKey, err := u.vault.MasterKey()
|
||||
if err != nil {
|
||||
u.renderError(w, r, http.StatusInternalServerError, "internal error")
|
||||
return
|
||||
}
|
||||
enc, nonce, err := crypto.SealAESGCM(masterKey, []byte(password))
|
||||
if err != nil {
|
||||
u.logger.Error("encrypt pg password", "error", err)
|
||||
u.renderError(w, r, http.StatusInternalServerError, "internal error")
|
||||
@@ -999,6 +1027,13 @@ func (u *UIServer) handleAdminResetPassword(w http.ResponseWriter, r *http.Reque
|
||||
}
|
||||
|
||||
// handleIssueSystemToken issues a long-lived service token for a system account.
|
||||
// Accessible to admins and to accounts that have been granted delegate access
|
||||
// for this specific service account via service_account_delegates.
|
||||
//
|
||||
// Security: authorization is checked server-side against the JWT claims stored
|
||||
// in the request context — it cannot be bypassed by client-side manipulation.
|
||||
// After issuance the token string is stored in a short-lived single-use
|
||||
// download nonce so the operator can retrieve it exactly once as a file.
|
||||
func (u *UIServer) handleIssueSystemToken(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
acct, err := u.db.GetAccountByUUID(id)
|
||||
@@ -1011,6 +1046,32 @@ func (u *UIServer) handleIssueSystemToken(w http.ResponseWriter, r *http.Request
|
||||
return
|
||||
}
|
||||
|
||||
// Security: require admin role OR an explicit delegate grant for this account.
|
||||
actorClaims := claimsFromContext(r.Context())
|
||||
var actorID *int64
|
||||
if !isAdmin(r) {
|
||||
if actorClaims == nil {
|
||||
u.renderError(w, r, http.StatusForbidden, "access denied")
|
||||
return
|
||||
}
|
||||
actor, err := u.db.GetAccountByUUID(actorClaims.Subject)
|
||||
if err != nil {
|
||||
u.renderError(w, r, http.StatusForbidden, "access denied")
|
||||
return
|
||||
}
|
||||
actorID = &actor.ID
|
||||
hasAccess, err := u.db.HasTokenIssueAccess(acct.ID, actor.ID)
|
||||
if err != nil || !hasAccess {
|
||||
u.renderError(w, r, http.StatusForbidden, "not authorized to issue tokens for this service account")
|
||||
return
|
||||
}
|
||||
} else if actorClaims != nil {
|
||||
actor, err := u.db.GetAccountByUUID(actorClaims.Subject)
|
||||
if err == nil {
|
||||
actorID = &actor.ID
|
||||
}
|
||||
}
|
||||
|
||||
roles, err := u.db.GetRoles(acct.ID)
|
||||
if err != nil {
|
||||
u.renderError(w, r, http.StatusInternalServerError, "failed to load roles")
|
||||
@@ -1044,17 +1105,18 @@ func (u *UIServer) handleIssueSystemToken(w http.ResponseWriter, r *http.Request
|
||||
u.logger.Warn("set system token record", "error", err)
|
||||
}
|
||||
|
||||
actorClaims := claimsFromContext(r.Context())
|
||||
var actorID *int64
|
||||
if actorClaims != nil {
|
||||
actor, err := u.db.GetAccountByUUID(actorClaims.Subject)
|
||||
if err == nil {
|
||||
actorID = &actor.ID
|
||||
}
|
||||
}
|
||||
u.writeAudit(r, model.EventTokenIssued, actorID, &acct.ID,
|
||||
fmt.Sprintf(`{"jti":%q,"via":"ui_system_token"}`, claims.JTI))
|
||||
|
||||
// Store the raw token in the short-lived download cache so the operator
|
||||
// can retrieve it exactly once via the download endpoint.
|
||||
downloadNonce, err := u.storeTokenDownload(tokenStr, acct.UUID)
|
||||
if err != nil {
|
||||
u.logger.Error("store token download nonce", "error", err)
|
||||
// Non-fatal: fall back to showing the token in the flash message.
|
||||
downloadNonce = ""
|
||||
}
|
||||
|
||||
// Re-fetch token list including the new token.
|
||||
tokens, err := u.db.ListTokensForAccount(acct.ID)
|
||||
if err != nil {
|
||||
@@ -1067,13 +1129,209 @@ func (u *UIServer) handleIssueSystemToken(w http.ResponseWriter, r *http.Request
|
||||
csrfToken = ""
|
||||
}
|
||||
|
||||
// Flash the raw token once at the top so the operator can copy it.
|
||||
var flash string
|
||||
if downloadNonce == "" {
|
||||
// Fallback: show token in flash when download nonce could not be stored.
|
||||
flash = fmt.Sprintf("Token issued. Copy now — it will not be shown again: %s", tokenStr)
|
||||
} else {
|
||||
flash = "Token issued. Download it now — it will not be available again."
|
||||
}
|
||||
|
||||
u.render(w, "token_list", AccountDetailData{
|
||||
PageData: PageData{
|
||||
CSRFToken: csrfToken,
|
||||
Flash: fmt.Sprintf("Token issued. Copy now — it will not be shown again: %s", tokenStr),
|
||||
},
|
||||
Account: acct,
|
||||
Tokens: tokens,
|
||||
PageData: PageData{CSRFToken: csrfToken, Flash: flash},
|
||||
Account: acct,
|
||||
Tokens: tokens,
|
||||
DownloadNonce: downloadNonce,
|
||||
})
|
||||
}
|
||||
|
||||
// handleDownloadToken serves the just-issued service token as a file
|
||||
// attachment. The nonce is single-use and expires after tokenDownloadTTL.
|
||||
//
|
||||
// Security: the nonce was generated with crypto/rand (128 bits) at issuance
|
||||
// time and is deleted from the in-memory store on first retrieval, preventing
|
||||
// replay. The response sets Content-Disposition: attachment so the browser
|
||||
// saves the file rather than rendering it, reducing the risk of an XSS vector
|
||||
// if the token were displayed inline.
|
||||
func (u *UIServer) handleDownloadToken(w http.ResponseWriter, r *http.Request) {
|
||||
nonce := r.PathValue("nonce")
|
||||
if nonce == "" {
|
||||
http.Error(w, "missing nonce", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
tokenStr, accountID, ok := u.consumeTokenDownload(nonce)
|
||||
if !ok {
|
||||
http.Error(w, "download link expired or already used", http.StatusGone)
|
||||
return
|
||||
}
|
||||
|
||||
filename := "service-account-" + accountID + ".token"
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
||||
// Security: Content-Type is text/plain and Content-Disposition is attachment,
|
||||
// so the browser will save the file rather than render it, mitigating XSS risk.
|
||||
_, _ = fmt.Fprint(w, tokenStr) //nolint:gosec // G705: token served as attachment, not rendered by browser
|
||||
}
|
||||
|
||||
// handleGrantTokenDelegate adds a delegate who may issue tokens for a system
|
||||
// account. Only admins may call this endpoint.
|
||||
//
|
||||
// Security: the target system account and grantee are looked up by UUID so the
|
||||
// URL/form fields cannot reference arbitrary row IDs. Audit event
|
||||
// EventTokenDelegateGranted is recorded on success.
|
||||
func (u *UIServer) handleGrantTokenDelegate(w http.ResponseWriter, r *http.Request) {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxFormBytes)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
u.renderError(w, r, http.StatusBadRequest, "invalid form")
|
||||
return
|
||||
}
|
||||
|
||||
id := r.PathValue("id")
|
||||
acct, err := u.db.GetAccountByUUID(id)
|
||||
if err != nil {
|
||||
u.renderError(w, r, http.StatusNotFound, "service account not found")
|
||||
return
|
||||
}
|
||||
if acct.AccountType != model.AccountTypeSystem {
|
||||
u.renderError(w, r, http.StatusBadRequest, "token issue delegates are only supported for system accounts")
|
||||
return
|
||||
}
|
||||
|
||||
granteeUUID := strings.TrimSpace(r.FormValue("grantee_uuid"))
|
||||
if granteeUUID == "" {
|
||||
u.renderError(w, r, http.StatusBadRequest, "grantee is required")
|
||||
return
|
||||
}
|
||||
grantee, err := u.db.GetAccountByUUID(granteeUUID)
|
||||
if err != nil {
|
||||
u.renderError(w, r, http.StatusNotFound, "grantee account not found")
|
||||
return
|
||||
}
|
||||
|
||||
actorClaims := claimsFromContext(r.Context())
|
||||
var actorID *int64
|
||||
if actorClaims != nil {
|
||||
actor, err := u.db.GetAccountByUUID(actorClaims.Subject)
|
||||
if err == nil {
|
||||
actorID = &actor.ID
|
||||
}
|
||||
}
|
||||
|
||||
if err := u.db.GrantTokenIssueAccess(acct.ID, grantee.ID, actorID); err != nil {
|
||||
u.logger.Error("grant token issue access", "error", err)
|
||||
u.renderError(w, r, http.StatusInternalServerError, "failed to grant access")
|
||||
return
|
||||
}
|
||||
|
||||
u.writeAudit(r, model.EventTokenDelegateGranted, actorID, &acct.ID,
|
||||
fmt.Sprintf(`{"grantee":%q}`, grantee.UUID))
|
||||
|
||||
delegates, err := u.db.ListTokenIssueDelegates(acct.ID)
|
||||
if err != nil {
|
||||
u.logger.Warn("list token issue delegates after grant", "error", err)
|
||||
}
|
||||
allAccounts, err := u.db.ListAccounts()
|
||||
if err != nil {
|
||||
u.logger.Warn("list accounts for delegate grant", "error", err)
|
||||
}
|
||||
csrfToken, err := u.setCSRFCookies(w)
|
||||
if err != nil {
|
||||
csrfToken = ""
|
||||
}
|
||||
u.render(w, "token_delegates", AccountDetailData{
|
||||
PageData: PageData{CSRFToken: csrfToken},
|
||||
Account: acct,
|
||||
TokenDelegates: delegates,
|
||||
DelegatableAccounts: allAccounts,
|
||||
})
|
||||
}
|
||||
|
||||
// handleRevokeTokenDelegate removes a delegate's permission to issue tokens for
|
||||
// a system account. Only admins may call this endpoint.
|
||||
//
|
||||
// Security: grantee looked up by UUID from the URL path. Audit event
|
||||
// EventTokenDelegateRevoked recorded on success.
|
||||
func (u *UIServer) handleRevokeTokenDelegate(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
acct, err := u.db.GetAccountByUUID(id)
|
||||
if err != nil {
|
||||
u.renderError(w, r, http.StatusNotFound, "service account not found")
|
||||
return
|
||||
}
|
||||
|
||||
granteeUUID := r.PathValue("grantee")
|
||||
grantee, err := u.db.GetAccountByUUID(granteeUUID)
|
||||
if err != nil {
|
||||
u.renderError(w, r, http.StatusNotFound, "grantee not found")
|
||||
return
|
||||
}
|
||||
|
||||
if err := u.db.RevokeTokenIssueAccess(acct.ID, grantee.ID); err != nil {
|
||||
u.renderError(w, r, http.StatusInternalServerError, "failed to revoke access")
|
||||
return
|
||||
}
|
||||
|
||||
actorClaims := claimsFromContext(r.Context())
|
||||
var actorID *int64
|
||||
if actorClaims != nil {
|
||||
actor, err := u.db.GetAccountByUUID(actorClaims.Subject)
|
||||
if err == nil {
|
||||
actorID = &actor.ID
|
||||
}
|
||||
}
|
||||
u.writeAudit(r, model.EventTokenDelegateRevoked, actorID, &acct.ID,
|
||||
fmt.Sprintf(`{"grantee":%q}`, grantee.UUID))
|
||||
|
||||
delegates, err := u.db.ListTokenIssueDelegates(acct.ID)
|
||||
if err != nil {
|
||||
u.logger.Warn("list token issue delegates after revoke", "error", err)
|
||||
}
|
||||
allAccounts, err := u.db.ListAccounts()
|
||||
if err != nil {
|
||||
u.logger.Warn("list accounts for delegate dropdown", "error", err)
|
||||
}
|
||||
csrfToken, err := u.setCSRFCookies(w)
|
||||
if err != nil {
|
||||
csrfToken = ""
|
||||
}
|
||||
u.render(w, "token_delegates", AccountDetailData{
|
||||
PageData: PageData{CSRFToken: csrfToken},
|
||||
Account: acct,
|
||||
TokenDelegates: delegates,
|
||||
DelegatableAccounts: allAccounts,
|
||||
})
|
||||
}
|
||||
|
||||
// handleServiceAccountsPage renders the /service-accounts page showing all
|
||||
// system accounts the current user has delegate access to, along with the
|
||||
// ability to issue and download tokens for them.
|
||||
func (u *UIServer) handleServiceAccountsPage(w http.ResponseWriter, r *http.Request) {
|
||||
csrfToken, err := u.setCSRFCookies(w)
|
||||
if err != nil {
|
||||
http.Error(w, "internal error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
claims := claimsFromContext(r.Context())
|
||||
if claims == nil {
|
||||
u.redirectToLogin(w, r)
|
||||
return
|
||||
}
|
||||
actor, err := u.db.GetAccountByUUID(claims.Subject)
|
||||
if err != nil {
|
||||
u.renderError(w, r, http.StatusInternalServerError, "could not resolve actor")
|
||||
return
|
||||
}
|
||||
|
||||
accounts, err := u.db.ListDelegatedServiceAccounts(actor.ID)
|
||||
if err != nil {
|
||||
u.renderError(w, r, http.StatusInternalServerError, "failed to load service accounts")
|
||||
return
|
||||
}
|
||||
|
||||
u.render(w, "service_accounts", ServiceAccountsData{
|
||||
PageData: PageData{CSRFToken: csrfToken, ActorName: u.actorName(r), IsAdmin: isAdmin(r)},
|
||||
Accounts: accounts,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ func (u *UIServer) handleAuditDetail(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
u.render(w, "audit_detail", AuditDetailData{
|
||||
PageData: PageData{CSRFToken: csrfToken, ActorName: u.actorName(r)},
|
||||
PageData: PageData{CSRFToken: csrfToken, ActorName: u.actorName(r), IsAdmin: isAdmin(r)},
|
||||
Event: event,
|
||||
})
|
||||
}
|
||||
@@ -116,7 +116,7 @@ func (u *UIServer) buildAuditData(r *http.Request, page int, csrfToken string) (
|
||||
}
|
||||
|
||||
return AuditData{
|
||||
PageData: PageData{CSRFToken: csrfToken, ActorName: u.actorName(r)},
|
||||
PageData: PageData{CSRFToken: csrfToken, ActorName: u.actorName(r), IsAdmin: isAdmin(r)},
|
||||
Events: events,
|
||||
EventTypes: auditEventTypes,
|
||||
FilterType: filterType,
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.wntrmute.dev/kyle/mcias/internal/audit"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/auth"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/crypto"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/model"
|
||||
@@ -59,7 +59,7 @@ func (u *UIServer) handleLoginPost(w http.ResponseWriter, r *http.Request) {
|
||||
// Security: always run dummy Argon2 to prevent timing-based user enumeration.
|
||||
_, _ = auth.VerifyPassword("dummy", u.dummyHash())
|
||||
u.writeAudit(r, model.EventLoginFail, nil, nil,
|
||||
fmt.Sprintf(`{"username":%q,"reason":"unknown_user"}`, username))
|
||||
audit.JSON("username", username, "reason", "unknown_user"))
|
||||
u.render(w, "login", LoginData{Error: "invalid credentials"})
|
||||
return
|
||||
}
|
||||
@@ -80,7 +80,9 @@ func (u *UIServer) handleLoginPost(w http.ResponseWriter, r *http.Request) {
|
||||
if locked {
|
||||
_, _ = auth.VerifyPassword("dummy", u.dummyHash())
|
||||
u.writeAudit(r, model.EventLoginFail, &acct.ID, nil, `{"reason":"account_locked"}`)
|
||||
u.render(w, "login", LoginData{Error: "account temporarily locked, please try again later"})
|
||||
// Security: return the same "invalid credentials" as wrong-password
|
||||
// to prevent user-enumeration via lockout differentiation (SEC-02).
|
||||
u.render(w, "login", LoginData{Error: "invalid credentials"})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -130,7 +132,7 @@ func (u *UIServer) handleTOTPStep(w http.ResponseWriter, r *http.Request) {
|
||||
accountID, ok := u.consumeTOTPNonce(nonce)
|
||||
if !ok {
|
||||
u.writeAudit(r, model.EventLoginFail, nil, nil,
|
||||
fmt.Sprintf(`{"username":%q,"reason":"invalid_totp_nonce"}`, username))
|
||||
audit.JSON("username", username, "reason", "invalid_totp_nonce"))
|
||||
u.render(w, "login", LoginData{Error: "session expired, please log in again"})
|
||||
return
|
||||
}
|
||||
@@ -143,7 +145,12 @@ func (u *UIServer) handleTOTPStep(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Decrypt and validate TOTP secret.
|
||||
secret, err := crypto.OpenAESGCM(u.masterKey, acct.TOTPSecretNonce, acct.TOTPSecretEnc)
|
||||
masterKey, err := u.vault.MasterKey()
|
||||
if err != nil {
|
||||
u.render(w, "login", LoginData{Error: "internal error"})
|
||||
return
|
||||
}
|
||||
secret, err := crypto.OpenAESGCM(masterKey, acct.TOTPSecretNonce, acct.TOTPSecretEnc)
|
||||
if err != nil {
|
||||
u.logger.Error("decrypt TOTP secret", "error", err, "account_id", acct.ID)
|
||||
u.render(w, "login", LoginData{Error: "internal error"})
|
||||
@@ -206,7 +213,12 @@ func (u *UIServer) finishLogin(w http.ResponseWriter, r *http.Request, acct *mod
|
||||
// Login succeeded: clear any outstanding failure counter.
|
||||
_ = u.db.ClearLoginFailures(acct.ID)
|
||||
|
||||
tokenStr, claims, err := token.IssueToken(u.privKey, u.cfg.Tokens.Issuer, acct.UUID, roles, expiry)
|
||||
privKey, err := u.vault.PrivKey()
|
||||
if err != nil {
|
||||
u.render(w, "login", LoginData{Error: "internal error"})
|
||||
return
|
||||
}
|
||||
tokenStr, claims, err := token.IssueToken(privKey, u.cfg.Tokens.Issuer, acct.UUID, roles, expiry)
|
||||
if err != nil {
|
||||
u.logger.Error("issue token", "error", err)
|
||||
u.render(w, "login", LoginData{Error: "internal error"})
|
||||
@@ -238,7 +250,7 @@ func (u *UIServer) finishLogin(w http.ResponseWriter, r *http.Request, acct *mod
|
||||
|
||||
u.writeAudit(r, model.EventLoginOK, &acct.ID, nil, "")
|
||||
u.writeAudit(r, model.EventTokenIssued, &acct.ID, nil,
|
||||
fmt.Sprintf(`{"jti":%q,"via":"ui"}`, claims.JTI))
|
||||
audit.JSON("jti", claims.JTI, "via", "ui"))
|
||||
|
||||
// Redirect to dashboard.
|
||||
if isHTMX(r) {
|
||||
@@ -253,13 +265,14 @@ func (u *UIServer) finishLogin(w http.ResponseWriter, r *http.Request, acct *mod
|
||||
func (u *UIServer) handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
cookie, err := r.Cookie(sessionCookieName)
|
||||
if err == nil && cookie.Value != "" {
|
||||
claims, err := validateSessionToken(u.pubKey, cookie.Value, u.cfg.Tokens.Issuer)
|
||||
pubKey, _ := u.vault.PubKey()
|
||||
claims, err := validateSessionToken(pubKey, cookie.Value, u.cfg.Tokens.Issuer)
|
||||
if err == nil {
|
||||
if revokeErr := u.db.RevokeToken(claims.JTI, "ui_logout"); revokeErr != nil {
|
||||
u.logger.Warn("revoke token on UI logout", "error", revokeErr)
|
||||
}
|
||||
u.writeAudit(r, model.EventTokenRevoked, nil, nil,
|
||||
fmt.Sprintf(`{"jti":%q,"reason":"ui_logout"}`, claims.JTI))
|
||||
audit.JSON("jti", claims.JTI, "reason", "ui_logout"))
|
||||
}
|
||||
}
|
||||
u.clearSessionCookie(w)
|
||||
@@ -281,6 +294,7 @@ func (u *UIServer) handleProfilePage(w http.ResponseWriter, r *http.Request) {
|
||||
PageData: PageData{
|
||||
CSRFToken: csrfToken,
|
||||
ActorName: u.actorName(r),
|
||||
IsAdmin: isAdmin(r),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -393,6 +407,7 @@ func (u *UIServer) handleSelfChangePassword(w http.ResponseWriter, r *http.Reque
|
||||
PageData: PageData{
|
||||
CSRFToken: csrfToken,
|
||||
ActorName: u.actorName(r),
|
||||
IsAdmin: isAdmin(r),
|
||||
Flash: "Password updated successfully. Other active sessions have been revoked.",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -7,7 +7,8 @@ import (
|
||||
"git.wntrmute.dev/kyle/mcias/internal/model"
|
||||
)
|
||||
|
||||
// handleDashboard renders the main dashboard page with account counts and recent events.
|
||||
// handleDashboard renders the main dashboard page. Admin users see account
|
||||
// counts and recent audit events; non-admin users see a welcome page.
|
||||
func (u *UIServer) handleDashboard(w http.ResponseWriter, r *http.Request) {
|
||||
csrfToken, err := u.setCSRFCookies(w)
|
||||
if err != nil {
|
||||
@@ -16,30 +17,33 @@ func (u *UIServer) handleDashboard(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
accounts, err := u.db.ListAccounts()
|
||||
if err != nil {
|
||||
u.renderError(w, r, http.StatusInternalServerError, "failed to load accounts")
|
||||
return
|
||||
admin := isAdmin(r)
|
||||
|
||||
data := DashboardData{
|
||||
PageData: PageData{CSRFToken: csrfToken, ActorName: u.actorName(r), IsAdmin: admin},
|
||||
}
|
||||
|
||||
var total, active int
|
||||
for _, a := range accounts {
|
||||
total++
|
||||
if a.Status == model.AccountStatusActive {
|
||||
active++
|
||||
if admin {
|
||||
accounts, err := u.db.ListAccounts()
|
||||
if err != nil {
|
||||
u.renderError(w, r, http.StatusInternalServerError, "failed to load accounts")
|
||||
return
|
||||
}
|
||||
|
||||
for _, a := range accounts {
|
||||
data.TotalAccounts++
|
||||
if a.Status == model.AccountStatusActive {
|
||||
data.ActiveAccounts++
|
||||
}
|
||||
}
|
||||
|
||||
events, _, err := u.db.ListAuditEventsPaged(db.AuditQueryParams{Limit: 10, Offset: 0})
|
||||
if err != nil {
|
||||
u.logger.Warn("load recent audit events", "error", err)
|
||||
events = nil
|
||||
}
|
||||
data.RecentEvents = events
|
||||
}
|
||||
|
||||
events, _, err := u.db.ListAuditEventsPaged(db.AuditQueryParams{Limit: 10, Offset: 0})
|
||||
if err != nil {
|
||||
u.logger.Warn("load recent audit events", "error", err)
|
||||
events = nil
|
||||
}
|
||||
|
||||
u.render(w, "dashboard", DashboardData{
|
||||
PageData: PageData{CSRFToken: csrfToken, ActorName: u.actorName(r)},
|
||||
TotalAccounts: total,
|
||||
ActiveAccounts: active,
|
||||
RecentEvents: events,
|
||||
})
|
||||
u.render(w, "dashboard", data)
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ func (u *UIServer) handlePoliciesPage(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
data := PoliciesData{
|
||||
PageData: PageData{CSRFToken: csrfToken, ActorName: u.actorName(r)},
|
||||
PageData: PageData{CSRFToken: csrfToken, ActorName: u.actorName(r), IsAdmin: isAdmin(r)},
|
||||
Rules: views,
|
||||
AllActions: allActionStrings,
|
||||
}
|
||||
|
||||
81
internal/ui/handlers_vault.go
Normal file
81
internal/ui/handlers_vault.go
Normal file
@@ -0,0 +1,81 @@
|
||||
// UI handlers for vault unseal page.
|
||||
package ui
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.wntrmute.dev/kyle/mcias/internal/audit"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/middleware"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/model"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/vault"
|
||||
)
|
||||
|
||||
// UnsealData is the view model for the unseal page.
|
||||
type UnsealData struct {
|
||||
Error string
|
||||
}
|
||||
|
||||
// handleUnsealPage renders the unseal form, or redirects to login if already unsealed.
|
||||
func (u *UIServer) handleUnsealPage(w http.ResponseWriter, r *http.Request) {
|
||||
if !u.vault.IsSealed() {
|
||||
http.Redirect(w, r, "/login", http.StatusFound)
|
||||
return
|
||||
}
|
||||
u.render(w, "unseal", UnsealData{})
|
||||
}
|
||||
|
||||
// handleUnsealPost processes the unseal form submission.
|
||||
//
|
||||
// Security: The passphrase is never logged. No CSRF protection is applied
|
||||
// because there is no session to protect (the vault is sealed), and CSRF
|
||||
// token generation depends on the master key (chicken-and-egg).
|
||||
func (u *UIServer) handleUnsealPost(w http.ResponseWriter, r *http.Request) {
|
||||
if !u.vault.IsSealed() {
|
||||
http.Redirect(w, r, "/login", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxFormBytes)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
u.render(w, "unseal", UnsealData{Error: "invalid form data"})
|
||||
return
|
||||
}
|
||||
|
||||
passphrase := r.FormValue("passphrase")
|
||||
if passphrase == "" {
|
||||
u.render(w, "unseal", UnsealData{Error: "passphrase is required"})
|
||||
return
|
||||
}
|
||||
|
||||
// Derive master key from passphrase.
|
||||
masterKey, err := vault.DeriveFromPassphrase(passphrase, u.db)
|
||||
if err != nil {
|
||||
u.logger.Error("vault unseal (UI): derive key", "error", err)
|
||||
u.render(w, "unseal", UnsealData{Error: "unseal failed"})
|
||||
return
|
||||
}
|
||||
|
||||
// Decrypt the signing key.
|
||||
privKey, pubKey, err := vault.DecryptSigningKey(u.db, masterKey)
|
||||
if err != nil {
|
||||
// Zero derived master key on failure.
|
||||
for i := range masterKey {
|
||||
masterKey[i] = 0
|
||||
}
|
||||
u.logger.Error("vault unseal (UI): decrypt signing key", "error", err)
|
||||
u.render(w, "unseal", UnsealData{Error: "unseal failed"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := u.vault.Unseal(masterKey, privKey, pubKey); err != nil {
|
||||
u.logger.Error("vault unseal (UI): state transition", "error", err)
|
||||
http.Redirect(w, r, "/login", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
ip := middleware.ClientIP(r, nil)
|
||||
u.writeAudit(r, model.EventVaultUnsealed, nil, nil, audit.JSON("source", "ui", "ip", ip))
|
||||
u.logger.Info("vault unsealed via UI", "ip", ip)
|
||||
|
||||
http.Redirect(w, r, "/login", http.StatusFound)
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package ui
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.wntrmute.dev/kyle/mcias/internal/token"
|
||||
@@ -16,5 +17,9 @@ func validateSessionToken(pubKey ed25519.PublicKey, tokenStr, issuer string) (*t
|
||||
|
||||
// issueToken is a convenience method for issuing a signed JWT.
|
||||
func (u *UIServer) issueToken(subject string, roles []string, expiry time.Duration) (string, *token.Claims, error) {
|
||||
return token.IssueToken(u.privKey, u.cfg.Tokens.Issuer, subject, roles, expiry)
|
||||
privKey, err := u.vault.PrivKey()
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("vault sealed: %w", err)
|
||||
}
|
||||
return token.IssueToken(privKey, u.cfg.Tokens.Issuer, subject, roles, expiry)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@ package ui
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/ed25519"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
@@ -24,6 +23,7 @@ import (
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -32,6 +32,7 @@ import (
|
||||
"git.wntrmute.dev/kyle/mcias/internal/db"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/middleware"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/model"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/vault"
|
||||
"git.wntrmute.dev/kyle/mcias/web"
|
||||
)
|
||||
|
||||
@@ -53,17 +54,31 @@ type pendingLogin struct {
|
||||
accountID int64
|
||||
}
|
||||
|
||||
// tokenDownload is a short-lived record that holds a just-issued service token
|
||||
// string so the operator can download it as a file. It is single-use and
|
||||
// expires after tokenDownloadTTL.
|
||||
//
|
||||
// Security: the token string is stored only for tokenDownloadTTL after
|
||||
// issuance. The nonce is random (128 bits) and single-use: it is deleted from
|
||||
// the map on first retrieval so it cannot be replayed.
|
||||
type tokenDownload struct {
|
||||
expiresAt time.Time
|
||||
token string
|
||||
accountID string // service account UUID (for the filename)
|
||||
}
|
||||
|
||||
const tokenDownloadTTL = 5 * time.Minute
|
||||
|
||||
// UIServer serves the HTMX-based management UI.
|
||||
type UIServer struct {
|
||||
pendingLogins sync.Map // nonce (string) → *pendingLogin
|
||||
tmpls map[string]*template.Template // page name → template set
|
||||
db *db.DB
|
||||
cfg *config.Config
|
||||
logger *slog.Logger
|
||||
csrf *CSRFManager
|
||||
pubKey ed25519.PublicKey
|
||||
privKey ed25519.PrivateKey
|
||||
masterKey []byte
|
||||
tmpls map[string]*template.Template // page name → template set
|
||||
db *db.DB
|
||||
cfg *config.Config
|
||||
logger *slog.Logger
|
||||
csrf *CSRFManager
|
||||
vault *vault.Vault
|
||||
pendingLogins sync.Map // nonce (string) → *pendingLogin
|
||||
tokenDownloads sync.Map // nonce (string) → *tokenDownload
|
||||
}
|
||||
|
||||
// issueTOTPNonce creates a random single-use nonce for the TOTP step and
|
||||
@@ -107,8 +122,12 @@ func (u *UIServer) dummyHash() string {
|
||||
|
||||
// New constructs a UIServer, parses all templates, and returns it.
|
||||
// Returns an error if template parsing fails.
|
||||
func New(database *db.DB, cfg *config.Config, priv ed25519.PrivateKey, pub ed25519.PublicKey, masterKey []byte, logger *slog.Logger) (*UIServer, error) {
|
||||
csrf := newCSRFManager(masterKey)
|
||||
//
|
||||
// The CSRFManager is created lazily from vault key material when the vault
|
||||
// is unsealed. When sealed, CSRF operations fail, but the sealed middleware
|
||||
// prevents reaching CSRF-protected routes (chicken-and-egg resolution).
|
||||
func New(database *db.DB, cfg *config.Config, v *vault.Vault, logger *slog.Logger) (*UIServer, error) {
|
||||
csrf := newCSRFManagerFromVault(v)
|
||||
|
||||
funcMap := template.FuncMap{
|
||||
"formatTime": func(t time.Time) string {
|
||||
@@ -193,6 +212,7 @@ func New(database *db.DB, cfg *config.Config, priv ed25519.PrivateKey, pub ed255
|
||||
"templates/fragments/policy_form.html",
|
||||
"templates/fragments/password_reset_form.html",
|
||||
"templates/fragments/password_change_form.html",
|
||||
"templates/fragments/token_delegates.html",
|
||||
}
|
||||
base, err := template.New("").Funcs(funcMap).ParseFS(web.TemplateFS, sharedFiles...)
|
||||
if err != nil {
|
||||
@@ -202,15 +222,17 @@ func New(database *db.DB, cfg *config.Config, priv ed25519.PrivateKey, pub ed255
|
||||
// Each page template defines "content" and "title" blocks; parsing them
|
||||
// into separate clones prevents the last-defined block from winning.
|
||||
pageFiles := map[string]string{
|
||||
"login": "templates/login.html",
|
||||
"dashboard": "templates/dashboard.html",
|
||||
"accounts": "templates/accounts.html",
|
||||
"account_detail": "templates/account_detail.html",
|
||||
"audit": "templates/audit.html",
|
||||
"audit_detail": "templates/audit_detail.html",
|
||||
"policies": "templates/policies.html",
|
||||
"pgcreds": "templates/pgcreds.html",
|
||||
"profile": "templates/profile.html",
|
||||
"login": "templates/login.html",
|
||||
"dashboard": "templates/dashboard.html",
|
||||
"accounts": "templates/accounts.html",
|
||||
"account_detail": "templates/account_detail.html",
|
||||
"audit": "templates/audit.html",
|
||||
"audit_detail": "templates/audit_detail.html",
|
||||
"policies": "templates/policies.html",
|
||||
"pgcreds": "templates/pgcreds.html",
|
||||
"profile": "templates/profile.html",
|
||||
"unseal": "templates/unseal.html",
|
||||
"service_accounts": "templates/service_accounts.html",
|
||||
}
|
||||
tmpls := make(map[string]*template.Template, len(pageFiles))
|
||||
for name, file := range pageFiles {
|
||||
@@ -225,14 +247,12 @@ func New(database *db.DB, cfg *config.Config, priv ed25519.PrivateKey, pub ed255
|
||||
}
|
||||
|
||||
srv := &UIServer{
|
||||
db: database,
|
||||
cfg: cfg,
|
||||
pubKey: pub,
|
||||
privKey: priv,
|
||||
masterKey: masterKey,
|
||||
logger: logger,
|
||||
csrf: csrf,
|
||||
tmpls: tmpls,
|
||||
db: database,
|
||||
cfg: cfg,
|
||||
vault: v,
|
||||
logger: logger,
|
||||
csrf: csrf,
|
||||
tmpls: tmpls,
|
||||
}
|
||||
|
||||
// Security (DEF-02): launch a background goroutine to evict expired TOTP
|
||||
@@ -240,6 +260,7 @@ func New(database *db.DB, cfg *config.Config, priv ed25519.PrivateKey, pub ed255
|
||||
// entries abandoned by users who never complete step 2 would otherwise
|
||||
// accumulate indefinitely, enabling a memory-exhaustion attack.
|
||||
go srv.cleanupPendingLogins()
|
||||
go srv.cleanupTokenDownloads()
|
||||
|
||||
return srv, nil
|
||||
}
|
||||
@@ -262,6 +283,56 @@ func (u *UIServer) cleanupPendingLogins() {
|
||||
}
|
||||
}
|
||||
|
||||
// storeTokenDownload saves a just-issued token string in the short-lived
|
||||
// download store and returns a random single-use nonce the caller can include
|
||||
// in the response. The download nonce expires after tokenDownloadTTL.
|
||||
func (u *UIServer) storeTokenDownload(tokenStr, accountID string) (string, error) {
|
||||
raw := make([]byte, 16)
|
||||
if _, err := rand.Read(raw); err != nil {
|
||||
return "", fmt.Errorf("ui: generate download nonce: %w", err)
|
||||
}
|
||||
nonce := hex.EncodeToString(raw)
|
||||
u.tokenDownloads.Store(nonce, &tokenDownload{
|
||||
token: tokenStr,
|
||||
accountID: accountID,
|
||||
expiresAt: time.Now().Add(tokenDownloadTTL),
|
||||
})
|
||||
return nonce, nil
|
||||
}
|
||||
|
||||
// consumeTokenDownload looks up, validates, and deletes the download nonce.
|
||||
// Returns the token string and account UUID, or ("", "", false) if the nonce
|
||||
// is unknown or expired.
|
||||
//
|
||||
// Security: single-use deletion prevents replay; expiry bounds the window.
|
||||
func (u *UIServer) consumeTokenDownload(nonce string) (tokenStr, accountID string, ok bool) {
|
||||
v, loaded := u.tokenDownloads.LoadAndDelete(nonce)
|
||||
if !loaded {
|
||||
return "", "", false
|
||||
}
|
||||
td, valid := v.(*tokenDownload)
|
||||
if !valid || time.Now().After(td.expiresAt) {
|
||||
return "", "", false
|
||||
}
|
||||
return td.token, td.accountID, true
|
||||
}
|
||||
|
||||
// cleanupTokenDownloads periodically evicts expired entries from tokenDownloads.
|
||||
func (u *UIServer) cleanupTokenDownloads() {
|
||||
ticker := time.NewTicker(5 * time.Minute)
|
||||
defer ticker.Stop()
|
||||
for range ticker.C {
|
||||
now := time.Now()
|
||||
u.tokenDownloads.Range(func(key, value any) bool {
|
||||
td, ok := value.(*tokenDownload)
|
||||
if !ok || now.After(td.expiresAt) {
|
||||
u.tokenDownloads.Delete(key)
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Register attaches all UI routes to mux, wrapped with security headers.
|
||||
// All UI responses (pages, fragments, redirects, static assets) carry the
|
||||
// headers added by securityHeaders.
|
||||
@@ -275,7 +346,10 @@ func (u *UIServer) Register(mux *http.ServeMux) {
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("ui: static sub-FS: %v", err))
|
||||
}
|
||||
uiMux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServerFS(staticSubFS)))
|
||||
// Security (SEC-07): wrap the file server to suppress directory listings.
|
||||
// Without this, GET /static/ returns an index of all static assets,
|
||||
// revealing framework details to an attacker.
|
||||
uiMux.Handle("GET /static/", http.StripPrefix("/static/", noDirListing(http.FileServerFS(staticSubFS))))
|
||||
|
||||
// Redirect root to login.
|
||||
uiMux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -295,6 +369,11 @@ func (u *UIServer) Register(mux *http.ServeMux) {
|
||||
}
|
||||
loginRateLimit := middleware.RateLimit(10, 10, trustedProxy)
|
||||
|
||||
// Vault unseal routes (no session required, no CSRF — vault is sealed).
|
||||
unsealRateLimit := middleware.RateLimit(3, 5, trustedProxy)
|
||||
uiMux.HandleFunc("GET /unseal", u.handleUnsealPage)
|
||||
uiMux.Handle("POST /unseal", unsealRateLimit(http.HandlerFunc(u.handleUnsealPost)))
|
||||
|
||||
// Auth routes (no session required).
|
||||
uiMux.HandleFunc("GET /login", u.handleLoginPage)
|
||||
uiMux.Handle("POST /login", loginRateLimit(http.HandlerFunc(u.handleLoginPost)))
|
||||
@@ -314,7 +393,7 @@ func (u *UIServer) Register(mux *http.ServeMux) {
|
||||
return authed(u.requireAdminRole(http.HandlerFunc(h)))
|
||||
}
|
||||
|
||||
uiMux.Handle("GET /dashboard", adminGet(u.handleDashboard))
|
||||
uiMux.Handle("GET /dashboard", authed(http.HandlerFunc(u.handleDashboard)))
|
||||
uiMux.Handle("GET /accounts", adminGet(u.handleAccountsList))
|
||||
uiMux.Handle("POST /accounts", admin(u.handleCreateAccount))
|
||||
uiMux.Handle("GET /accounts/{id}", adminGet(u.handleAccountDetail))
|
||||
@@ -323,7 +402,14 @@ func (u *UIServer) Register(mux *http.ServeMux) {
|
||||
uiMux.Handle("GET /accounts/{id}/roles/edit", adminGet(u.handleRolesEditForm))
|
||||
uiMux.Handle("PUT /accounts/{id}/roles", admin(u.handleSetRoles))
|
||||
uiMux.Handle("DELETE /token/{jti}", admin(u.handleRevokeToken))
|
||||
uiMux.Handle("POST /accounts/{id}/token", admin(u.handleIssueSystemToken))
|
||||
// Token issuance is accessible to both admins and delegates; the handler
|
||||
// enforces the admin-or-delegate check internally.
|
||||
uiMux.Handle("POST /accounts/{id}/token", authed(u.requireCSRF(http.HandlerFunc(u.handleIssueSystemToken))))
|
||||
// Token download uses a one-time nonce issued at token-issuance time.
|
||||
uiMux.Handle("GET /token/download/{nonce}", authed(http.HandlerFunc(u.handleDownloadToken)))
|
||||
// Token issue delegate management — admin only.
|
||||
uiMux.Handle("POST /accounts/{id}/token/delegates", admin(u.handleGrantTokenDelegate))
|
||||
uiMux.Handle("DELETE /accounts/{id}/token/delegates/{grantee}", admin(u.handleRevokeTokenDelegate))
|
||||
uiMux.Handle("PUT /accounts/{id}/pgcreds", admin(u.handleSetPGCreds))
|
||||
uiMux.Handle("POST /accounts/{id}/pgcreds/access", admin(u.handleGrantPGCredAccess))
|
||||
uiMux.Handle("DELETE /accounts/{id}/pgcreds/access/{grantee}", admin(u.handleRevokePGCredAccess))
|
||||
@@ -339,6 +425,10 @@ func (u *UIServer) Register(mux *http.ServeMux) {
|
||||
uiMux.Handle("PUT /accounts/{id}/tags", admin(u.handleSetAccountTags))
|
||||
uiMux.Handle("PUT /accounts/{id}/password", admin(u.handleAdminResetPassword))
|
||||
|
||||
// Service accounts page — accessible to any authenticated user; shows only
|
||||
// the service accounts for which the current user is a token-issue delegate.
|
||||
uiMux.Handle("GET /service-accounts", authed(http.HandlerFunc(u.handleServiceAccountsPage)))
|
||||
|
||||
// Profile routes — accessible to any authenticated user (not admin-only).
|
||||
uiMux.Handle("GET /profile", authed(http.HandlerFunc(u.handleProfilePage)))
|
||||
uiMux.Handle("PUT /profile/password", authed(u.requireCSRF(http.HandlerFunc(u.handleSelfChangePassword))))
|
||||
@@ -361,7 +451,12 @@ func (u *UIServer) requireCookieAuth(next http.Handler) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
claims, err := validateSessionToken(u.pubKey, cookie.Value, u.cfg.Tokens.Issuer)
|
||||
pubKey, err := u.vault.PubKey()
|
||||
if err != nil {
|
||||
u.redirectToLogin(w, r)
|
||||
return
|
||||
}
|
||||
claims, err := validateSessionToken(pubKey, cookie.Value, u.cfg.Tokens.Issuer)
|
||||
if err != nil {
|
||||
u.clearSessionCookie(w)
|
||||
u.redirectToLogin(w, r)
|
||||
@@ -530,6 +625,21 @@ func (u *UIServer) renderError(w http.ResponseWriter, r *http.Request, status in
|
||||
// Security: prevents memory exhaustion from oversized POST bodies (gosec G120).
|
||||
const maxFormBytes = 1 << 20
|
||||
|
||||
// noDirListing wraps an http.Handler (typically http.FileServerFS) to return
|
||||
// 404 for directory requests instead of an auto-generated directory index.
|
||||
//
|
||||
// Security (SEC-07): directory listings expose the names of all static assets,
|
||||
// leaking framework and version information to attackers.
|
||||
func noDirListing(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasSuffix(r.URL.Path, "/") || r.URL.Path == "" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// securityHeaders returns middleware that adds defensive HTTP headers to every
|
||||
// UI response.
|
||||
//
|
||||
@@ -545,6 +655,9 @@ const maxFormBytes = 1 << 20
|
||||
// requests to this origin for two years, preventing TLS-strip on revisit.
|
||||
// - Referrer-Policy: suppresses the Referer header on outbound navigations so
|
||||
// JWTs or session identifiers embedded in URLs are not leaked to third parties.
|
||||
// - Permissions-Policy: disables browser features (camera, microphone,
|
||||
// geolocation, payment) that this application does not use, reducing the
|
||||
// attack surface if a content-injection vulnerability is exploited.
|
||||
func securityHeaders(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
h := w.Header()
|
||||
@@ -554,6 +667,7 @@ func securityHeaders(next http.Handler) http.Handler {
|
||||
h.Set("X-Frame-Options", "DENY")
|
||||
h.Set("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
|
||||
h.Set("Referrer-Policy", "no-referrer")
|
||||
h.Set("Permissions-Policy", "camera=(), microphone=(), geolocation=(), payment=()")
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
@@ -569,6 +683,13 @@ func (u *UIServer) clientIP(r *http.Request) string {
|
||||
return middleware.ClientIP(r, proxyIP)
|
||||
}
|
||||
|
||||
// isAdmin reports whether the authenticated user holds the "admin" role.
|
||||
// Returns false if claims are absent.
|
||||
func isAdmin(r *http.Request) bool {
|
||||
claims := claimsFromContext(r.Context())
|
||||
return claims != nil && claims.HasRole("admin")
|
||||
}
|
||||
|
||||
// actorName resolves the username of the currently authenticated user from the
|
||||
// request context. Returns an empty string if claims are absent or the account
|
||||
// cannot be found; callers should treat an empty string as "not logged in".
|
||||
@@ -594,6 +715,10 @@ type PageData struct {
|
||||
// ActorName is the username of the currently logged-in user, populated by
|
||||
// handlers so the base template can display it in the navigation bar.
|
||||
ActorName string
|
||||
// IsAdmin is true when the logged-in user holds the "admin" role.
|
||||
// Used by the base template to conditionally render admin-only navigation
|
||||
// links (SEC-09: non-admin users must not see links they cannot access).
|
||||
IsAdmin bool
|
||||
}
|
||||
|
||||
// LoginData is the view model for the login page.
|
||||
@@ -633,11 +758,38 @@ type AccountDetailData struct {
|
||||
// ActorID is the DB id of the currently logged-in user; used in templates
|
||||
// to decide whether to show the owner-only management controls.
|
||||
ActorID *int64
|
||||
// TokenDelegates lists accounts that may issue tokens for this service account.
|
||||
// Only populated for system accounts when viewed by an admin.
|
||||
TokenDelegates []*model.ServiceAccountDelegate
|
||||
// DelegatableAccounts is the list of human accounts available for the
|
||||
// "add delegate" dropdown. Only populated for admins.
|
||||
DelegatableAccounts []*model.Account
|
||||
// DownloadNonce is a one-time nonce for downloading the just-issued token.
|
||||
// Populated by handleIssueSystemToken; empty otherwise.
|
||||
DownloadNonce string
|
||||
PageData
|
||||
Roles []string
|
||||
AllRoles []string
|
||||
Tags []string
|
||||
Tokens []*model.TokenRecord
|
||||
// CanIssueToken is true when the viewing actor may issue tokens for this
|
||||
// system account (admin role or explicit delegate grant).
|
||||
// Placed last to minimise GC scan area.
|
||||
CanIssueToken bool
|
||||
}
|
||||
|
||||
// ServiceAccountsData is the view model for the /service-accounts page.
|
||||
// It shows the system accounts for which the current user has delegate access,
|
||||
// plus the just-issued token download nonce (if a token was just issued).
|
||||
type ServiceAccountsData struct {
|
||||
// Accounts is the list of system accounts the actor may issue tokens for.
|
||||
Accounts []*model.Account
|
||||
// DownloadNonce is a one-time nonce for downloading the just-issued token.
|
||||
// Non-empty immediately after a successful token issuance.
|
||||
DownloadNonce string
|
||||
// IssuedFor is the UUID of the account whose token was just issued.
|
||||
IssuedFor string
|
||||
PageData
|
||||
}
|
||||
|
||||
// AuditData is the view model for the audit log page.
|
||||
|
||||
@@ -13,10 +13,11 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.wntrmute.dev/kyle/mcias/internal/auth"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/config"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/db"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/model"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/token"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/vault"
|
||||
)
|
||||
|
||||
const testIssuer = "https://auth.example.com"
|
||||
@@ -47,7 +48,8 @@ func newTestUIServer(t *testing.T) *UIServer {
|
||||
cfg := config.NewTestConfig(testIssuer)
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
|
||||
uiSrv, err := New(database, cfg, priv, pub, masterKey, logger)
|
||||
v := vault.NewUnsealed(masterKey, priv, pub)
|
||||
uiSrv, err := New(database, cfg, v, logger)
|
||||
if err != nil {
|
||||
t.Fatalf("new UIServer: %v", err)
|
||||
}
|
||||
@@ -79,6 +81,7 @@ func assertSecurityHeaders(t *testing.T, h http.Header, label string) {
|
||||
{"X-Frame-Options", "DENY"},
|
||||
{"Strict-Transport-Security", "max-age="},
|
||||
{"Referrer-Policy", "no-referrer"},
|
||||
{"Permissions-Policy", "camera=()"},
|
||||
}
|
||||
for _, c := range checks {
|
||||
val := h.Get(c.header)
|
||||
@@ -317,7 +320,7 @@ func issueAdminSession(t *testing.T, u *UIServer) (tokenStr, accountUUID string,
|
||||
if err := u.db.SetRoles(acct.ID, []string{"admin"}, nil); err != nil {
|
||||
t.Fatalf("SetRoles: %v", err)
|
||||
}
|
||||
tok, claims, err := token.IssueToken(u.privKey, testIssuer, acct.UUID, []string{"admin"}, time.Hour)
|
||||
tok, claims, err := u.issueToken(acct.UUID, []string{"admin"}, time.Hour)
|
||||
if err != nil {
|
||||
t.Fatalf("IssueToken: %v", err)
|
||||
}
|
||||
@@ -355,6 +358,34 @@ func authenticatedGET(t *testing.T, sessionToken string, path string) *http.Requ
|
||||
return req
|
||||
}
|
||||
|
||||
// TestStaticDirectoryListingDisabled verifies that GET /static/ returns 404
|
||||
// instead of a directory listing (SEC-07).
|
||||
func TestStaticDirectoryListingDisabled(t *testing.T) {
|
||||
mux := newTestMux(t)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/static/", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
mux.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusNotFound {
|
||||
t.Errorf("GET /static/ status = %d, want %d (directory listing must be disabled)", rr.Code, http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStaticFileStillServed verifies that individual static files are still
|
||||
// served normally after the directory listing fix (SEC-07).
|
||||
func TestStaticFileStillServed(t *testing.T) {
|
||||
mux := newTestMux(t)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/static/style.css", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
mux.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Errorf("GET /static/style.css status = %d, want %d", rr.Code, http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetPGCredsRejectsHumanAccount verifies that the PUT /accounts/{id}/pgcreds
|
||||
// endpoint returns 400 when the target account is a human (not system) account.
|
||||
func TestSetPGCredsRejectsHumanAccount(t *testing.T) {
|
||||
@@ -527,3 +558,195 @@ func TestAccountDetailShowsPGCredsSection(t *testing.T) {
|
||||
t.Error("human account detail page must not include pgcreds-section")
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoginLockedAccountShowsInvalidCredentials verifies that a locked-out
|
||||
// account gets the same "invalid credentials" error as a wrong-password
|
||||
// attempt in the UI login form, preventing user-enumeration via lockout
|
||||
// differentiation (SEC-02).
|
||||
func TestLoginLockedAccountShowsInvalidCredentials(t *testing.T) {
|
||||
u := newTestUIServer(t)
|
||||
|
||||
// Create an account with a known password.
|
||||
hash, err := auth.HashPassword("testpass123", auth.ArgonParams{Time: 3, Memory: 65536, Threads: 4})
|
||||
if err != nil {
|
||||
t.Fatalf("hash password: %v", err)
|
||||
}
|
||||
acct, err := u.db.CreateAccount("lockuiuser", model.AccountTypeHuman, hash)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateAccount: %v", err)
|
||||
}
|
||||
|
||||
// Lower the lockout threshold so we don't need 10 failures.
|
||||
origThreshold := db.LockoutThreshold
|
||||
db.LockoutThreshold = 3
|
||||
t.Cleanup(func() { db.LockoutThreshold = origThreshold })
|
||||
|
||||
for range db.LockoutThreshold {
|
||||
if err := u.db.RecordLoginFailure(acct.ID); err != nil {
|
||||
t.Fatalf("RecordLoginFailure: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
locked, err := u.db.IsLockedOut(acct.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("IsLockedOut: %v", err)
|
||||
}
|
||||
if !locked {
|
||||
t.Fatal("expected account to be locked out after threshold failures")
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
u.Register(mux)
|
||||
|
||||
// POST login for the locked account.
|
||||
form := url.Values{}
|
||||
form.Set("username", "lockuiuser")
|
||||
form.Set("password", "testpass123")
|
||||
req := httptest.NewRequest(http.MethodPost, "/login", strings.NewReader(form.Encode()))
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
lockedRR := httptest.NewRecorder()
|
||||
mux.ServeHTTP(lockedRR, req)
|
||||
|
||||
// POST login with wrong password for comparison.
|
||||
form2 := url.Values{}
|
||||
form2.Set("username", "lockuiuser")
|
||||
form2.Set("password", "wrongpassword")
|
||||
req2 := httptest.NewRequest(http.MethodPost, "/login", strings.NewReader(form2.Encode()))
|
||||
req2.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
wrongRR := httptest.NewRecorder()
|
||||
mux.ServeHTTP(wrongRR, req2)
|
||||
|
||||
lockedBody := lockedRR.Body.String()
|
||||
wrongBody := wrongRR.Body.String()
|
||||
|
||||
// Neither response should mention "locked" or "try again".
|
||||
if strings.Contains(lockedBody, "locked") || strings.Contains(lockedBody, "try again") {
|
||||
t.Error("locked account response leaks lockout state")
|
||||
}
|
||||
|
||||
// Both must contain "invalid credentials".
|
||||
if !strings.Contains(lockedBody, "invalid credentials") {
|
||||
t.Error("locked account response does not contain 'invalid credentials'")
|
||||
}
|
||||
if !strings.Contains(wrongBody, "invalid credentials") {
|
||||
t.Error("wrong password response does not contain 'invalid credentials'")
|
||||
}
|
||||
}
|
||||
|
||||
// ---- SEC-09: admin nav link visibility tests ----
|
||||
|
||||
// issueUserSession creates a human account with the "user" role (non-admin),
|
||||
// issues a JWT, tracks it, and returns the raw token string.
|
||||
func issueUserSession(t *testing.T, u *UIServer) string {
|
||||
t.Helper()
|
||||
acct, err := u.db.CreateAccount("regular-user", model.AccountTypeHuman, "")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateAccount: %v", err)
|
||||
}
|
||||
if err := u.db.SetRoles(acct.ID, []string{"user"}, nil); err != nil {
|
||||
t.Fatalf("SetRoles: %v", err)
|
||||
}
|
||||
tok, claims, err := u.issueToken(acct.UUID, []string{"user"}, time.Hour)
|
||||
if err != nil {
|
||||
t.Fatalf("IssueToken: %v", err)
|
||||
}
|
||||
if err := u.db.TrackToken(claims.JTI, acct.ID, claims.IssuedAt, claims.ExpiresAt); err != nil {
|
||||
t.Fatalf("TrackToken: %v", err)
|
||||
}
|
||||
return tok
|
||||
}
|
||||
|
||||
// TestNonAdminDashboardHidesAdminNavLinks verifies that a non-admin user's
|
||||
// dashboard does not contain links to admin-only pages (SEC-09).
|
||||
func TestNonAdminDashboardHidesAdminNavLinks(t *testing.T) {
|
||||
u := newTestUIServer(t)
|
||||
mux := http.NewServeMux()
|
||||
u.Register(mux)
|
||||
|
||||
userToken := issueUserSession(t, u)
|
||||
|
||||
req := authenticatedGET(t, userToken, "/dashboard")
|
||||
rr := httptest.NewRecorder()
|
||||
mux.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200; body: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
|
||||
body := rr.Body.String()
|
||||
for _, adminPath := range []string{
|
||||
`href="/accounts"`,
|
||||
`href="/audit"`,
|
||||
`href="/policies"`,
|
||||
`href="/pgcreds"`,
|
||||
} {
|
||||
if strings.Contains(body, adminPath) {
|
||||
t.Errorf("non-admin dashboard contains admin link %s — SEC-09 violation", adminPath)
|
||||
}
|
||||
}
|
||||
|
||||
// Dashboard link should still be present.
|
||||
if !strings.Contains(body, `href="/dashboard"`) {
|
||||
t.Error("dashboard link missing from non-admin nav")
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdminDashboardShowsAdminNavLinks verifies that an admin user's
|
||||
// dashboard contains all admin navigation links.
|
||||
func TestAdminDashboardShowsAdminNavLinks(t *testing.T) {
|
||||
u := newTestUIServer(t)
|
||||
mux := http.NewServeMux()
|
||||
u.Register(mux)
|
||||
|
||||
adminToken, _, _ := issueAdminSession(t, u)
|
||||
|
||||
req := authenticatedGET(t, adminToken, "/dashboard")
|
||||
rr := httptest.NewRecorder()
|
||||
mux.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200; body: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
|
||||
body := rr.Body.String()
|
||||
for _, adminPath := range []string{
|
||||
`href="/accounts"`,
|
||||
`href="/audit"`,
|
||||
`href="/policies"`,
|
||||
`href="/pgcreds"`,
|
||||
} {
|
||||
if !strings.Contains(body, adminPath) {
|
||||
t.Errorf("admin dashboard missing admin link %s", adminPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestNonAdminProfileHidesAdminNavLinks verifies that the profile page
|
||||
// also hides admin nav links for non-admin users (SEC-09).
|
||||
func TestNonAdminProfileHidesAdminNavLinks(t *testing.T) {
|
||||
u := newTestUIServer(t)
|
||||
mux := http.NewServeMux()
|
||||
u.Register(mux)
|
||||
|
||||
userToken := issueUserSession(t, u)
|
||||
|
||||
req := authenticatedGET(t, userToken, "/profile")
|
||||
rr := httptest.NewRecorder()
|
||||
mux.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200; body: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
|
||||
body := rr.Body.String()
|
||||
for _, adminPath := range []string{
|
||||
`href="/accounts"`,
|
||||
`href="/audit"`,
|
||||
`href="/policies"`,
|
||||
`href="/pgcreds"`,
|
||||
} {
|
||||
if strings.Contains(body, adminPath) {
|
||||
t.Errorf("non-admin profile page contains admin link %s — SEC-09 violation", adminPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,11 +45,22 @@ func Username(username string) error {
|
||||
// password.
|
||||
const MinPasswordLen = 12
|
||||
|
||||
// Password returns nil if the plaintext password meets the minimum length
|
||||
// requirement, or a descriptive error if not.
|
||||
// MaxPasswordLen is the maximum acceptable plaintext password length.
|
||||
//
|
||||
// Security (SEC-05): Argon2id processes the full password input. Without
|
||||
// an upper bound an attacker could submit a multi-megabyte password and
|
||||
// force expensive hashing. 128 characters is generous for any real
|
||||
// password or passphrase while capping the cost.
|
||||
const MaxPasswordLen = 128
|
||||
|
||||
// Password returns nil if the plaintext password meets the length
|
||||
// requirements, or a descriptive error if not.
|
||||
func Password(password string) error {
|
||||
if len(password) < MinPasswordLen {
|
||||
return fmt.Errorf("password must be at least %d characters", MinPasswordLen)
|
||||
}
|
||||
if len(password) > MaxPasswordLen {
|
||||
return fmt.Errorf("password must be at most %d characters", MaxPasswordLen)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -32,6 +32,17 @@ func TestPasswordTooShort(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPasswordTooLong(t *testing.T) {
|
||||
// Exactly MaxPasswordLen should be accepted.
|
||||
if err := Password(strings.Repeat("a", MaxPasswordLen)); err != nil {
|
||||
t.Errorf("Password(len=%d) = %v, want nil", MaxPasswordLen, err)
|
||||
}
|
||||
// One over the limit should be rejected.
|
||||
if err := Password(strings.Repeat("a", MaxPasswordLen+1)); err == nil {
|
||||
t.Errorf("Password(len=%d) = nil, want error", MaxPasswordLen+1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUsernameValid(t *testing.T) {
|
||||
valid := []string{
|
||||
"alice",
|
||||
|
||||
67
internal/vault/derive.go
Normal file
67
internal/vault/derive.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package vault
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"git.wntrmute.dev/kyle/mcias/internal/crypto"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/db"
|
||||
)
|
||||
|
||||
// DeriveFromPassphrase derives the master encryption key from a passphrase
|
||||
// using the Argon2id KDF with a salt stored in the database.
|
||||
//
|
||||
// Security: The Argon2id parameters used by crypto.DeriveKey exceed OWASP 2023
|
||||
// minimums (time=3, memory=128MiB, threads=4). The salt is 32 random bytes
|
||||
// stored in the database on first run.
|
||||
func DeriveFromPassphrase(passphrase string, database *db.DB) ([]byte, error) {
|
||||
salt, err := database.ReadMasterKeySalt()
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
return nil, fmt.Errorf("no master key salt in database (first-run requires startup passphrase)")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read master key salt: %w", err)
|
||||
}
|
||||
|
||||
key, err := crypto.DeriveKey(passphrase, salt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("derive master key: %w", err)
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
|
||||
// DecryptSigningKey decrypts the Ed25519 signing key pair from the database
|
||||
// using the provided master key.
|
||||
//
|
||||
// Security: The private key is stored AES-256-GCM encrypted in the database.
|
||||
// A fresh random nonce is used for each encryption. The plaintext key only
|
||||
// exists in memory during the process lifetime.
|
||||
func DecryptSigningKey(database *db.DB, masterKey []byte) (ed25519.PrivateKey, ed25519.PublicKey, error) {
|
||||
enc, nonce, err := database.ReadServerConfig()
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("read server config: %w", err)
|
||||
}
|
||||
if enc == nil || nonce == nil {
|
||||
return nil, nil, fmt.Errorf("no signing key in database (first-run requires startup passphrase)")
|
||||
}
|
||||
|
||||
privPEM, err := crypto.OpenAESGCM(masterKey, nonce, enc)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("decrypt signing key: %w", err)
|
||||
}
|
||||
|
||||
priv, err := crypto.ParsePrivateKeyPEM(privPEM)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("parse signing key PEM: %w", err)
|
||||
}
|
||||
|
||||
// Security: ed25519.PrivateKey.Public() always returns ed25519.PublicKey,
|
||||
// but we use the ok form to make the type assertion explicit and safe.
|
||||
pub, ok := priv.Public().(ed25519.PublicKey)
|
||||
if !ok {
|
||||
return nil, nil, fmt.Errorf("signing key has unexpected public key type")
|
||||
}
|
||||
|
||||
return priv, pub, nil
|
||||
}
|
||||
127
internal/vault/vault.go
Normal file
127
internal/vault/vault.go
Normal file
@@ -0,0 +1,127 @@
|
||||
// Package vault provides a thread-safe container for the server's
|
||||
// cryptographic key material with seal/unseal lifecycle management.
|
||||
//
|
||||
// Security design:
|
||||
// - The Vault holds the master encryption key and Ed25519 signing key pair.
|
||||
// - All accessors return ErrSealed when the vault is sealed, ensuring that
|
||||
// callers cannot use key material that has been zeroed.
|
||||
// - Seal() explicitly zeroes all key material before nilling the slices,
|
||||
// reducing the window in which secrets remain in memory after seal.
|
||||
// - All state transitions are protected by sync.RWMutex. Readers (IsSealed,
|
||||
// MasterKey, PrivKey, PubKey) take a read lock; writers (Seal, Unseal)
|
||||
// take a write lock.
|
||||
package vault
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"errors"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// ErrSealed is returned by accessor methods when the vault is sealed.
|
||||
var ErrSealed = errors.New("vault is sealed")
|
||||
|
||||
// Vault holds the server's cryptographic key material behind a mutex.
|
||||
// All three servers (REST, UI, gRPC) share a single Vault by pointer.
|
||||
type Vault struct {
|
||||
mu sync.RWMutex
|
||||
masterKey []byte
|
||||
privKey ed25519.PrivateKey
|
||||
pubKey ed25519.PublicKey
|
||||
sealed bool
|
||||
}
|
||||
|
||||
// NewSealed creates a Vault in the sealed state. No key material is held.
|
||||
func NewSealed() *Vault {
|
||||
return &Vault{sealed: true}
|
||||
}
|
||||
|
||||
// NewUnsealed creates a Vault in the unsealed state with the given key material.
|
||||
// This is the backward-compatible path used when the passphrase is available at
|
||||
// startup.
|
||||
func NewUnsealed(masterKey []byte, privKey ed25519.PrivateKey, pubKey ed25519.PublicKey) *Vault {
|
||||
return &Vault{
|
||||
masterKey: masterKey,
|
||||
privKey: privKey,
|
||||
pubKey: pubKey,
|
||||
sealed: false,
|
||||
}
|
||||
}
|
||||
|
||||
// IsSealed reports whether the vault is currently sealed.
|
||||
func (v *Vault) IsSealed() bool {
|
||||
v.mu.RLock()
|
||||
defer v.mu.RUnlock()
|
||||
return v.sealed
|
||||
}
|
||||
|
||||
// MasterKey returns the master encryption key, or ErrSealed if sealed.
|
||||
func (v *Vault) MasterKey() ([]byte, error) {
|
||||
v.mu.RLock()
|
||||
defer v.mu.RUnlock()
|
||||
if v.sealed {
|
||||
return nil, ErrSealed
|
||||
}
|
||||
return v.masterKey, nil
|
||||
}
|
||||
|
||||
// PrivKey returns the Ed25519 private signing key, or ErrSealed if sealed.
|
||||
func (v *Vault) PrivKey() (ed25519.PrivateKey, error) {
|
||||
v.mu.RLock()
|
||||
defer v.mu.RUnlock()
|
||||
if v.sealed {
|
||||
return nil, ErrSealed
|
||||
}
|
||||
return v.privKey, nil
|
||||
}
|
||||
|
||||
// PubKey returns the Ed25519 public key, or ErrSealed if sealed.
|
||||
func (v *Vault) PubKey() (ed25519.PublicKey, error) {
|
||||
v.mu.RLock()
|
||||
defer v.mu.RUnlock()
|
||||
if v.sealed {
|
||||
return nil, ErrSealed
|
||||
}
|
||||
return v.pubKey, nil
|
||||
}
|
||||
|
||||
// Unseal transitions the vault from sealed to unsealed, storing the provided
|
||||
// key material. Returns an error if the vault is already unsealed.
|
||||
func (v *Vault) Unseal(masterKey []byte, privKey ed25519.PrivateKey, pubKey ed25519.PublicKey) error {
|
||||
v.mu.Lock()
|
||||
defer v.mu.Unlock()
|
||||
if !v.sealed {
|
||||
return errors.New("vault is already unsealed")
|
||||
}
|
||||
v.masterKey = masterKey
|
||||
v.privKey = privKey
|
||||
v.pubKey = pubKey
|
||||
v.sealed = false
|
||||
return nil
|
||||
}
|
||||
|
||||
// Seal transitions the vault from unsealed to sealed. All key material is
|
||||
// zeroed before being released to minimize the window of memory exposure.
|
||||
//
|
||||
// Security: explicit zeroing loops ensure the key bytes are overwritten even
|
||||
// if the garbage collector has not yet reclaimed the backing arrays.
|
||||
func (v *Vault) Seal() {
|
||||
v.mu.Lock()
|
||||
defer v.mu.Unlock()
|
||||
// Zero master key.
|
||||
for i := range v.masterKey {
|
||||
v.masterKey[i] = 0
|
||||
}
|
||||
v.masterKey = nil
|
||||
// Zero private key.
|
||||
for i := range v.privKey {
|
||||
v.privKey[i] = 0
|
||||
}
|
||||
v.privKey = nil
|
||||
// Zero public key (not secret, but consistent cleanup).
|
||||
for i := range v.pubKey {
|
||||
v.pubKey[i] = 0
|
||||
}
|
||||
v.pubKey = nil
|
||||
v.sealed = true
|
||||
}
|
||||
149
internal/vault/vault_test.go
Normal file
149
internal/vault/vault_test.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package vault
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"crypto/rand"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func generateTestKeys(t *testing.T) ([]byte, ed25519.PrivateKey, ed25519.PublicKey) {
|
||||
t.Helper()
|
||||
pub, priv, err := ed25519.GenerateKey(rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatalf("generate key: %v", err)
|
||||
}
|
||||
mk := make([]byte, 32)
|
||||
if _, err := rand.Read(mk); err != nil {
|
||||
t.Fatalf("generate master key: %v", err)
|
||||
}
|
||||
return mk, priv, pub
|
||||
}
|
||||
|
||||
func TestNewSealed(t *testing.T) {
|
||||
v := NewSealed()
|
||||
if !v.IsSealed() {
|
||||
t.Fatal("NewSealed() should be sealed")
|
||||
}
|
||||
if _, err := v.MasterKey(); err != ErrSealed {
|
||||
t.Fatalf("MasterKey() error = %v, want ErrSealed", err)
|
||||
}
|
||||
if _, err := v.PrivKey(); err != ErrSealed {
|
||||
t.Fatalf("PrivKey() error = %v, want ErrSealed", err)
|
||||
}
|
||||
if _, err := v.PubKey(); err != ErrSealed {
|
||||
t.Fatalf("PubKey() error = %v, want ErrSealed", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewUnsealed(t *testing.T) {
|
||||
mk, priv, pub := generateTestKeys(t)
|
||||
v := NewUnsealed(mk, priv, pub)
|
||||
if v.IsSealed() {
|
||||
t.Fatal("NewUnsealed() should not be sealed")
|
||||
}
|
||||
gotMK, err := v.MasterKey()
|
||||
if err != nil {
|
||||
t.Fatalf("MasterKey() error = %v", err)
|
||||
}
|
||||
if len(gotMK) != 32 {
|
||||
t.Fatalf("MasterKey() len = %d, want 32", len(gotMK))
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnsealFromSealed(t *testing.T) {
|
||||
mk, priv, pub := generateTestKeys(t)
|
||||
v := NewSealed()
|
||||
if err := v.Unseal(mk, priv, pub); err != nil {
|
||||
t.Fatalf("Unseal() error = %v", err)
|
||||
}
|
||||
if v.IsSealed() {
|
||||
t.Fatal("should be unsealed after Unseal()")
|
||||
}
|
||||
gotPriv, err := v.PrivKey()
|
||||
if err != nil {
|
||||
t.Fatalf("PrivKey() error = %v", err)
|
||||
}
|
||||
if !priv.Equal(gotPriv) {
|
||||
t.Fatal("PrivKey() mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnsealAlreadyUnsealed(t *testing.T) {
|
||||
mk, priv, pub := generateTestKeys(t)
|
||||
v := NewUnsealed(mk, priv, pub)
|
||||
if err := v.Unseal(mk, priv, pub); err == nil {
|
||||
t.Fatal("Unseal() on unsealed vault should return error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSealZeroesKeys(t *testing.T) {
|
||||
mk, priv, pub := generateTestKeys(t)
|
||||
// Keep references to the backing arrays so we can verify zeroing.
|
||||
mkRef := mk
|
||||
privRef := priv
|
||||
v := NewUnsealed(mk, priv, pub)
|
||||
v.Seal()
|
||||
|
||||
if !v.IsSealed() {
|
||||
t.Fatal("should be sealed after Seal()")
|
||||
}
|
||||
// Verify the original backing arrays were zeroed.
|
||||
for i, b := range mkRef {
|
||||
if b != 0 {
|
||||
t.Fatalf("masterKey[%d] = %d, want 0", i, b)
|
||||
}
|
||||
}
|
||||
for i, b := range privRef {
|
||||
if b != 0 {
|
||||
t.Fatalf("privKey[%d] = %d, want 0", i, b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSealUnsealCycle(t *testing.T) {
|
||||
mk, priv, pub := generateTestKeys(t)
|
||||
v := NewUnsealed(mk, priv, pub)
|
||||
v.Seal()
|
||||
|
||||
mk2, priv2, pub2 := generateTestKeys(t)
|
||||
if err := v.Unseal(mk2, priv2, pub2); err != nil {
|
||||
t.Fatalf("Unseal() after Seal() error = %v", err)
|
||||
}
|
||||
gotPub, err := v.PubKey()
|
||||
if err != nil {
|
||||
t.Fatalf("PubKey() error = %v", err)
|
||||
}
|
||||
if !pub2.Equal(gotPub) {
|
||||
t.Fatal("PubKey() mismatch after re-unseal")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConcurrentAccess(t *testing.T) {
|
||||
mk, priv, pub := generateTestKeys(t)
|
||||
v := NewUnsealed(mk, priv, pub)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
// Concurrent readers.
|
||||
for range 50 {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
_ = v.IsSealed()
|
||||
_, _ = v.MasterKey()
|
||||
_, _ = v.PrivKey()
|
||||
_, _ = v.PubKey()
|
||||
}()
|
||||
}
|
||||
// Concurrent seal/unseal cycles.
|
||||
for range 10 {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
v.Seal()
|
||||
mk2, priv2, pub2 := generateTestKeys(t)
|
||||
_ = v.Unseal(mk2, priv2, pub2)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
@@ -77,7 +77,7 @@ WAL mode and foreign key enforcement are enabled automatically.
|
||||
Issuer claim embedded in every JWT.
|
||||
Use the base URL of your MCIAS server.
|
||||
.It Sy default_expiry
|
||||
.Pq optional, default 720h
|
||||
.Pq optional, default 168h
|
||||
Token expiry for interactive logins.
|
||||
Go duration string.
|
||||
.It Sy admin_expiry
|
||||
|
||||
226
openapi.yaml
226
openapi.yaml
@@ -221,8 +221,8 @@ components:
|
||||
nullable: true
|
||||
description: |
|
||||
Time after which the rule is no longer active. NULL means no
|
||||
constraint (never expires). Rules where `expires_at <= now()` are
|
||||
skipped during evaluation.
|
||||
constraint (never expires). Rules where expires_at is in the past
|
||||
are skipped during evaluation.
|
||||
example: "2026-06-01T00:00:00Z"
|
||||
created_at:
|
||||
type: string
|
||||
@@ -307,6 +307,18 @@ components:
|
||||
error: rate limit exceeded
|
||||
code: rate_limited
|
||||
|
||||
VaultSealed:
|
||||
description: |
|
||||
The vault is sealed. The server is running but has no key material.
|
||||
Unseal via `POST /v1/vault/unseal` before retrying.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
example:
|
||||
error: vault is sealed
|
||||
code: vault_sealed
|
||||
|
||||
paths:
|
||||
|
||||
# ── Public ────────────────────────────────────────────────────────────────
|
||||
@@ -314,12 +326,17 @@ paths:
|
||||
/v1/health:
|
||||
get:
|
||||
summary: Health check
|
||||
description: Returns `{"status":"ok"}` if the server is running. No auth required.
|
||||
description: |
|
||||
Returns server health status. Always returns HTTP 200, even when the
|
||||
vault is sealed. No auth required.
|
||||
|
||||
When the vault is sealed, `status` is `"sealed"` and most other
|
||||
endpoints return 503. When healthy, `status` is `"ok"`.
|
||||
operationId: getHealth
|
||||
tags: [Public]
|
||||
responses:
|
||||
"200":
|
||||
description: Server is healthy.
|
||||
description: Server is running (check `status` for sealed state).
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
@@ -327,6 +344,7 @@ paths:
|
||||
properties:
|
||||
status:
|
||||
type: string
|
||||
enum: [ok, sealed]
|
||||
example: ok
|
||||
|
||||
/v1/keys/public:
|
||||
@@ -369,6 +387,121 @@ paths:
|
||||
description: Base64url-encoded public key bytes.
|
||||
example: 11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo
|
||||
|
||||
/v1/vault/status:
|
||||
get:
|
||||
summary: Vault seal status
|
||||
description: |
|
||||
Returns whether the vault is currently sealed. Always accessible,
|
||||
even when sealed. No auth required.
|
||||
|
||||
Clients should poll this after startup or after a 503 `vault_sealed`
|
||||
response to determine when to attempt an unseal.
|
||||
operationId: getVaultStatus
|
||||
tags: [Public]
|
||||
responses:
|
||||
"200":
|
||||
description: Current vault seal state.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [sealed]
|
||||
properties:
|
||||
sealed:
|
||||
type: boolean
|
||||
example: false
|
||||
|
||||
/v1/vault/unseal:
|
||||
post:
|
||||
summary: Unseal the vault
|
||||
description: |
|
||||
Provide the master passphrase to derive the encryption key, decrypt
|
||||
the Ed25519 signing key, and unseal the vault. Once unsealed, all
|
||||
other endpoints become available.
|
||||
|
||||
Rate limited to 3 requests per second per IP (burst 5) to limit
|
||||
brute-force attempts against the passphrase.
|
||||
|
||||
The passphrase is never logged. A generic `"unseal failed"` error
|
||||
is returned for any failure (wrong passphrase, vault already unsealed
|
||||
mid-flight, etc.) to avoid leaking information.
|
||||
operationId: unsealVault
|
||||
tags: [Public]
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [passphrase]
|
||||
properties:
|
||||
passphrase:
|
||||
type: string
|
||||
description: Master passphrase used to derive the encryption key.
|
||||
example: correct-horse-battery-staple
|
||||
responses:
|
||||
"200":
|
||||
description: Vault unsealed (or was already unsealed).
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
status:
|
||||
type: string
|
||||
enum: [unsealed, already unsealed]
|
||||
example: unsealed
|
||||
"400":
|
||||
$ref: "#/components/responses/BadRequest"
|
||||
"401":
|
||||
description: Wrong passphrase or key decryption failure.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
example:
|
||||
error: unseal failed
|
||||
code: unauthorized
|
||||
"429":
|
||||
$ref: "#/components/responses/RateLimited"
|
||||
|
||||
/v1/vault/seal:
|
||||
post:
|
||||
summary: Seal the vault (admin)
|
||||
description: |
|
||||
Zero all key material in memory and transition the server to the
|
||||
sealed state. After this call:
|
||||
|
||||
- All subsequent requests (except health, vault status, and unseal)
|
||||
return 503 `vault_sealed`.
|
||||
- The caller's own JWT is immediately invalidated because the public
|
||||
key needed to verify it is no longer held in memory.
|
||||
- The server can be unsealed again via `POST /v1/vault/unseal`.
|
||||
|
||||
This is an emergency operation. Use it to protect key material if a
|
||||
compromise is suspected. It does **not** restart the server or wipe
|
||||
the database.
|
||||
operationId: sealVault
|
||||
tags: [Admin — Vault]
|
||||
security:
|
||||
- bearerAuth: []
|
||||
responses:
|
||||
"200":
|
||||
description: Vault sealed (or was already sealed).
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
status:
|
||||
type: string
|
||||
enum: [sealed, already sealed]
|
||||
example: sealed
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
"403":
|
||||
$ref: "#/components/responses/Forbidden"
|
||||
|
||||
/v1/auth/login:
|
||||
post:
|
||||
summary: Login
|
||||
@@ -473,6 +606,10 @@ paths:
|
||||
format: uuid
|
||||
description: Subject (account UUID). Present when valid=true.
|
||||
example: 550e8400-e29b-41d4-a716-446655440000
|
||||
username:
|
||||
type: string
|
||||
description: Account username. Present when valid=true and the account exists.
|
||||
example: alice
|
||||
roles:
|
||||
type: array
|
||||
items:
|
||||
@@ -486,7 +623,7 @@ paths:
|
||||
example: "2026-04-10T12:34:56Z"
|
||||
examples:
|
||||
valid:
|
||||
value: {valid: true, sub: "550e8400-...", roles: [editor], expires_at: "2026-04-10T12:34:56Z"}
|
||||
value: {valid: true, sub: "550e8400-...", username: alice, roles: [editor], expires_at: "2026-04-10T12:34:56Z"}
|
||||
invalid:
|
||||
value: {valid: false}
|
||||
"429":
|
||||
@@ -550,6 +687,17 @@ paths:
|
||||
tags: [Auth]
|
||||
security:
|
||||
- bearerAuth: []
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [password]
|
||||
properties:
|
||||
password:
|
||||
type: string
|
||||
description: Current account password (required to prevent session-theft escalation).
|
||||
responses:
|
||||
"200":
|
||||
description: TOTP secret generated.
|
||||
@@ -1123,6 +1271,70 @@ paths:
|
||||
"404":
|
||||
$ref: "#/components/responses/NotFound"
|
||||
|
||||
/v1/pgcreds:
|
||||
get:
|
||||
summary: List accessible Postgres credentials
|
||||
description: |
|
||||
Return all Postgres credentials accessible to the authenticated account:
|
||||
credentials owned by the account plus any explicitly granted by an admin.
|
||||
|
||||
The `id` field is the credential record ID; use it together with the
|
||||
`service_account_id` to fetch full details via
|
||||
`GET /v1/accounts/{id}/pgcreds`. Passwords are **not** returned by this
|
||||
endpoint.
|
||||
operationId: listAccessiblePGCreds
|
||||
tags: [Admin — Credentials]
|
||||
security:
|
||||
- bearerAuth: []
|
||||
responses:
|
||||
"200":
|
||||
description: Array of accessible Postgres credential summaries.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
type: object
|
||||
required: [id, service_account_id, host, port, database, username, created_at, updated_at]
|
||||
properties:
|
||||
id:
|
||||
type: integer
|
||||
description: Credential record ID.
|
||||
example: 7
|
||||
service_account_id:
|
||||
type: string
|
||||
format: uuid
|
||||
description: UUID of the system account that owns these credentials.
|
||||
example: 550e8400-e29b-41d4-a716-446655440000
|
||||
service_account_name:
|
||||
type: string
|
||||
description: Username of the owning system account (omitted if unavailable).
|
||||
example: payments-api
|
||||
host:
|
||||
type: string
|
||||
example: db.example.com
|
||||
port:
|
||||
type: integer
|
||||
example: 5432
|
||||
database:
|
||||
type: string
|
||||
example: mydb
|
||||
username:
|
||||
type: string
|
||||
example: myuser
|
||||
created_at:
|
||||
type: string
|
||||
format: date-time
|
||||
example: "2026-03-11T09:00:00Z"
|
||||
updated_at:
|
||||
type: string
|
||||
format: date-time
|
||||
example: "2026-03-11T09:00:00Z"
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
"503":
|
||||
$ref: "#/components/responses/VaultSealed"
|
||||
|
||||
/v1/audit:
|
||||
get:
|
||||
summary: Query audit log (admin)
|
||||
@@ -1137,7 +1349,7 @@ paths:
|
||||
`pgcred_accessed`, `pgcred_updated`, `pgcred_access_granted`,
|
||||
`pgcred_access_revoked`, `tag_added`, `tag_removed`,
|
||||
`policy_rule_created`, `policy_rule_updated`, `policy_rule_deleted`,
|
||||
`policy_deny`.
|
||||
`policy_deny`, `vault_sealed`, `vault_unsealed`.
|
||||
operationId: listAudit
|
||||
tags: [Admin — Audit]
|
||||
security:
|
||||
@@ -1519,3 +1731,5 @@ tags:
|
||||
description: Requires admin role.
|
||||
- name: Admin — Policy
|
||||
description: Requires admin role. Manage policy rules and account tags.
|
||||
- name: Admin — Vault
|
||||
description: Requires admin role. Emergency vault seal operation.
|
||||
|
||||
@@ -45,8 +45,12 @@ message RenewTokenResponse {
|
||||
|
||||
// --- TOTP enrollment ---
|
||||
|
||||
// EnrollTOTPRequest carries no body; the acting account is from the JWT.
|
||||
message EnrollTOTPRequest {}
|
||||
// EnrollTOTPRequest carries the current password for re-authentication.
|
||||
// Security (SEC-01): password is required to prevent a stolen session token
|
||||
// from being used to enroll attacker-controlled TOTP on the victim's account.
|
||||
message EnrollTOTPRequest {
|
||||
string password = 1; // security: current password required; never logged
|
||||
}
|
||||
|
||||
// EnrollTOTPResponse returns the TOTP secret and otpauth URI for display.
|
||||
// Security: the secret is shown once; it is stored only in encrypted form.
|
||||
|
||||
@@ -36,6 +36,7 @@ import (
|
||||
"git.wntrmute.dev/kyle/mcias/internal/model"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/server"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/token"
|
||||
"git.wntrmute.dev/kyle/mcias/internal/vault"
|
||||
)
|
||||
|
||||
const e2eIssuer = "https://auth.e2e.test"
|
||||
@@ -73,7 +74,8 @@ func newTestEnv(t *testing.T) *testEnv {
|
||||
|
||||
cfg := config.NewTestConfig(e2eIssuer)
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
srv := server.New(database, cfg, priv, pub, masterKey, logger)
|
||||
v := vault.NewUnsealed(masterKey, priv, pub)
|
||||
srv := server.New(database, cfg, v, logger)
|
||||
|
||||
ts := httptest.NewServer(srv.Handler())
|
||||
t.Cleanup(func() {
|
||||
@@ -223,19 +225,22 @@ func TestE2ELoginLogoutFlow(t *testing.T) {
|
||||
// TestE2ETokenRenewal verifies that renewal returns a new token and revokes the old one.
|
||||
func TestE2ETokenRenewal(t *testing.T) {
|
||||
e := newTestEnv(t)
|
||||
e.createAccount(t, "bob")
|
||||
acct := e.createAccount(t, "bob")
|
||||
|
||||
// Login.
|
||||
resp := e.do(t, "POST", "/v1/auth/login", map[string]string{
|
||||
"username": "bob",
|
||||
"password": "testpass123",
|
||||
}, "")
|
||||
mustStatus(t, resp, http.StatusOK)
|
||||
var lr struct {
|
||||
Token string `json:"token"`
|
||||
// Issue a short-lived token (10s) directly so we can wait past the 50%
|
||||
// renewal threshold (SEC-03) without blocking the test for minutes.
|
||||
// 10s gives ample headroom: we sleep 6s (>50%), leaving 4s for the HTTP
|
||||
// round-trip before expiry — eliminating the race that plagued the 2s token.
|
||||
oldToken, claims, err := token.IssueToken(e.privKey, e2eIssuer, acct.UUID, nil, 10*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("IssueToken: %v", err)
|
||||
}
|
||||
decodeJSON(t, resp, &lr)
|
||||
oldToken := lr.Token
|
||||
if err := e.db.TrackToken(claims.JTI, acct.ID, claims.IssuedAt, claims.ExpiresAt); err != nil {
|
||||
t.Fatalf("TrackToken: %v", err)
|
||||
}
|
||||
|
||||
// Wait for >50% of the 10s lifetime to elapse.
|
||||
time.Sleep(6 * time.Second)
|
||||
|
||||
// Renew.
|
||||
resp2 := e.do(t, "POST", "/v1/auth/renew", nil, oldToken)
|
||||
|
||||
@@ -118,6 +118,121 @@ components:
|
||||
description: JSON blob with event-specific metadata. Never contains credentials.
|
||||
example: '{"jti":"f47ac10b-..."}'
|
||||
|
||||
TagsResponse:
|
||||
type: object
|
||||
required: [tags]
|
||||
properties:
|
||||
tags:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
description: Current tag list for the account.
|
||||
example: ["env:production", "svc:payments-api"]
|
||||
|
||||
RuleBody:
|
||||
type: object
|
||||
required: [effect]
|
||||
description: |
|
||||
The match conditions and effect of a policy rule. All fields except
|
||||
`effect` are optional; an omitted field acts as a wildcard.
|
||||
properties:
|
||||
effect:
|
||||
type: string
|
||||
enum: [allow, deny]
|
||||
example: allow
|
||||
roles:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
description: Subject must have at least one of these roles.
|
||||
example: ["svc:payments-api"]
|
||||
account_types:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
enum: [human, system]
|
||||
description: Subject account type must be one of these.
|
||||
example: ["system"]
|
||||
subject_uuid:
|
||||
type: string
|
||||
format: uuid
|
||||
description: Match only this specific subject UUID.
|
||||
example: 550e8400-e29b-41d4-a716-446655440000
|
||||
actions:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
description: |
|
||||
One of the defined action constants, e.g. `pgcreds:read`,
|
||||
`accounts:list`. Subject action must be in this list.
|
||||
example: ["pgcreds:read"]
|
||||
resource_type:
|
||||
type: string
|
||||
description: Resource type the rule applies to.
|
||||
example: pgcreds
|
||||
owner_matches_subject:
|
||||
type: boolean
|
||||
description: Resource owner UUID must equal the subject UUID.
|
||||
example: true
|
||||
service_names:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
description: Resource service name must be one of these.
|
||||
example: ["payments-api"]
|
||||
required_tags:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
description: Resource must have ALL of these tags.
|
||||
example: ["env:staging"]
|
||||
|
||||
PolicyRule:
|
||||
type: object
|
||||
required: [id, priority, description, rule, enabled, created_at, updated_at]
|
||||
properties:
|
||||
id:
|
||||
type: integer
|
||||
example: 1
|
||||
priority:
|
||||
type: integer
|
||||
description: Lower number = evaluated first.
|
||||
example: 100
|
||||
description:
|
||||
type: string
|
||||
example: Allow payments-api to read its own pgcreds
|
||||
rule:
|
||||
$ref: "#/components/schemas/RuleBody"
|
||||
enabled:
|
||||
type: boolean
|
||||
example: true
|
||||
not_before:
|
||||
type: string
|
||||
format: date-time
|
||||
nullable: true
|
||||
description: |
|
||||
Earliest time the rule becomes active. NULL means no constraint
|
||||
(always active). Rules where `not_before > now()` are skipped
|
||||
during evaluation.
|
||||
example: "2026-04-01T00:00:00Z"
|
||||
expires_at:
|
||||
type: string
|
||||
format: date-time
|
||||
nullable: true
|
||||
description: |
|
||||
Time after which the rule is no longer active. NULL means no
|
||||
constraint (never expires). Rules where expires_at is in the past
|
||||
are skipped during evaluation.
|
||||
example: "2026-06-01T00:00:00Z"
|
||||
created_at:
|
||||
type: string
|
||||
format: date-time
|
||||
example: "2026-03-11T09:00:00Z"
|
||||
updated_at:
|
||||
type: string
|
||||
format: date-time
|
||||
example: "2026-03-11T09:00:00Z"
|
||||
|
||||
PGCreds:
|
||||
type: object
|
||||
required: [host, port, database, username, password]
|
||||
@@ -192,6 +307,18 @@ components:
|
||||
error: rate limit exceeded
|
||||
code: rate_limited
|
||||
|
||||
VaultSealed:
|
||||
description: |
|
||||
The vault is sealed. The server is running but has no key material.
|
||||
Unseal via `POST /v1/vault/unseal` before retrying.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
example:
|
||||
error: vault is sealed
|
||||
code: vault_sealed
|
||||
|
||||
paths:
|
||||
|
||||
# ── Public ────────────────────────────────────────────────────────────────
|
||||
@@ -199,12 +326,17 @@ paths:
|
||||
/v1/health:
|
||||
get:
|
||||
summary: Health check
|
||||
description: Returns `{"status":"ok"}` if the server is running. No auth required.
|
||||
description: |
|
||||
Returns server health status. Always returns HTTP 200, even when the
|
||||
vault is sealed. No auth required.
|
||||
|
||||
When the vault is sealed, `status` is `"sealed"` and most other
|
||||
endpoints return 503. When healthy, `status` is `"ok"`.
|
||||
operationId: getHealth
|
||||
tags: [Public]
|
||||
responses:
|
||||
"200":
|
||||
description: Server is healthy.
|
||||
description: Server is running (check `status` for sealed state).
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
@@ -212,6 +344,7 @@ paths:
|
||||
properties:
|
||||
status:
|
||||
type: string
|
||||
enum: [ok, sealed]
|
||||
example: ok
|
||||
|
||||
/v1/keys/public:
|
||||
@@ -254,6 +387,121 @@ paths:
|
||||
description: Base64url-encoded public key bytes.
|
||||
example: 11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo
|
||||
|
||||
/v1/vault/status:
|
||||
get:
|
||||
summary: Vault seal status
|
||||
description: |
|
||||
Returns whether the vault is currently sealed. Always accessible,
|
||||
even when sealed. No auth required.
|
||||
|
||||
Clients should poll this after startup or after a 503 `vault_sealed`
|
||||
response to determine when to attempt an unseal.
|
||||
operationId: getVaultStatus
|
||||
tags: [Public]
|
||||
responses:
|
||||
"200":
|
||||
description: Current vault seal state.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [sealed]
|
||||
properties:
|
||||
sealed:
|
||||
type: boolean
|
||||
example: false
|
||||
|
||||
/v1/vault/unseal:
|
||||
post:
|
||||
summary: Unseal the vault
|
||||
description: |
|
||||
Provide the master passphrase to derive the encryption key, decrypt
|
||||
the Ed25519 signing key, and unseal the vault. Once unsealed, all
|
||||
other endpoints become available.
|
||||
|
||||
Rate limited to 3 requests per second per IP (burst 5) to limit
|
||||
brute-force attempts against the passphrase.
|
||||
|
||||
The passphrase is never logged. A generic `"unseal failed"` error
|
||||
is returned for any failure (wrong passphrase, vault already unsealed
|
||||
mid-flight, etc.) to avoid leaking information.
|
||||
operationId: unsealVault
|
||||
tags: [Public]
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [passphrase]
|
||||
properties:
|
||||
passphrase:
|
||||
type: string
|
||||
description: Master passphrase used to derive the encryption key.
|
||||
example: correct-horse-battery-staple
|
||||
responses:
|
||||
"200":
|
||||
description: Vault unsealed (or was already unsealed).
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
status:
|
||||
type: string
|
||||
enum: [unsealed, already unsealed]
|
||||
example: unsealed
|
||||
"400":
|
||||
$ref: "#/components/responses/BadRequest"
|
||||
"401":
|
||||
description: Wrong passphrase or key decryption failure.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
example:
|
||||
error: unseal failed
|
||||
code: unauthorized
|
||||
"429":
|
||||
$ref: "#/components/responses/RateLimited"
|
||||
|
||||
/v1/vault/seal:
|
||||
post:
|
||||
summary: Seal the vault (admin)
|
||||
description: |
|
||||
Zero all key material in memory and transition the server to the
|
||||
sealed state. After this call:
|
||||
|
||||
- All subsequent requests (except health, vault status, and unseal)
|
||||
return 503 `vault_sealed`.
|
||||
- The caller's own JWT is immediately invalidated because the public
|
||||
key needed to verify it is no longer held in memory.
|
||||
- The server can be unsealed again via `POST /v1/vault/unseal`.
|
||||
|
||||
This is an emergency operation. Use it to protect key material if a
|
||||
compromise is suspected. It does **not** restart the server or wipe
|
||||
the database.
|
||||
operationId: sealVault
|
||||
tags: [Admin — Vault]
|
||||
security:
|
||||
- bearerAuth: []
|
||||
responses:
|
||||
"200":
|
||||
description: Vault sealed (or was already sealed).
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
status:
|
||||
type: string
|
||||
enum: [sealed, already sealed]
|
||||
example: sealed
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
"403":
|
||||
$ref: "#/components/responses/Forbidden"
|
||||
|
||||
/v1/auth/login:
|
||||
post:
|
||||
summary: Login
|
||||
@@ -358,6 +606,10 @@ paths:
|
||||
format: uuid
|
||||
description: Subject (account UUID). Present when valid=true.
|
||||
example: 550e8400-e29b-41d4-a716-446655440000
|
||||
username:
|
||||
type: string
|
||||
description: Account username. Present when valid=true and the account exists.
|
||||
example: alice
|
||||
roles:
|
||||
type: array
|
||||
items:
|
||||
@@ -371,7 +623,7 @@ paths:
|
||||
example: "2026-04-10T12:34:56Z"
|
||||
examples:
|
||||
valid:
|
||||
value: {valid: true, sub: "550e8400-...", roles: [editor], expires_at: "2026-04-10T12:34:56Z"}
|
||||
value: {valid: true, sub: "550e8400-...", username: alice, roles: [editor], expires_at: "2026-04-10T12:34:56Z"}
|
||||
invalid:
|
||||
value: {valid: false}
|
||||
"429":
|
||||
@@ -435,6 +687,17 @@ paths:
|
||||
tags: [Auth]
|
||||
security:
|
||||
- bearerAuth: []
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [password]
|
||||
properties:
|
||||
password:
|
||||
type: string
|
||||
description: Current account password (required to prevent session-theft escalation).
|
||||
responses:
|
||||
"200":
|
||||
description: TOTP secret generated.
|
||||
@@ -485,6 +748,68 @@ paths:
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
|
||||
/v1/auth/password:
|
||||
put:
|
||||
summary: Change own password (self-service)
|
||||
description: |
|
||||
Change the password of the currently authenticated human account.
|
||||
The caller must supply the correct `current_password` to prevent
|
||||
token-theft attacks: possession of a valid JWT alone is not sufficient.
|
||||
|
||||
On success:
|
||||
- The stored Argon2id hash is replaced with the new password hash.
|
||||
- All active sessions *except* the caller's current token are revoked.
|
||||
- The lockout failure counter is cleared.
|
||||
|
||||
On failure (wrong current password):
|
||||
- A login failure is recorded against the account, subject to the
|
||||
same lockout rules as `POST /v1/auth/login`.
|
||||
|
||||
Only applies to human accounts. System accounts have no password.
|
||||
operationId: changePassword
|
||||
tags: [Auth]
|
||||
security:
|
||||
- bearerAuth: []
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [current_password, new_password]
|
||||
properties:
|
||||
current_password:
|
||||
type: string
|
||||
description: The account's current password (required for verification).
|
||||
example: old-s3cr3t
|
||||
new_password:
|
||||
type: string
|
||||
description: The new password. Minimum 12 characters.
|
||||
example: new-s3cr3t-long
|
||||
responses:
|
||||
"204":
|
||||
description: Password changed. Other active sessions revoked.
|
||||
"400":
|
||||
$ref: "#/components/responses/BadRequest"
|
||||
"401":
|
||||
description: Current password is incorrect.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
example:
|
||||
error: current password is incorrect
|
||||
code: unauthorized
|
||||
"429":
|
||||
description: Account temporarily locked due to too many failed attempts.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
example:
|
||||
error: account temporarily locked
|
||||
code: account_locked
|
||||
|
||||
# ── Admin ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/v1/auth/totp:
|
||||
@@ -818,6 +1143,76 @@ paths:
|
||||
"404":
|
||||
$ref: "#/components/responses/NotFound"
|
||||
|
||||
post:
|
||||
summary: Grant a role to an account (admin)
|
||||
description: |
|
||||
Add a single role to an account's role set. If the role already exists,
|
||||
this is a no-op. Roles take effect in the **next** token issued or
|
||||
renewed; existing tokens continue to carry the roles embedded at
|
||||
issuance time.
|
||||
operationId: grantRole
|
||||
tags: [Admin — Accounts]
|
||||
security:
|
||||
- bearerAuth: []
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [role]
|
||||
properties:
|
||||
role:
|
||||
type: string
|
||||
example: editor
|
||||
responses:
|
||||
"204":
|
||||
description: Role granted.
|
||||
"400":
|
||||
$ref: "#/components/responses/BadRequest"
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
"403":
|
||||
$ref: "#/components/responses/Forbidden"
|
||||
"404":
|
||||
$ref: "#/components/responses/NotFound"
|
||||
|
||||
/v1/accounts/{id}/roles/{role}:
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
example: 550e8400-e29b-41d4-a716-446655440000
|
||||
- name: role
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
example: editor
|
||||
|
||||
delete:
|
||||
summary: Revoke a role from an account (admin)
|
||||
description: |
|
||||
Remove a single role from an account's role set. Roles take effect in
|
||||
the **next** token issued or renewed; existing tokens continue to carry
|
||||
the roles embedded at issuance time.
|
||||
operationId: revokeRole
|
||||
tags: [Admin — Accounts]
|
||||
security:
|
||||
- bearerAuth: []
|
||||
responses:
|
||||
"204":
|
||||
description: Role revoked.
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
"403":
|
||||
$ref: "#/components/responses/Forbidden"
|
||||
"404":
|
||||
$ref: "#/components/responses/NotFound"
|
||||
|
||||
/v1/accounts/{id}/pgcreds:
|
||||
parameters:
|
||||
- name: id
|
||||
@@ -876,6 +1271,70 @@ paths:
|
||||
"404":
|
||||
$ref: "#/components/responses/NotFound"
|
||||
|
||||
/v1/pgcreds:
|
||||
get:
|
||||
summary: List accessible Postgres credentials
|
||||
description: |
|
||||
Return all Postgres credentials accessible to the authenticated account:
|
||||
credentials owned by the account plus any explicitly granted by an admin.
|
||||
|
||||
The `id` field is the credential record ID; use it together with the
|
||||
`service_account_id` to fetch full details via
|
||||
`GET /v1/accounts/{id}/pgcreds`. Passwords are **not** returned by this
|
||||
endpoint.
|
||||
operationId: listAccessiblePGCreds
|
||||
tags: [Admin — Credentials]
|
||||
security:
|
||||
- bearerAuth: []
|
||||
responses:
|
||||
"200":
|
||||
description: Array of accessible Postgres credential summaries.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
type: object
|
||||
required: [id, service_account_id, host, port, database, username, created_at, updated_at]
|
||||
properties:
|
||||
id:
|
||||
type: integer
|
||||
description: Credential record ID.
|
||||
example: 7
|
||||
service_account_id:
|
||||
type: string
|
||||
format: uuid
|
||||
description: UUID of the system account that owns these credentials.
|
||||
example: 550e8400-e29b-41d4-a716-446655440000
|
||||
service_account_name:
|
||||
type: string
|
||||
description: Username of the owning system account (omitted if unavailable).
|
||||
example: payments-api
|
||||
host:
|
||||
type: string
|
||||
example: db.example.com
|
||||
port:
|
||||
type: integer
|
||||
example: 5432
|
||||
database:
|
||||
type: string
|
||||
example: mydb
|
||||
username:
|
||||
type: string
|
||||
example: myuser
|
||||
created_at:
|
||||
type: string
|
||||
format: date-time
|
||||
example: "2026-03-11T09:00:00Z"
|
||||
updated_at:
|
||||
type: string
|
||||
format: date-time
|
||||
example: "2026-03-11T09:00:00Z"
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
"503":
|
||||
$ref: "#/components/responses/VaultSealed"
|
||||
|
||||
/v1/audit:
|
||||
get:
|
||||
summary: Query audit log (admin)
|
||||
@@ -887,7 +1346,10 @@ paths:
|
||||
`token_issued`, `token_renewed`, `token_revoked`, `token_expired`,
|
||||
`account_created`, `account_updated`, `account_deleted`,
|
||||
`role_granted`, `role_revoked`, `totp_enrolled`, `totp_removed`,
|
||||
`pgcred_accessed`, `pgcred_updated`.
|
||||
`pgcred_accessed`, `pgcred_updated`, `pgcred_access_granted`,
|
||||
`pgcred_access_revoked`, `tag_added`, `tag_removed`,
|
||||
`policy_rule_created`, `policy_rule_updated`, `policy_rule_deleted`,
|
||||
`policy_deny`, `vault_sealed`, `vault_unsealed`.
|
||||
operationId: listAudit
|
||||
tags: [Admin — Audit]
|
||||
security:
|
||||
@@ -948,6 +1410,310 @@ paths:
|
||||
"403":
|
||||
$ref: "#/components/responses/Forbidden"
|
||||
|
||||
/v1/accounts/{id}/tags:
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
example: 550e8400-e29b-41d4-a716-446655440000
|
||||
|
||||
get:
|
||||
summary: Get account tags (admin)
|
||||
description: |
|
||||
Return the current tag set for an account. Tags are used by the policy
|
||||
engine for machine/service gating (e.g. `env:production`,
|
||||
`svc:payments-api`).
|
||||
operationId: getAccountTags
|
||||
tags: [Admin — Policy]
|
||||
security:
|
||||
- bearerAuth: []
|
||||
responses:
|
||||
"200":
|
||||
description: Tag list.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/TagsResponse"
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
"403":
|
||||
$ref: "#/components/responses/Forbidden"
|
||||
"404":
|
||||
$ref: "#/components/responses/NotFound"
|
||||
|
||||
put:
|
||||
summary: Set account tags (admin)
|
||||
description: |
|
||||
Replace the account's full tag set atomically. Pass an empty array to
|
||||
clear all tags. Changes take effect immediately for new policy
|
||||
evaluations; no token renewal is required.
|
||||
operationId: setAccountTags
|
||||
tags: [Admin — Policy]
|
||||
security:
|
||||
- bearerAuth: []
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [tags]
|
||||
properties:
|
||||
tags:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
example: ["env:production", "svc:payments-api"]
|
||||
responses:
|
||||
"200":
|
||||
description: Updated tag list.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/TagsResponse"
|
||||
"400":
|
||||
$ref: "#/components/responses/BadRequest"
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
"403":
|
||||
$ref: "#/components/responses/Forbidden"
|
||||
"404":
|
||||
$ref: "#/components/responses/NotFound"
|
||||
|
||||
/v1/accounts/{id}/password:
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
example: 550e8400-e29b-41d4-a716-446655440000
|
||||
|
||||
put:
|
||||
summary: Admin password reset (admin)
|
||||
description: |
|
||||
Reset the password for a human account without requiring the current
|
||||
password. This is intended for account recovery (e.g. a user forgot
|
||||
their password).
|
||||
|
||||
On success:
|
||||
- The stored Argon2id hash is replaced with the new password hash.
|
||||
- All active sessions for the target account are revoked.
|
||||
|
||||
Only applies to human accounts. The new password must be at least
|
||||
12 characters.
|
||||
operationId: adminSetPassword
|
||||
tags: [Admin — Accounts]
|
||||
security:
|
||||
- bearerAuth: []
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [new_password]
|
||||
properties:
|
||||
new_password:
|
||||
type: string
|
||||
description: The new password. Minimum 12 characters.
|
||||
example: new-s3cr3t-long
|
||||
responses:
|
||||
"204":
|
||||
description: Password reset. All active sessions for the account revoked.
|
||||
"400":
|
||||
$ref: "#/components/responses/BadRequest"
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
"403":
|
||||
$ref: "#/components/responses/Forbidden"
|
||||
"404":
|
||||
$ref: "#/components/responses/NotFound"
|
||||
|
||||
/v1/policy/rules:
|
||||
get:
|
||||
summary: List policy rules (admin)
|
||||
description: |
|
||||
Return all operator-defined policy rules ordered by priority (ascending).
|
||||
Built-in default rules (IDs -1 to -7) are not included.
|
||||
operationId: listPolicyRules
|
||||
tags: [Admin — Policy]
|
||||
security:
|
||||
- bearerAuth: []
|
||||
responses:
|
||||
"200":
|
||||
description: Array of policy rules.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
$ref: "#/components/schemas/PolicyRule"
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
"403":
|
||||
$ref: "#/components/responses/Forbidden"
|
||||
|
||||
post:
|
||||
summary: Create policy rule (admin)
|
||||
description: |
|
||||
Create a new operator policy rule. Rules are evaluated in priority order
|
||||
(lower number = evaluated first, default 100). Deny-wins: if any matching
|
||||
rule has effect `deny`, access is denied regardless of allow rules.
|
||||
operationId: createPolicyRule
|
||||
tags: [Admin — Policy]
|
||||
security:
|
||||
- bearerAuth: []
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [description, rule]
|
||||
properties:
|
||||
description:
|
||||
type: string
|
||||
example: Allow payments-api to read its own pgcreds
|
||||
priority:
|
||||
type: integer
|
||||
description: Evaluation priority. Lower = first. Default 100.
|
||||
example: 50
|
||||
rule:
|
||||
$ref: "#/components/schemas/RuleBody"
|
||||
not_before:
|
||||
type: string
|
||||
format: date-time
|
||||
description: Earliest activation time (RFC3339, optional).
|
||||
example: "2026-04-01T00:00:00Z"
|
||||
expires_at:
|
||||
type: string
|
||||
format: date-time
|
||||
description: Expiry time (RFC3339, optional).
|
||||
example: "2026-06-01T00:00:00Z"
|
||||
responses:
|
||||
"201":
|
||||
description: Rule created.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/PolicyRule"
|
||||
"400":
|
||||
$ref: "#/components/responses/BadRequest"
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
"403":
|
||||
$ref: "#/components/responses/Forbidden"
|
||||
|
||||
/v1/policy/rules/{id}:
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: integer
|
||||
example: 1
|
||||
|
||||
get:
|
||||
summary: Get policy rule (admin)
|
||||
operationId: getPolicyRule
|
||||
tags: [Admin — Policy]
|
||||
security:
|
||||
- bearerAuth: []
|
||||
responses:
|
||||
"200":
|
||||
description: Policy rule.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/PolicyRule"
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
"403":
|
||||
$ref: "#/components/responses/Forbidden"
|
||||
"404":
|
||||
$ref: "#/components/responses/NotFound"
|
||||
|
||||
patch:
|
||||
summary: Update policy rule (admin)
|
||||
description: |
|
||||
Update one or more fields of an existing policy rule. All fields are
|
||||
optional; omitted fields are left unchanged.
|
||||
operationId: updatePolicyRule
|
||||
tags: [Admin — Policy]
|
||||
security:
|
||||
- bearerAuth: []
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
description:
|
||||
type: string
|
||||
example: Updated description
|
||||
priority:
|
||||
type: integer
|
||||
example: 75
|
||||
enabled:
|
||||
type: boolean
|
||||
example: false
|
||||
rule:
|
||||
$ref: "#/components/schemas/RuleBody"
|
||||
not_before:
|
||||
type: string
|
||||
format: date-time
|
||||
description: Set earliest activation time (RFC3339).
|
||||
example: "2026-04-01T00:00:00Z"
|
||||
expires_at:
|
||||
type: string
|
||||
format: date-time
|
||||
description: Set expiry time (RFC3339).
|
||||
example: "2026-06-01T00:00:00Z"
|
||||
clear_not_before:
|
||||
type: boolean
|
||||
description: Set to true to remove not_before constraint.
|
||||
clear_expires_at:
|
||||
type: boolean
|
||||
description: Set to true to remove expires_at constraint.
|
||||
responses:
|
||||
"200":
|
||||
description: Updated rule.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/PolicyRule"
|
||||
"400":
|
||||
$ref: "#/components/responses/BadRequest"
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
"403":
|
||||
$ref: "#/components/responses/Forbidden"
|
||||
"404":
|
||||
$ref: "#/components/responses/NotFound"
|
||||
|
||||
delete:
|
||||
summary: Delete policy rule (admin)
|
||||
description: Permanently delete a policy rule. This action cannot be undone.
|
||||
operationId: deletePolicyRule
|
||||
tags: [Admin — Policy]
|
||||
security:
|
||||
- bearerAuth: []
|
||||
responses:
|
||||
"204":
|
||||
description: Rule deleted.
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
"403":
|
||||
$ref: "#/components/responses/Forbidden"
|
||||
"404":
|
||||
$ref: "#/components/responses/NotFound"
|
||||
|
||||
tags:
|
||||
- name: Public
|
||||
description: No authentication required.
|
||||
@@ -963,3 +1729,7 @@ tags:
|
||||
description: Requires admin role.
|
||||
- name: Admin — Audit
|
||||
description: Requires admin role.
|
||||
- name: Admin — Policy
|
||||
description: Requires admin role. Manage policy rules and account tags.
|
||||
- name: Admin — Vault
|
||||
description: Requires admin role. Emergency vault seal operation.
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
<div class="card">
|
||||
<div class="d-flex align-center justify-between" style="margin-bottom:1rem">
|
||||
<h2 style="font-size:1rem;font-weight:600">Tokens</h2>
|
||||
{{if eq (string .Account.AccountType) "system"}}
|
||||
{{if and (eq (string .Account.AccountType) "system") .CanIssueToken}}
|
||||
<button class="btn btn-sm btn-secondary"
|
||||
hx-post="/accounts/{{.Account.UUID}}/token"
|
||||
hx-target="#token-list" hx-swap="outerHTML">Issue Token</button>
|
||||
@@ -39,6 +39,10 @@
|
||||
<h2 style="font-size:1rem;font-weight:600;margin-bottom:1rem">Postgres Credentials</h2>
|
||||
{{template "pgcreds_form" .}}
|
||||
</div>
|
||||
<div class="card">
|
||||
<h2 style="font-size:1rem;font-weight:600;margin-bottom:1rem">Token Issue Access</h2>
|
||||
<div id="token-delegates-section">{{template "token_delegates" .}}</div>
|
||||
</div>
|
||||
{{end}}
|
||||
<div class="card">
|
||||
<h2 style="font-size:1rem;font-weight:600;margin-bottom:1rem">Tags</h2>
|
||||
|
||||
@@ -12,10 +12,10 @@
|
||||
<span class="nav-brand">MCIAS</span>
|
||||
<ul class="nav-links">
|
||||
<li><a href="/dashboard">Dashboard</a></li>
|
||||
<li><a href="/accounts">Accounts</a></li>
|
||||
{{if .IsAdmin}}<li><a href="/accounts">Accounts</a></li>
|
||||
<li><a href="/audit">Audit</a></li>
|
||||
<li><a href="/policies">Policies</a></li>
|
||||
<li><a href="/pgcreds">PG Creds</a></li>
|
||||
<li><a href="/pgcreds">PG Creds</a></li>{{else}}<li><a href="/service-accounts">Service Accounts</a></li>{{end}}
|
||||
{{if .ActorName}}<li><a href="/profile">{{.ActorName}}</a></li>{{end}}
|
||||
<li><form method="POST" action="/logout" style="margin:0"><button class="btn btn-sm btn-secondary" type="submit">Logout</button></form></li>
|
||||
</ul>
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
<div class="page-header">
|
||||
<h1>Dashboard</h1>
|
||||
</div>
|
||||
{{if .IsAdmin}}
|
||||
<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(200px,1fr));gap:1rem;margin-bottom:1.5rem">
|
||||
<div class="card" style="text-align:center">
|
||||
<div style="font-size:2rem;font-weight:700;color:#2563eb">{{.TotalAccounts}}</div>
|
||||
@@ -33,4 +34,9 @@
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
{{else}}
|
||||
<div class="card">
|
||||
<p>Welcome, <strong>{{.ActorName}}</strong>. Use the navigation above to access your profile and credentials.</p>
|
||||
</div>
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
47
web/templates/fragments/token_delegates.html
Normal file
47
web/templates/fragments/token_delegates.html
Normal file
@@ -0,0 +1,47 @@
|
||||
{{define "token_delegates"}}
|
||||
<div id="token-delegates-section">
|
||||
<h3 style="font-size:.9rem;font-weight:600;margin-bottom:.5rem">Token Issue Delegates</h3>
|
||||
<p class="text-muted text-small" style="margin-bottom:.75rem">
|
||||
Delegates can issue and rotate tokens for this service account without holding the admin role.
|
||||
</p>
|
||||
{{if .TokenDelegates}}
|
||||
<table class="table table-sm" style="font-size:.85rem;margin-bottom:.75rem">
|
||||
<thead>
|
||||
<tr><th>Account</th><th>Granted</th><th></th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range .TokenDelegates}}
|
||||
<tr>
|
||||
<td>{{.GranteeName}}</td>
|
||||
<td class="text-small text-muted">{{formatTime .GrantedAt}}</td>
|
||||
<td>
|
||||
<button class="btn btn-sm btn-danger"
|
||||
hx-delete="/accounts/{{$.Account.UUID}}/token/delegates/{{.GranteeUUID}}"
|
||||
hx-target="#token-delegates-section" hx-swap="outerHTML"
|
||||
hx-confirm="Remove delegate access for {{.GranteeName}}?">Remove</button>
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
{{else}}
|
||||
<p class="text-muted text-small" style="margin-bottom:.75rem">No delegates.</p>
|
||||
{{end}}
|
||||
|
||||
{{if .DelegatableAccounts}}
|
||||
<form hx-post="/accounts/{{.Account.UUID}}/token/delegates"
|
||||
hx-target="#token-delegates-section" hx-swap="outerHTML"
|
||||
style="display:flex;gap:.5rem;align-items:center">
|
||||
<select class="form-control" name="grantee_uuid" required style="flex:1">
|
||||
<option value="">— select account to add as delegate —</option>
|
||||
{{range .DelegatableAccounts}}
|
||||
{{if eq (string .AccountType) "human"}}
|
||||
<option value="{{.UUID}}">{{.Username}}</option>
|
||||
{{end}}
|
||||
{{end}}
|
||||
</select>
|
||||
<button class="btn btn-sm btn-secondary" type="submit">Add Delegate</button>
|
||||
</form>
|
||||
{{end}}
|
||||
</div>
|
||||
{{end}}
|
||||
@@ -1,5 +1,16 @@
|
||||
{{define "token_list"}}
|
||||
<div id="token-list">
|
||||
{{if .Flash}}
|
||||
<div class="alert alert-success" role="alert" style="margin-bottom:1rem">
|
||||
{{.Flash}}
|
||||
{{if .DownloadNonce}}
|
||||
<div style="margin-top:.5rem">
|
||||
<a class="btn btn-sm btn-secondary"
|
||||
href="/token/download/{{.DownloadNonce}}">Download token as file</a>
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
{{end}}
|
||||
{{if .Tokens}}
|
||||
<div class="table-wrapper">
|
||||
<table>
|
||||
|
||||
@@ -11,10 +11,10 @@
|
||||
<div class="login-box">
|
||||
<div class="brand-heading">MCIAS</div>
|
||||
<div class="brand-subtitle">Metacircular Identity & Access System</div>
|
||||
<div class="card">
|
||||
<div class="card" id="login-card">
|
||||
{{if .Error}}<div class="alert alert-error" role="alert">{{.Error}}</div>{{end}}
|
||||
<form id="login-form" method="POST" action="/login"
|
||||
hx-post="/login" hx-target="#login-form" hx-swap="outerHTML">
|
||||
hx-post="/login" hx-target="#login-card" hx-swap="outerHTML" hx-select="#login-card">
|
||||
<div class="form-group">
|
||||
<label for="username">Username</label>
|
||||
<input class="form-control" type="text" id="username" name="username"
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
{{range .Creds}}
|
||||
<div style="border:1px solid var(--color-border);border-radius:6px;padding:1rem;margin-bottom:1rem">
|
||||
<dl style="display:grid;grid-template-columns:140px 1fr;gap:.35rem .75rem;font-size:.9rem;margin-bottom:.75rem">
|
||||
<dt class="text-muted">Credential ID</dt><dd><code style="font-size:.8rem;color:var(--color-fg-muted)">{{.ID}}</code></dd>
|
||||
<dt class="text-muted">Service Account</dt><dd>{{.ServiceUsername}}</dd>
|
||||
<dt class="text-muted">Host</dt><dd>{{.PGHost}}:{{.PGPort}}</dd>
|
||||
<dt class="text-muted">Database</dt><dd>{{.PGDatabase}}</dd>
|
||||
|
||||
47
web/templates/service_accounts.html
Normal file
47
web/templates/service_accounts.html
Normal file
@@ -0,0 +1,47 @@
|
||||
{{define "service_accounts"}}{{template "base" .}}{{end}}
|
||||
{{define "title"}}Service Accounts — MCIAS{{end}}
|
||||
{{define "content"}}
|
||||
<div class="page-header">
|
||||
<h1>Service Accounts</h1>
|
||||
<p class="text-muted text-small">Service accounts for which you have been granted token-issue access.</p>
|
||||
</div>
|
||||
{{if .DownloadNonce}}
|
||||
<div class="alert alert-success" role="alert" style="margin-bottom:1rem">
|
||||
Token issued.
|
||||
<a class="btn btn-sm btn-secondary" style="margin-left:.5rem"
|
||||
href="/token/download/{{.DownloadNonce}}">Download token as file</a>
|
||||
</div>
|
||||
{{end}}
|
||||
{{if .Accounts}}
|
||||
<div class="card">
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>Name</th><th>Status</th><th>Action</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range .Accounts}}
|
||||
<tr>
|
||||
<td>{{.Username}}</td>
|
||||
<td><span class="badge badge-{{string .Status}}">{{string .Status}}</span></td>
|
||||
<td>
|
||||
<button class="btn btn-sm btn-secondary"
|
||||
hx-post="/accounts/{{.UUID}}/token"
|
||||
hx-target="#issue-result-{{.UUID}}"
|
||||
hx-swap="outerHTML">Issue Token</button>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="3">
|
||||
<div id="issue-result-{{.UUID}}"></div>
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{{else}}
|
||||
<div class="card">
|
||||
<p class="text-muted text-small">You have not been granted access to any service accounts.</p>
|
||||
</div>
|
||||
{{end}}
|
||||
{{end}}
|
||||
31
web/templates/unseal.html
Normal file
31
web/templates/unseal.html
Normal file
@@ -0,0 +1,31 @@
|
||||
{{define "unseal"}}<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>Unseal Vault — MCIAS</title>
|
||||
<link rel="stylesheet" href="/static/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="login-wrapper">
|
||||
<div class="login-box">
|
||||
<div class="brand-heading">MCIAS</div>
|
||||
<div class="brand-subtitle">Vault is Sealed</div>
|
||||
<div class="card">
|
||||
{{if .Error}}<div class="alert alert-error" role="alert">{{.Error}}</div>{{end}}
|
||||
<form id="unseal-form" method="POST" action="/unseal">
|
||||
<div class="form-group">
|
||||
<label for="passphrase">Master Passphrase</label>
|
||||
<input class="form-control" type="password" id="passphrase" name="passphrase"
|
||||
autocomplete="off" required autofocus>
|
||||
</div>
|
||||
<div class="form-actions">
|
||||
<button class="btn btn-primary" type="submit" style="width:100%">Unseal</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
||||
Reference in New Issue
Block a user