a23996c50361d104596e95ebed2af7093657874f
[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: reducing permission of key file and cert file
84   file:
85     path: "{{ cert_path }}/{{ item }}"
86     mode: 0000
87   with_items:
88     - "{{ _key }}"
89     - "{{ _cert }}"
90   when: not cert.stat.exists
91
92 - name: remove cert request and serial file
93   file:
94     path: "{{ cert_path }}/{{ item }}"
95     state: absent
96   with_items:
97     - "{{ instance }}.csr"
98     - "{{ instance }}.slr"
99   when: not cert.stat.exists
100
101 - name: setting ca.pem permission
102   file:
103     path: "{{ cert_path }}/ca.pem"
104     mode: 0000
105   when: not cert_path_register.stat.exists
106
107 - name: adding default acl read to {{ users.admin_user_name }} to {{ cert_path }}/ca.epm
108   acl:
109     name:  "{{ cert_path }}/ca.pem"
110     entity: "{{ users.admin_user_name }}"
111     etype: user
112     permissions: rx
113     state: present
114
115 - name: allowing users to access keys
116   acl:
117     name: "{{ item[0] }}"
118     entity: "{{ item[1] }}"
119     etype: user
120     permissions: "r"
121     state: present
122   with_nested:
123     - [ "{{ cert_path }}/{{ _key }}", "{{ cert_path }}/{{ _cert }}", "{{ cert_path }}/ca.pem" ]
124     - "{{ add_users | default([]) }}"
125
126 - name: adding exec flag to {{ cert_path }} directory for users
127   acl:
128     name: "{{ cert_path }}"
129     entity: "{{ item }}"
130     etype: user
131     permissions: "rx"
132     state: present
133   with_items: "{{ add_users | default([]) }}"
134
135 - name: create kubeconfig from cert
136   include_role:
137     name: kubeconfig
138   vars:
139     config:
140       path: "{{ item.path }}"
141       owner: "{{ item.owner | default('root') }}"
142       group: "{{ item.group | default('root') }}"
143       restricted: "{{ item.restricted | default(true) }}"
144       user: "{{ _cn }}"
145       cert: "{{ cert_path }}/{{ _cert }}"
146       key: "{{ cert_path }}/{{ _key }}"
147       apiserver: "{{ item.apiserver }}"
148       apiserver_port: "{{ item.apiserver_port }}"
149       add_users: "{{ add_users | default([]) }}"
150   with_items: "{{ kube_conf | default([]) }}"
151
152 - name: force IO to write data to disk
153   shell: "sync"