-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #22
- Loading branch information
Showing
11 changed files
with
155 additions
and
18 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,24 @@ | ||
import { SecretsManager } from '@aws-sdk/client-secrets-manager'; | ||
|
||
export const getSecretString = async (secretId: string, smClient: SecretsManager, jsonField?: string): Promise<string> => { | ||
const secretToEncrypt = await smClient.getSecretValue({ SecretId: secretId }); | ||
|
||
let secretString = secretToEncrypt.SecretString; | ||
if (!secretString) { | ||
throw new Error('SecretString is empty from secret with id: ' + secretId); | ||
} | ||
if (jsonField) { | ||
try { | ||
secretString = JSON.parse(secretString)[jsonField]; | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
throw new Error('Error while parsing SecretString with id: ' + secretId + ' and jsonField: ' + jsonField + ': ' + error.message); | ||
} | ||
throw error; | ||
} | ||
} | ||
if (!secretString) { | ||
throw new Error('SecretString is empty from secret with id: ' + secretId); | ||
} | ||
return secretString; | ||
}; |
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,4 @@ | ||
export * from './aws-secret-helper'; | ||
export * from './github-helper'; | ||
export * from './github-secret-encryptor'; | ||
export * from './github-secret-name-validator'; |
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,64 @@ | ||
import { GetSecretValueCommand, SecretsManager } from '@aws-sdk/client-secrets-manager'; | ||
import { mockClient } from 'aws-sdk-client-mock'; | ||
import { getSecretString } from '../../../src/handler/secrets/aws-secret-helper'; | ||
|
||
describe('aws-secret-helper', () => { | ||
|
||
const smMock = mockClient(SecretsManager); | ||
const SecretId = 'arn:aws:secretsmanager:eu-central-1:123456789012:secret:secret-id'; | ||
const sm = new SecretsManager({}); | ||
|
||
it('getSecretValue', async () => { | ||
smMock.on(GetSecretValueCommand, { | ||
SecretId, | ||
}).resolves({ | ||
SecretString: 'mySecretValue', | ||
}); | ||
expect(await getSecretString(SecretId, sm)).toBe('mySecretValue'); | ||
}); | ||
|
||
it('getSecretValue with jsonField', async () => { | ||
smMock.on(GetSecretValueCommand, { | ||
SecretId, | ||
}).resolves({ | ||
SecretString: JSON.stringify({ mySecretKey: 'mySecretValue' }), | ||
}); | ||
expect(await getSecretString(SecretId, sm, 'mySecretKey')).toBe('mySecretValue'); | ||
}); | ||
|
||
it('getSecretValue with jsonField without json structure', async () => { | ||
smMock.on(GetSecretValueCommand, { | ||
SecretId, | ||
}).resolves({ | ||
SecretString: 'mySecretValue', | ||
}); | ||
|
||
await expect(getSecretString(SecretId, sm, 'mySecretKey')) | ||
.rejects | ||
.toThrowError('Error while parsing SecretString with id: arn:aws:secretsmanager:eu-central-1:123456789012:secret:secret-id and jsonField: mySecretKey: Unexpected token m in JSON at position 0'); | ||
}); | ||
|
||
it('getSecretValue with empty jsonField', async () => { | ||
smMock.on(GetSecretValueCommand, { | ||
SecretId, | ||
}).resolves({ | ||
SecretString: JSON.stringify({ mySecretKey: '' }), | ||
}); | ||
|
||
await expect(getSecretString(SecretId, sm, 'mySecretKey')) | ||
.rejects | ||
.toThrowError('SecretString is empty from secret with id: arn:aws:secretsmanager:eu-central-1:123456789012:secret:secret-id'); | ||
}); | ||
|
||
it('getSecretValue with empty secret value', async () => { | ||
smMock.on(GetSecretValueCommand, { | ||
SecretId, | ||
}).resolves({ | ||
SecretString: '', | ||
}); | ||
await expect(getSecretString(SecretId, sm, 'mySecretKey')) | ||
.rejects | ||
.toThrowError('SecretString is empty from secret with id: arn:aws:secretsmanager:eu-central-1:123456789012:secret:secret-id'); | ||
}); | ||
|
||
}); |