Skip to content

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

fix: restore audit logs for Login/Signup/ExchangeToken (#20024)

Danny Xue79f0770b041
diff --git a/backend/api/v1/audit.go b/backend/api/v1/audit.go
index fa4cfdcd6d..1c551b1f16 100644
--- a/backend/api/v1/audit.go
+++ b/backend/api/v1/audit.go
@@ -63,6 +63,14 @@ func (in *AuditInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc
serviceData = a
})
+ // Allow handlers to announce the workspace a request should be audited
+ // against. Needed for allow_without_credential methods (Login/Signup/
+ // ExchangeToken) where the workspace is resolved inside the handler.
+ var handlerAuditWorkspaceID string
+ ctx = common.WithSetAuditWorkspaceID(ctx, func(workspaceID string) {
+ handlerAuditWorkspaceID = workspaceID
+ })
+
startTime := time.Now()
response, rerr := next(ctx, req)
latency := time.Since(startTime)
@@ -72,7 +80,18 @@ func (in *AuditInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc
if !common.IsNil(response) {
respMsg = response.Any()
}
- if err := createAuditLogConnect(ctx, req.Any(), respMsg, req.Spec().Procedure, in.store, in.secret, in.profile, serviceData, rerr, req.Header(), req.Peer().Addr, latency); err != nil {
+ entry := &auditEntry{
+ request: req.Any(),
+ response: respMsg,
+ method: req.Spec().Procedure,
+ serviceData: serviceData,
+ handlerAuditWorkspaceID: handlerAuditWorkspaceID,
+ rerr: rerr,
+ headers: req.Header(),
+ peerAddr: req.Peer().Addr,
+ latency: latency,
+ }
+ if err := in.createAuditLog(ctx, entry); err != nil {
slog.Warn("audit interceptor: failed to create audit log", log.BBError(err), slog.String("method", req.Spec().Procedure))
}
}
@@ -132,25 +151,52 @@ func (c *auditConnectStreamingConn) Send(resp any) error {
}
// Create audit log for each message pair
if c.curRequest != nil {
- latency := time.Since(c.startTime)
- if auditErr := createAuditLogConnect(c.ctx, c.curRequest, resp, c.method, c.interceptor.store, c.interceptor.secret, c.interceptor.profile, nil, nil, c.RequestHeader(), c.Peer().Addr, latency); auditErr != nil {
+ entry := &auditEntry{
+ request: c.curRequest,
+ response: resp,
+ method: c.method,
+ headers: c.RequestHeader(),
+ peerAddr: c.Peer().Addr,
+ latency: time.Since(c.startTime),
+ }
+ if auditErr := c.interceptor.createAuditLog(c.ctx, entry); auditErr != nil {
return auditErr
}
}
return nil
}
-func createAuditLogConnect(ctx context.Context, request, response any, method string, storage *store.Store, secret string, profile *config.Profile, serviceData *anypb.Any, rerr error, headers http.Header, peerAddr string, latency time.Duration) error {
+// auditEntry bundles the per-request data needed to write an audit log.
+// Bundling here keeps AuditInterceptor.createAuditLog to a short signature
+// (previously 13 positional args — easy to misorder).
+type auditEntry struct {
+ request any
+ response any
+ method string
+ // serviceData is populated by handlers via common.WithSetServiceData.
+ serviceData *anypb.Any
+ // handlerAuditWorkspaceID is populated by handlers via
+ // common.SetAuditWorkspaceID. Used as the audit-parent fallback for
+ // allow_without_credential methods (Login/Signup/ExchangeToken) where
+ // authContext.Resources is empty because no workspace is in the context.
+ handlerAuditWorkspaceID string
+ rerr error
+ headers http.Header
+ peerAddr string
+ latency time.Duration
+}
+
+func (in *AuditInterceptor) createAuditLog(ctx context.Context, e *auditEntry) error {
// Skip audit logging for validate-only requests.
- if isValidateOnlyRequest(request) {
+ if isValidateOnlyRequest(e.request) {
return nil
}
- requestString, err := getRequestString(request)
+ requestString, err := getRequestString(e.request)
if err != nil {
return errors.Wrapf(err, "failed to get request string")
}
- responseString, err := getResponseString(response)
+ responseString, err := getResponseString(e.response)
if err != nil {
return errors.Wrapf(err, "failed to get response string")
}
@@ -160,7 +206,7 @@ func createAuditLogConnect(ctx context.Context, request, response any, method st
user = common.FormatUserEmail(u.Email)
} else {
// Try to get user from successful login response.
- if loginResponse, ok := response.(*v1pb.LoginResponse); ok {
+ if loginResponse, ok := e.response.(*v1pb.LoginResponse); ok {
user = loginResponse.GetUser().GetName()
}
}
@@ -171,50 +217,86 @@ func createAuditLogConnect(ctx context.Context, request, response any, method st
return connect.NewError(connect.CodeInternal, errors.New("auth context not found"))
}
- requestMetadata := getRequestMetadataFromHeaders(headers, peerAddr)
+ requestMetadata := getRequestMetadataFromHeaders(e.headers, e.peerAddr)
- createAuditLogCtx := context.WithoutCancel(ctx)
+ // Build the list of parents to audit under. Normally these come from the
+ // ACL interceptor via authContext.Resources. For audited methods that run
+ // without a workspace-bound caller (Login/Signup/ExchangeToken), Resources
+ // is empty; fall back to the workspace the handler announced, or any
+ // workspace embedded in the response, so the audit entry is still written.
+ type auditParent struct {
+ parent string
+ auditWorkspaceID string
+ }
+ var parents []auditParent
for _, authResource := range authContext.Resources {
- var parent string
- var auditWorkspaceID string
switch authResource.Type {
case common.ResourceTypeProject:
- parent = common.FormatProject(authResource.ID)
+ parents = append(parents, auditParent{
+ parent: common.FormatProject(authResource.ID),
+ })
case common.ResourceTypeWorkspace:
- parent = common.FormatWorkspace(authResource.ID)
- auditWorkspaceID = authResource.ID
+ parents = append(parents, auditParent{
+ parent: common.FormatWorkspace(authResource.ID),
+ auditWorkspaceID: authResource.ID,
+ })
default:
- continue
}
- resource := getRequestResource(request)
+ }
+ if len(parents) == 0 {
+ fallbackWS := e.handlerAuditWorkspaceID
+ if fallbackWS == "" {
+ if loginResp, ok := e.response.(*v1pb.LoginResponse); ok {
+ if wsID, err := common.GetWorkspaceID(loginResp.GetUser().GetWorkspace()); err == nil && wsID != "" {
+ fallbackWS = wsID
+ }
+ }
+ }
+ // Defensive: covers edge cases where populateRawResources produced no
+ // resource despite the caller being authenticated (e.g. malformed
+ // inputs like "instances/" that match the prefix but fail the regex).
+ if fallbackWS == "" {
+ fallbackWS = common.GetWorkspaceIDFromContext(ctx)
+ }
+ if fallbackWS != "" {
+ parents = append(parents, auditParent{
+ parent: common.FormatWorkspace(fallbackWS),
+ auditWorkspaceID: fallbackWS,
+ })
+ }
+ }
+
+ createAuditLogCtx := context.WithoutCancel(ctx)
+ for _, ap := range parents {
+ resource := getRequestResource(e.request)
// For login requests, if resource is empty, try to get email from user context or MFA temp token.
// This handles MFA phase where request doesn't have email field.
- if resource == "" && method == v1connect.AuthServiceLoginProcedure {
+ if resource == "" && e.method == v1connect.AuthServiceLoginProcedure {
if u, ok := GetUserFromContext(ctx); ok {
resource = u.Email
- } else if loginRequest, ok := request.(*v1pb.LoginRequest); ok && loginRequest.MfaTempToken != nil {
+ } else if loginRequest, ok := e.request.(*v1pb.LoginRequest); ok && loginRequest.MfaTempToken != nil {
// Extract user email from MFA temp token.
- if userEmail, err := auth.GetUserEmailFromMFATempToken(*loginRequest.MfaTempToken, secret); err == nil {
+ if userEmail, err := auth.GetUserEmailFromMFATempToken(*loginRequest.MfaTempToken, in.secret); err == nil {
resource = userEmail
}
}
}
p := &storepb.AuditLog{
- Parent: parent,
- Method: method,
+ Parent: ap.parent,
+ Method: e.method,
Resource: resource,
Severity: storepb.AuditLog_INFO,
User: user,
Request: requestString,
Response: responseString,
- Status: convertErrToStatus(rerr),
- Latency: durationpb.New(latency),
- ServiceData: serviceData,
+ Status: convertErrToStatus(e.rerr),
+ Latency: durationpb.New(e.latency),
+ ServiceData: e.serviceData,
RequestMetadata: requestMetadata,
}
// Resolve workspace for audit log.
- workspaceIDForAudit := auditWorkspaceID
+ workspaceIDForAudit := ap.auditWorkspaceID
if workspaceIDForAudit == "" {
workspaceIDForAudit = common.GetWorkspaceIDFromContext(createAuditLogCtx)
}
@@ -222,12 +304,12 @@ func createAuditLogConnect(ctx context.Context, request, response any, method st
// Skip audit log if no workspace can be determined (e.g., unauthenticated request).
continue
}
- if err := storage.CreateAuditLog(createAuditLogCtx, workspaceIDForAudit, p); err != nil {
+ if err := in.store.CreateAuditLog(createAuditLogCtx, workspaceIDForAudit, p); err != nil {
return err
}
// Log audit event to stdout using slog (if enabled)
- if profile.RuntimeEnableAuditLogStdout.Load() {
+ if in.profile.RuntimeEnableAuditLogStdout.Load() {
logAuditToStdout(ctx, p)
}
}
@@ -330,6 +412,10 @@ func getRequestResource(request any) string {
return r.GetUser().GetName()
case *v1pb.LoginRequest:
return r.GetEmail()
+ case *v1pb.SignupRequest:
+ return r.GetEmail()
+ case *v1pb.ExchangeTokenRequest:
+ return r.GetEmail()
case *v1pb.CreateInstanceRequest:
return r.GetInstance().GetName()
case *v1pb.UpdateInstanceRequest:
@@ -365,6 +451,10 @@ func getRequestString(request any) (string, error) {
return redactUpdateUserRequest(r)
case *v1pb.LoginRequest:
return redactLoginRequest(r)
+ case *v1pb.SignupRequest:
+ return redactSignupRequest(r)
+ case *v1pb.ExchangeTokenRequest:
+ return redactExchangeTokenRequest(r)
case *v1pb.CreateInstanceRequest:
r.Instance = redactInstance(r.Instance)
return r
@@ -412,6 +502,8 @@ func getResponseString(response any) (string, error) {
return nil
case *v1pb.LoginResponse:
return redactLoginResponse(r)
+ case *v1pb.ExchangeTokenResponse:
+ return redactExchangeTokenResponse(r)
case *v1pb.User:
return redactUser(r)
case *v1pb.Instance:
@@ -471,6 +563,43 @@ func redactLoginRequest(r *v1pb.LoginRequest) *v1pb.LoginRequest {
return r
}
+func redactSignupRequest(r *v1pb.SignupRequest) *v1pb.SignupRequest {
+ if r == nil {
+ return nil
+ }
+ r = proto.CloneOf(r)
+ if r.Password != "" {
+ r.Password = maskedString
+ }
+ return r
+}
+
+// redactExchangeTokenRequest masks the external OIDC JWT. The token is a
+// credential — it could be replayed against the original IdP or, if logged,
+// reveal workload identity claims. The caller's email is kept for audit
+// correlation.
+func redactExchangeTokenRequest(r *v1pb.ExchangeTokenRequest) *v1pb.ExchangeTokenRequest {
+ if r == nil {
+ return nil
+ }
+ r = proto.CloneOf(r)
+ if r.Token != "" {
+ r.Token = maskedString
+ }
+ return r
+}
+
+// redactExchangeTokenResponse drops the issued Bytebase access token. Logging
+// it would give anyone with audit-log read access a valid API token for the
+// workload identity. Returns an empty response so the audit entry still
+// records that the call happened.
+func redactExchangeTokenResponse(r *v1pb.ExchangeTokenResponse) *v1pb.ExchangeTokenResponse {
+ if r == nil {
+ return nil
+ }
+ return &v1pb.ExchangeTokenResponse{}
+}
+
func redactCreateUserRequest(r *v1pb.CreateUserRequest) *v1pb.CreateUserRequest {
if r == nil {
return nil
diff --git a/backend/api/v1/audit_test.go b/backend/api/v1/audit_test.go
new file mode 100644
index 0000000000..f4c226271d
--- /dev/null
+++ b/backend/api/v1/audit_test.go
@@ -0,0 +1,173 @@
+package v1
+
+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"
+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
+)
+
+// 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")
+}
+
+// TestAuditRedactsCredentials covers the request/response redactors that
+// strip secrets before the audit pipeline serializes payloads. Specifically
+// guards against regressions like the Signup password leak and the
+// ExchangeToken OIDC/access-token leak that Codex flagged on #20024 — both
+// only surfaced once the corresponding audit path was re-enabled by the
+// SetAuditWorkspaceID callback.
+func TestAuditRedactsCredentials(t *testing.T) {
+ a := require.New(t)
+
+ t.Run("LoginRequest redacts password and MFA secrets", func(_ *testing.T) {
+ otp := "123456"
+ mfa := "mfa-temp-jwt"
+ reqStr, err := getRequestString(&v1pb.LoginRequest{
+ Email: "[email protected]",
+ Password: "hunter2",
+ OtpCode: &otp,
+ MfaTempToken: &mfa,
+ })
+ a.NoError(err)
+ a.Contains(reqStr, "[email protected]", "non-sensitive email stays")
+ a.NotContains(reqStr, "hunter2", "plaintext password must not appear")
+ a.NotContains(reqStr, "123456", "OTP must not appear")
+ a.NotContains(reqStr, "mfa-temp-jwt", "MFA temp token must not appear")
+ })
+
+ t.Run("LoginResponse drops token", func(_ *testing.T) {
+ respStr, err := getResponseString(&v1pb.LoginResponse{
+ Token: "secret-access-token",
+ User: &v1pb.User{Name: "users/[email protected]"},
+ })
+ a.NoError(err)
+ a.Contains(respStr, "users/[email protected]", "user info is retained")
+ a.NotContains(respStr, "secret-access-token", "access token must not appear")
+ })
+
+ t.Run("SignupRequest redacts password", func(_ *testing.T) {
+ reqStr, err := getRequestString(&v1pb.SignupRequest{
+ Email: "[email protected]",
+ Password: "signup-password",
+ Title: "bob",
+ })
+ a.NoError(err)
+ a.Contains(reqStr, "[email protected]")
+ a.NotContains(reqStr, "signup-password",
+ "plaintext password must not appear in Signup audit")
+ })
+
+ t.Run("ExchangeTokenRequest redacts OIDC token", func(_ *testing.T) {
+ reqStr, err := getRequestString(&v1pb.ExchangeTokenRequest{
+ Token: "oidc.jwt.payload",
+ Email: "[email protected]",
+ })
+ a.NoError(err)
+ a.Contains(reqStr, "[email protected]",
+ "workload email retained for audit correlation")
+ a.NotContains(reqStr, "oidc.jwt.payload",
+ "external OIDC token must not appear in audit — it can be replayed "+
+ "against the original IdP or reveal workload claims")
+ })
+
+ t.Run("ExchangeTokenResponse drops issued access token", func(_ *testing.T) {
+ respStr, err := getResponseString(&v1pb.ExchangeTokenResponse{
+ AccessToken: "issued-bytebase-api-token",
+ })
+ a.NoError(err)
+ a.NotContains(respStr, "issued-bytebase-api-token",
+ "issued Bytebase access token must never be logged — anyone with "+
+ "audit read access could use it as a valid API token")
+ })
+}
diff --git a/backend/api/v1/auth_service.go b/backend/api/v1/auth_service.go
index 7afbd4e165..26c1747152 100644
--- a/backend/api/v1/auth_service.go
+++ b/backend/api/v1/auth_service.go
@@ -101,6 +101,7 @@ func (s *AuthService) Login(ctx context.Context, req *connect.Request[v1pb.Login
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to resolve workspace"))
}
+ common.SetAuditWorkspaceID(ctx, workspaceID)
// 3. Post-auth checks (deleted, domain, license)
if err := s.validateLoginPermissions(ctx, loginUser, workspaceID, request); err != nil {
@@ -212,6 +213,15 @@ func (s *AuthService) Signup(ctx context.Context, req *connect.Request[v1pb.Sign
}
var workspaceID string
+ // Announce the workspace on every exit path so early-failure cases
+ // (DisallowSignup, password restriction, CreateUser failure, …) still
+ // produce audit entries for invited/self-hosted signups where the
+ // workspace is already resolved. The setter no-ops on empty input, so
+ // SaaS new-workspace signups that fail before workspace creation stay
+ // (correctly) unaudited — there's genuinely no workspace to attribute
+ // them to.
+ defer func() { common.SetAuditWorkspaceID(ctx, workspaceID) }()
+
isMember := existingWS != nil
if isMember {
workspaceID = existingWS.ResourceID
@@ -1289,6 +1299,10 @@ func (s *AuthService) ExchangeToken(ctx context.Context, req *connect.Request[v1
if wi == nil {
return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("workload identity %q not found", request.Email))
}
+ // Announce the workspace as soon as we know it (from the WI record) so
+ // that a deactivated-WI attempt — which compliance wants to see — still
+ // lands in the audit log.
+ common.SetAuditWorkspaceID(ctx, wi.Workspace)
if wi.MemberDeleted {
return nil, connect.NewError(connect.CodeUnauthenticated,
errors.New("workload identity has been deactivated"))
diff --git a/backend/common/context.go b/backend/common/context.go
index 860cf212da..6bf43ce61e 100644
--- a/backend/common/context.go
+++ b/backend/common/context.go
@@ -16,6 +16,7 @@ const (
AuthContextKey
ServiceDataKey
WorkspaceIDContextKey
+ AuditWorkspaceIDKey
)
func WithSetServiceData(ctx context.Context, setServiceData func(a *anypb.Any)) context.Context {
@@ -27,6 +28,29 @@ func GetSetServiceDataFromContext(ctx context.Context) (func(a *anypb.Any), bool
return setServiceData, ok
}
+// WithSetAuditWorkspaceID registers a callback handlers can use to tell the
+// audit interceptor which workspace a request should be audited against. This
+// is needed for methods that run with allow_without_credential=true (e.g.
+// Login/Signup/ExchangeToken): the workspace is unknown when the interceptor
+// chain starts, but the handler learns it before returning.
+func WithSetAuditWorkspaceID(ctx context.Context, setAuditWorkspaceID func(workspaceID string)) context.Context {
+ return context.WithValue(ctx, AuditWorkspaceIDKey, setAuditWorkspaceID)
+}
+
+// SetAuditWorkspaceID records the workspace that the current request should be
+// audited against, if the audit interceptor registered a setter on the context.
+// Safe to call even when auditing is disabled for the current method.
+func SetAuditWorkspaceID(ctx context.Context, workspaceID string) {
+ if workspaceID == "" {
+ return
+ }
+ setter, ok := ctx.Value(AuditWorkspaceIDKey).(func(string))
+ if !ok {
+ return
+ }
+ setter(workspaceID)
+}
+
// GetWorkspaceIDFromContext returns the workspace ID from the request context.
func GetWorkspaceIDFromContext(ctx context.Context) string {
if v, ok := ctx.Value(WorkspaceIDContextKey).(string); ok {
diff --git a/backend/tests/login_audit_test.go b/backend/tests/login_audit_test.go
new file mode 100644
index 0000000000..2fd8595941
--- /dev/null
+++ b/backend/tests/login_audit_test.go
@@ -0,0 +1,243 @@
+package tests
+
+import (
+ "context"
+ "regexp"
+ "strings"
+ "testing"
+
+ "connectrpc.com/connect"
+ "github.com/stretchr/testify/require"
+ "google.golang.org/protobuf/types/known/fieldmaskpb"
+
+ "github.com/bytebase/bytebase/backend/common"
+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
+)
+
+// TestAuditLogFormat is both a regression test for the 3.17.0 bug where
+// AuthService/Login (and Signup/ExchangeToken) silently dropped audit entries,
+// AND a contract test for the shape of audit log entries returned by
+// AuditLogService/SearchAuditLogs. Downstream consumers (SIEMs, compliance
+// tooling, `docker logs | grep log_type:audit`) depend on this shape being
+// stable across releases — changes here are user-visible breaking changes.
+func TestAuditLogFormat(t *testing.T) {
+ a := require.New(t)
+ ctx := context.Background()
+ ctl := &controller{}
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
+ a.NoError(err)
+ defer ctl.Close(ctx)
+
+ // --- Part 1: Login (workspace-scoped, allow_without_credential) ---
+ //
+ // Clear the token so the Login call runs without credentials — the exact
+ // path that regressed in 3.17.0 (Resources is empty, so the audit loop
+ // never fires unless the handler hands us the workspace).
+ adminToken := ctl.authInterceptor.token
+ ctl.authInterceptor.token = ""
+
+ loginResp, err := ctl.authServiceClient.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{
+ Email: "[email protected]",
+ Password: "1024bytebase",
+ }))
+ a.NoError(err)
+ workspace := loginResp.Msg.GetUser().GetWorkspace()
+ a.NotEmpty(workspace, "login response should carry the user's workspace")
+
+ // Restore the token so the SearchAuditLogs call below authenticates.
+ ctl.authInterceptor.token = adminToken
+
+ loginAuditLogs, err := ctl.auditLogServiceClient.SearchAuditLogs(ctx, connect.NewRequest(&v1pb.SearchAuditLogsRequest{
+ Parent: workspace,
+ Filter: `method == "/bytebase.v1.AuthService/Login"`,
+ OrderBy: "create_time desc",
+ }))
+ a.NoError(err)
+ a.NotEmpty(loginAuditLogs.Msg.AuditLogs, "Login must produce an audit entry under the caller's workspace (regression guard for 3.17.0)")
+
+ entry := loginAuditLogs.Msg.AuditLogs[0]
+ // Name: "workspaces/{id}/auditLogs/{uid}" — the resource name format is
+ // part of the API contract and also the parent users filter on.
+ a.Regexp(regexp.MustCompile(`^workspaces/[^/]+/auditLogs/[^/]+$`), entry.Name,
+ "audit log name must match workspaces/{id}/auditLogs/{uid}")
+ a.True(strings.HasPrefix(entry.Name, workspace+"/auditLogs/"),
+ "audit log must be parented under the login workspace")
+ a.NotNil(entry.CreateTime, "CreateTime must be set")
+ a.Equal("users/[email protected]", entry.User, "User must be users/{email}")
+ a.Equal("/bytebase.v1.AuthService/Login", entry.Method,
+ "Method is part of the filter API contract and must be the full procedure name")
+ a.Equal(v1pb.AuditLog_INFO, entry.Severity, "successful Login is INFO severity")
+ a.Equal("[email protected]", entry.Resource, "Login's Resource is the login email")
+ a.Nil(entry.Status, "successful Login has nil Status (code 0)")
+ a.NotNil(entry.Latency, "Latency must be recorded")
+
+ // Request JSON must round-trip back to LoginRequest, have the caller's
+ // email, and must NOT contain the plaintext password.
+ gotReq := &v1pb.LoginRequest{}
+ a.NoError(common.ProtojsonUnmarshaler.Unmarshal([]byte(entry.Request), gotReq),
+ "Request JSON must be valid protojson for LoginRequest")
+ a.Equal("[email protected]", gotReq.Email)
+ a.Empty(gotReq.Password, "password must be redacted in the Request payload")
+ a.NotContains(entry.Request, "1024bytebase", "plaintext password must never appear in audit Request")
+
+ // Response JSON must round-trip back to LoginResponse with the user info
+ // but NO token (tokens are intentionally dropped from the audit payload).
+ gotResp := &v1pb.LoginResponse{}
+ a.NoError(common.ProtojsonUnmarshaler.Unmarshal([]byte(entry.Response), gotResp),
+ "Response JSON must be valid protojson for LoginResponse")
+ a.Equal("users/[email protected]", gotResp.GetUser().GetName())
+ a.Equal("[email protected]", gotResp.GetUser().GetEmail())
+ a.Empty(gotResp.Token, "token must be redacted from the Response payload")
+ a.NotContains(entry.Response, loginResp.Msg.Token,
+ "actual access token must never appear in audit Response")
+
+ // --- Part 2: Signup (workspace-scoped, allow_without_credential) ---
+ //
+ // The initial `signupAndLogin` in setup already produced a Signup audit
+ // entry; just assert it landed with the expected parent and method.
+ signupAuditLogs, err := ctl.auditLogServiceClient.SearchAuditLogs(ctx, connect.NewRequest(&v1pb.SearchAuditLogsRequest{
+ Parent: workspace,
+ Filter: `method == "/bytebase.v1.AuthService/Signup"`,
+ }))
+ a.NoError(err)
+ a.NotEmpty(signupAuditLogs.Msg.AuditLogs, "Signup must produce an audit entry under the caller's workspace")
+ signupEntry := signupAuditLogs.Msg.AuditLogs[0]
+ a.True(strings.HasPrefix(signupEntry.Name, workspace+"/auditLogs/"))
+ a.Equal("/bytebase.v1.AuthService/Signup", signupEntry.Method)
+ a.Equal(v1pb.AuditLog_INFO, signupEntry.Severity)
+ a.NotContains(signupEntry.Request, "1024bytebase",
+ "plaintext password must never appear in Signup audit Request")
+
+ // --- Part 2.5: Denied Signup is still audited ---
+ //
+ // Regression guard for #20024 (discussion_r3089978499): Signup failures
+ // that return *before* the original SetAuditWorkspaceID call site (e.g.
+ // DisallowSignup denial on invited/self-hosted signups) must still land
+ // in audit. Signup uses a defer'd SetAuditWorkspaceID so every exit
+ // path announces the resolved workspace.
+ _, err = ctl.settingServiceClient.UpdateSetting(ctx, connect.NewRequest(&v1pb.UpdateSettingRequest{
+ Setting: &v1pb.Setting{
+ Name: "settings/" + v1pb.Setting_WORKSPACE_PROFILE.String(),
+ Value: &v1pb.SettingValue{
+ Value: &v1pb.SettingValue_WorkspaceProfile{
+ WorkspaceProfile: &v1pb.WorkspaceProfileSetting{
+ DisallowSignup: true,
+ },
+ },
+ },
+ },
+ UpdateMask: &fieldmaskpb.FieldMask{
+ Paths: []string{"value.workspace_profile.disallow_signup"},
+ },
+ }))
+ a.NoError(err)
+
+ // Signup is allow_without_credential; clear token for cleanliness.
+ savedToken := ctl.authInterceptor.token
+ ctl.authInterceptor.token = ""
+ _, signupErr := ctl.authServiceClient.Signup(ctx, connect.NewRequest(&v1pb.SignupRequest{
+ Email: "[email protected]",
+ Password: "1024bytebase",
+ Title: "denied",
+ }))
+ a.Error(signupErr, "DisallowSignup must reject the new-user signup")
+ ctl.authInterceptor.token = savedToken
+
+ deniedSignupLogs, err := ctl.auditLogServiceClient.SearchAuditLogs(ctx, connect.NewRequest(&v1pb.SearchAuditLogsRequest{
+ Parent: workspace,
+ Filter: `method == "/bytebase.v1.AuthService/Signup"`,
+ OrderBy: "create_time desc",
+ }))
+ a.NoError(err)
+
+ var deniedEntry *v1pb.AuditLog
+ for _, e := range deniedSignupLogs.Msg.AuditLogs {
+ if e.Resource == "[email protected]" {
+ deniedEntry = e
+ break
+ }
+ }
+ a.NotNil(deniedEntry,
+ "denied Signup must produce an audit entry under the workspace "+
+ "(without the defer'd SetAuditWorkspaceID, the handler returns "+
+ "before the workspace is announced and the entry is silently dropped)")
+ a.NotNil(deniedEntry.Status, "denied Signup must carry a non-nil Status")
+ a.NotEqual(int32(0), deniedEntry.Status.Code,
+ "denied Signup must carry a non-zero status code")
+
+ // --- Part 3: SetIamPolicy (project-scoped, authenticated) ---
+ //
+ // Project-scoped audit entries must land under projects/{id} (not under
+ // the workspace). This is what compliance tooling filters on.
+ projectResource := ctl.project.Name // "projects/test-project"
+
+ policyResp, err := ctl.projectServiceClient.GetIamPolicy(ctx, connect.NewRequest(&v1pb.GetIamPolicyRequest{
+ Resource: projectResource,
+ }))
+ a.NoError(err)
+ policy := policyResp.Msg
+ policy.Bindings = append(policy.Bindings, &v1pb.Binding{
+ Role: "roles/projectDeveloper",
+ Members: []string{"user:[email protected]"},
+ })
+ _, err = ctl.projectServiceClient.SetIamPolicy(ctx, connect.NewRequest(&v1pb.SetIamPolicyRequest{
+ Etag: policy.Etag,
+ Policy: policy,
+ Resource: projectResource,
+ }))
+ a.NoError(err)
+
+ projectAuditLogs, err := ctl.auditLogServiceClient.SearchAuditLogs(ctx, connect.NewRequest(&v1pb.SearchAuditLogsRequest{
+ Parent: projectResource,
+ Filter: `method == "/bytebase.v1.ProjectService/SetIamPolicy"`,
+ OrderBy: "create_time desc",
+ }))
+ a.NoError(err)
+ a.NotEmpty(projectAuditLogs.Msg.AuditLogs,
+ "SetIamPolicy must produce an audit entry under projects/{id}")
+
+ projEntry := projectAuditLogs.Msg.AuditLogs[0]
+ a.Regexp(regexp.MustCompile(`^projects/[^/]+/auditLogs/[^/]+$`), projEntry.Name,
+ "project audit log name must match projects/{id}/auditLogs/{uid}")
+ a.True(strings.HasPrefix(projEntry.Name, projectResource+"/auditLogs/"),
+ "audit entry must be parented under the target project, not the workspace")
+ a.Equal("/bytebase.v1.ProjectService/SetIamPolicy", projEntry.Method)
+ a.Equal("users/[email protected]", projEntry.User)
+ a.Equal(v1pb.AuditLog_INFO, projEntry.Severity)
+ a.Equal(projectResource, projEntry.Resource,
+ "SetIamPolicy's Resource is the target project name")
+ a.Nil(projEntry.Status)
+ a.NotNil(projEntry.Latency)
+
+ gotSetReq := &v1pb.SetIamPolicyRequest{}
+ a.NoError(common.ProtojsonUnmarshaler.Unmarshal([]byte(projEntry.Request), gotSetReq),
+ "Request JSON must be valid protojson for SetIamPolicyRequest")
+ a.Equal(projectResource, gotSetReq.Resource)
+ a.NotNil(gotSetReq.Policy, "Policy must round-trip through the audit Request")
+
+ gotIamPolicy := &v1pb.IamPolicy{}
+ a.NoError(common.ProtojsonUnmarshaler.Unmarshal([]byte(projEntry.Response), gotIamPolicy),
+ "Response JSON must be valid protojson for IamPolicy")
+ // The updated binding must be visible in the recorded response.
+ foundBinding := false
+ for _, b := range gotIamPolicy.Bindings {
+ if b.Role == "roles/projectDeveloper" {
+ for _, m := range b.Members {
+ if m == "user:[email protected]" {
+ foundBinding = true
+ }
+ }
+ }
+ }
+ a.True(foundBinding, "Response JSON must reflect the new IAM binding")
+
+ // A project audit entry must NOT appear when searching under the
+ // workspace — verifies parent scoping is strict.
+ workspaceSearch, err := ctl.auditLogServiceClient.SearchAuditLogs(ctx, connect.NewRequest(&v1pb.SearchAuditLogsRequest{
+ Parent: workspace,
+ Filter: `method == "/bytebase.v1.ProjectService/SetIamPolicy"`,
+ }))
+ a.NoError(err)
+ a.Empty(workspaceSearch.Msg.AuditLogs,
+ "project-scoped SetIamPolicy audit must not leak into the workspace-scoped log stream")
+}