|
| 1 | +--- |
| 2 | +title: Load test secured endpoints |
| 3 | +description: Learn how to load test secured endpoints with Azure Load Testing. Use shared secrets, credentials, or client certificates for load testing applications that require authentication. |
| 4 | +author: ntrogh |
| 5 | +ms.author: nicktrog |
| 6 | +services: load-testing |
| 7 | +ms.service: load-testing |
| 8 | +ms.topic: how-to |
| 9 | +ms.date: 09/28/2022 |
| 10 | +ms.custom: template-how-to |
| 11 | +--- |
| 12 | + |
| 13 | +# Load test secured endpoints with Azure Load Testing Preview |
| 14 | + |
| 15 | +In this article, you learn how to load test applications with Azure Load Testing Preview that require authentication. Azure Load Testing enables you to [authenticate with endpoints by using shared secrets or credentials](#authenticate-with-a-shared-secret-or-credentials), or to [authenticate with client certificates](#authenticate-with-client-certificates). |
| 16 | + |
| 17 | +> [!IMPORTANT] |
| 18 | +> Azure Load Testing is currently in preview. For legal terms that apply to Azure features that are in beta, in preview, or otherwise not yet released into general availability, see the [Supplemental Terms of Use for Microsoft Azure Previews](https://azure.microsoft.com/support/legal/preview-supplemental-terms/). |
| 19 | +
|
| 20 | +## Prerequisites |
| 21 | + |
| 22 | +## Authenticate with a shared secret or credentials |
| 23 | + |
| 24 | +In this scenario, the application endpoint requires that you use a shared secret, such as an access token, an API key, or user credentials to authenticate. In the JMeter script, you have to provide this security information with each application request. For example, to load test a web endpoint that uses OAuth 2.0, you add an `Authorization` header, which contains the access token, to the HTTP request. |
| 25 | + |
| 26 | +To avoid storing, and disclosing, security information in the JMeter script, Azure Load Testing enables you to securely store secrets in Azure Key Vault or in the CI/CD secrets store. By using a custom JMeter function `GetSecret`, you can retrieve the secret value and pass it to the application endpoint. |
| 27 | + |
| 28 | +The following diagram shows how to use shared secrets or credentials to authenticate with an application endpoint in your load test. |
| 29 | + |
| 30 | +:::image type="content" source="./media/how-to-test-secured-endpoints/load-test-authentication-with-shared-secret.png" alt-text="Diagram that shows how to use shared-secret authentication with Azure Load Testing."::: |
| 31 | + |
| 32 | +1. Add the security information in a secrets store in either of two ways: |
| 33 | + |
| 34 | + * Add the secret information in Azure Key Vault. Follow the steps in [Parameterize load tests with secrets](./how-to-parameterize-load-tests.md) to store a secret and authorize your load testing resource to read its value. |
| 35 | + |
| 36 | + * Add the secret information as a secret in CI/CD ([GitHub Actions secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets) or [Azure Pipelines secret variables](/azure/devops/pipelines/process/set-secret-variables)). |
| 37 | + |
| 38 | +1. Add the secret to the load test configuration: |
| 39 | + |
| 40 | + # [Azure portal](#tab/portal) |
| 41 | + |
| 42 | + To add a secret to your load test in the Azure portal: |
| 43 | + |
| 44 | + 1. Navigate to your load testing resource in the Azure portal. If you don't have a load test yet, [create a new load test using a JMeter script](./how-to-create-and-run-load-test-with-jmeter-script.md). |
| 45 | + 1. On the left pane, select **Tests** to view the list of load tests. |
| 46 | + 1. Select your test from the list, and then select **Edit**, to edit the load test configuration. |
| 47 | + |
| 48 | + :::image type="content" source="./media/how-to-test-secured-endpoints/edit-load-test.png" alt-text="Screenshot that shows how to edit a load test in the Azure portal."::: |
| 49 | + |
| 50 | + 1. On the **Parameters** tab, enter the details of the secret. |
| 51 | + |
| 52 | + | Field | Value | |
| 53 | + | ----- | ----- | |
| 54 | + | **Name** | Name of the secret. You'll provide this name to the `GetSecret` function to retrieve the secret value in the JMeter script. | |
| 55 | + | **Value** | Matches the Azure Key Vault **Secret identifier**. | |
| 56 | + |
| 57 | + :::image type="content" source="media/how-to-test-secured-endpoints/load-test-secrets.png" alt-text="Screenshot that shows how to add secrets to a load test in the Azure portal."::: |
| 58 | + |
| 59 | + 1. Select **Apply**, to save the load test configuration changes. |
| 60 | + |
| 61 | + # [GitHub Actions](#tab/github) |
| 62 | + |
| 63 | + To add a secret to your load test in GitHub Actions, update the GitHub Actions workflow YAML file. In the workflow, add a `secrets` parameter to the `azure/load-testing` action. |
| 64 | + |
| 65 | + | Field | Value | |
| 66 | + | ----- | ----- | |
| 67 | + | **name** | Name of the secret. You'll provide this name to the `GetSecret` function to retrieve the secret value in the JMeter script. | |
| 68 | + | **value** | References the GitHub Actions secret name. | |
| 69 | + |
| 70 | + The following code snippet gives an example of how to configure a load test secret in GitHub Actions. |
| 71 | + |
| 72 | + ```yaml |
| 73 | + - name: 'Azure Load Testing' |
| 74 | + uses: azure/load-testing@v1 |
| 75 | + with: |
| 76 | + loadtestConfigFile: 'SampleApp.yaml' |
| 77 | + loadtestResource: 'MyTest' |
| 78 | + resourceGroup: 'loadtests-rg' |
| 79 | + secrets: | |
| 80 | + [ |
| 81 | + { |
| 82 | + "name": "appToken", |
| 83 | + "value": "${{ secrets.APP_TOKEN }}" |
| 84 | + } |
| 85 | + ] |
| 86 | + ``` |
| 87 | + |
| 88 | + # [Azure Pipelines](#tab/pipelines) |
| 89 | + |
| 90 | + To add a secret to your load test in Azure Pipelines, update the Azure Pipelines definition file. In the pipeline, add a `secrets` parameter to the `AzureLoadTest` task. |
| 91 | + |
| 92 | + | Field | Value | |
| 93 | + | ----- | ----- | |
| 94 | + | **name** | Name of the secret. You'll provide this name to the `GetSecret` function to retrieve the secret value in the JMeter script. | |
| 95 | + | **value** | References the Azure Pipelines secret variable name. | |
| 96 | + |
| 97 | + The following code snippet gives an example of how to configure a load test secret in Azure Pipelines. |
| 98 | + |
| 99 | + ```yaml |
| 100 | + - task: AzureLoadTest@1 |
| 101 | + inputs: |
| 102 | + azureSubscription: 'MyAzureLoadTestingRG' |
| 103 | + loadTestConfigFile: 'SampleApp.yaml' |
| 104 | + loadTestResource: 'MyTest' |
| 105 | + resourceGroup: 'loadtests-rg' |
| 106 | + secrets: | |
| 107 | + [ |
| 108 | + { |
| 109 | + "name": "appToken", |
| 110 | + "value": "$(appToken)" |
| 111 | + } |
| 112 | + ] |
| 113 | + ``` |
| 114 | + --- |
| 115 | + |
| 116 | +1. Update the JMeter script to retrieve the secret value: |
| 117 | + |
| 118 | + 1. Create a user-defined variable that retrieves the secret value with the `GetSecret` custom function: |
| 119 | + <!-- Add screenshot --> |
| 120 | + |
| 121 | + 1. Update the JMeter sampler component to pass the secret in the request. For example, to provide an OAuth2 access token, you configure the `Authentication` HTTP header: |
| 122 | + <!-- Add screenshot --> |
| 123 | + |
| 124 | +When you now run your load test, the JMeter script can retrieve the secret information from the secrets store and authenticate with the application endpoint. |
| 125 | + |
| 126 | +## Authenticate with client certificates |
| 127 | + |
| 128 | +In this scenario, the application endpoint requires that you use a client certificate to authenticate. Azure Load Testing supports Public Key Certificate Standard #12 (PKCS12) type of certificates. You can use only one client certificate in a load test. |
| 129 | + |
| 130 | +To avoid storing, and disclosing, the client certificate alongside the JMeter script, Azure Load Testing uses Azure Key Vault to store the certificate. When you run the load test, Azure Load Testing passes the certificate to JMeter, which uses it to authenticate with the application endpoint. You don't have to update the JMeter script to use the client certificate. |
| 131 | + |
| 132 | +The following diagram shows how to use a client certificate to authenticate with an application endpoint in your load test. |
| 133 | + |
| 134 | +:::image type="content" source="./media/how-to-test-secured-endpoints/load-test-authentication-with-client-certificate.png" alt-text="Diagram that shows how to use client-certificate authentication with Azure Load Testing."::: |
| 135 | + |
| 136 | +1. Follow the steps in [Import a certificate](/azure/key-vault/certificates/tutorial-import-certificate) to store your certificate in Azure Key Vault. |
| 137 | + |
| 138 | + > [!IMPORTANT] |
| 139 | + > Azure Load Testing only supports PKCS12 certificates. Upload the client certificate in PFX file format. |
| 140 | + |
| 141 | +1. Verify that your load testing resource has permissions to retrieve the certificate from your key vault. |
| 142 | + |
| 143 | + Azure Load Testing retrieves the certificate as a secret to ensure that the private key for the certificate is available. [Assign the Get secret permission to your load testing resource](./how-to-use-a-managed-identity.md#grant-access-to-your-azure-key-vault) in Azure Key Vault. |
| 144 | + |
| 145 | +1. Add the certificate to the load test configuration: |
| 146 | + |
| 147 | + # [Azure portal](#tab/portal) |
| 148 | + |
| 149 | + To add a client certificate to your load test in the Azure portal: |
| 150 | + |
| 151 | + 1. Navigate to your load testing resource in the Azure portal. If you don't have a load test yet, [create a new load test using a JMeter script](./how-to-create-and-run-load-test-with-jmeter-script.md). |
| 152 | + 1. On the left pane, select **Tests** to view the list of load tests. |
| 153 | + 1. Select your test from the list, and then select **Edit**, to edit the load test configuration. |
| 154 | + |
| 155 | + :::image type="content" source="./media/how-to-test-secured-endpoints/edit-load-test.png" alt-text="Screenshot that shows how to edit a load test in the Azure portal."::: |
| 156 | + |
| 157 | + 1. On the **Parameters** tab, enter the details of the certificate. |
| 158 | + |
| 159 | + | Field | Value | |
| 160 | + | ----- | ----- | |
| 161 | + | **Name** | Name of the certificate. | |
| 162 | + | **Value** | Matches the Azure Key Vault **Secret identifier** of the certificate. | |
| 163 | + |
| 164 | + :::image type="content" source="media/how-to-test-secured-endpoints/load-test-certificates.png" alt-text="Screenshot that shows how to add a certificate to a load test in the Azure portal."::: |
| 165 | + |
| 166 | + 1. Select **Apply**, to save the load test configuration changes. |
| 167 | + |
| 168 | + # [GitHub Actions](#tab/github) |
| 169 | + |
| 170 | + To add a client certificate for your load test, update the `certificates` property in the [load test YAML configuration file](./reference-test-config-yaml.md). |
| 171 | + |
| 172 | + | Field | Value | |
| 173 | + | ----- | ----- | |
| 174 | + | **name** | Name of the client certificate. | |
| 175 | + | **value** | Matches the Azure Key Vault **Secret identifier** of the certificate. | |
| 176 | + |
| 177 | + ```yml |
| 178 | + certificates: |
| 179 | + - name: <my-certificate-name> |
| 180 | + value: <my-keyvault-secret-ID> |
| 181 | + ``` |
| 182 | + |
| 183 | + # [Azure Pipelines](#tab/pipelines) |
| 184 | + |
| 185 | + To add a client certificate for your load test, update the `certificates` property in the [load test YAML configuration file](./reference-test-config-yaml.md). |
| 186 | + |
| 187 | + | Field | Value | |
| 188 | + | ----- | ----- | |
| 189 | + | **name** | Name of the client certificate. | |
| 190 | + | **value** | Matches the Azure Key Vault **Secret identifier** of the certificate. | |
| 191 | + |
| 192 | + ```yml |
| 193 | + certificates: |
| 194 | + - name: <my-certificate-name> |
| 195 | + value: <my-keyvault-secret-ID> |
| 196 | + ``` |
| 197 | + --- |
| 198 | + |
| 199 | +When you now run your load test, Azure Load Testing retrieves the client certificate from Azure Key Vault, and injects it in the JMeter web requests. |
| 200 | + |
| 201 | +## Next steps |
| 202 | + |
| 203 | +* Learn more about [how to parameterize a load test](./how-to-parameterize-load-tests.md). |
0 commit comments