When running JMeter load tests, you often need to access sensitive information like API keys, passwords, or connection strings. Azure Key Vault is the perfect solution for storing these secrets, but connecting JMeter to Key Vault requires proper authentication, especially when you are intending to run it from self-hosted agents.
I recently had build and run load tests, and my initial intent was to leverage Azure Load Testing service. Azure Load Testing service provides a convenient way to retrieve secrets from Key Vault using a managed identity through the __GetSecret
JMeter function. However, there were a couple of reasons why we ended up running JMeter from Azure Pipelines:
SimpleDataWriter
result collector under specific path and configuration, but Azure Load Testing was not uploading the results from that collector to the storage accountAt the same time, we already had the test plan developed and tested, and we didn’t want to have to make changes to it. It would allow us to use the same test plan for both Azure Load Testing service and Azure Pipelines without any changes to the test plan in the future.
You can read more about Azure Load Testing Service here.
Below are the prerequisites for integrating Azure Key Vault with JMeter tests running in Azure Pipelines:
jmeter-plugins-azure-load-testing-stub
in JMeter’s lib/ext
folder__GetSecret
functionLet’s assume that we have a JMeter test plan that is using the __GetSecret
JMeter function to fetch the secrets from the Key Vault. Name of the secret is api-key
and in our test plan we are using it like this:
${__GetSecret(api-key)}
if we would be running this from Azure Load Testing service, the secret value would be fetched automatically using the managed identity. However, since we are running it from Azure Pipelines and self-hosted agents, we need to handle the secret fetching manually.
To enable JMeter to fetch secrets from Azure Key Vault when running from Azure Pipelines, we need to create a properties file that will configure the authentication and secret retrieval settings. We’ll use the Azure CLI authentication method since it’s already available in our pipeline environment.
Create a file named azure-key-vault.properties
with the following content:
# Configure the secret store type and authentication method
azure_load_testing_stub.get_secret.api-key.store_type=keyvault
azure_load_testing_stub.get_secret.api-key.auth_type=azure_cli
# Key Vault configuration
azure_load_testing_stub.get_secret.api-key.vault_uri=https://your-keyvault.vault.azure.net
azure_load_testing_stub.get_secret.api-key.secret_name=api-key
The properties file configuration follows these key points:
keyvault
as the store type and azure_cli
as the authentication methodsecret_name
matches the name of your secret in Key VaultMake sure to replace api-key
in the property names with the actual secret name you’re using in your JMeter test plan’s __GetSecret
function.
To run the JMeter tests with Azure Key Vault integration, we need to set up our pipeline to handle the Azure CLI authentication and JMeter execution. Here’s an example pipeline configuration:
trigger:
- main
pool:
name: 'your-agent-pool' # Use your self-hosted agent pool name
steps:
- task: AzureCLI@2
inputs:
azureSubscription: 'your-azure-service-connection' # Use your Azure service connection name
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
# Run JMeter test with properties file
& jmeter -n `
-t your-test-plan.jmx `
-q azure-key-vault.properties `
-l results.jtl `
-j jmeter.log
Let’s break down the key components of this pipeline:
AzureCLI@2
task with scriptType: 'pscore'
for PowerShell Core support-q azure-key-vault.properties
parameter loads our Key Vault configurationresults.jtl
and logs to jmeter.log
Make sure your Azure service connection has the necessary permissions to read secrets from your Key Vault. You can configure this in the Azure Portal by adding an access policy for the service principal used by your service connection.
It’s crucial to understand that the JMeter test execution must happen within the AzureCLI@2
task context. If you try to run JMeter in a regular powershell or script task, it will not have access to the Azure CLI credentials and will fail.
Instead of using service principal authentication, you can also use managed identities to authenticate to Azure Key Vault. This approach requires that the the agent has access to the managed identity assigned and that the managed identity has the necessary permissions to access the Key Vault.
Then, in the properties file, you would need to set the auth_type
to managed_identity
and (optionally, according to the documentation) provide the managed identity client ID.
This setup would allow run the JMeter tests without the need for AzureCLI@2
task in the pipeline as the wrapper for providing the Azure CLI credentials. JMeter would be able to fetch the secrets using the managed identity automatically.
When running JMeter tests with Azure Key Vault integration, you might encounter some common issues. The best way to diagnose these problems is by examining the jmeter.log
file. You can publish this file as a pipeline artifact using the following task:
- task: PublishPipelineArtifact@1
inputs:
targetPath: 'jmeter.log'
artifact: 'jmeter-logs'
It will allow you to download the log file from the pipeline run and inspect it for any errors or warnings. I helped me a lot when I was setting up the integration for the first time.
Misconfigured Key Vault URI and/or secret name
Access Permission Issues If you see authentication or authorization errors in the logs, verify that the Azure CLI credentials have access to the Key Vault and can read the secret(s) you are trying to access.
Integrating Azure Key Vault with JMeter tests running in Azure Pipelines provides a secure way to manage sensitive data in your load testing scenarios. By using Azure CLI authentication, we’ve simplified the setup process while maintaining security best practices. It allows us to keep our test plans consistent between Azure Load Testing service and Azure Pipelines without any changes to the test plan.
Happy testing!
Cover photo by DALL-E 3