Skip to content
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

CBG-4046 add attachment and create, update, delete events #6988

Merged
merged 27 commits into from
Jul 23, 2024

Conversation

torcolvin
Copy link
Collaborator

@torcolvin torcolvin commented Jul 19, 2024

I added tests for REST api and didn't add tests for blip code.

Pre-review checklist

  • Removed debug logging (fmt.Print, log.Print, ...)
  • Logging sensitive data? Make sure it's tagged (e.g. base.UD(docID), base.MD(dbName))
  • Updated relevant information in the API specifications (such as endpoint descriptions, schemas, ...) in docs/api

Integration Tests

@torcolvin torcolvin force-pushed the CBG-4046-attachment-events branch from a066f72 to d037861 Compare July 19, 2024 17:14
Base automatically changed from CBG-4046-read-events to main July 19, 2024 19:38
Copy link
Collaborator

@adamcfraser adamcfraser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall approach looks good, some questions about attachment name. Might merit unit tests to verify auditing of name is consistent across create/update/delete and via different pathways.

@torcolvin torcolvin assigned adamcfraser and unassigned torcolvin Jul 22, 2024
@torcolvin torcolvin requested a review from adamcfraser July 22, 2024 19:01
adamcfraser
adamcfraser previously approved these changes Jul 22, 2024
Copy link
Collaborator

@adamcfraser adamcfraser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks fine. Some comment and code style comments, but can leave those to come back to in future.

@torcolvin torcolvin changed the title CBG-4046 add attachment events CBG-4046 add attachment and create, update, delete events Jul 23, 2024
db/crud.go Outdated
if doc.IsDeleted() {
base.Audit(ctx, base.AuditIDDocumentDelete, base.AuditFields{
base.AuditFieldDocID: docid,
base.AuditFieldDocVersion: newRevID,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tombstones can have channels - feels like we should be including base.AuditFieldChannels here too.

},

optionalFieldGroups: []fieldGroup{
fieldGroupRequest, // this will be present everywhere except tests,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect this comment should match the one for Delete, since exceptions would be the same?

fieldGroupKeyspace,
},
optionalFieldGroups: []fieldGroup{
fieldGroupRequest, // this is not present on ISGR or import,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be included on AuditIDDocumentUpdate and AuditIDDocumentCreate (or omitted here)?

db/database.go Outdated
return base.UserLogCtx(ctx, "rosmar_noauth", base.UserDomainBuiltin, nil)
}
username, _, _ := dbCtx.BucketSpec.Auth.GetCredentials()
return base.UserLogCtx(ctx, username, base.UserDomainBuiltin, nil) // FIXME, should I pick up roles?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bbrks what's your take on this question?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed. Roles are passed for filtering only. In the case of builtin users, people can use the username based filtering if they don't want these audit events.

@@ -536,7 +536,8 @@ func (rt *RestTester) GetUserAdminAPI(username string) auth.PrincipalConfig {
// GetSingleTestDatabaseCollection will return a DatabaseCollection if there is only one. Depending on test environment configuration, it may or may not be the default collection.
func (rt *RestTester) GetSingleTestDatabaseCollection() (*db.DatabaseCollection, context.Context) {
c := db.GetSingleDatabaseCollection(rt.TB(), rt.GetDatabase())
return c, c.AddCollectionContext(rt.Context())
ctx := base.UserLogCtx(c.AddCollectionContext(rt.Context()), "gotest", base.UserDomainBuiltin, nil)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The UserLogCtx feels like it is getting set arbitrarily in a lot of places. Can you help me understand the general approach we're using to decide whether to set "gotest"? I would have thought a DatabaseCollection shouldn't have a user context (since it's not a DatabaseCollectionWithUser).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for code that works directly in a test: collection.Put(ctx, ..) needs a user and this provides a user. I wanted to make it clear that this wasn't a real user. This is for cases like import tests where we directly call functions outside of REST calls.

rest/doc_api.go Outdated
@@ -644,6 +648,10 @@ func (h *handler) handleGetLocalDoc() error {
}
value[db.BodyId] = "_local/" + docid
h.writeJSON(value)
base.Audit(h.ctx(), base.AuditIDDocumentRead, base.AuditFields{
base.AuditFieldDocID: docid,
base.AuditFieldDocVersion: value[db.BodyRev], // this value might not be populated if this is a doc that starts with _sync
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I don't think BodyRev is a required field for local docs. Can we just remove this?

events := jsonLines(rt.TB(), output)
countFound := 0
for _, event := range events {
// skip events that are not document read events
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// skip events that are not document read events
// skip events that are not the target eventID

@@ -974,6 +1120,22 @@ func requireDocumentReadEvents(rt *RestTester, output []byte, docID string, docV
require.Equal(rt.TB(), docVersions, docVersionsFound)
}

// requireAttachmentEvents validates that create attachment events
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the idea that this expects that eventID is one of the attachment crud event IDs?

@bbrks bbrks assigned bbrks and unassigned torcolvin Jul 23, 2024
@torcolvin torcolvin mentioned this pull request Jul 23, 2024
6 tasks
@bbrks bbrks assigned torcolvin and unassigned bbrks Jul 23, 2024
rest/bulk_api.go Outdated
Comment on lines 461 to 473
if atts, ok := body[db.BodyAttachments]; ok && atts != nil {
attsMap, ok := atts.(db.AttachmentsMeta)
if !ok {
base.WarnfCtx(h.ctx(), "Unexpected format of attachments in the body %+v", atts)
}
for attachment := range attsMap {
base.Audit(h.ctx(), base.AuditIDAttachmentRead, base.AuditFields{
base.AuditFieldDocID: docid,
base.AuditFieldDocVersion: revid,
base.AuditFieldAttachmentID: attachment,
})
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only reading metadata of the attachment, unless includeAttachments is set.

Does this need to be conditional?

@@ -0,0 +1,14 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd rather be more deliberate about switching to the use of embed and standalone json files for testing, instead of just adding this single standalone file - would like to think about organization, packaging, etc. Any reason this can't be inline with the test file, like we do in other spots?

@@ -2355,7 +2399,7 @@ func (db *DatabaseCollectionWithUser) DeleteDoc(ctx context.Context, docid strin
}

// Purges a document from the bucket (no tombstone)
func (db *DatabaseCollectionWithUser) Purge(ctx context.Context, key string) error {
func (db *DatabaseCollectionWithUser) Purge(ctx context.Context, key string, needsAudit bool) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing any cases that call this with needsAudit=false. Are there such cases?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The single case is

purgeErr := collection.Purge(ctx, tombstonesRow.Id)

@torcolvin torcolvin assigned adamcfraser and unassigned torcolvin Jul 23, 2024
adamcfraser
adamcfraser previously approved these changes Jul 23, 2024
@adamcfraser adamcfraser merged commit 0f05fda into main Jul 23, 2024
38 checks passed
@adamcfraser adamcfraser deleted the CBG-4046-attachment-events branch July 23, 2024 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants