fix(auth): deduplicate users in add_users_to_policy#176
Open
bhudevbhanpuriya wants to merge 1 commit into
Open
fix(auth): deduplicate users in add_users_to_policy#176bhudevbhanpuriya wants to merge 1 commit into
bhudevbhanpuriya wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The add_users_to_policy function in database/istsos_auth.sql used simple array concatenation (old_roles_ || users_), which allows duplicate usernames to accumulate across repeated calls. Over time this causes role array drift, making audits harder and potentially leading to unexpected behavior.
Root cause Line 778 previously read:
new_roles_ := old_roles_ || users_;This merges arrays without removing duplicates.
Fix Changed the merge to deduplicate using DISTINCT:
new_roles_ := (SELECT array_agg(DISTINCT x) FROM unnest(old_roles_ || users_) AS x);This ensures the resulting array always contains unique usernames, whether duplicates existed in old_roles_, appear in users_, or span across both arrays.
Changes
database/istsos_auth.sql- fixed the array mergetest/database/test_auth_sql.py- added regression test test_add_users_to_policy_deduplicates_duplicate_usersMigration Existing databases may already have duplicate entries in policy role arrays. After deploying this fix, run a one-time cleanup:
UPDATE pg_policies SET roles = (SELECT array_agg(DISTINCT r) FROM unnest(roles) AS r) WHERE schemaname = 'sensorthings';Alternatively, recreate policies via the existing admin endpoints.
Testing The new regression test verifies:
Run: pytest test/database/test_auth_sql.py::TestAuth::test_add_users_to_policy_deduplicates_duplicate_users