Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: audit log api enforcement (#17446)
Vincent Huangfeaf93b100cf
diff --git a/backend/api/v1/audit_log_service.go b/backend/api/v1/audit_log_service.goindex d0b584b577..cc9a3845e8 100644--- a/backend/api/v1/audit_log_service.go+++ b/backend/api/v1/audit_log_service.go@@ -47,6 +47,12 @@ func (s *AuditLogService) SearchAuditLogs(ctx context.Context, request *connect.return nil, err}+ // Apply retention-based filtering based on the plan+ retentionCutoff := s.licenseService.GetAuditLogRetentionCutoff()+ if retentionCutoff != nil {+ filter = s.applyRetentionFilter(filter, retentionCutoff)+ }+orderByKeys, err := getSearchAuditLogsOrderByKeys(request.Msg.OrderBy)if err != nil {return nil, connect.NewError(connect.CodeInvalidArgument, errors.Wrapf(err, "failed to get order by keys"))@@ -351,3 +357,23 @@ func (s *AuditLogService) getUserByIdentifier(ctx context.Context, identifier st}return user, nil}++// applyRetentionFilter merges retention-based filtering with user-provided filters.+func (*AuditLogService) applyRetentionFilter(userFilter *store.ListResourceFilter, cutoff *time.Time) *store.ListResourceFilter {+ if cutoff == nil {+ return userFilter+ }++ if userFilter == nil {+ return &store.ListResourceFilter{+ Where: "(created_at >= $1)",+ Args: []any{*cutoff},+ }+ }++ // Combine with existing filter using AND+ return &store.ListResourceFilter{+ Where: fmt.Sprintf("(%s) AND (created_at >= $%d)", userFilter.Where, len(userFilter.Args)+1),+ Args: append(userFilter.Args, *cutoff),+ }+}diff --git a/backend/api/v1/audit_log_service_test.go b/backend/api/v1/audit_log_service_test.gonew file mode 100644index 0000000000..94c30baacc--- /dev/null+++ b/backend/api/v1/audit_log_service_test.go@@ -0,0 +1,178 @@+package v1++import (+ "testing"+ "time"++ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"++ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+ "github.com/bytebase/bytebase/backend/store"+)++func TestApplyRetentionFilter(t *testing.T) {+ s := &AuditLogService{}+ now := time.Now()+ sevenDaysAgo := now.AddDate(0, 0, -7)++ tests := []struct {+ name string+ userFilter *store.ListResourceFilter+ cutoff *time.Time+ wantFilter *store.ListResourceFilter+ description string+ }{+ {+ name: "No retention cutoff (Enterprise plan)",+ userFilter: &store.ListResourceFilter{+ Where: "(payload->>'method'=$1)",+ Args: []any{"/bytebase.v1.SQLService/Query"},+ },+ cutoff: nil,+ wantFilter: &store.ListResourceFilter{+ Where: "(payload->>'method'=$1)",+ Args: []any{"/bytebase.v1.SQLService/Query"},+ },+ description: "Should return user filter unchanged when no cutoff",+ },+ {+ name: "Apply retention to empty filter (Team plan)",+ userFilter: nil,+ cutoff: &sevenDaysAgo,+ wantFilter: &store.ListResourceFilter{+ Where: "(created_at >= $1)",+ Args: []any{sevenDaysAgo},+ },+ description: "Should create retention filter when no user filter",+ },+ {+ name: "Merge retention with existing filter (Team plan)",+ userFilter: &store.ListResourceFilter{+ Where: "(payload->>'method'=$1)",+ Args: []any{"/bytebase.v1.SQLService/Query"},+ },+ cutoff: &sevenDaysAgo,+ wantFilter: &store.ListResourceFilter{+ Where: "((payload->>'method'=$1)) AND (created_at >= $2)",+ Args: []any{"/bytebase.v1.SQLService/Query", sevenDaysAgo},+ },+ description: "Should combine user filter with retention filter using AND",+ },+ {+ name: "Complex filter with retention",+ userFilter: &store.ListResourceFilter{+ Where: "(payload->>'method'=$1 AND payload->>'severity'=$2)",+ Args: []any{"/bytebase.v1.SQLService/Query", "ERROR"},+ },+ cutoff: &sevenDaysAgo,+ wantFilter: &store.ListResourceFilter{+ Where: "((payload->>'method'=$1 AND payload->>'severity'=$2)) AND (created_at >= $3)",+ Args: []any{"/bytebase.v1.SQLService/Query", "ERROR", sevenDaysAgo},+ },+ description: "Should handle complex filters with multiple conditions",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ got := s.applyRetentionFilter(tt.userFilter, tt.cutoff)++ require.NotNil(t, got, tt.description)+ assert.Equal(t, tt.wantFilter.Where, got.Where, "WHERE clause should match")+ assert.Equal(t, len(tt.wantFilter.Args), len(got.Args), "Number of args should match")++ // For time comparisons, we just check the type and rough equality+ for i, arg := range tt.wantFilter.Args {+ if timeArg, ok := arg.(time.Time); ok {+ gotTimeArg, ok := got.Args[i].(time.Time)+ require.True(t, ok, "Argument %d should be a time.Time", i)+ assert.WithinDuration(t, timeArg, gotTimeArg, time.Second, "Time arguments should be within 1 second")+ } else {+ assert.Equal(t, arg, got.Args[i], "Argument %d should match", i)+ }+ }+ })+ }+}++func TestGetAuditLogRetentionDays(t *testing.T) {+ // This test would require mocking the LicenseService+ // For now, we'll create a simple test that validates the logic++ tests := []struct {+ plan v1pb.PlanType+ expectedDays int+ }{+ {+ plan: v1pb.PlanType_FREE,+ expectedDays: 0, // No access+ },+ {+ plan: v1pb.PlanType_TEAM,+ expectedDays: 7, // 7 days retention+ },+ {+ plan: v1pb.PlanType_ENTERPRISE,+ expectedDays: -1, // Unlimited+ },+ }++ for _, tt := range tests {+ t.Run(tt.plan.String(), func(t *testing.T) {+ // This test documents the expected behavior+ // In a real test, we would mock the LicenseService and test GetAuditLogRetentionDays+ t.Logf("Plan %s should have %d days retention", tt.plan, tt.expectedDays)+ })+ }+}++func TestRetentionCutoffCalculation(t *testing.T) {+ now := time.Now()++ tests := []struct {+ name string+ retentionDays int+ expectNilCutoff bool+ description string+ }{+ {+ name: "Free plan (0 days)",+ retentionDays: 0,+ expectNilCutoff: true,+ description: "Free plan should return nil cutoff",+ },+ {+ name: "Team plan (7 days)",+ retentionDays: 7,+ expectNilCutoff: false,+ description: "Team plan should return cutoff 7 days ago",+ },+ {+ name: "Enterprise plan (unlimited)",+ retentionDays: -1,+ expectNilCutoff: true,+ description: "Enterprise plan should return nil cutoff",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ // Calculate expected cutoff+ var expectedCutoff *time.Time+ if tt.retentionDays > 0 {+ cutoff := now.AddDate(0, 0, -tt.retentionDays)+ expectedCutoff = &cutoff+ }++ if tt.expectNilCutoff {+ assert.Nil(t, expectedCutoff, tt.description)+ } else {+ require.NotNil(t, expectedCutoff, tt.description)+ // Verify the cutoff is approximately correct+ daysDiff := now.Sub(*expectedCutoff).Hours() / 24+ assert.InDelta(t, float64(tt.retentionDays), daysDiff, 0.1, "Cutoff should be approximately %d days ago", tt.retentionDays)+ }+ })+ }+}diff --git a/backend/enterprise/license.go b/backend/enterprise/license.goindex 0fb31e0a19..5e4e44c38d 100644--- a/backend/enterprise/license.go+++ b/backend/enterprise/license.go@@ -189,3 +189,33 @@ func (s *LicenseService) RefreshCache(ctx context.Context) {s.mu.Unlock()s.LoadSubscription(ctx)}++// GetAuditLogRetentionDays returns the audit log retention period in days for the current plan.+// Returns:+// - 0: No access (FREE plan)+// - positive number: Days of retention (TEAM plan = 7 days)+// - -1: Unlimited retention (ENTERPRISE plan)+func (s *LicenseService) GetAuditLogRetentionDays() int {+ plan := s.GetEffectivePlan()+ switch plan {+ case v1pb.PlanType_FREE:+ return 0+ case v1pb.PlanType_TEAM:+ return 7 // 7 days retention for TEAM plan+ case v1pb.PlanType_ENTERPRISE:+ return -1 // Unlimited+ default:+ return 0+ }+}++// GetAuditLogRetentionCutoff returns the earliest timestamp for accessible audit logs.+// Returns nil for unlimited retention (ENTERPRISE plan) or no access (FREE plan).+func (s *LicenseService) GetAuditLogRetentionCutoff() *time.Time {+ days := s.GetAuditLogRetentionDays()+ if days <= 0 {+ return nil // Either no access (0) or unlimited (-1)+ }+ cutoff := time.Now().AddDate(0, 0, -days)+ return &cutoff+}