Skip to content

Commit

Permalink
Make Systemd::Unit type stricter
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
traylenator committed Jul 1, 2022
1 parent 673cbc9 commit 9867823
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
9 changes: 6 additions & 3 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
* [`Systemd::MachineInfoSettings`](#systemdmachineinfosettings): Matches Systemd machine-info (hostnamectl) file Struct
* [`Systemd::OomdSettings`](#systemdoomdsettings): Configurations for oomd.conf
* [`Systemd::ServiceLimits`](#systemdservicelimits): Matches Systemd Service Limit Struct
* [`Systemd::Unit`](#systemdunit): custom datatype that validates different filenames for systemd units
* [`Systemd::Unit`](#systemdunit): custom datatype that validates different filenames for systemd units and unit templates

## Classes

Expand Down Expand Up @@ -1803,11 +1803,14 @@ Struct[{

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

custom datatype that validates different filenames for systemd units
custom datatype that validates different filenames for systemd units and unit templates

* **See also**
* https://www.freedesktop.org/software/systemd/man/systemd.unit.html

Alias of

```puppet
Pattern['^[^/]+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)$']
Pattern[/^[[a-z][A-Z][0-9]:\-_.\\@]+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)$/]
```

32 changes: 32 additions & 0 deletions spec/type_aliases/unit_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'Systemd::Unit' do
context 'with a permitted unit name' do
[
'foo.service',
'foo.socket',
'atemplate@.service',
'atemplate@instance.service',
'backward\slash.swap',
'extra.dot.scope',
'a:colon.path',
'an_underscore.device',
'a-dash.slice',
].each do |unit|
it { is_expected.to allow_value(unit.to_s) }
end
end

context 'with a illegal unit name' do
[
'a space.service',
'noending',
'wrong.ending',
'forward/slash.unit',
].each do |unit|
it { is_expected.not_to allow_value(unit.to_s) }
end
end
end
6 changes: 4 additions & 2 deletions types/unit.pp
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# @summary custom datatype that validates different filenames for systemd units
type Systemd::Unit = Pattern['^[^/]+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)$']
# @summary custom datatype that validates different filenames for systemd units and unit templates
# @see https://www.freedesktop.org/software/systemd/man/systemd.unit.html
type Systemd::Unit = Pattern[/^[[a-z][A-Z][0-9]:\-_.\\@]+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)$/]

0 comments on commit 9867823

Please sign in to comment.