generated from notablehealth/terraform-google-module-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
215 lines (207 loc) · 7.59 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* # terraform-google-gke-eso-namespace
*
* [![Releases](https://img.shields.io/github/v/release/notablehealth/terraform-google-gke-eso-namespace)](https://github.com/notablehealth/terraform-google-gke-eso-namespace/releases)
*
* [Terraform Module Registry](https://registry.terraform.io/modules/notablehealth/gke-eso-namespace/google)
*
* Setup External Secrets Operator (ESO) in a GKE namespace
*
* ## Module sets up a Kubernetes namespace for ESO
*
* - Create GCP service account
* - Create k8 namespace
* - Create Kubernetes service account for ESO
* - Create ESO secret store
* - Create ESO namespace secret rule
* - Create ESO shared secret rules
*
* ## Limitations
*
* - Kubernetes secrets are limited to 1 MB
*
* ## Provider setup required in calling module
*
* Access GKE cluster with DNS endpoint
*
* ``` hcl
* data "google_client_config" "self" {}
* data "google_container_cluster" "self" {
* name = var.cluster_name
* location = var.cluster_location
* project = var.project_id
* }
* provider "kubectl" {
* host = "https://${data.google_container_cluster.self.control_plane_endpoints_config[0].dns_endpoint_config[0].endpoint}"
* token = data.google_client_config.self.access_token
* load_config_file = false
* }
* provider "kubernetes" {
* host = "https://${data.google_container_cluster.self.control_plane_endpoints_config[0].dns_endpoint_config[0].endpoint}"
* token = data.google_client_config.self.access_token
* }
* terraform {
* required_providers {
* kubectl = {
* source = "gavinbunney/kubectl"
* version = ">= 1.18.0"
* }
* }
* }
* ```
*
*/
# https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/namespace
# https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/manifest
###--------------------------------
### GCP Service Account
###--------------------------------
resource "google_service_account" "self" {
account_id = "${var.gcpsm_secret_prefix}${var.cluster_name}-${var.namespace}"
description = "Permissions for ESO to access secrets for ${var.namespace}"
display_name = "ESO Secret Access for ${var.namespace}"
project = var.project_id
}
# Grant permissions to service account
locals {
# Allow k8-global, k8-namespace, and legacy prefixes (namespace, {namespace with _}, global)
prefixes = [
"${var.gcpsm_secret_prefix}${var.namespace}",
"${var.gcpsm_secret_prefix}${var.shared_prefix}",
replace(var.namespace, "/[_-]/", "_"),
replace(var.namespace, "/[_-]/", "-"),
var.shared_prefix,
]
expression = join(" || ", [
for prefix in local.prefixes : "resource.name.startsWith(\"projects/${var.project_number}/secrets/${prefix}${var.secret_separator}\")"
])
}
resource "google_project_iam_member" "secretAccessor" {
role = "roles/secretmanager.secretAccessor"
project = var.project_id
member = google_service_account.self.member
condition {
title = "secret_manager_accessor"
description = "Service Account for accessing secrets with certain prefix"
expression = (local.expression)
}
}
resource "google_project_iam_member" "viewer" {
role = "roles/secretmanager.viewer"
project = var.project_id
member = google_service_account.self.member
}
## Create K8 service account
resource "kubernetes_service_account" "self" {
metadata {
name = "eso-${var.namespace}"
namespace = var.namespace
annotations = {
"iam.gke.io/gcp-service-account" = google_service_account.self.email
}
#labels = {}
}
#automount_service_account_token = false
depends_on = [
kubernetes_namespace.self
]
}
## Allow ESO to impersonate the service account
resource "google_service_account_iam_binding" "k8-service-account-iam" {
service_account_id = google_service_account.self.name
role = "roles/iam.workloadIdentityUser"
members = [
"serviceAccount:${var.project_id}.svc.id.goog[${kubernetes_service_account.self.id}]",
]
depends_on = [
google_service_account.self,
kubernetes_service_account.self,
]
}
###--------------------------------
### Define k8 namespace
###--------------------------------
resource "kubernetes_namespace" "self" {
metadata {
name = var.namespace
}
}
# https://github.com/jrhouston/tfk8s cli to convert k8s yaml to terraform
###--------------------------------
### ESO secret store manifest
###--------------------------------
# Kubernetes provider 2.35.1
# kubernetes_manifest could not manage ESO secret store with Workload Identity
# Switched to kubectl provider
resource "kubectl_manifest" "eso_secret_store" {
yaml_body = templatefile("${path.module}/templates/k8-eso-secret-store-wi.yaml.tmpl", {
cluster_location = var.cluster_location
cluster_name = var.cluster_name
k8_namespace = var.namespace
k8_sa_namespace = replace(kubernetes_service_account.self.id, "/^(.*)[/].*$/", "$1")
project_id = var.project_id
service_account = replace(kubernetes_service_account.self.id, "/^.*[/](.*)$/", "$1")
})
depends_on = [
kubernetes_service_account.self
]
}
resource "local_file" "k8_eso_secret_store" {
count = var.local_manifests ? 1 : 0
content = templatefile("${path.module}/templates/k8-eso-secret-store-wi.yaml.tmpl", {
cluster_location = var.cluster_location
cluster_name = var.cluster_name
k8_namespace = var.namespace
k8_sa_namespace = replace(kubernetes_service_account.self.id, "/^(.*)[/].*$/", "$1")
project_id = var.project_id
service_account = replace(kubernetes_service_account.self.id, "/^.*[/](.*)$/", "$1")
})
filename = "${path.module}/manifests/${var.project_id}/${var.namespace}/secret-store.yaml"
file_permission = "0644"
}
###--------------------------------
### ESO namespace secrets manifest
###--------------------------------
resource "kubectl_manifest" "eso_namespace_secrets" {
yaml_body = templatefile("${path.module}/templates/k8-eso-secret-rule.yaml.tmpl", {
k8_namespace = var.namespace
k8_secret = var.namespace_secret_name
gcpsm_secret_prefix = var.gcpsm_secret_prefix
})
depends_on = [kubectl_manifest.eso_secret_store]
}
resource "local_file" "eso_namespace_secrets" {
count = var.local_manifests ? 1 : 0
content = templatefile("${path.module}/templates/k8-eso-secret-rule.yaml.tmpl", {
k8_namespace = var.namespace
k8_secret = var.namespace_secret_name
gcpsm_secret_prefix = var.gcpsm_secret_prefix
})
filename = "${path.module}/manifests/${var.project_id}/${var.namespace}/secrets-namespace.yaml"
file_permission = "0644"
}
###--------------------------------
### ESO shared secrets manifest
###--------------------------------
locals {
shared_header = templatefile("${path.module}/templates/k8-eso-secret-header.yaml.tmpl", {
k8_namespace = var.namespace
k8_secret = var.shared_secret_name
})
shared_keys = [
for key in var.shared_secrets : templatefile("${path.module}/templates/k8-eso-secret-key.yaml.tmpl", {
k8_key = key
gcp_secret = "${var.gcpsm_secret_prefix}${var.shared_prefix}${var.secret_separator}${key}"
})]
shared_manifest = join("\n", concat([local.shared_header], local.shared_keys))
}
resource "kubectl_manifest" "eso_shared_secrets" {
yaml_body = local.shared_manifest
depends_on = [kubectl_manifest.eso_secret_store]
}
resource "local_file" "eso_shared_secrets" {
count = var.local_manifests ? 1 : 0
content = local.shared_manifest
filename = "${path.module}/manifests/${var.project_id}/${var.namespace}/secrets-shared.yaml"
file_permission = "0644"
}