Skip to content

Commit

Permalink
Merge pull request #144 from hmcts/marty/DTSPO-15781-bugfix
Browse files Browse the repository at this point in the history
Updating functions for role/group assignment to be more specific when searching for the base User group
  • Loading branch information
MartyFox committed Jan 23, 2024
2 parents 1150334 + 783c4d5 commit aeee8d4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 24 deletions.
6 changes: 4 additions & 2 deletions apps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ apps:
- name: "My app101"
externalUrl: "https://my-app.app-proxy-poc.sandbox.platform.hmcts.net"
logoUrl: https://raw.githubusercontent.com/hmcts/azure-app-proxy/main/logos/incident-bot.png
appRoleAssignments:
- Test app
internalUrl: "https://my-on-bau101.sandbox.platform.hmcts.net"
userAssignmentRequired: true
tls:
Expand All @@ -18,13 +16,17 @@ apps:
optionalClaims:
- name: "groups"
additionalProperties: []
appRoleAssignments:
- Test app
- test_group_A
appRoles:
- displayName: "A first app role"
description: "Some description"
value: "testing"
id: "aa9aaaaa-2aa8-49aa-954a-a3aaaa08aaa3"
groups:
- "test_group_A"
- "Test app"
- displayName: "My second app role"
description: "Some description"
value: "testing_again"
Expand Down
15 changes: 12 additions & 3 deletions src/applicationManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import { DefaultAzureCredential } from "@azure/identity";
import { expect, describe, test, beforeAll, afterEach } from "vitest";
import { defaultOnPremisesFlags } from "./configuration";
import {
assignGroups,
assignUserRoleToGroups,
readServicePrincipal,
setUserAssignmentRequired,
isAppRoleAssignedToGroup,
getEntraGroupId,
getAppRoleId,
} from "./servicePrincipalManager";
import * as process from "process";

Expand Down Expand Up @@ -147,7 +148,8 @@ describe("applicationManager", () => {
objectId: appDetails.servicePrincipalObjectId,
assignmentRequired: false,
});
await assignGroups({

await assignUserRoleToGroups({
token,
objectId: appDetails.servicePrincipalObjectId,
groups: [groupNameForRoleAssignments],
Expand Down Expand Up @@ -187,14 +189,21 @@ describe("applicationManager", () => {
applicationId: appDetails.applicationId,
});

let testAppRoleId = await getAppRoleId({
token,
objectId: appDetails.servicePrincipalObjectId,
displayName: application.appRoles[0].displayName,
}); // Find app role id

expect(application.appRoles[0].displayName).toEqual("Some name");
expect(
await isAppRoleAssignedToGroup({
token,
groupId: groupId,
objectId: appDetails.servicePrincipalObjectId,
appRoleId: testAppRoleId,
}),
).toEqual(true);
).toBeTruthy();
expect(application.groupMembershipClaims).toEqual("SecurityGroup");
expect(application.optionalClaims.saml2Token[0].name).toEqual("groups");

Expand Down
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from "./applicationManager.js";
import { loadApps } from "./configuration.js";
import {
assignGroups,
assignUserRoleToGroups,
setUserAssignmentRequired,
enableSaml,
} from "./servicePrincipalManager.js";
Expand Down Expand Up @@ -82,7 +82,8 @@ for await (const app of apps) {
objectId: servicePrincipalObjectId,
assignmentRequired: app.appRoleAssignmentRequired,
});
await assignGroups({

await assignUserRoleToGroups({
token,
objectId: servicePrincipalObjectId,
groups: app.appRoleAssignments,
Expand Down
55 changes: 38 additions & 17 deletions src/servicePrincipalManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,33 @@ export async function findExistingServicePrincipal({
return undefined;
}

async function getAppRoleId(objectId: string, token: string) {
const result = await fetch(
`https://graph.microsoft.com/beta/servicePrincipals/${objectId}/appRoles`,
{
method: "GET",
headers: {
Authorization: `Bearer ${token}`,
},
export async function getAppRoleId({
token,
objectId,
displayName,
}: {
token: string;
objectId: string;
displayName: string;
}) {
const url = `https://graph.microsoft.com/beta/servicePrincipals/${objectId}/appRoles`;

const result = await fetch(url, {
method: "GET",
headers: {
Authorization: `Bearer ${token}`,
},
);
});

await errorHandler("finding app roles", result);
await errorHandler("finding app role Id", result);

const body = await result.json();
return body.value[0].id;
var appRole = body.value.find(
(element: any) => element.displayName === displayName,
);

console.log("App Role Id for role:", displayName, "found:", appRole.id);
return appRole.id;
}

export async function getEntraGroupId(groupName: string, token: string) {
Expand All @@ -114,10 +126,12 @@ export async function isAppRoleAssignedToGroup({
groupId,
objectId,
token,
appRoleId,
}: {
groupId: string;
objectId: string;
token: string;
appRoleId: string;
}) {
const result = await fetch(
`https://graph.microsoft.com/v1.0/groups/${groupId}/appRoleAssignments?$filter=resourceId eq ${objectId}`,
Expand All @@ -129,14 +143,16 @@ export async function isAppRoleAssignedToGroup({
},
);

await errorHandler("finding if app role is already assigned", result);
await errorHandler("Checking if app role is already assigned", result);

const body = await result.json();
const appRole =
body.value.find((element: any) => element.appRoleId === appRoleId) || false;

return body.value.length === 1;
return appRole;
}

async function assignGroup({
async function assignRoleToGroup({
group,
token,
objectId,
Expand All @@ -153,6 +169,7 @@ async function assignGroup({
groupId,
objectId,
token,
appRoleId,
});

if (appRoleAssignedAlready) {
Expand Down Expand Up @@ -184,7 +201,7 @@ async function assignGroup({
}
}

export async function assignGroups({
export async function assignUserRoleToGroups({
token,
objectId,
groups,
Expand All @@ -194,10 +211,14 @@ export async function assignGroups({
token: string;
}) {
if (groups.length > 0) {
const appRoleId = await getAppRoleId(objectId, token);
const appRoleId = await getAppRoleId({
token,
objectId,
displayName: "User",
}); // Find User app role id

for await (const group of groups) {
await assignGroup({ group, token, objectId, appRoleId });
await assignRoleToGroup({ group, token, objectId, appRoleId });
}
}
}
Expand Down

0 comments on commit aeee8d4

Please sign in to comment.