Skip to content

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

feat: store audit log parent (#11771)

p0ny8b875e9cc994
diff --git a/backend/api/v1/audit.go b/backend/api/v1/audit.go
index f62d72f56b..e7928b7e20 100644
--- a/backend/api/v1/audit.go
+++ b/backend/api/v1/audit.go
@@ -55,19 +55,39 @@ func (in *AuditInterceptor) AuditInterceptor(ctx context.Context, request any, s
st, _ := status.FromError(rerr)
- p := &storepb.AuditLog{
- Method: serverInfo.FullMethod,
- Resource: getRequestResource(request),
- Severity: storepb.AuditLog_INFO,
- User: user,
- Request: requestString,
- Response: responseString,
- Status: st.Proto(),
+ projectIDs, ok := common.GetProjectIDsFromContext(ctx)
+ if !ok {
+ return errors.Errorf("failed to get projects ids from context")
+ }
+ var parents []string
+ if len(projectIDs) == 0 {
+ workspaceID, err := in.store.GetWorkspaceID(ctx)
+ if err != nil {
+ return errors.Wrapf(err, "failed to get workspace id")
+ }
+ parents = append(parents, common.FormatWorkspace(workspaceID))
+ } else {
+ for _, projectID := range projectIDs {
+ parents = append(parents, common.FormatProject(projectID))
+ }
}
- if err := in.store.CreateAuditLog(ctx, p); err != nil {
- return errors.Wrapf(err, "failed to create audit log")
+ for _, parent := range parents {
+ p := &storepb.AuditLog{
+ Parent: parent,
+ Method: serverInfo.FullMethod,
+ Resource: getRequestResource(request),
+ Severity: storepb.AuditLog_INFO,
+ User: user,
+ Request: requestString,
+ Response: responseString,
+ Status: st.Proto(),
+ }
+ if err := in.store.CreateAuditLog(ctx, p); err != nil {
+ return errors.Wrapf(err, "failed to create audit log")
+ }
}
+
return nil
}(); err != nil {
slog.Warn("audit interceptor: failed to create audit log", log.BBError(err))
diff --git a/backend/api/v1/ctx.go b/backend/api/v1/ctx.go
index a35b01a9e7..3478c9b2b3 100644
--- a/backend/api/v1/ctx.go
+++ b/backend/api/v1/ctx.go
@@ -52,8 +52,7 @@ func (p *ContextProvider) StreamInterceptor(request any, ss grpc.ServerStream, s
func (p *ContextProvider) do(ctx context.Context, fullMethod string, req any) ([]string, error) {
switch fullMethod {
- // below are "workspace-level" permissions.
- // we don't have to go down to the project level.
+ // skip workspace-level methods
case
v1pb.InstanceService_GetInstance_FullMethodName,
v1pb.InstanceRoleService_GetInstanceRole_FullMethodName,
@@ -116,6 +115,8 @@ func (p *ContextProvider) do(ctx context.Context, fullMethod string, req any) ([
return nil, nil
case
+ v1pb.SQLService_Query_FullMethodName,
+ v1pb.SQLService_Export_FullMethodName,
v1pb.DatabaseService_GetDatabase_FullMethodName,
v1pb.DatabaseService_UpdateDatabase_FullMethodName,
v1pb.DatabaseService_BatchUpdateDatabases_FullMethodName,
@@ -130,7 +131,7 @@ func (p *ContextProvider) do(ctx context.Context, fullMethod string, req any) ([
v1pb.DatabaseService_AdviseIndex_FullMethodName,
v1pb.DatabaseService_ListChangeHistories_FullMethodName,
v1pb.DatabaseService_GetChangeHistory_FullMethodName:
- return p.getProjectIDsForDatabaseService(ctx, req)
+ return p.getProjectIDsForDatabase(ctx, req)
case
v1pb.VCSConnectorService_CreateVCSConnector_FullMethodName,
@@ -433,11 +434,15 @@ func (*ContextProvider) getProjectIDsForProjectService(_ context.Context, req an
return uniq(projectIDs), nil
}
-func (p *ContextProvider) getProjectIDsForDatabaseService(ctx context.Context, req any) ([]string, error) {
+func (p *ContextProvider) getProjectIDsForDatabase(ctx context.Context, req any) ([]string, error) {
var projectIDs []string
var databaseNames []string
switch r := req.(type) {
+ case *v1pb.QueryRequest:
+ databaseNames = append(databaseNames, r.GetName())
+ case *v1pb.ExportRequest:
+ databaseNames = append(databaseNames, r.GetName())
case *v1pb.GetDatabaseRequest:
databaseNames = append(databaseNames, r.GetName())
case *v1pb.SyncDatabaseRequest:
diff --git a/backend/common/resource_name.go b/backend/common/resource_name.go
index 7d1fd53d44..d4fac01c9f 100644
--- a/backend/common/resource_name.go
+++ b/backend/common/resource_name.go
@@ -551,6 +551,10 @@ func GetNameParentTokens(name string, tokenPrefixes ...string) ([]string, error)
return tokens, nil
}
+func FormatWorkspace(id string) string {
+ return fmt.Sprintf("%s%s", WorkspacePrefix, id)
+}
+
func FormatProject(id string) string {
return fmt.Sprintf("%s%s", ProjectNamePrefix, id)
}