We are using k6 in testing, I want to import the certificate ( perm file & key ) in k6 , can you pls let me know how we can use the certificate in k6 run & postman-k6 command.
Not sure if I got the question fully But let me try to answer for the headline part. So if the question is how to set up the TLS Auth you can use the option Options reference.
1st i am doing postman to K6 using below command and have manged to get certificate in same path were where below file is present $POSTMAN_ENV-$API_NAME.postman_environment.json . Later it execute below
So do you want me to add certificate details in $POSTMAN_ENV-$API_NAME.postman_environment.json file ?
So above command converts into .js and then it execute below
k6 run --summary-export=load-performance.json $file --config=<PATH>/config.json
( for the performance test we get result as all failed with response 200 )
Okay, sorry that I didn’t mention that explicitly. If you want to define the certificate in the config.json you should provide the value of the certificate/keys instead of the expressions open('./mtls.pem') and open(‘./mtls.key’).
Thanks for the revert .
We dont need to pass or disclose cert & key in config.json.
Is their any way we can refer mtls.perm and mtls.key in config.json file with performance parameter?
Hi ,
So $file is nothing but it.js file present below. This file is generate on the fly while executing below command postman-to-k6 -e <PATH>/$POSTMAN_ENV-$API_NAME.postman_environment.json $file -o <PATH>/$filename-k6-script.js ;
My certificate details are also on the same location. I am not sure how this certificate can be mapped to <API_NAME>.postman_collection.json-k6-script.js i.e $file.
Is their any way were we can export the certificate in below command while executing?
k6 run --summary-export=load-performance.json $file --config=src/test/performance/config.json
I understand that in your case, the file is generated on the fly, but as I said in the k6, there are only two ways two configure this option, and it’s either value stored in the config.json or using options.
As I understood, you’re not planning to keep the load test scripts in the k6, but are planning to use automation, which is totally fine. I believe it’s possible also to automate modifying (sed, jq, whatever) the $file/config.json on the fly by injecting the options with open or values as the additional command in your pipeline in between the postman-to-k6 and k6 run.
As i mentioned it run postman-to-k6 -e /$POSTMAN_ENV-$API_NAME.postman_environment.json $file -o /$filename-k6-script.js ;
Further i have added below to call certificate value
script:
- export MTLS_PEM=“$(PATH”)"
- echo $MTLS_PEM | sed ‘s/\n/\n/g’ > /mtls.pem
- export MTLS_KEY="$(“PATH)”
- echo $MTLS_KEY | sed ‘s/\n/\n/g’ > /mtls.key
- pwd
- ls -la
- |
for file in /*.js ;
do
k6 run --summary-export=load-performance.json $file --config=/config.json;
done
So over here certificate are placed on the fly at same path where by config.json is placed.
If i want to use option as mentioned earlier ( Options reference (k6.io))
then where i can update this value and how do it call this tls option in K6 run?
Can we use this
k6 run --summary-export=load-performance.json --ssl-client-cert mtls.pem --ssl-client-key mtls.key $file --config=/config.json;
For now, there are no flags like --ssl-client-cert and --ssl-client-key available in the k6, however, you can open an issue in our GitHub.
As I said you can inject the options into your script or config in your pipeline, see the ugly version:
# converting to a k6 scripts
postman-to-k6 -e /$POSTMAN_ENV-$API_NAME.postman_environment.json $file -o /$filename-k6-script.js
# defining the environment variables
- export MTLS_PEM=“$(PATH”)"
- export MTLS_KEY="$(“PATH)”
# injecting the options to scripts (you can user there environment variables)
sed -i 's/export let options = {/export let options = { tlsAuth: {domains: ["example.com"], cert: __ENV.MTLS_PEM, key: __ENV.MTLS_KEY}, /g' *.js
# executing scripts
for file in /*.js ;
do
k6 run --summary-export=load-performance.json $file --config=/config.json;
done
" level=error msg=“ReferenceError: prv is not defined\n\tat file:///tmp/build//mulesoft/shared_libs/performance-test/API/src/test/performance/API.postman_collection.json-k6-script.js:72:89(24)\n” hint=“script exception”
But *.js file is in same path
script:
- export MTLS_PEM=“$(vault kv get -tls-skip-verify -field=pem “kv/mulesoft/postman/mtls”)”
- echo $MTLS_PEM | sed ‘s/\n/\n/g’ > src/test/performance/mtls.pem
- export MTLS_KEY=“$(vault kv get -tls-skip-verify -field=key “kv/mulesoft/postman/mtls”)”
- echo $MTLS_KEY | sed ‘s/\n/\n/g’ > src/test/performance/mtls.key
- pwd
- ls -la src/test/performance/
- |
for file in src/test/performance/.js ;
do
sed -i “s/export let options = {/export let options = { tlsAuth: {domains: [“prv-vip.api-n.ms..com”], cert: __ENV.MTLS_PEM, key: __ENV.MTLS_KEY}, /g” src/test/performance/.js ;
k6 run --summary-export=load-performance.json $file --config=src/test/performance/config.json;
done
artifacts:
reports:
load_performance: load-performance.json
paths:
- load-performance.json
allow_failure: true