Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
("", " ", " ", YAMLValueFormats.BARE),
("", '" "', '" "', YAMLValueFormats.DQUOTE),
("", "' '", "' '", YAMLValueFormats.SQUOTE),
("", " ", " ", YAMLValueFormats.FOLDED),
("", " ", " ", YAMLValueFormats.LITERAL),
(True, False, False, YAMLValueFormats.BOOLEAN),
(True, "no", False, YAMLValueFormats.BOOLEAN),
(1.1, 1.2, 1.2, YAMLValueFormats.FLOAT),
(ScalarFloat(1.1, anchor="test"), 1.2, 1.2, YAMLValueFormats.FLOAT),
(1, 2, 2, YAMLValueFormats.INT),
])
def test_make_new_node(self, source, value, check, vformat):
assert check == make_new_node(source, value, vformat)
def test_preserve_old_blockiness(self, quiet_logger, eyamldata_f, old_eyaml_keys, yaml_path, newval, eoformat, yvformat):
processor = EYAMLProcessor(quiet_logger, eyamldata_f, privatekey=old_eyaml_keys[0], publickey=old_eyaml_keys[1])
processor.set_eyaml_value(yaml_path, newval, output=eoformat)
encvalue = None
encformat = YAMLValueFormats.DEFAULT
for encnode in processor.get_nodes(yaml_path):
encvalue = unwrap_node_coords(encnode)
encformat = YAMLValueFormats.from_node(encvalue)
break
assert EYAMLProcessor.is_eyaml_value(encvalue) and yvformat == encformat
(True, False, False, YAMLValueFormats.BOOLEAN),
(True, "no", False, YAMLValueFormats.BOOLEAN),
(1.1, 1.2, 1.2, YAMLValueFormats.FLOAT),
(ScalarFloat(1.1, anchor="test"), 1.2, 1.2, YAMLValueFormats.FLOAT),
(1, 2, 2, YAMLValueFormats.INT),
])
def test_make_new_node(self, source, value, check, vformat):
assert check == make_new_node(source, value, vformat)
("aliases[&test_scalarstring]", "A whole new BARE value.", YAMLValueFormats.BARE, True),
("aliases[&test_scalarstring]", "A whole new DQUOTE value.", YAMLValueFormats.DQUOTE, True),
("aliases[&test_scalarstring]", "A whole new SQUOTE value.", YAMLValueFormats.SQUOTE, True),
("aliases[&test_scalarstring]", "A whole new FOLDED value.", YAMLValueFormats.FOLDED, True),
("aliases[&test_scalarstring]", "A whole new LITERAL value.", YAMLValueFormats.LITERAL, True),
("aliases[&new_scalarfloat]", 10.01, YAMLValueFormats.FLOAT, False),
("aliases[&new_scalarint]", 42, YAMLValueFormats.INT, False),
("aliases[&test_scalarstring]", "A whole new bare value.", "bare", True),
("aliases[&test_scalarstring]", "A whole new dquote value.", "dquote", True),
("aliases[&test_scalarstring]", "A whole new squote value.", "squote", True),
("aliases[&test_scalarstring]", "A whole new folded value.", "folded", True),
("aliases[&test_scalarstring]", "A whole new literal value.", "literal", True),
("aliases[&new_scalarfloat]", 1.1, "float", False),
("aliases[&new_scalarint]", 24, "int", False),
("aliases[&new_scalarstring]", "Did not previously exist.", YAMLValueFormats.BARE, False),
])
def test_happy_set_value(yamlpath, yamldata, search, compare, vformat, mexist):
def test_preserve_old_blockiness(self, quiet_logger, eyamldata_f, old_eyaml_keys, yaml_path, newval, eoformat, yvformat):
processor = EYAMLProcessor(quiet_logger, eyamldata_f, privatekey=old_eyaml_keys[0], publickey=old_eyaml_keys[1])
processor.set_eyaml_value(yaml_path, newval, output=eoformat)
encvalue = None
encformat = YAMLValueFormats.DEFAULT
for encnode in processor.get_nodes(yaml_path):
encvalue = unwrap_node_coords(encnode)
encformat = YAMLValueFormats.from_node(encvalue)
break
assert EYAMLProcessor.is_eyaml_value(encvalue) and yvformat == encformat
2. value (any) The value to encrypt
3. output (EYAMLOutputFormats) the output format of the encryption
4. mustexist (bool) Indicates whether YAML Path must
specify a pre-existing node
Returns: N/A
Raises:
- `YAMLPathException` when YAML Path is invalid
"""
self.logger.verbose(
"Encrypting value(s) for {}."
.format(yaml_path)
)
encval: str = self.encrypt_eyaml(value, output)
emit_format: YAMLValueFormats = YAMLValueFormats.FOLDED
if output is EYAMLOutputFormats.STRING:
emit_format = YAMLValueFormats.DEFAULT
self.set_value(
yaml_path,
encval,
mustexist=mustexist,
value_format=emit_format
)
publickey=args.publickey, privatekey=args.privatekey)
try:
for node_coordinate in processor.get_nodes(
change_path, mustexist=(args.mustexist or args.saveto),
default_value=("" if new_value else " ")):
log.debug('Got "{}" from {}.'.format(node_coordinate, change_path))
change_node_coordinates.append(node_coordinate)
except YAMLPathException as ex:
log.critical(ex, 1)
if len(change_node_coordinates) == 1:
# When there is exactly one result, its old format can be known. This
# is necessary to retain whether the replacement value should be
# represented later as a multi-line string when the new value is to be
# encrypted.
old_format = YAMLValueFormats.from_node(
change_node_coordinates[0].node)
log.debug("Collected nodes:")
log.debug(change_node_coordinates)
# Check the value(s), if desired
if args.check:
for node_coordinate in change_node_coordinates:
if processor.is_eyaml_value(node_coordinate.node):
# Sanity check: If either --publickey or --privatekey were set
# then they must both be set in order to decrypt this value.
# This is enforced only when the value must be decrypted due to
# a --check request.
if (
(args.publickey and not args.privatekey)
or (args.privatekey and not args.publickey)
old_value = old_value.replace(" ", "\n")
try:
processor.set_value(
saveto_path, clone_node(old_value),
value_format=old_format)
except YAMLPathException as ex:
log.critical(ex, 1)
# Set the requested value
log.verbose("Setting the new value for {}.".format(change_path))
if args.eyamlcrypt:
# If the user hasn't specified a format, use the same format as the
# value being replaced, if known.
format_type = YAMLValueFormats.from_str(args.format)
if format_type is YAMLValueFormats.DEFAULT:
format_type = old_format
output_type = EYAMLOutputFormats.STRING
if format_type in [YAMLValueFormats.FOLDED, YAMLValueFormats.LITERAL]:
output_type = EYAMLOutputFormats.BLOCK
try:
processor.set_eyaml_value(
change_path, new_value
, output=output_type
, mustexist=False
)
except EYAMLCommandException as ex:
log.critical(ex, 2)
else:
processor.set_value(change_path, new_value, value_format=args.format)
if len(change_node_coordinates) > 1:
log.critical(
"It is impossible to meaningly save more than one matched"
+ " value. Please omit --saveto or set --change to affect"
+ " exactly one value.", 1)
saveto_path = YAMLPath(args.saveto, pathsep=args.pathsep)
log.verbose("Saving the old value to {}.".format(saveto_path))
# Folded EYAML values have their embedded newlines converted to spaces
# when read. As such, writing them back out breaks their original
# format, despite being properly typed. To restore the original
# written form, reverse the conversion, here.
old_value = change_node_coordinates[0].node
if (
(old_format is YAMLValueFormats.FOLDED
or old_format is YAMLValueFormats.LITERAL
)
and EYAMLProcessor.is_eyaml_value(old_value)
):
old_value = old_value.replace(" ", "\n")
try:
processor.set_value(
saveto_path, clone_node(old_value),
value_format=old_format)
except YAMLPathException as ex:
log.critical(ex, 1)
# Set the requested value
log.verbose("Setting the new value for {}.".format(change_path))
if args.eyamlcrypt:
default=YAMLValueFormats.DEFAULT
* pathsep (PathSeperators) Forced YAML Path segment seperator; set
only when automatic inference fails;
default = PathSeperators.AUTO
Returns: N/A
Raises:
- `YAMLPathException` when YAML Path is invalid
"""
if self.data is None:
return
mustexist: bool = kwargs.pop("mustexist", False)
value_format: YAMLValueFormats = kwargs.pop("value_format",
YAMLValueFormats.DEFAULT)
pathsep: PathSeperators = kwargs.pop("pathsep", PathSeperators.AUTO)
if isinstance(yaml_path, str):
yaml_path = YAMLPath(yaml_path, pathsep)
elif pathsep is not PathSeperators.AUTO:
yaml_path.seperator = pathsep
if mustexist:
self.logger.debug(
"Processor::set_value: Seeking required node at {}."
.format(yaml_path)
)
found_nodes: int = 0
for req_node in self._get_required_nodes(
self.data, yaml_path
):