forked from microsoft/sample-app-aoai-chatGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update scripts to use requirements-dev.txt
- Loading branch information
Showing
13 changed files
with
164 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
. ./loadenv.ps1 | ||
|
||
$pythonCmd = Get-Command python -ErrorAction SilentlyContinue | ||
if (-not $pythonCmd) { | ||
# fallback to python3 if python not found | ||
$pythonCmd = Get-Command python3 -ErrorAction SilentlyContinue | ||
} | ||
|
||
Write-Host 'Running auth_update.py' | ||
Start-Process -FilePath ($pythonCmd).Source -ArgumentList "./scripts/prepdocs.py --appid $env:AUTH_APP_ID --uri $env:BACKEND_URI" -Wait -NoNewWindow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,89 @@ | ||
import argparse | ||
import subprocess | ||
|
||
from azure.identity import AzureDeveloperCliCredential | ||
import urllib3 | ||
|
||
credential = AzureDeveloperCliCredential() | ||
|
||
resp = urllib3.request( | ||
"POST", | ||
"https://graph.microsoft.com/v1.0/applications", | ||
headers={ | ||
"Authorization": "Bearer " | ||
+ credential.get_token("https://graph.microsoft.com/.default").token, | ||
}, | ||
json={ | ||
"displayName": "WebApp", | ||
"signInAudience": "AzureADandPersonalMicrosoftAccount", | ||
"web": {"redirectUris": ["http://localhost:5000/.auth/login/aad/callback"], | ||
"implicitGrantSettings": { | ||
"enableIdTokenIssuance": True | ||
}}, | ||
}, | ||
timeout=urllib3.Timeout(connect=10, read=10), | ||
) | ||
|
||
app_id = resp.json()["id"] | ||
client_id = resp.json()["appId"] | ||
|
||
# Add a client secret to the application | ||
# using https://graph.microsoft.com/v1.0/applications/{id}/addPassword | ||
# where {id} is the application ID returned from the previous step | ||
resp = urllib3.request( | ||
"POST", | ||
f"https://graph.microsoft.com/v1.0/applications/{app_id}/addPassword", | ||
headers={ | ||
def get_auth_headers(credential): | ||
return { | ||
"Authorization": "Bearer " | ||
+ credential.get_token("https://graph.microsoft.com/.default").token, | ||
}, | ||
json={"passwordCredential": {"displayName": "WebAppSecret"}}, | ||
timeout=urllib3.Timeout(connect=10, read=10), | ||
) | ||
client_secret = resp.json()["secretText"] | ||
|
||
subprocess.run(f"azd env set AUTH_APP_ID {app_id}", shell=True) | ||
subprocess.run(f"azd env set AUTH_CLIENT_ID {client_id}", shell=True) | ||
subprocess.run(f"azd env set AUTH_CLIENT_SECRET {client_secret}", shell=True) | ||
+ credential.get_token("https://graph.microsoft.com/.default").token | ||
} | ||
|
||
def check_for_application(credential, app_id): | ||
resp = urllib3.request( | ||
"GET", | ||
f"https://graph.microsoft.com/v1.0/applications/{app_id}", | ||
headers=get_auth_headers(credential)) | ||
if resp.status != 200: | ||
print("Application not found") | ||
return False | ||
return True | ||
|
||
|
||
def create_application(credential): | ||
resp = urllib3.request( | ||
"POST", | ||
"https://graph.microsoft.com/v1.0/applications", | ||
headers=get_auth_headers(credential), | ||
json={ | ||
"displayName": "WebApp", | ||
"signInAudience": "AzureADandPersonalMicrosoftAccount", | ||
"web": {"redirectUris": ["http://localhost:5000/.auth/login/aad/callback"], | ||
"implicitGrantSettings": { | ||
"enableIdTokenIssuance": True | ||
}}, | ||
}, | ||
timeout=urllib3.Timeout(connect=10, read=10), | ||
) | ||
|
||
app_id = resp.json()["id"] | ||
client_id = resp.json()["appId"] | ||
|
||
return app_id, client_id | ||
|
||
def add_client_secret(credential, app_id): | ||
resp = urllib3.request( | ||
"POST", | ||
f"https://graph.microsoft.com/v1.0/applications/{app_id}/addPassword", | ||
headers=get_auth_headers(credential), | ||
json={"passwordCredential": {"displayName": "WebAppSecret"}}, | ||
timeout=urllib3.Timeout(connect=10, read=10), | ||
) | ||
client_secret = resp.json()["secretText"] | ||
return client_secret | ||
|
||
def update_azd_env(name, val): | ||
subprocess.run(f"azd env set {name} {val}", shell=True) | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description="Create an App Registration and client secret (if not already created)", | ||
epilog="Example: auth_update.py", | ||
) | ||
parser.add_argument( | ||
"--appid", | ||
required=False, | ||
help="Optional. ID of registered application. If provided, this script just makes sure it exists.", | ||
) | ||
args = parser.parse_args() | ||
|
||
credential = AzureDeveloperCliCredential() | ||
|
||
if args.appid: | ||
print(f"Checking if application {args.appid} exists") | ||
if check_for_application(credential, args.appid): | ||
print("Application already exists, not creating new one.") | ||
exit(0) | ||
|
||
print("Creating application registration") | ||
app_id, client_id = create_application(credential) | ||
|
||
print(f"Adding client secret to {app_id}") | ||
client_secret = add_client_secret(credential, app_id) | ||
|
||
print("Updating azd env with AUTH_APP_ID, AUTH_CLIENT_ID, AUTH_CLIENT_SECRET") | ||
update_azd_env("AUTH_APP_ID", app_id) | ||
update_azd_env("AUTH_CLIENT_ID", client_id) | ||
update_azd_env("AUTH_CLIENT_SECRET", client_secret) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,6 @@ | ||
#!/bin/sh | ||
|
||
echo "" | ||
echo "Loading azd .env file from current environment" | ||
echo "" | ||
|
||
while IFS='=' read -r key value; do | ||
value=$(echo "$value" | sed 's/^"//' | sed 's/"$//') | ||
export "$key=$value" | ||
done <<EOF | ||
$(azd env get-values) | ||
EOF | ||
|
||
echo 'Creating python virtual environment "scripts/.venv"' | ||
python3 -m venv scripts/.venv | ||
|
||
echo 'Installing dependencies from "requirements.txt" into virtual environment' | ||
./scripts/.venv/bin/python -m pip install -r scripts/requirements.txt | ||
. ./scripts/loadenv.sh | ||
|
||
echo 'Running "auth_init.py"' | ||
./scripts/.venv/bin/python ./scripts/auth_init.py | ||
python3 ./scripts/auth_init.py --appid "$AUTH_APP_ID" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/sh | ||
|
||
. ./scripts/loadenv.sh | ||
|
||
echo 'Running "auth_update.py"' | ||
python3 ./scripts/auth_update.py --appid "$AUTH_APP_ID" --uri "$BACKEND_URI" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Write-Host "Loading azd .env file from current environment" | ||
|
||
$output = azd env get-values | ||
|
||
foreach ($line in $output) { | ||
if (!$line.Contains('=')) { | ||
continue | ||
} | ||
|
||
$name, $value = $line.Split("=") | ||
$value = $value -replace '^\"|\"$' | ||
[Environment]::SetEnvironmentVariable($name, $value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
echo "Loading azd .env file from current environment" | ||
|
||
while IFS='=' read -r key value; do | ||
value=$(echo "$value" | sed 's/^"//' | sed 's/"$//') | ||
export "$key=$value" | ||
done <<EOF | ||
$(azd env get-values) | ||
EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,6 @@ | ||
#!/bin/sh | ||
|
||
echo "" | ||
echo "Loading azd .env file from current environment" | ||
echo "" | ||
. ./scripts/loadenv.sh | ||
|
||
while IFS='=' read -r key value; do | ||
value=$(echo "$value" | sed 's/^"//' | sed 's/"$//') | ||
export "$key=$value" | ||
done <<EOF | ||
$(azd env get-values) | ||
EOF | ||
|
||
echo 'Creating python virtual environment "scripts/.venv"' | ||
python3 -m venv scripts/.venv | ||
|
||
echo 'Installing dependencies from "requirements.txt" into virtual environment' | ||
./scripts/.venv/bin/python -m pip install -r scripts/requirements.txt | ||
|
||
echo 'Running "auth_update.py"' | ||
./scripts/.venv/bin/python ./scripts/auth_update.py --appid "$AUTH_APP_ID" --uri "$BACKEND_URI" | ||
echo 'Running "prepdocs.py"' | ||
./scripts/.venv/bin/python ./scripts/prepdocs.py --searchservice "$AZURE_SEARCH_SERVICE" --index "$AZURE_SEARCH_INDEX" --formrecognizerservice "$AZURE_FORMRECOGNIZER_SERVICE" --tenantid "$AZURE_TENANT_ID" | ||
python3 ./scripts/prepdocs.py --searchservice "$AZURE_SEARCH_SERVICE" --index "$AZURE_SEARCH_INDEX" --formrecognizerservice "$AZURE_FORMRECOGNIZER_SERVICE" --tenantid "$AZURE_TENANT_ID" |