0
votes

I'm trying to generate a signed AWS request in JMeter.

My first attempt was to put the same parameters, that work fine in my Postman requests, inside an HTTP Header Manager. Those parameters were "AccessKey", "SecretKey", "AWS Region", "Service Name" and "Session Token" but the response said "Access denied". In a best case scenario, I just used the wrong names in this step and renaming them already fixes everything.

My second attempt was to follow the AWS documentation and write a Java class that generates the signed request. Then I wanted to use a JSR223 PreProcessor to set the variables using vars.put. How can I do this correctly? I'm new to JMeter and thought that I might be able to paste my Java code, add the vars.put statement and that's it. But sadly this doesn't create a variable called "aaauthorization" if I run a Debug Sampler afterwards:

class aws_request {

public static String bytesToHex(byte[] hash) {
    ...
    return hexString.toString();
}

private static byte[] HmacSHA256(String data, byte[] key) throws Exception {
    ...
    return mac.doFinal(data.getBytes("UTF8"));
}

private static byte[] getSignatureKey(String key, String dateStamp, String regionName, String serviceName)
        throws Exception {
    ...
    return kSigning;
}

public static String getAuthorization() {
    try {...
        return authorization;
    } catch (Exception e) {
        return "failed";
    }

}
public static void main(String args[]) {
    vars.put("aaauthorization", getAuthorization());
}
}

Thank you in advance for your help!

1

1 Answers

1
votes

You need to remove your class aws_request { definition and closing curly bracket }. JSR223 Test Elements are different from normal Java classes, the code is being evaluated via ScriptEngine therefore you are not allowed to have "classes" however normal methods will be resolved and you will be able to execute them.

If your Groovy script isn't doing what it is supposed to be doing first of all check jmeter.log file - if there will be any errors - they will go there.

See Apache Groovy - Why and How You Should Use It article for more information on using Groovy scripting in JMeter tests.

Don't be confused with java language option, it is being implemented via Beanshell interpreter which limits you to Java SE 1.5 language features and its performance is a big question mark when it comes to high loads.