-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from ai-cfia/45-as-a-devops-i-would-like-a-vau…
…lt-integration-for-azure-aks-cluster Issue #45: Adds vault module
- Loading branch information
Showing
17 changed files
with
356 additions
and
20 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
File renamed without changes.
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
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
File renamed without changes.
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,13 @@ | ||
# This requires that jq and openssl are installed in the runtime environment | ||
# It creates a certificate signing request (CSR) based on the vault-csr.conf file | ||
# The 2 jq at the beginning and end of the pipes are used to read the input and wrap the result in json | ||
# since this is how terraform "external" passes data. | ||
data "external" "k8s_cert_request" { | ||
program = [ | ||
"bash", "-c", | ||
"curl -o jq https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 && chmod +x jq && jq -rc '.key' | openssl req -new -noenc -config ${path.module}/vault-csr.conf -key /dev/stdin | jq -rRncs '{\"request\": inputs}'" | ||
] | ||
query = { | ||
"key" = tls_private_key.pair.private_key_pem | ||
} | ||
} |
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,103 @@ | ||
data "azurerm_client_config" "current" {} | ||
|
||
# Needed to provide unique vault name | ||
resource "random_string" "azurerm_key_vault_name" { | ||
length = 5 | ||
numeric = true | ||
special = false | ||
upper = false | ||
} | ||
|
||
locals { | ||
suffix = random_string.azurerm_key_vault_name.result | ||
} | ||
|
||
# Create Azure Key Vault | ||
resource "azurerm_key_vault" "vault" { | ||
name = "${var.prefix}-vault-${local.suffix}" | ||
location = var.location | ||
resource_group_name = var.resource_group | ||
tenant_id = data.azurerm_client_config.current.tenant_id | ||
sku_name = "standard" | ||
soft_delete_retention_days = 7 | ||
purge_protection_enabled = false | ||
|
||
access_policy { | ||
tenant_id = data.azurerm_client_config.current.tenant_id | ||
object_id = data.azurerm_client_config.current.object_id | ||
|
||
key_permissions = var.key_permissions | ||
secret_permissions = var.secret_permissions | ||
} | ||
} | ||
|
||
resource "azurerm_key_vault_key" "vault_unseal" { | ||
name = "${var.prefix}-vault-unseal-${local.suffix}" | ||
key_vault_id = azurerm_key_vault.vault.id | ||
key_type = var.key_type | ||
key_size = var.key_size | ||
key_opts = var.key_opts | ||
|
||
# Define the rotation policy | ||
rotation_policy { | ||
automatic { | ||
time_before_expiry = "P7D" | ||
} | ||
|
||
expire_after = "P28D" | ||
notify_before_expiry = "P7D" | ||
} | ||
} | ||
|
||
# Set an access policy for the service principal to the Key Vault | ||
resource "azurerm_key_vault_access_policy" "vault" { | ||
key_vault_id = azurerm_key_vault.vault.id | ||
tenant_id = data.azurerm_client_config.current.tenant_id | ||
object_id = var.cluster_principal_id | ||
|
||
key_permissions = var.aks_key_permissions | ||
secret_permissions = var.aks_secret_permissions | ||
} | ||
|
||
# We make ask Kubernetes to sign the certificate | ||
# https://kubernetes.io/docs/reference/access-authn-authz/certificate-signing-requests/ | ||
resource "kubernetes_certificate_signing_request_v1" "vault_kube_cert_req" { | ||
metadata { | ||
name = "vault.svc" | ||
} | ||
spec { | ||
request = data.external.k8s_cert_request.result["request"] | ||
signer_name = "kubernetes.io/kubelet-serving" | ||
usages = ["digital signature", "key encipherment", "server auth"] | ||
} | ||
auto_approve = true | ||
lifecycle { | ||
ignore_changes = [spec[0].request] | ||
replace_triggered_by = [tls_private_key.pair] | ||
} | ||
} | ||
|
||
# Makes sure the vault namespace is created before adding secrets | ||
resource "kubernetes_namespace" "vault_ns" { | ||
metadata { | ||
name = "vault" | ||
labels = { | ||
"pod-security.kubernetes.io/enforce" = "privileged" | ||
} | ||
} | ||
} | ||
|
||
resource "kubernetes_secret" "vault_tls" { | ||
metadata { | ||
name = "vault-tls" | ||
namespace = kubernetes_namespace.vault_ns.metadata[0].name | ||
} | ||
|
||
data = { | ||
"vault.crt" = kubernetes_certificate_signing_request_v1.vault_kube_cert_req.certificate | ||
"vault.key" = tls_private_key.pair.private_key_pem | ||
"vault.ca" = var.ca_cluster | ||
} | ||
|
||
type = "kubernetes.io/generic" | ||
} |
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,35 @@ | ||
terraform { | ||
|
||
required_version = ">= 1.7.2" | ||
|
||
required_providers { | ||
local = { | ||
source = "hashicorp/local" | ||
version = "2.4.0" | ||
} | ||
tls = { | ||
source = "hashicorp/tls" | ||
version = "4.0.4" | ||
} | ||
external = { | ||
source = "hashicorp/external" | ||
version = "2.3.1" | ||
} | ||
kubernetes = { | ||
source = "hashicorp/kubernetes" | ||
version = "2.24.0" | ||
} | ||
azurerm = { | ||
source = "hashicorp/azurerm" | ||
version = "~> 3.25" | ||
} | ||
azuread = { | ||
source = "hashicorp/azuread" | ||
version = "2.47.0" | ||
} | ||
random = { | ||
source = "hashicorp/random" | ||
version = "~>3.0" | ||
} | ||
} | ||
} |
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,4 @@ | ||
resource "tls_private_key" "pair" { | ||
algorithm = "RSA" | ||
rsa_bits = 2048 | ||
} |
Oops, something went wrong.