Skip to content

Commit 2de580e

Browse files
committed
switch to gh app auth
1 parent c10bec8 commit 2de580e

File tree

7 files changed

+44
-18
lines changed

7 files changed

+44
-18
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"github_selfhosted_runner_on_container_app_jobs": minor
3+
---
4+
5+
Switch to GitHub App-based authentication replacing PAT-based. This approach is generally more secure and scalable.

infra/modules/github_selfhosted_runner_on_container_app_jobs/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ This module creates a Container App Job to be used as a GitHub self-hosted runne
88

99
- **Container App Job**: Deploys a Container App Job in the specified Azure Container App Environment.
1010
- **Managed Identity**: Provides System Assigned Managed Identity for secure authentication with Azure resources.
11-
- **Key Vault Access**: Grant access to the KeyVault instance with GitHub credentials (PAT token)
11+
- **Key Vault Access**: Grant access to the KeyVault instance with GitHub App credentials (private key, app ID, and installation ID).
1212
- **Auto GitHub Registration**: Automatically scale and register as self-hosted runner in the target repository.
1313

1414
## Usage Example
1515

1616
A usage example can be found in the [examples](https://github.com/pagopa-dx/terraform-azurerm-azure-container-app/tree/main/examples/basic) directory.
17+
1718
<!-- markdownlint-disable -->
1819
<!-- BEGIN_TF_DOCS -->
1920
## Requirements
@@ -42,7 +43,7 @@ No modules.
4243
|------|-------------|------|---------|:--------:|
4344
| <a name="input_container_app_environment"></a> [container\_app\_environment](#input\_container\_app\_environment) | Configuration for the Container App Environment. | <pre>object({<br/> id = string<br/> location = string<br/> replica_timeout_in_seconds = optional(number, 1800)<br/> polling_interval_in_seconds = optional(number, 30)<br/> min_instances = optional(number, 0)<br/> max_instances = optional(number, 30)<br/> use_labels = optional(bool, false)<br/> override_labels = optional(list(string), [])<br/> cpu = optional(number, 1.5)<br/> memory = optional(string, "3Gi")<br/> image = optional(string, "ghcr.io/pagopa/github-self-hosted-runner-azure:latest")<br/> env_vars = optional(map(string), {})<br/> secrets = optional(map(string), {})<br/> })</pre> | n/a | yes |
4445
| <a name="input_environment"></a> [environment](#input\_environment) | Values which are used to generate resource names and location short names. They are all mandatory except for domain, which should not be used only in the case of a resource used by multiple domains. | <pre>object({<br/> prefix = string<br/> env_short = string<br/> location = string<br/> instance_number = string<br/> })</pre> | n/a | yes |
45-
| <a name="input_key_vault"></a> [key\_vault](#input\_key\_vault) | Details of the Key Vault used to store secrets for the Container App Job. | <pre>object({<br/> name = string<br/> resource_group_name = string<br/> use_rbac = optional(bool, false)<br/> secret_name = optional(string, "github-runner-pat")<br/> })</pre> | n/a | yes |
46+
| <a name="input_key_vault"></a> [key\_vault](#input\_key\_vault) | Details of the Key Vault used to store GitHub App credentials. | <pre>object({<br/> name = string<br/> resource_group_name = string<br/> use_rbac = optional(bool, false)<br/> app_key_secret_name = optional(string, "gh-app-engineering")<br/> app_id_secret_name = optional(string, "github-runner-app-id")<br/> installation_id_secret_name = optional(string, "github-runner-installation-id")<br/> })</pre> | n/a | yes |
4647
| <a name="input_repository"></a> [repository](#input\_repository) | Details of the GitHub repository, including the owner and repository name. | <pre>object({<br/> owner = optional(string, "pagopa")<br/> name = string<br/> })</pre> | n/a | yes |
4748
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | The name of the resource group where the Container App Job will be deployed. Defaults to null. | `string` | `null` | no |
4849
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to assign to the resources. | `map(any)` | n/a | yes |

infra/modules/github_selfhosted_runner_on_container_app_jobs/container_app_job.tf

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,45 @@ resource "azurerm_container_app_job" "github_runner" {
2424
name = "github-runner-rule"
2525
custom_rule_type = "github-runner"
2626

27-
# https://keda.sh/docs/2.17/scalers/github-runner/
27+
# https://keda.sh/docs/2.19/scalers/github-runner/
2828
metadata = merge({
29+
githubApiURL = "https://api.github.com"
2930
owner = var.repository.owner
3031
runnerScope = "repo"
3132
repos = var.repository.name
32-
targetWorkflowQueueLength = "1"
33-
github-runner = "https://api.github.com"
3433
enableEtags = "true"
34+
targetWorkflowQueueLength = "1"
35+
applicationIDFromEnv = "GITHUB_APP_ID"
36+
installationIDFromEnv = "GITHUB_APP_INSTALLATION_ID"
3537
}, var.container_app_environment.use_labels ? { labels = local.labels } : {})
3638

3739
authentication {
38-
secret_name = var.key_vault.secret_name
39-
trigger_parameter = "personalAccessToken"
40+
secret_name = var.key_vault.app_key_secret_name
41+
trigger_parameter = "appKey"
4042
}
4143
}
4244
}
4345
}
4446

4547
secret {
46-
key_vault_secret_id = "${local.key_vault_uri}secrets/${var.key_vault.secret_name}" # no versioning
48+
key_vault_secret_id = "${local.key_vault_uri}secrets/${var.key_vault.app_key_secret_name}" # no versioning
49+
50+
identity = "System"
51+
name = var.key_vault.app_key_secret_name
52+
}
53+
54+
secret {
55+
key_vault_secret_id = "${local.key_vault_uri}secrets/${var.key_vault.app_id_secret_name}" # no versioning
56+
57+
identity = "System"
58+
name = var.key_vault.app_id_secret_name
59+
}
60+
61+
secret {
62+
key_vault_secret_id = "${local.key_vault_uri}secrets/${var.key_vault.installation_id_secret_name}" # no versioning
4763

4864
identity = "System"
49-
name = var.key_vault.secret_name
65+
name = var.key_vault.installation_id_secret_name
5066
}
5167

5268
template {

infra/modules/github_selfhosted_runner_on_container_app_jobs/examples/basic/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
| Name | Source | Version |
1313
|------|--------|---------|
14-
| <a name="module_container_app_job_selfhosted_runner"></a> [container\_app\_job\_selfhosted\_runner](#module\_container\_app\_job\_selfhosted\_runner) | pagopa-dx/github-selfhosted-runner-on-container-app-jobs/azurerm | ~> 1.2 |
14+
| <a name="module_container_app_job_selfhosted_runner"></a> [container\_app\_job\_selfhosted\_runner](#module\_container\_app\_job\_selfhosted\_runner) | pagopa-dx/github-selfhosted-runner-on-container-app-jobs/azurerm | ~> 1.3 |
1515

1616
## Resources
1717

infra/modules/github_selfhosted_runner_on_container_app_jobs/examples/basic/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module "container_app_job_selfhosted_runner" {
22
source = "pagopa-dx/github-selfhosted-runner-on-container-app-jobs/azurerm"
3-
version = "~> 1.2"
3+
version = "~> 1.3"
44

55
environment = local.environment
66

infra/modules/github_selfhosted_runner_on_container_app_jobs/locals.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ locals {
2727
})
2828

2929
runner_secrets = merge(var.container_app_environment.secrets, {
30-
GITHUB_PAT = var.key_vault.secret_name
30+
GITHUB_APP_KEY = var.key_vault.app_key_secret_name
31+
GITHUB_APP_ID = var.key_vault.app_id_secret_name
32+
GITHUB_APP_INSTALLATION_ID = var.key_vault.installation_id_secret_name
3133
})
3234

3335
labels = join(",", coalescelist(var.container_app_environment.override_labels, [local.env[var.environment.env_short]]))
3436

35-
key_vault_id = "/subscriptions/${data.azurerm_client_config.current.subscription_id}/resourceGroups/${var.key_vault.resource_group_name}/providers/Microsoft.KeyVault/vaults/${var.key_vault.name}"
37+
key_vault_id = provider::azurerm::normalise_resource_id("/subscriptions/${data.azurerm_client_config.current.subscription_id}/resourceGroups/${var.key_vault.resource_group_name}/providers/Microsoft.KeyVault/vaults/${var.key_vault.name}")
3638
key_vault_uri = "https://${var.key_vault.name}.vault.azure.net/"
3739
}

infra/modules/github_selfhosted_runner_on_container_app_jobs/variables.tf

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ variable "container_app_environment" {
5151

5252
variable "key_vault" {
5353
type = object({
54-
name = string
55-
resource_group_name = string
56-
use_rbac = optional(bool, false)
57-
secret_name = optional(string, "github-runner-pat")
54+
name = string
55+
resource_group_name = string
56+
use_rbac = optional(bool, false)
57+
app_key_secret_name = optional(string, "gh-app-engineering")
58+
app_id_secret_name = optional(string, "github-runner-app-id")
59+
installation_id_secret_name = optional(string, "github-runner-installation-id")
5860
})
5961

60-
description = "Details of the Key Vault used to store secrets for the Container App Job."
62+
description = "Details of the Key Vault used to store GitHub App credentials."
6163
}

0 commit comments

Comments
 (0)