Skip to content

Commit 62c087e

Browse files
Merge pull request #47 from devopsabcs-engineering/feature/2135-cleanup-stale-oidc-credentials
fix(scripts): remove stale prod-env OIDC creds and add teardown entries AB#2135
2 parents 8330472 + a1bf877 commit 62c087e

1 file changed

Lines changed: 32 additions & 9 deletions

File tree

scripts/setup-oidc.ps1

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ $Audience = 'api://AzureADTokenExchange'
2626

2727
# All repos that need federated credentials (scanner + 5 demo apps)
2828
# Each repo gets a main branch credential; demo apps get deploy-NNN and teardown-NNN environment credentials
29+
# Azure AD limit: 20 federated credentials per app registration (current count: 17)
2930
$FederatedRepos = @(
3031
@{ Repo = $ScannerRepo; CredName = 'github-actions-scanner-main'; Subject = "repo:${RepoOwner}/${ScannerRepo}:ref:refs/heads/main"; Description = "OIDC for $RepoOwner/$ScannerRepo main branch" }
3132
@{ Repo = $ScannerRepo; CredName = 'github-actions-scanner-teardown-env'; Subject = "repo:${RepoOwner}/${ScannerRepo}:environment:teardown"; Description = "OIDC for $RepoOwner/$ScannerRepo teardown environment" }
@@ -46,10 +47,19 @@ $FederatedRepos = @(
4647
@{ Repo = 'a11y-demo-app-005'; CredName = 'github-actions-demo-005-teardown-env'; Subject = "repo:${RepoOwner}/a11y-demo-app-005:environment:teardown-005"; Description = "OIDC for $RepoOwner/a11y-demo-app-005 teardown environment" }
4748
)
4849

50+
# Stale credentials to remove (legacy prod-env entries no longer used by any workflow)
51+
$StaleCreds = @(
52+
'github-actions-demo-001-prod-env'
53+
'github-actions-demo-002-prod-env'
54+
'github-actions-demo-003-prod-env'
55+
'github-actions-demo-004-prod-env'
56+
'github-actions-demo-005-prod-env'
57+
)
58+
4959
Write-Host '=== OIDC Federation Setup ===' -ForegroundColor Cyan
5060

5161
# Step 1: Get or create app registration
52-
Write-Host "`n[1/5] Checking for existing app registration '$AppName'..."
62+
Write-Host "`n[1/6] Checking for existing app registration '$AppName'..."
5363
$existingApp = az ad app list --display-name $AppName --query '[0]' -o json 2>$null | ConvertFrom-Json
5464

5565
if ($existingApp) {
@@ -64,8 +74,21 @@ if ($existingApp) {
6474
Write-Host " Created app: $appId" -ForegroundColor Green
6575
}
6676

67-
# Step 2: Create or verify federated credentials for all repos
68-
Write-Host "`n[2/5] Configuring federated credentials for $($FederatedRepos.Count) entries..."
77+
# Step 2: Remove stale federated credentials (legacy prod-env entries)
78+
Write-Host "`n[2/6] Removing stale federated credentials..."
79+
foreach ($staleName in $StaleCreds) {
80+
$staleCred = az ad app federated-credential list --id $objectId --query "[?name=='$staleName']" -o json 2>$null | ConvertFrom-Json
81+
if ($staleCred -and $staleCred.Count -gt 0) {
82+
Write-Host " Removing stale credential '$staleName'..."
83+
az ad app federated-credential delete --id $objectId --federated-credential-id $staleCred[0].id -o none
84+
Write-Host " Removed" -ForegroundColor Green
85+
} else {
86+
Write-Host " '$staleName' not found, skipping" -ForegroundColor Gray
87+
}
88+
}
89+
90+
# Step 3: Create or verify federated credentials for all repos
91+
Write-Host "`n[3/6] Configuring federated credentials for $($FederatedRepos.Count) entries..."
6992
foreach ($fedRepo in $FederatedRepos) {
7093
$credName = $fedRepo.CredName
7194
$subject = $fedRepo.Subject
@@ -96,8 +119,8 @@ foreach ($fedRepo in $FederatedRepos) {
96119
}
97120
}
98121

99-
# Step 3: Create or get service principal
100-
Write-Host "`n[3/5] Checking for existing service principal..."
122+
# Step 4: Create or get service principal
123+
Write-Host "`n[4/6] Checking for existing service principal..."
101124
$existingSp = az ad sp list --filter "appId eq '$appId'" --query '[0]' -o json 2>$null | ConvertFrom-Json
102125

103126
if ($existingSp) {
@@ -110,8 +133,8 @@ if ($existingSp) {
110133
Write-Host " Created service principal: $spObjectId" -ForegroundColor Green
111134
}
112135

113-
# Step 4: Assign Contributor role on subscription (required for deployments)
114-
Write-Host "`n[4/5] Checking Contributor role assignment..."
136+
# Step 5: Assign Contributor role on subscription (required for deployments)
137+
Write-Host "`n[5/6] Checking Contributor role assignment..."
115138
$subscriptionId = az account show --query 'id' -o tsv
116139
$existingRole = az role assignment list `
117140
--assignee $appId `
@@ -131,10 +154,10 @@ if ($existingRole) {
131154
Write-Host " Contributor role assigned" -ForegroundColor Green
132155
}
133156

134-
# Step 5: Output configuration
157+
# Step 6: Output configuration
135158
$tenantId = az account show --query 'tenantId' -o tsv
136159

137-
Write-Host "`n[5/5] Configuration for GitHub Secrets:" -ForegroundColor Cyan
160+
Write-Host "`n[6/6] Configuration for GitHub Secrets:" -ForegroundColor Cyan
138161
Write-Host "========================================" -ForegroundColor Cyan
139162
Write-Host " AZURE_CLIENT_ID: $appId"
140163
Write-Host " AZURE_TENANT_ID: $tenantId"

0 commit comments

Comments
 (0)