Skip to content

chore: convert dbauthz tests to also run with Postgres #15862

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 5 commits into from
Jan 8, 2025

Conversation

hugodutka
Copy link
Contributor

Another PR to address #15109.

  • adds the DisableForeignKeysAndTriggers utility, which simplifies converting tests from in-mem to postgres
  • converts the dbauthz test suite to pass on both the in-mem db and Postgres

@hugodutka hugodutka marked this pull request as ready for review December 13, 2024 13:34
@hugodutka hugodutka requested a review from Emyrk December 13, 2024 13:34
@Emyrk
Copy link
Member

Emyrk commented Dec 20, 2024

I will take a look a this today.

I really regret not making the DB a mock 😢. That would be the best, but fixing some ~400 tests to a mock sounds like the absolute worst thing.

We might be able to leverage this .Returns function to mock responses and make the diffs a bit smaller...

// Returns is optional. If it is never called, it will not be asserted.
func (m *expects) Returns(rets ...any) *expects {
m.outputs = values(rets...)
return m
}

Copy link
Member

@Emyrk Emyrk left a comment

Choose a reason for hiding this comment

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

The tests now take ~18s on my machine. not that bad given there is 1973 test cases 🤯

If we remove the actual authorizer from the fake, then this LGTM 👍

At some flex week in the future, I'll look to removing the need for the DB and we can remove the new query added to remove foreign keys.

Comment on lines 361 to 362
RegoAuthorizer *rbac.RegoAuthorizer
sqlFilter string
Copy link
Member

Choose a reason for hiding this comment

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

Do we include RegoAuthorizer only to get the CompileToSQL function?

I ask because the FakeAuthorizer should really not use the rego authorizer at all.

t1 := dbgen.Template(s.T(), db, database.Template{})
check.Args(database.UpdateWorkspacesTTLByTemplateIDParams{
TemplateID: t1.ID,
}).Asserts(t1, policy.ActionUpdate)
}))
s.Run("UpdateTemplateActiveVersionByID", s.Subtest(func(db database.Store, check *expects) {
dbtestutil.DisableForeignKeysAndTriggers(s.T(), db)
Copy link
Member

Choose a reason for hiding this comment

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

We do have this for some common cases:

		tv := dbfake.TemplateVersion(s.T(), db).Do()
		check.Args(database.UpdateTemplateActiveVersionByIDParams{
			ID:              tv.Template.ID,
			ActiveVersionID: tv.TemplateVersion.ID,
		}).Asserts(tv.Template, policy.ActionUpdate).Returns()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh nice, I wasn't aware. It's going to come in handy in future conversions. Since you'll be working on removing the need for the DB altogether, I suppose it's fine if we leave the code as is without converting to use dbfake?

Copy link
Member

Choose a reason for hiding this comment

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

@hugodutka yea, it is fine 👍

Mainly just showing dbfake is a nice spot for dbgen "tooling"

Comment on lines 405 to 421
func (*fakePreparedAuthorizer) CompileToSQL(_ context.Context, _ regosql.ConvertConfig) (string, error) {
return "not a valid sql string", nil
func (f *fakePreparedAuthorizer) CompileToSQL(ctx context.Context, cfg regosql.ConvertConfig) (string, error) {
if f.Original.sqlFilter != "" {
return f.Original.sqlFilter, nil
}
return f.PreparedRegoAuthorizer.CompileToSQL(ctx, cfg)
Copy link
Member

Choose a reason for hiding this comment

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

I'd remove the real rego authorizer and just always return true as the string literal for the response as the default. Would that work?

fakeAuthorizer := &coderdtest.FakeAuthorizer{}
db, _ := dbtestutil.NewDB(t)
fakeAuthorizer := &coderdtest.FakeAuthorizer{
RegoAuthorizer: rbac.NewAuthorizer(prometheus.NewRegistry()),
Copy link
Member

Choose a reason for hiding this comment

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

As mentioned above, lets remove the need for a real authorizer.

Comment on lines +223 to +224
if testCase.outOfOrder {
rec.AssertOutOfOrder(s.T(), actor, pairs...)
Copy link
Member

Choose a reason for hiding this comment

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

👍

@github-actions github-actions bot added the stale This issue is like stale bread. label Dec 31, 2024
@matifali matifali removed the stale This issue is like stale bread. label Jan 2, 2025
@hugodutka hugodutka force-pushed the hugodutka/convert-dbauthz-tests-to-pg branch from 2f188e8 to 4ab19f7 Compare January 8, 2025 12:34
@hugodutka
Copy link
Contributor Author

@Emyrk thanks for the tip about removing the RegoAuthorizer from FakeAuthorizer. I didn't take the time to fully understand the authorization system and wasn't sure whether f.PreparedRegoAuthorizer.CompileToSQL(ctx, cfg) could just be replaced by "TRUE". I did the replacement and everything works. 👍

Let me know if this looks good to you.

Copy link
Member

@Emyrk Emyrk left a comment

Choose a reason for hiding this comment

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

The SQL handling is good 👍

Nice!

@hugodutka hugodutka merged commit 106b1cd into main Jan 8, 2025
30 checks passed
@hugodutka hugodutka deleted the hugodutka/convert-dbauthz-tests-to-pg branch January 8, 2025 15:22
@github-actions github-actions bot locked and limited conversation to collaborators Jan 8, 2025
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.

3 participants