| 1 |
--- |
| 2 |
# Copyright (c) Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
# |
| 7 |
# Prepare X509 and PKCS#12 materials |
| 8 |
# |
| 9 |
|
| 10 |
- name: Create private keys |
| 11 |
community.crypto.openssl_privatekey: |
| 12 |
path: "{{ item }}" |
| 13 |
mode: "u=rw,go=" |
| 14 |
loop: |
| 15 |
- "{{ test_key_path }}" |
| 16 |
- "{{ test_key2_path }}" |
| 17 |
|
| 18 |
- name: Generate CSR for self-signed certificate used as a placeholder to create the java keystore |
| 19 |
community.crypto.openssl_csr: |
| 20 |
path: "{{ test_csr_path }}" |
| 21 |
privatekey_path: "{{ test_key_path }}" |
| 22 |
commonName: "localhost" |
| 23 |
|
| 24 |
- name: Generate CSR for self-signed certificate used for testing |
| 25 |
community.crypto.openssl_csr: |
| 26 |
path: "{{ test_csr2_path }}" |
| 27 |
privatekey_path: "{{ test_key2_path }}" |
| 28 |
commonName: "localhost" |
| 29 |
|
| 30 |
- name: Generate the self-signed cert used as a placeholder to create the java keystore |
| 31 |
community.crypto.x509_certificate: |
| 32 |
path: "{{ test_cert_path }}" |
| 33 |
csr_path: "{{ test_csr_path }}" |
| 34 |
privatekey_path: "{{ test_key_path }}" |
| 35 |
provider: selfsigned |
| 36 |
|
| 37 |
- name: Generate the self signed cert we will use for testing |
| 38 |
community.crypto.x509_certificate: |
| 39 |
path: "{{ test_cert2_path }}" |
| 40 |
csr_path: "{{ test_csr2_path }}" |
| 41 |
privatekey_path: "{{ test_key2_path }}" |
| 42 |
provider: selfsigned |
| 43 |
|
| 44 |
- name: Create the pkcs12 archive from the test x509 cert |
| 45 |
community.crypto.openssl_pkcs12: |
| 46 |
name: "test_pkcs12_cert" |
| 47 |
path: "{{ test_pkcs_path }}" |
| 48 |
passphrase: "{{ test_keystore2_password }}" |
| 49 |
certificate_path: "{{ test_cert_path }}" |
| 50 |
privatekey_path: "{{ test_key_path }}" |
| 51 |
when: |
| 52 |
- "not (ansible_facts.os_family == 'RedHat' and ansible_facts.distribution_version is version('8.0', '<'))" |
| 53 |
|
| 54 |
- name: Create the pkcs12 archive from the test x509 cert (command) |
| 55 |
ansible.builtin.command: |
| 56 |
cmd: > |
| 57 |
openssl pkcs12 -export |
| 58 |
-in {{ test_cert_path }} |
| 59 |
-inkey {{ test_key_path }} |
| 60 |
-name test_pkcs12_cert |
| 61 |
-out {{ test_pkcs_path }} |
| 62 |
-passout stdin |
| 63 |
stdin: "{{ test_keystore2_password }}" |
| 64 |
when: |
| 65 |
- "ansible_facts.os_family == 'RedHat'" |
| 66 |
- "ansible_facts.distribution_version is version('8.0', '<')" |
| 67 |
|
| 68 |
- name: Create the pkcs12 archive from the certificate we will be trying to add to the keystore |
| 69 |
community.crypto.openssl_pkcs12: |
| 70 |
name: "test_pkcs12_cert" |
| 71 |
path: "{{ test_pkcs2_path }}" |
| 72 |
passphrase: "{{ test_keystore2_password }}" |
| 73 |
certificate_path: "{{ test_cert2_path }}" |
| 74 |
privatekey_path: "{{ test_key2_path }}" |
| 75 |
when: |
| 76 |
- "not (ansible_facts.os_family == 'RedHat' and ansible_facts.distribution_version is version('8.0', '<'))" |
| 77 |
|
| 78 |
- name: Create the pkcs12 archive from the certificate we will be trying to add to the keystore (command) |
| 79 |
ansible.builtin.command: |
| 80 |
cmd: > |
| 81 |
openssl pkcs12 -export |
| 82 |
-in {{ test_cert2_path }} |
| 83 |
-inkey {{ test_key2_path }} |
| 84 |
-name test_pkcs12_cert |
| 85 |
-out {{ test_pkcs2_path }} |
| 86 |
-passout stdin |
| 87 |
stdin: "{{ test_keystore2_password }}" |
| 88 |
when: |
| 89 |
- "ansible_facts.os_family == 'RedHat'" |
| 90 |
- "ansible_facts.distribution_version is version('8.0', '<')" |
| 91 |
|
| 92 |
# |
| 93 |
# Run tests |
| 94 |
# |
| 95 |
|
| 96 |
- name: try to create the test keystore based on the just created pkcs12, keystore_create flag not enabled |
| 97 |
community.general.java_cert: |
| 98 |
cert_alias: test_pkcs12_cert |
| 99 |
pkcs12_alias: test_pkcs12_cert |
| 100 |
pkcs12_path: "{{ test_pkcs_path }}" |
| 101 |
pkcs12_password: "{{ test_keystore2_password }}" |
| 102 |
keystore_path: "{{ test_keystore2_path }}" |
| 103 |
keystore_pass: "{{ test_keystore2_password }}" |
| 104 |
ignore_errors: true |
| 105 |
register: result_x509_changed |
| 106 |
|
| 107 |
- name: Verify the x509 status is failed |
| 108 |
ansible.builtin.assert: |
| 109 |
that: |
| 110 |
- result_x509_changed is failed |
| 111 |
|
| 112 |
- name: Create the test keystore based on the just created pkcs12 |
| 113 |
community.general.java_cert: |
| 114 |
cert_alias: test_pkcs12_cert |
| 115 |
pkcs12_alias: test_pkcs12_cert |
| 116 |
pkcs12_path: "{{ test_pkcs_path }}" |
| 117 |
pkcs12_password: "{{ test_keystore2_password }}" |
| 118 |
keystore_path: "{{ test_keystore2_path }}" |
| 119 |
keystore_pass: "{{ test_keystore2_password }}" |
| 120 |
keystore_create: true |
| 121 |
|
| 122 |
- name: List newly created keystore content |
| 123 |
ansible.builtin.command: |
| 124 |
cmd: "keytool -list -keystore {{ test_keystore2_path }}" |
| 125 |
stdin: "{{ test_keystore2_password }}" |
| 126 |
register: keytool_list_keystore |
| 127 |
|
| 128 |
- name: Assert that the keystore has a private key entry |
| 129 |
ansible.builtin.assert: |
| 130 |
that: |
| 131 |
- "keytool_list_keystore.stdout_lines[5] is match('test_pkcs12_cert,.*, PrivateKeyEntry, $')" |
| 132 |
|
| 133 |
- name: try to import from pkcs12 a non existing alias |
| 134 |
community.general.java_cert: |
| 135 |
cert_alias: test_pkcs12_cert |
| 136 |
pkcs12_alias: non_existing_alias |
| 137 |
pkcs12_path: "{{ test_pkcs_path }}" |
| 138 |
pkcs12_password: "{{ test_keystore2_password }}" |
| 139 |
keystore_path: "{{ test_keystore2_path }}" |
| 140 |
keystore_pass: "{{ test_keystore2_password }}" |
| 141 |
keystore_create: true |
| 142 |
ignore_errors: true |
| 143 |
register: result_x509_changed |
| 144 |
|
| 145 |
- name: Verify the x509 status is failed |
| 146 |
ansible.builtin.assert: |
| 147 |
that: |
| 148 |
- result_x509_changed is failed |
| 149 |
|
| 150 |
- name: Import initial test certificate from file path |
| 151 |
community.general.java_cert: |
| 152 |
cert_alias: test_cert |
| 153 |
cert_path: "{{ test_cert_path }}" |
| 154 |
keystore_path: "{{ test_keystore2_path }}" |
| 155 |
keystore_pass: "{{ test_keystore2_password }}" |
| 156 |
keystore_create: true |
| 157 |
state: present |
| 158 |
register: result_x509_changed |
| 159 |
|
| 160 |
- name: Verify the x509 status is changed |
| 161 |
ansible.builtin.assert: |
| 162 |
that: |
| 163 |
- result_x509_changed is changed |
| 164 |
|
| 165 |
- name: | |
| 166 |
Import the newly created certificate. This is our main test. |
| 167 |
If the java_cert has been updated properly, then this task will report changed each time |
| 168 |
since the module will be comparing the hash of the certificate instead of validating that the alias |
| 169 |
simply exists |
| 170 |
community.general.java_cert: |
| 171 |
cert_alias: test_cert |
| 172 |
cert_path: "{{ test_cert2_path }}" |
| 173 |
keystore_path: "{{ test_keystore2_path }}" |
| 174 |
keystore_pass: "{{ test_keystore2_password }}" |
| 175 |
state: present |
| 176 |
register: result_x509_changed |
| 177 |
|
| 178 |
- name: Verify the x509 status is changed |
| 179 |
ansible.builtin.assert: |
| 180 |
that: |
| 181 |
- result_x509_changed is changed |
| 182 |
|
| 183 |
- name: | |
| 184 |
We also want to make sure that the status does not change if we import the same cert |
| 185 |
community.general.java_cert: |
| 186 |
cert_alias: test_cert |
| 187 |
cert_path: "{{ test_cert2_path }}" |
| 188 |
keystore_path: "{{ test_keystore2_path }}" |
| 189 |
keystore_pass: "{{ test_keystore2_password }}" |
| 190 |
state: present |
| 191 |
register: result_x509_succeeded |
| 192 |
|
| 193 |
- name: Verify the x509 status is ok |
| 194 |
ansible.builtin.assert: |
| 195 |
that: |
| 196 |
- result_x509_succeeded is succeeded |
| 197 |
|
| 198 |
- name: > |
| 199 |
Ensure the original pkcs12 cert is in the keystore |
| 200 |
community.general.java_cert: |
| 201 |
cert_alias: test_pkcs12_cert |
| 202 |
pkcs12_alias: test_pkcs12_cert |
| 203 |
pkcs12_path: "{{ test_pkcs_path }}" |
| 204 |
pkcs12_password: "{{ test_keystore2_password }}" |
| 205 |
keystore_path: "{{ test_keystore2_path }}" |
| 206 |
keystore_pass: "{{ test_keystore2_password }}" |
| 207 |
state: present |
| 208 |
|
| 209 |
- name: | |
| 210 |
Perform the same test, but we will now be testing the pkcs12 functionality |
| 211 |
If we add a different pkcs12 cert with the same alias, we should have a changed result, NOT the same |
| 212 |
community.general.java_cert: |
| 213 |
cert_alias: test_pkcs12_cert |
| 214 |
pkcs12_alias: test_pkcs12_cert |
| 215 |
pkcs12_path: "{{ test_pkcs2_path }}" |
| 216 |
pkcs12_password: "{{ test_keystore2_password }}" |
| 217 |
keystore_path: "{{ test_keystore2_path }}" |
| 218 |
keystore_pass: "{{ test_keystore2_password }}" |
| 219 |
state: present |
| 220 |
register: result_pkcs12_changed |
| 221 |
|
| 222 |
- name: Verify the pkcs12 status is changed |
| 223 |
ansible.builtin.assert: |
| 224 |
that: |
| 225 |
- result_pkcs12_changed is changed |
| 226 |
|
| 227 |
- name: | |
| 228 |
We are requesting the same cert now, so the status should show OK |
| 229 |
community.general.java_cert: |
| 230 |
cert_alias: test_pkcs12_cert |
| 231 |
pkcs12_alias: test_pkcs12_cert |
| 232 |
pkcs12_path: "{{ test_pkcs2_path }}" |
| 233 |
pkcs12_password: "{{ test_keystore2_password }}" |
| 234 |
keystore_path: "{{ test_keystore2_path }}" |
| 235 |
keystore_pass: "{{ test_keystore2_password }}" |
| 236 |
register: result_pkcs12_succeeded |
| 237 |
|
| 238 |
- name: Verify the pkcs12 status is ok |
| 239 |
ansible.builtin.assert: |
| 240 |
that: |
| 241 |
- result_pkcs12_succeeded is succeeded |
| 242 |
|
| 243 |
- name: Copy the ssl server script |
| 244 |
copy: |
| 245 |
src: "setupSSLServer.py" |
| 246 |
dest: "{{ remote_tmp_dir }}" |
| 247 |
|
| 248 |
- name: Create an SSL server that we will use for testing URL imports |
| 249 |
command: "{{ ansible_facts.python.executable }} {{ remote_tmp_dir }}/setupSSLServer.py {{ remote_tmp_dir }} {{ test_ssl_port }}" |
| 250 |
async: 10 |
| 251 |
poll: 0 |
| 252 |
|
| 253 |
- name: "Wait for one second to make sure that the serve script has actually been started" |
| 254 |
pause: |
| 255 |
seconds: 1 |
| 256 |
|
| 257 |
- name: | |
| 258 |
Download the original cert.pem from our temporary server. The current cert should contain |
| 259 |
cert2.pem. Importing this cert should return a status of changed |
| 260 |
community.general.java_cert: |
| 261 |
cert_alias: test_cert_localhost |
| 262 |
cert_url: localhost |
| 263 |
cert_port: "{{ test_ssl_port }}" |
| 264 |
keystore_path: "{{ test_keystore2_path }}" |
| 265 |
keystore_pass: "{{ test_keystore2_password }}" |
| 266 |
state: present |
| 267 |
register: result_url_changed |
| 268 |
|
| 269 |
- name: Verify that the url status is changed |
| 270 |
ansible.builtin.assert: |
| 271 |
that: |
| 272 |
- result_url_changed is changed |
| 273 |
|
| 274 |
- name: Ensure we can remove the x509 cert |
| 275 |
community.general.java_cert: |
| 276 |
cert_alias: test_cert |
| 277 |
keystore_path: "{{ test_keystore2_path }}" |
| 278 |
keystore_pass: "{{ test_keystore2_password }}" |
| 279 |
state: absent |
| 280 |
register: result_x509_absent |
| 281 |
|
| 282 |
- name: Verify the x509 cert is absent |
| 283 |
ansible.builtin.assert: |
| 284 |
that: |
| 285 |
- result_x509_absent is changed |
| 286 |
|
| 287 |
- name: Ensure we can remove the certificate imported from pkcs12 archive |
| 288 |
community.general.java_cert: |
| 289 |
cert_alias: test_pkcs12_cert |
| 290 |
keystore_path: "{{ test_keystore2_path }}" |
| 291 |
keystore_pass: "{{ test_keystore2_password }}" |
| 292 |
state: absent |
| 293 |
register: result_pkcs12_absent |
| 294 |
|
| 295 |
- name: Verify the pkcs12 archive is absent |
| 296 |
ansible.builtin.assert: |
| 297 |
that: |
| 298 |
- result_pkcs12_absent is changed |