Skip to content

Instantly share code, notes, and snippets.

@robingustafsson
Created March 25, 2019 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robingustafsson/799477e447e2889872e74c345f06ded6 to your computer and use it in GitHub Desktop.
Save robingustafsson/799477e447e2889872e74c345f06ded6 to your computer and use it in GitHub Desktop.
Postman-to-k6 PR review: AWS v4 auth
{
"info": {
"_postman_id": "737b103a-8e4b-4cc9-8d12-5e38486dc5aa",
"name": "Postman-to-k6: AWSv4",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "AWSv4",
"event": [
{
"listen": "test",
"script": {
"id": "bb870956-ada5-4bba-a805-eaacfee78f9f",
"exec": [
"pm.test(\"AWSv4 body matches string\", function () {",
" pm.expect(pm.response.text()).to.include(\"eu-north-1\");",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"auth": {
"type": "awsv4",
"awsv4": [
{
"key": "secretKey",
"value": "{{AWS_SECRET_KEY}}",
"type": "string"
},
{
"key": "accessKey",
"value": "{{AWS_ACCESS_KEY}}",
"type": "string"
},
{
"key": "service",
"value": "ec2",
"type": "string"
},
{
"key": "region",
"value": "us-east-1",
"type": "string"
}
]
},
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "https://ec2.us-east-1.amazonaws.com/?Action=DescribeRegions&Version=2014-06-15",
"protocol": "https",
"host": [
"ec2",
"us-east-1",
"amazonaws",
"com"
],
"path": [
""
],
"query": [
{
"key": "Action",
"value": "DescribeRegions"
},
{
"key": "Version",
"value": "2014-06-15"
}
]
}
},
"response": []
}
]
}
// Auto-generated by the Load Impact converter
import "./libs/shim/core.js";
import "./libs/shim/expect.js";
import URI from "./libs/urijs.js";
import aws4 from "./libs/aws4.js";
export let options = { maxRedirects: 4 };
const Request = Symbol.for("request");
postman[Symbol.for("initial")]({
environment: {
AWS_ACCESS_KEY: "<removed>",
AWS_SECRET_KEY: "<removed>"
}
});
export default function() {
postman[Request]({
name: "AWSv4",
method: "GET",
address:
"https://ec2.us-east-1.amazonaws.com/?Action=DescribeRegions&Version=2014-06-15",
post(response) {
pm.test("AWSv4 body matches string", function() {
pm.expect(pm.response.text()).to.include("eu-north-1");
});
},
auth(config, Var) {
const address = new URI(config.address);
const options = {
method: "get",
protocol: address.protocol(),
hostname: address.hostname(),
port: address.port(),
path: address.path() + address.search(),
region: "us-east-1",
service: "ec2"
};
const credential = {
accessKeyId: `${pm[Var]("AWS_ACCESS_KEY")}`,
secretAccessKey: `${pm[Var]("AWS_SECRET_KEY")}`
};
const signed = aws4.sign(options, credential);
config.address = new URI()
.protocol(address.protocol())
.hostname(signed.hostname)
.path(signed.path)
.toString();
Object.assign(config.headers, signed.headers);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment