Skip to content

Commit d00c686

Browse files
author
Phil Varner
authored
Merge pull request #394 from stac-utils/pv/change-permission-to-write
auth pre-hook: change expected value for secrets manager secret from string to array, require write permission
2 parents 64fe3fd + 51da078 commit d00c686

File tree

3 files changed

+5
-6
lines changed

3 files changed

+5
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ To enable this example pre-hook:
912912
- In the serverless.yml file, uncomment the `preHook` function, the `preHook` IAM
913913
permissions, and the environment variables `PRE_HOOK` and `API_KEYS_SECRET_ID`
914914
- Create a Secrets Manager secret with the name used in `API_KEYS_SECRET_ID` with
915-
the keys as the strings allowed for API Keys and the values as `read`.
915+
the keys as the strings allowed for API Keys and the values as an array `["write"]`.
916916
- Build and deploy.
917917

918918
### Post-Hook

src/lambdas/pre-hook/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const response401 = {
1313
}
1414

1515
// eslint-disable-next-line import/no-mutable-exports
16-
export let apiKeys = new Map()
16+
export let apiKeys = new Map() // string -> string[]
1717

1818
const updateApiKeys = async () => {
1919
await new SecretsManagerClient({ region: process.env['AWS_REGION'] || 'us-west-2' })
@@ -35,8 +35,7 @@ const updateApiKeys = async () => {
3535
})
3636
}
3737

38-
const READ = ['read']
39-
const isValidReadToken = (token) => READ.includes(apiKeys.get(token))
38+
const isValidToken = (token) => (apiKeys.get(token) || []).includes('write')
4039

4140
export const handler = async (event, _context) => {
4241
let token = null
@@ -54,7 +53,7 @@ export const handler = async (event, _context) => {
5453
await updateApiKeys()
5554
}
5655

57-
if (isValidReadToken(token)) {
56+
if (isValidToken(token)) {
5857
return event
5958
}
6059

tests/unit/test-pre-hook.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ test.serial('authenticate cases', async (t) => {
9090
// @ts-ignore
9191
.on(GetSecretValueCommand)
9292
// @ts-ignore
93-
.resolves({ SecretString: JSON.stringify({ ABC: 'read', DEF: 'other' }) })
93+
.resolves({ SecretString: JSON.stringify({ ABC: ['write'], DEF: ['other'] }) })
9494

9595
const event = { ...DEFAULT_EVENT }
9696
const context = { ...DEFAULT_CONTEXT }

0 commit comments

Comments
 (0)