Skip to content

Add PostgreSQL auto-user deletion#32792

Merged
gabrielcorado merged 13 commits intomasterfrom
gabrielcorado/pg-user-auto-deletion
Oct 16, 2023
Merged

Add PostgreSQL auto-user deletion#32792
gabrielcorado merged 13 commits intomasterfrom
gabrielcorado/pg-user-auto-deletion

Conversation

@gabrielcorado
Copy link
Copy Markdown
Contributor

@gabrielcorado gabrielcorado commented Sep 29, 2023

Related to #31199 and #29731

Adds a create database user mode (similar to what is done for host and desktop users), where:

  • off: Disables user creation. (same as create_database_user: false).
  • keep: Creates the user but deactivates it when the session ends (keeps the user but removes its roles assignments). This is the current default behavior.
  • best_effort_drop: Tries to drop the database user. If it fails, it returns to keep mode, deactivating the user.

For backward compatibility, the create_database_user: true role option is kept, and set the mode value to keep if it is set to true.

@gabrielcorado gabrielcorado self-assigned this Sep 29, 2023
@github-actions github-actions Bot requested a review from espadolini September 29, 2023 14:39
@github-actions github-actions Bot added database-access Database access related issues and PRs size/md labels Sep 29, 2023
Comment thread api/proto/teleport/legacy/types/types.proto Outdated
Comment thread api/types/role.go
Comment thread lib/services/access_checker.go Outdated
Comment thread lib/services/role.go Outdated
Comment thread lib/services/role_test.go
Comment thread lib/srv/db/common/autousers.go Outdated
Comment thread lib/srv/db/common/test.go Outdated
Comment thread lib/srv/db/common/autousers.go Outdated
// function to make sure no 2 processes run user activation simultaneously.
func (a *UserProvisioner) Activate(ctx context.Context, sessionCtx *Session) (func(), error) {
if !sessionCtx.AutoCreateUser {
if !services.IsCreateDatabaseUserEnabled(sessionCtx.AutoCreateUserMode) {
Copy link
Copy Markdown
Contributor

@greedy52 greedy52 Sep 29, 2023

Choose a reason for hiding this comment

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

I would throw errors if the Backend doesn't support the new mode yet (somewhere in this function). IMO, it's better to alert the user the mode configuration is wrong than silently falling back to another mode. But certainly up for discussion.

Comment thread lib/srv/db/postgres/engine.go Outdated
Comment thread lib/srv/db/postgres/delete-user.sql
Copy link
Copy Markdown
Contributor

@greedy52 greedy52 left a comment

Choose a reason for hiding this comment

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

LGTM, assuming a debug log will be added somewhere to show if the user is actually deleted or a fallback is done.

Comment thread api/proto/teleport/legacy/types/types.proto Outdated
Comment thread api/types/role.go
Comment thread lib/services/access_checker.go Outdated
func (e *Engine) DeleteUser(ctx context.Context, sessionCtx *common.Session) error {
// TODO(gabrielcorado): implement delete database user. for now, just
// fallback to deactivate user.
return e.DeactivateUser(ctx, sessionCtx)
Copy link
Copy Markdown
Contributor

@greedy52 greedy52 Oct 10, 2023

Choose a reason for hiding this comment

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

nit: I am adding Redshift (postgres engine) auto-user provisioning so there is a chance Redshift will start with only keep mode too. I am wondering if it would be easier to return not implemented in backend.DeleteUser and let common.UserProvisioner to do the fallback. Probably the same thing either way.

@espadolini espadolini removed their request for review October 13, 2023 14:21
@gabrielcorado
Copy link
Copy Markdown
Contributor Author

@greedy52 @smallinsky FYI, I've made the following updates:

  • Changed preferDrop mode name to best_effort_drop (keeping the same pattern used in some other places).
  • Added logs into DeleteUser indicating if the user was dropped (debug) or deactivated (info).

Comment thread lib/srv/db/postgres/users.go Outdated
@gabrielcorado gabrielcorado added this pull request to the merge queue Oct 16, 2023
Merged via the queue into master with commit 9582262 Oct 16, 2023
@gabrielcorado gabrielcorado deleted the gabrielcorado/pg-user-auto-deletion branch October 16, 2023 17:29
gabrielcorado added a commit that referenced this pull request Oct 17, 2023
* feat: add auto-user deletion postgres

* refactor: change to IsEnabled func to check auto-user

* test: fix linting and test

* refactor(db): code review suggestions

* refactor: rename option to best effort drop

* refactor(api): rename createa database user mode property

* refactor(services): review suggestions

* feat(postgres): add log for user deletion result

* refactor(integrations): regenerate crd manifests

* feat(examples): update operator role spec

* refactor(db): use common sql state codes
github-merge-queue Bot pushed a commit that referenced this pull request Oct 19, 2023
* feat: add auto-user deletion postgres

* refactor: change to IsEnabled func to check auto-user

* test: fix linting and test

* refactor(db): code review suggestions

* refactor: rename option to best effort drop

* refactor(api): rename createa database user mode property

* refactor(services): review suggestions

* feat(postgres): add log for user deletion result

* refactor(integrations): regenerate crd manifests

* feat(examples): update operator role spec

* refactor(db): use common sql state codes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

database-access Database access related issues and PRs size/md

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants