Skip to content

Commit a35fdfa

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

File tree

4 files changed

+78
-27
lines changed

4 files changed

+78
-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: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,10 @@ interface AuxiaProxyResponseData {
5252
shouldShowSignInGate: boolean;
5353
}
5454

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-
55+
const buildAuxiaAPIRequestPayload = async (
56+
projectId: string,
57+
userId: string,
58+
): Promise<AuxiaAPIRequestPayload> => {
6659
// For the moment we are hard coding the data provided in contextualAttributes and surfaces.
6760
return {
6861
projectId: projectId,
@@ -88,22 +81,19 @@ const buildAuxiaAPIRequestPayload = async (): Promise<AuxiaAPIRequestPayload> =>
8881
};
8982
};
9083

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

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-
10191
const headers = {
10292
'Content-Type': 'application/json',
10393
'x-api-key': apiKey,
10494
};
10595

106-
const payload = await buildAuxiaAPIRequestPayload();
96+
const payload = await buildAuxiaAPIRequestPayload(projectId, userId);
10797

10898
const params = {
10999
method: 'POST',
@@ -130,9 +120,37 @@ const buildAuxiaProxyResponseData = (auxiaData: AuxiaAPIAnswerData): AuxiaProxyR
130120
return { shouldShowSignInGate };
131121
};
132122

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

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

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

143161
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
144162
try {
145-
const auxiaData = await fetchAuxiaData();
163+
const auxiaData = await fetchAuxiaData(
164+
config.apiKey,
165+
config.projectId,
166+
config.userId,
167+
);
146168
const response = buildAuxiaProxyResponseData(auxiaData);
147169

148170
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)