add app_roles terraform module for vault and declare erp app
This commit is contained in:
6
hashicorp-vault/iac/backend.tf
Normal file
6
hashicorp-vault/iac/backend.tf
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
terraform {
|
||||||
|
backend "gcs" {
|
||||||
|
bucket = "arcodange-tf"
|
||||||
|
prefix = "tools/hashicorp_vault/main"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,27 +1,3 @@
|
|||||||
terraform {
|
|
||||||
backend "gcs" {
|
|
||||||
bucket = "arcodange-tf"
|
|
||||||
prefix = "tools/hashicorp_vault/main"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
terraform {
|
|
||||||
required_providers {
|
|
||||||
vault = {
|
|
||||||
source = "vault"
|
|
||||||
version = "4.4.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
provider vault {
|
|
||||||
address = "https://vault.arcodange.duckdns.org"
|
|
||||||
auth_login_jwt { # TERRAFORM_VAULT_AUTH_JWT environment variable
|
|
||||||
mount = "gitea_jwt"
|
|
||||||
role = "gitea_cicd"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
resource "vault_auth_backend" "kubernetes" {
|
resource "vault_auth_backend" "kubernetes" {
|
||||||
type = "kubernetes"
|
type = "kubernetes"
|
||||||
}
|
}
|
||||||
@@ -98,14 +74,9 @@ resource "vault_kubernetes_auth_backend_role" "vso" {
|
|||||||
alias_name_source = "serviceaccount_name"
|
alias_name_source = "serviceaccount_name"
|
||||||
}
|
}
|
||||||
|
|
||||||
locals { # turn into *.tfvars ?
|
|
||||||
apps = toset([
|
|
||||||
"webapp",
|
|
||||||
])
|
|
||||||
}
|
|
||||||
module "app_policies" {
|
module "app_policies" {
|
||||||
source = "./modules/app_policy"
|
source = "./modules/app_policy"
|
||||||
for_each = local.apps
|
for_each = var.applications
|
||||||
name = each.value
|
name = each.value
|
||||||
gitea_app_id = var.gitea_app_id
|
gitea_app_id = var.gitea_app_id
|
||||||
}
|
}
|
||||||
@@ -27,6 +27,11 @@ data "vault_policy_document" "ops" {
|
|||||||
path = "kvv1/google/credentials"
|
path = "kvv1/google/credentials"
|
||||||
capabilities = [ "read" ]
|
capabilities = [ "read" ]
|
||||||
}
|
}
|
||||||
|
# read tofu_module_reader gitea bot user ssh keys
|
||||||
|
rule {
|
||||||
|
path = "kvv1/gitea/tofu_module_reader"
|
||||||
|
capabilities = [ "read" ]
|
||||||
|
}
|
||||||
|
|
||||||
# edit postgres credentials access permissions
|
# edit postgres credentials access permissions
|
||||||
rule {
|
rule {
|
||||||
|
|||||||
38
hashicorp-vault/iac/modules/app_roles/main.tf
Normal file
38
hashicorp-vault/iac/modules/app_roles/main.tf
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
data "vault_auth_backend" "kubernetes" {
|
||||||
|
path = "kubernetes"
|
||||||
|
}
|
||||||
|
|
||||||
|
locals {
|
||||||
|
name = lower(var.name)
|
||||||
|
database = var.database == null ? local.name : var.database
|
||||||
|
|
||||||
|
vault_mount_postgres = { path = "postgres" }
|
||||||
|
vault_mount_kvv2 = { path = "kvv2" }
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "vault_database_secret_backend_role" "role" {
|
||||||
|
backend = local.vault_mount_postgres.path
|
||||||
|
name = "${local.name}"
|
||||||
|
db_name = "postgres"
|
||||||
|
creation_statements = [
|
||||||
|
"CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}';",
|
||||||
|
"GRANT ${local.name}_role TO \"{{name}}\";",
|
||||||
|
]
|
||||||
|
revocation_statements = [
|
||||||
|
"REASSIGN OWNED BY \"{{name}}\" TO ${local.name}_role;",
|
||||||
|
"REVOKE ALL ON DATABASE ${local.database} FROM \"{{name}}\";", # should we drop the role ?
|
||||||
|
]
|
||||||
|
renew_statements=[]
|
||||||
|
rollback_statements=[]
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "vault_kubernetes_auth_backend_role" "role" {
|
||||||
|
backend = data.vault_auth_backend.kubernetes.path
|
||||||
|
role_name = local.name
|
||||||
|
bound_service_account_names = [local.name]
|
||||||
|
bound_service_account_namespaces = [local.name]
|
||||||
|
token_ttl = 3600
|
||||||
|
token_policies = ["default", local.name]
|
||||||
|
audience = "vault"
|
||||||
|
alias_name_source = "serviceaccount_name"
|
||||||
|
}
|
||||||
16
hashicorp-vault/iac/modules/app_roles/outputs.tf
Normal file
16
hashicorp-vault/iac/modules/app_roles/outputs.tf
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
output "name" {
|
||||||
|
value = local.name
|
||||||
|
}
|
||||||
|
output "database" {
|
||||||
|
value = local.database
|
||||||
|
}
|
||||||
|
output "mount_paths" {
|
||||||
|
value = {
|
||||||
|
k8s = data.vault_auth_backend.kubernetes.path
|
||||||
|
pg = local.vault_mount_postgres.path
|
||||||
|
kvv2 = local.vault_mount_kvv2.path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
output "kvv2_path_prefix" {
|
||||||
|
value = format("%s/", local.name)
|
||||||
|
}
|
||||||
8
hashicorp-vault/iac/modules/app_roles/providers.tf
Normal file
8
hashicorp-vault/iac/modules/app_roles/providers.tf
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
terraform {
|
||||||
|
required_providers {
|
||||||
|
vault = {
|
||||||
|
source = "vault"
|
||||||
|
version = ">= 4.4.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
8
hashicorp-vault/iac/modules/app_roles/variables.tf
Normal file
8
hashicorp-vault/iac/modules/app_roles/variables.tf
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
variable "name" {
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
variable "database" {
|
||||||
|
type = string
|
||||||
|
nullable = true
|
||||||
|
default = null
|
||||||
|
}
|
||||||
16
hashicorp-vault/iac/providers.tf
Normal file
16
hashicorp-vault/iac/providers.tf
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
terraform {
|
||||||
|
required_providers {
|
||||||
|
vault = {
|
||||||
|
source = "vault"
|
||||||
|
version = "4.4.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
provider vault {
|
||||||
|
address = "https://vault.arcodange.duckdns.org"
|
||||||
|
auth_login_jwt { # TERRAFORM_VAULT_AUTH_JWT environment variable
|
||||||
|
mount = "gitea_jwt"
|
||||||
|
role = "gitea_cicd"
|
||||||
|
}
|
||||||
|
}
|
||||||
4
hashicorp-vault/iac/terraform.tfvars
Normal file
4
hashicorp-vault/iac/terraform.tfvars
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
applications = [
|
||||||
|
"webapp",
|
||||||
|
"erp",
|
||||||
|
]
|
||||||
@@ -9,3 +9,6 @@ variable "POSTGRES_CREDENTIALS_EDITOR_PASSWORD" {
|
|||||||
type = string
|
type = string
|
||||||
sensitive = true
|
sensitive = true
|
||||||
}
|
}
|
||||||
|
variable "applications" {
|
||||||
|
type = set(string)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user