Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom ports #38

Merged
merged 43 commits into from Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
050939f
Add support for custom ports
May 10, 2021
01bf854
typo
May 10, 2021
23c512f
changed exist to exists now it all works
May 10, 2021
59aa205
made the requested changes
Jun 9, 2021
eea9d5e
fix
Jun 9, 2021
99cd26d
type-o
Jun 9, 2021
f91df23
requested changes were implemented
Aug 23, 2021
42af42a
removed the rhel-system-roles
Aug 23, 2021
740d068
Merge branch 'linux-system-roles:master' into master
lessfoobar Aug 9, 2022
81d01c5
swapped firewalld module with the LSR.firewall
lessfoobar Aug 9, 2022
9d6defb
removed FQDN for import_role
lessfoobar Aug 9, 2022
b7b3c71
removed by requiest
lessfoobar Aug 9, 2022
bfa7193
fixed the FQDN for the selinux and firewall roles
lessfoobar Aug 9, 2022
1295970
add requirements for the fedora.linux_system_roles
lessfoobar Aug 9, 2022
2144329
update the readme with requirements section
lessfoobar Aug 9, 2022
bf8d525
address some of the linter issues
richm Aug 10, 2022
aa07654
fix more lint issues
richm Aug 10, 2022
05c94b7
fix lint issues
richm Aug 10, 2022
677c1f7
remove extra blank line
richm Aug 10, 2022
51b443a
swap blockinfile to template usage
lessfoobar Aug 11, 2022
9fac310
add template for custom port
lessfoobar Aug 11, 2022
1c81ed7
add when statement against the default values
lessfoobar Aug 11, 2022
91272a8
test firewall changes
lessfoobar Aug 11, 2022
208cf90
add TCP check on listening port
lessfoobar Aug 11, 2022
638c75a
Added comments for the tasks in the test
lessfoobar Aug 15, 2022
487f868
fix yamllint, style issues
richm Aug 19, 2022
80c4b01
fix yamlint issues
richm Aug 19, 2022
7530e3f
more yamllint issues
richm Aug 19, 2022
1327743
fix more yamllint issues
richm Aug 19, 2022
71644f7
fixed yamllint errors
Sep 7, 2022
a01a3bb
Merge branch 'master' of https://github.com/lessfoobar/nbde_server
Sep 7, 2022
a9aff14
Merge branch 'linux-system-roles:master' into master
lessfoobar Sep 7, 2022
2cd1a28
fixing merger errors
Sep 7, 2022
1a26448
Merge branch 'master' of https://github.com/lessfoobar/nbde_server
Sep 7, 2022
cd3fb90
fixed role name
Sep 8, 2022
efb96d3
add fixes suggested by nhosoi
lessfoobar Sep 14, 2022
6f22e1c
fix for port possibility as int
Sep 15, 2022
c112230
Merge branch 'master' of https://github.com/lessfoobar/nbde_server
Sep 15, 2022
344462b
implement the proposed changes from richm & nhosoi
Sep 27, 2022
b515a68
sorry I have word wrap and its hard to follow this 80 chars per line
lessfoobar Sep 27, 2022
ee98762
yeap forgot the empty space on the two lines. Fixed now
lessfoobar Sep 27, 2022
0b32b1e
remove the stat and moved the templates shortcut
lessfoobar Sep 28, 2022
0dbc506
added the int conversion
lessfoobar Sep 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions README.md
Expand Up @@ -11,11 +11,15 @@ Supported Distributions
* RHEL-7+, CentOS-7+
* Fedora

Limitations
-----------
Requirements
------------

It is not currently possible to specify a custom port for the NBDE servers configured by this role.
The role requires additional collections which are specified in `meta/collection-requirements.yml`. These are not automatically installed. You must install them like this:

`ansible-galaxy install -vv -r meta/collection-requirements.yml`

Limitations
-----------

Role Variables
--------------
Expand All @@ -30,6 +34,9 @@ These are the variables that can be passed to the role:
|`nbde_server_fetch_keys`| `no` | indicates whether we should fetch keys to the control node, in which case they will be placed in `nbde_server_keys_dir`. You **must** set `nbde_server_keys_dir` to use `nbde_server_fetch_keys`.
|`nbde_server_deploy_keys`| `no` |indicates whether we should deploy the keys located in `nbde_server_keys_dir` directory to the remote hosts. You **must** set `nbde_server_keys_dir` to use `nbde_server_deploy_keys`.
|`nbde_server_keys_dir`| | specifies a directory in the control node that contains keys to be deployed to the remote hosts. Keys located in the top level directory will be deployed to every remote host, while keys located within subdirectories named after the remote hosts -- as per the inventory -- will be deployed only to these specific hosts. `nbde_server_keys_dir` **must** be an absolute path. You need to set this to use either `nbde_server_fetch_keys` and/or `nbde_server_deploy_keys`.
|`nbde_server_port`|`80`| setup custom port which will be enabled in SELinux and firewalld.
|`nbde_server_firewall_zone`|`public`| change the default zone where the port should be opened.



#### nbde_server_fetch_keys and nbde_server_deploy_keys
Expand Down Expand Up @@ -108,6 +115,18 @@ To redeploy keys, they must be placed into subdirectories named after the host t
- linux-system-roles.nbde_server
```

#### Example 5: deploy NBDE server with custom port and zone
```yaml
---
- hosts: all

vars:
nbde_server_port: 7500
nbde_server_firewall_zone: dmz
roles:
- linux-system-roles.nbde_server
```

License
-------

Expand Down
10 changes: 10 additions & 0 deletions defaults/main.yml
Expand Up @@ -34,4 +34,14 @@ nbde_server_deploy_keys: no
# to use either nbde_server_fetch_keys and/or nbde_server_deploy_keys.
nbde_server_keys_dir: ""

# nbde_server_port indicate the port that nbde_server's firewall should open.
# The default port is 80. Tang uses the TCP protocol. Thus the specified port
# will listen on the nbde_server_port/TCP
nbde_server_port: 80

# nbde_server_firewall_zone indicate the zone that nbde_server's firewall
# should write into. The default zone is public. If that is not the case you
# can change that here. Usually it should be left like that.
nbde_server_firewall_zone: public

# vim:set ts=2 sw=2 et:
3 changes: 3 additions & 0 deletions meta/collection-requirements.yml
@@ -0,0 +1,3 @@
---
collections:
- name: fedora.linux_system_roles
4 changes: 4 additions & 0 deletions tasks/main-tang.yml
Expand Up @@ -26,6 +26,10 @@
when: (nbde_server_fetch_keys | bool) or (nbde_server_deploy_keys | bool)
include_tasks: tang-key-management.yml

- name: Open port in firewalld
when: (nbde_server_port != 80) or (nbde_server_firewall_zone != "public")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
when: (nbde_server_port != 80) or (nbde_server_firewall_zone != "public")
when: (nbde_server_port | int != 80) or (nbde_server_firewall_zone != "public")

This should fix the error from the log:

ERROR: The following numeric operations found at runtime need int or float cast filters:
[nbde_server_port] [ne] [80] at tasks/main-tang.yml:29

the issue is that since nbde_server_port is a public variable, there is no guarantee that the data type will be int e.g. if the user does ansible-playbook -e nbde_server_port=80 ... then the variable will be a string data type and the comparison (nbde_server_port != 80) will always fail. Using | int to "cast" the type will ensure that the comparison is done correctly, and will also ensure that an error is generated if the data type cannot be cast to int.

include_tasks: tangd-custom-port.yml

- name: Ensure required services are enabled and at the right state
service:
name: "{{ item }}"
Expand Down
59 changes: 59 additions & 0 deletions tasks/tangd-custom-port.yml
@@ -0,0 +1,59 @@
---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are changing the way we use system roles from other system roles - https://linux-system-roles.github.io/documentation/role-requirements.html

The short answer is

  • you'll need a meta/collection-requirements.yml like this
collections:
  - fedora.linux_system_roles
  • refer to the selinux and firewall system roles by the collection FQCN
- name: Allow the custom port for tangd_port_t in SELinux
  import_role:
    name: fedora.linux_system_roles.selinux
  vars:
...

# This task tells SELinux for the port that we want the tangd
# service to use when distribution Fedora
- name: Allow the custom port for tangd_port_t in SELinux
import_role:
name: fedora.linux_system_roles.selinux
vars:
selinux_ports:
- ports: "{{ nbde_server_port }}"
proto: tcp
setype: tangd_port_t
state: present

# This block creates the override file for systemd with the new
# port that we have requested
- name: Create override file
block:
# This task checks if the directory /etc/systemd/system/tangd.socket.d
# exist and registers the value in systemd__system__tangd_socket
- name: Check if directory /etc/systemd/system/tangd.socket.d exist
stat:
path: /etc/systemd/system/tangd.socket.d
register: systemd_system_tangd_socket

# This tasks Create the /etc/systemd/system/tangd.socket.d directory if
# it does not exist
- name: Create a directory if it does not exist
file:
path: /etc/systemd/system/tangd.socket.d
state: directory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will already create the directory if it does not exist, no? There is no need to check/register in the previous task and run this conditionally here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

mode: '0755'
when: not systemd_system_tangd_socket.stat.exists

# This tasks creates the file with the port entry that we want tangd to
# listen to
- name: Creates the file with the port entry that we want tangd to listen to
template:
src: tangd_socket_override.conf.j2
dest: /etc/systemd/system/tangd.socket.d/override.conf
backup: true
mode: '0644'
register: __nbde_server_daemon_reload

# This tasks reload the daemons so the new changes take effect
- name: Reload the daemons so the new changes take effect
systemd:
daemon_reload: true
when: __nbde_server_daemon_reload is changed

- name: Ensure the desired port is added to firewalld
import_role:
name: fedora.linux_system_roles.firewall
vars:
firewall:
- port: "{{ nbde_server_port }}/tcp"
zone: "{{ nbde_server_firewall_zone }}"
state: enabled
immediate: true
permanent: true
4 changes: 4 additions & 0 deletions templates/tangd_socket_override.conf.j2
@@ -0,0 +1,4 @@
{{ ansible_managed | comment }}
[Socket]
ListenStream=
ListenStream={{ nbde_server_port }}
1 change: 1 addition & 0 deletions tests/templates
45 changes: 45 additions & 0 deletions tests/tests_tangd_custom_port.yml
@@ -0,0 +1,45 @@
---
- name: Test tangd_custom_port
hosts: all
vars:
nbde_server_port: 7500
nbde_server_firewall_zone: public
tasks:
- name: install with custom port and firewall zone
import_role:
name: linux-system-roles.nbde_server

- name: check if port is open
shell:
cmd: |-
set -euo pipefail
ss -tulpn | grep {{ nbde_server_port }} | awk -F' ' '{print $5}'
register: __open_ports_output
failed_when: not __open_ports_output.stdout is
search(':' ~ (nbde_server_port | string) ~ '$')

- name: check if port TCP is open
shell:
cmd: |-
set -euo pipefail
ss -tulpn | grep {{ nbde_server_port }} | awk -F' ' '{print $1}'
register: __open_ports_output
failed_when: __open_ports_output.stdout != "tcp"

- name: check if port is opened in firewall
command: >-
firewall-cmd --zone {{ nbde_server_firewall_zone }} --query-port
{{ nbde_server_port }}/tcp
register: __firewall_output

- name: check if firewall zone is set
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this check at all - the firewall-cmd --zone {{ nbde_server_firewall_zone }} --query-port {{ nbde_server_port }}/tcp will check that the given zone exists and that the port is set in the given zone.

shell:
cmd: |-
set -euo pipefail
firewall-cmd --list-all | grep {{ nbde_server_firewall_zone }} | \
awk -F' ' '{print $1}'
register: __firewall_output_zone
failed_when: >-
__firewall_output_zone.stdout != "{{ nbde_server_firewall_zone }}"

# vim:set ts=2 sw=2 et: