Skip to content

Instantly share code, notes, and snippets.

@gbvanrenswoude
Last active April 1, 2022 17:12
Show Gist options
  • Save gbvanrenswoude/35cd61d39919371dcb6e6c6643197c55 to your computer and use it in GitHub Desktop.
Save gbvanrenswoude/35cd61d39919371dcb6e6c6643197c55 to your computer and use it in GitHub Desktop.
postman pre-request script integrating with local server that links temp credentials through
const profile = pm.collectionVariables.get("aws_profile")
if (!profile) {
console.log('No aws_profile variable set, using aws_profile: default')
pm.collectionVariables.set("aws_profile", "default");
}
const a = pm.collectionVariables.get("aws_access_key")
const b = pm.collectionVariables.get("aws_secret_key")
if (!a && !b) {
console.log('Using nn-auth served aws_access_key and aws_secret_key at collection level')
pm.sendRequest({
url: `https://localhost:9898/aws/${profile}`,
method: "POST",
}, function (_, response) {
if (response.status == "OK") {
const body = response.json()
pm.collectionVariables.set("aws_access_key", body.accessKey)
pm.collectionVariables.set("aws_secret_key", body.secretKey)
if (body.sessionToken) {
pm.collectionVariables.set("aws_session_token", body.sessionToken)
}
console.log(`Using aws credentials from '${profile}'`)
return
} else {
throw new Error(response.text() || "Error fetching AWS credentials, did you run: nn-auth --no-proxy serve-postman?")
}
}
)
}
else {
console.log('Using in Postman collection defined aws_access_key_id and aws_secret_access_key')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment