Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pf/reuters poller #92

Draft
wants to merge 4 commits into
base: pf/ap-poller-business-logic
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
847 changes: 847 additions & 0 deletions cdk/lib/__snapshots__/newswires.test.ts.snap

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions cdk/lib/__snapshots__/riffraff.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ deployments:
fileName: poller-lambdas.zip
actions:
- uploadLambda
lambda-upload-eu-west-1-editorial-feeds-reuters_poller_lambda:
type: aws-lambda
stacks:
- editorial-feeds
regions:
- eu-west-1
app: reuters_poller_lambda
contentDirectory: poller-lambdas
parameters:
bucketSsmLookup: true
lookupByTags: true
fileName: poller-lambdas.zip
actions:
- uploadLambda
asg-upload-eu-west-1-editorial-feeds-newswires:
type: autoscaling
actions:
Expand Down Expand Up @@ -79,6 +93,7 @@ deployments:
dependencies:
- lambda-upload-eu-west-1-editorial-feeds-ingestion-lambda
- lambda-upload-eu-west-1-editorial-feeds-apPoller_poller_lambda
- lambda-upload-eu-west-1-editorial-feeds-reuters_poller_lambda
- asg-upload-eu-west-1-editorial-feeds-newswires
- cfn-eu-west-1-editorial-feeds-wires-feeds
lambda-update-eu-west-1-editorial-feeds-ingestion-lambda:
Expand Down Expand Up @@ -113,6 +128,22 @@ deployments:
- updateLambda
dependencies:
- cfn-eu-west-1-editorial-feeds-newswires
lambda-update-eu-west-1-editorial-feeds-reuters_poller_lambda:
type: aws-lambda
stacks:
- editorial-feeds
regions:
- eu-west-1
app: reuters_poller_lambda
contentDirectory: poller-lambdas
parameters:
bucketSsmLookup: true
lookupByTags: true
fileName: poller-lambdas.zip
actions:
- updateLambda
dependencies:
- cfn-eu-west-1-editorial-feeds-newswires
asg-update-eu-west-1-editorial-feeds-newswires:
type: autoscaling
actions:
Expand Down
1 change: 1 addition & 0 deletions cdk/lib/constructs/pollerLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class PollerLambda {
});

secret.grantRead(lambda);
secret.grantWrite(lambda);

// wire up lambda to process its own queue
lambda.addEventSource(new SqsEventSource(lambdaQueue, { batchSize: 1 }));
Expand Down
120 changes: 67 additions & 53 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,55 +1,69 @@
{
"name": "newswires",
"version": "1.0.0",
"description": "Newswires feed app",
"workspaces": [
"cdk",
"ingestion-lambda",
"poller-lambdas"
],
"scripts": {
"test": "npm run test --workspaces --if-present",
"synth": "npm run synth --workspace=cdk",
"typecheck": "npm run typecheck --workspaces",
"format": "prettier --write --ignore-path .gitignore \"./**/*{.js,.ts,.json,.md,.yml}\"",
"build": "npm run build --workspaces --if-present",
"lint": "npm run lint --workspaces",
"lint:ci": "npm run lint:ci --workspaces",
"dev": "npm run dev --workspace=ingestion-lambda"
},
"repository": {
"type": "git",
"url": "github.com/guardian/newswires"
},
"devDependencies": {
"@guardian/eslint-config-typescript": "8.0.0",
"@guardian/prettier": "5.0.0",
"@guardian/tsconfig": "1.0.0",
"@types/jest": "29.5.12",
"@types/node": "22.4.2",
"esbuild": "^0.24.0",
"eslint": "8.57.0",
"jest": "29.7.0",
"prettier": "3.3.3",
"ts-jest": "29.2.4",
"ts-node-dev": "^2.0.0",
"typescript": "5.1.6"
},
"prettier": "@guardian/prettier",
"eslintConfig": {
"extends": [
"@guardian/eslint-config-typescript"
],
"ignorePatterns": [
"**/*.js",
"node_modules",
"cdk.out",
".eslintrc.js",
"jest.config.js",
"**/generated/*"
]
},
"dependencies": {
"zod": "3.23.8"
}
"name": "newswires",
"version": "1.0.0",
"description": "Newswires feed app",
"workspaces": [
"cdk",
"ingestion-lambda",
"poller-lambdas"
],
"scripts": {
"test": "npm run test --workspaces --if-present",
"synth": "npm run synth --workspace=cdk",
"typecheck": "npm run typecheck --workspaces",
"format": "prettier --write --ignore-path .gitignore \"./**/*{.js,.ts,.json,.md,.yml}\"",
"build": "npm run build --workspaces --if-present",
"lint": "npm run lint --workspaces",
"lint:ci": "npm run lint:ci --workspaces",
"dev": "npm run dev --workspace=ingestion-lambda"
},
"repository": {
"type": "git",
"url": "github.com/guardian/newswires"
},
"devDependencies": {
"@guardian/eslint-config-typescript": "8.0.0",
"@guardian/prettier": "5.0.0",
"@guardian/tsconfig": "1.0.0",
"@types/jest": "29.5.12",
"@types/node": "22.4.2",
"esbuild": "^0.24.0",
"eslint": "8.57.0",
"jest": "29.7.0",
"prettier": "3.3.3",
"ts-jest": "29.2.4",
"ts-node-dev": "^2.0.0",
"typescript": "5.1.6"
},
"prettier": "@guardian/prettier",
"eslintConfig": {
"extends": [
"@guardian/eslint-config-typescript"
],
"rules": {
"@typescript-eslint/no-unused-vars": [
"error",
{
"args": "all",
"argsIgnorePattern": "^_",
"caughtErrors": "all",
"caughtErrorsIgnorePattern": "^_",
"destructuredArrayIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"ignoreRestSiblings": true
}
]
},
"ignorePatterns": [
"**/*.js",
"node_modules",
"cdk.out",
".eslintrc.js",
"jest.config.js",
"**/generated/*"
]
},
"dependencies": {
"zod": "3.23.8"
}
}
25 changes: 21 additions & 4 deletions poller-lambdas/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import { GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
import {
GetSecretValueCommand,
PutSecretValueCommand,
} from '@aws-sdk/client-secrets-manager';
import type { SendMessageCommandInput } from '@aws-sdk/client-sqs';
import { SendMessageCommand } from '@aws-sdk/client-sqs';
import type { PollerId } from '../../shared/pollers';
import { POLLER_LAMBDA_ENV_VAR_KEYS } from '../../shared/pollers';
import { queueNextInvocation, secretsManager, sqs } from './aws';
import { getEnvironmentVariableOrCrash } from './config';
import { apPoller } from './pollers/AP/apPoller';
import { reutersPoller } from './pollers/reuters/reutersPoller';
import type { HandlerInputSqsPayload, PollFunction } from './types';
import { isFixedFrequencyPollOutput } from './types';

const pollerWrapper =
(pollerFunction: PollFunction) =>
async ({ Records }: HandlerInputSqsPayload) => {
const startTimeEpochMillis = Date.now();
const secretName = getEnvironmentVariableOrCrash(
POLLER_LAMBDA_ENV_VAR_KEYS.SECRET_NAME,
);
const secret = await secretsManager
.send(
new GetSecretValueCommand({
SecretId: getEnvironmentVariableOrCrash(
POLLER_LAMBDA_ENV_VAR_KEYS.SECRET_NAME,
),
SecretId: secretName,
}),
)
.then((_) => _.SecretString);
Expand Down Expand Up @@ -89,6 +94,17 @@ const pollerWrapper =
MessageBody: output.valueForNextPoll,
});
}

if (output.newSecretValue) {
// set new value in secrets manager
console.log(`Updating secret value for ${secretName}`);
await secretsManager.send(
new PutSecretValueCommand({
SecretId: secretName,
SecretString: output.newSecretValue,
}),
);
}
})
.catch((error) => {
console.error('FAILED', error);
Expand All @@ -102,6 +118,7 @@ export const handlers = {
// EXAMPLE_long_polling: pollerWrapper(EXAMPLE_long_polling),
// EXAMPLE_fixed_frequency: pollerWrapper(EXAMPLE_fixed_frequency),
apPoller: pollerWrapper(apPoller),
reuters: pollerWrapper(reutersPoller),
} satisfies Record<
PollerId,
(sqsEvent: HandlerInputSqsPayload) => Promise<void>
Expand Down
39 changes: 39 additions & 0 deletions poller-lambdas/src/pollers/reuters/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { z } from 'zod';

const AuthSchema = z.object({
access_token: z.string(),
expires_in: z.number(),
token_type: z.string(),
});

const scopes =
'https://api.thomsonreuters.com/auth/reutersconnect.contentapi.read https://api.thomsonreuters.com/auth/reutersconnect.contentapi.write';
const authUrl = 'https://auth.thomsonreuters.com/oauth/token';
const grantType = 'client_credentials';
const audience = '7a14b6a2-73b8-4ab2-a610-80fb9f40f769';

export async function auth(
clientId: string,
clientSecret: string,
): Promise<string> {
const req = new Request(authUrl, {
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
body: `grant_type=${grantType}&client_id=${clientId}&client_secret=${clientSecret}&audience=${audience}&scope=${encodeURIComponent(scopes)}`,
});
try {
console.log('Requesting new auth token from Reuters');
const response = await fetch(req);
const data = (await response.json()) as unknown;
const { access_token, expires_in } = AuthSchema.parse(data);
console.log(
`Received new auth token from Reuters, expires in ${expires_in} seconds`,
);
return access_token;
} catch (error) {
console.error(error);
throw new Error('Failed to get auth token from Reuters');
}
}
Loading
Loading