Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

6.4 KB
Raw Blame
package audit

import (
	"bytes"
	"context"
	"encoding/json"
	"log/slog"
	"testing"

	"github.com/stretchr/testify/require"
	spb "google.golang.org/genproto/googleapis/rpc/status"
	"google.golang.org/protobuf/types/known/durationpb"

	storepb "github.com/bytebase/bytebase/backend/generated-go/store"
)

// TestLogAuditToStdoutFormat is a contract test for the structured JSON fields
// emitted to stdout when audit log stdout is enabled. Operators run commands
// like `docker logs <container> | grep '"log_type":"audit"'` — changes to
// these keys or their values are breaking for downstream log pipelines.
func TestLogAuditToStdoutFormat(t *testing.T) {
	a := require.New(t)

	// Redirect slog default to a JSON handler writing into a buffer so the
	// test can inspect the exact structured payload.
	prev := slog.Default()
	t.Cleanup(func() { slog.SetDefault(prev) })

	var buf bytes.Buffer
	slog.SetDefault(slog.New(slog.NewJSONHandler(&buf, &slog.HandlerOptions{
		Level: slog.LevelInfo,
	})))

	p := &storepb.AuditLog{
		Parent:   "workspaces/ws-abc",
		Method:   "/bytebase.v1.AuthService/Login",
		Resource: "[email protected]",
		Severity: storepb.AuditLog_INFO,
		User:     "users/[email protected]",
		Request:  `{"email":"[email protected]","web":true}`,
		Response: `{"user":{"name":"users/[email protected]","email":"[email protected]"}}`,
		Status:   &spb.Status{Code: 0, Message: ""},
		Latency:  durationpb.New(123_000_000), // 123ms
		RequestMetadata: &storepb.RequestMetadata{
			CallerIp:                "10.0.1.50",
			CallerSuppliedUserAgent: "TestAgent/1.0",
		},
	}

	LogAuditToStdout(context.Background(), p)

	// Every line the handler wrote is an independent JSON object.
	lines := bytes.Split(bytes.TrimSpace(buf.Bytes()), []byte{'\n'})
	a.Len(lines, 1, "LogAuditToStdout must emit exactly one JSON record")

	var got map[string]any
	a.NoError(json.Unmarshal(lines[0], &got),
		"stdout record must be valid JSON: %s", string(lines[0]))

	// Keys the customer explicitly greps for — these form our public contract.
	// If any of these assertions need to change, it's a breaking change for
	// everyone consuming audit stdout logs.
	a.Equal("audit", got["log_type"], `log_type=="audit" is how operators filter audit lines from application logs`)
	a.Equal("workspaces/ws-abc", got["parent"])
	a.Equal("/bytebase.v1.AuthService/Login", got["method"])
	a.Equal("[email protected]", got["resource"])
	a.Equal("users/[email protected]", got["user"])
	a.Equal("INFO", got["severity"])
	a.Equal("10.0.1.50", got["client_ip"])
	a.Equal("TestAgent/1.0", got["user_agent"])
	a.Equal(float64(123), got["latency_ms"], "latency must be exposed in milliseconds")
	a.Equal(`{"email":"[email protected]","web":true}`, got["request"],
		"request payload must be emitted verbatim (already redacted upstream)")
	a.Equal(`{"user":{"name":"users/[email protected]","email":"[email protected]"}}`, got["response"])

	// Status code 0 (OK) should be reported explicitly so downstream pipelines
	// can distinguish successful from failed calls.
	a.Equal(float64(0), got["status_code"])

	// slog standard fields — keep them stable too.
	a.Equal("INFO", got["level"])
	a.Equal("/bytebase.v1.AuthService/Login", got["msg"],
		"slog msg is set to the method so operators can read the log without parsing")

	// Error status round-trip: a failed call must emit status_code and status_message.
	buf.Reset()
	p2 := &storepb.AuditLog{
		Parent:   "workspaces/ws-abc",
		Method:   "/bytebase.v1.AuthService/Login",
		Severity: storepb.AuditLog_INFO,
		Status:   &spb.Status{Code: 16, Message: "invalid email or password"}, // UNAUTHENTICATED
		Latency:  durationpb.New(5_000_000),
	}
	LogAuditToStdout(context.Background(), p2)

	var failed map[string]any
	a.NoError(json.Unmarshal(bytes.TrimSpace(buf.Bytes()), &failed))
	a.Equal(float64(16), failed["status_code"], "failed call must carry status_code")
	a.Equal("invalid email or password", failed["status_message"],
		"failed call must carry status_message")

	// MCP-delegated calls carry their provenance in stdout too, so operators
	// tailing container logs can trace an agent session without the API.
	a.NotContains(got, "mcp_correlation_id",
		"non-MCP rows must not emit MCP keys")
	buf.Reset()
	p3 := &storepb.AuditLog{
		Parent:   "workspaces/ws-abc",
		Method:   "/bytebase.v1.SQLService/Query",
		Severity: storepb.AuditLog_INFO,
		McpDelegation: &storepb.MCPDelegation{
			Scope:         "mcp:read-only",
			Resource:      "https://bb.example.com/mcp",
			ClientId:      "client-A",
			CorrelationId: "corr-42",
		},
	}
	LogAuditToStdout(context.Background(), p3)

	var delegated map[string]any
	a.NoError(json.Unmarshal(bytes.TrimSpace(buf.Bytes()), &delegated))
	a.Equal(true, delegated["mcp"], `mcp==true is how operators filter MCP-originated rows`)
	a.Equal("corr-42", delegated["mcp_correlation_id"],
		"the session correlation ID is the key operators pivot on")
	a.Equal("mcp:read-only", delegated["mcp_scope"])
	a.Equal("https://bb.example.com/mcp", delegated["mcp_resource"])
	a.Equal("client-A", delegated["mcp_client_id"])
}

// recordingWriter captures the row RecordOutOfBandAudit inserts.
type recordingWriter struct {
	rows []*storepb.AuditLog
}

func (w *recordingWriter) CreateAuditLog(_ context.Context, _ string, row *storepb.AuditLog) error {
	w.rows = append(w.rows, row)
	return nil
}

// TestRecordOutOfBandAuditStampsWarning pins that a door outside the connect
// chains cannot file a refusal the compliance reader's severity filter misses.
// The /mcp connection and OAuth consent doors both refuse on the same ceiling
// verdict the per-request gate does, and the gate's refusal is WARNING.
func TestRecordOutOfBandAuditStampsWarning(t *testing.T) {
	a := require.New(t)

	prev := slog.Default()
	t.Cleanup(func() { slog.SetDefault(prev) })
	var buf bytes.Buffer
	slog.SetDefault(slog.New(slog.NewJSONHandler(&buf, nil)))

	writer := &recordingWriter{}
	row := &storepb.AuditLog{
		Parent: "workspaces/ws-abc",
		Method: AuditMethodMCPSessionAuthorize,
		User:   "users/[email protected]",
		Status: &spb.Status{Code: 7, Message: "MCP access is turned off"},
	}

	RecordOutOfBandAudit(context.Background(), writer, true, "ws-abc", row)

	a.Len(writer.rows, 1)
	a.Equal(storepb.AuditLog_WARNING, writer.rows[0].Severity, "a refusal is stored as a warning")

	var line map[string]any
	a.NoError(json.Unmarshal(bytes.TrimSpace(buf.Bytes()), &line))
	a.Equal(storepb.AuditLog_WARNING.String(), line["severity"],
		"the stream carries the same severity as the row")
}