Skip to content

Commit d9d99c3

Browse files
authored
Make Systemd::Unit type stricter (#290)
Previously lots of unit names like ``` this is a service with spaces in.service ``` were permitted for instance. From systemd.unit > Valid unit names consist of a "name prefix" and a dot and a suffix specifying the unit type. The "unit prefix" must consist of one or more valid characters (ASCII letters, digits, ":", "-", "_", ".", and "\"). The total length of the unit name including the suffix must not exceed 256 characters. The type suffix must be one of ".service", ".socket", ".device", ".mount", ".automount", ".swap", ".target", ".path", ".timer", ".slice", or ".scope". in addition we allow `@` to cover the case of template or template instance.
1 parent f1cb857 commit d9d99c3

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

REFERENCE.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
* [`Systemd::MachineInfoSettings`](#systemdmachineinfosettings): Matches Systemd machine-info (hostnamectl) file Struct
5959
* [`Systemd::OomdSettings`](#systemdoomdsettings): Configurations for oomd.conf
6060
* [`Systemd::ServiceLimits`](#systemdservicelimits): Matches Systemd Service Limit Struct
61-
* [`Systemd::Unit`](#systemdunit): custom datatype that validates different filenames for systemd units
61+
* [`Systemd::Unit`](#systemdunit): custom datatype that validates different filenames for systemd units and unit templates
6262

6363
## Classes
6464

@@ -1803,11 +1803,14 @@ Struct[{
18031803

18041804
### <a name="systemdunit"></a>`Systemd::Unit`
18051805

1806-
custom datatype that validates different filenames for systemd units
1806+
custom datatype that validates different filenames for systemd units and unit templates
1807+
1808+
* **See also**
1809+
* https://www.freedesktop.org/software/systemd/man/systemd.unit.html
18071810

18081811
Alias of
18091812

18101813
```puppet
1811-
Pattern['^[^/]+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)$']
1814+
Pattern[/^[a-zA-Z0-9:\-_.\\@]+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)$/]
18121815
```
18131816

spec/type_aliases/unit_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'Systemd::Unit' do
6+
context 'with a permitted unit name' do
7+
[
8+
'foo.service',
9+
'foo.socket',
10+
11+
12+
'backward\slash.swap',
13+
'extra.dot.scope',
14+
'a:colon.path',
15+
'an_underscore.device',
16+
'a-dash.slice',
17+
].each do |unit|
18+
it { is_expected.to allow_value(unit.to_s) }
19+
end
20+
end
21+
22+
context 'with a illegal unit name' do
23+
[
24+
'a space.service',
25+
'noending',
26+
'wrong.ending',
27+
'forward/slash.unit',
28+
].each do |unit|
29+
it { is_expected.not_to allow_value(unit.to_s) }
30+
end
31+
end
32+
end

types/unit.pp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
# @summary custom datatype that validates different filenames for systemd units
2-
type Systemd::Unit = Pattern['^[^/]+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)$']
1+
# @summary custom datatype that validates different filenames for systemd units and unit templates
2+
# @see https://www.freedesktop.org/software/systemd/man/systemd.unit.html
3+
type Systemd::Unit = Pattern[/^[a-zA-Z0-9:\-_.\\@]+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)$/]

0 commit comments

Comments
 (0)