Skip to content

Commit c6d7694

Browse files
Include certificate handling in Locust testing guide
Added code snippet for handling certificates in Locust tests.
1 parent 4b9198e commit c6d7694

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

articles/app-testing/load-testing/how-to-test-secured-endpoints.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,32 @@ When you run your load test, Azure Load Testing retrieves the client certificate
246246
For Locust-based tests, you can retrieve the certificate and use it in your tests script. The certificate configured in the load test configuration are available in the `ALT_CERTIFICATES_DIR`.
247247

248248
```Python
249+
endpoint = os.getenv("endpoint") or "localhost"
249250
cert_dir = os.getenv("ALT_CERTIFICATES_DIR")
250-
cert_file = open(os.path.join(cert_dir), "cert_name_in_keyvault.pfx")
251+
252+
base_url = "https://" + endpoint
253+
254+
key_path = "client.key.pem"
255+
crt_path = "client.crt.pem"
256+
257+
258+
@events.test_start.add_listener
259+
def on_test_start(environment, **kwargs):
260+
pfx_path = open(os.path.join(cert_dir, "cert_name_in_keyvault.pfx"))
261+
262+
# Generate the private key
263+
subprocess.check_output(
264+
f'openssl pkcs12 -in "{pfx_path}" -out "{key_path}" -nocerts -nodes -passin pass:',
265+
shell=True,
266+
stderr=subprocess.STDOUT,
267+
)
268+
269+
# Generate the public certificate
270+
subprocess.check_output(
271+
f'openssl pkcs12 -in "{pfx_path}" -out "{crt_path}" -clcerts -nokeys -passin pass:',
272+
shell=True,
273+
stderr=subprocess.STDOUT,
274+
)
251275
```
252276

253277
## Authenticate with a managed identity

0 commit comments

Comments
 (0)