feat: add support aws govcloud (us)

Signed-off-by: Timothy Deakin <cftad@protonmail.com>
This commit is contained in:
Timothy Deakin
2024-04-07 05:29:51 +01:00
parent cb3092ea7d
commit 5f1d385489
2 changed files with 26 additions and 10 deletions
@@ -26,7 +26,13 @@ import { makeProfileInfo, provisionKeyCache } from './helpers';
jest.mock('crypto');
const cryptoMock = crypto as jest.Mocked<any>;
describe('helpers', () => {
describe.each([
['eu-west-1', 'https://public-keys.auth.elb.eu-west-1.amazonaws.com/kid'],
[
'us-gov-west-1',
'https://s3-us-gov-west-1.amazonaws.com/aws-elb-public-keys-prod-us-gov-west-1/kid',
],
])('helpers', (region, url) => {
const server = setupServer();
setupRequestMockHandlers(server);
@@ -37,7 +43,7 @@ describe('helpers', () => {
jest.clearAllMocks();
server.use(
http.get(
'https://public-keys.auth.elb.eu-west-1.amazonaws.com/kid',
url,
() =>
new HttpResponse(
`-----BEGIN PUBLIC KEY-----
@@ -51,12 +57,12 @@ yOlxJ2VW88mLAQGJ7HPAvOdylxZsItMnzCuqNzZvie8m/NJsOjhDncVkrw==
});
it('should create a key', () => {
const getKey = provisionKeyCache('eu-west-1', nodeCache);
const getKey = provisionKeyCache(region, nodeCache);
expect(getKey).toBeDefined();
});
it('should return a key from cache', async () => {
const getKey = provisionKeyCache('eu-west-1', nodeCache);
const getKey = provisionKeyCache(region, nodeCache);
cryptoMock.createPublicKey.mockReturnValueOnce('key');
nodeCache.get = jest.fn().mockReturnValue('key');
@@ -67,7 +73,7 @@ yOlxJ2VW88mLAQGJ7HPAvOdylxZsItMnzCuqNzZvie8m/NJsOjhDncVkrw==
});
it('should update cache if key is not found', async () => {
const getKey = provisionKeyCache('eu-west-1', nodeCache);
const getKey = provisionKeyCache(region, nodeCache);
nodeCache.get = jest.fn().mockReturnValue(undefined);
jest.spyOn(nodeCache, 'set');
@@ -80,7 +86,7 @@ yOlxJ2VW88mLAQGJ7HPAvOdylxZsItMnzCuqNzZvie8m/NJsOjhDncVkrw==
});
it('should throw error if key is not found', async () => {
const getKey = provisionKeyCache('eu-west-1', nodeCache);
const getKey = provisionKeyCache(region, nodeCache);
nodeCache.get = jest.fn().mockReturnValue(undefined);
cryptoMock.createPublicKey.mockReturnValue(undefined);
@@ -91,7 +97,7 @@ yOlxJ2VW88mLAQGJ7HPAvOdylxZsItMnzCuqNzZvie8m/NJsOjhDncVkrw==
});
it('should throw if key is not present in request header', async () => {
const getKey = provisionKeyCache('eu-west-1', nodeCache);
const getKey = provisionKeyCache(region, nodeCache);
nodeCache.get = jest.fn().mockReturnValue(undefined);
@@ -70,6 +70,17 @@ export const makeProfileInfo = (
};
};
const getPublicKeyEndpoint = (region: string) => {
const commercialEndpoint = `https://public-keys.auth.elb.${encodeURIComponent(
region,
)}.amazonaws.com/`;
const govEndpoint = `https://s3-${encodeURIComponent(
region,
)}.amazonaws.com/aws-elb-public-keys-prod-${encodeURIComponent(region)}/`;
return region.startsWith('us-gov') ? govEndpoint : commercialEndpoint;
};
export const provisionKeyCache = (region: string, keyCache: NodeCache) => {
return async (header: JWTHeaderParameters): Promise<KeyObject> => {
if (!header.kid) {
@@ -79,10 +90,9 @@ export const provisionKeyCache = (region: string, keyCache: NodeCache) => {
if (optionalCacheKey) {
return crypto.createPublicKey(optionalCacheKey);
}
const keyText: string = await fetch(
`https://public-keys.auth.elb.${encodeURIComponent(
region,
)}.amazonaws.com/${encodeURIComponent(header.kid)}`,
getPublicKeyEndpoint(region) + header.kid,
).then(response => response.text());
const keyValue = crypto.createPublicKey(keyText);