Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Bosorawis domain iam implement role grant scopes all #5701

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

Open
wants to merge 144 commits into
base: llb-normalized-grants
Choose a base branch
from

Conversation

bosorawis
Copy link
Collaborator

@bosorawis bosorawis commented Apr 29, 2025

Change implementation of following methods to use new grants data model

  • AddRoleGrantScopes
  • DeleteRoleGrantScopes
  • SetRoleGrantScopes

@bosorawis bosorawis marked this pull request as ready for review May 2, 2025 00:15
@bosorawis bosorawis requested a review from a team as a code owner May 2, 2025 00:15

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

Comment on lines 48 to 56
// thisGrantScope return value of `GrantScopeThis` column as *RoleGrantScope.
// Prior to the grants refactor, `this` grant scope is granted to a role by
// inserting a row to 'role_grant_scope' table, but we've moved on to storing
// 'this' grant as a dedicated column in the type-specific role tables
thisGrantScope() *RoleGrantScope

// specialGrantScope returns special grant scopes ['descendants', 'children'] if available.
// returns nil, false if special grant scope is 'individual'
specialGrantScope() (*RoleGrantScope, bool)
Copy link
Collaborator

@dkanney dkanney May 2, 2025

Choose a reason for hiding this comment

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

Since we're going with a setter/getter style here, I think it could be clearer if we prepend get to these last two methods, so we end up having:

// roleGrantScopeUpdater represents an internal scope type specific role structs that
// support grant scope columns. Currently this only applies to globalRole and orgRole
// this is used in SetRoleGrantScope, AddRoleGrantScope, DeleteRoleGrantScope
type roleGrantScopeUpdater interface {
	// setVersion sets value of `Version` of this role. This is used in
	// `repository_grant_scope` operations where version column of the associated role
	// has to increase when grant scopes list is changed (add/remove) which is done in a different table.
	// This version bump cannot be done by automatically with trigger and is being handled in the application code
	setVersion(version uint32)

	// setThisGrantScope sets value of `GrantThisRoleScope` of this role which control
	// whether this role has 'this' scope granted to it
	setThisGrantScope(grantThis bool)

	// getThisGrantScope return value of `GrantScopeThis` column as *RoleGrantScope.
	// Prior to the grants refactor, `this` grant scope is granted to a role by
	// inserting a row to 'role_grant_scope' table, but we've moved on to storing
	// 'this' grant as a dedicated column in the type-specific role tables
	getThisGrantScope() *RoleGrantScope

	// setSpecialGrantScope sets value of `GrantScope` column of this role.  The allowed values depends on the scope
	// that the role is in
	// 	- global-role: ['descendants', 'children', 'individual']
	// 	- org-role: ['children', 'individual']
	//	- project-role: [] (None)
	// This value controls whether special grant scope is granted to this role
	setSpecialGrantScope(specialGrant string)

	// getSpecialGrantScope returns special grant scopes ['descendants', 'children'] if available.
	// returns nil, false if special grant scope is 'individual'
	getSpecialGrantScope() (*RoleGrantScope, bool)
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel bad to "actually..." this discussion, but this is against the recommendation in Effective Go: https://go.dev/doc/effective_go#Getters. Personally I prefer not to have the "Get" prefix either 😅.

Comment on lines 51 to 52
type toRoleGrantScope interface {
roleGrantScope() *RoleGrantScope
Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel like this interface name and its method should be swapped. If an object can be a roleGrantScope, I'd expect it to implement a method called toRoleGrantScope() that converts it to a RoleGrantScope.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

renaming interface to roleGrantScoper which has a function calledroleGrantScope

roleGrantScopes := []*RoleGrantScope{}
if err := r.reader.SearchWhere(ctx, &roleGrantScopes, "role_id = ?", []any{roleId}); err != nil {
// Find existing grant scopes to find duplicate grants
roleGrantScopes, err := listRoleGrantScopes(ctx, r.reader, []string{roleId})
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: maybe call this

Suggested change
roleGrantScopes, err := listRoleGrantScopes(ctx, r.reader, []string{roleId})
originalGrantScopes, err := listRoleGrantScopes(ctx, r.reader, []string{roleId})

To be consistent with the map name below

Comment on lines 67 to 68
// allocation type specific role and manually bump version to ensure that version gets updated
// even when only individual grant scopes which are stored in separate tables are modified
Copy link
Collaborator

Choose a reason for hiding this comment

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

This sentence reads like it accidentally a word to me. Is this right?

Suggested change
// allocation type specific role and manually bump version to ensure that version gets updated
// even when only individual grant scopes which are stored in separate tables are modified
// Allocate a subtype-specific role and manually bump version to ensure that version gets updated
// even when only individual grant scopes, which are stored in separate tables, are modified.

Comment on lines 82 to 85
// finalSpecialGrantScope is used to pass into individualGlobalRoleGrantScope
// this only matters if the role is a global role and the grantScopes contains individual project scope
// because there are 2 possible values ['individual', 'children'].
// If there's no scope being added, we have to use the existing iam_role_<type>.grant_scope column
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit:

Suggested change
// finalSpecialGrantScope is used to pass into individualGlobalRoleGrantScope
// this only matters if the role is a global role and the grantScopes contains individual project scope
// because there are 2 possible values ['individual', 'children'].
// If there's no scope being added, we have to use the existing iam_role_<type>.grant_scope column
// finalSpecialGrantScope is used to pass into individualGlobalRoleGrantScope.
// This only matters if the role is a global role and the grantScopes contains an individual project
// scope, because there are 2 possible values: ['individual', 'children'].
// If there's no scope being added, we have to use the existing iam_role_<type>.grant_scope column

I don't think the grammar needs to be perfect in comments, but this was a bit confusing to read because of the lack of a full stop.

Comment on lines 48 to 56
// thisGrantScope return value of `GrantScopeThis` column as *RoleGrantScope.
// Prior to the grants refactor, `this` grant scope is granted to a role by
// inserting a row to 'role_grant_scope' table, but we've moved on to storing
// 'this' grant as a dedicated column in the type-specific role tables
thisGrantScope() *RoleGrantScope

// specialGrantScope returns special grant scopes ['descendants', 'children'] if available.
// returns nil, false if special grant scope is 'individual'
specialGrantScope() (*RoleGrantScope, bool)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel bad to "actually..." this discussion, but this is against the recommendation in Effective Go: https://go.dev/doc/effective_go#Getters. Personally I prefer not to have the "Get" prefix either 😅.

Copy link

github-actions bot commented May 5, 2025

Database schema diff between llb-normalized-grants and bosorawis-domain-iam-implement-role-grant-scopes-all @ 5bc40c7

To understand how these diffs are generated and some limitations see the
documentation of the script.

Functions

Unchanged

Tables

diff --git a/.schema-diff/tables_8336e489933de2090fecf4b084d799b1a404652a/iam_role_org.sql b/.schema-diff/tables_22a692c4005b5d62b3a756c23a552038641f9ad4/iam_role_org.sql
index 83a60b4a7..5dd37af9e 100644
--- a/.schema-diff/tables_8336e489933de2090fecf4b084d799b1a404652a/iam_role_org.sql
+++ b/.schema-diff/tables_22a692c4005b5d62b3a756c23a552038641f9ad4/iam_role_org.sql
@@ -35,7 +35,8 @@ create table public.iam_role_org (
     grant_this_role_scope_update_time public.wt_timestamp,
     grant_scope_update_time public.wt_timestamp,
     create_time public.wt_timestamp,
-    update_time public.wt_timestamp
+    update_time public.wt_timestamp,
+    constraint iam_role_org_grant_scope_valid_chk check ((grant_scope = any (array['children'::text, 'individual'::text])))
 );
 
 

Views

Unchanged

Triggers

Unchanged

Indexes

Unchanged

Constraints

Unchanged

Foreign Key Constraints

Unchanged

@@ -670,7 +670,7 @@ func Test_SetRoleGrantScope(t *testing.T) {
expectRoleVersionChange: 0,
scopes: []string{globals.GrantScopeChildren, globals.GrantScopeDescendants},
wantErr: true,
wantErrMsg: "iam.(Repository).SetRoleGrantScopes: only one of ['children', 'descendants'] can be specified: parameter violation: error #100",
Copy link
Collaborator

Choose a reason for hiding this comment

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

This error might've been clearer before. I personally think I prefer ' to ` 😅 . I won't insist on it though.

Comment on lines +54 to +56
unique(name, scope_id),
constraint iam_role_org_grant_scope_valid_chk
check (grant_scope in ('children', 'individual'))
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 this constraint accomplishes the same thing as the above constraint, iam_role_org_grant_scope_enm_fkey

Also, I think this constraint is now causing the insert_invalid_grant_scope sqltest to fail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants