Debian Codegraph @

ansible_collections/community/crypto/tests/integration/targets/x509_certificate-acme/tasks/main.yml

raw
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 # WARNING: These are designed specifically for Ansible tests #
8 # and should not be used as examples of how to write Ansible roles #
9 ####################################################################
10
11 - block:
12 - name: Obtain root and intermediate certificates
13 ansible.builtin.get_url:
14 url: "http://{{ acme_host }}:5000/{{ item.0 }}-certificate-for-ca/{{ item.1 }}"
15 dest: "{{ remote_tmp_dir }}/acme-{{ item.0 }}-{{ item.1 }}.pem"
16 loop: "{{ query('nested', types, root_numbers) }}"
17
18 - name: Analyze root certificates
19 community.crypto.x509_certificate_info:
20 path: "{{ remote_tmp_dir }}/acme-root-{{ item }}.pem"
21 loop: "{{ root_numbers }}"
22 register: acme_roots
23
24 - name: Analyze intermediate certificates
25 community.crypto.x509_certificate_info:
26 path: "{{ remote_tmp_dir }}/acme-intermediate-{{ item }}.pem"
27 loop: "{{ root_numbers }}"
28 register: acme_intermediates
29
30 - name: Read root certificates
31 ansible.builtin.slurp:
32 src: "{{ remote_tmp_dir ~ '/acme-root-' ~ item ~ '.pem' }}"
33 loop: "{{ root_numbers }}"
34 register: slurp_roots
35
36 - ansible.builtin.set_fact:
37 x__: "{{ item | dict2items | selectattr('key', 'in', interesting_keys) | list | items2dict }}"
38 loop: "{{ acme_roots.results }}"
39 register: acme_roots_tmp
40
41 - name: Read intermediate certificates
42 ansible.builtin.slurp:
43 src: "{{ remote_tmp_dir ~ '/acme-intermediate-' ~ item ~ '.pem' }}"
44 loop: "{{ root_numbers }}"
45 register: slurp_intermediates
46
47 - ansible.builtin.set_fact:
48 x__: "{{ item | dict2items | selectattr('key', 'in', interesting_keys) | list | items2dict }}"
49 loop: "{{ acme_intermediates.results }}"
50 register: acme_intermediates_tmp
51
52 - ansible.builtin.set_fact:
53 acme_roots: "{{ acme_roots_tmp.results | map(attribute='ansible_facts.x__') | list }}"
54 acme_root_certs: "{{ slurp_roots.results | map(attribute='content') | map('b64decode') | list }}"
55 acme_intermediates: "{{ acme_intermediates_tmp.results | map(attribute='ansible_facts.x__') | list }}"
56 acme_intermediate_certs: "{{ slurp_intermediates.results | map(attribute='content') | map('b64decode') | list }}"
57
58 vars:
59 types:
60 - root
61 - intermediate
62 root_numbers:
63 - 0
64 interesting_keys:
65 - authority_key_identifier
66 - subject_key_identifier
67 - issuer
68 - subject
69
70 - name: Get hold of acme-tiny executable
71 ansible.builtin.get_url:
72 url: https://raw.githubusercontent.com/diafygi/acme-tiny/master/acme_tiny.py
73 dest: "{{ remote_tmp_dir }}/acme-tiny"
74 mode: "0755"
75
76 - name: "Monkey-patch acme-tiny: Disable certificate validation"
77 ansible.builtin.blockinfile:
78 path: "{{ remote_tmp_dir }}/acme-tiny"
79 marker: "# {mark} ANSIBLE MANAGED BLOCK: DISABLE CERTIFICATE VALIDATION FOR HTTPS REQUESTS"
80 insertafter: '^#!.*'
81 block: |
82 import ssl
83 try:
84 ssl._create_default_https_context = ssl._create_unverified_context
85 except Exception:
86 # Python before 2.7.9 has no verification at all. So nothing to disable.
87 pass
88 # For later:
89 try:
90 from urllib.request import Request # Python 3
91 except ImportError:
92 from urllib2 import Request # Python 2
93
94 - name: "Monkey-patch acme-tiny: adjust shebang"
95 ansible.builtin.replace:
96 path: "{{ remote_tmp_dir }}/acme-tiny"
97 regexp: '^\#\!/usr/bin/env .*$'
98 replace: '#!{{ ansible_python_interpreter }}'
99
100 - name: "Monkey-patch acme-tiny: Disable check that challenge file is reachable via HTTP"
101 ansible.builtin.replace:
102 path: "{{ remote_tmp_dir }}/acme-tiny"
103 regexp: 'parser\.add_argument\("--disable-check", default=False,'
104 replace: 'parser.add_argument("--disable-check", default=True,'
105
106 - name: "Monkey-patch acme-tiny: Instead of writing challenge files to disk, post them to challenge server"
107 ansible.builtin.replace:
108 path: "{{ remote_tmp_dir }}/acme-tiny"
109 regexp: 'with open\(wellknown_path, "w"\) as [^:]+:\n\s+[^. ]+\.write\(([^)]+)\)'
110 replace: 'r = Request(url="http://{{ acme_host }}:5000/http/" + domain + "/" + token, data=\1.encode("utf8"), headers={"content-type": "application/octet-stream"}) ; r.get_method = lambda: "PUT" ; urlopen(r).close()'
111
112 - name: "Monkey-patch acme-tiny: Remove file cleanup"
113 ansible.builtin.replace:
114 path: "{{ remote_tmp_dir }}/acme-tiny"
115 regexp: 'os\.remove\(wellknown_path\)'
116 replace: 'pass'
117
118 - name: "Monkey-patch acme-tiny: Allow to run with Python 2"
119 ansible.builtin.replace:
120 path: "{{ remote_tmp_dir }}/acme-tiny"
121 regexp: '#!/usr/bin/env python3'
122 replace: '#!/usr/bin/env python'
123 when: ansible_facts.python.version.major == 2
124
125 - name: Create challenges directory
126 ansible.builtin.file:
127 path: '{{ remote_tmp_dir }}/challenges'
128 state: directory
129
130 - name: Running tests
131 ansible.builtin.include_tasks: impl.yml
132 # Make x509_certificate module happy
133 when: cryptography_version is version('3.3', '>=')