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

Publish status messages (read in Fronts tool) #51

Merged
merged 15 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
27 changes: 26 additions & 1 deletion cdk/lib/__snapshots__/recipes-backend.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ exports[`The RecipesBackend stack matches the snapshot 1`] = `
"GuParameter",
"GuParameter",
"GuParameter",
"GuParameter",
"GuParameter",
"GuLambdaFunction",
"GuLambdaFunction",
"GuLambdaErrorPercentageAlarm",
Expand Down Expand Up @@ -75,6 +77,16 @@ exports[`The RecipesBackend stack matches the snapshot 1`] = `
"Default": "/account/content-api-common/alarms/urgent-alarm-topic",
"Type": "AWS::SSM::Parameter::Value<String>",
},
"faciaPublishStatusSNSTopicParam": {
"Default": "/TEST/content-api/recipes-responder/facia-status-sns-topic-arn",
"Description": "The ARN of the facia-tool SNS topic that receives publication status messages",
"Type": "AWS::SSM::Parameter::Value<String>",
},
"faciaPublishStatusSNSTopicRoleParam": {
"Default": "/TEST/content-api/recipes-responder/facia-status-sns-topic-role-arn",
"Description": "The ARN of role that permits us to write to faciaPublishStatusSNSTopic",
"Type": "AWS::SSM::Parameter::Value<String>",
},
"faciaSNSTopicParam": {
"Default": "/TEST/content-api/recipes-responder/facia-sns-topic-arn",
"Description": "The ARN of the facia-tool SNS topic that emits curation notifications",
Expand Down Expand Up @@ -1560,7 +1572,7 @@ def submit_response(event: dict, context, response_status: str, error_message: s
},
"Type": "AWS::SQS::QueuePolicy",
},
"RecipesFaciaConnectionRecipesBackendRecipesFaciafaciaSNSTopic4A8496A449BAE420": {
"RecipesFaciaConnectionRecipesBackendRecipesFaciafaciaPublishSNSTopicA854730B0579B71E": {
"DependsOn": [
"RecipesFaciaConnectionPolicyC7CB124D",
],
Expand Down Expand Up @@ -1635,6 +1647,12 @@ def submit_response(event: dict, context, response_status: str, error_message: s
"Variables": {
"APP": "recipes-facia-responder",
"CONTENT_URL_BASE": "recipes.guardianapis.com",
"FACIA_PUBLISH_STATUS_ROLE_ARN": {
"Ref": "faciaPublishStatusSNSTopicRoleParam",
},
"FACIA_PUBLISH_STATUS_TOPIC_ARN": {
"Ref": "faciaPublishStatusSNSTopicParam",
},
"FASTLY_API_KEY": {
"Ref": "fastlyKey",
},
Expand Down Expand Up @@ -1904,6 +1922,13 @@ def submit_response(event: dict, context, response_status: str, error_message: s
],
},
},
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Resource": {
"Ref": "faciaPublishStatusSNSTopicRoleParam",
},
},
{
"Action": [
"sqs:ReceiveMessage",
Expand Down
37 changes: 30 additions & 7 deletions cdk/lib/facia-connection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { GuParameter, GuStack } from '@guardian/cdk/lib/constructs/core';
import { GuLambdaFunction } from '@guardian/cdk/lib/constructs/lambda';
import { Duration } from 'aws-cdk-lib';
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { Effect, PolicyStatement, Role } from 'aws-cdk-lib/aws-iam';
import { Architecture, Runtime } from 'aws-cdk-lib/aws-lambda';
import { SqsEventSource } from 'aws-cdk-lib/aws-lambda-event-sources';
import { Topic } from 'aws-cdk-lib/aws-sns';
Expand All @@ -15,7 +15,9 @@ interface FaciaConnectionProps {
fastlyKeyParam: GuParameter;
serving: StaticServing;
externalParameters: ExternalParameters;
faciaSNSTopicARN: string;
faciaPublishStatusSNSRoleARN: string;
faciaPublishStatusSNSTopicARN: string;
faciaPublishSNSTopicARN: string;
contentUrlBase: string;
}

Expand All @@ -24,7 +26,9 @@ export class FaciaConnection extends Construct {
scope: GuStack,
id: string,
{
faciaSNSTopicARN,
faciaPublishSNSTopicARN,
faciaPublishStatusSNSTopicARN,
faciaPublishStatusSNSRoleARN,
externalParameters,
fastlyKeyParam,
serving,
Expand All @@ -33,10 +37,22 @@ export class FaciaConnection extends Construct {
) {
super(scope, id);

const faciaSNSTopic = Topic.fromTopicArn(
const faciaPublishStatusSNSTopic = Topic.fromTopicArn(
this,
'faciaSNSTopic',
faciaSNSTopicARN,
'faciaPublishStatusSNSTopic',
faciaPublishStatusSNSTopicARN,
);

const faciaPublishStatusSNSRole = Role.fromRoleArn(
scope,
'faciaPublishStatusSNSTopicRole',
faciaPublishStatusSNSRoleARN,
);

const faciaPublishSNSTopic = Topic.fromTopicArn(
this,
'faciaPublishSNSTopic',
faciaPublishSNSTopicARN,
);

const faciaDLQ = new Queue(this, 'DLQ');
Expand All @@ -49,7 +65,7 @@ export class FaciaConnection extends Construct {
},
});

faciaSNSTopic.addSubscription(new SqsSubscription(faciaQueue));
faciaPublishSNSTopic.addSubscription(new SqsSubscription(faciaQueue));

new GuLambdaFunction(scope, 'RecipesFaciaResponder', {
events: [
Expand All @@ -68,6 +84,8 @@ export class FaciaConnection extends Construct {
FASTLY_API_KEY: fastlyKeyParam.valueAsString,
STATIC_BUCKET: serving.staticBucket.bucketName,
CONTENT_URL_BASE: contentUrlBase,
FACIA_PUBLISH_STATUS_TOPIC_ARN: faciaPublishStatusSNSTopic.topicArn,
FACIA_PUBLISH_STATUS_ROLE_ARN: faciaPublishStatusSNSRole.roleArn
},
fileName: 'facia-responder.zip',
functionName: `RecipesFaciaResponder-${scope.stage}`,
Expand All @@ -86,6 +104,11 @@ export class FaciaConnection extends Construct {
resources: [serving.staticBucket.bucketArn + '/*'],
actions: ['s3:PutObject', 's3:ListObjects'],
}),
new PolicyStatement({
effect: Effect.ALLOW,
actions: ['sts:AssumeRole'],
resources: [faciaPublishStatusSNSRole.roleArn],
}),
],
memorySize: 256,
runtime: Runtime.NODEJS_18_X,
Expand Down
40 changes: 33 additions & 7 deletions cdk/lib/recipes-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,33 @@ export class RecipesBackend extends GuStack {
description: "ARN of the SNS topic to use for data submissions"
});

const faciaSNSTopicARNParam = new GuParameter(this, 'faciaSNSTopicParam', {
default: `/${this.stage}/${this.stack}/${app}/facia-sns-topic-arn`,
fromSSM: true,
description: 'The ARN of the facia-tool SNS topic that emits curation notifications',
});
const faciaSNSTopicARNParam = new GuParameter(this, 'faciaSNSTopicParam', {
default: `/${this.stage}/${this.stack}/${app}/facia-sns-topic-arn`,
fromSSM: true,
description: 'The ARN of the facia-tool SNS topic that emits curation notifications',
});
jonathonherbert marked this conversation as resolved.
Show resolved Hide resolved

const faciaPublishStatusSNSTopicParam = new GuParameter(
this,
'faciaPublishStatusSNSTopicParam',
{
default: `/${this.stage}/${this.stack}/${app}/facia-status-sns-topic-arn`,
fromSSM: true,
type: 'String',
description: 'The ARN of the facia-tool SNS topic that receives publication status messages',
},
);

const faciaPublishStatusSNSRoleARNParam = new GuParameter(
this,
'faciaPublishStatusSNSTopicRoleParam',
{
default: `/${this.stage}/${this.stack}/${app}/facia-status-sns-topic-role-arn`,
fromSSM: true,
type: 'String',
description: 'The ARN of role that permits us to write to faciaPublishStatusSNSTopic',
},
);

const contentUrlBase = this.stage === "CODE" ? "recipes.code.dev-guardianapis.com" : "recipes.guardianapis.com";

Expand Down Expand Up @@ -161,7 +183,11 @@ export class RecipesBackend extends GuStack {
fastlyKeyParam,
serving,
externalParameters,
faciaSNSTopicARN: faciaSNSTopicARNParam.valueAsString,
faciaPublishSNSTopicARN: faciaSNSTopicARNParam.valueAsString,
faciaPublishStatusSNSTopicARN:
faciaPublishStatusSNSTopicParam.valueAsString,
faciaPublishStatusSNSRoleARN:
faciaPublishStatusSNSRoleARNParam.valueAsString,
contentUrlBase
});

Expand Down Expand Up @@ -234,4 +260,4 @@ export class RecipesBackend extends GuStack {
);
}

}
}
4 changes: 4 additions & 0 deletions lambda/facia-responder/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { mandatoryParameter } from 'lib/recipes-data/src/lib/config';

export const faciaPublicationStatusTopicArn = mandatoryParameter("FACIA_PUBLISH_STATUS_TOPIC_ARN");
export const faciaPublicationStatusRoleArn = mandatoryParameter("FACIA_PUBLISH_STATUS_ROLE_ARN");
61 changes: 61 additions & 0 deletions lambda/facia-responder/src/facia-notifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { PublishCommand, SNSClient } from '@aws-sdk/client-sns';
import { fromTemporaryCredentials } from '@aws-sdk/credential-providers';
import {
faciaPublicationStatusRoleArn,
faciaPublicationStatusTopicArn,
} from './config';
import { getErrorMessage } from './util';

// The publication status event we send over SNS.
type PublicationStatusEventEnvelope = {
event: PublicationStatusEvent;
};

export type PublicationStatusEvent = {
edition: string;
issueDate: string;
version: string;
status:
| 'Started'
| 'Proofing'
| 'Proofed'
| 'Publishing'
| 'Published'
| 'Failed'
| 'PostProcessing';
message: string;
timestamp: number;
};

export async function notifyFaciaTool(
event: PublicationStatusEvent,
): Promise<void> {
const payload = JSON.stringify({ event } as PublicationStatusEventEnvelope);

console.log(
`Publishing publish event to SNS: ${payload} to ${faciaPublicationStatusTopicArn} via ${faciaPublicationStatusRoleArn}`,
);

const sns = new SNSClient({
region: 'eu-west-1',
credentials: fromTemporaryCredentials({
params: {
RoleArn: faciaPublicationStatusRoleArn,
RoleSessionName: 'recipes-backend-assume-role-access-for-sns',
},
}),
});

try {
const sendStatus = await sns.send(
new PublishCommand({
TopicArn: faciaPublicationStatusTopicArn,
Message: payload,
}),
);

console.log(`SNS status publish response: ${JSON.stringify(sendStatus)}`);
} catch (e) {
console.error(`Failed to publish to SNS: ${getErrorMessage(e)}`);
}
}
48 changes: 48 additions & 0 deletions lambda/facia-responder/src/fixtures/sns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export const validMessageContent = {
id: 'D9AEEA41-F8DB-4FC8-A0DA-275571EA7331',
edition: 'feast-northern-hemisphere',
version: 'v1',
issueDate: '2024-01-02',
fronts: {
'all-recipes': [
{
id: 'd353e2de-1a65-45de-85ca-d229bc1fafad',
title: 'Dish of the day',
body: '',
items: [
{
recipe: {
id: '14129325',
},
},
],
},
],
'meat-free': [
{
id: 'fa6ccb35-926b-4eff-b3a9-5d0ca88387ff',
title: 'Dish of the day',
body: '',
items: [
{
recipe: {
id: '14132263',
},
},
],
},
],
},
};

export const validMessage = {
Message: JSON.stringify(validMessageContent),
};

export const brokenMessage = {
...validMessage,
Message: JSON.stringify({
...validMessageContent,
issueDate: 'dfsdfsjk',
}),
};
Loading
Loading