Skip to content

Prevent duplicate OIDC groups #1126

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

Merged
merged 1 commit into from
Apr 14, 2025
Merged

Prevent duplicate OIDC groups #1126

merged 1 commit into from
Apr 14, 2025

Conversation

nscuro
Copy link
Member

@nscuro nscuro commented Apr 11, 2025

Description

Prevents duplicate OIDC groups.

Addressed Issue

Fixes DependencyTrack/hyades#1756

Additional Details

Migration query tested with the following script:

begin;

-- Create two duplicate and one unique group.
INSERT INTO "OIDCGROUP" ("NAME", "UUID")
VALUES ('foo', gen_random_uuid())
     , ('foo', gen_random_uuid())
     , ('bar', gen_random_uuid());

-- Create two teams.
INSERT INTO "TEAM" ("NAME", "UUID") VALUES ('a', gen_random_uuid());
INSERT INTO "TEAM" ("NAME", "UUID") VALUES ('b', gen_random_uuid());

-- Map groups to teams such that duplicate groups overlap.
INSERT INTO "MAPPEDOIDCGROUP" ("GROUP_ID", "TEAM_ID", "UUID")
VALUES ((SELECT "ID" FROM "OIDCGROUP" WHERE "NAME" = 'foo' ORDER BY "ID" LIMIT 1), (SELECT "ID" FROM "TEAM" WHERE "NAME" = 'a'), gen_random_uuid())
     , ((SELECT "ID" FROM "OIDCGROUP" WHERE "NAME" = 'foo' ORDER BY "ID" OFFSET 1 LIMIT 1), (SELECT "ID" FROM "TEAM" WHERE "NAME" = 'a'), gen_random_uuid())
     , ((SELECT "ID" FROM "OIDCGROUP" WHERE "NAME" = 'foo' ORDER BY "ID" OFFSET 1 LIMIT 1), (SELECT "ID" FROM "TEAM" WHERE "NAME" = 'b'), gen_random_uuid())
     , ((SELECT "ID" FROM "OIDCGROUP" WHERE "NAME" = 'bar' LIMIT 1), (SELECT "ID" FROM "TEAM" WHERE "NAME" = 'a'), gen_random_uuid());

-- Identify OIDC groups with duplicate names.
WITH cte_duplicate_group AS (
  SELECT "NAME" AS name
       , MIN("ID") AS canonical_id
    FROM "OIDCGROUP"
   GROUP BY "NAME"
  HAVING COUNT(*) > 1
),
-- Delete mappings of duplicate OIDC groups.
cte_deleted_mapping AS (
  DELETE FROM "MAPPEDOIDCGROUP"
   USING cte_duplicate_group
       , "OIDCGROUP"
   WHERE "MAPPEDOIDCGROUP"."GROUP_ID" = "OIDCGROUP"."ID"
     AND "OIDCGROUP"."NAME" = cte_duplicate_group.name
     AND "OIDCGROUP"."ID" != cte_duplicate_group.canonical_id
  RETURNING "OIDCGROUP"."NAME" AS group_name
          , "MAPPEDOIDCGROUP"."TEAM_ID" AS team_id
          , "MAPPEDOIDCGROUP"."UUID" AS uuid
),
-- Delete duplicate OIDC groups.
cte_deleted_group AS (
  DELETE FROM "OIDCGROUP"
   USING cte_duplicate_group
   WHERE "OIDCGROUP"."NAME" = cte_duplicate_group.name
     AND "OIDCGROUP"."ID" != cte_duplicate_group.canonical_id
  RETURNING "OIDCGROUP"."ID" AS id
)
-- Re-create deleted mappings, but using the canonical group ID.
INSERT INTO "MAPPEDOIDCGROUP" ("GROUP_ID", "TEAM_ID", "UUID")
SELECT "OIDCGROUP"."ID"
     , cte_deleted_mapping.team_id
     , cte_deleted_mapping.uuid
  FROM cte_deleted_mapping
 INNER JOIN "OIDCGROUP"
    ON "OIDCGROUP"."NAME" = cte_deleted_mapping.group_name
 -- This condition mostly just forces evaluation of cte_deleted_group.
 WHERE "OIDCGROUP"."ID" NOT IN (SELECT id FROM cte_deleted_group)
-- If the duplicate groups had overlapping mappings, we'll get conflicts here.
ON CONFLICT ("TEAM_ID", "GROUP_ID") DO NOTHING;

SELECT * FROM "OIDCGROUP";
SELECT * FROM "MAPPEDOIDCGROUP";

rollback;

Checklist

  • I have read and understand the contributing guidelines
  • This PR fixes a defect, and I have provided tests to verify that the fix is effective
  • This PR implements an enhancement, and I have provided tests to verify that it works as intended
  • This PR introduces changes to the database model, and I have updated the migration changelog accordingly
  • This PR introduces new or alters existing behavior, and I have updated the documentation accordingly

@nscuro nscuro added the defect Something isn't working label Apr 11, 2025
@nscuro nscuro added this to the 5.6.0 milestone Apr 11, 2025
Copy link

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
+0.00% (target: -1.00%) (target: 70.00%)
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (c1f0123) 22861 19238 84.15%
Head commit (ea33591) 22861 (+0) 19238 (+0) 84.15% (+0.00%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#1126) 0 0 ∅ (not applicable)

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

@nscuro nscuro merged commit 4ba30a9 into main Apr 14, 2025
9 checks passed
@nscuro nscuro deleted the dupe-oidcgroups branch April 14, 2025 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Database schema allows for duplicate OIDC group names
2 participants