Skip to content

Commit

Permalink
feat: add netsec ntp_client support for ntp client configuration (#5631)
Browse files Browse the repository at this point in the history
In many distributions, ntp is a transitional package that provides
a mechansim to install either ntpd or ntpsec.

Installing ntp transitional package may cause issues with APT package
dependency resolution when APT sources are not updated and latest
systemd-timesyncd is out of date because both systemd-timesyncd
and ntpsec declare a Provides: time-daemon and conflict with an
unresolvable version upgrade path unless the conflicting package
also happens to be fully updated.

Add #cloud-config 'ntpsec' option for 'ntp_client' in user-data to
allow opting into the specific ntpsec client over transitional ntp
package on alpine, debian, redhat, sles and ubuntu.

Add template config files for rhel, sles, ubuntu, debian and alpine
obtain by pulling latest configuration files from lxc container launches
of each distribution.

Add json schema strict enum of ntp_client type to better document and
limit ntp_client values to valid configuration.

Update integration tests to prefer ntpsec over ntp
  • Loading branch information
blackboxsw committed Aug 23, 2024
1 parent a6fbcbe commit 9c66960
Show file tree
Hide file tree
Showing 9 changed files with 414 additions and 15 deletions.
31 changes: 25 additions & 6 deletions cloudinit/config/cc_ntp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

frequency = PER_INSTANCE
NTP_CONF = "/etc/ntp.conf"
NTPSEC_CONF = "/etc/ntpsec/ntp.conf"
NR_POOL_SERVERS = 4
distros = [
"almalinux",
Expand Down Expand Up @@ -79,6 +80,14 @@
"template_name": "ntp.conf.{distro}",
"template": None,
},
"ntpsec": {
"check_exe": "ntpd",
"confpath": NTPSEC_CONF,
"packages": ["ntpsec"],
"service_name": "ntpsec",
"template_name": "ntpsec.conf.{distro}",
"template": None,
},
"openntpd": {
"check_exe": "ntpd",
"confpath": "/etc/ntpd.conf",
Expand All @@ -105,10 +114,15 @@
"service_name": "chronyd",
},
"ntp": {
"confpath": "/etc/ntp.conf",
"confpath": NTP_CONF,
"packages": [],
"service_name": "ntpd",
},
"ntpsec": {
"confpath": NTP_CONF,
"packages": [],
"service_name": "ntpsec",
},
},
"aosc": {
"systemd-timesyncd": {
Expand All @@ -129,6 +143,7 @@
"ntp": {
"service_name": "ntpd",
},
"ntpsec": {"service_name": "ntpd", "confpath": NTP_CONF},
"chrony": {
"service_name": "chronyd",
},
Expand All @@ -146,7 +161,7 @@
},
"freebsd": {
"ntp": {
"confpath": "/etc/ntp.conf",
"confpath": NTP_CONF,
"service_name": "ntpd",
"template_name": "ntp.conf.{distro}",
},
Expand Down Expand Up @@ -181,7 +196,7 @@
"service_name": "chronyd",
},
"ntp": {
"confpath": "/etc/ntp.conf",
"confpath": NTP_CONF,
"service_name": "ntpd",
},
"systemd-timesyncd": {
Expand All @@ -193,9 +208,10 @@
"service_name": "chronyd",
},
"ntp": {
"confpath": "/etc/ntp.conf",
"confpath": NTP_CONF,
"service_name": "ntpd",
},
"ntpsec": {"service_name": "ntpd", "confpath": NTP_CONF},
"systemd-timesyncd": {
"check_exe": "/usr/lib/systemd/systemd-timesyncd",
},
Expand All @@ -204,7 +220,8 @@
"chrony": {
"service_name": "chronyd",
},
"ntp": {"service_name": "ntpd", "confpath": "/etc/ntp.conf"},
"ntp": {"service_name": "ntpd", "confpath": NTP_CONF},
"ntpsec": {"service_name": "ntpd", "confpath": NTP_CONF},
"systemd-timesyncd": {
"check_exe": "/usr/lib/systemd/systemd-timesyncd",
"confpath": "/etc/systemd/timesyncd.conf",
Expand All @@ -214,6 +231,7 @@
"ntp": {
"service_name": "ntpd",
},
"ntpsec": {"service_name": "ntpd", "confpath": NTP_CONF},
"chrony": {
"service_name": "chronyd",
},
Expand All @@ -223,9 +241,10 @@
"service_name": "chronyd",
},
"ntp": {
"confpath": "/etc/ntp.conf",
"confpath": NTP_CONF,
"service_name": "ntpd",
},
"ntpsec": {"service_name": "ntpd", "confpath": NTP_CONF},
"systemd-timesyncd": {
"check_exe": "/usr/lib/systemd/systemd-timesyncd",
},
Expand Down
9 changes: 9 additions & 0 deletions cloudinit/config/schemas/schema-cloud-config-v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -2133,6 +2133,15 @@
"ntp_client": {
"type": "string",
"default": "auto",
"enum": [
"auto",
"chrony",
"ntp",
"ntpdate",
"ntpsec",
"openntpd",
"systemd-timesyncd"
],
"description": "Name of an NTP client to use to configure system NTP. When unprovided or 'auto' the default client preferred by the distribution will be used. The following built-in client names can be used to override existing configuration defaults: chrony, ntp, openntpd, ntpdate, systemd-timesyncd."
},
"enabled": {
Expand Down
69 changes: 69 additions & 0 deletions templates/ntpsec.conf.alpine.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# /etc/ntp.conf
# Sensible default NTPsec configuration file.
# See ntp.conf(5) man page for help.
# More examples can be found at https://gitlab.com/NTPsec/ntpsec/-/tree/master/etc/ntp.d

# If you have no other local chimers to help NTP perform sanity checks
# then you can use some public chimers from the NTP public pool:
# http://www.pool.ntp.org/en/
#
# iburst tells it to send the first few requests at 2 second intervals rather
# than wait for the poll interval, which defaults to 64 seconds. That greatly
# speeds up the time for ntpd to set the system time and start responding to
# requests.
#
# You can speed up initialization, and spread the load better, by
# using a country-specific potion of the pool, e.g. something like
#
# us.pool.ntp.org
#
# If you are not in the USA, then it will probably work to
# change the 'us' to your two letter country code.
#
# Major Internet-using countries with pools include:
# us de fr uk nl ch ru ca au cn za br
#
# If you don't know your country code, find it at
#
# https://en.wikipedia.org/wiki/ISO_3166-1
#
# and then try prepending it to ".pool.ntp.org" and pinging that.
# hostname. If you get a response, you can use it.
#
# Alternatively, if you are running Linux your distribution may have
# a designated pool disparcher, e.g. ubuntu.pool.ntp.org
#
{% if pools %}# pools
{% endif %}
{% for pool in pools -%}
pool {{pool}} iburst
{% endfor %}
{%- if servers %}# servers
{% endif %}
{% for server in servers -%}
server {{server}} iburst
{% endfor %}
{% for peer in peers -%}
peer {{peer}}
{% endfor %}

# The following setting reduces the maximum number of sources to not use more
# than four servers from the pool.ntp.org pool. If you have specified other
# servers, you can increase the maxclock number accordingly.
tos maxclock 5

# Exchange time with everybody, but don't allow configuration.
# This is the right security setup for 99% of deployments.
restrict default kod limited nomodify noquery
restrict -6 default kod limited nomodify noquery

# Local users may interrogate the NTP server more closely.
restrict 127.0.0.1
restrict -6 ::1

# Minimal logging - we declare a drift file and that's it.
driftfile /var/lib/ntp/ntp.drift

# We don't log any statistics by default, but the directory
# still needs to exist
statsdir /var/log/ntpstats/
62 changes: 62 additions & 0 deletions templates/ntpsec.conf.debian.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
## template:jinja

# /etc/ntpsec/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntpsec/ntp.drift
leapfile /usr/share/zoneinfo/leap-seconds.list

# To enable Network Time Security support as a server, obtain a certificate
# (e.g. with Let's Encrypt), configure the paths below, and uncomment:
# nts cert CERT_FILE
# nts key KEY_FILE
# nts enable

# You must create /var/log/ntpsec (owned by ntpsec:ntpsec) to enable logging.
#statsdir /var/log/ntpsec/
#statistics loopstats peerstats clockstats
#filegen loopstats file loopstats type day enable
#filegen peerstats file peerstats type day enable
#filegen clockstats file clockstats type day enable

# This should be maxclock 7, but the pool entries count towards maxclock.
tos maxclock 11

# Comment this out if you have a refclock and want it to be able to discipline
# the clock by itself (e.g. if the system is not connected to the network).
tos minclock 4 minsane 3

# Specify one or more NTP servers.

# Public NTP servers supporting Network Time Security:
# server time.cloudflare.com nts

# pool.ntp.org maps to about 1000 low-stratum NTP servers. Your server will
# pick a different set every time it starts up. Please consider joining the
# pool: <https://www.pool.ntp.org/join.html>
{% if pools %}# pools
{% endif %}
{% for pool in pools -%}
pool {{pool}} iburst
{% endfor %}
{%- if servers %}# servers
{% endif %}
{% for server in servers -%}
server {{server}} iburst
{% endfor %}
{% for peer in peers -%}
peer {{peer}}
{% endfor %}

# Access control configuration; see /usr/share/doc/ntpsec-doc/html/accopt.html
# for details.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
restrict default kod nomodify nopeer noquery limited

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1
35 changes: 35 additions & 0 deletions templates/ntpsec.conf.rhel.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## template:jinja

# For more information about this file, see the ntp.conf(5) man page.

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (https://www.pool.ntp.org/join.html).
{% if pools %}# pools
{% endif %}
{% for pool in pools -%}
pool {{pool}} iburst
{% endfor %}
{%- if servers %}# servers
{% endif %}
{% for server in servers -%}
server {{server}} iburst
{% endfor %}
{% for peer in peers -%}
peer {{peer}}
{% endfor %}

# Reduce the maximum number of servers used from the pool.
tos maxclock 5

# Record the frequency of the system clock.
driftfile /var/lib/ntp/drift

# Disable configuration and monitoring access by default.
restrict default nomodify noquery

# Enable all access for localhost.
restrict 127.0.0.1
restrict ::1

# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
Loading

0 comments on commit 9c66960

Please sign in to comment.