| Newsgroups |
gmane.comp.jakarta.jmeter.user |
| Message-ID |
<[email protected]> |
sorry, I made a copy paste error, once again a fixed code snippet:...
and an additional information: you must provide the appKey and the appSecre=
t as an environment variable from outside.
that means that you have to export those variables in the terminla from whi=
ch you start your jmeter session, or as usual in an encapsulating shell scr=
ipt=20
// here the code...
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import java.util.Base64
// Retrieve environment variables (using System.getenv as an example)
def key =3D System.getenv("appKey") ?: "defaultKey"
def secret =3D System.getenv("appSecret") ?: "am1ldGVy"
def ts =3D System.currentTimeMillis()
def baseString =3D "${key}-2-${ts}"
// Decode the secret from Base64
def secretDecoded =3D Base64.decoder.decode(secret)
// Generate HMAC-SHA256 hash
Mac mac =3D Mac.getInstance("HmacSHA256")
mac.init(new SecretKeySpec(secretDecoded, "HmacSHA256"))
byte[] macSignature =3D mac.doFinal(baseString.getBytes("UTF-8"))
// Encode the signature to Base64 and sanitize it
def macSignatureBase64 =3D Base64.encoder.encodeToString(macSignature)
.replace("=3D", "")
.replace("+", "-")
.replace("/", "_")
// Simulated environment variable storage (Postman equivalent)
def environment =3D [:] // A map to mimic Postman environment variables
environment["signature"] =3D macSignatureBase64
environment["timestamp"] =3D ts
// put the result in jmeter vars
vars.put("signature",macSignatureBase64)
vars.put("timestamp","" + ts)
// Debug output
println "Signature: ${macSignatureBase64}"
println "Timestamp: ${ts}"
> Abhitosh Patil <[email protected]> hat am 03.04.2025 21:15 CEST gesch=
rieben:
>=20
>=20
> Hi Chriss,
> Hi executed below Groovy code with JSR223 sampler and got an error for th=
at.
>=20
> Code:
> import javax.crypto.Mac
> import javax.crypto.spec.SecretKeySpec
> import java.util.Base64
>=20
> // Retrieve environment variables (using System.getenv as an example)
> def key =3D System.getenv("d552a1b3-3558-3c47-b34a-1ba87558b74b") ?: "def=
aultKey"
> def secret =3D System.getenv("vz7noe5z17i2usV58oFR5YMtU1BUFL0Ah9+eXuwV6IQ=
=3D") ?: "am1ldGVy"
> def ts =3D System.currentTimeMillis()
> def baseString =3D "${key}-2-${ts}"
>=20
> // Decode the secret from Base64
> def secretDecoded =3D Base64.decoder.decode(secret)
>=20
> // Generate HMAC-SHA256 hash
> Mac mac =3D Mac.getInstance("HmacSHA256")
> mac.init(new SecretKeySpec(secretDecoded, "HmacSHA256"))
> byte[] macSignature =3D mac.doFinal(baseString.getBytes("UTF-8"))
>=20
> // Encode the signature to Base64 and sanitize it
> def macSignatureBase64 =3D Base64.encoder.encodeToString(macSignature)
> .replace("=3D", "")import javax.crypto.Mac
> import javax.crypto.spec.SecretKeySpec
> import java.util.Base64
>=20
> // Retrieve environment variables (using System.getenv as an example)
> def key =3D System.getenv("appKey") ?: "defaultKey"
> def secret =3D System.getenv("appSecret") ?: "am1ldGVy"
>=20
> def ts =3D System.currentTimeMillis()
> def baseString =3D "${key}-2-${ts}"
>=20
> // Decode the secret from Base64
> def secretDecoded =3D Base64.decoder.decode(secret)
>=20
> // Generate HMAC-SHA256 hash
> Mac mac =3D Mac.getInstance("HmacSHA256")
> mac.init(new SecretKeySpec(secretDecoded, "HmacSHA256"))
> byte[] macSignature =3D mac.doFinal(baseString.getBytes("UTF-8"))
>=20
> // Encode the signature to Base64 and sanitize it
> def macSignatureBase64 =3D Base64.encoder.encodeToString(macSignature)
> .replace("=3D", "")
> .replace("+", "-")
> .replace("/", "_")
>=20
> // Simulated environment variable storage (Postman equivalent)
> def environment =3D [:] // A map to mimic Postman environment variables
> environment["signature"] =3D macSignatureBase64
> environment["timestamp"] =3D ts
>=20
> // put the result in jmeter vars
> vars.put("signature",macSignatureBase64)
> vars.put("timestamp","" + ts)
>=20
> // Debug output
> println "Signature: ${macSignatureBase64}"
> println "Timestamp: ${ts}"
>=20
> Getting this error:
>=20
>=20
> On Thu, Apr 3, 2025 at 11:30=E2=80=AFPM Christof Schmalenbach <c.schmalen=
[email protected]> wrote:
> > I see that my last mail was not well formatted; here another trial...
> >=20
> > import javax.crypto.Mac
> > import javax.crypto.spec.SecretKeySpec
> > import java.util.Base64
> >=20
> > // Retrieve environment variables (using System.getenv as an example)
> > def key =3D System.getenv("appKey") ?: "defaultKey"
> > def secret =3D System.getenv("appSecret") ?: "am1ldGVy"
> >=20
> > def ts =3D System.currentTimeMillis()
> > def baseString =3D "${key}-2-${ts}"
> >=20
> > // Decode the secret from Base64
> > def secretDecoded =3D Base64.decoder.decode(secret)
> >=20
> > // Generate HMAC-SHA256 hash
> > Mac mac =3D Mac.getInstance("HmacSHA256")
> > mac.init(new SecretKeySpec(secretDecoded, "HmacSHA256"))
> > byte[] macSignature =3D mac.doFinal(baseString.getBytes("UTF-8"))
> >=20
> > // Encode the signature to Base64 and sanitize it
> > def macSignatureBase64 =3D Base64.encoder.encodeToString(macSignature)
> > .replace("=3D", "")import javax.crypto.Mac
> > import javax.crypto.spec.SecretKeySpec
> > import java.util.Base64
> >=20
> > // Retrieve environment variables (using System.getenv as an example)
> > def key =3D System.getenv("appKey") ?: "defaultKey"
> > def secret =3D System.getenv("appSecret") ?: "am1ldGVy"
> >=20
> > def ts =3D System.currentTimeMillis()
> > def baseString =3D "${key}-2-${ts}"
> >=20
> > // Decode the secret from Base64
> > def secretDecoded =3D Base64.decoder.decode(secret)
> >=20
> > // Generate HMAC-SHA256 hash
> > Mac mac =3D Mac.getInstance("HmacSHA256")
> > mac.init(new SecretKeySpec(secretDecoded, "HmacSHA256"))
> > byte[] macSignature =3D mac.doFinal(baseString.getBytes("UTF-8"))
> >=20
> > // Encode the signature to Base64 and sanitize it
> > def macSignatureBase64 =3D Base64.encoder.encodeToString(macSignature)
> > .replace("=3D", "")
> > .replace("+", "-")
> > .replace("/", "_")
> >=20
> > // Simulated environment variable storage (Postman equivalent)
> > def environment =3D [:] // A map to mimic Postman environment variables
> > environment["signature"] =3D macSignatureBase64
> > environment["timestamp"] =3D ts
> >=20
> > // put the result in jmeter vars
> > vars.put("signature",macSignatureBase64)
> > vars.put("timestamp","" + ts)
> >=20
> > // Debug output
> > println "Signature: ${macSignatureBase64}"
> > println "Timestamp: ${ts}"
> >=20
> > .replace("+", "-")
> > .replace("/", "_")
> >=20
> > // Simulated environment variable storage (Postman equivalent)
> > def environment =3D [:] // A map to mimic Postman environment variables
> > environment["signature"] =3D macSignatureBase64
> > environment["timestamp"] =3D ts
> >=20
> > // put the result in jmeter vars
> > vars.put("signature",macSignatureBase64)
> > vars.put("timestamp","" + ts)
> >=20
> > // Debug output
> > println "Signature: ${macSignatureBase64}"
> > println "Timestamp: ${ts}"
> >=20
> >=20
> > On 4/3/25 19:49, Christof Schmalenbach wrote:
> > > Hi Abhitosh,
> > > I'm not sure, whether I understand your js code completely.
> > > But I got at least an idea. This is the result of my code migration.
> > > I test the code in a jmeter script within a jsr223 sampler and it see=
ms
> > > to work.
> > > (OpenJDK version 19.X; may be that in java version 8.X not all packag=
es
> > > used are supported???)
> > >
> > > regards
> > > Christof
> > >
> > > import javax.crypto.Mac import javax.crypto.spec.SecretKeySpec import
> > > java.util.Base64 // Retrieve environment variables (using System.gete=
nv
> > > as an example) def key =3D System.getenv("appKey") ?: "defaultKey" de=
f
> > > secret =3D System.getenv("appSecret") ?: "am1ldGVy" def ts =3D
> > > System.currentTimeMillis() def baseString =3D "${key}-2-${ts}" // Dec=
ode
> > > the secret from Base64 def secretDecoded =3D Base64.decoder.decode(se=
cret)
> > > // Generate HMAC-SHA256 hash Mac mac =3D Mac.getInstance("HmacSHA256"=
)
> > > mac.init(new SecretKeySpec(secretDecoded, "HmacSHA256")) byte[]
> > > macSignature =3D mac.doFinal(baseString.getBytes("UTF-8")) // Encode =
the
> > > signature to Base64 and sanitize it def macSignatureBase64 =3D
> > > Base64.encoder.encodeToString(macSignature) .replace("=3D", "")
> > > .replace("+", "-") .replace("/", "_") // Simulated environment variab=
le
> > > storage (Postman equivalent) def environment =3D [:] // A map to mimi=
c
> > > Postman environment variables environment["signature"] =3D
> > > macSignatureBase64 environment["timestamp"] =3D ts // Debug output pr=
intln
> > > "Signature: ${macSignatureBase64}" println "Timestamp: ${ts}"
> > >
> > >
> > >
> > > On 4/3/25 16:32, Abhitosh Patil wrote:
> > >> Javascript:
> > >>
> > >> var key =3D pm.environment.get('appKey);
> > >>
> > >> var secret =3D pm.environment.get('appSecret);
> > >>
> > >> var ts =3D (new Date()).getTime());
> > >> var baseString =3D key + "-" + "2" + "-" + ts;
> > >>
> > >> var words =3D CryptoJS. enc.Base64.parse(secret);
> > >>
> > >> vaz macSignature =3D CryptoJS. HmacSHA256 (CryptoJS. enc. Utf8.parse
> > >> (baseString), words);
> > >>
> > >> var macSignatureBase64 =3D macSignature.toString (CryptoJS. enc.Base=
64);
> > >>
> > >> macSignatureBase64 =3D macSignatureBase64.replace(/=3D/g, "");
> > >>
> > >> mac SignatureBase64 =3D macSignatureBase64.replace(/\+/g, "-");
> > >>
> > >> macSignatureBase64 =3D macSignatureBase64.replace(/\//g, "_");
> > >>
> > >> postman.setEnvironmentVariable("signature", macSignatureBase64);
> > >>
> > >> postman, setEnvironmentVariable("timestamp", ts);
> > >>
> > >> On Thu, 3 Apr, 2025, 5:12 pm Christof,<[email protected]=
m>
> > >> wrote:
> > >>
> > >>> Hi Abhitosh,
> > >>> is the javascript script small in size?
> > >>> If so you could remove or replace with dummies all hardcoded securi=
ty
> > >>> related parts and attach the script in this channel.
> > >>> May be , that someone will take the challenge and migrate it to
> > >>> groovy .
> > >>> It it is 100 lines or more I will not have the time to do that.
> > >>> Otherwise
> > >>> I will give it a chance.
> > >>>
> > >>> Christof
> > >>>
> > >>>
> > >>>
> > >>> Am 3. April 2025 09:02:36 UTC schrieb Abhitosh Patil <
> > >>> [email protected]>:
> > >>>> Unfortunately ChatGPT or other AI tools are not allowed in our
> > >>> organisation
> > >>>> network.
> > >>>>
> > >>>> On Wed, 2 Apr, 2025, 9:04 pm Abhitosh Patil,<[email protected]=
>
> > >>> wrote:
> > >>>>> Thank you Chris.
> > >>>>> I'll check this possibility.
> > >>>>>
> > >>>>> On Wed, 2 Apr, 2025, 9:03 pm
> > >>>>> Christof,<[email protected]>
> > >>>>> wrote:
> > >>>>>
> > >>>>>> Hi Abhitosh,
> > >>>>>> groovy is one of the supported languages in jsr223 sampler. If y=
our
> > >>>>>> javascript has not thousand codelines, I'm very optimistic, that
> > >>>>>> with
> > >>> the
> > >>>>>> help of chatGPT you should be able to migrate your code
> > >>>>>> from js -> groovy.
> > >>>>>> regards
> > >>>>>> Christof
> > >>>>>>
> > >>>>>>
> > >>>>>> Am 2. April 2025 14:49:15 UTC schrieb Abhitosh Patil <
> > >>>>>> [email protected]>:
> > >>>>>>> Initially I was using Java 17 but later switched to java 8
> > >>>>>>> Currently getting error like "javax.script.scriptExecution:
> > >>>>>>> reference
> > >>>>>>> error: pm is not defined.
> > >>>>>>>
> > >>>>>>> Rhino or Graalvm are not available on our company download port=
al.
> > >>>>>>>
> > >>>>>>> I don't know anything about Groovy.
> > >>>>>>>
> > >>>>>>> On Wed, 2 Apr, 2025, 8:10 pm Dmitri T,<[email protected]> wrote:
> > >>>>>>>
> > >>>>>>>> 1.
> > >>>>>>>> You answered your own question: JavaScript code cannot be run =
in
> > >>>>>> JMeter.
> > >>>>>>>>
> > >>>>>>>> 1.
> > >>>>>>>> Also, if you're on Java 15+ you won't be able able to choose
> > >>>>>> JavaScript
> > >>>>>>>> in JSR223 test elements because it has been removed from JDK<
> > >>>>>>>> https://openjdk.org/jeps/372>
> > >>>>>>>> 2.
> > >>>>>>>> There could be inconsistencies between Postman JavaScript API =
and
> > >>>>>> JMeter
> > >>>>>>>> so you won't have access to pm. namespace in JMeter
> > >>>>>>>>
> > >>>>>>>>
> > >>>>>>>> So you either need to restore JavaScript engine by adding Mozi=
lla
> > >>>>>> Rhino<
> > >>>>>>>> https://github.com/mozilla/rhino> or switching to GraalVM<
> > >>>>>>>> https://www.graalvm.org/> or re-write your code in Groovy. The
> > >>> latter
> > >>>>>>>> option is better from performance perspective as Groovy script=
s
> > >>>>>>>> can
> > >>> be
> > >>>>>>>> compiled and cached and it matters especially for "heavy" task=
s
> > >>>>>>>> like
> > >>>>>>>> cryptography. More information: Apache Groovy: What Is Groovy =
Used
> > >>>>>> For?<
> > >>>>>>>> https://www.blazemeter.com/blog/apache-groovy>
> > >>>>>>>>
> > >>>>>>>>
> > >>>>>>>>
> > >>>>>>>> ________________________________
> > >>>>>>>> From: Abhitosh Patil<[email protected]>
> > >>>>>>>> Sent: Wednesday, April 2, 2025 5:14 PM
> > >>>>>>>> To: JMeter Users List<[email protected]>
> > >>>>>>>> Subject: Need to execute javascript using JMeter
> > >>>>>>>>
> > >>>>>>>> We have a requirement to generate unique authentication
> > >>>>>>>> signature to
> > >>>>>> pass
> > >>>>>>>> as a header in API request. We have a pre-request javascript f=
or
> > >>> same
> > >>>>>> in
> > >>>>>>>> postman tool.
> > >>>>>>>>
> > >>>>>>>> Same javascript we tried in JMeter using jsr223 and beanshell
> > >>>>>> elements. But
> > >>>>>>>> it's not working. Any solution for this?
> > >>>>>>>>
> > >
> >=20
> >=20
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [email protected]
> > For additional commands, e-mail: [email protected]
> >