forked from CloudSnorkel/cdk-github-runners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda-helpers.ts
More file actions
81 lines (68 loc) · 2.23 KB
/
Copy pathlambda-helpers.ts
File metadata and controls
81 lines (68 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { SecretsManagerClient, GetSecretValueCommand, UpdateSecretCommand } from '@aws-sdk/client-secrets-manager';
export interface StepFunctionLambdaInput {
readonly owner: string;
readonly repo: string;
readonly runnerName: string;
readonly installationId?: number;
readonly labels: string[];
readonly error?: {
readonly Error: string;
readonly Cause: string;
};
}
const sm = new SecretsManagerClient();
export async function getSecretValue(arn: string | undefined) {
if (!arn) {
throw new Error('Missing secret ARN');
}
const secret = await sm.send(new GetSecretValueCommand({ SecretId: arn }));
if (!secret.SecretString) {
throw new Error(`No SecretString in ${arn}`);
}
return secret.SecretString;
}
export async function getSecretJsonValue(arn: string | undefined) {
return JSON.parse(await getSecretValue(arn));
}
export async function updateSecretValue(arn: string | undefined, value: string) {
if (!arn) {
throw new Error('Missing secret ARN');
}
await sm.send(new UpdateSecretCommand({ SecretId: arn, SecretString: value }));
}
export async function customResourceRespond(event: AWSLambda.CloudFormationCustomResourceEvent, responseStatus: string,
reason: string, physicalResourceId: string, data: any) {
const responseBody = JSON.stringify({
Status: responseStatus,
Reason: reason,
PhysicalResourceId: physicalResourceId,
StackId: event.StackId,
RequestId: event.RequestId,
LogicalResourceId: event.LogicalResourceId,
NoEcho: false,
Data: data,
});
console.log('Responding', responseBody);
// eslint-disable-next-line @typescript-eslint/no-require-imports
const parsedUrl = require('url').parse(event.ResponseURL);
const requestOptions = {
hostname: parsedUrl.hostname,
path: parsedUrl.path,
method: 'PUT',
headers: {
'content-type': '',
'content-length': responseBody.length,
},
};
return new Promise((resolve, reject) => {
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const request = require('https').request(requestOptions, resolve);
request.on('error', reject);
request.write(responseBody);
request.end();
} catch (e) {
reject(e);
}
});
}