diff --git a/CHANGELOG b/CHANGELOG index 443ce3c..db70cb6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,18 @@ # Changelog All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [1.3.0] - 2023-12-27 + +### Added + +- Added time check. If the local clock is more than 10 min away from pool.ntp.org + the script will exit with an error. + +### Changed + +- Changed default location of log from PWD, to `/var/log/router_registration.log` + ## [1.2.4] - 2023-12-13 ### Changed diff --git a/requirements.txt b/requirements.txt index 9165302..a092801 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,4 +9,5 @@ pyyaml colorama jinja2 distro -cryptography \ No newline at end of file +cryptography +ntplib \ No newline at end of file diff --git a/router_registration.py b/router_registration.py index 5c3e99d..f2ef323 100755 --- a/router_registration.py +++ b/router_registration.py @@ -9,11 +9,13 @@ import logging import json import time +from datetime import datetime, timedelta import ssl import ipaddress import subprocess import platform from urllib.parse import urlparse +import ntplib import yaml from cryptography import x509 from cryptography.hazmat.backends import default_backend @@ -200,6 +202,40 @@ def check_subnet(subnet_str): logging.error("Unable to parser subnet") sys.exit(1) +def check_time_diff(margin_minutes=10, server="pool.ntp.org"): + """ + Check if the local time is within a specified margin from the NTP server. + + :param margin_minutes: The acceptable margin in minutes. Default is 5 minutes. + :param server: The NTP server address. Default is "pool.ntp.org". + return: True if local time is within the acceptable margin from the NTP server + """ + # Get the current NTP time + try: + client = ntplib.NTPClient() + response = client.request(server) + ntp_time = datetime.utcfromtimestamp(response.tx_time) + except SystemError: + logging.warning("Unable to compare time.") + + if ntp_time is not None: + logging.debug("ntp time: %s", ntp_time) + # Get the local time + local_time = datetime.now() + logging.debug("local time: %s", local_time) + + # Calculate the difference between local time and NTP time + time_difference = local_time - ntp_time + logging.debug("Time difference: %s", time_difference) + + # Check if the absolute difference is within the specified margin + if abs(time_difference) >= timedelta(minutes=margin_minutes): + logging.error("Time difference: %s", time_difference) + logging.error("Unable to proceed, please check local time") + sys.exit(1) + else: + logging.warning("Unable to compare time.") + def create_netfoundry_tuning_file(): """ Creates a file named '01-netfoundry_tuning.conf' containing specific tuning content @@ -240,7 +276,7 @@ def create_parser(): :return: A Namespace containing arguments """ - __version__ = '1.2.4' + __version__ = '1.3.0' parser = argparse.ArgumentParser() mgroup = parser.add_mutually_exclusive_group(required=True) @@ -259,7 +295,8 @@ def create_parser(): help='Set the logging level - Default: INFO)') parser.add_argument('--logFile', type=str, help='Specify the log file -' - 'Default router_registration.log') + 'Default router_registration.log', + default='/var/log/router_registration.log') parser.add_argument('-s', '--salt', action="store_false", help='Skip salt-stack setup', @@ -868,6 +905,9 @@ def main(): logging.info("\033[0;35mStarting Registration\033[0m") + # check time + check_time_diff() + # check the number of interfaces if not args.edge: check_ipv4_interface_count(parser)