generated from ministryofjustice/cloud-platform-terraform-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadd-github-teams-to-aws-saml.js
61 lines (49 loc) · 1.92 KB
/
add-github-teams-to-aws-saml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const axios = require('axios');
exports.onExecutePostLogin = async (event, api) => {
if (event.connection.name === "github") {
const samlProvider = event.secrets.AWS_SAML_PROVIDER_NAME;
const awsAccount = event.secrets.AWS_ACCOUNT_ID;
const filterApiKey = event.secrets.FILTER_API_KEY;
const rolePrefix = "arn:aws:iam::" + awsAccount;
const role = "access-via-github";
const samlIdP = rolePrefix + ":saml-provider/" + samlProvider;
const filterUrl = "https://github-teams-filter.apps.live.cloud-platform.service.justice.gov.uk/filter-teams"
const git_teams = event.user.user_metadata["gh_teams"].map((t) =>
t.replace("github:", ""),
);
const filteredTeams = git_teams.filter((t) => t != "all-org-members");
const teamsString = ":" + filteredTeams.join(":") + ":";
let filteredTeamsString;
try {
const response = await axios.post(filterUrl,
{ teams: teamsString },
{
headers: {
"X-API-Key": filterApiKey,
"Content-Type": "application/json"
}
});
const result = response.data;
filteredTeamsString = result["filtered_teams"];
} catch (error) {
console.error("Error whilst calling github-teams-filter API:", error);
filteredTeamsString = teamsString;
}
api.user.GithubTeam = filteredTeamsString;
api.user.awsRoleSession = event.user.nickname;
api.user.awsTagKeys = ["GithubTeam"];
api.user.awsRole = rolePrefix + ":role/" + role + "," + samlIdP;
api.samlResponse.setAttribute(
"https://aws.amazon.com/SAML/Attributes/Role",
rolePrefix + ":role/" + role + "," + samlIdP,
);
api.samlResponse.setAttribute(
"https://aws.amazon.com/SAML/Attributes/RoleSessionName",
event.user.nickname,
);
api.samlResponse.setAttribute(
"https://aws.amazon.com/SAML/Attributes/PrincipalTag:GithubTeam",
filteredTeamsString,
);
}
};