Skip to content

Commit

Permalink
Add another testcase for multiple sudoers
Browse files Browse the repository at this point in the history
Signed-off-by: Radovan Sroka <[email protected]>
  • Loading branch information
radosroka committed May 17, 2024
1 parent eed07ed commit c977c1b
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
include_role:
name: linux-system-roles.sudo
vars:
sudoers_rewrite_default_sudoers_file: True
sudoers_remove_unauthorized_included_files: True
sudoers_files:
sudo_rewrite_default_sudoers_file: True
sudo_remove_unauthorized_included_files: True
sudo_sudoers_files:
- path: /etc/sudoers
defaults:
- "!visiblepw"
Expand Down Expand Up @@ -79,7 +79,7 @@
user_alias:
- name: PINGERS
users:
- ahuffman
- username
- path: /etc/sudoers.d/pingers
user_specifications:
- type: user
Expand Down
1 change: 0 additions & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
mode: 0440
validate: "{{ sudo_visudo_path }} -cf %s"
with_items: "{{ sudo_sudoers_files }}"
# become: True
loop_control:
label: "{{ item.path }}"
when: item.path != '/etc/sudoers'
Expand Down
2 changes: 1 addition & 1 deletion templates/sudoers.j2
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Runas_Alias {{ ra.name }} = {{ _runas_alias | join(", ") }}
## User Aliases
{% for ua in item.aliases.user_alias %}
{% set _user_alias = [] %}
{% for user in ya.users %}
{% for user in ua.users %}
{% set _ = _user_alias.append(user) %}
{% endfor %}
User_Alias {{ ua.name }} = {{ _user_alias | join(", ") }}
Expand Down
25 changes: 25 additions & 0 deletions tests/files/tests_multiple_sudoers.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#
# Ansible managed
#

# Default specifications
Defaults !visiblepw
Defaults always_set_home
Defaults match_group_by_gid
Defaults always_query_group_plugin
Defaults env_reset
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Defaults env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR"
Defaults env_keep += "LS_COLORS MAIL PS1 PS2 QTDIR"
Defaults env_keep += "USERNAME LANG LC_ADDRESS LC_CTYPE LC_COLLATE"
Defaults env_keep += "LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME"
Defaults env_keep += "LC_NUMERIC LC_PAPER LC_TELEPHONE LC_TIME LC_ALL"
Defaults env_keep += "LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

# User specifications
root ALL=(ALL) ALL
%wheel ALL=(ALL) ALL

# Includes
## Include directories
#includedir /etc/sudoers.d
11 changes: 11 additions & 0 deletions tests/files/tests_multiple_sudoers_pingers.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#
# Ansible managed
#
# system_role:sudo




# Default override specifications
Defaults: PINGERS !requiretty

13 changes: 13 additions & 0 deletions tests/files/tests_multiple_sudoers_root.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
# Ansible managed
#
# system_role:sudo

# Default specifications
Defaults syslog=auth



# Default override specifications
Defaults> root !set_logname

35 changes: 35 additions & 0 deletions tests/files/tests_multiple_sudoers_sudoers.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# Ansible managed
#
# system_role:sudo

# Default specifications
Defaults !visiblepw
Defaults always_set_home
Defaults match_group_by_gid
Defaults always_query_group_plugin
Defaults env_reset
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Defaults env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR"
Defaults env_keep += "LS_COLORS MAIL PS1 PS2 QTDIR"
Defaults env_keep += "USERNAME LANG LC_ADDRESS LC_CTYPE LC_COLLATE"
Defaults env_keep += "LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME"
Defaults env_keep += "LC_NUMERIC LC_PAPER LC_TELEPHONE LC_TIME LC_ALL"
Defaults env_keep += "LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

# Alias specifications
## Command Aliases
Cmnd_Alias PING = /bin/ping



## User Aliases
User_Alias PINGERS = username

# User specifications
root ALL=(ALL) ALL
%wheel ALL=(ALL) ALL

# Includes
## Include directories
#includedir /etc/sudoers.d
162 changes: 162 additions & 0 deletions tests/tests_multiple_sudoers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
---
- name: Basic test for Sudo
hosts: all
tasks:
- name: Run tests
block:
- name: Run the role
include_role:
name: linux-system-roles.sudo
vars:
sudo_rewrite_default_sudoers_file: True
sudo_remove_unauthorized_included_files: True
sudo_sudoers_files:
- path: /etc/sudoers
defaults:
- "!visiblepw"
- always_set_home
- match_group_by_gid
- always_query_group_plugin
- env_reset
- secure_path:
- /sbin
- /bin
- /usr/sbin
- /usr/bin
- env_keep:
- COLORS
- DISPLAY
- HOSTNAME
- HISTSIZE
- KDEDIR
- LS_COLORS
- MAIL
- PS1
- PS2
- QTDIR
- USERNAME
- LANG
- LC_ADDRESS
- LC_CTYPE
- LC_COLLATE
- LC_IDENTIFICATION
- LC_MEASUREMENT
- LC_MESSAGES
- LC_MONETARY
- LC_NAME
- LC_NUMERIC
- LC_PAPER
- LC_TELEPHONE
- LC_TIME
- LC_ALL
- LANGUAGE
- LINGUAS
- _XKB_CHARSET
- XAUTHORITY
user_specifications:
- users:
- root
hosts:
- ALL
operators:
- ALL
commands:
- ALL
- users:
- %wheel
hosts:
- ALL
operators:
- ALL
commands:
- ALL
include_directories:
- /etc/sudoers.d
aliases:
cmnd_alias:
- name: PING
commands:
- /bin/ping
user_alias:
- name: PINGERS
users:
- ahuffman
- path: /etc/sudoers.d/pingers
user_specifications:
- type: user
defaults:
- "!requiretty"
users:
- PINGERS
- path: /etc/sudoers.d/root
defaults:
- syslog=auth
user_specifications:
- type: runas
defaults:
- "!set_logname"
operators:
- root

- name: Create temp test directory
tempfile:
path: /var/tmp
prefix: sudo_
state: directory
register: __sudo_tmpdir

- name: Backup sudoers
copy:
src: /etc/sudoers
dest: "{{ __sudo_tmpdir.path }}/sudoers"
owner: root
group: root
mode: 0644
remote_src: true

- name: Backup sudoers.d
copy:
src: /etc/sudoers.d
dest: "{{ __sudo_tmpdir.path }}/sudoers.d"
owner: root
group: root
mode: 0644
remote_src: true

# sha256sum ./files/test_multiple_sudoers_sudoers.ok
- name: Check sudoers
command: >-
sha256sum /etc/sudoers | sha256sum --check 5be0fd4d601eaa7ae037045f7333d935520117b6b81f4605f1079cd29f472d0c
# sha256sum ./files/test_multiple_sudoers_pingers.ok
- name: Check pingers
command: >-
sha256sum /etc/sudoers.d/pingers | sha256sum --check 381c8fec4c1aa100be800f6640a12010319ec44b8da72fa39d2558ecc381d41d
# sha256sum ./files/test_multiple_sudoers_root.ok
- name: Check root
command: >-
sha256sum /etc/sudoers.d/root | sha256sum --check 6a4a84012548edf0ee995c126e7329fe1fea62bbc746ec4efc2d664f387b92ba
- name: Restore sudoers
copy:
src: "{{ __sudo_tmpdir.path }}/sudoers"
dest: /etc/sudoers
owner: root
group: root
mode: 0644
remote_src: true

- name: Restore sudoers.d
copy:
src: "{{ __sudo_tmpdir.path }}/sudoers.d"
dest: /etc/sudoers.d
owner: root
group: root
mode: 0644
remote_src: true

- name: Clean up temp directory
file:
path: "{{ __sudo_tmpdir.path }}"
state: absent

0 comments on commit c977c1b

Please sign in to comment.