Skip to content

fix(enterprise/dbcrypt): do not skip deleted users when encrypting or deleting #9694

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

Merged
merged 11 commits into from
Sep 15, 2023

Conversation

johnstcn
Copy link
Member

This PR fixes an issue where deleted users are not taken into account when encrypting or decrypting data. GetUsers will by default omit soft-deleted users.

  • Broadens scope of data generation in TestServerDBCrypt over all user login types, statuses, and deletion status.
  • Adds support for specifying user status / user deletion status in dbgen
  • Adds more comprehensive logging in TestServerDBCrypt upon test failure
  • Modifies dbcrypt to query all active userIDs directly.

@johnstcn johnstcn self-assigned this Sep 15, 2023
@johnstcn johnstcn marked this pull request as ready for review September 15, 2023 10:53
FROM users u
LEFT OUTER JOIN user_links ul ON u.id = ul.user_id
LEFT OUTER JOIN git_auth_links gal ON u.id = gal.user_id
ORDER BY u.created_at ASC;`)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should add a test that this function works as expected since it's static and will only be run on failure, meaning it will most likely be outdated by the time it's needed. 😆

Perhaps this could be motivation for a testqueries package. Similar to what I did here: #9519 to generate a new (slim) package that can be wrapped around sql.DB.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd definitely be in favour of this; I'm fairly sure there are at least a couple of queries that only ever get run in unit tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe we should add a test that this function works as expected since it's static and will only be run on failure, meaning it will most likely be outdated by the time it's needed. 😆

Golden files?

Copy link
Member

Choose a reason for hiding this comment

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

I think verifying a few fields in the test is enough. Otherwise you need to stabilize the generated IDs.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think instead of making that query more complex I'll go ahead and add a follow-up PR to dump the entire test database on failure (optional, opt-in). Less work, less fuss, more benefit.

@@ -240,6 +240,14 @@ func User(t testing.TB, db database.Store, orig database.User) database.User {
})
require.NoError(t, err, "user last seen")
}

if orig.Deleted {
err = db.UpdateUserDeletedByID(genCtx, database.UpdateUserDeletedByIDParams{
Copy link
Member

Choose a reason for hiding this comment

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

FYI There is also dormant status, hopefully it does not change anything.

Copy link
Member Author

Choose a reason for hiding this comment

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

All combinations of login_type, status, and deleted should be covered by this test.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm fairly sure user dormancy and deletion are completely orthogonal i.e. both can be set independently.

@@ -41,15 +44,38 @@ func TestServerDBCrypt(t *testing.T) {
})
db := database.New(sqlDB)

t.Cleanup(func() {
if t.Failed() {
t.Logf("Dumping data due to failed test. I hope you find what you're looking for!")
Copy link
Member

Choose a reason for hiding this comment

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

oh, this should be a general pattern in coder/coder. Great idea!

Copy link
Member

Choose a reason for hiding this comment

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

Indeed. Actually I'm wondering if it would make sense to have/write a full psql dump as well (or instead). It'd be great if the test author didn't need to consider all failure scenarios and the information would always be there in full (and repeatable by importing dump).

Copy link
Member Author

Choose a reason for hiding this comment

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

I can add that as a follow-up PR! Maybe something like dbtestutil.WithDumpOnFailure(t)?

}
return users
}

func dumpUsers(t *testing.T, db *sql.DB) {
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 limit the OS test env for this test?

Copy link
Member Author

Choose a reason for hiding this comment

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

If there were the possibility of either leaking sensitive information or performing destructive database changes, I would say yes. I don't believe either of those are the case here, though.

Copy link
Member Author

Choose a reason for hiding this comment

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

In any case, I'll be removing this in a follow-up PR and replacing it with a more general "pg_dump on test failure".

@mtojek mtojek self-requested a review September 15, 2023 14:08
@johnstcn johnstcn merged commit 72dff7f into main Sep 15, 2023
@johnstcn johnstcn deleted the cj/dbcrypt-deleted-users branch September 15, 2023 14:09
@github-actions github-actions bot locked and limited conversation to collaborators Sep 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants