Initial commit
[ta/infra-ansible.git] / roles / ops-hardening / tasks / main.yaml
1 ---
2
3 # Copyright 2019 Nokia
4
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 #
18 # Linux password hardening
19 #
20
21 - name: "Set Password Strength Minimum Digit Characters."
22   lineinfile:
23     path: /etc/security/pwquality.conf
24     regexp: '^[#\s]*dcredit'
25     line: 'dcredit = -1'
26
27 - name: "Set Password Minimum Length."
28   lineinfile:
29     path: /etc/security/pwquality.conf
30     regexp: '^[#\s]*minlen'
31     line: 'minlen = 8'
32
33 - name: "Set Password Strength Minimum Uppercase Characters."
34   lineinfile:
35     path: /etc/security/pwquality.conf
36     regexp: '^[#\s]*ucredit'
37     line: 'ucredit = -1'
38
39 - name: "Set Password Strength Minimum Special Characters."
40   lineinfile:
41     path: /etc/security/pwquality.conf
42     regexp: '^[#\s]*ocredit'
43     line: 'ocredit = -1'
44
45 - name: "Set Password Strength Minimum Lowercase Characters."
46   lineinfile:
47     path: /etc/security/pwquality.conf
48     regexp: '^[#\s]*lcredit'
49     line: 'lcredit = -1'
50
51 - name: "Set Password Strength Minimum Different Categories."
52   lineinfile:
53     path: /etc/security/pwquality.conf
54     regexp: '^[#\s]*minclass'
55     line: 'minclass = 3'
56
57 - name: "Set Password Minimum Length in login.defs"
58   lineinfile:
59     path: /etc/login.defs
60     regexp: '^PASS_MIN_LEN[\s]*[0-9]*$'
61     line: 'PASS_MIN_LEN   8'
62
63 - name: "Set Password Minimum Age"
64   lineinfile:
65     path: /etc/login.defs
66     regexp: '^PASS_MIN_DAYS[\s]*[0-9]*$'
67     line: 'PASS_MIN_DAYS   0'
68
69 #
70 # YUM config
71 #
72
73 - name: "Ensure YUM Removes Previous Package Versions"
74   lineinfile:
75     path: /etc/yum.conf
76     insertafter: '^[#\s]*\[main\]'
77     line: 'clean_requirements_on_remove = 1'
78
79 - name: "Ensure gpgcheck Enabled for Local Packages"
80   lineinfile:
81     path: /etc/yum.conf
82     insertafter: '^[#\s]*\[main\]'
83     line: 'localpkg_gpgcheck = 1'
84
85 #
86 # Setting Ctrl-Alt-Del action
87 #
88
89 - name: "Disable Ctrl-Alt-Del Burst Action"
90   lineinfile:
91     path: /etc/systemd/system.conf
92     insertafter: '^[#\s]*CtrlAltDelBurstAction'
93     line: 'CtrlAltDelBurstAction=none'
94
95 - name: "Disable Ctrl-Alt-Del Reboot Activation"
96   command: systemctl mask ctrl-alt-del.target
97
98 #
99 # Configure kernel modules
100 #
101
102 - name: "kernel module setting"
103   lineinfile:
104     create=yes
105     dest="/etc/modprobe.d/{{item}}.conf"
106     regexp="{{item}}"
107     line="install {{item}} /bin/true"
108   with_items:
109     - bluetooth
110     - dccp
111     - squashfs
112     - hfsplus
113     - hfs
114     - jffs2
115     - freevxfs
116     - cramfs
117     - usb-storage
118     - udf
119
120 #
121 # Disable interactive boot
122 #
123
124 - name:  Verify that Interactive Boot is Disabled GRUB_CMDLINE_LINUX Setting
125   lineinfile:
126     path: /etc/default/grub
127     backrefs: yes
128     regexp: '^GRUB_CMDLINE_LINUX=(.*)systemd\.confirm_spawn=(1|yes|true|on)\s*(.*)$'
129     line: 'GRUB_CMDLINE_LINUX=\1\3'
130
131 - name:  Verify that Interactive Boot is Disabled GRUB_CMDLINE_LINUX_DEFAULT Setting
132   lineinfile:
133     path: /etc/default/grub
134     backrefs: yes
135     regexp: '^GRUB_CMDLINE_LINUX_DEFAULT=(.*)systemd\.confirm_spawn=(1|yes|true|on)\s*(.*)$'
136     line: 'GRUB_CMDLINE_LINUX_DEFAULT=\1\3'
137
138 #
139 # Set file permissions
140 #
141
142 - name: "Set set the 600 file permissions"
143   file:
144     path: "{{item}}"
145     state: touch
146     mode: 600
147   with_items:
148     - /boot/grub2/grub.cfg
149     - /var/log/boot.log
150     - /var/log/cron
151
152 #
153 # Disable direct root login
154 #
155
156 - name: "Direct root Logins Not Allowed"
157   shell: echo > /etc/securetty
158
159 #
160 # Configure IPv6
161 #
162
163 - name: Disable ipv6 support if the ipv6 is not needed
164   when: ansible_default_ipv6|length == 0
165   sysctl:
166     name: net.ipv6.conf.all.disable_ipv6
167     value: 1
168     state: present
169     reload: yes
170
171 - name: Disable Support for udp6
172   when: ansible_default_ipv6|length == 0
173   lineinfile:
174     path: /etc/netconfig
175     state: absent
176     regexp: '^udp6.*'
177
178 - name: Disable Support for tcp6
179   when: ansible_default_ipv6|length == 0
180   lineinfile:
181     path: /etc/netconfig
182     state: absent
183     regexp: '^tcp6.*'
184
185 - name: Disable automatic ipv6 configuration
186   when: ansible_default_ipv6|length > 0
187   sysctl:
188     name: "{{ item.name }}"
189     value: "{{ item.value }}"
190     state: present
191     reload: yes
192   with_items:
193     - { name: 'net.ipv6.conf.all.accept_source_route', value: 0 }
194     - { name: 'net.ipv6.conf.all.accept_ra', value: 0 }
195     - { name: 'net.ipv6.conf.default.accept_ra', value: 0 }
196     - { name: 'net.ipv6.conf.all.accept_redirects', value: 0 }
197     - { name: 'net.ipv6.conf.default.accept_redirects', value: 0 }
198     - { name: 'net.ipv6.conf.default.accept_source_route', value: 0 }
199     - { name: 'net.ipv6.conf.all.forwarding', value: 0 }
200
201 #
202 # Configure kernel parameters
203 #
204
205 - name: Configure the kernel parameters
206   sysctl:
207     name: "{{ item.name }}"
208     value: "{{ item.value }}"
209     state: present
210     reload: yes
211   with_items:
212     - { name: 'net.ipv4.conf.default.send_redirects', value: 0 }
213     - { name: 'net.ipv4.conf.all.send_redirects', value: 0 }
214     - { name: 'net.ipv4.ip_forward', value: 0 }
215     - { name: 'net.ipv4.conf.all.accept_redirects', value: 0 }
216     - { name: 'net.ipv4.conf.all.secure_redirects', value: 0 }
217     - { name: 'net.ipv4.conf.all.log_martians', value: 1 }
218     - { name: 'net.ipv4.conf.default.log_martians', value: 1 }
219     - { name: 'net.ipv4.conf.default.accept_redirects', value: 0 }
220     - { name: 'net.ipv4.conf.default.secure_redirects', value: 0 }
221     - { name: 'net.ipv4.icmp_echo_ignore_broadcasts', value: 1 }
222     - { name: 'net.ipv4.icmp_ignore_bogus_error_responses', value: 1 }
223     - { name: 'net.ipv4.tcp_syncookies', value: 1 }
224     - { name: 'fs.suid_dumpable', value: 0 }
225     - { name: 'kernel.dmesg_restrict', value: 1 }
226     - { name: 'kernel.core_uses_pid', value: 1 }
227     - { name: 'kernel.randomize_va_space', value: 2 }
228     - { name: 'kernel.core_pattern', value: '/var/core/core'}
229
230 #
231 # Configure core dump
232 #
233
234 - name: "Disable core dump for all user"
235   lineinfile:
236     path: /etc/security/limits.conf
237     insertbefore: '^[a-z].*'
238     line: '*               hard    core            0'
239
240 - name: "Configure systemd not to store core dumps"
241   lineinfile:
242     path: /etc/systemd/coredump.conf
243     insertafter: '^\[Coredump\]'
244     line: 'Storage=none'
245
246 #
247 # Configure syslog
248 #
249 - name: "Stop rsyslog Service"
250   shell: systemctl stop rsyslog.service
251
252 - name: "Disable rsyslog Service"
253   shell: systemctl disable rsyslog.service
254
255 - name: "Ensure the /var/log/boot.log Rotated by logrotate"
256   lineinfile:
257     path: /etc/logrotate.d/syslog
258     insertbefore: 'cron$'
259     line: /var/log/boot.log
260
261 - name: "Set the umasks by profile file"
262   lineinfile:
263     path: /etc/profile
264     regexp: '{{ item.old }}'
265     line: '{{ item.new }}'
266   with_items:
267     - { old: 'umask 002', new: umask 027 }
268     - { old: 'umask 022', new: umask 077 }
269
270 #
271 # Keystone config
272 #
273
274 - name: Set the max_request_body_size in the keystone.conf
275   lineinfile:
276     path: /etc/keystone/keystone.conf
277     insertafter: 'DEFAULT'
278     line: "# enforced by optional sizelimit middleware (keystone.middleware:RequestBodySizeLimiter)\nmax_request_body_size = 114688\n"
279
280 - name: Set the insecure_debug in the keystone.conf
281   lineinfile:
282     path: /etc/keystone/keystone.conf
283     insertafter: 'DEFAULT'
284     line: "# If set to true, then the server will return information in HTTP responses\n# that may allow an unauthenticated or authenticated user to get more\n# information than normal, such as additional details about why authentication\n# failed. This may be useful for debugging but is insecure. (boolean value)\ninsecure_debug = false\n"
285
286 #
287 #Setting bootloader password
288 #
289 - name: set host os variable
290   when: host_os is defined
291   set_fact:
292     grub2_pass: "{{  host_os.grub2_password | default('Empty')  }}"
293
294 - name: protect grub with root password
295   when: grub2_pass is defined and grub2_pass != 'Empty'
296   blockinfile:
297     dest: /etc/grub.d/40_custom
298     state: present
299     insertafter: 'EOF'
300     content: |
301       # define superusers
302       set superusers="root"
303       #define users
304       password_pbkdf2 root "{{ grub2_pass }}"
305
306 - name: generate grub config
307   when: grub2_pass is defined and grub2_pass != 'Empty'
308   command: /usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg
309
310 #
311 #Setting the noexec option to the /dev/shm mount dir
312 #
313
314 - name: get back device associated to mountpoint
315   shell: mount | grep ' /dev/shm ' |cut -d ' ' -f 1
316   register: device_name
317   check_mode: no
318
319 - name: get back device previous mount option
320   shell: mount | grep ' /dev/shm ' | sed -re 's:.*\((.*)\):\1:'
321   register: device_cur_mountoption
322   check_mode: no
323
324 - name: get back device fstype
325   shell: mount | grep ' /dev/shm ' | cut -d ' ' -f 5
326   register: device_fstype
327   check_mode: no
328
329 - name: Ensure permission noexec are set on /dev/shm
330   mount:
331     path: "/dev/shm"
332     src: "{{device_name.stdout}}"
333     opts: "{{device_cur_mountoption.stdout}},noexec"
334     state: "mounted"
335     fstype: "{{device_fstype.stdout}}"
336
337 #
338 # Setting file permissions
339 #
340
341 #- name: "Remove the other user write permission from the system directorys"
342 #  command: find / -xdev \( -perm -0002 -a ! -perm -1000 \) -type d -exec chmod o-w {} \;
343 #
344 #- name: "Remove the other user write permission from the system files"
345 #  command: find / -xdev -perm -0002 -type f -exec chmod o-w {} \;
346 #
347 #- name: "Modified the unauthorized SUID/SGID system executables"
348 #  command: sudo chmod -s $(sudo find / -xdev \( -perm -4000 -o -perm -2000 \) -type f | grep -v sudo)