Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore: change sql parameter for custom roles to be a(name,org) tuple#13480

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Emyrk merged 7 commits intomainfromstevenmasley/org_roles_query_args
Jun 6, 2024

Conversation

Emyrk
Copy link
Member

@EmyrkEmyrk commentedJun 5, 2024
edited
Loading

What this does

When looking up a role in the database, it requires aname andorganization_id. Before this change I was usingname[:<org_id] as a comparison string, but this is obviously not ideal.

I found a way to instead pass a slice of(name, org_id) tuples as the search filter. This allows 1 query to grab all org roles across all orgs, and site roles in 1 query.

This is used in APIKeyMW when pulling a user's roles via thisrolestore.Expand():

lookupArgs=append(lookupArgs, database.NameOrganizationPair{
Name:roleName,
OrganizationID:parsedOrgID,
})
}
// If some roles are missing from the database, they are omitted from
// the expansion. These roles are no-ops. Should we raise some kind of
// warning when this happens?
dbroles,err:=db.CustomRoles(ctx, database.CustomRolesParams{
LookupRoles:lookupArgs,
ExcludeOrgRoles:false,
OrganizationID:uuid.Nil,
})

How this is done in sqlc

  1. Create a custom type with the rightValue() function to return a tuple literal (go does not have native tuples).

// NameOrganizationPair is used as a lookup tuple for custom role rows.
typeNameOrganizationPairstruct {
Namestring`db:"name" json:"name"`
// OrganizationID if unset will assume a null column value
OrganizationID uuid.UUID`db:"organization_id" json:"organization_id"`
}
func (*NameOrganizationPair)Scan(_interface{})error {
returnxerrors.Errorf("this should never happen, type 'NameOrganizationPair' should only be used as a parameter")
}
// Value returns the tuple **literal**
// To get the literal value to return, you can use the expression syntax in a psql
// shell.
//
//SELECT ('customrole'::text,'ece79dac-926e-44ca-9790-2ff7c5eb6e0c'::uuid);
//To see 'null' option. Using the nil uuid as null to avoid empty string literals for null.
//SELECT ('customrole',00000000-0000-0000-0000-000000000000);
//
// This value is usually used as an array, NameOrganizationPair[]. You can see
// what that literal is as well, with proper quoting.
//
//SELECT ARRAY[('customrole'::text,'ece79dac-926e-44ca-9790-2ff7c5eb6e0c'::uuid)];
func (aNameOrganizationPair)Value() (driver.Value,error) {
returnfmt.Sprintf(`(%s,%s)`,a.Name,a.OrganizationID.String()),nil
}

  1. Define the type in a migration, so that SQLc and Postgres know the type:

CREATETYPEname_organization_pairAS (nametext, organization_id uuid);

  1. Do an override insqlc.yaml because SQLc does not have the functionality to interpret these types:

-db_type:"name_organization_pair"
go_type:
type:"NameOrganizationPair"

The result is a really nice auto-generated param

typeCustomRolesParamsstruct {
LookupRoles []NameOrganizationPair`db:"lookup_roles" json:"lookup_roles"`
ExcludeOrgRolesbool`db:"exclude_org_roles" json:"exclude_org_roles"`
OrganizationID uuid.UUID`db:"organization_id" json:"organization_id"`
}

Tested

A series of tests to verify it all works:

funcTestReadCustomRoles(t*testing.T) {

DB Logging

DB logging was super helpful doing this:a305e70. I reverted the commit because it brings in an extra dependency. We should consider keeping something like this.

Comment on lines +12 to +13
-- Using 'coalesce' to avoid troubles with null literals being an empty string.
(name, coalesce(organization_id,'00000000-0000-0000-0000-000000000000' ::uuid))= ANY (@lookup_roles::name_organization_pair[])
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Maybe we can bringNULL back, but for the life of me I could not get it to work. I think it has to do with my understanding of representingNULL as a literal

@EmyrkEmyrk marked this pull request as ready for reviewJune 6, 2024 00:08
@EmyrkEmyrk changed the titlechore: sql parameter to custom roles to be a (name,org) tuplechore: change sql parameter for custom roles to be a (name,org) tupleJun 6, 2024
@EmyrkEmyrk changed the titlechore: change sql parameter for custom roles to be a (name,org) tuplechore: change sql parameter for custom roles to be a(name,org) tupleJun 6, 2024
@EmyrkEmyrk requested review frommafredri andcoadlerJune 6, 2024 00:13
Copy link
Contributor

@coadlercoadler left a comment

Choose a reason for hiding this comment

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

Definitely seems like an improvement 👍, don't really have any issues

//
//SELECT ARRAY[('customrole'::text,'ece79dac-926e-44ca-9790-2ff7c5eb6e0c'::uuid)];
func (aNameOrganizationPair)Value() (driver.Value,error) {
returnfmt.Sprintf(`(%s,%s)`,a.Name,a.OrganizationID.String()),nil
Copy link
Contributor

Choose a reason for hiding this comment

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

For the name here probably want to usehttps://pkg.go.dev/github.com/lib/pq#QuoteLiteral unless I'm misunderstanding. This seems like direct sql injection.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

It cannot be quoted, I was trying that before. This is a parameter argument still, so it will replace?#.

I had considered a sql injection, but can a sql injection happen at a parameter substitution?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, good point actually. This is fine, sry

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I just tried to send in some invalid sql with-- and; as the names, and the query still works.

ThedefaultValueConverter which implments this function for all primitive types just does the literal string value without quotes or escapes:https://github.com/golang/go/blob/master/src/database/sql/driver/types.go#L291-L293

That is the function behavior I am overriding. 👍

@EmyrkEmyrk merged commite2b330f intomainJun 6, 2024
@EmyrkEmyrk deleted the stevenmasley/org_roles_query_args branchJune 6, 2024 20:36
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsJun 6, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@mafredrimafredriAwaiting requested review from mafredri

1 more reviewer

@coadlercoadlercoadler approved these changes

Reviewers whose approvals may not affect merge requirements
Assignees

@EmyrkEmyrk

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@Emyrk@coadler

[8]ページ先頭

©2009-2025 Movatter.jp