-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathinvoke.ts
194 lines (175 loc) · 5.59 KB
/
invoke.ts
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { Construct } from 'constructs';
import * as events from '../../../aws-events';
import * as iam from '../../../aws-iam';
import * as sfn from '../../../aws-stepfunctions';
import { integrationResourceArn } from '../private/task-utils';
/**
* The style used when applying URL encoding to array values.
*/
export enum URLEncodingFormat {
/**
* Encode arrays using brackets. For example, {'array': ['a','b','c']} encodes to 'array[]=a&array[]=b&array[]=c'
*/
BRACKETS = 'BRACKETS',
/**
* Encode arrays using commas. For example, {'array': ['a','b','c']} encodes to 'array=a,b,c,d'
*/
COMMAS = 'COMMAS',
/**
* Apply the default URL encoding style (INDICES).
*/
DEFAULT = 'DEFAULT',
/**
* Encode arrays using the index value. For example, {'array': ['a','b','c']} encodes to 'array[0]=a&array[1]=b&array[2]=c'
*/
INDICES = 'INDICES',
/**
* Do not apply URL encoding.
*/
NONE = 'NONE',
/**
* Repeat key for each item in the array. For example, {'array': ['a','b','c']} encodes to 'array[]=a&array[]=b&array[]=c'
*/
REPEAT = 'REPEAT',
}
/**
* The StepFunctions parameters for the http:invoke task.
*/
interface TaskParameters {
ApiEndpoint: string;
Authentication: {
ConnectionArn: string;
};
Method: string;
Headers?: { [key: string]: string };
RequestBody?: string;
QueryParameters?: { [key: string]: string };
Transform?: {
RequestBodyEncoding: string;
RequestEncodingOptions?: {
ArrayFormat: string;
};
};
}
/**
* Properties for calling an external HTTP endpoint with HttpInvoke.
*/
export interface HttpInvokeProps extends sfn.TaskStateBaseProps {
/**
* Permissions are granted to call all resources under this path.
*
* @example 'https://api.example.com'
*/
readonly apiRoot: string;
/**
* The API endpoint to call, relative to `apiRoot`.
* @example sfn.TaskInput.fromText('path/to/resource')
*/
readonly apiEndpoint: sfn.TaskInput;
/**
* The HTTP method to use.
*
* @example sfn.TaskInput.fromText('GET')
*/
readonly method: sfn.TaskInput;
/**
* The EventBridge Connection to use for authentication.
*/
readonly connection: events.IConnection;
/**
* The body to send to the HTTP endpoint.
*
* @default - No body is sent with the request.
*/
readonly body?: sfn.TaskInput;
/**
* The headers to send to the HTTP endpoint.
*
* @example sfn.TaskInput.fromObject({ 'Content-Type': 'application/json' })
*
* @default - No additional headers are added to the request.
*/
readonly headers?: sfn.TaskInput;
/**
* The query string parameters to send to the HTTP endpoint.
* @default - No query string parameters are sent in the request.
*/
readonly queryStringParameters?: sfn.TaskInput;
/**
* Determines whether to apply URL encoding to the request body, and which array encoding format to use.
*
* `URLEncodingFormat.NONE` passes the JSON-serialized `RequestBody` field as the HTTP request body.
* Otherwise, the HTTP request body is the URL-encoded form data of the `RequestBody` field using the
* specified array encoding format, and the `Content-Type` header is set to `application/x-www-form-urlencoded`.
*
* @default - URLEncodingFormat.NONE
*/
readonly urlEncodingFormat?: URLEncodingFormat;
}
/**
* A Step Functions Task to call a public third-party API.
*/
export class HttpInvoke extends sfn.TaskStateBase {
protected readonly taskMetrics?: sfn.TaskMetricsConfig;
protected readonly taskPolicies?: iam.PolicyStatement[];
constructor(scope: Construct, id: string, private readonly props: HttpInvokeProps) {
super(scope, id, props);
this.taskPolicies = this.buildTaskPolicyStatements();
}
/**
* Provides the HTTP Invoke service integration task configuration.
*
* @internal
*/
protected _renderTask(): any {
return {
Resource: integrationResourceArn('http', 'invoke'),
Parameters: sfn.FieldUtils.renderObject(this.buildTaskParameters()),
};
}
protected buildTaskPolicyStatements(): iam.PolicyStatement[] {
return [
new iam.PolicyStatement({
actions: ['events:RetrieveConnectionCredentials'],
resources: [this.props.connection.connectionArn],
}),
new iam.PolicyStatement({
actions: ['secretsmanager:GetSecretValue', 'secretsmanager:DescribeSecret'],
resources: [this.props.connection.connectionSecretArn],
}),
new iam.PolicyStatement({
actions: ['states:InvokeHTTPEndpoint'],
resources: ['*'],
conditions: {
StringLike: {
'states:HTTPEndpoint': `${this.props.apiRoot}*`,
},
},
}),
];
}
private buildTaskParameters() {
const parameters: TaskParameters = {
ApiEndpoint: `${this.props.apiRoot}/${this.props.apiEndpoint.value}`,
Authentication: {
ConnectionArn: this.props.connection.connectionArn,
},
Method: this.props.method.value,
Headers: this.props.headers?.value,
RequestBody: this.props.body?.value,
QueryParameters: this.props.queryStringParameters?.value,
};
if (this.props.urlEncodingFormat != null && this.props.urlEncodingFormat !== URLEncodingFormat.NONE) {
parameters.Headers = { ...parameters.Headers, 'Content-Type': 'application/x-www-form-urlencoded' };
parameters.Transform = {
RequestBodyEncoding: 'URL_ENCODED',
};
if (this.props.urlEncodingFormat !== URLEncodingFormat.DEFAULT) {
parameters.Transform.RequestEncodingOptions = {
ArrayFormat: this.props.urlEncodingFormat,
};
}
}
return parameters;
}
}