Skip to content

Commit

Permalink
Inlined and adapted dependency code
Browse files Browse the repository at this point in the history
Includes:

* Add `CMD` to Docker configuration to reflect the container's entrypoint
* Split export and copy functionality into separate classes
* Isolate AWS configuration within its own module
* Move "glue" code to `index.js`
* Set default value for S3 bucket
* Add docker-compose configuration to mimic how we will run and configure the container in Fargate
  • Loading branch information
mjgiarlo committed May 2, 2019
1 parent 0258213 commit 4059b7e
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ COPY . .

RUN npm install

# TODO: Add CMD later once JavaScript package is in place
CMD ["bin/backup"]
2 changes: 1 addition & 1 deletion bin/backup
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash

npx babel-node src/backupUsers.js
npx babel-node index.js
2 changes: 1 addition & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ module.exports = {
awsAccessKey: process.env.AWS_ACCESS_KEY_ID || '',
awsAccessSecret: process.env.AWS_SECRET_ACCESS_KEY || '',
awsRegion: process.env.AWS_REGION || 'us-west-2',
s3BucketUri: process.env.S3_BUCKET_URI || '',
s3BucketUri: process.env.S3_BUCKET_URI || 'sinopia-cognito-development',
userPoolId: process.env.COGNITO_USER_POOL_ID || 'us-west-2_CGd9Wq136'
}
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: "3"
services:
app:
build:
context: .
environment:
AWS_REGION: ${AWS_REGION:-us-west-2}
AWS_ACCESS_KEY_ID: "${AWS_ACCESS_KEY_ID}" # add to .env file (DO NOT CHECK IN)
AWS_SECRET_ACCESS_KEY: "${AWS_SECRET_ACCESS_KEY}" # add to .env file (DO NOT CHECK IN)
COGNITO_USER_POOL_ID: ${COGNITO_USER_POOL_ID:-us-west-2_CGd9Wq136}
S3_BUCKET_URI: ${S3_BUCKET_URI:-sinopia-cognito-development}
command: bin/backup
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ExportUsers from './src/ExportUsers'
import CopyUsers from './src/CopyUsers'

const userBackup = async () => {
const userList = await new ExportUsers().export()
new CopyUsers(userList).copy()
}

userBackup()
26 changes: 26 additions & 0 deletions src/CopyUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import AWS from 'aws-sdk'
import config from 'config'
import { configureAWS } from './configureAWS'

export default class CopyUsers {
constructor(userList) {
AWS.config = configureAWS(AWS.config)
this.userListString = JSON.stringify(userList)
this.s3 = new AWS.S3()
this.objectKey = `${new Date().toISOString()}/${config.get('userPoolId')}.json`
}

copy() {
this.s3.putObject({
Body: this.userListString,
Bucket: config.get('s3BucketUri'),
Key: this.objectKey
}, error => {
if (error) {
console.error(`error copying backup to S3: ${error}`)
return
}
console.log('Users backed up to S3')
})
}
}
30 changes: 30 additions & 0 deletions src/ExportUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import AWS from 'aws-sdk'
import config from 'config'
import { configureAWS } from './configureAWS'

export default class ExportUsers {
constructor() {
AWS.config = configureAWS(AWS.config)
this.cognito = new AWS.CognitoIdentityServiceProvider()
}

async export() {
const params = { UserPoolId: config.get('userPoolId') }

const paginationCalls = async (exportedUsers = []) => {
const { Users = [], PaginationToken } = await this.cognito.listUsers(params).promise()
const combinedUsers = exportedUsers.concat(Users)

if (PaginationToken) {
params.PaginationToken = PaginationToken
await paginationCalls(combinedUsers)
}

return combinedUsers
}

const userList = await paginationCalls()
console.log('Users exported from Cognito')
return userList
}
}
36 changes: 0 additions & 36 deletions src/backupUsers.js

This file was deleted.

12 changes: 12 additions & 0 deletions src/configureAWS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import AWS from 'aws-sdk'
import config from 'config'

export const configureAWS = configuration => {
configuration.update({ region: config.get('awsRegion') })

configuration.credentials = new AWS.Credentials({
accessKeyId: config.get('awsAccessKey'), secretAccessKey: config.get('awsAccessSecret')
})

return configuration
}

0 comments on commit 4059b7e

Please sign in to comment.