Skip to content

Commit a338b18

Browse files
authored
Fix a couple of bugs (#32)
* Fix bug in installing PostgreSQL DB install process * Remove use of legacy resolver for pip installs * Ensure there's an admin email address provided, use default if not * Ensure special characters in passwords are handled correctly when setting the initial web admin password
1 parent 42d215b commit a338b18

File tree

3 files changed

+101
-7
lines changed

3 files changed

+101
-7
lines changed

embd/factory_2.0.3

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import logging
2+
from dojo.models import Test_Type
3+
4+
PARSERS = {}
5+
# TODO remove that
6+
SCAN_SONARQUBE_API = 'SonarQube API Import'
7+
8+
9+
def register(parser_type):
10+
for scan_type in parser_type().get_scan_types():
11+
parser = parser_type()
12+
if scan_type.endswith('detailed'):
13+
parser.set_mode('detailed')
14+
register_parser(scan_type, parser)
15+
16+
17+
def register_parser(scan_type, parser):
18+
logging.debug(f"register scan_type:{scan_type} with parser:{parser}")
19+
# check double registration or registration with an existing key
20+
if scan_type in PARSERS:
21+
raise ValueError(f"Try to register an existing parser '{scan_type}'")
22+
PARSERS[scan_type] = parser
23+
24+
25+
def import_parser_factory(file, test, active, verified, scan_type=None):
26+
"""Return a parser by the scan type
27+
This function exists only for backward compatibility
28+
"""
29+
if scan_type in PARSERS:
30+
# create dynamicaly in DB
31+
test_type, created = Test_Type.objects.get_or_create(name=scan_type)
32+
if created:
33+
test_type.save()
34+
return PARSERS[scan_type]
35+
else:
36+
raise ValueError(f'Unknown Test Type {scan_type}')
37+
38+
39+
def get_choices():
40+
res = list()
41+
for key in PARSERS:
42+
res.append((key, PARSERS[key].get_label_for_scan_types(key)))
43+
return tuple(res)
44+
45+
46+
def requires_file(scan_type):
47+
if scan_type is None or scan_type not in PARSERS:
48+
return False
49+
# FIXME switch to method of the parser
50+
# parser = PARSERS[scan_type]
51+
return scan_type != SCAN_SONARQUBE_API
52+
53+
54+
import os
55+
from inspect import isclass
56+
from pkgutil import iter_modules
57+
from pathlib import Path
58+
from importlib import import_module
59+
60+
# iterate through the modules in the current package
61+
package_dir = str(Path(__file__).resolve().parent)
62+
for (path, module_name, _) in iter_modules([package_dir]):
63+
# check if it's submodule
64+
if os.path.isdir(os.path.join(package_dir, module_name)):
65+
try:
66+
# import the module and iterate through its attributes
67+
module = import_module(f"dojo.tools.{module_name}.parser")
68+
for attribute_name in dir(module):
69+
attribute = getattr(module, attribute_name)
70+
if isclass(attribute) and attribute_name.lower() == module_name.replace("_", "") + 'parser':
71+
register(attribute)
72+
except:
73+
logging.exception(f"failed to load {module_name}")

ubuntu.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ func ubuntuInstPostgreSQLClient(id string, b *osCmds) {
165165
b.id = id
166166
b.cmds = []string{
167167
"DEBIAN_FRONTEND=noninteractive apt-get install -y postgresql-client-12",
168-
"/usr/sbin/groupadd -f postgres",
169-
"/usr/sbin/useradd -s /bin/bash -m -g postgres postgres",
168+
"/usr/sbin/groupadd -f postgres", // TODO: consider using os.Group.Lookup before calling this
169+
"/usr/sbin/useradd -s /bin/bash -m -g postgres postgres", // TODO: consider using os.User.Lookup before calling this
170170
}
171171
b.errmsg = []string{
172172
"Unable to install PostgreSQL client",
@@ -276,10 +276,10 @@ func ubuntuOSPrep(id string, inst *config.InstallConfig, b *osCmds) {
276276
b.cmds = []string{
277277
"python3 -m virtualenv --python=/usr/bin/python3 " + inst.Root,
278278
inst.Root + "/bin/python3 -m pip install --upgrade pip",
279-
inst.Root + "/bin/pip3 install --use-deprecated=legacy-resolver -r " + inst.Root + "/django-DefectDojo/requirements.txt",
279+
inst.Root + "/bin/pip3 install -r " + inst.Root + "/django-DefectDojo/requirements.txt",
280280
"mkdir " + inst.Root + "/logs",
281-
"/usr/sbin/groupadd -f " + inst.OS.Group,
282-
"id " + inst.OS.User + " &>/dev/null; if [ $? -ne 0 ]; then useradd -s /bin/bash -m -g " + inst.OS.Group + " " + inst.OS.User + "; fi",
281+
"/usr/sbin/groupadd -f " + inst.OS.Group, // TODO: check with os.Group.Lookup
282+
"id " + inst.OS.User + " &>/dev/null; if [ $? -ne 0 ]; then useradd -s /bin/bash -m -g " + inst.OS.Group + " " + inst.OS.User + "; fi", // TODO: check with os.User.Lookup
283283
"chown -R " + inst.OS.User + "." + inst.OS.Group + " " + inst.Root,
284284
}
285285
b.errmsg = []string{
@@ -334,12 +334,18 @@ func ubuntuSetupDDjango(id string, inst *config.InstallConfig, b *osCmds) {
334334
addCmd(b, "cd "+inst.Root+"/django-DefectDojo && source ../bin/activate && python3 manage.py migrate",
335335
"Failed during database migrate", true)
336336

337+
// Ensure there's a value for email as the call will fail without one
338+
adminEmail := "default.user@defectdojo.org"
339+
if len(inst.Admin.Email) > 0 {
340+
// If user configures an incorrect email, this will still fail but that's on them, not godojo
341+
adminEmail = inst.Admin.Email
342+
}
337343
addCmd(b, "cd "+inst.Root+"/django-DefectDojo && source ../bin/activate && python3 manage.py createsuperuser --noinput --username=\""+
338-
inst.Admin.User+"\" --email=\""+inst.Admin.Email+"\"",
344+
inst.Admin.User+"\" --email=\""+adminEmail+"\"",
339345
"Failed while creating DefectDojo superuser", true)
340346

341347
addCmd(b, "cd "+inst.Root+"/django-DefectDojo && source ../bin/activate && "+
342-
inst.Root+"/django-DefectDojo/setup-superuser.expect "+inst.Admin.User+" "+inst.Admin.Pass,
348+
inst.Root+"/django-DefectDojo/setup-superuser.expect "+inst.Admin.User+" \""+escSpCar(inst.Admin.Pass)+"\"",
343349
"Failed while setting the password for the DefectDojo superuser", true)
344350

345351
// Roles showed up in 2.x.x

util.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,21 @@ func addRedact(s string) {
153153
sensStr = append(sensStr, s)
154154
}
155155

156+
func escSpCar(s string) string {
157+
// Replace special characters that cause issues when exec'ing in Bash
158+
fmt.Printf("Before escaping string - %s\n", s)
159+
160+
// Replace $ with \$
161+
s = strings.ReplaceAll(s, "\\", "\\\\")
162+
// Replace $ with \$
163+
s = strings.ReplaceAll(s, "$", "\\$")
164+
// Replace $ with \$
165+
s = strings.ReplaceAll(s, "`", "\\`")
166+
167+
fmt.Printf("After escaping string - %s\n", s)
168+
return s
169+
}
170+
156171
// Deemb -
157172
func deemb(f []string, o string) error {
158173
// Testing embedding files

0 commit comments

Comments
 (0)