This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configtest.py
142 lines (114 loc) · 4.22 KB
/
configtest.py
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
#!/usr/bin/env python3
"""
configtest.py
Tests the validity of your configuration in settings.py.
"""
import os
import settings as settings
# Default settings
defaults = {
"UPLOAD_FOLDER": "/path/to/images",
"ALLOWED_EXTENSIONS": [".png", ".jpg", ".jpeg", ".svg", ".bmp", ".gif", ".ico", ".webp"],
"ROOTURL": "https://example.com/",
"SAVELOG": "savelog.log",
"SAVELOG_CHMOD": "0o644",
"SAVELOG_KEYPREFIX": 4,
}
deftypes = {
"UPLOAD_FOLDER": str,
"ALLOWED_EXTENSIONS": list,
"ROOTURL": str,
"SAVELOG": str,
"SAVELOG_CHMOD": int,
"SAVELOG_KEYPREFIX": int,
}
# Check for unset settings
checksettings = list(defaults.keys())
unset_settings = [i for i in defaults.keys() if i not in dir(settings)]
if len(unset_settings) > 0:
for unset in unset_settings:
checksettings.remove(unset)
print(f"[!] {unset} is unset. The default value is type {deftypes[unset].__name__} with value {defaults[unset]}")
else:
print("[" + u"\u2713" + "] Found all required settings!")
# Check if types of settings are correct
typesgood = True
typeswrong = []
for testtype in checksettings:
if not isinstance(getattr(settings, testtype), deftypes[testtype]):
print(f"[!] {testtype} requires {deftypes[testtype].__name__}, but is {type(getattr(settings, testtype)).__name__}")
typeswrong.append(testtype)
typesgood = False
if typesgood:
print("[" + u"\u2713" + "] Types are good!")
# Check if allowed extensions all start with a .
invalid_exts = []
if "ALLOWED_EXTENSIONS" in checksettings:
for e in settings.ALLOWED_EXTENSIONS:
if not e.startswith("."):
invalid_exts.append(e)
if len(invalid_exts) > 0:
print("[!] The following extensions listed in ALLOWED_EXTENSIONS are invalid:")
for e in invalid_exts:
print(f" {e} is listed in ALLOWED_EXTENSIONS, but doesn't start with a .")
else:
print("[" + u"\u2713" + "] ALLOWED_EXTENSIONS is good!")
# Check if UPLOAD_FOLDER exists
uploadfolder_exists = True
if "UPLOAD_FOLDER" in checksettings:
if not os.path.isdir(settings.UPLOAD_FOLDER):
uploadfolder_exists = False
print(f"[!] The directory set in UPLOAD_FOLDER ('{settings.UPLOAD_FOLDER}') doesn't exist!")
else:
print("[" + u"\u2713" + "] UPLOAD_FOLDER exists!")
# Check if ROOTURL starts with http(s):// and ends with /
rooturl_good = True
if "ROOTURL" in checksettings:
if settings.ROOTURL.startswith("http://") or settings.ROOTURL.startswith("https://"):
pass
else:
rooturl_good = False
print("[!] ROOTURL does not start with `http://` or `https://`! This may cause issues!")
if not settings.ROOTURL.endswith("/"):
rooturl_good = False
print("[!] ROOTURL does not end with a `/`. This WILL cause issues!")
if not rooturl_good:
print(" With your current settings, this is what a generated url would look like:")
print(f" {settings.ROOTURL}example.png")
else:
print("[" + u"\u2713" + "] ROOTURL is good!")
# Ask the user if SAVELOG is the intended filename
if "SAVELOG" in checksettings:
print(f"[*] SAVELOG was interpreted to be {settings.SAVELOG}")
print("[*] If this is not the intended filename, please fix it.")
# Show summary
print()
print("----- SUMMARY -----")
summarygood = True
if len(unset_settings) > 0:
summarygood = False
print("Unset settings:")
for unset in unset_settings:
print(f" {unset}")
if len(typeswrong) > 0:
summarygood = False
print("Incorrect types:")
for wtype in typeswrong:
print(f" {wtype}")
if len(invalid_exts) > 0:
summarygood = False
print("Invalid extensions:")
for wext in invalid_exts:
print(f" '{wext}'")
if not uploadfolder_exists:
summarygood = False
print(f"UPLOAD_FOLDER ({settings.UPLOAD_FOLDER}) does not exist!")
if not rooturl_good:
summarygood = False
print("ROOTURL may cause issues!")
print("With current settings, this is what a generated URL would look like:")
print(f"{settings.ROOTURL}example.png")
if "SAVELOG" in checksettings:
print(f"[*] SAVELOG is {settings.SAVELOG}")
if summarygood:
print("[" + u"\u2713" + "] This configuration passes all tests!")