Skip to content

Commit

Permalink
feat: [PRDP-41] ci: Introducing .identity folder with terraform files…
Browse files Browse the repository at this point in the history
… for G… (#6)

* [PRDP-41] ci: Introducing .identity folder with terraform files for Github Runner

* fix

---------

Co-authored-by: Alessio Cialini <[email protected]>
Co-authored-by: pasqualespica <[email protected]>
  • Loading branch information
3 people authored Jun 16, 2023
1 parent 78cbc20 commit 7a4d509
Show file tree
Hide file tree
Showing 17 changed files with 520 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ hs_err_pid*
local.settings.json
bin/
obj/
**/.terraform
89 changes: 89 additions & 0 deletions .identity/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions .identity/00_data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
data "azurerm_resource_group" "dashboards" {
name = "dashboards"
}

data "azurerm_kubernetes_cluster" "aks" {
name = local.aks_cluster.name
resource_group_name = local.aks_cluster.resource_group_name
}

data "github_organization_teams" "all" {
root_teams_only = true
summary_only = true
}

data "azurerm_key_vault" "key_vault" {

name = "pagopa-${var.env_short}-kv"
resource_group_name = "pagopa-${var.env_short}-sec-rg"
}

data "azurerm_key_vault_secret" "key_vault_sonar" {

name = "sonar-token"
key_vault_id = data.azurerm_key_vault.key_vault.id
}

data "azurerm_key_vault_secret" "key_vault_bot_token" {

name = "bot-token-github"
key_vault_id = data.azurerm_key_vault.key_vault.id
}

data "azurerm_key_vault_secret" "key_vault_cucumber_token" {

name = "cucumber-token"
key_vault_id = data.azurerm_key_vault.key_vault.id
}
83 changes: 83 additions & 0 deletions .identity/02_application_action.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
module "github_runner_app" {
source = "git::https://github.com/pagopa/github-actions-tf-modules.git//app-github-runner-creator?ref=main"

app_name = local.app_name

subscription_id = data.azurerm_subscription.current.id

github_org = local.github.org
github_repository = local.github.repository
github_environment_name = var.env

container_app_github_runner_env_rg = local.container_app_environment.resource_group
}

resource "null_resource" "github_runner_app_permissions_to_namespace" {
triggers = {
aks_id = data.azurerm_kubernetes_cluster.aks.id
service_principal_id = module.github_runner_app.client_id
namespace = local.domain
version = "v2"
}

provisioner "local-exec" {
command = <<EOT
az role assignment create --role "Azure Kubernetes Service RBAC Admin" \
--assignee ${self.triggers.service_principal_id} \
--scope ${self.triggers.aks_id}/namespaces/${self.triggers.namespace}
az role assignment list --role "Azure Kubernetes Service RBAC Admin" \
--scope ${self.triggers.aks_id}/namespaces/${self.triggers.namespace}
EOT
}

provisioner "local-exec" {
when = destroy
command = <<EOT
az role assignment delete --role "Azure Kubernetes Service RBAC Admin" \
--assignee ${self.triggers.service_principal_id} \
--scope ${self.triggers.aks_id}/namespaces/${self.triggers.namespace}
EOT
}
}

resource "azurerm_role_assignment" "environment_terraform_resource_group_dashboards" {
scope = data.azurerm_resource_group.dashboards.id
role_definition_name = "Contributor"
principal_id = module.github_runner_app.object_id
}




resource "azuread_application" "action" {
display_name = "github-${local.github.org}-${local.github.repository}-${var.env}"
}

resource "azuread_service_principal" "action" {
application_id = azuread_application.action.application_id
}

resource "azurerm_role_assignment" "environment_terraform_subscription" {
scope = data.azurerm_subscription.current.id
role_definition_name = "Reader"
principal_id = azuread_service_principal.action.object_id
}

resource "azurerm_role_assignment" "environment_key_vault" {
scope = data.azurerm_key_vault.key_vault.id
role_definition_name = "Reader"
principal_id = azuread_service_principal.action.object_id
}

resource "azurerm_key_vault_access_policy" "ad_group_policy" {
key_vault_id = data.azurerm_key_vault.key_vault.id

tenant_id = data.azurerm_client_config.current.tenant_id
object_id = azuread_service_principal.action.object_id

key_permissions = ["Get", "List", "Import" ]
secret_permissions = ["Get", "List"]
storage_permissions = []
certificate_permissions = []
}
88 changes: 88 additions & 0 deletions .identity/03_github_environment.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
resource "github_repository_environment" "github_repository_environment" {
environment = var.env
repository = local.github.repository
# filter teams reviewers from github_organization_teams
# if reviewers_teams is null no reviewers will be configured for environment
dynamic "reviewers" {
for_each = (var.github_repository_environment.reviewers_teams == null || var.env_short != "p" ? [] : [1])
content {
teams = matchkeys(
data.github_organization_teams.all.teams.*.id,
data.github_organization_teams.all.teams.*.name,
var.github_repository_environment.reviewers_teams
)
}
}
deployment_branch_policy {
protected_branches = var.github_repository_environment.protected_branches
custom_branch_policies = var.github_repository_environment.custom_branch_policies
}
}

locals {
env_secrets = {
"CLIENT_ID" : module.github_runner_app.application_id,
"TENANT_ID" : data.azurerm_client_config.current.tenant_id,
"SUBSCRIPTION_ID" : data.azurerm_subscription.current.subscription_id,
}
env_variables = {
"CONTAINER_APP_ENVIRONMENT_NAME" : local.container_app_environment.name,
"CONTAINER_APP_ENVIRONMENT_RESOURCE_GROUP_NAME" : local.container_app_environment.resource_group,
"CLUSTER_NAME" : local.aks_cluster.name,
"CLUSTER_RESOURCE_GROUP" : local.aks_cluster.resource_group_name,
"DOMAIN" : local.domain,
"NAMESPACE" : local.domain,
}
}

###############
# ENV Secrets #
###############

resource "github_actions_environment_secret" "github_environment_runner_secrets" {
for_each = local.env_secrets
repository = local.github.repository
environment = var.env
secret_name = each.key
plaintext_value = each.value
}

#################
# ENV Variables #
#################


resource "github_actions_environment_variable" "github_environment_runner_variables" {
for_each = local.env_variables
repository = local.github.repository
environment = var.env
variable_name = each.key
value = each.value
}

#############################
# Secrets of the Repository #
#############################

#tfsec:ignore:github-actions-no-plain-text-action-secrets # not real secret
resource "github_actions_secret" "secret_sonar_token" {
repository = local.github.repository
secret_name = "SONAR_TOKEN"
plaintext_value = data.azurerm_key_vault_secret.key_vault_sonar.value
}

#tfsec:ignore:github-actions-no-plain-text-action-secrets # not real secret
resource "github_actions_secret" "secret_bot_token" {

repository = local.github.repository
secret_name = "BOT_TOKEN_GITHUB"
plaintext_value = data.azurerm_key_vault_secret.key_vault_bot_token.value
}

#tfsec:ignore:github-actions-no-plain-text-action-secrets # not real secret
resource "github_actions_secret" "secret_cucumber_token" {

repository = local.github.repository
secret_name = "CUCUMBER_PUBLISH_TOKEN"
plaintext_value = data.azurerm_key_vault_secret.key_vault_cucumber_token.value
}
32 changes: 32 additions & 0 deletions .identity/99_main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
terraform {
required_version = ">=1.3.0"

required_providers {
azuread = {
source = "hashicorp/azuread"
version = "2.30.0"
}
azurerm = {
source = "hashicorp/azurerm"
version = "3.45.0"
}
github = {
source = "integrations/github"
version = "5.18.3"
}
}

backend "azurerm" {}
}

provider "azurerm" {
features {}
}

provider "github" {
owner = "pagopa"
}

data "azurerm_subscription" "current" {}

data "azurerm_client_config" "current" {}
59 changes: 59 additions & 0 deletions .identity/99_variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
locals {
github = {
org = "pagopa"
repository = "pagopa-receipt-pdf-datastore"
}

prefix = "pagopa"
domain = "receipts"
location_short = "weu"
product = "${var.prefix}-${var.env_short}"

app_name = "github-${local.github.org}-${local.github.repository}-${var.prefix}-${local.domain}-${var.env}-aks"

pagopa_apim_name = "${local.product}-apim"
pagopa_apim_rg = "${local.product}-api-rg"

aks_cluster = {
name = "${local.product}-${local.location_short}-${var.env}-aks"
resource_group_name = "${local.product}-${local.location_short}-${var.env}-aks-rg"
}

container_app_environment = {
name = "${local.prefix}-${var.env_short}-${local.location_short}-github-runner-cae",
resource_group = "${local.prefix}-${var.env_short}-${local.location_short}-github-runner-rg",
}
}

variable "env" {
type = string
}

variable "env_short" {
type = string
}

variable "prefix" {
type = string
default = "pagopa"
validation {
condition = (
length(var.prefix) <= 6
)
error_message = "Max length is 6 chars."
}
}

variable "github_repository_environment" {
type = object({
protected_branches = bool
custom_branch_policies = bool
reviewers_teams = list(string)
})
description = "GitHub Continuous Integration roles"
default = {
protected_branches = false
custom_branch_policies = true
reviewers_teams = ["pagopa-tech", "infrastrutture-admins"]
}
}
Loading

0 comments on commit 7a4d509

Please sign in to comment.