Skip to content

Commit

Permalink
Move aws helpers to separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejearley committed Aug 8, 2023
1 parent 5967f29 commit 668ef46
Show file tree
Hide file tree
Showing 9 changed files with 654 additions and 599 deletions.
2 changes: 2 additions & 0 deletions common/aws/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
16 changes: 16 additions & 0 deletions common/aws/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@casimir/aws",
"private": true,
"main": "src/index.ts",
"scripts": {
"build": "echo '@casimir/aws build not specified. Disregard this warning and any listed errors above if @casimir/aws is not needed for the current project build.' && exit 0",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@aws-sdk/client-secrets-manager": "^3.204.0",
"@aws-sdk/credential-providers": "^3.204.0"
},
"devDependencies": {
"@types/node": "^17.0.38"
}
}
38 changes: 38 additions & 0 deletions common/aws/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { exec, execSync } from 'child_process'
import { fromIni } from '@aws-sdk/credential-providers'
import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager'

/**
* Gets a secret from AWS Secrets Manager.
* @param id secret id
* @returns secret string
*/
export async function getSecret(id: string) {
const aws = new SecretsManagerClient({})
const { SecretString } = await aws.send(
new GetSecretValueCommand(
{
SecretId: id
}
)
)
return SecretString
}

/**
* Ensures AWS credentials are available and returns them.
* Checks for AWS credentials in the environment variables.
* If not found, loads credentials from `AWS_PROFILE` or the default profile.
* @returns AWS credentials
*/
export async function loadCredentials() {
const defaultProfile = 'consensus-networks-dev'
if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY) {
process.env.AWS_PROFILE = process.env.AWS_PROFILE || defaultProfile
return await fromIni()()
}
return {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
}
}
21 changes: 21 additions & 0 deletions common/aws/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "ESNext",
"strict": true,
"preserveConstEnums": true,
"noEmit": true,
"sourceMap": false,
"module": "CommonJS",
"moduleResolution": "Node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true
},
"exclude": [
"node_modules"
],
"include": [
"./src/*"
]
}
2 changes: 0 additions & 2 deletions common/helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@aws-sdk/client-secrets-manager": "^3.204.0",
"@aws-sdk/credential-providers": "^3.204.0",
"ethers": "^5.7.2"
},
"devDependencies": {
Expand Down
37 changes: 0 additions & 37 deletions common/helpers/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,6 @@
import { exec, execSync } from 'child_process'
import { fromIni } from '@aws-sdk/credential-providers'
import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager'
import { ethers } from 'ethers'

/**
* Gets a secret from AWS Secrets Manager.
* @param id secret id
* @returns secret string
*/
export async function getSecret(id: string) {
const aws = new SecretsManagerClient({})
const { SecretString } = await aws.send(
new GetSecretValueCommand(
{
SecretId: id
}
)
)
return SecretString
}

/**
* Ensures AWS credentials are available and returns them.
* Checks for AWS credentials in the environment variables.
* If not found, loads credentials from `AWS_PROFILE` or the default profile.
* @returns AWS credentials
*/
export async function loadCredentials() {
const defaultProfile = 'consensus-networks-dev'
if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY) {
process.env.AWS_PROFILE = process.env.AWS_PROFILE || defaultProfile
return await fromIni()()
}
return {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
}
}

/**
* Convert any string to camelCase.
* @param string - The input string
Expand Down
Loading

0 comments on commit 668ef46

Please sign in to comment.