summaryrefslogtreecommitdiff
path: root/src/Propellor/Property/Mysql.hs
blob: 66827df8cede4afd812fdb508ac7fbfc0f76239c (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
-- | Maintainer: Nicolas Schodet <nico@ni.fr.eu.org>
--
-- Support for MySQL/MariaDB server.
--
-- Enough to setup a database with a dedicated user for web applications.

module Propellor.Property.Mysql (
	Database(..),
	Privilege(..),
	allPrivileges,
	basicPrivileges,
	basicStructurePrivileges,
	installed,
	installedAndReady,
	databaseExists,
	databaseRestored,
	userGrantedOnDatabase,
	userGrantedOnDatabaseWithPassword,
	userGranted,
	userGrantedWithPassword,
) where

import Propellor
import Propellor.Base
import Propellor.Types
import Data.List
import qualified Propellor.Property.Apt as Apt

-- | A database is defined by its name.
newtype Database = Database String

data Privilege
	= Select
	| Insert
	| Update
	| Delete
	| Create
	| Drop
	| Index
	| Alter
	| CreateTemporaryTables
	| LockTables
	| Execute
	| CreateView
	| ShowView
	| CreateRoutine
	| AlterRoutine
	| Event
	| Trigger
	deriving (Eq, Ord, Enum)

sqlPrivilege :: Privilege -> String
sqlPrivilege Select = "SELECT"
sqlPrivilege Insert = "INSERT"
sqlPrivilege Update = "UPDATE"
sqlPrivilege Delete = "DELETE"
sqlPrivilege Create = "CREATE"
sqlPrivilege Drop = "DROP"
sqlPrivilege Index = "INDEX"
sqlPrivilege Alter = "ALTER"
sqlPrivilege CreateTemporaryTables = "CREATE TEMPORARY TABLES"
sqlPrivilege LockTables = "LOCK TABLES"
sqlPrivilege Execute = "EXECUTE"
sqlPrivilege CreateView = "CREATE VIEW"
sqlPrivilege ShowView = "SHOW VIEW"
sqlPrivilege CreateRoutine = "CREATE ROUTINE"
sqlPrivilege AlterRoutine = "ALTER ROUTINE"
sqlPrivilege Event = "EVENT"
sqlPrivilege Trigger = "TRIGGER"

-- | All privileges
allPrivileges :: [Privilege]
allPrivileges = [Select .. Trigger]

-- | Basic privileges needed to use a classic database already created.
basicPrivileges :: [Privilege]
basicPrivileges =
	[ Select
	, Insert
	, Update
	, Delete
	]

-- | Classic privileges needed to create a database and its structure.
basicStructurePrivileges :: [Privilege]
basicStructurePrivileges =
	[ Select
	, Insert
	, Update
	, Delete
	, Create
	, Drop
	, Index
	, Alter
	]

-- | Make sure a server is installed.
installed :: RevertableProperty DebianLike DebianLike
installed = install <!> remove
  where
	install = Apt.installed server
	remove = Apt.removed server
	server = ["default-mysql-server"]

-- | Make sure a server is installed and ready.
installedAndReady :: Property DebianLike
installedAndReady = ready `requires` installed
  where
	ready = scriptProperty ["mysqladmin -w3 ping > /dev/null"]
		`assume` NoChange

-- | Check whether server is installed.
isInstalled :: IO Bool
isInstalled = Apt.isInstalled server
  where
	server = "default-mysql-server"

-- | Create a database if it does not exist.  When reverted, remove the
-- database.
databaseExists :: Database -> RevertableProperty DebianLike UnixLike
databaseExists (Database dbname) =
	setup <!> cleanup
  where
	setup :: Property DebianLike
	setup = setup' `requires` installedAndReady

	cleanup :: Property UnixLike
	cleanup = check isInstalled $ cleanup'

	-- Test for database existance and create it if needed.
	setup' :: Property UnixLike
	setup' = property' desc $ \w -> do
		present <- liftIO $ dbPresent
		ensureProperty w $ setupprop present
	  where
		desc = "database " ++ dbname ++ " exists"

	setupprop :: Bool -> Property UnixLike
	setupprop True = doNothing
	setupprop False = cmdProperty "mysqladmin" ["create", dbname]
		`assume` MadeChange

	-- Test for database existance and drop it if needed.
	cleanup' :: Property UnixLike
	cleanup' = property' desc $ \w -> do
		present <- liftIO $ dbPresent
		ensureProperty w $ cleanupprop present
	  where
		desc = "database " ++ dbname ++ " does not exist"

	cleanupprop :: Bool -> Property UnixLike
	cleanupprop True = cmdProperty "mysqladmin" ["drop", "-f", dbname]
		`assume` MadeChange
	cleanupprop False = doNothing

	-- Is database present?
	dbPresent :: IO Bool
	dbPresent = (== trueResult) <$> readProcess "mysql" ["-BNre", sql]
	  where
		sql = "SHOW DATABASES LIKE " ++ qdbname
		qdbname = sqlQuote '\'' dbname
		trueResult = dbname ++ "\n"

-- | Restore a database from a gziped backup.
--
-- Only does anything if the database does not exist, or is completely empty.
--
-- If the backup does not exists yet, only make sure the database exists.
databaseRestored :: Database -> FilePath -> Property DebianLike
databaseRestored (Database dbname) gzFile =
	restored `requires` databaseExists (Database dbname)
  where
	restored :: Property UnixLike
	restored = property desc $ ifM (liftIO $ doesFileExist gzFile)
		( ifM (liftIO dbEmpty)
			( do
				warningMessage restoringMessage
				liftIO restore
			, noChange
			)
		, do
			warningMessage nobackupMessage
			noChange
		)

	dbEmpty :: IO Bool
	dbEmpty = (== "") <$> readProcess "mysql" ["-BNre", sql]
	  where
		sql = "SHOW TABLES FROM " ++ qdbname
		qdbname = sqlQuote '`' dbname

	restore :: IO Result
	restore = restoreResult <$> boolSystem "sh" [
		Param "-c"
		, Param $ "zcat " ++ shellEscape gzFile
			++ " | mysql " ++ shellEscape dbname
		]

	restoreResult False = FailedChange
	restoreResult True = MadeChange

	desc = "database " ++ dbname ++ " restored"
	restoringMessage =
		"database " ++ dbname ++ " is empty; restoring from backup ..."
	nobackupMessage = "no backup for database " ++ dbname

-- | Create an user and make sure he has grants on the specific database but
-- no other grant.
userGrantedOnDatabase
	:: IsContext c
	=> User
	-> Database
	-> [Privilege]
	-> c
	-> RevertableProperty (HasInfo + DebianLike) UnixLike
userGrantedOnDatabase user db privs context =
	userGrantedOnDatabase' user db privs withPassword
  where
	withPassword = withPasswordFromPrivData user context

-- | Same as userGrantedOnDatabase, but provide the password as parameter.
userGrantedOnDatabaseWithPassword
	:: User
	-> Database
	-> [Privilege]
	-> String
	-> RevertableProperty DebianLike UnixLike
userGrantedOnDatabaseWithPassword user db privs password =
	userGrantedOnDatabase' user db privs withPassword
  where
	withPassword = withPasswordFromParameter password

-- | Common code between userGrantedOnDatabase*.
userGrantedOnDatabase'
	:: Combines (Property i) (Property UnixLike)
	=> User
	-> Database
	-> [Privilege]
	-> ((((String -> Propellor Result) -> Propellor Result)
		-> Property i) -> Property i)
	-> RevertableProperty (CombinedType (Property i) (Property UnixLike)) UnixLike
userGrantedOnDatabase' user@(User username) (Database dbname) privs withPassword =
	userGrantedProp user privs withPassword setupDesc setupSql userGrants
  where
	setupDesc = "user " ++ username ++ " granted on database " ++ dbname
	setupSql quser hash privList =
		"GRANT " ++ privList ++ " ON " ++ privLevel
		++ " TO " ++ quser
		++ " IDENTIFIED BY PASSWORD '" ++ hash ++ "'"
	-- Expected user grants as output by MySQL.
	userGrants quser hash privList =
		"GRANT USAGE ON *.* TO " ++ quser
		++ " IDENTIFIED BY PASSWORD '" ++ hash ++ "'\n"
		++ "GRANT " ++ privList ++ " ON " ++ privLevel
		++ " TO " ++ quser ++ "\n"
	-- Privilege level for database access.
	privLevel = (sqlQuote '`' dbname) ++ ".*"

-- | Create an user and make sure he has global grants but no other grant.
userGranted
	:: IsContext c
	=> User
	-> [Privilege]
	-> c
	-> RevertableProperty (HasInfo + DebianLike) UnixLike
userGranted user privs context =
	userGranted' user privs withPassword
  where
	withPassword = withPasswordFromPrivData user context

-- | Same as userGranted, but provide the password as parameter.
userGrantedWithPassword
	:: User
	-> [Privilege]
	-> String
	-> RevertableProperty DebianLike UnixLike
userGrantedWithPassword user privs password =
	userGranted' user privs withPassword
  where
	withPassword = withPasswordFromParameter password

-- | Common code between userGranted*.
userGranted'
	:: Combines (Property i) (Property UnixLike)
	=> User
	-> [Privilege]
	-> ((((String -> Propellor Result) -> Propellor Result)
		-> Property i) -> Property i)
	-> RevertableProperty (CombinedType (Property i) (Property UnixLike)) UnixLike
userGranted' user@(User username) privs withPassword =
	userGrantedProp user privs withPassword setupDesc setupSql userGrants
  where
	setupDesc = "user " ++ username ++ " granted"
	setupSql quser hash privList =
		"GRANT " ++ privList ++ " ON *.*"
		++ " TO " ++ quser
		++ " IDENTIFIED BY PASSWORD '" ++ hash ++ "'"
	-- Expected user grants as output by MySQL.
	userGrants quser hash privList =
		"GRANT " ++ privList ++ " ON *.* TO " ++ quser
		++ " IDENTIFIED BY PASSWORD '" ++ hash ++ "'\n"

-- | Common code to get password from private data.
withPasswordFromPrivData
	:: IsContext c
	=> User
	-> c
	-> ((((String -> Propellor Result) -> Propellor Result)
		-> Property (HasInfo + UnixLike))
			-> Property (HasInfo + UnixLike))
withPasswordFromPrivData (User username) context = \mkprop ->
	withPrivData (Password username) context
		$ \getdata -> mkprop
			$ \a -> getdata $ \priv -> a $ privDataVal priv

-- | Common code to pass password from parameter.
withPasswordFromParameter
	:: String
	-> ((((String -> Propellor Result) -> Propellor Result)
		-> Property UnixLike) -> Property UnixLike)
withPasswordFromParameter password = \mkprop ->
	mkprop $ \a -> a password

-- | Common code to grant or remove an user.
userGrantedProp
	:: Combines (Property i) (Property UnixLike)
	=> User
	-> [Privilege]
	-> ((((String -> Propellor Result) -> Propellor Result)
		-> Property i) -> Property i)
	-> String
	-> (String -> String -> String -> String)
	-> (String -> String -> String -> String)
	-> RevertableProperty (CombinedType (Property i) (Property UnixLike)) UnixLike
userGrantedProp (User username) privs withPassword setupDesc setupSql userGrants =
	setup <!> cleanup
  where
	setup :: CombinedType (Property i) (Property UnixLike)
	setup = setup' `requires` installedAndReady

	cleanup :: Property UnixLike
	cleanup = check isInstalled $ cleanup'

	-- Check user grants and reset them if needed.
	setup' :: Property i
	setup' = withPassword $ \getpassword ->
		property' setupDesc $ \w -> getpassword $ \password -> do
			hash <- liftIO $ hashPassword $ password
			curGrants <- liftIO $ getUserGrants
			let match = curGrants ==
				(userGrants quser hash privList)
			ensureProperty w $ setupprop match hash

	setupprop :: Bool -> String -> Property UnixLike
	setupprop True _ = doNothing
	setupprop False hash = cmdProperty "mysql" ["-BNre", sql]
		`assume` MadeChange
	  where
		sql = "DROP USER IF EXISTS " ++ quser ++ ";" ++
			(setupSql quser hash privList)

	-- Test for user existance and drop it if needed.
	cleanup' :: Property UnixLike
	cleanup' =
		property' desc $ \w -> do
			curGrants <- liftIO $ getUserGrants
			ensureProperty w $ cleanupprop $ curGrants /= ""
	  where
		desc = "user " ++ username ++ " does not exist"

	cleanupprop :: Bool -> Property UnixLike
	cleanupprop False = doNothing
	cleanupprop True = cmdProperty "mysql" ["-BNre", sql]
		`assume` MadeChange
	  where
		sql = "DROP USER " ++ quser

	-- Request MySQL to hash a password.
	hashPassword :: String -> IO String
	hashPassword password =
		Data.List.head . lines <$>
			writeReadProcessEnv "mysql" ["-BNr"] Nothing
				(Just writer) Nothing
	  where
		writer h = hPutStr h sql
		sql = "SELECT PASSWORD(" ++ qpassword ++ ")"
		qpassword = sqlQuote '\'' password

	-- Request current user grants from MySQL.
	getUserGrants :: IO String
	getUserGrants =
		catchDefaultIO "" $ readProcess "mysql" ["-BNre", sql]
	  where
		sql = "SHOW GRANTS FOR " ++ quser

	-- Privilege list as output by MySQL.
	privList = intercalate ", " $ map sqlPrivilege $ nub $ sort privs
	-- Qualified user name.
	quser = (sqlQuote '\'' username) ++ "@'localhost'"

-- | Quote a string using the given quote character.
sqlQuote :: Char -> String -> String
sqlQuote quote s =
	[quote] ++ (concatMap escape s) ++ [quote]
  where
	escape c
		| c == quote = [quote, quote]
		| otherwise = [c]