-
Notifications
You must be signed in to change notification settings - Fork 125
/
setup.js
691 lines (604 loc) · 20.1 KB
/
setup.js
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
import aws from 'aws-sdk'
import os from 'os'
import logger from './logger'
import crypto from './crypto'
import peers from './peers'
import nightly from './nightly'
let awsAccountId
let initialized = false
const defaultRegion = process.env.NODE_ENV == 'development' ? 'us-west-2' : 'us-east-1'
const defineUsername = () => {
let appUsername = ''
if (process.env.NODE_ENV == 'development') {
if (process.env.USERNAME) {
appUsername = process.env.USERNAME
} else {
appUsername = os.userInfo().username
}
} else {
appUsername = 'prod-v1'
}
return appUsername
}
logger.info("Running as USER: " + defineUsername())
// if running in dev mode, prefix the DynamoDB tables and S3 buckets with the username
const resourceNamePrefix = 'userbase-' + defineUsername() + '-'
const ddbTableGroup = 'userbase-' + defineUsername()
const adminTableName = resourceNamePrefix + 'admins'
const adminAccessTokensTableName = resourceNamePrefix + 'admin-access-tokens'
const appsTableName = resourceNamePrefix + 'apps'
const domainWhitelistTableName = resourceNamePrefix + 'domain-whitelist'
const usersTableName = resourceNamePrefix + 'users'
const sessionsTableName = resourceNamePrefix + 'sessions'
const databaseTableName = resourceNamePrefix + 'databases'
const userDatabaseTableName = resourceNamePrefix + 'user-databases'
const transactionsTableName = resourceNamePrefix + 'transactions'
const deletedUsersTableName = resourceNamePrefix + 'deleted-users'
const deletedAppsTableName = resourceNamePrefix + 'deleted-apps'
const deletedAdminsTableName = resourceNamePrefix + 'deleted-admins'
const dbStatesBucketNamePrefix = resourceNamePrefix + 'database-states'
const filesBucketNamePrefix = resourceNamePrefix + 'files'
const secretManagerSecretId = resourceNamePrefix + 'env'
exports.adminTableName = adminTableName
exports.adminAccessTokensTableName = adminAccessTokensTableName
exports.appsTableName = appsTableName
exports.domainWhitelistTableName = domainWhitelistTableName
exports.usersTableName = usersTableName
exports.sessionsTableName = sessionsTableName
exports.databaseTableName = databaseTableName
exports.userDatabaseTableName = userDatabaseTableName
exports.transactionsTableName = transactionsTableName
exports.deletedUsersTableName = deletedUsersTableName
exports.deletedAppsTableName = deletedAppsTableName
exports.deletedAdminsTableName = deletedAdminsTableName
const adminIdIndex = 'AdminIdIndex'
const accessTokenIndex = 'AccessTokenIndex'
const userIdIndex = 'UserIdIndex'
const appIdIndex = 'AppIdIndex'
const authTokenIndex = 'AuthTokenIndex'
const shareTokenIdReadOnlyIndex = 'ShareTokenIdReadOnlyIndex'
const shareTokenIdWriteIndex = 'ShareTokenIdWriteIndex'
const subscriptionPlanIndex = 'SubscriptionPlanIndex'
const userDatabaseIdIndex = 'UserDatabaseIdIndex'
exports.adminIdIndex = adminIdIndex
exports.accessTokenIndex = accessTokenIndex
exports.userIdIndex = userIdIndex
exports.appIdIndex = appIdIndex
exports.authTokenIndex = authTokenIndex
exports.shareTokenIdReadOnlyIndex = shareTokenIdReadOnlyIndex
exports.shareTokenIdWriteIndex = shareTokenIdWriteIndex
exports.subscriptionPlanIndex = subscriptionPlanIndex
exports.userDatabaseIdIndex = userDatabaseIdIndex
const _getBucketName = function (bucketNamePrefix) {
if (!initialized || !awsAccountId) {
throw new Error('Setup not initialized')
}
return bucketNamePrefix + '-' + awsAccountId
}
const getDbStatesBucketName = function () {
return _getBucketName(dbStatesBucketNamePrefix)
}
const getFilesBucketName = function () {
return _getBucketName(filesBucketNamePrefix)
}
let s3
const getS3Connection = () => s3
exports.s3 = getS3Connection
exports.getDbStatesBucketName = getDbStatesBucketName
exports.getFilesBucketName = getFilesBucketName
let sm
exports.getSecrets = getSecrets
exports.updateSecrets = updateSecrets
let emailDomain
let ses
exports.sendEmail = sendEmail
exports.init = async function (userbaseConfig) {
// look for AWS credentials under the 'encrypted' profile
const profile = 'encrypted'
// create a custom AWS credentials chain to provide the custom profile
const chain = new aws.CredentialProviderChain([
function () { return new aws.EnvironmentCredentials('AWS') },
function () { return new aws.EnvironmentCredentials('AMAZON') },
function () { return new aws.SharedIniFileCredentials({ profile }) },
function () { return new aws.ECSCredentials() },
function () { return new aws.ProcessCredentials({ profile }) },
function () { return new aws.EC2MetadataCredentials() }
])
logger.info('Loading AWS credentials')
// eslint-disable-next-line require-atomic-updates
aws.config.credentials = await chain.resolvePromise()
logger.info('Default AWS region: ' + defaultRegion)
aws.config.update({ region: defaultRegion })
// get the AWS account id
const accountInfo = await (new aws.STS({ apiVersion: '2011-06-15' })).getCallerIdentity({}).promise()
awsAccountId = accountInfo.Account
logger.info(`Running as Account ID: ${awsAccountId}`)
// remember the email domain
emailDomain = userbaseConfig.emailDomain
initialized = true
await setupDdb()
await setupS3()
await setupSM()
await setupSes()
await setupEc2PeerDiscovery()
// kick off the interval to run nightly job
nightly()
}
async function setupDdb() {
const ddb = new aws.DynamoDB({ apiVersion: '2012-08-10' })
// the admin table holds a record per admin account
const adminTableParams = {
TableName: adminTableName,
BillingMode: 'PAY_PER_REQUEST',
AttributeDefinitions: [
{ AttributeName: 'email', AttributeType: 'S' },
{ AttributeName: 'admin-id', AttributeType: 'S' }
],
KeySchema: [
{ AttributeName: 'email', KeyType: 'HASH' }
],
GlobalSecondaryIndexes: [{
IndexName: adminIdIndex,
KeySchema: [
{ AttributeName: 'admin-id', KeyType: 'HASH' }
],
Projection: { ProjectionType: 'ALL' }
}]
}
// the admin table holds a record per admin account
const adminAccessTokensTableParams = {
TableName: adminAccessTokensTableName,
BillingMode: 'PAY_PER_REQUEST',
AttributeDefinitions: [
{ AttributeName: 'admin-id', AttributeType: 'S' },
{ AttributeName: 'label', AttributeType: 'S' },
{ AttributeName: 'access-token', AttributeType: 'S' }
],
KeySchema: [
{ AttributeName: 'admin-id', KeyType: 'HASH' },
{ AttributeName: 'label', KeyType: 'RANGE' }
],
GlobalSecondaryIndexes: [{
IndexName: accessTokenIndex,
KeySchema: [
{ AttributeName: 'access-token', KeyType: 'HASH' }
],
Projection: { ProjectionType: 'ALL' }
}]
}
// the apps table holds a record for apps per admin account
const appsTableParams = {
TableName: appsTableName,
BillingMode: 'PAY_PER_REQUEST',
AttributeDefinitions: [
{ AttributeName: 'admin-id', AttributeType: 'S' },
{ AttributeName: 'app-name', AttributeType: 'S' },
{ AttributeName: 'app-id', AttributeType: 'S' },
],
KeySchema: [
{ AttributeName: 'admin-id', KeyType: 'HASH' },
{ AttributeName: 'app-name', KeyType: 'RANGE' }
],
GlobalSecondaryIndexes: [
{
IndexName: appIdIndex,
KeySchema: [
{ AttributeName: 'app-id', KeyType: 'HASH' }
],
Projection: { ProjectionType: 'ALL' }
}
]
}
// the domain whitelist table holds a record for each whitelisted domain per app
const domainWhitelistTableParams = {
TableName: domainWhitelistTableName,
BillingMode: 'PAY_PER_REQUEST',
AttributeDefinitions: [
{ AttributeName: 'app-id', AttributeType: 'S' },
{ AttributeName: 'domain', AttributeType: 'S' }
],
KeySchema: [
{ AttributeName: 'app-id', KeyType: 'HASH' },
{ AttributeName: 'domain', KeyType: 'RANGE' }
],
}
// the users table holds a record for user per app
const usersTableParams = {
TableName: usersTableName,
BillingMode: 'PAY_PER_REQUEST',
AttributeDefinitions: [
{ AttributeName: 'username', AttributeType: 'S' },
{ AttributeName: 'app-id', AttributeType: 'S' },
{ AttributeName: 'user-id', AttributeType: 'S' }
],
KeySchema: [
{ AttributeName: 'username', KeyType: 'HASH' },
{ AttributeName: 'app-id', KeyType: 'RANGE' }
],
GlobalSecondaryIndexes: [
{
IndexName: userIdIndex,
KeySchema: [
{ AttributeName: 'user-id', KeyType: 'HASH' }
],
Projection: { ProjectionType: 'ALL' }
},
{
IndexName: appIdIndex,
KeySchema: [
{ AttributeName: 'app-id', KeyType: 'HASH' }
],
Projection: { ProjectionType: 'ALL' }
}
]
}
// the sessions table holds a record per admin OR user session
const sessionsTableParams = {
TableName: sessionsTableName,
BillingMode: 'PAY_PER_REQUEST',
AttributeDefinitions: [
{ AttributeName: 'session-id', AttributeType: 'S' },
{ AttributeName: 'auth-token', AttributeType: 'S' },
],
KeySchema: [
{ AttributeName: 'session-id', KeyType: 'HASH' }
],
GlobalSecondaryIndexes: [
{
IndexName: authTokenIndex,
KeySchema: [
{ AttributeName: 'auth-token', KeyType: 'HASH' }
],
Projection: { ProjectionType: 'ALL' }
},
]
}
const sessionsTimeToLive = {
TableName: sessionsTableName,
TimeToLiveSpecification: {
AttributeName: 'ttl',
Enabled: true
}
}
// the database table holds a record per database
const databaseTableParams = {
AttributeDefinitions: [
{ AttributeName: 'database-id', AttributeType: 'S' },
{ AttributeName: 'share-token-id-read-only', AttributeType: 'S' },
{ AttributeName: 'share-token-id-write', AttributeType: 'S' },
],
KeySchema: [
{ AttributeName: 'database-id', KeyType: 'HASH' },
],
BillingMode: 'PAY_PER_REQUEST',
TableName: databaseTableName,
GlobalSecondaryIndexes: [
{
IndexName: shareTokenIdReadOnlyIndex,
KeySchema: [
{ AttributeName: 'share-token-id-read-only', KeyType: 'HASH' }
],
Projection: { ProjectionType: 'ALL' }
},
{
IndexName: shareTokenIdWriteIndex,
KeySchema: [
{ AttributeName: 'share-token-id-write', KeyType: 'HASH' }
],
Projection: { ProjectionType: 'ALL' }
}
]
}
// the user database table holds a record for each user database relationship
const userDatabaseTableParams = {
AttributeDefinitions: [
{ AttributeName: 'user-id', AttributeType: 'S' },
{ AttributeName: 'database-name-hash', AttributeType: 'S' },
{ AttributeName: 'database-id', AttributeType: 'S' },
],
KeySchema: [
{ AttributeName: 'user-id', KeyType: 'HASH' },
{ AttributeName: 'database-name-hash', KeyType: 'RANGE' }
],
GlobalSecondaryIndexes: [
{
IndexName: userDatabaseIdIndex,
KeySchema: [
{ AttributeName: 'database-id', KeyType: 'HASH' },
{ AttributeName: 'user-id', KeyType: 'RANGE' },
],
Projection: { ProjectionType: 'ALL' }
},
],
BillingMode: 'PAY_PER_REQUEST',
TableName: userDatabaseTableName
}
const userDatabasesTimeToLive = {
TableName: userDatabaseTableName,
TimeToLiveSpecification: {
AttributeName: 'ttl',
Enabled: true
}
}
// the transactions table holds a record per database transaction
const transactionsTableParams = {
AttributeDefinitions: [
{ AttributeName: 'database-id', AttributeType: 'S' },
{ AttributeName: 'sequence-no', AttributeType: 'N' }
],
KeySchema: [
{ AttributeName: 'database-id', KeyType: 'HASH' },
{ AttributeName: 'sequence-no', KeyType: 'RANGE' }
],
BillingMode: 'PAY_PER_REQUEST',
TableName: transactionsTableName
}
// the deleted users table holds a record per deleted user
const deletedUsersTableParams = {
TableName: deletedUsersTableName,
BillingMode: 'PAY_PER_REQUEST',
AttributeDefinitions: [
{ AttributeName: 'user-id', AttributeType: 'S' }
],
KeySchema: [
{ AttributeName: 'user-id', KeyType: 'HASH' }
]
}
// the deleted apps table holds a record per deleted app
const deletedAppsTableParams = {
TableName: deletedAppsTableName,
BillingMode: 'PAY_PER_REQUEST',
AttributeDefinitions: [
{ AttributeName: 'app-id', AttributeType: 'S' }
],
KeySchema: [
{ AttributeName: 'app-id', KeyType: 'HASH' }
]
}
// the deleted apps table holds a record per deleted app
const deletedAdminsTableParams = {
TableName: deletedAdminsTableName,
BillingMode: 'PAY_PER_REQUEST',
AttributeDefinitions: [
{ AttributeName: 'admin-id', AttributeType: 'S' }
],
KeySchema: [
{ AttributeName: 'admin-id', KeyType: 'HASH' }
]
}
logger.info('Creating DynamoDB tables if necessary')
await Promise.all([
createTable(ddb, adminTableParams),
createTable(ddb, adminAccessTokensTableParams),
createTable(ddb, appsTableParams),
createTable(ddb, domainWhitelistTableParams),
createTable(ddb, usersTableParams),
createTable(ddb, sessionsTableParams),
createTable(ddb, databaseTableParams),
createTable(ddb, userDatabaseTableParams),
createTable(ddb, transactionsTableParams),
createTable(ddb, deletedUsersTableParams),
createTable(ddb, deletedAppsTableParams),
createTable(ddb, deletedAdminsTableParams),
])
logger.info('Setting time to live on tables if necessary')
await Promise.all([
setTimeToLive(ddb, sessionsTimeToLive),
setTimeToLive(ddb, userDatabasesTimeToLive),
])
}
async function setupS3() {
s3 = new aws.S3({ apiVersion: '2006-03-01' })
// holds database's encrypted states
const dbStatesBucketParams = {
Bucket: getDbStatesBucketName(),
ACL: 'private'
}
// holds encrypted files
const filesBucketParams = {
Bucket: getFilesBucketName(),
ACL: 'private'
}
logger.info('Creating S3 buckets if necessary')
await Promise.all([
createBucket(s3, dbStatesBucketParams),
createBucket(s3, filesBucketParams),
])
}
async function createTable(ddb, params) {
params.Tags = [{ Key: 'DDBTableGroupKey-' + ddbTableGroup, Value: ddbTableGroup }]
try {
await ddb.createTable(params).promise()
logger.info(`Table ${params.TableName} created successfully`)
} catch (e) {
if (!e.message.includes('Table already exists')) {
throw e
}
}
await ddb.waitFor('tableExists', { TableName: params.TableName, $waiter: { delay: 2, maxAttempts: 60 } }).promise()
const enableBackup = async function () {
try {
await ddb.updateContinuousBackups({ TableName: params.TableName, PointInTimeRecoverySpecification: { PointInTimeRecoveryEnabled: true } }).promise()
} catch (e) {
if (e.code === 'ContinuousBackupsUnavailableException') {
setTimeout(enableBackup, 15000)
} else {
logger.error(e)
throw e
}
}
}
enableBackup()
}
async function setTimeToLive(ddb, params) {
try {
await ddb.updateTimeToLive(params).promise()
logger.info(`Time to live set on ${params.TableName} successfully`)
} catch (e) {
if (!e.message.includes('TimeToLive is already enabled')) {
throw e
}
}
}
async function createBucket(s3, params) {
try {
await s3.createBucket(params).promise()
logger.info(`Bucket ${params.Bucket} created successfully`)
} catch (e) {
if (!e.message.includes('Your previous request to create the named bucket succeeded')) {
throw e
}
}
await s3.waitFor('bucketExists', { Bucket: params.Bucket, $waiter: { delay: 2, maxAttempts: 60 } }).promise()
await s3.putPublicAccessBlock({
Bucket: params.Bucket,
PublicAccessBlockConfiguration: {
BlockPublicAcls: true,
BlockPublicPolicy: true,
IgnorePublicAcls: true,
RestrictPublicBuckets: true
}
}).promise()
await s3.putBucketEncryption({
Bucket: params.Bucket,
ServerSideEncryptionConfiguration: {
Rules: [{ ApplyServerSideEncryptionByDefault: { SSEAlgorithm: 'AES256' } }]
}
}).promise()
}
async function setupSM() {
logger.info('Setting environment variables from secrets manager')
sm = new aws.SecretsManager()
const secrets = await getSecrets()
if (!secrets[crypto.diffieHellman.dhPrivateKeyName]) {
await setupDhSecret(secrets)
}
for (const [key, value] of Object.entries(secrets)) {
process.env['sm.' + key] = value
}
}
async function setupDhSecret(secrets) {
const dhPrivateKey = crypto.diffieHellman.generatePrivateKey().toString('hex')
await updateSecrets(secrets, crypto.diffieHellman.dhPrivateKeyName, dhPrivateKey)
logger.info('Successfully created diffie hellman private key')
}
async function getSecrets() {
try {
await sm.createSecret({ Name: secretManagerSecretId }).promise()
} catch (e) {
if (e.code !== 'ResourceExistsException') {
throw e
}
}
try {
const secret = await sm.getSecretValue({ SecretId: secretManagerSecretId }).promise()
const secrets = JSON.parse(secret.SecretString)
return secrets
} catch (e) {
if (e.code !== 'ResourceNotFoundException') {
throw e
}
return {}
}
}
async function updateSecrets(secrets, secretKeyName, secretValue) {
secrets[secretKeyName] = secretValue
const params = {
SecretId: secretManagerSecretId,
SecretString: JSON.stringify(secrets)
}
await sm.updateSecret(params).promise()
process.env['sm.' + secretKeyName] = secretValue
}
async function setupSes() {
logger.info('Setting up SES')
ses = new aws.SES()
}
async function sendEmail(to, subject, body) {
if (!emailDomain) {
throw new Error('Email domain not set')
}
const params = {
Source: 'no-reply@' + emailDomain,
Destination: { ToAddresses: [to] },
Message: {
Subject: { Data: subject, Charset: 'UTF-8' },
Body: {
Text: { Data: body, Charset: 'UTF-8' },
Html: { Data: body, Charset: 'UTF-8' }
}
}
}
try {
await ses.sendEmail(params).promise()
logger.info('Email sent successfully')
} catch (e) {
logger.error('Failed to send email')
throw e
}
}
async function setupEc2PeerDiscovery() {
if (!peers.ipAddresses) peers.ipAddresses = []
const thisInstanceId = await thisEc2InstanceId()
if (thisInstanceId) {
const ec2 = new aws.EC2({ apiVersion: '2016-11-15' })
// get the stack name used for all EC2's in the same scaling group
const stackName = (await ec2.describeTags({
Filters: [{ Name: 'resource-id', Values: [thisInstanceId] }]
}).promise())
.Tags
.find(tag => tag.Key === 'aws:cloudformation:stack-name')
.Value
peers.ipAddresses = await getEc2Peers(ec2, stackName, thisInstanceId) || []
// check for EC2 peers every 30 seconds
setInterval(async function () {
// if request to getEc2Peers() fails, don't change current list of peers
peers.ipAddresses = await getEc2Peers(ec2, stackName, thisInstanceId) || peers.ipAddresses
}, 30000)
}
}
async function thisEc2InstanceId() {
try {
return await new Promise((resolve, reject) => {
new aws.MetadataService({
httpOptions: {
timeout: 2000,
maxRetries: 0
}
}).request('/latest/dynamic/instance-identity/document', function (err, data) {
if (err) {
reject(err)
return
}
const instanceId = JSON.parse(data).instanceId
logger.info('Running on EC2...')
resolve(instanceId)
})
setTimeout(() => reject(new Error('timeout')), 5000)
})
} catch {
logger.info('Not running on EC2...')
return null
}
}
async function getEc2Peers(ec2, stackName, thisInstanceId) {
try {
const ec2Peers = []
const ec2Instances = await ec2.describeInstances({
Filters: [{ Name: 'tag:aws:cloudformation:stack-name', Values: [stackName] }]
}).promise()
for (let i = 0; i < ec2Instances.Reservations.length; i++) {
const instances = ec2Instances.Reservations[i].Instances
for (let j = 0; j < instances.length; j++) {
const instance = instances[j]
if (instance.InstanceId !== thisInstanceId) {
ec2Peers.push(instance.PrivateIpAddress)
}
}
}
return ec2Peers
} catch (e) {
logger.child({ err: e }).warn('Failed to discover ec2 peers')
return null
}
}