/
config.clj
625 lines (541 loc) · 25.6 KB
/
config.clj
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
(ns clojure-commons.config
(:use [clojure.java.io :only [file]]
[clojurewerkz.propertied.properties :only [map->properties]]
[slingshot.slingshot :only [throw+]])
(:require [clojure.string :as string]
[clojure-commons.error-codes :as ce]
[clojure-commons.props :as cp]
[clojure.tools.logging :as log])
(:import [java.io IOException]
[java.util UUID]))
(defn load-config-from-file
"A multi-arity function that loads the configuration properties from a file.
Parameters:
conf-dir - the path to the configuration directory.
filename - the name of the configuration file.
props - the reference to the properties.
or:
filepath - the path to the configuration file.
props - the reference to the properties."
([filepath props]
(dosync (ref-set props (cp/read-properties (file filepath)))))
([conf-dir filename props]
(if (nil? conf-dir)
(dosync (ref-set props (cp/read-properties (file filename))))
(dosync (ref-set props (cp/read-properties (file conf-dir filename)))))))
(defn load-config-from-map
"Loads configuration properties from a Clojure map. This function is used primarily for unit testing services."
[m props]
(dosync (ref-set props (map->properties m))))
(defn masked-field?
"Returns a truthy value if the field should be masked and a falsey value if it shouldn't."
[field filters]
(some #(re-find % field) filters))
(defn- log-prop
"Logs a single configuration setting."
[[k v] filters]
(let [v (if (masked-field? k filters) "********" v)]
(log/info "CONFIG:" k "=" v)))
(defn- mask-prop
"Masks a single configuration setting, if necessary. Does not log it."
[[k v] filters]
[k (if (masked-field? k filters) "********" v)])
(defn log-config
"Logs the configuration settings.
Parameters:
props - the reference to the properties.
:filters - list of regexes matching keys in a properties file
whose values should be masked. Defaults to: [#\"passord\" #\"password\"].
The defaults will be added to the :filters list anyway, so you don't need
to specify them."
[props & {:keys [filters]
:or {filters []}}]
(let [all-filters (concat filters [#"password" #"pass"])
log-it #(log-prop % all-filters)]
(dorun (map log-it (sort-by key @props)))))
(defn mask-config
"Returns a new configuration map with the appropriate fields masked.
Parameters:
props - the reference to the properties map.
:filters - same as what's passed in to (log-config)"
[props & {:keys [filters]
:or {filters []}}]
(let [all-filters (concat filters [#"password" #"pass"])
mask-it #(mask-prop % all-filters)]
(into {} (mapv mask-it (sort-by key @props)))))
(defn record-missing-prop
"Records a property that is missing. Instead of failing on the first missing parameter, we log
the missing parameter, mark the configuration as invalid and keep going so that we can log as
many configuration errors as possible in one run.
Parameters:
prop-name - the name of the property.
config-valid - a ref containing a validity flag."
[prop-name config-valid]
(log/error "required configuration setting" prop-name "is empty or"
"undefined")
(dosync (ref-set config-valid false)))
(defn record-invalid-prop
"Records a property that has an invalid value. Instead of failing on the first missing
parameter, we log the missing parameter, mark the configuration as invalid and keep going so
that we can log as many configuration errors as possible in one run.
Parameters:
prop-name - the name of the property.
t - the Throwable instance that caused the error.
confiv-valid - a ref containing a validity flag."
[prop-name t config-valid]
(log/error "invalid configuration setting for" prop-name ":" t)
(dosync (ref-set config-valid false)))
(defn get-required-prop
"Gets a required property from a set of properties.
Parameters:
props - a ref containing the properties.
prop-name - the name of the property.
config-valid - a ref containing a validity flag."
[props prop-name config-valid]
(let [value (get @props prop-name "")]
(when (empty? value)
(record-missing-prop prop-name config-valid))
value))
(defn get-optional-prop
"Gets an optional property from a set of properties.
Parameters:
props - a ref containing the properties.
prop-name - the name of the property.
config-valid - a ref containing a validity flag.
default - the default property value."
([props prop-name config-valid]
(get @props prop-name ""))
([props prop-name config-valid default]
(get @props prop-name default)))
(defn vector-from-prop
"Derives a list of values from a single comma-delimited value.
Parameters:
value - the value to convert to a vector."
[value]
(remove string/blank? (string/split value #", *")))
(defn get-required-vector-prop
"Gets a required vector property from a set of properties.
Parameters:
props - a ref containing the properties.
prop-name - the name of the property.
config-valid - a ref containing a validity flag."
[props prop-name config-valid]
(vector-from-prop (get-required-prop props prop-name config-valid)))
(defn get-optional-vector-prop
"Gets an optional vector property from a set of properties.
Parameters:
props - a ref containing the properties.
prop-name - the name of the property.
config-valid - a ref containing a validity flag."
([props prop-name config-valid]
(vector-from-prop (get-optional-prop props prop-name config-valid)))
([props prop-name config-valid default]
(if-let [string-value (get-optional-prop props prop-name config-valid nil)]
(vector-from-prop string-value)
default)))
(defn string-to-int
"Attempts to convert a String property to an integer property. Returns zero if the property
can't be converted.
Parameters:
prop-name - the name of the property.
value - the value of the property as a string.
config-valid - a ref containing a vailidity flag."
[prop-name value config-valid]
(try
(Integer/parseInt value)
(catch NumberFormatException e
(record-invalid-prop prop-name e config-valid)
0)))
(defn string-to-long
"Attempts to convert a String property to a long property. Returns zero if the property can't
be converted.
Parameters:
prop-name - the name of the property.
value - the value of the property as a string.
config-valid - a ref containing a validity flag."
[prop-name value config-valid]
(try
(Long/parseLong value)
(catch NumberFormatException e
(record-invalid-prop prop-name e config-valid)
0)))
(defn string-to-boolean
"Attempts to convert a String property to a Boolean property. Returns false if the property
can't be converted.
Parameters:
prop-name - the name of the property.
value - the value of the property as a string.
config-valid - a ref containing a validity flag."
[prop-name value config-valid]
(try
(Boolean/parseBoolean value)
(catch Exception e
(record-invalid-prop prop-name e config-valid)
false)))
(defn string-to-uuid
"Attempts to convert a String property to a UUID property. Returns false if the property can't
be converted.
Parameters:
prop-name - the name of the property.
value - the value of the property as a string.
config-valid - a ref containing a validity flag."
[prop-name value config-valid]
(try
(UUID/fromString value)
(catch Exception e
(record-invalid-prop prop-name e config-valid)
false)))
(defn get-required-integer-prop
"Gets a required integer property from a set of properties. If the property is missing or not
able to be converted to an integer then the configuration will be marked as invalid and zero
will be returned.
Parameters:
props - a ref containing the properties.
prop-name - the name of the property.
config-valid - a ref containing a validity flag."
[props prop-name config-valid]
(let [value (get-required-prop props prop-name config-valid)]
(if (string/blank? value)
0
(string-to-int prop-name value config-valid))))
(defn get-optional-integer-prop
"Gets an optional integer property from a set of properties.
Parameters:
props - a ref containing the properties.
prop-name - the name of the property.
config-valid - a ref containing the validity flag.
default - the default value."
([props prop-name config-valid]
(get-optional-integer-prop props prop-name config-valid 0))
([props prop-name config-valid default]
(if-let [string-value (get-optional-prop props prop-name config-valid nil)]
(string-to-int prop-name string-value config-valid)
default)))
(defn get-required-long-prop
"Gets a required long property from a set of properties. If a property is missing or not able to
be converted to a long then the configuration will be marked as invalid and zero will be
returned.
Parameters:
props - a ref containing the properties.
prop-name - the name of a property.
config-valid - a ref containing a validity flag."
[props prop-name config-valid]
(let [value (get-required-prop props prop-name config-valid)]
(if (string/blank? value)
0
(string-to-long prop-name value config-valid))))
(defn get-optional-long-prop
"Gets an optional long property from a set of properties.
Parameters:
props - a ref containing the properties.
prop-name - the name of the property.
config-valid - a ref containing the vailidity flag.
default - the default value."
([props prop-name config-valid]
(get-optional-long-prop props prop-name config-valid 0))
([props prop-name config-valid default]
(if-let [string-value (get-optional-prop props prop-name config-valid nil)]
(string-to-long prop-name string-value config-valid)
default)))
(defn get-required-boolean-prop
"Gets a required Boolean property from a set of properties. If a property is missing or not able
to be converted to a Boolean then the configuration will be marked as invalid and false will
be returned.
Parameters:
props - a ref containing the properties.
prop-name - the name of a property.
config-valid - a ref containing a validity flag."
[props prop-name config-valid]
(let [value (get-required-prop props prop-name config-valid)]
(if (string/blank? value)
false
(string-to-boolean prop-name value config-valid))))
(defn get-optional-boolean-prop
"Gets an optional Boolean property from a set of properties.
Parameters:
props - a ref containing the properties.
prop-name - the name of the property.
config-valid - a ref containing the vailidity flag.
default - the default value."
([props prop-name config-valid]
(get-optional-boolean-prop props prop-name config-valid false))
([props prop-name config-valid default]
(if-let [string-value (get-optional-prop props prop-name config-valid nil)]
(string-to-boolean prop-name string-value config-valid)
default)))
(defn get-required-uuid-prop
"Gets a required UUID property from a set of properties. If a property is missing or not able
to be converted to a UUID then the configuration will be marked as invalid and false will be
returned.
Parameters:
props - a ref containing the properties.
prop-name - the name of a property.
config-valid - a ref containing a validity flag."
[props prop-name config-valid]
(let [value (get-required-prop props prop-name config-valid)]
(if (string/blank? value)
false
(string-to-uuid prop-name value config-valid))))
(defn get-optional-uuid-prop
"Gets an optional UUID property from a set of properties.
Parameters:
props - a ref containing the properties.
prop-name - the name of the property.
config-valid - a ref containing the vailidity flag.
default - the default value."
([props prop-name config-valid]
(get-optional-uuid-prop props prop-name config-valid nil))
([props prop-name config-valid default]
(if-let [string-value (get-optional-prop props prop-name config-valid nil)]
(string-to-uuid prop-name string-value config-valid)
default)))
(defn- wrap-extraction-fn
"Places a property extraction function in an appropriate wrapper, depending on whether or not
the property is flagged. This function depends on the fact that property validation is done
in the extraction function itself. For flagged properties that are disabled, the extraction
function is not called when the value is retrieved. Therefore the validation for disabled
properties is skipped because the extraction function isn't called.
Parameters:
extraction-fn - the actual extraction function.
flag-props - the feature flag properties determining if the property is relevant."
[extraction-fn flag-props]
(if (empty? flag-props)
`(memoize ~extraction-fn)
`(memoize (fn [] (when (some #(%) ~(vec flag-props)) (~extraction-fn))))))
(defn define-property
"Defines a property. This is a helper function that performs common tasks required by all of the
defprop macros.
Parameters:
sym - the symbol to define.
desc - a brief description of the property.
configs - a ref containing the list of config settings.
flag-props - the feature flag properties determining if the property is relevant.
extraction-fn - the function used to extract the property value."
[sym desc configs flag-props extraction-fn]
(let [wrapped-extraction-fn (wrap-extraction-fn extraction-fn flag-props)]
`(dosync (alter ~configs conj (def ~sym ~desc ~wrapped-extraction-fn)))))
(defn define-required-property
"Defines a required property. This is a helper function that performs common tasks required by
the macros for required properties.
Parameters:
sym - the symbol to define.
desc - a brief description of the property.
props - a ref containing the properties.
config-valid - a ref containing the validity flag.
configs - a ref containing the list of config settings.
flag-props - the feature flag properties determining if the property is relevant.
prop-name - the name of the property.
extraction-fn - the function used to extract the property value."
[sym desc [props config-valid configs flag-props] prop-name extraction-fn]
(define-property sym desc configs flag-props
`(fn [] (~extraction-fn ~props ~prop-name ~config-valid))))
(defn define-optional-property
"Defines an optional property. This is a helper function that performs common tasks required by
the macros for optional properties.
Parameters:
sym - the symbol to define.
desc - a brief description of the property.
props - a ref containing the properties.
config-valid - a ref containing the validity flag.
configs - a ref containing the list of config settings.
flag-props - the feature flag properties determining if the property is relevant.
prop-name - the name of the property.
extraction-fn - the function used to extract the property value.
default-value - the default value for the property."
[sym desc [props config-valid configs flag-props] prop-name extraction-fn default-value]
(define-property sym desc configs flag-props
`(fn [] (~extraction-fn ~props ~prop-name ~config-valid ~default-value))))
(defmacro defprop-str
"defines a required string property.
Parameters:
sym - the symbol to define.
desc - a brief description of the property.
props - a ref containing the properties.
config-valid - a ref containing a validity flag.
configs - a ref containing the list of config settings.
flag-props - the feature flag properties determining if the property is relevant.
prop-name - the name of the property."
[sym desc [props config-valid configs & flag-props] prop-name]
(define-required-property
sym desc [props config-valid configs flag-props] prop-name get-required-prop))
(defmacro defprop-optstr
"Defines an optional string property.
Parameters:
sym - the symbol to define.
desc - a brief description of the property.
props - a ref containing the properties.
config-valid - a ref containing a validity flag.
configs - a ref containing the list of config settings.
flag-props - the feature flag properties determining if the property is relevant.
prop-name - the name of the property.
default - the default value."
([sym desc [props config-valid configs & flag-props] prop-name]
(define-optional-property
sym desc [props config-valid configs flag-props] prop-name get-optional-prop ""))
([sym desc [props config-valid configs & flag-props] prop-name default]
(define-optional-property
sym desc [props config-valid configs flag-props] prop-name get-optional-prop default)))
(defmacro defprop-vec
"Defines a required vector property.
Parameters:
sym - the symbol to define.
desc - a brief description of the property.
props - a ref containing the properties.
config-valid - a ref containing a validity flag.
configs - a ref containing the list of config settings.
flag-props - the feature flag properties determining if the property is relevant.
prop-name - the name of the property."
[sym desc [props config-valid configs & flag-props] prop-name]
(define-required-property
sym desc [props config-valid configs flag-props] prop-name get-required-vector-prop))
(defmacro defprop-optvec
"Defines an optional vector property.
Parameters:
sym - the symbol to define.
desc - a brief description of the property.
props - a ref containing the properties.
config-valid - a ref containing a validity flag.
configs - a ref containing the list of config settings.
flag-props - the feature flag properties determining if the property is relevant.
prop-name - the name of the property.
default - the default value."
([sym desc [props config-valid configs & flag-props] prop-name]
(define-optional-property
sym desc [props config-valid configs flag-props]
prop-name get-optional-vector-prop []))
([sym desc [props config-valid configs & flag-props] prop-name default]
(define-optional-property
sym desc [props config-valid configs flag-props]
prop-name get-optional-vector-prop default)))
(defmacro defprop-int
"Defines a required integer property.
Parameters:
sym - the symbol to define.
desc - a brief description of the property.
props - a ref containing the properties.
config-valid - a ref containing a validity flag.
configs - a ref containing the list of config settings.
flag-props - the feature flag properties determining if the property is relevant.
prop-name - the name of the property."
[sym desc [props config-valid configs & flag-props] prop-name]
(define-required-property
sym desc [props config-valid configs flag-props] prop-name get-required-integer-prop))
(defmacro defprop-optint
"Defines an optional integer property.
Parameters:
sym - the symbol to define.
desc - a brief description of the property.
props - a ref containing the properties.
config-valid - a ref containing the validity flag.
flag-props - the feature flag properties determining if the property is relevant.
prop-name - the name of the property.
default - the default value."
([sym desc [props config-valid configs & flag-props] prop-name]
(define-optional-property
sym desc [props config-valid configs flag-props]
prop-name get-optional-integer-prop 0))
([sym desc [props config-valid configs & flag-props] prop-name default]
(define-optional-property
sym desc [props config-valid configs flag-props]
prop-name get-optional-integer-prop default)))
(defmacro defprop-long
"Defines a required long property.
Parameters:
sym - the symbol to define.
desc - a brief description of the property.
props - a ref containing the properties.
config-valid - a ref containing a validity flag.
configs - a ref containing the list of config settings.
flag-props - the feature flag properties determining if the property is relevant.
prop-name - the name of the property."
[sym desc [props config-valid configs & flag-props] prop-name]
(define-required-property
sym desc [props config-valid configs flag-props] prop-name get-required-long-prop))
(defmacro defprop-optlong
"Defines an optional long property.
Parameters:
sym - the symbol to define.
desc - a brief description of the property.
props - a ref containing the properties.
config-valid - a ref containing the validity flag.
prop-name - the name of the property.
flag-props - the feature flag properties determining if the property is relevant.
default - the default value."
([sym desc [props config-valid configs & flag-props] prop-name]
(define-optional-property
sym desc [props config-valid configs flag-props] prop-name get-optional-long-prop 0))
([sym desc [props config-valid configs & flag-props] prop-name default]
(define-optional-property
sym desc [props config-valid configs flag-props] prop-name get-optional-long-prop default)))
(defmacro defprop-boolean
"Defines a required Boolean property.
Parameters:
sym - the symbol to define.
desc - a brief description of the property.
props - a ref containing the properties.
config-valid - a ref containing a validity flag.
configs - a ref containing the list of config settings.
flag-props - the feature flag properties determining if the property is relevant.
prop-name - the name of the property."
[sym desc [props config-valid configs & flag-props] prop-name]
(define-required-property
sym desc [props config-valid configs flag-props] prop-name get-required-boolean-prop))
(defmacro defprop-optboolean
"Defines an optional Boolean property.
Parameters:
sym - the symbol to define.
desc - a brief description of the property.
props - a ref containing the properties.
config-valid - a ref containing the validity flag.
configs - a ref containing the list of config settings.
flag-props - the feature flag properties determining if the property is relevant.
prop-name - the name of the property.
default - the default value."
([sym desc [props config-valid configs & flag-props] prop-name]
(define-optional-property
sym desc [props config-valid configs flag-props]
prop-name get-optional-boolean-prop false))
([sym desc [props config-valid configs & flag-props] prop-name default]
(define-optional-property
sym desc [props config-valid configs flag-props]
prop-name get-optional-boolean-prop default)))
(defmacro defprop-uuid
"Defines a required UUID property
Parameters:
sym - the symbol to define.
desc - a brief description of the property.
props - a ref containing the properties.
config-valid - a ref containing a validity flag.
configs - a ref containing the list of config settings.
flag-props - the feature flag properties determining if the property is relevant.
prop-name - the name of the property."
[sym desc [props config-valid configs & flag-props] prop-name]
(define-required-property
sym desc [props config-valid configs flag-props] prop-name get-required-uuid-prop))
(defmacro defprop-optuuid
"Defines an optional UUID property.
Parameters:
sym - the symbol to define.
desc - a brief description of the property.
props - a ref containing the properties.
config-valid - a ref containing the validity flag.
configs - a ref containing the list of config settings.
flag-props - the feature flag properties determining if the property is relevant.
prop-name - the name of the property.
default - the default value."
([sym desc [props config-valid configs & flag-props] prop-name]
(define-optional-property
sym desc [props config-valid configs flag-props]
prop-name get-optional-uuid-prop false))
([sym desc [props config-valid configs & flag-props] prop-name default]
(define-optional-property
sym desc [props config-valid configs flag-props]
prop-name get-optional-uuid-prop default)))
(defn validate-config
"Validates a configuration that has been defined and loaded using this library.
Parameters:
configs - a ref containing an array of symbols that were defined.
config-valid - a ref to the configuration validity flag."
[configs config-valid]
(dorun (map #(%) @configs))
@config-valid)