-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add github-ci module for easy actions OIDC login setup
- Loading branch information
Showing
4 changed files
with
116 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
data "azurerm_subscription" "primary" { | ||
} | ||
|
||
resource "azuread_application_registration" "github_oidc" { | ||
for_each = var.repo_app_service_map | ||
|
||
display_name = "github-actions-${replace(each.key, "/", "-")}" | ||
} | ||
|
||
|
||
resource "azuread_service_principal" "github_oidc" { | ||
for_each = azuread_application_registration.github_oidc | ||
client_id = each.value.client_id | ||
} | ||
|
||
resource "azuread_application_federated_identity_credential" "github_oidc" { | ||
for_each = azuread_application_registration.github_oidc | ||
application_id = each.value.id | ||
display_name = "github-actions-${replace(each.key, "/", "-")}-federated-credential" | ||
audiences = ["api://AzureADTokenExchange"] | ||
issuer = "https://token.actions.githubusercontent.com" | ||
subject = "repo:${each.key}:ref:refs/heads/main" | ||
} | ||
|
||
|
||
locals { | ||
flattened_role_assignments = flatten([ | ||
for repo, app_services in var.repo_app_service_map : | ||
[ | ||
for app_service in app_services : | ||
{ | ||
repo = repo | ||
app_service = app_service | ||
} | ||
] | ||
]) | ||
} | ||
resource "azurerm_role_definition" "github_oidc" { | ||
name = "Github Actions deployments" | ||
scope = data.azurerm_subscription.primary.id | ||
permissions { | ||
actions = [ | ||
"Microsoft.Resources/deployments/*" | ||
] | ||
} | ||
} | ||
|
||
resource "azurerm_role_assignment" "github_oidc_role" { | ||
for_each = { | ||
for assignment in local.flattened_role_assignments : | ||
"${assignment.repo}-${replace(assignment.app_service, "/", "-")}" => assignment | ||
} | ||
|
||
scope = each.value.app_service | ||
role_definition_id = azurerm_role_definition.github_oidc.role_definition_resource_id | ||
principal_id = azuread_service_principal.github_oidc[each.value.repo].object_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Output Azure Client IDs for Each Repository | ||
output "azure_client_ids" { | ||
description = "Mapping of GitHub repositories to their Azure AD Application Client IDs." | ||
value = { | ||
for repo, app in azuread_application_registration.github_oidc : | ||
repo => app.client_id | ||
} | ||
} | ||
|
||
# Output Azure Subscription ID | ||
output "azure_subscription_id" { | ||
description = "Azure Subscription ID." | ||
value = data.azurerm_subscription.primary.subscription_id | ||
} | ||
|
||
# Output Azure Tenant ID | ||
output "azure_tenant_id" { | ||
description = "Azure Tenant ID." | ||
value = data.azurerm_subscription.primary.tenant_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
variable "repo_app_service_map" { | ||
description = <<EOT | ||
Mapping of GitHub repositories to a list of Azure App Service resource IDs. | ||
Format: { | ||
\"owner/repo1\" = [ | ||
\"/subscriptions/<sub>/resourceGroups/<rg>/providers/Microsoft.Web/sites/<app_service1>\", | ||
\"/subscriptions/<sub>/resourceGroups/<rg>/providers/Microsoft.Web/sites/<app_service2>\" | ||
], | ||
\"owner/repo2\" = [ | ||
\"/subscriptions/<sub>/resourceGroups/<rg>/providers/Microsoft.Web/sites/<app_service3>\" | ||
] | ||
EOT | ||
type = map(list(string)) | ||
} | ||
|
||
|
||
#variable "azure_tenant_id" { | ||
# description = "The Tenant ID for Azure Active Directory." | ||
# type = string | ||
#} |