summaryrefslogtreecommitdiff
path: root/src/Propellor/Property/Icinga2.hs
blob: 8f6f5eeb5a8f7762a5f66e3750f8dd2b02e9be10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
-- | Maintainer: Nicolas Schodet <nico@ni.fr.eu.org>
--
-- Support for Icinga2 monitoring system.

module Propellor.Property.Icinga2 (
	ApiPermissions(..),
	allApiPermissions,
	webApiPermissions,
	installed,
	restarted,
	reloaded,
	idoMysqlConfigured,
	apiConfigured,
	apiUserGranted,
	webInstalled,
	confAdjustSection,
	confContainsSetting,
	confFileContains,
	confHosts,
	confServices,
	confQuote,
) where

import Propellor.Base
import Data.List
import qualified Propellor.Property.Apt as Apt
import qualified Propellor.Property.ConfFile as ConfFile
import qualified Propellor.Property.File as File
import qualified Propellor.Property.Service as Service

-- | API User permissions.
newtype ApiPermissions = ApiPermissions [String]

-- | Allow everything.
allApiPermissions :: ApiPermissions
allApiPermissions = ApiPermissions ["*"]

-- | Needed permissions for Icinga Web 2.
webApiPermissions :: ApiPermissions
webApiPermissions = ApiPermissions
	[ "status/query"
	, "actions/*"
	, "objects/modify/*"
	, "objects/query/*" ]

-- | A section in configuration, need to be the full section description.
type ConfSection = String

-- | A key in configuration.
type ConfKey = String

-- | A configuration statement, can be multiline.
type ConfStatement = [String]

-- | Make sure Icinga 2 is installed.
installed :: Property DebianLike
installed = Apt.installed ["icinga2"]

-- | Restart Icinga 2.
restarted :: Property DebianLike
restarted = Service.restarted "icinga2"

-- | Reload Icinga 2 configuration.
reloaded :: Property DebianLike
reloaded =
	Service.reloaded "icinga2" `before` delay
  where
	-- Reloading too fast causes problems, systemd waits forever.
	delay = cmdProperty "sleep" ["1"] `assume` MadeChange

-- | Configure IDO MySQL.
idoMysqlConfigured :: Property DebianLike
idoMysqlConfigured =
	enabled
		`describe` "icinga2 IDO MySQL feature enabled"
		`requires` installed
		`requires` Apt.installed ["icinga2-ido-mysql"]
		`onChange` restarted
  where
	enabled = pathEnabled `File.isSymlinkedTo` pathAvailable
	pathEnabled = "/etc/icinga2/features-enabled/ido-mysql.conf"
	pathAvailable = File.LinkTarget "../features-available/ido-mysql.conf"

-- | Configure and enable the API.
apiConfigured :: Property DebianLike
apiConfigured =
	check (not <$> isConfigured) go
		`requires` installed
		`onChange` restarted
  where
	isConfigured :: IO Bool
	isConfigured = doesFileExist "/etc/icinga2/features-enabled/api.conf"
	go = cmdProperty "icinga2" ["api", "setup"]

-- | Create an API user and grant permissions.
apiUserGranted
	:: IsContext c
	=> User
	-> ApiPermissions
	-> c
	-> Property (HasInfo + DebianLike)
apiUserGranted (User username) (ApiPermissions perms) context =
	go
		`requires` apiConfigured
  where
	go :: Property (HasInfo + DebianLike)
	go = withPrivData (Password username) context $ \getpassword ->
		property' desc $ \w -> getpassword $ \priv -> ensureProperty w $
			goprop $ privDataVal priv
	goprop :: String -> Property DebianLike
	goprop password = confAdjustSection desc section (adj password) (ins password) usersFile
	desc = ("user " ++ username ++ " granted API usage")
	section = "object ApiUser " ++ (confQuote username)
	usersFile = "conf.d/api-users.conf"
	adj password = const
		[ section ++ " {"
		, "  password = " ++ (confQuote password)
		, "  permissions = [ " ++ qperms ++ " ]" ]
	ins password ls = ls ++ [""] ++ (adj password []) ++ ["}"]
	qperms = intercalate ", " $ map confQuote $ perms

-- | Install Icinga Web 2.
webInstalled :: Property DebianLike
webInstalled = Apt.installed ["icingaweb2"]

-- | Adjust a section of Icinga configuration file.
confAdjustSection
	:: Desc
	-> ConfSection
	-> ConfFile.AdjustSection
	-> ConfFile.InsertSection
	-> FilePath
	-> Property DebianLike
confAdjustSection desc section adj ins f =
	ConfFile.adjustSection desc start past adj ins conff
		`onChange` reloaded
  where
	start = (== (section ++ " {"))
	past = (== "}")
	conff = "/etc/icinga2" </> f

-- | Ensures that a configuration file contains a section with a key=value
-- setting. Works for simple item using a simple format (single line).
confContainsSetting
	:: FilePath
	-> (ConfSection, ConfKey, String)
	-> Property DebianLike
confContainsSetting f (section, key, value) = confAdjustSection
	(f ++ " section '" ++ section ++ "' contains " ++ key ++ "=" ++ value)
	section
	go
	(++ confsection)
	f
  where
	confkey = "  " ++ key ++ " = "
	confline = confkey ++ value
	go [] = [confline]
	go (l:ls) = if isKeyVal l then confline : ls else l : go ls
	isKeyVal = (confkey `isPrefixOf`)
	confsection = ["", section ++ " {", confline, "}"]

-- | Replace a full configuration file.
confFileContains :: FilePath -> [ConfStatement] -> Property DebianLike
confFileContains f stmts =
	conff `File.hasContent` (concat stmts)
		`onChange` reloaded
  where
	conff = "/etc/icinga2" </> f

-- | Replace the full host definition.
confHosts :: [ConfStatement] -> Property DebianLike
confHosts hosts = confFileContains "conf.d/hosts.conf" hosts

-- | Replace the full services definition.
confServices :: [ConfStatement] -> Property DebianLike
confServices services = confFileContains "conf.d/services.conf" services

-- | Quote a string to be included in configuration.
confQuote :: String -> String
confQuote s =
	['"'] ++ (concatMap escape s) ++ ['"']
  where
	escape c
		| c == '"' = ['\\', '"']
		| c == '\\' = ['\\', '\\']
		| c == '\t' = ['\\', 't']
		| c == '\r' = ['\\', 'r']
		| c == '\n' = ['\\', 'n']
		| c == '\b' = ['\\', 'b']
		| c == '\f' = ['\\', 'f']
		| otherwise = [c]