Skip to content

Commit

Permalink
allow access ssm parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
shtukas committed Jan 20, 2025
1 parent 8f4af1b commit 26e116b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 27 deletions.
22 changes: 22 additions & 0 deletions cdk/lib/__snapshots__/dotcom-components.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions cdk/lib/dotcom-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
GuStringParameter,
} from '@guardian/cdk/lib/constructs/core';
import {
GuAllowPolicy,
GuDynamoDBReadPolicy,
GuGetS3ObjectsPolicy,
GuPutCloudwatchMetricsPolicy,
Expand Down Expand Up @@ -207,7 +208,11 @@ chown -R dotcom-components:support /var/log/dotcom-components
new GuDynamoDBReadPolicy(this, 'DynamoBanditReadPolicy', {
tableName: `support-bandit-${this.stage}`,
}),
];
new GuAllowPolicy(this, 'SSMGet', {
actions: ['ssm:GetParameter'],
resources: ['*'],
}),
];

const scaling: GuAsgCapacity = {
minimumInstances: this.stage === 'CODE' ? 1 : 3,
Expand Down Expand Up @@ -249,5 +254,5 @@ chown -R dotcom-components:support /var/log/dotcom-components
ec2App.autoScalingGroup.scaleOnCpuUtilization('CpuScalingPolicy', {
targetUtilizationPercent: 40,
});
}
}
}
66 changes: 43 additions & 23 deletions src/server/api/auxiaProxyRouter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express, { Router } from 'express';
import { getSsmValue } from '../utils/ssm';
import fetch from 'node-fetch';

interface AuxiaApiRequestPayloadContextualAttributes {
key: string;
Expand Down Expand Up @@ -52,17 +53,7 @@ interface AuxiaProxyResponseData {
shouldShowSignInGate: boolean;
}

const buildAuxiaAPIRequestPayload = async (): Promise<AuxiaAPIRequestPayload> => {
const projectId = await getSsmValue('PROD', 'auxia-projectId');
if (projectId === undefined) {
throw new Error('auxia-projectId is undefined');
}

const userId = await getSsmValue('PROD', 'auxia-userId');
if (userId === undefined) {
throw new Error('auxia-userId is undefined');
}

const buildAuxiaAPIRequestPayload = (projectId: string, userId: string): AuxiaAPIRequestPayload => {
// For the moment we are hard coding the data provided in contextualAttributes and surfaces.
return {
projectId: projectId,
Expand All @@ -88,22 +79,19 @@ const buildAuxiaAPIRequestPayload = async (): Promise<AuxiaAPIRequestPayload> =>
};
};

const fetchAuxiaData = async (): Promise<AuxiaAPIAnswerData> => {
const fetchAuxiaData = async (
apiKey: string,
projectId: string,
userId: string,
): Promise<AuxiaAPIAnswerData> => {
const url = 'https://apis.auxia.io/v1/GetTreatments';

// We are hardcoding PROD for the moment, because I haven't created a CODE key
const apiKey = await getSsmValue('PROD', 'auxia-api-key');

if (apiKey === undefined) {
throw new Error('auxia-api-key is undefined');
}

const headers = {
'Content-Type': 'application/json',
'x-api-key': apiKey,
};

const payload = await buildAuxiaAPIRequestPayload();
const payload = await buildAuxiaAPIRequestPayload(projectId, userId);

const params = {
method: 'POST',
Expand All @@ -130,9 +118,37 @@ const buildAuxiaProxyResponseData = (auxiaData: AuxiaAPIAnswerData): AuxiaProxyR
return { shouldShowSignInGate };
};

export const buildAuxiaProxyRouter = (): Router => {
const router = Router();
interface AuxiaRouterConfig {
apiKey: string;
projectId: string;
userId: string;
}

export const getAuxiaRouterConfig = async (): Promise<AuxiaRouterConfig> => {
const apiKey = await getSsmValue('PROD', 'auxia-api-key');
if (apiKey === undefined) {
throw new Error('auxia-api-key is undefined');
}

const projectId = await getSsmValue('PROD', 'auxia-projectId');
if (projectId === undefined) {
throw new Error('auxia-projectId is undefined');
}

const userId = await getSsmValue('PROD', 'auxia-userId');
if (userId === undefined) {
throw new Error('auxia-userId is undefined');
}

return {
apiKey,
projectId,
userId,
};
};

export const buildAuxiaProxyRouter = (config: AuxiaRouterConfig): Router => {
const router = Router();
router.post(
'/auxia',

Expand All @@ -142,7 +158,11 @@ export const buildAuxiaProxyRouter = (): Router => {

async (req: express.Request, res: express.Response, next: express.NextFunction) => {
try {
const auxiaData = await fetchAuxiaData();
const auxiaData = await fetchAuxiaData(
config.apiKey,
config.projectId,
config.userId,
);
const response = buildAuxiaProxyResponseData(auxiaData);

res.send(response);
Expand Down
6 changes: 4 additions & 2 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { logError } from './utils/logging';
import { buildEpicRouter } from './api/epicRouter';
import { buildBannerRouter } from './api/bannerRouter';
import { buildHeaderRouter } from './api/headerRouter';
import { buildAuxiaProxyRouter } from './api/auxiaProxyRouter';
import { buildAuxiaProxyRouter, getAuxiaRouterConfig } from './api/auxiaProxyRouter';
import { buildAmpEpicRouter } from './api/ampEpicRouter';
import { buildChannelSwitchesReloader } from './channelSwitches';
import { buildSuperModeArticlesReloader } from './lib/superMode';
Expand Down Expand Up @@ -114,7 +114,7 @@ const buildApp = async (): Promise<Express> => {
),
);
app.use(buildHeaderRouter(channelSwitches, headerTests));
app.use(buildAuxiaProxyRouter());

app.use('/amp', buildAmpEpicRouter(choiceCardAmounts, tickerData, ampEpicTests));

app.use(errorHandlingMiddleware);
Expand All @@ -124,6 +124,8 @@ const buildApp = async (): Promise<Express> => {
res.send('OK');
});

app.use(buildAuxiaProxyRouter(await getAuxiaRouterConfig()));

return Promise.resolve(app);
};

Expand Down

0 comments on commit 26e116b

Please sign in to comment.