Skip to content

Adding Grantor Information in sys.babelfish_schema_permissions catalog#73

Open
shreyeah38 wants to merge 22 commits intoamazon-aurora:bbf-database-permission-viewfrom
shreyeah38:database_permissions
Open

Adding Grantor Information in sys.babelfish_schema_permissions catalog#73
shreyeah38 wants to merge 22 commits intoamazon-aurora:bbf-database-permission-viewfrom
shreyeah38:database_permissions

Conversation

@shreyeah38
Copy link
Copy Markdown

@shreyeah38 shreyeah38 commented Jun 9, 2025

Description

The following PR adds grantor information in sys.babelfish_schema_permissions catalog, addressing a current limitation in Babelfish.

The functionality is achieved by hook implementation and reversing the flow of GRANT / REVOKE statement in Babelfish. This enhancement enables users to view grantor of various permissions.

Another functionality added is the storage of database-level CONNECT permission granted to users. This PR also provides a distinction between the storage of normal permissions and permissions with grant option in sys.babelfish_schema_permissions catalog.

Part of Jira - BABEL-5690

Engine PR : amazon-aurora/postgresql_modified_for_babelfish#132

Signed-off-by: Shreya Rai shreyaxr@amazon.com

@shreyeah38 shreyeah38 changed the title Added full support for sys.database_permissions view Adding Grantor Information in sys.babelfish_schema_permissions Jun 9, 2025
@shreyeah38 shreyeah38 changed the title Adding Grantor Information in sys.babelfish_schema_permissions Adding Grantor Information in sys.babelfish_schema_permissions catalog Jun 10, 2025
Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
…updated primary key

Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
function_args TEXT COLLATE "C",
grantor sys.NVARCHAR(128) COLLATE sys.database_default,
PRIMARY KEY(dbid, schema_name, object_name, grantee, object_type)
PRIMARY KEY(dbid, schema_name, object_name, permission, grantee, object_type, grantor)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why is permission a primary key?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Now we store "grant" permissions and "grant with options" permissions in 2 different rows. If there's no primary key on permission if would lead to an error when all the parameters are same except the permission.

Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
new_record_bbf_schema[Anum_bbf_schema_perms_permission - 1] = Int32GetDatum(current_permission);
new_record_repl_bbf_schema[Anum_bbf_schema_perms_permission - 1] = true;

new_tuple = heap_modify_tuple(tuple_bbf_schema,
Copy link
Copy Markdown

@anju15bharti anju15bharti Jun 18, 2025

Choose a reason for hiding this comment

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

We are not releasing tuple_bbf_schema, Make sure to release this using ReleaseSysCache.

Also in this file, I see multiple such instances where we haven't actually released it.

…e, function or procedure

Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
@coveralls
Copy link
Copy Markdown

coveralls commented Jun 19, 2025

Pull Request Test Coverage Report for Build 15847758092

Details

  • 248 of 269 (92.19%) changed or added relevant lines in 4 files are covered.
  • 28 unchanged lines in 5 files lost coverage.
  • Overall coverage decreased (-0.02%) to 75.434%

Changes Missing Coverage Covered Lines Changed/Added Lines %
contrib/babelfishpg_tsql/src/pl_exec-2.c 26 27 96.3%
contrib/babelfishpg_tsql/src/catalog.c 91 101 90.1%
contrib/babelfishpg_tsql/src/hooks.c 114 124 91.94%
Files with Coverage Reduction New Missed Lines %
contrib/babelfishpg_tsql/src/hooks.c 1 84.87%
contrib/babelfishpg_tsql/src/pl_handler.c 1 90.14%
contrib/babelfishpg_tsql/src/pl_funcs.c 2 19.04%
contrib/babelfishpg_tsql/src/catalog.c 11 86.1%
contrib/babelfishpg_tsql/src/pltsql_utils.c 13 90.98%
Totals Coverage Status
Change from base Build 15531425462: -0.02%
Covered Lines: 48866
Relevant Lines: 64780

💛 - Coveralls

Comment on lines +6345 to +6351
/*
* Special database roles should throw an error.
*/
if(strcmp(grantee, PUBLIC_ROLE_NAME) != 0)
{
throw_error_for_fixed_db_role((char *) grantee, dbname);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

please explain this

/*
* Remove all entries from normal grants
*/
update_privileges_of_object(logical_schema, object_name, old_priv_normal_grant, grantee, OBJ_RELATION, false, grantor, false);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Blocking: There is high chances of race condition here.

* The following hook aims to add/remove entries into BBF schema permissions catalog
*/
static bool
update_bbf_schema_permissions_catalog(AclMode privileges, bool is_grant, List *grantees,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit:

  1. Please add explanatory description and comments.
  2. Indentation
  3. The following hook -> Function update_bbf_schema_permissions_catalog
  4. BBF -> Babelfish [Lets not use acronym in description]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, will make the changes

Comment on lines +3944 to +3946
/*
* If grantor ends with "_bbfobj", remove the suffix as this is an internal BBF role.
*/
Copy link
Copy Markdown

@shalinilohia50 shalinilohia50 Jun 20, 2025

Choose a reason for hiding this comment

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

How can the grantor be _bbfobj for grant on schema in T-SQL?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This is the case when the schema_owner is the member of db_owner user role, this sets the grantor to be _bbfobj

Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
Signed-off-by: Shreya Rai <shreyaxr@amazon.com>
Copy link
Copy Markdown

@Imran-imtiaz48 Imran-imtiaz48 left a comment

Choose a reason for hiding this comment

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

This script demonstrates a well-structured and advanced implementation of database permission mapping using Common Table Expressions (CTEs), bitwise operations, and role-based access logic in a Babelfish-compatible PostgreSQL environment. The use of GRANT, CREATE OR REPLACE VIEW, and JOIN LATERAL shows a solid understanding of both SQL Server-style and PostgreSQL-style syntax. The permission_mapping CTE is thoughtfully constructed to map bit values to permission names and type codes, enhancing readability and maintainability. The main view, sys.database_permissions, effectively combines permissions from multiple object types (database, schema, object) while dynamically resolving principals and ensuring correct filtering through pg_has_role checks. One suggestion for improvement would be to modularize repeated expressions (e.g., pg_has_role(...)) into reusable components or helper functions if the environment allows. Also, ensuring consistent and thorough commenting would help future developers quickly understand the complex logic. Overall, this is a strong and comprehensive implementation of a permissions view with well-handled edge cases and system compatibility.

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.

7 participants