-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathstart-query-execution.ts
299 lines (265 loc) · 9.75 KB
/
start-query-execution.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import * as iam from '@aws-cdk/aws-iam';
import * as kms from '@aws-cdk/aws-kms';
import * as s3 from '@aws-cdk/aws-s3';
import * as sfn from '@aws-cdk/aws-stepfunctions';
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import { integrationResourceArn, validatePatternSupported } from '../private/task-utils';
/**
* Properties for starting a Query Execution
*/
export interface AthenaStartQueryExecutionProps extends sfn.TaskStateBaseProps {
/**
* Query that will be started
*/
readonly queryString: string;
/**
* Unique string string to ensure idempotence
*
* @default - No client request token
*/
readonly clientRequestToken?: string;
/**
* Database within which query executes
*
* @default - No query execution context
*/
readonly queryExecutionContext?: QueryExecutionContext;
/**
* Configuration on how and where to save query
*
* @default - No result configuration
*/
readonly resultConfiguration?: ResultConfiguration;
/**
* Configuration on how and where to save query
*
* @default - No work group
*/
readonly workGroup?: string;
}
/**
* Start an Athena Query as a Task
*
* @see https://docs.aws.amazon.com/step-functions/latest/dg/connect-athena.html
*/
export class AthenaStartQueryExecution extends sfn.TaskStateBase {
private static readonly SUPPORTED_INTEGRATION_PATTERNS: sfn.IntegrationPattern[] = [
sfn.IntegrationPattern.REQUEST_RESPONSE,
sfn.IntegrationPattern.RUN_JOB,
];
protected readonly taskMetrics?: sfn.TaskMetricsConfig;
protected readonly taskPolicies?: iam.PolicyStatement[];
private readonly integrationPattern: sfn.IntegrationPattern;
constructor(scope: Construct, id: string, private readonly props: AthenaStartQueryExecutionProps) {
super(scope, id, props);
this.integrationPattern = props.integrationPattern ?? sfn.IntegrationPattern.REQUEST_RESPONSE;
validatePatternSupported(this.integrationPattern, AthenaStartQueryExecution.SUPPORTED_INTEGRATION_PATTERNS);
this.taskPolicies = this.createPolicyStatements();
}
private createPolicyStatements(): iam.PolicyStatement[] {
const policyStatements = [
new iam.PolicyStatement({
resources: [
cdk.Stack.of(this).formatArn({
service: 'athena',
resource: 'datacatalog',
resourceName: this.props.queryExecutionContext?.catalogName ?? 'AwsDataCatalog',
}),
cdk.Stack.of(this).formatArn({
service: 'athena',
resource: 'workgroup',
resourceName: this.props.workGroup ?? 'primary',
}),
],
actions: ['athena:getDataCatalog', 'athena:startQueryExecution', 'athena:getQueryExecution'],
}),
];
policyStatements.push(
new iam.PolicyStatement({
actions: ['s3:CreateBucket',
's3:ListBucket',
's3:GetBucketLocation',
's3:GetObject'],
resources: ['*'], // Need * permissions to create new output location https://docs.aws.amazon.com/athena/latest/ug/security-iam-athena.html
}),
);
policyStatements.push(
new iam.PolicyStatement({
actions: ['s3:AbortMultipartUpload',
's3:ListBucketMultipartUploads',
's3:ListMultipartUploadParts',
's3:PutObject'],
resources: [this.props.resultConfiguration?.outputLocation?.bucketName ? `arn:aws:s3:::${this.props.resultConfiguration?.outputLocation?.bucketName}/${this.props.resultConfiguration?.outputLocation?.objectKey}/*` : '*'], // Need S3 location where data is stored or Athena throws an Unable to verify/create output bucket https://docs.aws.amazon.com/athena/latest/ug/security-iam-athena.html
}),
);
policyStatements.push(
new iam.PolicyStatement({
actions: ['lakeformation:GetDataAccess'],
resources: ['*'], // State machines scoped to output location fail and * permissions are required as per documentation https://docs.aws.amazon.com/lake-formation/latest/dg/permissions-reference.html
}),
);
policyStatements.push(
new iam.PolicyStatement({
actions: ['glue:BatchCreatePartition',
'glue:BatchDeletePartition',
'glue:BatchDeleteTable',
'glue:BatchGetPartition',
'glue:CreateDatabase',
'glue:CreatePartition',
'glue:CreateTable',
'glue:DeleteDatabase',
'glue:DeletePartition',
'glue:DeleteTable',
'glue:GetDatabase',
'glue:GetDatabases',
'glue:GetPartition',
'glue:GetPartitions',
'glue:GetTable',
'glue:GetTables',
'glue:UpdateDatabase',
'glue:UpdatePartition',
'glue:UpdateTable'],
resources: [
cdk.Stack.of(this).formatArn({
service: 'glue',
resource: 'catalog',
}),
cdk.Stack.of(this).formatArn({
service: 'glue',
resource: 'database',
resourceName: this.props.queryExecutionContext?.databaseName ?? 'default',
}),
cdk.Stack.of(this).formatArn({
service: 'glue',
resource: 'table',
resourceName: (this.props.queryExecutionContext?.databaseName ?? 'default') + '/*', // grant access to all tables in the specified or default database to prevent cross database access https://docs.aws.amazon.com/IAM/latest/UserGuide/list_awsglue.html
}),
cdk.Stack.of(this).formatArn({
service: 'glue',
resource: 'userDefinedFunction',
resourceName: (this.props.queryExecutionContext?.databaseName ?? 'default') + '/*', // grant access to get all user defined functions for the particular database in the request or the default database https://docs.aws.amazon.com/IAM/latest/UserGuide/list_awsglue.html
}),
],
}),
);
return policyStatements;
}
private renderEncryption(): any {
const encryptionConfiguration = this.props.resultConfiguration?.encryptionConfiguration !== undefined
? {
EncryptionOption: this.props.resultConfiguration.encryptionConfiguration.encryptionOption,
KmsKey: this.props.resultConfiguration.encryptionConfiguration.encryptionKey,
}
: undefined;
return encryptionConfiguration;
}
/**
* Provides the Athena start query execution service integration task configuration
*/
/**
* @internal
*/
protected _renderTask(): any {
return {
Resource: integrationResourceArn('athena', 'startQueryExecution', this.integrationPattern),
Parameters: sfn.FieldUtils.renderObject({
QueryString: this.props.queryString,
ClientRequestToken: this.props.clientRequestToken,
QueryExecutionContext: (this.props.queryExecutionContext?.catalogName || this.props.queryExecutionContext?.databaseName) ? {
Catalog: this.props.queryExecutionContext?.catalogName,
Database: this.props.queryExecutionContext?.databaseName,
} : undefined,
ResultConfiguration: {
EncryptionConfiguration: this.renderEncryption(),
OutputLocation: this.props.resultConfiguration?.outputLocation ? `s3://${this.props.resultConfiguration.outputLocation.bucketName}/${this.props.resultConfiguration.outputLocation.objectKey}/` : undefined,
},
WorkGroup: this.props?.workGroup,
}),
};
}
}
/**
* Location of query result along with S3 bucket configuration
*
* @see https://docs.aws.amazon.com/athena/latest/APIReference/API_ResultConfiguration.html
*/
export interface ResultConfiguration {
/**
* S3 path of query results
*
* Example value: `s3://query-results-bucket/folder/`
*
* @default - Query Result Location set in Athena settings for this workgroup
*/
readonly outputLocation?: s3.Location;
/**
* Encryption option used if enabled in S3
*
* @default - SSE_S3 encrpytion is enabled with default encryption key
*/
readonly encryptionConfiguration?: EncryptionConfiguration
}
/**
* Encryption Configuration of the S3 bucket
*
* @see https://docs.aws.amazon.com/athena/latest/APIReference/API_EncryptionConfiguration.html
*/
export interface EncryptionConfiguration {
/**
* Type of S3 server-side encryption enabled
*
* @default EncryptionOption.S3_MANAGED
*/
readonly encryptionOption: EncryptionOption;
/**
* KMS key ARN or ID
*
* @default - No KMS key for Encryption Option SSE_S3 and default master key for Encryption Option SSE_KMS and CSE_KMS
*/
readonly encryptionKey?: kms.IKey;
}
/**
* Encryption Options of the S3 bucket
*
* @see https://docs.aws.amazon.com/athena/latest/APIReference/API_EncryptionConfiguration.html#athena-Type-EncryptionConfiguration-EncryptionOption
*/
export enum EncryptionOption {
/**
* Server side encryption (SSE) with an Amazon S3-managed key.
*
* @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingServerSideEncryption.html
*/
S3_MANAGED = 'SSE_S3',
/**
* Server-side encryption (SSE) with an AWS KMS key managed by the account owner.
*
* @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingKMSEncryption.html
*/
KMS = 'SSE_KMS',
/**
* Client-side encryption (CSE) with an AWS KMS key managed by the account owner.
*
* @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingClientSideEncryption.html
*/
CLIENT_SIDE_KMS = 'CSE_KMS'
}
/**
* Database and data catalog context in which the query execution occurs
*
* @see https://docs.aws.amazon.com/athena/latest/APIReference/API_QueryExecutionContext.html
*/
export interface QueryExecutionContext {
/**
* Name of catalog used in query execution
*
* @default - No catalog
*/
readonly catalogName?: string;
/**
* Name of database used in query execution
*
* @default - No database
*/
readonly databaseName?: string;
}