Skip to content

Commit a55d556

Browse files
feat: encrypt ocp config when not in use
1 parent aa38887 commit a55d556

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

solutions/standard-openshift/ansible/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ No modules.
2727
| Name | Description | Type | Default | Required |
2828
|------|-------------|------|---------|:--------:|
2929
| <a name="input_ansible_host_or_ip"></a> [ansible\_host\_or\_ip](#input\_ansible\_host\_or\_ip) | Private IP of virtual server instance running RHEL OS on which ansible will be installed and configured to act as central ansible node. | `string` | n/a | yes |
30-
| <a name="input_ansible_vault_password"></a> [ansible\_vault\_password](#input\_ansible\_vault\_password) | Vault password to encrypt ansible playbooks that contain sensitive information. Password requirements: 15-100 characters and at least one uppercase letter, one lowercase letter, one number, and one special character. Allowed characters: A-Z, a-z, 0-9, !#$%&()*+-.:;<=>?@[]\_{\|}~. | `string` | `null` | no |
30+
| <a name="input_ansible_vault_password"></a> [ansible\_vault\_password](#input\_ansible\_vault\_password) | Vault password to encrypt ansible playbooks that contain sensitive information. Password requirements: 15-100 characters and at least one uppercase letter, one lowercase letter, one number, and one special character. Allowed characters: A-Z, a-z, 0-9, !#$%&()*+-.:;<=>?@[]\_{\|}~. | `string` | n/a | yes |
3131
| <a name="input_bastion_host_ip"></a> [bastion\_host\_ip](#input\_bastion\_host\_ip) | Jump/Bastion server public IP address to reach the ansible host which has private IP. | `string` | n/a | yes |
3232
| <a name="input_configure_ansible_host"></a> [configure\_ansible\_host](#input\_configure\_ansible\_host) | If set to true, bash script will be executed to install and configure the collections and packages on ansible node. | `bool` | n/a | yes |
3333
| <a name="input_dst_inventory_file_name"></a> [dst\_inventory\_file\_name](#input\_dst\_inventory\_file\_name) | Name for the inventory file to be generated on the Ansible host. | `string` | n/a | yes |
3434
| <a name="input_dst_playbook_file_name"></a> [dst\_playbook\_file\_name](#input\_dst\_playbook\_file\_name) | Name for the playbook file to be generated on the Ansible host. | `string` | n/a | yes |
3535
| <a name="input_dst_script_file_name"></a> [dst\_script\_file\_name](#input\_dst\_script\_file\_name) | Name for the bash file to be generated on the Ansible host. | `string` | n/a | yes |
36+
| <a name="input_encrypt_playbook"></a> [encrypt\_playbook](#input\_encrypt\_playbook) | Whether to encrypt the playbook using ansible vault. The ocp configuration will always be encrypted, this only applies to the playbooks. | `bool` | n/a | yes |
3637
| <a name="input_ibmcloud_api_key"></a> [ibmcloud\_api\_key](#input\_ibmcloud\_api\_key) | IBM Cloud platform API key needed to deploy IAM enabled resources. | `string` | `null` | no |
3738
| <a name="input_inventory_template_vars"></a> [inventory\_template\_vars](#input\_inventory\_template\_vars) | Map values for the inventory template. | `map(any)` | n/a | yes |
3839
| <a name="input_playbook_template_vars"></a> [playbook\_template\_vars](#input\_playbook\_template\_vars) | Map values for the ansible playbook template. | `map(any)` | n/a | yes |

solutions/standard-openshift/ansible/main.tf

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ resource "terraform_data" "trigger_ansible_vars" {
6666

6767
resource "terraform_data" "execute_playbooks" {
6868
depends_on = [terraform_data.setup_ansible_host]
69-
count = var.ansible_vault_password != null ? 0 : 1
69+
count = var.encrypt_playbook ? 0 : 1
7070

7171
connection {
7272
type = "ssh"
@@ -119,6 +119,15 @@ resource "terraform_data" "execute_playbooks" {
119119
]
120120
}
121121

122+
# Decrypt ocp config if it already exists
123+
provisioner "remote-exec" {
124+
inline = [
125+
"if [ -f \"~/.powervs/config.json\" ]; then echo ${var.ansible_vault_password} > password_file",
126+
"if [ -f \"~/.powervs/config.json\" ]; then ansible-vault decrypt ~/.powervs/config.json --vault-password-file password_file",
127+
"rm -f password_file"
128+
]
129+
}
130+
122131
# Execute bash shell script to run ansible playbooks
123132
provisioner "remote-exec" {
124133
inline = [
@@ -134,6 +143,15 @@ resource "terraform_data" "execute_playbooks" {
134143
]
135144
}
136145

146+
# Encrypt ocp config if it already exists
147+
provisioner "remote-exec" {
148+
inline = [
149+
"if [ -f \"~/.powervs/config.json\" ]; then echo ${var.ansible_vault_password} > password_file",
150+
"if [ -f \"~/.powervs/config.json\" ]; then ansible-vault encrypt ~/.powervs/config.json --vault-password-file password_file",
151+
"rm -f password_file"
152+
]
153+
}
154+
137155
# print output of openshift installation if applicable, else do nothing
138156
provisioner "remote-exec" {
139157
inline = [
@@ -145,7 +163,7 @@ resource "terraform_data" "execute_playbooks" {
145163

146164
resource "terraform_data" "execute_playbooks_with_vault" {
147165
depends_on = [terraform_data.setup_ansible_host]
148-
count = var.ansible_vault_password != null ? 1 : 0
166+
count = var.encrypt_playbook ? 1 : 0
149167

150168
connection {
151169
type = "ssh"
@@ -206,6 +224,15 @@ resource "terraform_data" "execute_playbooks_with_vault" {
206224
]
207225
}
208226

227+
# Decrypt ocp config if it already exists
228+
provisioner "remote-exec" {
229+
inline = [
230+
"if [ -f \"~/.powervs/config.json\" ]; then echo ${var.ansible_vault_password} > password_file",
231+
"if [ -f \"~/.powervs/config.json\" ]; then ansible-vault decrypt ~/.powervs/config.json --vault-password-file password_file",
232+
"rm -f password_file"
233+
]
234+
}
235+
209236
# Execute bash shell script to run ansible playbooks
210237
provisioner "remote-exec" {
211238
inline = [
@@ -222,6 +249,15 @@ resource "terraform_data" "execute_playbooks_with_vault" {
222249
"rm -rf ${local.private_key_file}"
223250
]
224251
}
252+
253+
# Encrypt ocp config if it already exists
254+
provisioner "remote-exec" {
255+
inline = [
256+
"if [ -f \"~/.powervs/config.json\" ]; then echo ${var.ansible_vault_password} > password_file",
257+
"if [ -f \"~/.powervs/config.json\" ]; then ansible-vault encrypt ~/.powervs/config.json --vault-password-file password_file",
258+
"rm -f password_file"
259+
]
260+
}
225261
}
226262

227263

solutions/standard-openshift/ansible/variables.tf

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,33 +63,37 @@ variable "ansible_vault_password" {
6363
description = "Vault password to encrypt ansible playbooks that contain sensitive information. Password requirements: 15-100 characters and at least one uppercase letter, one lowercase letter, one number, and one special character. Allowed characters: A-Z, a-z, 0-9, !#$%&()*+-.:;<=>?@[]_{|}~."
6464
type = string
6565
sensitive = true
66-
default = null
6766
validation {
68-
condition = var.ansible_vault_password == null ? true : (length(var.ansible_vault_password) >= 15 && length(var.ansible_vault_password) <= 100)
67+
condition = (length(var.ansible_vault_password) >= 15 && length(var.ansible_vault_password) <= 100)
6968
error_message = "ansible_vault_password needs to be between 15 and 100 characters in length."
7069
}
7170
validation {
72-
condition = var.ansible_vault_password == null ? true : can(regex("[A-Z]", var.ansible_vault_password))
71+
condition = can(regex("[A-Z]", var.ansible_vault_password))
7372
error_message = "ansible_vault_password needs to contain at least one uppercase character (A-Z)."
7473
}
7574
validation {
76-
condition = var.ansible_vault_password == null ? true : can(regex("[a-z]", var.ansible_vault_password))
75+
condition = can(regex("[a-z]", var.ansible_vault_password))
7776
error_message = "ansible_vault_password needs to contain at least one lowercase character (a-z)."
7877
}
7978
validation {
80-
condition = var.ansible_vault_password == null ? true : can(regex("[0-9]", var.ansible_vault_password))
79+
condition = can(regex("[0-9]", var.ansible_vault_password))
8180
error_message = "ansible_vault_password needs to contain at least one number (0-9)."
8281
}
8382
validation {
84-
condition = var.ansible_vault_password == null ? true : can(regex("[!#$%&()*+\\-.:;<=>?@[\\]_{|}~]", var.ansible_vault_password))
83+
condition = can(regex("[!#$%&()*+\\-.:;<=>?@[\\]_{|}~]", var.ansible_vault_password))
8584
error_message = "ansible_vault_password needs to contain at least one of the following special characters: !#$%&()*+-.:;<=>?@[]_{|}~"
8685
}
8786
validation {
88-
condition = var.ansible_vault_password == null ? true : can(regex("^[A-Za-z0-9!#$%&()*+\\-.:;<=>?@[\\]_{|}~]+$", var.ansible_vault_password))
87+
condition = can(regex("^[A-Za-z0-9!#$%&()*+\\-.:;<=>?@[\\]_{|}~]+$", var.ansible_vault_password))
8988
error_message = "ansible_vault_password contains illegal characters. Allowed characters: A-Z, a-z, 0-9, !#$%&()*+-.:;<=>?@[]_{|}~"
9089
}
9190
}
9291

92+
variable "encrypt_playbook" {
93+
description = "Whether to encrypt the playbook using ansible vault. The ocp configuration will always be encrypted, this only applies to the playbooks."
94+
type = bool
95+
}
96+
9397
variable "ibmcloud_api_key" {
9498
description = "IBM Cloud platform API key needed to deploy IAM enabled resources."
9599
type = string

solutions/standard-openshift/main.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ module "ocp_cluster_install_configuration" {
7171
ansible_host_or_ip = module.standard.ansible_host_or_ip
7272
ssh_private_key = var.ssh_private_key
7373
ansible_vault_password = var.ansible_vault_password
74+
encrypt_playbook = true
7475
configure_ansible_host = false
7576

7677
src_script_template_name = "deploy-openshift-cluster/ansible_exec_vault.sh.tftpl"
@@ -120,6 +121,8 @@ module "ocp_cluster_manifest_creation" {
120121
bastion_host_ip = module.standard.access_host_or_ip
121122
ansible_host_or_ip = module.standard.ansible_host_or_ip
122123
ssh_private_key = var.ssh_private_key
124+
ansible_vault_password = var.ansible_vault_password
125+
encrypt_playbook = false
123126
configure_ansible_host = false
124127
ibmcloud_api_key = var.ibmcloud_api_key
125128

@@ -148,6 +151,8 @@ module "ocp_cluster_deployment" {
148151
bastion_host_ip = module.standard.access_host_or_ip
149152
ansible_host_or_ip = module.standard.ansible_host_or_ip
150153
ssh_private_key = var.ssh_private_key
154+
ansible_vault_password = var.ansible_vault_password
155+
encrypt_playbook = false
151156
configure_ansible_host = false
152157
ibmcloud_api_key = var.ibmcloud_api_key
153158

0 commit comments

Comments
 (0)