-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathconfig.sh
631 lines (532 loc) · 21 KB
/
config.sh
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
626
627
628
629
630
631
#!/usr/bin/env bash
# ==============================================================================
# Home Assistant Community Add-ons: Bashio
# Bashio is a bash function library for use with Home Assistant add-ons.
#
# It contains a set of commonly used operations and can be used
# to be included in add-on scripts to reduce code duplication across add-ons.
# ==============================================================================
# ------------------------------------------------------------------------------
# Fetches a configuration value from the add-on config file.
#
# Arguments:
# $1 Key of the config option
# $2 Default value for not set config option (optional: defaults to 'null')
# ------------------------------------------------------------------------------
function bashio::config() {
local key=${1}
local default_value=${2:-null}
local query
local result
bashio::log.trace "${FUNCNAME[0]}:" "$@"
read -r -d '' query << QUERY
if (.${key} == null) then
null
elif (.${key} | type == "string") then
.${key} // empty
elif (.${key} | type == "boolean") then
.${key} // false
elif (.${key} | type == "array") then
if (.${key} == []) then
empty
else
.${key}[]
end
elif (.${key} | type == "object") then
if (.${key} == {}) then
empty
else
.${key}
end
else
.${key}
end
QUERY
options=$(bashio::addon.config)
result=$(bashio::jq "${options}" "${query}")
if [[ "${result}" == "null" ]];
then
echo "${default_value}"
else
printf "%s" "${result}"
fi
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Checks if a configuration option exists in the config file
#
# Arguments:
# $1 Key of the config option
# ------------------------------------------------------------------------------
function bashio::config.exists() {
local key=${1}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
if [[ $(bashio::config "${key}") == "null" ]]; then
return "${__BASHIO_EXIT_NOK}"
fi
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Checks if a configuration option has an actual value.
#
# Arguments:
# $1 Key of the config option
# ------------------------------------------------------------------------------
function bashio::config.has_value() {
local key=${1}
local value
bashio::log.trace "${FUNCNAME[0]}:" "$@"
value=$(bashio::config "${key}")
if [[ "${value}" == "null" ]]; then
return "${__BASHIO_EXIT_NOK}"
fi
if ! bashio::var.has_value "${value}"; then
return "${__BASHIO_EXIT_NOK}"
fi
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Checks if a configuration option has an empty value.
#
# Arguments:
# $1 Key of the config option
# ------------------------------------------------------------------------------
function bashio::config.is_empty() {
local key=${1}
local value
bashio::log.trace "${FUNCNAME[0]}:" "$@"
value=$(bashio::config "${key}")
if bashio::var.is_empty "${value}"; then
return "${__BASHIO_EXIT_OK}"
fi
if [[ "${value}" == "null" ]]; then
return "${__BASHIO_EXIT_OK}"
fi
return "${__BASHIO_EXIT_NOK}"
}
# ------------------------------------------------------------------------------
# Checks if a configuration option equals a value.
#
# Arguments:
# $1 Key of the config option
# $2 Checks if the configuration option matches
# ------------------------------------------------------------------------------
function bashio::config.equals() {
local key=${1}
local equals=${2}
local value
bashio::log.trace "${FUNCNAME[0]}:" "$@"
value=$(bashio::config "${key}")
if ! bashio::var.equals "${value}" "${equals}"; then
return "${__BASHIO_EXIT_NOK}"
fi
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Checks if a configuration option is true.
#
# Arguments:
# $1 Key of the config option
# ------------------------------------------------------------------------------
function bashio::config.true() {
local key=${1}
local value
bashio::log.trace "${FUNCNAME[0]}:" "$@"
value=$(bashio::config "${key}")
if ! bashio::var.true "${value}"; then
return "${__BASHIO_EXIT_NOK}"
fi
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Checks if a configuration option is false.
#
# Arguments:
# $1 Key of the config option
# ------------------------------------------------------------------------------
function bashio::config.false() {
local key=${1}
local value
bashio::log.trace "${FUNCNAME[0]}:" "$@"
value=$(bashio::config "${key}")
if ! bashio::var.false "${value}"; then
return "${__BASHIO_EXIT_NOK}"
fi
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Checks if a password is safe to use, using IHaveBeenPwned.
#
# Arguments:
# $1 Key of the config option
# ------------------------------------------------------------------------------
function bashio::config.is_safe_password() {
local key=${1}
local password
bashio::log.trace "${FUNCNAME[0]}:" "$@"
# If the password is safe, we'll accept it anyways.
password=$(bashio::config "${key}")
if bashio::pwned.is_safe_password "${password}"; then
return "${__BASHIO_EXIT_OK}"
fi
# If the bypass is enabled, we'll return OK.
if bashio::config.true "i_like_to_be_pwned"; then
bashio::log.warning "Have I Been Pwned bypass enabled."
return "${__BASHIO_EXIT_OK}"
fi
# If we reach this point, we'll just fail.
return "${__BASHIO_EXIT_NOK}"
}
# ------------------------------------------------------------------------------
# Require a configuration option to be set by the user.
#
# Arguments:
# $1 Key of the config option
# $2 Addition reason why this is needed
# ------------------------------------------------------------------------------
function bashio::config.require() {
local key=${1}
local reason=${2:-}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
if bashio::config.has_value "${key}"; then
return "${__BASHIO_EXIT_OK}"
fi
bashio::log.fatal
bashio::log.fatal "A required add-on configuration option is missing!"
bashio::log.fatal
bashio::log.fatal "Please set a value for the '${key}' option."
if bashio::var.has_value "${reason}"; then
bashio::log.fatal
bashio::log.fatal "This option is required because:"
bashio::log.fatal "${reason}"
fi
bashio::log.fatal
bashio::log.fatal "If unsure, check the add-on manual for more information."
bashio::log.fatal
bashio::exit.nok
}
# ------------------------------------------------------------------------------
# Suggest on setting a configuration option to the user.
#
# Arguments:
# $1 Key of the config option
# $2 Addition reason why this is needed
# ------------------------------------------------------------------------------
function bashio::config.suggest() {
local key=${1}
local reason=${2:-}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
if ! bashio::config.has_value "${key}"; then
bashio::log.warning
bashio::log.warning \
"A recommended add-on configuration option is not set."
bashio::log.warning
bashio::log.warning "The configuration key '${key}' seems to be empty."
bashio::log.warning
if bashio::var.has_value "${reason}"; then
bashio::log.warning
bashio::log.warning "Consider configuring this because:"
bashio::log.warning "${reason}"
fi
bashio::log.warning "Check the add-on manual for more information."
bashio::log.warning
fi
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Suggest on enabling a configuration option to the user.
#
# Arguments:
# $1 Key of the config option
# $2 Addition reason why this is needed
# ------------------------------------------------------------------------------
function bashio::config.suggest.true() {
local key=${1}
local reason=${2:-}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
if ! bashio::config.true "${key}"; then
bashio::log.warning
bashio::log.warning \
"A recommended add-on configuration option is not enabled."
if bashio::var.has_value "${reason}"; then
bashio::log.warning
bashio::log.warning "Consider enabling this because:"
bashio::log.warning "${reason}"
fi
bashio::log.warning
bashio::log.warning "Enable config option '${key}' hide this message."
bashio::log.warning
bashio::log.warning "Check the add-on manual for more information."
bashio::log.warning
fi
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Suggest on disabling a configuration option to the user.
#
# Arguments:
# $1 Key of the config option
# $2 Addition reason why this is needed
# ------------------------------------------------------------------------------
function bashio::config.suggest.false() {
local key=${1}
local reason=${2:-}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
if ! bashio::config.false "${key}"; then
bashio::log.warning
bashio::log.warning \
"A recommended add-on configuration option is not disabled."
if bashio::var.has_value "${reason}"; then
bashio::log.warning
bashio::log.warning "Consider disabling this because:"
bashio::log.warning "${reason}"
fi
bashio::log.warning
bashio::log.warning "Disable config option '${key}' hide this message."
bashio::log.warning
bashio::log.warning "Check the add-on manual for more information."
bashio::log.warning
fi
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Require the user to configure a username.
#
# Arguments:
# $1 Key of the config option (optional: defaults to 'username')
# ------------------------------------------------------------------------------
function bashio::config.require.username() {
local key=${1:-"username"}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
if bashio::config.has_value "${key}"; then
return "${__BASHIO_EXIT_OK}"
fi
bashio::log.fatal
bashio::log.fatal "Setting a username is required!"
bashio::log.fatal
bashio::log.fatal "Please username in the '${key}' option."
bashio::log.fatal
bashio::log.fatal "If unsure, check the add-on manual for more information."
bashio::log.fatal
bashio::exit.nok
}
# ------------------------------------------------------------------------------
# Suggest to the user to set a username, in case it is not.
#
# Arguments:
# $1 Key of the config option (optional: defaults to 'username')
# ------------------------------------------------------------------------------
function bashio::config.suggest.username() {
local key=${1}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
if ! bashio::config.has_value "${key}"; then
bashio::log.warning
bashio::log.warning \
"Setting a username is highly recommended!"
bashio::log.warning
bashio::log.warning "Define a username in the '${key}' option."
bashio::log.warning
bashio::log.warning "Check the add-on manual for more information."
bashio::log.warning
fi
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Checks if the password has been set and exits when it is not.
#
# Arguments:
# $1 Key of the config option (optional: defaults to 'password')
# ------------------------------------------------------------------------------
function bashio::config.require.password() {
local key=${1:-"password"}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
if bashio::config.has_value "${key}"; then
return "${__BASHIO_EXIT_OK}"
fi
bashio::log.fatal
bashio::log.fatal "Setting a password is required!"
bashio::log.fatal
bashio::log.fatal "Please set a password in the '${key}' option."
bashio::log.fatal
bashio::log.fatal "If unsure, check the add-on manual for more information."
bashio::log.fatal
bashio::exit.nok
}
# ------------------------------------------------------------------------------
# Suggest to set a password if it was not set.
#
# Arguments:
# $1 Key of the config option (optional: defaults to 'password')
# ------------------------------------------------------------------------------
function bashio::config.suggest.password() {
local key=${1:-"password"}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
if bashio::config.has_value "${key}"; then
return "${__BASHIO_EXIT_OK}"
fi
bashio::log.warning
bashio::log.warning "Setting a password is highly recommended!"
bashio::log.warning
bashio::log.warning "Please set a password in the '${key}' option."
bashio::log.warning
bashio::log.warning \
"If unsure, check the add-on manual for more information."
bashio::log.warning
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Require to set a password which is not in HaveIBeenPwned database.
#
# Arguments:
# $1 Key of the config option (optional: defaults to 'password')
# ------------------------------------------------------------------------------
function bashio::config.require.safe_password() {
local key=${1:-"password"}
local password
bashio::log.trace "${FUNCNAME[0]}:" "$@"
bashio::config.require.password "${key}"
password=$(bashio::config "${key}")
if bashio::pwned.is_safe_password "${password}"; then
return "${__BASHIO_EXIT_OK}"
fi
bashio::log.fatal
bashio::log.fatal "We are trying to help you to protect your system the"
bashio::log.fatal "best we can. Therefore, this add-on checks your"
bashio::log.fatal "configured password against the HaveIBeenPwned database."
bashio::log.fatal
bashio::log.fatal "Unfortunately, your configured password is considered"
bashio::log.fatal "unsafe. We highly recommend you to pick a different one."
bashio::log.fatal
bashio::log.fatal "Please change the password in the '${key}' option."
bashio::log.fatal
bashio::log.fatal "Check the add-on manual for more information."
bashio::log.fatal
bashio::exit.nok
}
# ------------------------------------------------------------------------------
# Suggest to set a password which is not in HaveIBeenPwned database.
#
# Arguments:
# $1 Key of the config option (optional: defaults to 'password')
# ------------------------------------------------------------------------------
function bashio::config.suggest.safe_password() {
local key=${1:-"password"}
local password
bashio::log.trace "${FUNCNAME[0]}:" "$@"
if ! bashio::config.has_value "${key}"; then
bashio::config.suggest.password "${key}"
return "${__BASHIO_EXIT_OK}"
fi
password=$(bashio::config "${key}")
if bashio::pwned.is_safe_password "${password}"; then
return "${__BASHIO_EXIT_OK}"
fi
bashio::log.warning
bashio::log.warning "We are trying to help you to protect your system the"
bashio::log.warning "best we can. Therefore, this add-on checks your"
bashio::log.warning "configured password against the HaveIBeenPwned database."
bashio::log.warning
bashio::log.warning "Unfortunately, your configured password is considered"
bashio::log.warning "unsafe. It is recommended to pick a different one."
bashio::log.warning
bashio::log.warning "Please change the password in the '${key}' option."
bashio::log.warning
bashio::log.warning "Check the add-on manual for more information."
bashio::log.warning
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Check if certificate files exists when SSL is enabled.
#
# Arguments:
# $1 Key of the ssl config option (optional: defaults to 'ssl')
# $2 Key of the cert file config option (optional: defaults to 'certfile')
# $3 Key of the cert key file config option (optional: defaults to 'keyfile')
# ------------------------------------------------------------------------------
function bashio::config.require.ssl() {
local key=${1:-"ssl"}
local certfile=${2:-"certfile"}
local keyfile=${3:-"keyfile"}
if ! bashio::config.true "${key}"; then
return "${__BASHIO_EXIT_OK}"
fi
if bashio::config.is_empty "${certfile}"; then
bashio::log.fatal
bashio::log.fatal "SSL has been enabled using the '${key}' option,"
bashio::log.fatal "this requires an SSL certificate file which is"
bashio::log.fatal "configured using the '${certfile}' option in the"
bashio::log.fatal "add-on configuration."
bashio::log.fatal
bashio::log.fatal "Unfortunately, the '${certfile}' option is empty."
bashio::log.fatal
bashio::log.fatal "Consider configuring or getting an SSL certificate"
bashio::log.fatal "or setting the '${key}' option to 'false' in case"
bashio::log.fatal "you are not planning on using SSL with this add-on."
bashio::log.fatal
bashio::log.fatal "Check the add-on manual for more information."
bashio::log.fatal
bashio::exit.nok
fi
if bashio::config.is_empty "${keyfile}"; then
bashio::log.fatal
bashio::log.fatal "SSL has been enabled using the '${key}' option,"
bashio::log.fatal "this requires an SSL certificate key file which is"
bashio::log.fatal "configured using the '${keyfile}' option in the"
bashio::log.fatal "add-on configuration."
bashio::log.fatal
bashio::log.fatal "Unfortunately, the '${keyfile}' option is empty."
bashio::log.fatal
bashio::log.fatal "Consider configuring or getting an SSL certificate"
bashio::log.fatal "or setting the '${key}' option to 'false' in case"
bashio::log.fatal "you are not planning on using SSL with this add-on."
bashio::log.fatal
bashio::log.fatal "Check the add-on manual for more information."
bashio::log.fatal
bashio::exit.nok
fi
if ! bashio::fs.file_exists "/ssl/$(bashio::config "${certfile}")"; then
bashio::log.fatal
bashio::log.fatal "SSL has been enabled using the '${key}' option,"
bashio::log.fatal "this requires an SSL certificate file which is"
bashio::log.fatal "configured using the '${certfile}' option in the"
bashio::log.fatal "add-on configuration."
bashio::log.fatal
bashio::log.fatal "Unfortunately, the file specified in the"
bashio::log.fatal "'${certfile}' option does not exist."
bashio::log.fatal
bashio::log.fatal "Please ensure the certificate file exists and"
bashio::log.fatal "is placed in the '/ssl/' directory."
bashio::log.fatal
bashio::log.fatal "In case you don't have SSL yet, consider getting"
bashio::log.fatal "an SSL certificate or setting the '${key}' option"
bashio::log.fatal "to 'false' in case you are not planning on using"
bashio::log.fatal "SSL with this add-on."
bashio::log.fatal
bashio::log.fatal "Check the add-on manual for more information."
bashio::log.fatal
bashio::exit.nok
fi
if ! bashio::fs.file_exists "/ssl/$(bashio::config "${keyfile}")"; then
bashio::log.fatal
bashio::log.fatal "SSL has been enabled using the '${key}' option,"
bashio::log.fatal "this requires an SSL certificate key file which is"
bashio::log.fatal "configured using the '${keyfile}' option in the"
bashio::log.fatal "add-on configuration."
bashio::log.fatal
bashio::log.fatal "Unfortunately, the file specified in the"
bashio::log.fatal "'${keyfile}' option does not exist."
bashio::log.fatal
bashio::log.fatal "Please ensure the certificate key file exists and"
bashio::log.fatal "is placed in the '/ssl/' directory."
bashio::log.fatal
bashio::log.fatal "In case you don't have SSL yet, consider getting"
bashio::log.fatal "an SSL certificate or setting the '${key}' option"
bashio::log.fatal "to 'false' in case you are not planning on using"
bashio::log.fatal "SSL with this add-on."
bashio::log.fatal
bashio::log.fatal "Check the add-on manual for more information."
bashio::log.fatal
bashio::exit.nok
fi
return "${__BASHIO_EXIT_OK}"
}