-
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.
- Loading branch information
Showing
12 changed files
with
1,426 additions
and
22 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
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
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
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,19 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { APIGlobalHandler } from '../../utils/apiErrorHandler'; | ||
import { fromIni } from '@aws-sdk/credential-providers'; | ||
import { IS_PRODUCTION } from '../../utils/constants'; | ||
|
||
const credentials = fromIni({ profile: 'default' }); | ||
|
||
// this handler is required to pull shared AWS credentials stored locally | ||
// for use in middleware, as the middleware cannot access the filesystem | ||
// directly | ||
async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (IS_PRODUCTION) return res.status(403).json('local dev use only'); | ||
res.status(200).json(await credentials()); | ||
} | ||
|
||
const apiHandler = (req: NextApiRequest, res: NextApiResponse) => | ||
APIGlobalHandler(req, res, handler); | ||
|
||
export default apiHandler; |
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
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,65 @@ | ||
import { | ||
GetSecretValueCommand, | ||
SecretsManagerClient, | ||
SecretsManagerClientConfig, | ||
} from '@aws-sdk/client-secrets-manager'; | ||
import { atou } from './utils'; | ||
import { logger } from '../logger'; | ||
import { IS_PRODUCTION } from '../constants'; | ||
|
||
const HOST = process.env.HOST; | ||
const CSRF_SECRET_ARN = process.env.CSRF_SECRET_ARN; | ||
const AWS_REGION = process.env.AWS_REGION; | ||
|
||
const fetchCredentials = async () => { | ||
const response = await fetch(`${HOST}/api/aws-local`); | ||
const json = await response.json(); | ||
return json; | ||
}; | ||
|
||
let _client: SecretsManagerClient; | ||
|
||
const getClient = async () => { | ||
if (!_client) { | ||
const clientConfig: SecretsManagerClientConfig = {}; | ||
if (!IS_PRODUCTION) { | ||
clientConfig.credentials = await fetchCredentials(); | ||
clientConfig.region = AWS_REGION; | ||
} | ||
_client = new SecretsManagerClient(clientConfig); | ||
} | ||
return _client; | ||
}; | ||
|
||
const fetchSecret = async () => { | ||
const getSecretCommand = new GetSecretValueCommand({ | ||
SecretId: CSRF_SECRET_ARN, | ||
}); | ||
try { | ||
logger.info('Fetching CSRF secret'); | ||
const client = await getClient(); | ||
const response = await client.send(getSecretCommand); | ||
return atou(JSON.parse(response.SecretString).csrfSecret); | ||
} catch (e) { | ||
// the aws sdk throws a string, not an error... | ||
if (typeof e === 'string') throw new Error(e); | ||
throw e; | ||
} | ||
}; | ||
|
||
class CsrfSecret { | ||
private _lastFetch: number; | ||
// 1 hour | ||
private _ttl: number = 3600 * 1000; | ||
private _value: Uint8Array; | ||
|
||
async get() { | ||
if (!this._value || Date.now() - this._lastFetch >= this._ttl) { | ||
this._value = await fetchSecret(); | ||
this._lastFetch = Date.now(); | ||
} | ||
return this._value; | ||
} | ||
} | ||
|
||
export const csrfSecret = new CsrfSecret(); |
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
Oops, something went wrong.