-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
9 changed files
with
92 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ COPY . . | |
|
||
RUN npm install | ||
|
||
# TODO: Add CMD later once JavaScript package is in place | ||
CMD ["bin/backup"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |