Skip to content

Commit 0c9a203

Browse files
committed
Updated service principle script and federated identity
1 parent 443def3 commit 0c9a203

File tree

4 files changed

+93
-22
lines changed

4 files changed

+93
-22
lines changed

.github/workflows/deploy-azure.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
client-id: ${{ secrets.AZURE_CLIENT_ID }}
3232
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
3333
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
34-
auth-type: service_principal
34+
auth-type: oidc
3535

3636
- name: Setup Terraform
3737
uses: hashicorp/setup-terraform@v2

scripts/setup_azure.sh

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/zsh
2+
3+
# Check if AZURE_CLIENT_ID was provided as a parameter
4+
if [ -z "$1" ]; then
5+
echo "Error: AZURE_CLIENT_ID is required as a parameter."
6+
echo "Usage: $0 <AZURE_CLIENT_ID>"
7+
exit 1
8+
fi
9+
10+
AZURE_CLIENT_ID=$1
11+
CREDENTIAL_NAME="GitHubActionsFederatedCredential"
12+
13+
# Check if the federated credential already exists
14+
echo "Checking if federated credential '$CREDENTIAL_NAME' already exists..."
15+
EXISTING_CREDENTIAL=$(az ad app federated-credential list --id $AZURE_CLIENT_ID --query "[?name=='$CREDENTIAL_NAME']" -o json)
16+
17+
if [ "$(echo $EXISTING_CREDENTIAL | jq 'length')" -gt "0" ]; then
18+
echo "Federated credential '$CREDENTIAL_NAME' already exists. Skipping creation."
19+
else
20+
echo "Creating federated credential '$CREDENTIAL_NAME'..."
21+
az ad app federated-credential create --id $AZURE_CLIENT_ID \
22+
--parameters '{
23+
"name": "GitHubActionsFederatedCredential",
24+
"issuer": "https://token.actions.githubusercontent.com",
25+
"subject": "repo:nullchimp/ai-agent:ref:refs/heads/RAG",
26+
"audiences": [
27+
"api://AzureADTokenExchange"
28+
]
29+
}'
30+
31+
if [ $? -eq 0 ]; then
32+
echo "Federated credential created successfully."
33+
else
34+
echo "Failed to create federated credential."
35+
exit 1
36+
fi
37+
fi

scripts/setup_service_principle.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/zsh
2+
3+
# Set up Azure credentials for GitHub Actions
4+
# This script creates a service principal with Contributor access to the GitHub resource group
5+
# If the service principal already exists, it will retrieve it instead of creating a new one
6+
7+
# Login to Azure
8+
echo "Logging in to Azure..."
9+
az login
10+
11+
# Service principal name
12+
SP_NAME="ai-agent-github"
13+
SUBSCRIPTION_ID=$(az account show --query id -o tsv)
14+
RESOURCE_GROUP="GitHub"
15+
16+
# Check if service principal already exists
17+
echo "Checking if service principal '$SP_NAME' already exists..."
18+
SP_ID=$(az ad sp list --display-name "$SP_NAME" --query "[0].appId" -o tsv)
19+
20+
if [ -n "$SP_ID" ]; then
21+
echo "Service principal '$SP_NAME' already exists."
22+
23+
# Get existing service principal information without resetting credentials
24+
# Create JSON output in the format expected by GitHub Actions
25+
CLIENT_ID=$SP_ID
26+
TENANT_ID=$(az account show --query tenantId -o tsv)
27+
SUBSCRIPTION_ID=$(az account show --query id -o tsv)
28+
29+
echo "Using existing service principal."
30+
echo "Important: To use this service principal, ensure your GitHub repository has"
31+
echo "the correct credentials already configured as AZURE_CREDENTIALS secret."
32+
echo "If you need to reset credentials, you can do so manually with:"
33+
echo "az ad sp credential reset --id $SP_ID --sdk-auth"
34+
35+
# Display service principal information (without secret)
36+
echo "Service Principal Information:"
37+
echo "- Client ID: $CLIENT_ID"
38+
echo "- Tenant ID: $TENANT_ID"
39+
echo "- Subscription ID: $SUBSCRIPTION_ID"
40+
41+
# Set SERVICE_PRINCIPAL to empty to avoid displaying sensitive info
42+
SERVICE_PRINCIPAL='{}'
43+
else
44+
# Create service principal
45+
echo "Creating service principal for GitHub Actions..."
46+
SERVICE_PRINCIPAL=$(az ad sp create-for-rbac \
47+
--name "$SP_NAME" \
48+
--role contributor \
49+
--scopes /subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP \
50+
--sdk-auth)
51+
52+
echo "Service principal created. Add the following secret to your GitHub repository as AZURE_CREDENTIALS:"
53+
fi
54+
55+
echo $SERVICE_PRINCIPAL

0 commit comments

Comments
 (0)