-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Expand file tree
/
Copy pathutils.ts
More file actions
164 lines (147 loc) · 4.42 KB
/
Copy pathutils.ts
File metadata and controls
164 lines (147 loc) · 4.42 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* eslint-disable @typescript-eslint/no-require-imports, import/no-extraneous-dependencies */
/* eslint-disable no-console */
import type { AwsCredentialIdentityProvider } from '@smithy/types';
import { AwsSdkCall } from './construct-types';
type Event = AWSLambda.CloudFormationCustomResourceEvent;
/**
* Serialized form of the physical resource id for use in the operation parameters
*/
export const PHYSICAL_RESOURCE_ID_REFERENCE = 'PHYSICAL:RESOURCEID:';
/**
* Decodes encoded special values (physicalResourceId)
*/
export function decodeSpecialValues(object: object, physicalResourceId: string): any {
return recurse(object);
function recurse(x: any): any {
if (x === PHYSICAL_RESOURCE_ID_REFERENCE) {
return physicalResourceId;
}
if (Array.isArray(x)) {
return x.map(recurse);
}
if (x && typeof x === 'object') {
for (const [key, value] of Object.entries(x)) {
x[key] = recurse(value);
}
return x;
}
return x;
}
}
/**
* Parses a stringified JSON API call.
*/
export function decodeCall(call: string | undefined): any {
if (!call) { return undefined; }
return JSON.parse(call);
}
/**
* Responds to the CloudFormation Custom Resource.
*/
export function respond(
event: Event,
responseStatus: string,
reason: string,
physicalResourceId: string,
data: any, logApiResponseData: boolean,
): Promise<void> {
const responseObject = {
Status: responseStatus,
Reason: reason,
PhysicalResourceId: physicalResourceId,
StackId: event.StackId,
RequestId: event.RequestId,
LogicalResourceId: event.LogicalResourceId,
NoEcho: false,
Data: data,
};
if (logApiResponseData) {
console.log('Responding', JSON.stringify(responseObject));
} else {
const { Data, ...filteredResponseObject } = responseObject;
console.log('Responding', JSON.stringify(filteredResponseObject));
}
// eslint-disable-next-line @typescript-eslint/no-require-imports
const parsedUrl = require('url').parse(event.ResponseURL);
const responseBody = JSON.stringify(responseObject);
const requestOptions = {
hostname: parsedUrl.hostname,
path: parsedUrl.path,
method: 'PUT',
headers: {
'content-type': '',
'content-length': Buffer.byteLength(responseBody, 'utf8'),
},
};
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);
}
});
}
/**
* Gets credentials used to make an API call.
*/
export async function getCredentials(call: AwsSdkCall, physicalResourceId: string): Promise<AwsCredentialIdentityProvider | undefined> {
let credentials;
if (call.assumedRoleArn) {
const timestamp = (new Date()).getTime();
const params = {
RoleArn: call.assumedRoleArn,
RoleSessionName: `${timestamp}-${physicalResourceId}`.substring(0, 64),
};
const { fromTemporaryCredentials } = await import('@aws-sdk/credential-providers');
credentials = fromTemporaryCredentials({
params,
clientConfig: call.region !== undefined ? { region: call.region } : undefined,
});
}
return credentials;
}
/**
* Formats API response data based on outputPath or outputPaths configured in the SDK call.
*/
export function formatData(call: AwsSdkCall, flatData: { [key: string]: string }): { [key: string]: string } {
let outputPaths: string[] | undefined;
if (call.outputPath) {
outputPaths = [call.outputPath];
} else if (call.outputPaths) {
outputPaths = call.outputPaths;
}
if (outputPaths) {
return filterKeys(flatData, startsWithOneOf(outputPaths));
}
return flatData;
}
/**
* Returns a predicate function that returns true if a target string starts with any of the specified
* search strings.
*/
function startsWithOneOf(searchStrings: string[]): (string: string) => boolean {
return function(string: string): boolean {
for (const searchString of searchStrings) {
if (string.startsWith(searchString)) {
return true;
}
}
return false;
};
}
/**
* Filters the keys of an object.
*/
function filterKeys(object: object, pred: (key: string) => boolean): {} {
return Object.entries(object)
.reduce(
(acc, [k, v]) => pred(k)
? { ...acc, [k]: v }
: acc,
{},
);
}