Skip to content

Commit 05e20e5

Browse files
committed
allow access ssm parameters
1 parent 8f4af1b commit 05e20e5

File tree

4 files changed

+76
-27
lines changed

4 files changed

+76
-27
lines changed

cdk/lib/__snapshots__/dotcom-components.test.ts.snap

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cdk/lib/dotcom-components.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
GuStringParameter,
1010
} from '@guardian/cdk/lib/constructs/core';
1111
import {
12+
GuAllowPolicy,
1213
GuDynamoDBReadPolicy,
1314
GuGetS3ObjectsPolicy,
1415
GuPutCloudwatchMetricsPolicy,
@@ -207,7 +208,11 @@ chown -R dotcom-components:support /var/log/dotcom-components
207208
new GuDynamoDBReadPolicy(this, 'DynamoBanditReadPolicy', {
208209
tableName: `support-bandit-${this.stage}`,
209210
}),
210-
];
211+
new GuAllowPolicy(this, 'SSMGet', {
212+
actions: ['ssm:GetParameter'],
213+
resources: ['*'],
214+
}),
215+
];
211216

212217
const scaling: GuAsgCapacity = {
213218
minimumInstances: this.stage === 'CODE' ? 1 : 3,
@@ -249,5 +254,5 @@ chown -R dotcom-components:support /var/log/dotcom-components
249254
ec2App.autoScalingGroup.scaleOnCpuUtilization('CpuScalingPolicy', {
250255
targetUtilizationPercent: 40,
251256
});
252-
}
257+
}
253258
}

src/server/api/auxiaProxyRouter.ts

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import express, { Router } from 'express';
22
import { getSsmValue } from '../utils/ssm';
3+
import fetch from 'node-fetch';
34

45
interface AuxiaApiRequestPayloadContextualAttributes {
56
key: string;
@@ -52,17 +53,7 @@ interface AuxiaProxyResponseData {
5253
shouldShowSignInGate: boolean;
5354
}
5455

55-
const buildAuxiaAPIRequestPayload = async (): Promise<AuxiaAPIRequestPayload> => {
56-
const projectId = await getSsmValue('PROD', 'auxia-projectId');
57-
if (projectId === undefined) {
58-
throw new Error('auxia-projectId is undefined');
59-
}
60-
61-
const userId = await getSsmValue('PROD', 'auxia-userId');
62-
if (userId === undefined) {
63-
throw new Error('auxia-userId is undefined');
64-
}
65-
56+
const buildAuxiaAPIRequestPayload = (projectId: string, userId: string): AuxiaAPIRequestPayload => {
6657
// For the moment we are hard coding the data provided in contextualAttributes and surfaces.
6758
return {
6859
projectId: projectId,
@@ -88,22 +79,19 @@ const buildAuxiaAPIRequestPayload = async (): Promise<AuxiaAPIRequestPayload> =>
8879
};
8980
};
9081

91-
const fetchAuxiaData = async (): Promise<AuxiaAPIAnswerData> => {
82+
const fetchAuxiaData = async (
83+
apiKey: string,
84+
projectId: string,
85+
userId: string,
86+
): Promise<AuxiaAPIAnswerData> => {
9287
const url = 'https://apis.auxia.io/v1/GetTreatments';
9388

94-
// We are hardcoding PROD for the moment, because I haven't created a CODE key
95-
const apiKey = await getSsmValue('PROD', 'auxia-api-key');
96-
97-
if (apiKey === undefined) {
98-
throw new Error('auxia-api-key is undefined');
99-
}
100-
10189
const headers = {
10290
'Content-Type': 'application/json',
10391
'x-api-key': apiKey,
10492
};
10593

106-
const payload = await buildAuxiaAPIRequestPayload();
94+
const payload = buildAuxiaAPIRequestPayload(projectId, userId);
10795

10896
const params = {
10997
method: 'POST',
@@ -130,9 +118,37 @@ const buildAuxiaProxyResponseData = (auxiaData: AuxiaAPIAnswerData): AuxiaProxyR
130118
return { shouldShowSignInGate };
131119
};
132120

133-
export const buildAuxiaProxyRouter = (): Router => {
134-
const router = Router();
121+
interface AuxiaRouterConfig {
122+
apiKey: string;
123+
projectId: string;
124+
userId: string;
125+
}
135126

127+
export const getAuxiaRouterConfig = async (): Promise<AuxiaRouterConfig> => {
128+
const apiKey = await getSsmValue('PROD', 'auxia-api-key');
129+
if (apiKey === undefined) {
130+
throw new Error('auxia-api-key is undefined');
131+
}
132+
133+
const projectId = await getSsmValue('PROD', 'auxia-projectId');
134+
if (projectId === undefined) {
135+
throw new Error('auxia-projectId is undefined');
136+
}
137+
138+
const userId = await getSsmValue('PROD', 'auxia-userId');
139+
if (userId === undefined) {
140+
throw new Error('auxia-userId is undefined');
141+
}
142+
143+
return {
144+
apiKey,
145+
projectId,
146+
userId,
147+
};
148+
};
149+
150+
export const buildAuxiaProxyRouter = (config: AuxiaRouterConfig): Router => {
151+
const router = Router();
136152
router.post(
137153
'/auxia',
138154

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

143159
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
144160
try {
145-
const auxiaData = await fetchAuxiaData();
161+
const auxiaData = await fetchAuxiaData(
162+
config.apiKey,
163+
config.projectId,
164+
config.userId,
165+
);
146166
const response = buildAuxiaProxyResponseData(auxiaData);
147167

148168
res.send(response);

src/server/server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { logError } from './utils/logging';
1111
import { buildEpicRouter } from './api/epicRouter';
1212
import { buildBannerRouter } from './api/bannerRouter';
1313
import { buildHeaderRouter } from './api/headerRouter';
14-
import { buildAuxiaProxyRouter } from './api/auxiaProxyRouter';
14+
import { buildAuxiaProxyRouter, getAuxiaRouterConfig } from './api/auxiaProxyRouter';
1515
import { buildAmpEpicRouter } from './api/ampEpicRouter';
1616
import { buildChannelSwitchesReloader } from './channelSwitches';
1717
import { buildSuperModeArticlesReloader } from './lib/superMode';
@@ -114,7 +114,7 @@ const buildApp = async (): Promise<Express> => {
114114
),
115115
);
116116
app.use(buildHeaderRouter(channelSwitches, headerTests));
117-
app.use(buildAuxiaProxyRouter());
117+
118118
app.use('/amp', buildAmpEpicRouter(choiceCardAmounts, tickerData, ampEpicTests));
119119

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

127+
app.use(buildAuxiaProxyRouter(await getAuxiaRouterConfig()));
128+
127129
return Promise.resolve(app);
128130
};
129131

0 commit comments

Comments
 (0)