From 3ed00b733eb9fb13b5d8046d7489d5de6c869ba0 Mon Sep 17 00:00:00 2001 From: Jon Date: Sun, 25 Feb 2024 16:19:59 +0930 Subject: [PATCH] feat(server): Convert scanned time within scan report to UTC !13 closes #13 --- .../collection/phpipam_scan_agent/index.md | 4 ++ .../collection/phpipam_scan_agent/server.md | 4 ++ playbooks/tasks/server/ipaddress.yaml | 38 ++++++++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/docs/projects/ansible/collection/phpipam_scan_agent/index.md b/docs/projects/ansible/collection/phpipam_scan_agent/index.md index b94082d..2dce13f 100644 --- a/docs/projects/ansible/collection/phpipam_scan_agent/index.md +++ b/docs/projects/ansible/collection/phpipam_scan_agent/index.md @@ -32,6 +32,8 @@ This collection is available on Ansible Galaxy and can be installed with `ansibl Prefer to use our [docker](docker.md) image? It's available on Docker Hub `docker pull nofusscomputing/phpipam-scan-agent:latest`. +There is no timezone support within phpIPAM. Due to this it's recommended that you setup phpIPAM, the database and the scanner component to use UTC (GMT +00:00) timezone or that all components be setup to use the same timezone. + ## Features @@ -54,6 +56,8 @@ The following features are available or planned to be implmented: - [**ToDo** Resolve DNS names](https://gitlab.com/nofusscomputing/projects/ansible/collections/phpipam_scan_agent/-/issues/4) +- [Timezone Normalization](server.md#timezone-normalization) + ## phpIPAM Features diff --git a/docs/projects/ansible/collection/phpipam_scan_agent/server.md b/docs/projects/ansible/collection/phpipam_scan_agent/server.md index 4992b9f..1c68b86 100644 --- a/docs/projects/ansible/collection/phpipam_scan_agent/server.md +++ b/docs/projects/ansible/collection/phpipam_scan_agent/server.md @@ -67,3 +67,7 @@ The [scan](scanner.md#remote-network-scannning) and server component must be set !!! danger "Security" Failing to secure the server component communication with TLS will allow anyone with direct access to the line of communication to view the `auth_token`. Anyone who has the `auth_token` will be able to upload data to the server. + +## Timezone normalization + +As there is no support within phpIPAM for different timezones. The server component when receiving updates from scanners, will convert any time found to UTC (GMT +00:00). This is required so that phpIPAM features that rely on time, function as they should. This setup requires that machine or docker containers for phpIPAM and the MySQL/MariaDB database both have their timezones set to UTC. If you don't wish for any timezone conversion to be done, ensure that where ever all components, including the scan server component, share the same timezone. diff --git a/playbooks/tasks/server/ipaddress.yaml b/playbooks/tasks/server/ipaddress.yaml index fb80058..1d44304 100644 --- a/playbooks/tasks/server/ipaddress.yaml +++ b/playbooks/tasks/server/ipaddress.yaml @@ -1,5 +1,41 @@ --- +- name: Convert Scan Time to Local Time + ansible.builtin.set_fact: + scan_time: |- + {%- if inbound_data.scan.tz != ('%z' | strftime | string) -%} + + {%- set adjust_time = scan_address.ipaddress.lastSeen -%} + + {%- if ('%z' | strftime)[0:1] == '+' -%} + + {{ + '%Y-%m-%d %H:%M:%S' | strftime( + (adjust_time | to_datetime).strftime('%s') | int - + ( + (inbound_data.scan.tz[1:3] | int * 3600) + (inbound_data.scan.tz[3:5] | int * 60) + ) | int + ) + }} + + {%- else -%} + + {{ + '%Y-%m-%d %H:%M:%S' | strftime((adjust_time | to_datetime).strftime('%s') | int + + ( + (inbound_data.scan.tz[1:3] | int * 3600) + (inbound_data.scan.tz[3:5] | int * 60) + ) | int) + }} + + {%- endif -%} + + {%- else -%} + + {{ scan_address.ipaddress.lastSeen }} + + {%- endif %} + cacheable: false + - name: "Update IP Address' found - {{ scan_address.ipaddress.ip }}" community.mysql.mysql_query: @@ -16,7 +52,7 @@ UPDATE ipaddresses SET - lastSeen = '{{ scan_address.ipaddress.lastSeen }}' + lastSeen = '{{ scan_time }}' {% if scan_address.ipaddress.hostname | default('') != '' %},