|
variable "vault_namespaces" { |
|
type = list(string) |
|
default = ["ns1", "ns2"] |
|
} |
|
locals { |
|
number_of_vault_secrets = var.use_vault ? length(var.vault_k8s_namespaces) : 0 |
|
|
|
vault_secret_name = "mysecret" |
|
vault_path_formatstr = "/path/to/%s" |
|
|
|
# Compute the vault path for all secrets, with and without the actual secret name |
|
# e.g. /path/to/<ns> and /path/to/<ns>/<secret_name> |
|
desired_paths_without_secret_name = [for ns in var.vault_namespaces : format(local.vault_path_formatstr, ns) ] |
|
desired_paths = [for path in local.desired_paths_without_secret_name : join("/", [path, local.vault_secret_name]) ] |
|
} |
|
|
|
# Get all the secrets in a particular namespace |
|
data "vault_kv_secrets_list_v2" "secret_list" { |
|
for_each = local.desired_paths_without_secret_name |
|
|
|
mount = "secrets" |
|
name = each.key |
|
} |
|
# Check which paths already have a secret with our name |
|
locals { |
|
existing_paths_without_secret_name = compact([for s in data.vault_kv_secrets_list_v2.secret_list : contains([for n in s.names : basename(n)], local.vault_secret_name) ? s.name : "" ]) |
|
} |
|
# compute full name os existing secrets |
|
locals { |
|
existing_paths = [for path in local.existing_paths_without_secret_name : join("/",[path, local.vault_secret_name]) ] |
|
} |
|
# Get data from existing Secrets |
|
data "vault_kv_secret_v2" "existing_secrets"{ |
|
for_each = toset(local.existing_paths) |
|
|
|
mount = "secrets" |
|
name = each.key |
|
} |
|
# map existing data to the secrets we want to create (or default to emtpy) |
|
locals { |
|
existing_data_or_default = [ |
|
for i, path in local.desired_paths : |
|
contains(local.existing_paths, path) |
|
? data.vault_kv_secret_v2.existing_secrets[path].data_json |
|
: "{}" |
|
] |
|
} |
|
|
|
|
|
# Store client_secret in vault |
|
resource "vault_kv_secret_v2" "client_secret" { |
|
count = local.number_of_vault_secrets |
|
|
|
mount = "secrets" |
|
name = "${local.desired_paths[count.index]}" |
|
data_json = jsonencode( |
|
merge( |
|
jsondecode(local.existing_data_or_default[count.index]), |
|
{ |
|
"foo" = "bar" |
|
} |
|
) |
|
) |
|
} |