Update cert role to enable to load them to secrets
[ta/caas-security.git] / ansible / roles / cert / tasks / main.yml
1 ---
2 # Copyright 2019 Nokia
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 - name: template node.conf
17   template:
18     src: "node.conf.j2"
19     dest: /etc/openssl/node.conf
20     mode: 0000
21
22 - name: check instance cert directory
23   stat:
24     path: "{{ cert_path }}/ca.pem"
25   register: cert_path_register
26
27 - name: create cert directory
28   file:
29     name: "{{ cert_path }}"
30     state: directory
31   when: not cert_path_register.stat.exists
32
33 # The 'create cert directory' and 'changing permissions of cert directory' tasks cannot merged together!
34 # Since 'state: directory' creates the directory recursively.
35 # So, if cert_path is e.g: /etc/kubernetes/ssl, then /etc/kubernetes would get 700 as it's permisson.
36 # And in that case the admin user would get access denied for the /etc/kubernetes folder.
37 - name: changing permissions of cert directory
38   file:
39     path: "{{ cert_path }}"
40     mode: 0700
41   when: not cert_path_register.stat.exists
42
43 - name: adding default acl read to {{ users.admin_user_name }} to {{ cert_path }}
44   acl:
45     default: yes
46     name:  "{{ cert_path }}"
47     entity: "{{ users.admin_user_name }}"
48     etype: user
49     permissions: rx
50     recursive: yes
51     state: present
52
53 - name: adding acl read to {{ users.admin_user_name }} to {{ cert_path }}
54   acl:
55     name:  "{{ cert_path }}"
56     entity: "{{ users.admin_user_name }}"
57     etype: user
58     permissions: rx
59     recursive: yes
60     state: present
61
62 - name: check instance cert
63   stat:
64     path: "{{ cert_path }}/{{ _cert }}"
65   register: cert
66
67 - name: copy CA to {{ cert_path }}
68   copy:
69     src: "/etc/openssl/ca.pem"
70     dest: "{{ cert_path }}/ca.pem"
71   when: not cert_path_register.stat.exists
72
73 - name: generate instance certificate
74   command: "{{ item }}"
75   with_items:
76     - "/usr/bin/openssl genrsa -out {{ _key }} 2048"
77     - "/usr/bin/openssl req -new -key {{ _key }} -out {{ instance }}.csr -subj '{{ _subject }}' {% if _common_key is sameas false %} -config /etc/openssl/{{ _conf_file }} {% endif %} -sha256"
78     - "/usr/bin/openssl x509 -req -in {{ instance }}.csr -CA ca.pem -CAserial {{ instance }}.slr -CAkey /etc/openssl/ca-key.pem -CAcreateserial -out {{ _cert }} -days {{ _expiry }} -extensions v3_req -extfile /etc/openssl/{{ _conf_file }} -sha256"
79   args:
80     chdir: "{{ cert_path }}"
81   when: not cert.stat.exists
82
83 - name: load certificate into secret
84   command: "kubectl -n {{ _secret_ns }} create secret {{ _secret_type }}  {{ _secret_name }} --cert={{ cert_path }}/{{ _cert }} --key={{ cert_path }}/{{ _key }}"
85   when: _secret_name != ''
86
87 - name: Unconditionally delete files in case of secrets
88   set_fact:
89     _keep_files: "{{ false if _secret_name != '' else _keep_files }}"
90
91 - name: create kubeconfig from cert
92   include_role:
93     name: kubeconfig
94   vars:
95     config:
96       path: "{{ item.path }}"
97       owner: "{{ item.owner | default('root') }}"
98       group: "{{ item.group | default('root') }}"
99       restricted: "{{ item.restricted | default(true) }}"
100       user: "{{ _cn }}"
101       cert: "{{ cert_path }}/{{ _cert }}"
102       key: "{{ cert_path }}/{{ _key }}"
103       apiserver: "{{ item.apiserver }}"
104       apiserver_port: "{{ item.apiserver_port }}"
105       add_users: "{{ add_users | default([]) }}"
106   with_items: "{{ kube_conf | default([]) }}"
107
108 - name: Unconditionally delete files in case of kubeconfig
109   set_fact:
110     _keep_files = "{{ false if kube_conf is defined else _keep_files }}"
111
112 - name: SECURITY settings on cert files
113   block:
114     - name: reducing permission of key file and cert file
115       file:
116         path: "{{ cert_path }}/{{ item }}"
117         mode: 0000
118       with_items:
119         - "{{ _key }}"
120         - "{{ _cert }}"
121       when: not cert.stat.exists
122
123     - name: remove cert request and serial file
124       file:
125         path: "{{ cert_path }}/{{ item }}"
126         state: absent
127       with_items:
128         - "{{ instance }}.csr"
129         - "{{ instance }}.slr"
130       when: not cert.stat.exists
131
132     - name: setting ca.pem permission
133       file:
134         path: "{{ cert_path }}/ca.pem"
135         mode: 0000
136       when: not cert_path_register.stat.exists
137
138     - name: adding default acl read to {{ users.admin_user_name }} to {{ cert_path }}/ca.pem
139       acl:
140         name:  "{{ cert_path }}/ca.pem"
141         entity: "{{ users.admin_user_name }}"
142         etype: user
143         permissions: rx
144         state: present
145
146     - name: allowing users to access keys
147       acl:
148         name: "{{ item[0] }}"
149         entity: "{{ item[1] }}"
150         etype: user
151         permissions: "r"
152         state: present
153       with_nested:
154         - [ "{{ cert_path }}/{{ _key }}", "{{ cert_path }}/{{ _cert }}", "{{ cert_path }}/ca.pem" ]
155         - "{{ add_users | default([]) }}"
156
157     - name: adding exec flag to {{ cert_path }} directory for users
158       acl:
159         name: "{{ cert_path }}"
160         entity: "{{ item }}"
161         etype: user
162         permissions: "rx"
163         state: present
164       with_items: "{{ add_users | default([]) }}"
165
166     - name: adding mask to the acl
167       acl:
168         name: "{{ cert_path }}"
169         etype: mask
170         permissions: "rx"
171         recursive: yes
172         state: present
173   when: _keep_files
174
175 - name: Remove directory in case of _keep_files==false
176   file:
177     name: "{{ cert_path }}"
178     state: absent
179   when: not _keep_files
180
181 - name: force IO to write data to disk
182   shell: "sync"