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 an issue auto-provisioned PostgreSQL user may keep old roles indefinitely #33969

Merged
merged 2 commits into from Nov 1, 2023

Conversation

greedy52
Copy link
Contributor

@greedy52 greedy52 commented Oct 27, 2023

changelog: Fix an issue auto-provisioned PostgreSQL user may keep old roles indefinitely

Fixes https://github.com/gravitational/teleport-private/issues/998

change:

        -- If the user has active connections, make sure the provided roles
        -- match what the user currently has.
        IF EXISTS (SELECT usename FROM pg_stat_activity WHERE usename = username) THEN
            SELECT CAST(array_agg(rolname) as varchar[]) INTO cur_roles FROM pg_auth_members JOIN pg_roles ON roleid = oid WHERE member=(SELECT oid FROM pg_roles WHERE rolname = username) AND rolname != 'teleport-auto-user';
            -- "a <@ b" checks if all unique elements in "a" are contained by
            -- "b". Using length check plus "contains" check to avoid sorting.
            IF ARRAY_LENGTH(roles, 1) = ARRAY_LENGTH(cur_roles, 1) AND roles <@ cur_roles THEN
                RETURN;
            END IF;
            RAISE EXCEPTION SQLSTATE 'TP002' USING MESSAGE = 'TP002: User has active connections and roles have changed';
        END IF;

This PR also UPPERCASE the existing SQL scripts to be consistent, see discussion #33307 (comment)

Note that all new implementations of auto-user provisioning (MySQL/MariaDB/Redshift) already include similar fixes for this issue in their initial implementation.

@greedy52 greedy52 force-pushed the STeve/p998_user_active_connection_role_changed branch from f594013 to dd8173a Compare October 27, 2023 15:35
@greedy52 greedy52 marked this pull request as ready for review October 27, 2023 15:41
@github-actions
Copy link

The PR changelog entry failed validation: Changelog entry not found in the PR body. Please add a "no-changelog" label to the PR, or changelog lines starting with changelog: followed by the changelog entries for the PR.

@github-actions github-actions bot added database-access Database access related issues and PRs size/sm labels Oct 27, 2023
Copy link
Contributor

@gabrielcorado gabrielcorado left a comment

Choose a reason for hiding this comment

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

LGTM.

@greedy52 greedy52 added this pull request to the merge queue Nov 1, 2023
Merged via the queue into master with commit 9da4362 Nov 1, 2023
36 of 37 checks passed
@greedy52 greedy52 deleted the STeve/p998_user_active_connection_role_changed branch November 1, 2023 17:00
@public-teleport-github-review-bot

@greedy52 See the table below for backport results.

Branch Result
branch/v13 Failed
branch/v14 Create PR

greedy52 added a commit that referenced this pull request Nov 1, 2023
…finitely (#33969)

* Fix an issue auto-provisioned PostgreSQL user may keep old roles indefinitely

* upper case the sql scripts
github-merge-queue bot pushed a commit that referenced this pull request Nov 7, 2023
…finitely (#33969) (#34120)

* Fix an issue auto-provisioned PostgreSQL user may keep old roles indefinitely

* upper case the sql scripts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants