Some checks failed
Build Pipeline / Build Binaries (push) Failing after 3m39s
Build Pipeline / Build Docker Images (push) Has been skipped
Build Pipeline / Sign HIPAA Config (push) Has been skipped
Build Pipeline / Generate SLSA Provenance (push) Has been skipped
Checkout test / test (push) Successful in 6s
CI Pipeline / Test (ubuntu-latest on self-hosted) (push) Failing after 1s
CI Pipeline / Dev Compose Smoke Test (push) Has been skipped
CI Pipeline / Security Scan (push) Has been skipped
CI Pipeline / Test Scripts (push) Has been skipped
CI Pipeline / Test Native Libraries (push) Has been skipped
CI Pipeline / Native Library Build Matrix (push) Has been skipped
Contract Tests / Spec Drift Detection (push) Failing after 11s
Contract Tests / API Contract Tests (push) Has been skipped
Deploy API Docs / Build API Documentation (push) Failing after 5s
Deploy API Docs / Deploy to GitHub Pages (push) Has been skipped
Documentation / build-and-publish (push) Failing after 40s
Test Matrix / test-native-vs-pure (cgo) (push) Failing after 14s
Test Matrix / test-native-vs-pure (native) (push) Failing after 35s
Test Matrix / test-native-vs-pure (pure) (push) Failing after 18s
CI Pipeline / Trigger Build Workflow (push) Failing after 1s
Build CLI with Embedded SQLite / build (arm64, aarch64-linux) (push) Has been cancelled
Build CLI with Embedded SQLite / build (x86_64, x86_64-linux) (push) Has been cancelled
Build CLI with Embedded SQLite / build-macos (arm64) (push) Has been cancelled
Build CLI with Embedded SQLite / build-macos (x86_64) (push) Has been cancelled
Security Scan / Security Analysis (push) Has been cancelled
Security Scan / Native Library Security (push) Has been cancelled
Verification & Maintenance / V.1 - Schema Drift Detection (push) Has been cancelled
Verification & Maintenance / V.4 - Custom Go Vet Analyzers (push) Has been cancelled
Verification & Maintenance / V.7 - Audit Chain Integrity (push) Has been cancelled
Verification & Maintenance / V.6 - Extended Security Scanning (push) Has been cancelled
Verification & Maintenance / V.10 - OpenSSF Scorecard (push) Has been cancelled
Verification & Maintenance / Verification Summary (push) Has been cancelled
- Introduce audit, plugin, and scheduler API handlers - Add spec_embed.go for OpenAPI spec embedding - Create modular build scripts (cli, go, native, cross-platform) - Add deployment cleanup and health-check utilities - New ADRs: hot reload, audit store, SSE updates, RBAC, caching, offline mode, KMS regions, tenant offboarding - Add KMS configuration schema and worker variants - Include KMS benchmark tests
201 lines
5.7 KiB
Go
201 lines
5.7 KiB
Go
// Package audit provides HTTP handlers for audit log management
|
|
package audit
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/jfraeys/fetch_ml/internal/auth"
|
|
"github.com/jfraeys/fetch_ml/internal/logging"
|
|
)
|
|
|
|
// Handler provides audit-related HTTP API handlers
|
|
type Handler struct {
|
|
logger *logging.Logger
|
|
store AuditStore // Optional: separate store for querying
|
|
}
|
|
|
|
// AuditStore interface for querying audit events
|
|
type AuditStore interface {
|
|
QueryEvents(from, to time.Time, eventType, userID string, limit, offset int) ([]AuditEvent, int, error)
|
|
}
|
|
|
|
// AuditEvent represents an audit event for API responses
|
|
type AuditEvent struct {
|
|
Timestamp time.Time `json:"timestamp"`
|
|
EventType string `json:"event_type"`
|
|
UserID string `json:"user_id,omitempty"`
|
|
Resource string `json:"resource,omitempty"`
|
|
Action string `json:"action,omitempty"`
|
|
Success bool `json:"success"`
|
|
IPAddress string `json:"ip_address,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
PrevHash string `json:"prev_hash,omitempty"`
|
|
EventHash string `json:"event_hash,omitempty"`
|
|
SequenceNum int `json:"sequence_num,omitempty"`
|
|
Metadata json.RawMessage `json:"metadata,omitempty"`
|
|
}
|
|
|
|
// AuditEventList represents a list of audit events
|
|
type AuditEventList struct {
|
|
Events []AuditEvent `json:"events"`
|
|
Total int `json:"total"`
|
|
Limit int `json:"limit"`
|
|
Offset int `json:"offset"`
|
|
}
|
|
|
|
// VerificationResult represents the result of audit chain verification
|
|
type VerificationResult struct {
|
|
Valid bool `json:"valid"`
|
|
TotalEvents int `json:"total_events"`
|
|
FirstTampered int `json:"first_tampered,omitempty"`
|
|
ChainRootHash string `json:"chain_root_hash,omitempty"`
|
|
VerifiedAt time.Time `json:"verified_at"`
|
|
}
|
|
|
|
// ChainRootResponse represents the chain root hash response
|
|
type ChainRootResponse struct {
|
|
RootHash string `json:"root_hash"`
|
|
Timestamp time.Time `json:"timestamp"`
|
|
TotalEvents int `json:"total_events"`
|
|
}
|
|
|
|
// NewHandler creates a new audit API handler
|
|
func NewHandler(logger *logging.Logger, store AuditStore) *Handler {
|
|
return &Handler{
|
|
logger: logger,
|
|
store: store,
|
|
}
|
|
}
|
|
|
|
// GetV1AuditEvents handles GET /v1/audit/events
|
|
func (h *Handler) GetV1AuditEvents(w http.ResponseWriter, r *http.Request) {
|
|
user := auth.GetUserFromContext(r.Context())
|
|
if !h.checkPermission(user, "audit:read") {
|
|
http.Error(w, `{"error":"Insufficient permissions","code":"FORBIDDEN"}`, http.StatusForbidden)
|
|
return
|
|
}
|
|
|
|
// Parse query parameters
|
|
fromStr := r.URL.Query().Get("from")
|
|
toStr := r.URL.Query().Get("to")
|
|
eventType := r.URL.Query().Get("event_type")
|
|
userID := r.URL.Query().Get("user_id")
|
|
limit := parseIntQueryParam(r, "limit", 100)
|
|
offset := parseIntQueryParam(r, "offset", 0)
|
|
|
|
// Validate limit
|
|
if limit > 1000 {
|
|
limit = 1000
|
|
}
|
|
|
|
// Parse timestamps
|
|
var from, to time.Time
|
|
if fromStr != "" {
|
|
from, _ = time.Parse(time.RFC3339, fromStr)
|
|
}
|
|
if toStr != "" {
|
|
to, _ = time.Parse(time.RFC3339, toStr)
|
|
}
|
|
|
|
// If store is available, query from it
|
|
if h.store != nil {
|
|
events, total, err := h.store.QueryEvents(from, to, eventType, userID, limit, offset)
|
|
if err != nil {
|
|
http.Error(w, `{"error":"Failed to query events","code":"INTERNAL_ERROR"}`, http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
response := AuditEventList{
|
|
Events: events,
|
|
Total: total,
|
|
Limit: limit,
|
|
Offset: offset,
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
return
|
|
}
|
|
|
|
// Return empty list if no store configured
|
|
response := AuditEventList{
|
|
Events: []AuditEvent{},
|
|
Total: 0,
|
|
Limit: limit,
|
|
Offset: offset,
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
// PostV1AuditVerify handles POST /v1/audit/verify
|
|
func (h *Handler) PostV1AuditVerify(w http.ResponseWriter, r *http.Request) {
|
|
user := auth.GetUserFromContext(r.Context())
|
|
if !h.checkPermission(user, "audit:verify") {
|
|
http.Error(w, `{"error":"Insufficient permissions","code":"FORBIDDEN"}`, http.StatusForbidden)
|
|
return
|
|
}
|
|
|
|
h.logger.Info("verifying audit chain", "user", user.Name)
|
|
|
|
// Perform verification (placeholder implementation)
|
|
result := VerificationResult{
|
|
Valid: true,
|
|
TotalEvents: 0, // Would be populated from actual verification
|
|
VerifiedAt: time.Now().UTC(),
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(result)
|
|
}
|
|
|
|
// GetV1AuditChainRoot handles GET /v1/audit/chain-root
|
|
func (h *Handler) GetV1AuditChainRoot(w http.ResponseWriter, r *http.Request) {
|
|
user := auth.GetUserFromContext(r.Context())
|
|
if !h.checkPermission(user, "audit:read") {
|
|
http.Error(w, `{"error":"Insufficient permissions","code":"FORBIDDEN"}`, http.StatusForbidden)
|
|
return
|
|
}
|
|
|
|
// Get chain root (placeholder implementation)
|
|
response := ChainRootResponse{
|
|
RootHash: "sha256:0000000000000000000000000000000000000000000000000000000000000000",
|
|
Timestamp: time.Now().UTC(),
|
|
TotalEvents: 0,
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
// checkPermission checks if the user has the required permission
|
|
func (h *Handler) checkPermission(user *auth.User, permission string) bool {
|
|
if user == nil {
|
|
return false
|
|
}
|
|
|
|
// Admin has all permissions
|
|
if user.Admin {
|
|
return true
|
|
}
|
|
|
|
// Check specific permission
|
|
return user.HasPermission(permission)
|
|
}
|
|
|
|
// parseIntQueryParam parses an integer query parameter
|
|
func parseIntQueryParam(r *http.Request, name string, defaultVal int) int {
|
|
str := r.URL.Query().Get(name)
|
|
if str == "" {
|
|
return defaultVal
|
|
}
|
|
val, err := strconv.Atoi(str)
|
|
if err != nil {
|
|
return defaultVal
|
|
}
|
|
return val
|
|
}
|