From b731ef4979de9e533ce9bb35110ab2b769e2eed4 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Thu, 8 Apr 2021 19:02:51 +0200 Subject: [PATCH] config: permit "all" and host group in .InGroup of templates These special groups were not handled properly. --- cmd/safcm/config/files_test.go | 13 +++++++++++++ cmd/safcm/config/permissions_test.go | 13 +++++++++++++ cmd/safcm/config/templates.go | 4 +++- cmd/safcm/config/templates_test.go | 9 +++++++++ cmd/safcm/config/triggers_test.go | 13 +++++++++++++ cmd/safcm/sync_sync_test.go | 4 ++-- cmd/safcm/testdata/project/group/files/etc/motd | 13 +++++++++++++ 7 files changed, 66 insertions(+), 3 deletions(-) diff --git a/cmd/safcm/config/files_test.go b/cmd/safcm/config/files_test.go index 1313fdf..82d60c9 100644 --- a/cmd/safcm/config/files_test.go +++ b/cmd/safcm/config/files_test.go @@ -126,6 +126,19 @@ This is GNU/Linux host {{if .InGroup "detected_freebsd"}} This is FreeBSD host {{end}} + +{{if .InGroup "all"}} +all +{{end}} +{{if .InGroup "host1.example.org"}} +host1.example.org +{{end}} +{{if .InGroup "host2"}} +host2 +{{end}} +{{if .InGroup "host3.example.net"}} +host3.example.net +{{end}} `), }, "/etc/rc.local": { diff --git a/cmd/safcm/config/permissions_test.go b/cmd/safcm/config/permissions_test.go index 98b82b6..2f742d0 100644 --- a/cmd/safcm/config/permissions_test.go +++ b/cmd/safcm/config/permissions_test.go @@ -87,6 +87,19 @@ This is GNU/Linux host {{if .InGroup "detected_freebsd"}} This is FreeBSD host {{end}} + +{{if .InGroup "all"}} +all +{{end}} +{{if .InGroup "host1.example.org"}} +host1.example.org +{{end}} +{{if .InGroup "host2"}} +host2 +{{end}} +{{if .InGroup "host3.example.net"}} +host3.example.net +{{end}} `), }, "/etc/rc.local": { diff --git a/cmd/safcm/config/templates.go b/cmd/safcm/config/templates.go index b84b494..0aad2b0 100644 --- a/cmd/safcm/config/templates.go +++ b/cmd/safcm/config/templates.go @@ -116,7 +116,9 @@ func (t *templateArgs) IsHost(host string) bool { func (t *templateArgs) InGroup(group string) bool { // Don't permit invalid groups to detect typos; detected groups cannot // be checked - if !t.allGroups[group] && + if group != GroupAll && + !t.allGroups[group] && + !t.allHosts[group] && !strings.HasPrefix(group, GroupDetectedPrefix) { panic(fmt.Sprintf("group %q does not exist", group)) } diff --git a/cmd/safcm/config/templates_test.go b/cmd/safcm/config/templates_test.go index 36b53dc..fd9ecb7 100644 --- a/cmd/safcm/config/templates_test.go +++ b/cmd/safcm/config/templates_test.go @@ -97,6 +97,15 @@ func TestLoadTemplates(t *testing.T) { This is GNU/Linux host + + +all + + +host1.example.org + + + `), }, "/etc/rc.local": { diff --git a/cmd/safcm/config/triggers_test.go b/cmd/safcm/config/triggers_test.go index 43c3a55..63d702a 100644 --- a/cmd/safcm/config/triggers_test.go +++ b/cmd/safcm/config/triggers_test.go @@ -90,6 +90,19 @@ This is GNU/Linux host {{if .InGroup "detected_freebsd"}} This is FreeBSD host {{end}} + +{{if .InGroup "all"}} +all +{{end}} +{{if .InGroup "host1.example.org"}} +host1.example.org +{{end}} +{{if .InGroup "host2"}} +host2 +{{end}} +{{if .InGroup "host3.example.net"}} +host3.example.net +{{end}} `), }, "/etc/rc.local": { diff --git a/cmd/safcm/sync_sync_test.go b/cmd/safcm/sync_sync_test.go index b5e8ce2..3125293 100644 --- a/cmd/safcm/sync_sync_test.go +++ b/cmd/safcm/sync_sync_test.go @@ -93,7 +93,7 @@ func TestHostSyncReq(t *testing.T) { Mode: 0644, Uid: -1, Gid: -1, - Data: []byte("Welcome to Host ONE\n\n\n\n"), + Data: []byte("Welcome to Host ONE\n\n\n\n\n\nall\n\n\nhost1.example.org\n\n\n\n"), }, "/etc/rc.local": &safcm.File{ OrigGroup: "group", @@ -191,7 +191,7 @@ func TestHostSyncReq(t *testing.T) { Mode: 0644, Uid: -1, Gid: -1, - Data: []byte("Welcome to Host ONE\n\n\n\n"), + Data: []byte("Welcome to Host ONE\n\n\n\n\n\nall\n\n\nhost1.example.org\n\n\n\n"), }, "/etc/rc.local": &safcm.File{ OrigGroup: "group", diff --git a/cmd/safcm/testdata/project/group/files/etc/motd b/cmd/safcm/testdata/project/group/files/etc/motd index be82b96..79a9952 100644 --- a/cmd/safcm/testdata/project/group/files/etc/motd +++ b/cmd/safcm/testdata/project/group/files/etc/motd @@ -9,3 +9,16 @@ This is GNU/Linux host {{if .InGroup "detected_freebsd"}} This is FreeBSD host {{end}} + +{{if .InGroup "all"}} +all +{{end}} +{{if .InGroup "host1.example.org"}} +host1.example.org +{{end}} +{{if .InGroup "host2"}} +host2 +{{end}} +{{if .InGroup "host3.example.net"}} +host3.example.net +{{end}} -- 2.45.2