-
Notifications
You must be signed in to change notification settings - Fork 8
Add identity audit logging #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
darshana-v
wants to merge
3
commits into
main
Choose a base branch
from
identity-audit-log
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package postgres | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "time" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/uptrace/bun" | ||
| ) | ||
|
|
||
| // IdentityAuditLog is the DB model for the identity_audit_logs table. | ||
| type IdentityAuditLog struct { | ||
| bun.BaseModel `bun:"table:identity_audit_logs,alias:ial"` | ||
|
|
||
| ID string `bun:"id,pk,type:uuid,default:gen_random_uuid()"` | ||
| AccountID string `bun:"account_id,notnull"` | ||
| ProjectID string `bun:"project_id,notnull"` | ||
| CallerUserID string `bun:"caller_user_id,notnull"` | ||
| IdentityID string `bun:"identity_id,notnull"` | ||
| Action string `bun:"action,notnull"` | ||
| Status string `bun:"status,notnull"` | ||
| OldData json.RawMessage `bun:"old_data,type:jsonb"` | ||
| NewData json.RawMessage `bun:"new_data,type:jsonb"` | ||
| CreatedAt time.Time `bun:"created_at,notnull,default:current_timestamp"` | ||
| } | ||
|
|
||
| // AuditRepository handles writes to the identity_audit_logs table. | ||
| type AuditRepository struct { | ||
| db *bun.DB | ||
| } | ||
|
|
||
| // NewAuditRepository creates a new AuditRepository. | ||
| func NewAuditRepository(db *bun.DB) *AuditRepository { | ||
| return &AuditRepository{db: db} | ||
| } | ||
|
|
||
| // Insert writes a single audit record. | ||
| func (r *AuditRepository) Insert(ctx context.Context, accountID, projectID, callerUserID, identityID, action, status string, oldData, newData json.RawMessage) error { | ||
| entry := &IdentityAuditLog{ | ||
| ID: uuid.New().String(), | ||
| AccountID: accountID, | ||
| ProjectID: projectID, | ||
| CallerUserID: callerUserID, | ||
| IdentityID: identityID, | ||
| Action: action, | ||
| Status: status, | ||
| OldData: oldData, | ||
| NewData: newData, | ||
| CreatedAt: time.Now().UTC(), | ||
| } | ||
| _, err := r.db.NewInsert().Model(entry).Exec(ctx) | ||
| return err | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| -- 008_identity_audit_logs.down.sql | ||
| DROP TABLE IF EXISTS identity_audit_logs; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| -- 008_identity_audit_logs.up.sql | ||
| -- Records who performed each identity create/update/delete operation. | ||
| -- caller_user_id is the acting user (from X-User-ID header), not the identity's owner_user_id. | ||
|
|
||
| CREATE TABLE IF NOT EXISTS identity_audit_logs ( | ||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
| account_id VARCHAR(255) NOT NULL, | ||
| project_id VARCHAR(255) NOT NULL DEFAULT '', | ||
| caller_user_id VARCHAR(255) NOT NULL DEFAULT '', | ||
| identity_id VARCHAR(255) NOT NULL DEFAULT '', | ||
| action VARCHAR(50) NOT NULL, | ||
| status VARCHAR(50) NOT NULL DEFAULT 'SUCCESS', | ||
| old_data JSONB, | ||
| new_data JSONB, | ||
| created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS idx_identity_audit_logs_tenant | ||
| ON identity_audit_logs (account_id, project_id); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS idx_identity_audit_logs_identity | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove second and fourth indexes |
||
| ON identity_audit_logs (identity_id) | ||
| WHERE identity_id != ''; | ||
|
|
||
| CREATE INDEX IF NOT EXISTS idx_identity_audit_logs_caller | ||
| ON identity_audit_logs (caller_user_id) | ||
| WHERE caller_user_id != ''; | ||
|
|
||
| CREATE INDEX IF NOT EXISTS idx_identity_audit_logs_created_at | ||
| ON identity_audit_logs (created_at DESC); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.