Skip to content
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

Restrict DROP USER/ROLE from non-dbo user #2859

Conversation

anju15bharti
Copy link
Contributor

Description

Earlier, any user was able to drop user/role, irrespective of whether that user has required privileges or not.

With this commit, only dbo should have the permission to drop user/role.

Issues Resolved

BABEL-5173

Test Scenarios Covered

  • Use case based -

  • Boundary conditions -

  • Arbitrary inputs -

  • Negative test cases -

  • Minor version upgrade tests -

  • Major version upgrade tests -

  • Performance tests -

  • Tooling impact -

  • Client tests -

Check List

  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is under the terms of the Apache 2.0 and PostgreSQL licenses, and grant any person obtaining a copy of the contribution permission to relicense all or a portion of my contribution to the PostgreSQL License solely to contribute all or a portion of my contribution to the PostgreSQL open source project.

For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@coveralls
Copy link
Collaborator

coveralls commented Aug 14, 2024

Pull Request Test Coverage Report for Build 10453221297

Details

  • 15 of 15 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 74.067%

Totals Coverage Status
Change from base Build 10389490935: 0.02%
Covered Lines: 44449
Relevant Lines: 60012

💛 - Coveralls

Comment on lines 3440 to 3443
if (!has_privs_of_role(GetUserId(),get_role_oid(db_owner_name, false)))
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("User does not have permission to perform this action.")));
Copy link
Contributor

@HarshLunagariya HarshLunagariya Aug 14, 2024

Choose a reason for hiding this comment

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

Is this aligning with ideal T-SQL behaviour? Error msg is matching?


/* must be database owner to drop user/role*/
db_owner_name = get_db_owner_name(get_cur_db_name());
if (!has_privs_of_role(GetUserId(),get_role_oid(db_owner_name, false)))
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: ...(GetUserId(),<WHITESPACE> get_role_oid...


/* must be database owner to drop user/role*/
db_owner_name = get_db_owner_name(get_cur_db_name());
if (!has_privs_of_role(GetUserId(),get_role_oid(db_owner_name, false)))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why has_privs_of_role , not is_member_of_role?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because this is what we are checking when creating the role/user.

Copy link
Contributor

@HarshLunagariya HarshLunagariya left a comment

Choose a reason for hiding this comment

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

Overall changes looks good.

Deepesh125
Deepesh125 previously approved these changes Aug 15, 2024
Comment on lines 3306 to 3307
if ((drop_user && strncmp(logical_role_name, "dbo", rolename_len) == 0) ||
(drop_role && strncmp(logical_role_name, "db_owner", rolename_len) == 0))
Copy link
Contributor

Choose a reason for hiding this comment

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

What if this check is run against user name d or db_o or db_ow etc.? We need to have the length comparison before using strncmp().

Copy link
Contributor

Choose a reason for hiding this comment

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

Please add such testcases.

const char *db_owner_name;
int role_oid;
int rolename_len;
char *logical_role_name = NULL;
Copy link
Contributor

Choose a reason for hiding this comment

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

logical_role_name is not needed IMO. Let's use rolespec->rolename only.

Signed-off-by: ANJU BHARTI <[email protected]>
@@ -3285,15 +3285,44 @@ bbf_ProcessUtility(PlannedStmt *pstmt,
{
RoleSpec *rolspec = lfirst(item);
char *user_name;
char *db_principal;
Copy link
Contributor

Choose a reason for hiding this comment

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

We should pfree db_principal. db_owner_name is a const char*, it can't be freed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should not pfree db_principal as it is pointing to string literal.

Copy link
Contributor

Choose a reason for hiding this comment

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

it is it pointing to string literal then make it const char * here itself.

const char * db_principal = drop_user ? "user" : "role"

Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need separate test files? Let's reuse some existing test files. IIRC we should avoid creating new test files wherever possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don’t have any such testfile for this usecase hence created one for this usecase testing.

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant wherever we have tests for restricting create login/user, we can use that file.

Copy link
Contributor

Choose a reason for hiding this comment

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

please add some tests which involves if exists, for example, drop user if exists username

Copy link
Contributor

@shalinilohia50 shalinilohia50 left a comment

Choose a reason for hiding this comment

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

LGTM!


user_name = get_physical_user_name(db_name, rolspec->rolename, false);
db_owner_name = get_db_owner_name(get_cur_db_name());
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: simply use db_name instead of get_cur_db_name


user_name = get_physical_user_name(db_name, rolspec->rolename, false);
db_owner_name = get_db_owner_name(get_cur_db_name());
role_oid = get_role_oid(user_name, true);
Copy link
Contributor

Choose a reason for hiding this comment

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

why missing_ok = true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For if exist case which is already present in some testfiles

@@ -3285,15 +3285,44 @@ bbf_ProcessUtility(PlannedStmt *pstmt,
{
RoleSpec *rolspec = lfirst(item);
char *user_name;
char *db_principal;
Copy link
Contributor

Choose a reason for hiding this comment

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

it is it pointing to string literal then make it const char * here itself.

const char * db_principal = drop_user ? "user" : "role"

Copy link
Contributor

Choose a reason for hiding this comment

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

please add some tests which involves if exists, for example, drop user if exists username

Signed-off-by: Harsh Lunagariya <[email protected]>

user_name = get_physical_user_name(db_name, rolspec->rolename, false);
db_owner_name = get_db_owner_name(db_name);
Copy link
Contributor

Choose a reason for hiding this comment

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

we should check whether user_name is valid T-SQL role or login.

Signed-off-by: Harsh Lunagariya <[email protected]>
@HarshLunagariya
Copy link
Contributor

Description

Earlier, any user was able to drop user/role, irrespective of whether that user has required privileges or not.

With this commit, only dbo should have the permission to drop user/role.

Issues Resolved

BABEL-5173

Description
Earlier, any user was able to drop user/role, irrespective of whether that user has required privileges or not.

With this commit, Only dbo and members of db_owner will have the permission to drop user/role. 
Additionally, this restricts dropping internal database principal such as dbo and db_owner, it restricts
dropping non-Babelfish roles from TDS endpoint.

Issues Resolved
BABEL-5173

@Deepesh125 Deepesh125 merged commit a66de39 into babelfish-for-postgresql:BABEL_4_X_DEV Aug 19, 2024
44 checks passed
@Deepesh125 Deepesh125 deleted the jira-BABEL-5173 branch August 19, 2024 13:46
shalinilohia50 pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Aug 20, 2024
)

Earlier, any user was able to drop user/role, irrespective of whether that user has required privileges or not.

With this commit, Only dbo and members of db_owner will have the permission to drop user/role.  Additionally, this
restricts dropping internal database principal such as dbo and db_owner, it restricts dropping non-Babelfish roles from TDS endpoint.

Task: BABEL-5173

Authored-by: ANJU BHARTI <[email protected]>
Co-authored-by: Harsh Lunagariya <[email protected]>
Signed-off-by: Harsh Lunagariya <[email protected]>
shalinilohia50 pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Aug 20, 2024
)

Earlier, any user was able to drop user/role, irrespective of whether that user has required privileges or not.

With this commit, Only dbo and members of db_owner will have the permission to drop user/role.  Additionally, this
restricts dropping internal database principal such as dbo and db_owner, it restricts dropping non-Babelfish roles from TDS endpoint.

Task: BABEL-5173

Authored-by: ANJU BHARTI <[email protected]>
Co-authored-by: Harsh Lunagariya <[email protected]>
Signed-off-by: Harsh Lunagariya <[email protected]>
shardgupta pushed a commit that referenced this pull request Aug 20, 2024
Earlier, a user was able to drop user/role that belonged to another database.
With this commit, a user can only drop the role/user that belongs to the same database with sufficient privileges.
Issues Resolved

Task: BABEL-5173

Signed-off-by: Shalini Lohia [email protected]
shardgupta pushed a commit that referenced this pull request Aug 20, 2024
Earlier, a user was able to drop user/role that belonged to another database.
With this commit, a user can only drop the role/user that belongs to the same database with sufficient privileges.
Issues Resolved

Task: BABEL-5173

Signed-off-by: Shalini Lohia [email protected]
anju15bharti added a commit to amazon-aurora/babelfish_extensions that referenced this pull request Aug 20, 2024
)

Earlier, any user was able to drop user/role, irrespective of whether that user has required privileges or not.

With this commit, Only dbo and members of db_owner will have the permission to drop user/role.  Additionally, this
restricts dropping internal database principal such as dbo and db_owner, it restricts dropping non-Babelfish roles from TDS endpoint.

Task: BABEL-5173

Authored-by: ANJU BHARTI <[email protected]>
Co-authored-by: Harsh Lunagariya <[email protected]>
Signed-off-by: Harsh Lunagariya <[email protected]>
sharathbp pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Aug 20, 2024
)

Earlier, any user was able to drop user/role, irrespective of whether that user has required privileges or not.

With this commit, Only dbo and members of db_owner will have the permission to drop user/role.  Additionally, this
restricts dropping internal database principal such as dbo and db_owner, it restricts dropping non-Babelfish roles from TDS endpoint.

Task: BABEL-5173

Authored-by: ANJU BHARTI <[email protected]>
Co-authored-by: Harsh Lunagariya <[email protected]>
Signed-off-by: Harsh Lunagariya <[email protected]>
anju15bharti added a commit to amazon-aurora/babelfish_extensions that referenced this pull request Aug 26, 2024
)

Earlier, any user was able to drop user/role, irrespective of whether that user has required privileges or not.

With this commit, Only dbo and members of db_owner will have the permission to drop user/role.  Additionally, this
restricts dropping internal database principal such as dbo and db_owner, it restricts dropping non-Babelfish roles from TDS endpoint.

Task: BABEL-5173

Authored-by: ANJU BHARTI <[email protected]>
Co-authored-by: Harsh Lunagariya <[email protected]>
Signed-off-by: Harsh Lunagariya <[email protected]>
anju15bharti added a commit to amazon-aurora/babelfish_extensions that referenced this pull request Aug 26, 2024
)

Earlier, any user was able to drop user/role, irrespective of whether that user has required privileges or not.

With this commit, Only dbo and members of db_owner will have the permission to drop user/role.  Additionally, this
restricts dropping internal database principal such as dbo and db_owner, it restricts dropping non-Babelfish roles from TDS endpoint.

Task: BABEL-5173

Authored-by: ANJU BHARTI <[email protected]>
Co-authored-by: Harsh Lunagariya <[email protected]>
Signed-off-by: Harsh Lunagariya <[email protected]>
anju15bharti pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Aug 26, 2024
) (babelfish-for-postgresql#2864)

Earlier, a user was able to drop user/role that belonged to another database.
With this commit, a user can only drop the role/user that belongs to the same database with sufficient privileges.
Issues Resolved

Task: BABEL-5173

Signed-off-by: Shalini Lohia [email protected]
anju15bharti pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Aug 26, 2024
) (babelfish-for-postgresql#2864)

Earlier, a user was able to drop user/role that belonged to another database.
With this commit, a user can only drop the role/user that belongs to the same database with sufficient privileges.
Issues Resolved

Task: BABEL-5173

Signed-off-by: Shalini Lohia [email protected]
anju15bharti pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Aug 26, 2024
) (babelfish-for-postgresql#2865)

Earlier, a user was able to drop user/role that belonged to another database.
With this commit, a user can only drop the role/user that belongs to the same database with sufficient privileges.
Issues Resolved

Task: BABEL-5173

Signed-off-by: Shalini Lohia [email protected]
anju15bharti pushed a commit to amazon-aurora/babelfish_extensions that referenced this pull request Aug 26, 2024
) (babelfish-for-postgresql#2865)

Earlier, a user was able to drop user/role that belonged to another database.
With this commit, a user can only drop the role/user that belongs to the same database with sufficient privileges.
Issues Resolved

Task: BABEL-5173

Signed-off-by: Shalini Lohia [email protected]
shardgupta pushed a commit that referenced this pull request Aug 26, 2024
Earlier, any user was able to drop user/role, irrespective of whether that user has required privileges or not.

With this commit, Only dbo and members of db_owner will have the permission to drop user/role.  Additionally, this
restricts dropping internal database principal such as dbo and db_owner, it restricts dropping non-Babelfish roles from TDS endpoint.

Task: BABEL-5173

Authored-by: ANJU BHARTI <[email protected]>
shardgupta pushed a commit that referenced this pull request Aug 26, 2024
Earlier, any user was able to drop user/role, irrespective of whether that user has required privileges or not.

With this commit, Only dbo and members of db_owner will have the permission to drop user/role.  Additionally, this
restricts dropping internal database principal such as dbo and db_owner, it restricts dropping non-Babelfish roles from TDS endpoint.

Task: BABEL-5173

Authored-by: ANJU BHARTI <[email protected]>
shardgupta pushed a commit that referenced this pull request Aug 26, 2024
Earlier, any user was able to drop user/role, irrespective of whether that user has required privileges or not.

With this commit, Only dbo and members of db_owner will have the permission to drop user/role.  Additionally, this
restricts dropping internal database principal such as dbo and db_owner, it restricts dropping non-Babelfish roles from TDS endpoint.

Task: BABEL-5173

Authored-by: ANJU BHARTI <[email protected]>
shardgupta pushed a commit that referenced this pull request Aug 26, 2024
Earlier, a user was able to drop user/role that belonged to another database.
With this commit, a user can only drop the role/user that belongs to the same database with sufficient privileges.
Issues Resolved

Task: BABEL-5173

Signed-off-by: Shalini Lohia [email protected]
staticlibs pushed a commit to wiltondb/babelfish_extensions that referenced this pull request Oct 20, 2024
) (babelfish-for-postgresql#2864)

Earlier, a user was able to drop user/role that belonged to another database.
With this commit, a user can only drop the role/user that belongs to the same database with sufficient privileges.
Issues Resolved

Task: BABEL-5173

Signed-off-by: Shalini Lohia [email protected]
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.

5 participants