-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store ephemeral user group associations
Users are associated with user groups via the "organizations" attribute of their authentication JWT. We're going to want our queries and authentication checks to account for those group associations, but we don't want to have to constantly pass an unpredictably long list of associations every time we make a query. This entity, and the middleware that populates it, allows a given API call to have those associations accounted for in a given HTTP request context. The table is labeled "ephemeral" to try to convey the fact that the data it contains should not be considered accurate outside of that context. The implementation of the middleware right now is intentionally simple and does not attempt to be efficient. For example, it does not attempt to re-use any past data in the table (e.g. associations that may have existed from a past transaction and wouldn't necessarily have to be re-created). We may find that having O(n) insert queries for every authenticated API call is too slow; if that ends up becoming a problem we may determine that our model for using KeyCloak as the group management and JWTs as the communication mechanism isn't correct. These associations are not actually used when making auth decisions yet. This commit is, however, a prerequisite for that feature. Issue #Create user-group associations "cache" middleware
- Loading branch information
Showing
12 changed files
with
182 additions
and
9 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/database/initialization/ephemeral_user_group_association_to_json.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
SELECT drop_function('ephemeral_user_group_association_to_json'); | ||
|
||
CREATE FUNCTION ephemeral_user_group_association_to_json( | ||
ephemeral_user_group_association ephemeral_user_group_associations | ||
) | ||
RETURNS jsonb AS $$ | ||
BEGIN | ||
RETURN jsonb_build_object( | ||
'userKeycloakUserId', ephemeral_user_group_association.user_keycloak_user_id, | ||
'userGroupKeycloakOrganizationId', ephemeral_user_group_association.user_group_keycloak_organization_id, | ||
'createdAt', data_provider.created_at | ||
); | ||
END; | ||
$$ LANGUAGE plpgsql; |
8 changes: 8 additions & 0 deletions
8
src/database/migrations/0050-create-ephemeral-user-group-associations.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CREATE TABLE ephemeral_user_group_associations ( | ||
user_keycloak_user_id uuid NOT NULL REFERENCES users ( | ||
keycloak_user_id | ||
) ON DELETE CASCADE, | ||
user_group_keycloak_organization_id uuid NOT NULL, | ||
created_at timestamp with time zone NOT NULL DEFAULT now(), | ||
PRIMARY KEY (user_keycloak_user_id, user_group_keycloak_organization_id) | ||
); |
17 changes: 17 additions & 0 deletions
17
...database/operations/ephemeralUserGroupAssociations/createEphemeralUserGroupAssociation.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { generateCreateOrUpdateItemOperation } from '../generators'; | ||
import type { | ||
EphemeralUserGroupAssociation, | ||
InternallyWritableEphemeralUserGroupAssociation, | ||
} from '../../../types'; | ||
|
||
const createEphemeralUserGroupAssociation = generateCreateOrUpdateItemOperation< | ||
EphemeralUserGroupAssociation, | ||
InternallyWritableEphemeralUserGroupAssociation, | ||
[] | ||
>( | ||
'ephemeralUserGroupAssociation.insertOne', | ||
['userKeycloakUserId', 'userGroupKeycloakOrganizationId'], | ||
[], | ||
); | ||
|
||
export { createEphemeralUserGroupAssociation }; |
2 changes: 2 additions & 0 deletions
2
src/database/operations/ephemeralUserGroupAssociations/index.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './createEphemeralUserGroupAssociation'; | ||
export * from './removeEphemeralUserGroupAssociationsByUserKeycloakUserId'; |
10 changes: 10 additions & 0 deletions
10
...phemeralUserGroupAssociations/removeEphemeralUserGroupAssociationsByUserKeycloakUserId.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { generateRemoveOperation } from '../generators'; | ||
import type { KeycloakId } from '../../../types'; | ||
|
||
const removeEphemeralUserGroupAssociationsByUserKeycloakUserId = | ||
generateRemoveOperation<[userKeycloakUserId: KeycloakId]>( | ||
'ephemeralUserGroupAssociations.deleteByUserKeycloakUserId', | ||
['userKeycloakUserId'], | ||
); | ||
|
||
export { removeEphemeralUserGroupAssociationsByUserKeycloakUserId }; |
This file contains 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
3 changes: 3 additions & 0 deletions
3
src/database/queries/ephemeralUserGroupAssociations/deleteByUserKeycloakUserId.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
DELETE FROM ephemeral_user_group_associations | ||
WHERE user_keycloak_user_id = :userKeycloakUserId | ||
RETURNING *; |
15 changes: 15 additions & 0 deletions
15
src/database/queries/ephemeralUserGroupAssociations/insertOne.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
INSERT INTO ephemeral_user_group_associations ( | ||
user_keycloak_user_id, | ||
user_group_keycloak_organization_id | ||
) VALUES ( | ||
:userKeycloakUserId, | ||
:userGroupKeycloakOrganizationId | ||
) | ||
ON CONFLICT ( | ||
user_keycloak_user_id, permission, user_group_keycloak_organization_id | ||
) DO UPDATE | ||
SET user_keycloak_user_id = user_keycloak_user_id | ||
RETURNING | ||
ephemeral_user_group_association_to_json( | ||
ephemeral_user_group_associations | ||
) AS object; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { KeycloakId } from './KeycloakId'; | ||
import { Writable } from './Writable'; | ||
|
||
interface EphemeralUserGroupAssociation { | ||
readonly userKeycloakUserId: KeycloakId; | ||
readonly userGroupKeycloakOrganizationId: KeycloakId; | ||
readonly createdAt: string; | ||
} | ||
|
||
type WritableEphemeralUserGroupAssociation = | ||
Writable<EphemeralUserGroupAssociation>; | ||
|
||
type InternallyWritableEphemeralUserGroupAssociation = | ||
WritableEphemeralUserGroupAssociation & | ||
Pick< | ||
EphemeralUserGroupAssociation, | ||
'userKeycloakUserId' | 'userGroupKeycloakOrganizationId' | ||
>; | ||
|
||
export { | ||
EphemeralUserGroupAssociation, | ||
InternallyWritableEphemeralUserGroupAssociation, | ||
WritableEphemeralUserGroupAssociation, | ||
}; |
This file contains 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
This file contains 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