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

Fix database user auto-provisioning #51945

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

GavinFrazar
Copy link
Contributor

@GavinFrazar GavinFrazar commented Feb 7, 2025

Changelog: Fixed Postgres database access control privileges auto-provisioning to grant USAGE on schemas as needed for table privileges and fixed an issue that prevented user privileges from being revoked at the end of their session in some cases.

Fixes #51681

Fixes #51851

This PR includes the following:

  • Use a custom query to find user db privileges on tables to avoid the "grantor" filter condition in the information_schema.tables_privileges view. This fixes the cases where the grantor for a privilege is set to the table owner rather than the user (teleport) who issued the grant. Most notably, this happens when a superuser grants privileges on a table they do not own to a user.
  • Grant USAGE on schemas that contain tables where we intend to grant table privileges. This is necessary to use the table privileges we grant.
  • Wrap all remaining plpgsql procedure creation/calls with retries.
  • Add a db permissions e2e test for RDS
  • Expand e2e tests to test with and without a superuser db admin
  • Significantly speed up the RDS e2e tests by wrapping EventuallyWithT in a helper func that tries the condition func immediately rather than waiting for the first tick duration.

I tested this quite a bit manually on RDS postgres and self-hosted, but still expanded the e2e tests just to be sure and to avoid regressions
I'm also hopeful that the additional retries I added will fix the Redshift flakiness.

Reviewers: sorry for the large diff in the e2e tests, but I noticed they were wasting quite a lot time after each subtest just waiting for 10 seconds before trying the post-connection test fn.

@GavinFrazar GavinFrazar added aws Used for AWS Related Issues. database-access Database access related issues and PRs db/postgres PostgreSQL related database access issues backport/branch/v15 backport/branch/v16 backport/branch/v17 labels Feb 7, 2025
@@ -8,11 +8,62 @@ BEGIN
IF EXISTS (SELECT usename FROM pg_stat_activity WHERE usename = username AND datname = current_database()) THEN
RAISE NOTICE 'User has active connections to current database';
ELSE
-- Loop through table permissions
FOR row_data IN (SELECT DISTINCT table_schema, table_name FROM information_schema.table_privileges WHERE grantee = username)
CREATE TEMPORARY TABLE cur_perms ON COMMIT DROP AS (
Copy link
Contributor Author

@GavinFrazar GavinFrazar Feb 7, 2025

Choose a reason for hiding this comment

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

by default postgres temp tables are dropped when your session ends instead of dropping them on commit/rollback, so I added ON COMMIT DROP to this and the other procedure to make them retryable, otherwise you can get an error that the table already exists if you retry calling the proc.

@GavinFrazar GavinFrazar force-pushed the gavinfrazar/fix-db-auto-user-bugs branch 2 times, most recently from 9789f5f to 925e216 Compare February 7, 2025 08:07
@GavinFrazar GavinFrazar marked this pull request as ready for review February 7, 2025 18:24
@github-actions github-actions bot requested a review from avatus February 7, 2025 18:25
lib/srv/db/postgres/sql/update-permissions.sql Outdated Show resolved Hide resolved
e2e/aws/rds_test.go Show resolved Hide resolved
e2e/aws/rds_test.go Show resolved Hide resolved
e2e/aws/rds_test.go Show resolved Hide resolved
@greedy52
Copy link
Contributor

greedy52 commented Feb 7, 2025

🙏 thanks for all the effort you've put into investigating this!!!

* Use a custom query to find user db privileges on tables to avoid the
  "grantor" filter condition in the information_schema.tables_privileges
  view. This fixes the cases where the grantor for a privilege is set to
  the table owner rather than the user (teleport) who issued the grant.
  Most notably, this happens when a superuser grants privileges on a
  table they do not own to a user.
* Grant USAGE on schemas that contain tables where we intend to grant
  table privileges. This is necessary to use the table privileges we
  grant.
* Wrap all remaining plpgsql procedure creation/calls with retries.
* Add a db permissions e2e test for RDS
* Expand e2e tests to test with and without a superuser db admin
* Significantly speed up the RDS e2e tests by wrapping EventuallyWithT
  in a helper func that tries the condition func immediately rather than
  waiting for the first tick duration.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aws Used for AWS Related Issues. backport/branch/v15 backport/branch/v16 backport/branch/v17 database-access Database access related issues and PRs db/postgres PostgreSQL related database access issues size/md
Projects
None yet
2 participants