Skip to content

Commit

Permalink
Use a set for relevant IAMs
Browse files Browse the repository at this point in the history
  • Loading branch information
valtterikantanen committed Nov 15, 2024
1 parent 567114d commit a4807a8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
37 changes: 20 additions & 17 deletions src/auth/IAMConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,23 +511,26 @@ const iamToFaculty = (iam) => {
}
}

const relevantIAMs = []
.concat(Object.keys(joryMap))
.concat(Object.keys(kojoMap))
.concat(Object.values(kojoMap))
.concat(Object.keys(kosuFacultyMap))
.concat(Object.keys(dekaaniFacultyMap))
.concat(opetusVaradekaani)
.concat(doctoralIams)
.concat(Object.keys(doctoralSchoolMap))
.concat(universityWideGroups)
.concat(superAdminGroups)
.concat(adminGroups)
.concat(openUniGroups)
.concat(hyOneGroups)
.concat(employeeGroups)
.concat(oodikoneIams)
.concat(katselmusViewerIams)
const relevantIAMs = new Set(
[
Object.keys(joryMap),
Object.keys(kojoMap),
Object.values(kojoMap),
Object.keys(kosuFacultyMap),
Object.keys(dekaaniFacultyMap),
opetusVaradekaani,
doctoralIams,
Object.keys(doctoralSchoolMap),
universityWideGroups,
superAdminGroups,
adminGroups,
openUniGroups,
hyOneGroups,
employeeGroups,
oodikoneIams,
katselmusViewerIams,
].flat(),
)

const RELEVANT_ORGANISATION_CODES = [
'H930', // Avoin yliopisto
Expand Down
12 changes: 4 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ app.get('/ping', (_req, res) => res.send('pong'))
app.post('/', async (req, res) => {
const { userId, iamGroups = [], getSisuAccess = false } = req.body

const relevantIamGroups = iamGroups.filter((iam) =>
relevantIAMs.includes(iam),
)
const relevantIamGroups = iamGroups.filter((iam) => relevantIAMs.has(iam))

const { access, specialGroup } = getIAMRights(relevantIamGroups)

Expand Down Expand Up @@ -76,7 +74,7 @@ app.get('/all-access', async (_req, res) => {
const users = await User.findAll()

const usersWithAccess = users.map(({ dataValues: user }) => {
const iamGroups = user.iamGroups.filter((iam) => relevantIAMs.includes(iam))
const iamGroups = user.iamGroups.filter((iam) => relevantIAMs.has(iam))

return {
...user,
Expand Down Expand Up @@ -107,9 +105,7 @@ app.post('/access-and-special-groups', async (req, res) => {
users.map(({ dataValues: user }) => user),
50,
async (user) => {
const iamGroups = user.iamGroups.filter((iam) =>
relevantIAMs.includes(iam),
)
const iamGroups = user.iamGroups.filter((iam) => relevantIAMs.has(iam))
const { access, specialGroup } = getIAMRights(iamGroups)
specialGroup.fullSisuAccess = await hasFullSisuAccess(user.id)

Expand Down Expand Up @@ -153,7 +149,7 @@ app.get('/:id', async (req, res) => {

if (!user) return res.sendStatus(404)

const iamGroups = user.iamGroups.filter((iam) => relevantIAMs.includes(iam))
const iamGroups = user.iamGroups.filter((iam) => relevantIAMs.has(iam))

return res.send({
...user.dataValues,
Expand Down
3 changes: 2 additions & 1 deletion tests/chaos.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ describe('Chaos test', () => {
it('User IAM endpoint should not crash', async () => {
const getRandomIams = () => {
const randomIams = []
const iamArray = [...relevantIAMs]
for (let i = 0; i < 5 + Math.ceil(Math.random() * 10); i++) {
// 50% are random strings
if (Math.random() < 0.5) {
randomIams.push(Math.random().toString(36).substring(2, 15))
} else {
const randomIam =
relevantIAMs[Math.floor(Math.random() * relevantIAMs.length)]
iamArray[Math.floor(Math.random() * iamArray.length)]
randomIams.push(randomIam)
}
}
Expand Down

0 comments on commit a4807a8

Please sign in to comment.