-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from guardian/users-refresher-lambda
users-refresher-lambda
- Loading branch information
Showing
20 changed files
with
1,414 additions
and
700 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,40 +1,11 @@ | ||
import * as AWS from "aws-sdk"; | ||
import { STAGE, standardAwsConfig } from "./awsIntegration"; | ||
import { standardAwsConfig } from "../../shared/awsIntegration"; | ||
import { getPinboardPermissionOverrides } from "../../shared/permissions"; | ||
|
||
const s3Client = new AWS.S3(standardAwsConfig); | ||
|
||
interface Permission { | ||
permission: { | ||
name: string; | ||
app: string; | ||
}; | ||
overrides: Array<{ | ||
userId: string; | ||
active: boolean; | ||
}>; | ||
} | ||
const S3 = new AWS.S3(standardAwsConfig); | ||
|
||
export const userHasPermission = (userEmail: string): Promise<boolean> => | ||
s3Client | ||
.getObject({ | ||
Bucket: "permissions-cache", | ||
Key: `${STAGE}/permissions.json`, | ||
}) | ||
.promise() | ||
.then(({ Body }) => { | ||
if (!Body) { | ||
throw Error("could not read permissions"); | ||
} | ||
|
||
const permissions: Permission[] = JSON.parse(Body.toString()); | ||
|
||
// see https://github.com/guardian/permissions/pull/128 | ||
return !!permissions.find( | ||
(_) => | ||
_.permission.app === "pinboard" && | ||
_.permission.name === "pinboard" && | ||
_.overrides.find( | ||
(override) => override.userId === userEmail && override.active | ||
) | ||
); | ||
}); | ||
getPinboardPermissionOverrides(S3).then( | ||
(overrides) => | ||
!!overrides?.find(({ userId, active }) => userId === userEmail && active) | ||
); |
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
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
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
2 changes: 1 addition & 1 deletion
2
bootstrapping-lambda/src/awsIntegration.ts → shared/awsIntegration.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
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 @@ | ||
export const userTableTTLAttribute = "ttlEpochSeconds"; |
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 @@ | ||
const STAGE = process.env.STAGE || "LOCAL"; | ||
|
||
const pandaConfigFilenameLookup: { [stage: string]: string } = { | ||
PROD: "gutools.co.uk.settings", | ||
CODE: "code.dev-gutools.co.uk.settings", | ||
LOCAL: "local.dev-gutools.co.uk.settings", | ||
} as const; | ||
|
||
export const pandaSettingsBucketName = "pan-domain-auth-settings"; | ||
|
||
export const pandaConfigFilename = | ||
pandaConfigFilenameLookup[STAGE] || pandaConfigFilenameLookup["LOCAL"]; | ||
|
||
export const pandaPublicConfigFilename = `${pandaConfigFilename}.public`; |
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,35 @@ | ||
import { STAGE } from "./awsIntegration"; | ||
import * as AWS from "aws-sdk"; | ||
|
||
interface Override { | ||
userId: string; | ||
active: boolean; | ||
} | ||
|
||
interface Permission { | ||
permission: { | ||
name: string; | ||
app: string; | ||
}; | ||
overrides: Override[]; | ||
} | ||
|
||
export const getPinboardPermissionOverrides = (S3: AWS.S3) => | ||
S3.getObject({ | ||
Bucket: "permissions-cache", | ||
Key: `${STAGE}/permissions.json`, | ||
}) | ||
.promise() | ||
.then(({ Body }) => { | ||
if (!Body) { | ||
throw Error("could not read permissions"); | ||
} | ||
|
||
const allPermissions = JSON.parse(Body.toString()) as Permission[]; | ||
|
||
return allPermissions.find( | ||
({ permission }) => | ||
// see https://github.com/guardian/permissions/pull/128 | ||
permission.app === "pinboard" && permission.name === "pinboard" | ||
)?.overrides; | ||
}); |
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,23 @@ | ||
{ | ||
"name": "users-refresher-lambda", | ||
"version": "1.0.0", | ||
"description": "Lambda to keep the users Dynamo table(s) up to date (run on a schedule)", | ||
"repository": "https://github.com/guardian/editorial-tools-pinboard", | ||
"scripts": { | ||
"watch": "ts-node-dev --respawn run.ts", | ||
"build": "ncc build src/index.ts -o dist -m -e aws-sdk" | ||
}, | ||
"devDependencies": { | ||
"@types/iniparser": "^0.0.29", | ||
"@types/node-forge": "^0.9.7", | ||
"@vercel/ncc": "^0.25.1", | ||
"aws-sdk": "^2.840.0", | ||
"ts-node-dev": "^1.0.0" | ||
}, | ||
"dependencies": { | ||
"@googleapis/admin": "^0.2.0", | ||
"@googleapis/people": "^0.2.1", | ||
"iniparser": "^1.0.5", | ||
"node-forge": "^0.10.0" | ||
} | ||
} |
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 @@ | ||
import { handler } from "./src"; | ||
|
||
handler().then(console.log).catch(console.error); |
Oops, something went wrong.