Skip to content

Conversation

coadler
Copy link
Contributor

@coadler coadler commented Apr 15, 2022

Implements: #467

@coadler coadler self-assigned this Apr 15, 2022
@codecov
Copy link

codecov bot commented Apr 15, 2022

Codecov Report

Merging #1046 (d0ebb78) into main (a2dd618) will increase coverage by 0.12%.
The diff coverage is 85.89%.

@@            Coverage Diff             @@
##             main    #1046      +/-   ##
==========================================
+ Coverage   66.24%   66.36%   +0.12%     
==========================================
  Files         255      263       +8     
  Lines       16120    16413     +293     
  Branches      156      156              
==========================================
+ Hits        10678    10893     +215     
- Misses       4335     4395      +60     
- Partials     1107     1125      +18     
Flag Coverage Δ
unittest-go-macos-latest 53.88% <85.89%> (?)
unittest-go-postgres- 65.76% <85.89%> (+0.11%) ⬆️
unittest-go-ubuntu-latest 56.44% <85.89%> (+0.10%) ⬆️
unittest-go-windows-2022 53.47% <85.89%> (?)
unittest-js 67.28% <ø> (ø)
Impacted Files Coverage Δ
coderd/audit/table.go 77.77% <77.77%> (ø)
coderd/audit/diff.go 86.56% <86.56%> (ø)
coderd/coderd.go 97.48% <100.00%> (ø)
coderd/users.go 60.36% <100.00%> (ø)
provisionersdk/serve.go 35.13% <0.00%> (-8.11%) ⬇️
provisionerd/provisionerd.go 75.70% <0.00%> (-0.81%) ⬇️
provisioner/echo/serve.go 56.80% <0.00%> (ø)
cli/ssh_windows.go 0.00% <0.00%> (ø)
cli/cliui/cliui_darwin.go 0.00% <0.00%> (ø)
pty/start_windows.go 66.95% <0.00%> (ø)
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a2dd618...d0ebb78. Read the comment docs.

Comment on lines 39 to 55
&database.User{}: {
"id": ActionIgnore, // Never changes.
"email": ActionAuditable, // A user can edit their email.
"name": ActionAuditable, // A user can edit their name.
"revoked": ActionAuditable, // An admin can revoke a user. This is different from deletion, which is implicit.
"login_type": ActionAuditable, // An admin can update the login type of a user.
"hashed_password": ActionSecret, // A user can change their own password.
"created_at": ActionIgnore, // Never changes.
"updated_at": ActionIgnore, // Changes, but is implicit and not helpful in a diff.
"username": ActionIgnore, // A user cannot change their username.
},
&database.Workspace{}: {
"id": ActionIgnore, // Never changes.
"created_at": ActionIgnore, // Never changes.
"updated_at": ActionIgnore, // Changes, but is implicit and not helpful in a diff.
"owner_id": ActionIgnore, // We don't allow workspaces to change ownership.
"template_id": ActionIgnore, // We don't allow workspaces to change templates.
"deleted": ActionIgnore, // Changes, but is implicit when a delete event is fired.
"name": ActionIgnore, // We don't allow workspaces to change names.
"autostart_schedule": ActionAuditable, // Autostart schedules are directly editable by users.
"autostop_schedule": ActionAuditable, // Autostart schedules are directly editable by users.
},
Copy link
Member

Choose a reason for hiding this comment

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

Big fan of how this is centralized.

Comment on lines 51 to 57
"id": ActionIgnore, // Never changes.
"created_at": ActionIgnore, // Never changes.
"updated_at": ActionIgnore, // Changes, but is implicit and not helpful in a diff.
"owner_id": ActionIgnore, // We don't allow workspaces to change ownership.
"template_id": ActionIgnore, // We don't allow workspaces to change templates.
"deleted": ActionIgnore, // Changes, but is implicit when a delete event is fired.
"name": ActionIgnore, // We don't allow workspaces to change names.
Copy link
Member

Choose a reason for hiding this comment

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

Should we do ignore by default? I assume most actions won't need to be audited, so adding them manually won't be a big deal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Theoretically it is because it's the first iota. But, I'd like to make this a runtime error if any fields aren't in this list so we have a definitive list for generating the docs later.

Copy link
Member

Choose a reason for hiding this comment

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

I suppose that's better than implicit because when editing the schema it becomes impossible to not decide whether something should be audited.

Copy link
Member

@kylecarbs kylecarbs left a comment

Choose a reason for hiding this comment

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

Super clean. I'm a big fan of this direction 🥳🥳🥳

// ActionIgnore ignores diffing for the field.
ActionIgnore = iota
// ActionAuditable includes the value in the diff if the value changed.
ActionAuditable
Copy link
Member

Choose a reason for hiding this comment

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

Thoughts on Track, Store, or Log instead? I'm just imaging the caller experience of audit.Track or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I like Track better 👍

@coadler coadler marked this pull request as ready for review April 22, 2022 20:02
@coadler coadler requested a review from kylecarbs April 22, 2022 20:08
Copy link
Member

@kylecarbs kylecarbs left a comment

Choose a reason for hiding this comment

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

Two minor nits on naming, but LGTM!

// TODO: this might need to be in the database package.
type DiffMap map[string]interface{}

func Empty[T Auditable]() T {
Copy link
Member

Choose a reason for hiding this comment

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

Is this just used for tests? Might be better to toss it in the test pkg.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I intend for this to be used by external callers, mainly because you can't pass nil anymore since we don't accept pointers. I thought it was a nice self describing replacement for nil.

Copy link
Member

Choose a reason for hiding this comment

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

Ahh I see. That makes sense!

@coadler coadler enabled auto-merge (squash) April 25, 2022 18:54
@coadler coadler merged commit 2a57ea7 into main Apr 25, 2022
@coadler coadler deleted the colin/audit-pkg branch April 25, 2022 18:58
@misskniss misskniss added this to the V2 Beta milestone May 15, 2022
kylecarbs pushed a commit that referenced this pull request Jun 10, 2022
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