Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Locust support for authentication #1935

Open
DuboisABB opened this issue Aug 25, 2024 · 2 comments
Open

Add Locust support for authentication #1935

DuboisABB opened this issue Aug 25, 2024 · 2 comments
Labels
auth open issue A validated issue that should be tackled. Comment if you'd like it assigned to you.

Comments

@DuboisABB
Copy link

This issue is for a: (mark with an x)

- [ ] bug report -> please search issues before submitting
- [X] feature request
- [ ] documentation issue or request
- [ ] regression (a behavior that used to work and stopped in a new release)

Please modify locustfile.py to handle a web app where authentication is required (i.e. AZURE_USE_AUTHENTICATION=true and AZURE_ENABLE_UNAUTHENTICATED_ACCESS=false).

I attempted to use GitHub Copilot to add this feature but it doesn't work and I'm a bit over my head.

@pamelafox pamelafox added open issue A validated issue that should be tackled. Comment if you'd like it assigned to you. auth labels Aug 28, 2024
@pamelafox
Copy link
Collaborator

I haven't got this working yet, but this is what I've attempted so far - sharing in case it helps. The token fetching isn't working for me yet unfortunately.

import json
import logging
import os
import random
import subprocess
import time

from azure.identity import AzureDeveloperCliCredential
from dotenv import load_dotenv
from locust import HttpUser, between, task


def load_azd_env():
    """Get path to current azd env file and load file using python-dotenv"""
    result = subprocess.run("azd env list -o json", shell=True, capture_output=True, text=True)
    if result.returncode != 0:
        raise Exception("Error loading azd env")
    env_json = json.loads(result.stdout)
    env_file_path = None
    for entry in env_json:
        if entry["IsDefault"]:
            env_file_path = entry["DotEnvPath"]
    if not env_file_path:
        raise Exception("No default azd env file found")
    logging.info(f"Loading azd env from {env_file_path}")
    load_dotenv(env_file_path, override=True)

class ChatUser(HttpUser):
    wait_time = between(5, 20)

    @task
    def ask_question(self):
        load_azd_env()
        token = AzureDeveloperCliCredential().get_token(f"api://{os.environ['AZURE_SERVER_APP_ID']}/access_as_user", tenant_id=os.getenv('AZURE_AUTH_TENANT_ID', os.getenv('AZURE_TENANT_ID')))

        self.client.get("/",
                        headers={"Authorization": f"Bearer {token.token}"})

@mattgotteiner
Copy link
Collaborator

Thanks for tagging me, I'll try to take a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth open issue A validated issue that should be tackled. Comment if you'd like it assigned to you.
Projects
None yet
Development

No branches or pull requests

3 participants