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
|
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction
from django.core.management import call_command
from orm.models import SrtSetting, DataSource, Access, User
from django.core.exceptions import ObjectDoesNotExist
import os
import traceback
import warnings
def DN(path):
if path is None:
return ""
else:
return os.path.dirname(path)
class Command(BaseCommand):
args = ""
help = "Verifies that the configured settings are valid and usable, or prompts the user to fix the settings."
def __init__(self, *args, **kwargs):
super(Command, self).__init__(*args, **kwargs)
self.guesspath = DN(DN(DN(DN(DN(DN(DN(__file__)))))))
def _verify_srt_source(self):
ds_loaded = {}
needs_import = False
if 0 == DataSource.objects.all().count():
needs_import = True
else:
for source in DataSource.objects.all():
# save data source loaded flags
ds_loaded[source.description] = source.loaded
if not source.loaded:
needs_import = True
if needs_import:
try:
print("Loading default settings")
call_command("loaddata", "settings")
# Import the Common fixture if it's present
with warnings.catch_warnings():
warnings.filterwarnings(
action="ignore",
message="^.*No fixture named.*$")
print("Importing Common settings if present")
try:
call_command("loaddata", "common")
except:
print("NOTE: optional fixture 'common' not found")
# Import the Mitre fixture if it's present
with warnings.catch_warnings():
warnings.filterwarnings(
action="ignore",
message="^.*No fixture named.*$")
print("Importing Mitre settings if present")
try:
call_command("loaddata", "mitre")
except:
print("NOTE: optional fixture 'mitre' not found")
# Import the NIST fixture if it's present
with warnings.catch_warnings():
warnings.filterwarnings(
action="ignore",
message="^.*No fixture named.*$")
print("Importing NIST settings if present")
try:
call_command("loaddata", "nist")
except:
print("NOTE: optional fixture 'nist' not found")
# Import the Sample_Test fixture if it's present
with warnings.catch_warnings():
warnings.filterwarnings(
action="ignore",
message="^.*No fixture named.*$")
print("Importing Sample Test settings if present")
try:
call_command("loaddata", "samples")
except:
print("NOTE: optional fixture 'samples' not found")
# restore data source loaded flags
for source in DataSource.objects.all():
if source.description in ds_loaded.keys():
source.loaded = ds_loaded[source.description]
source.save()
# we run data import after config update
print("\nImporting information from the data sources, "
"please wait.\nYou can re-update any time later "
"by running srt/lib/manage.py "
"lsupdates\n")
call_command("lsupdates")
except Exception as e:
print("Failure while trying to setup SRT: %s"
% e)
traceback.print_exc()
return 0
def _test_settings(self,key):
key_record,create = SrtSetting.objects.get_or_create(name=key)
if key in os.environ.keys():
key_record.value = 'yes' if os.environ[key].startswith('1') else 'no'
key_record.save()
elif create:
key_record.value = 'no'
key_record.save()
#print("%s = %s" % (ley,key_record.value))
def _verify_default_settings(self):
# verify that default settings are there
if SrtSetting.objects.filter(name='DEFAULT_RELEASE').count() != 1:
SrtSetting.objects.filter(name='DEFAULT_RELEASE').delete()
SrtSetting.objects.get_or_create(name='DEFAULT_RELEASE', value='')
# TEST: set up the test flags based on ENVIRONMENT values
# * export TEST_SKIP_NIST_IMPORT=1: we already have NIST data in the DB
# * export TEST_SKIP_CPE_IMPORT=1: we do not need to (re-)scan the CPEs for vulnerable CVEs
# * export TEST_MINIMAL_DB=1: only load the minimal database items for fast GUI tests
self._test_settings('TEST_SKIP_NIST_IMPORT')
self._test_settings('TEST_SKIP_CPE_IMPORT')
self._test_settings('TEST_MINIMAL_DB')
# TEMP: set up default user info
current_user = SrtSetting.objects.get_or_create(name='current_user')[0]
current_user.value = User.USER_GUEST
current_user.save()
current_user_access = SrtSetting.objects.get_or_create(name='current_user_access')[0]
current_user_access.value = User.READER
current_user_access.save()
return 0
def handle(self, **options):
retval = 0
retval += self._verify_default_settings()
retval += self._verify_srt_source()
return retval
|