-
Notifications
You must be signed in to change notification settings - Fork 21
/
search-stack.ts
310 lines (280 loc) · 10.4 KB
/
search-stack.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
300
301
302
303
304
305
306
307
308
309
310
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
import { Fn, Stack, Construct, StackProps, CfnParameter, CfnOutput, CfnJson, CustomResource, Duration } from '@aws-cdk/core';
import { AwsCustomResource, AwsCustomResourcePolicy, PhysicalResourceId, Provider } from '@aws-cdk/custom-resources'
import { CfnDomain } from '@aws-cdk/aws-elasticsearch';
import { CfnUserPoolDomain, CfnIdentityPool, CfnIdentityPoolRoleAttachment, CfnUserPool, CfnUserPoolGroup } from '@aws-cdk/aws-cognito';
import { Effect, Role, ManagedPolicy, ServicePrincipal, FederatedPrincipal, PolicyStatement } from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
import path = require('path');
import fs = require('fs');
export class SearchStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const applicationPrefix = new CfnParameter(this, 'applicationPrefix', {
default: this.node.tryGetContext('applicationPrefix'),
description: "Prefix for the Amazon Cognito domain and the Amazon Elasticsearch Service domain",
type: "String",
allowedPattern: "^[a-z0-9]*$",
minLength: 3,
maxLength: 20
}).valueAsString;
const userPool = new CfnUserPool(this, "userPool", {
adminCreateUserConfig: {
allowAdminCreateUserOnly: true
},
usernameAttributes: ["email"],
autoVerifiedAttributes: ["email"],
});
// get a unique suffix from the last element of the stackId, e.g. 06b321d6b6e2
const suffix = Fn.select(4, Fn.split("-", Fn.select(2, Fn.split("/", this.stackId))));
new CfnUserPoolDomain(this, "cognitoDomain", {
domain: applicationPrefix + "-" + suffix,
userPoolId: userPool.ref
});
const idPool = new CfnIdentityPool(this, "identityPool", {
allowUnauthenticatedIdentities: false,
cognitoIdentityProviders: []
});
const esLimitedUserRole = new Role(this, "esLimitedUserRole", {
assumedBy: new FederatedPrincipal('cognito-identity.amazonaws.com', {
"StringEquals": { "cognito-identity.amazonaws.com:aud": idPool.ref },
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "authenticated"
}
}, "sts:AssumeRoleWithWebIdentity")
});
const esAdminFnRole = new Role(this, "esAdminFnRole", {
assumedBy: new ServicePrincipal('lambda.amazonaws.com')
});
esAdminFnRole.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName("service-role/AWSLambdaBasicExecutionRole"))
const esAdminUserRole = new Role(this, "esAdminUserRole", {
assumedBy:
new FederatedPrincipal('cognito-identity.amazonaws.com', {
"StringEquals": { "cognito-identity.amazonaws.com:aud": idPool.ref },
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "authenticated"
}
}, "sts:AssumeRoleWithWebIdentity")
});
const elasticsearchHttpPolicy = new ManagedPolicy(this, "elasticsearchHttpPolicy", {
roles: [esAdminUserRole, esAdminFnRole]
});
new CfnUserPoolGroup(this, "userPoolAdminGroupPool", {
userPoolId: userPool.ref,
groupName: "es-admins",
roleArn: esAdminUserRole.roleArn
});
const domainArn = "arn:aws:es:" + this.region + ":" + this.account + ":domain/" + applicationPrefix + "/*"
elasticsearchHttpPolicy.addStatements(new PolicyStatement({
effect: Effect.ALLOW,
resources: [domainArn],
actions: ['es:ESHttpPost', 'es:ESHttpGet', 'es:ESHttpPut']
}));
const esRole = new Role(this, "esRole", {
assumedBy: new ServicePrincipal('es.amazonaws.com'),
managedPolicies: [ManagedPolicy.fromAwsManagedPolicyName("AmazonESCognitoAccess")]
});
const esDomain = new CfnDomain(this, "searchDomain", {
elasticsearchClusterConfig: { instanceType: "t3.small.elasticsearch" },
ebsOptions: { volumeSize: 10, ebsEnabled: true },
elasticsearchVersion: "7.9",
domainName: applicationPrefix,
nodeToNodeEncryptionOptions: { enabled: true },
encryptionAtRestOptions: { enabled: true },
advancedSecurityOptions: {
enabled: true,
masterUserOptions: { masterUserArn: esAdminFnRole.roleArn }
},
domainEndpointOptions: {
enforceHttps: true
},
cognitoOptions: {
enabled: true,
identityPoolId: idPool.ref,
roleArn: esRole.roleArn,
userPoolId: userPool.ref
},
// see recommended configuration for fgac
// https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/fgac.html
// don't use this without fgac, vpc support, or ip based restrictions
// as it enables anonymous access
accessPolicies: {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "es:ESHttp*",
"Resource": domainArn
}
]
}
});
const userPoolClients = new AwsCustomResource(this, 'clientIdResource', {
policy: AwsCustomResourcePolicy.fromSdkCalls({ resources: [userPool.attrArn] }),
onCreate: {
service: 'CognitoIdentityServiceProvider',
action: 'listUserPoolClients',
parameters: {
UserPoolId: userPool.ref
},
physicalResourceId: PhysicalResourceId.of(`ClientId-${applicationPrefix}`)
}
});
userPoolClients.node.addDependency(esDomain);
const clientId = userPoolClients.getResponseField('UserPoolClients.0.ClientId');
const providerName = `cognito-idp.${this.region}.amazonaws.com/${userPool.ref}:${clientId}`
new CfnIdentityPoolRoleAttachment(this, 'userPoolRoleAttachment', {
identityPoolId: idPool.ref,
roles: {
'authenticated': esLimitedUserRole.roleArn
},
roleMappings: new CfnJson(this, 'roleMappingsJson', {
value: {
[providerName]: {
Type: 'Token',
AmbiguousRoleResolution: 'AuthenticatedRole'
}
}
}
)
});
/**
* Function implementing the requests to Amazon Elasticsearch Service
* for the custom resource.
*/
const esRequestsFn = new lambda.Function(this, 'esRequestsFn', {
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'es-requests.handler',
code: lambda.Code.fromAsset(path.join(__dirname, '..', 'functions/es-requests')),
timeout: Duration.seconds(30),
role: esAdminFnRole,
environment: {
"DOMAIN": esDomain.attrDomainEndpoint,
"REGION": this.region
}
});
const esRequestProvider = new Provider(this, 'esRequestProvider', {
onEventHandler: esRequestsFn
});
/**
* You can import files exported via Kibana's
* Stack Management -> Save Objects as done with the
* dashboard.ndjson below.
*/
new CustomResource(this, 'esRequestsResource', {
serviceToken: esRequestProvider.serviceToken,
properties: {
requests: [
{
"method": "PUT",
"path": "_opendistro/_security/api/tenants/logs-tenant",
"body": {
"description": "A tenant for the sample kibana objects."
}
},
{
"method": "PUT",
"path": "_opendistro/_security/api/rolesmapping/all_access",
"body": {
"backend_roles": [
esAdminUserRole.roleArn,
esAdminFnRole.roleArn
],
"hosts": [],
"users": []
}
},
{
"method": "PUT",
"path": "_opendistro/_security/api/rolesmapping/security_manager",
"body": {
"backend_roles": [
esAdminFnRole.roleArn,
esAdminUserRole.roleArn
],
"hosts": [],
"users": []
}
},
{
"method": "PUT",
"path": "_template/example-index-template",
"body": fs.readFileSync(path.join(__dirname, "index-template.json")).toString()
},
{
"method": "POST",
"path": "_plugin/kibana/api/saved_objects/_import?overwrite=true",
"body": fs.readFileSync(path.join(__dirname, "dashboard.ndjson")).toString(),
"securitytenant": "logs-tenant",
"filename": "dashboard.ndjson"
},
{
"method": "PUT",
"path": "_opendistro/_security/api/roles/logs-role",
"body":
{
"cluster_permissions": [
"cluster_composite_ops",
"cluster_monitor"
],
"index_permissions": [{
"index_patterns": [
"logs-*"
],
"dls": "",
"fls": [],
"masked_fields": [],
"allowed_actions": [
"crud",
"create_index"
]
}],
"tenant_permissions": [{
"tenant_patterns": [
"logs-tenant"
],
"allowed_actions": [
"kibana_all_write"
]
}]
}
},
{
"method": "PUT",
"path": "_opendistro/_security/api/rolesmapping/logs-role",
"body": {
"backend_roles": [
esLimitedUserRole.roleArn
],
"hosts": [],
"users": []
}
},
{
"method": "PUT",
"path": "_opendistro/_security/api/rolesmapping/kibana_user",
"body": {
"backend_roles": [
esLimitedUserRole.roleArn
],
"hosts": [],
"users": []
}
}
]
}
});
new CfnOutput(this, 'createUserUrl', {
description: "Create a new user in the user pool here - add it to the es-admins group if fine grained access controls should not apply.",
value: "https://" + this.region + ".console.aws.amazon.com/cognito/users?region=" + this.region + "#/pool/" + userPool.ref + "/users"
});
new CfnOutput(this, 'kibanaUrl', {
description: "Access Kibana via this URL.",
value: "https://" + esDomain.attrDomainEndpoint + "/_plugin/kibana/"
});
}
}