Skip to content

Commit cc7e035

Browse files
authored
Merge pull request #532 from NASA-AMMOS/issue-530
Issue #530 - Use SafeLoader for YAML load calls
2 parents 69ad7fe + 3496113 commit cc7e035

File tree

18 files changed

+1665
-1568
lines changed

18 files changed

+1665
-1568
lines changed

ait/core/bin/ait_bsc.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python
2-
32
# Advanced Multi-Mission Operations System (AMMOS) Instrument Toolkit (AIT)
43
# Bespoke Link to Instruments and Small Satellites (BLISS)
54
#
@@ -13,18 +12,17 @@
1312
# laws and regulations. User has the responsibility to obtain export licenses,
1413
# or other export authority as may be required before exporting such
1514
# information to foreign countries or providing access to foreign persons.
16-
1715
"""
1816
Usage: ait-bsc
1917
2018
Start the ait BSC for capturing network traffic into PCAP files
2119
and the manager server for RESTful manipulation of active loggers.
2220
"""
23-
21+
import argparse
2422
import os
2523
import threading
24+
2625
import yaml
27-
import argparse
2826

2927
import ait
3028
from ait.core import bsc
@@ -49,7 +47,7 @@ def main():
4947

5048
else:
5149
with open(config_file) as log_conf:
52-
conf = yaml.load(log_conf, Loader=yaml.Loader)
50+
conf = yaml.safe_load(log_conf)
5351

5452
mngr_conf = conf["capture_manager"]
5553
host = mngr_conf["manager_server"]["host"]

ait/core/cfg.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@
1111
# laws and regulations. User has the responsibility to obtain export licenses,
1212
# or other export authority as may be required before exporting such
1313
# information to foreign countries or providing access to foreign persons.
14-
1514
"""
1615
AIT Configuration
1716
1817
The ait.core.cfg module provides classes and functions to manage
1918
(re)configurable aspects of AIT via a YAML configuration file.
2019
2120
"""
22-
2321
import os
2422
import platform
23+
import re
2524
import sys
2625
import time
27-
import re
28-
import yaml
2926
from io import IOBase
3027

28+
import yaml
29+
3130
import ait
32-
from ait.core import log, util
31+
from ait.core import log
32+
from ait.core import util
3333

3434

3535
DEFAULT_PATH_VARS = {
@@ -61,8 +61,9 @@ def expand_config_paths(
6161

6262
for p in cleaned:
6363
if not os.path.exists(p):
64-
msg = "Config parameter {}.{} specifies nonexistent path {}".format(
65-
parameter_key, name, p
64+
msg = (
65+
"Config parameter {}.{} specifies nonexistent "
66+
"path {}".format(parameter_key, name, p)
6667
)
6768
log.warn(msg)
6869

@@ -176,7 +177,7 @@ def load_yaml(filename=None, data=None):
176177
if filename:
177178
data = open(filename, "rt")
178179

179-
config = yaml.load(data, Loader=yaml.Loader)
180+
config = yaml.safe_load(data)
180181

181182
if isinstance(data, IOBase):
182183
data.close()
@@ -204,13 +205,13 @@ class AitConfigError(Exception):
204205
pass
205206

206207

207-
class AitConfigMissing(Exception):
208+
class AitConfigMissingError(Exception):
208209
"""Raised when a AIT configuration parameter is missing."""
209210

210211
def __init__(self, param):
211212
values = param, ait.config._filename
212213
format = "The parameter %s is missing from config.yaml (%s)."
213-
super(AitConfigMissing, self).__init__(format % values)
214+
super(AitConfigMissingError, self).__init__(format % values)
214215
self.param = param
215216

216217

@@ -342,7 +343,7 @@ def _datapaths(self):
342343
paths["mib"] = data["mib"]["path"]
343344

344345
except KeyError as e:
345-
raise AitConfigMissing(str(e))
346+
raise AitConfigMissingError(str(e))
346347
except Exception as e:
347348
raise AitConfigError("Error reading data paths: %s" % e)
348349

ait/core/cmd.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,23 @@
1111
# laws and regulations. User has the responsibility to obtain export licenses,
1212
# or other export authority as may be required before exporting such
1313
# information to foreign countries or providing access to foreign persons.
14-
1514
"""
1615
AIT Commands
1716
1817
The ait.core.cmd module provides commands and command dictionaries.
1918
Dictionaries contain command and argument definitions.
2019
"""
21-
2220
import os
23-
import pkg_resources
2421
import struct
25-
import yaml
2622
from io import IOBase
2723

24+
import pkg_resources
25+
import yaml
26+
2827
import ait
29-
from ait.core import json, util, log
28+
from ait.core import json
29+
from ait.core import log
30+
from ait.core import util
3031

3132

3233
MAX_CMD_WORDS = 54
@@ -124,7 +125,11 @@ def encode(self, value):
124125

125126
if type(value) == str and self.enum and value in self.enum:
126127
value = self.enum[value]
127-
return self.type.encode(*value) if type(value) in [tuple, list] else self.type.encode(value)
128+
return (
129+
self.type.encode(*value)
130+
if type(value) in [tuple, list]
131+
else self.type.encode(value)
132+
)
128133

129134
def slice(self, offset=0):
130135
"""Returns a Python slice object (e.g. for array indexing) indicating
@@ -486,7 +491,7 @@ def load(self, content):
486491
else:
487492
stream = content
488493

489-
cmds = yaml.load(stream, Loader=yaml.Loader)
494+
cmds = yaml.safe_load(stream)
490495
cmds = handle_includes(cmds)
491496
for cmd in cmds:
492497
self.add(cmd)
@@ -503,7 +508,7 @@ def getDefaultCmdDict(reload=False): # noqa
503508

504509

505510
def getDefaultDict(reload=False): # noqa
506-
create_cmd_dict_func = globals().get('createCmdDict', None)
511+
create_cmd_dict_func = globals().get("createCmdDict", None)
507512
loader = create_cmd_dict_func if create_cmd_dict_func else CmdDict
508513
return util.getDefaultDict(__name__, "cmddict", loader, reload)
509514

@@ -559,13 +564,13 @@ def YAMLCtor_include(loader, node): # noqa
559564
name = os.path.join(os.path.dirname(loader.name), node.value)
560565
data = None
561566
with open(name, "r") as f:
562-
data = yaml.load(f)
567+
data = yaml.safe_load(f)
563568
return data
564569

565570

566-
yaml.add_constructor("!include", YAMLCtor_include)
567-
yaml.add_constructor("!Command", YAMLCtor_CmdDefn)
568-
yaml.add_constructor("!Argument", YAMLCtor_ArgDefn)
569-
yaml.add_constructor("!Fixed", YAMLCtor_ArgDefn)
571+
yaml.SafeLoader.add_constructor("!include", YAMLCtor_include)
572+
yaml.SafeLoader.add_constructor("!Command", YAMLCtor_CmdDefn)
573+
yaml.SafeLoader.add_constructor("!Argument", YAMLCtor_ArgDefn)
574+
yaml.SafeLoader.add_constructor("!Fixed", YAMLCtor_ArgDefn)
570575

571576
util.__init_extensions__(__name__, globals())

ait/core/evr.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@
1111
# laws and regulations. User has the responsibility to obtain export licenses,
1212
# or other export authority as may be required before exporting such
1313
# information to foreign countries or providing access to foreign persons.
14-
1514
"""
1615
AIT Event Record (EVR) Reader
1716
1817
The ait.core.evr module is used to read the EVRs from a YAML file.
1918
"""
20-
2119
import os
22-
import pkg_resources
2320
import re
21+
22+
import pkg_resources
2423
import yaml
2524

2625
import ait.core
27-
from ait.core import dtype, json, log, util
26+
from ait.core import dtype
27+
from ait.core import json
28+
from ait.core import log
29+
from ait.core import util
2830

2931

3032
class EVRDict(dict):
@@ -59,7 +61,7 @@ def load(self, content):
5961
stream = content
6062

6163
try:
62-
evrs = yaml.load(stream, Loader=yaml.Loader)
64+
evrs = yaml.safe_load(stream)
6365
except IOError as e:
6466
msg = "Could not load EVR YAML '{}': '{}'".format(stream, str(e))
6567
log.error(msg)
@@ -196,7 +198,9 @@ def format_message(self, evr_hist_data):
196198
data_chunks.append(d)
197199
# TODO: Make this not suck
198200
except Exception:
199-
msg = "Unable to format EVR Message with data {}".format(evr_hist_data)
201+
msg = "Unable to format EVR Message with data " "{}".format(
202+
evr_hist_data
203+
)
200204
log.error(msg)
201205
raise ValueError(msg)
202206

@@ -212,9 +216,9 @@ def format_message(self, evr_hist_data):
212216
if len(formatters) == 0:
213217
return self._message
214218
else:
215-
# Python format strings cannot handle size formatter information. So something
216-
# such as %llu needs to be adjusted to be a valid identifier in python by
217-
# removing the size formatter.
219+
# Python format strings cannot handle size formatter information.
220+
# So something such as %llu needs to be adjusted to be a valid
221+
# identifier in python by removing the size formatter.
218222
msg = self._message
219223
for f in formatters:
220224
if len(f) > 1:
@@ -237,6 +241,6 @@ def YAMLCtor_EVRDefn(loader, node): # noqa
237241
return createEVRDefn(**fields) # noqa
238242

239243

240-
yaml.add_constructor("!EVR", YAMLCtor_EVRDefn)
244+
yaml.SafeLoader.add_constructor("!EVR", YAMLCtor_EVRDefn)
241245

242246
util.__init_extensions__(__name__, globals())

ait/core/limits.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# laws and regulations. User has the responsibility to obtain export licenses,
1212
# or other export authority as may be required before exporting such
1313
# information to foreign countries or providing access to foreign persons.
14-
1514
"""
1615
AIT Limits
1716
@@ -20,20 +19,26 @@
2019
The expected limits.yaml should follow this schema:
2120
2221
- !Limit
23-
source: -- telemetry source for the limit. should follow format 'Packet.field_name'
22+
source: -- telemetry source for the limit. should follow format
23+
'Packet.field_name'
2424
desc: -- description of the limit
25-
units: -- the units used for possible conversion depending on the units set in the
26-
telemetry dictionary
25+
units: -- the units used for possible conversion depending on
26+
the units set in the telemetry dictionary
2727
lower: -- lower limits
28-
error: -- trigger error if telemetry value exceeds this lower bound (exclusive)
29-
warn: -- trigger warning if telemetry value exceeds this lower bound (exclusive)
28+
error: -- trigger error if telemetry value exceeds this lower
29+
bound (exclusive)
30+
warn: -- trigger warning if telemetry value exceeds this lower
31+
bound (exclusive)
3032
upper: -- upper limits
31-
error: -- trigger error if telemetry value exceeds this upper bound (exclusive)
32-
warn: -- trigger warning if telemetry value exceeds this upper bound (exclusive)
33+
error: -- trigger error if telemetry value exceeds this upper
34+
bound (exclusive)
35+
warn: -- trigger warning if telemetry value exceeds this upper
36+
bound (exclusive)
3337
value: -- enumerated values to trigger error/warning
3438
error: -- trigger error if telemetry value == or in list of strings
3539
warn: -- trigger warning if telemetry value == or in list of strings
36-
when: -- when condition for specifying the necessary state when this limit applies
40+
when: -- when condition for specifying the necessary state when this
41+
limit applies
3742
persist: -- number of seconds the value must persist before limits trigger
3843
3944
For example:
@@ -62,14 +67,15 @@
6267
- BAR
6368
6469
"""
65-
6670
import os
71+
from io import IOBase
72+
6773
import pkg_resources
6874
import yaml
69-
from io import IOBase
7075

7176
import ait
72-
from ait.core import json, util
77+
from ait.core import json
78+
from ait.core import util
7379

7480

7581
class Thresholds(json.SlotSerializer, object):
@@ -201,7 +207,7 @@ def load(self, content):
201207
else:
202208
stream = content
203209

204-
limits = yaml.load(stream, Loader=yaml.Loader)
210+
limits = yaml.safe_load(stream)
205211

206212
for lmt in limits:
207213
self.add(lmt)
@@ -230,6 +236,6 @@ def YAMLCtor_LimitDefinition(loader, node): # noqa
230236
return createLimitDefinition(**fields) # noqa
231237

232238

233-
yaml.add_constructor("!Limit", YAMLCtor_LimitDefinition)
239+
yaml.SafeLoader.add_constructor("!Limit", YAMLCtor_LimitDefinition)
234240

235241
util.__init_extensions__(__name__, globals())

0 commit comments

Comments
 (0)