简体   繁体   中英

Generating AWS signature in JMeter

I'm testing an application which is hosted on AWS infrastructure using JMeter tool wherein each request triggered needs to have a AWS signed header passed along with the request to validate the request at the IAM level in AWS. I have the access and secret key pertaining to the user role created in AWS console.

Is there any beanshell code available in JMeter which helps in generating AWS signature using the access and secret key for each request in JMeter?

The code is available on AWS docs website, see Deriving the Signing Key with Java for instance. Here is minimal listing just in case:

static byte[] HmacSHA256(String data, byte[] key) throws Exception {
    String algorithm="HmacSHA256";
    Mac mac = Mac.getInstance(algorithm);
    mac.init(new SecretKeySpec(key, algorithm));
    return mac.doFinal(data.getBytes("UTF8"));
}

static byte[] getSignatureKey(String key, String dateStamp, String regionName, String serviceName) throws Exception {
    byte[] kSecret = ("AWS4" + key).getBytes("UTF8");
    byte[] kDate = HmacSHA256(dateStamp, kSecret);
    byte[] kRegion = HmacSHA256(regionName, kDate);
    byte[] kService = HmacSHA256(serviceName, kRegion);
    byte[] kSigning = HmacSHA256("aws4_request", kService);
    return kSigning;
}

Be aware that starting from JMeter version 3.1 it is recommended to use JSR223 Elements and Groovy language for scripting as it is more modern (supports all new Java SDK features), has a lot of "syntax sugar" on top of "normal" Java SDK and Groovy performance is much better comparing to Beanshell, JavaScript and other available options. See Apache Groovy - Why and How You Should Use It article for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM