Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_bad_encryption_keys(self, quiet_logger):
processor = EYAMLProcessor(quiet_logger, None)
processor.privatekey = "/no/such/file"
processor.publickey = "/no/such/file"
with pytest.raises(EYAMLCommandException):
processor.encrypt_eyaml("test")
def test_no_encrypt_without_eyaml(self, quiet_logger):
processor = EYAMLProcessor(quiet_logger, None)
processor.eyaml = None
with pytest.raises(EYAMLCommandException):
processor.encrypt_eyaml("test")
def test_impossible_decryption(self, quiet_logger, old_eyaml_keys):
processor = EYAMLProcessor(quiet_logger, None)
testval = "ENC[...]"
with pytest.raises(EYAMLCommandException):
processor.decrypt_eyaml(testval)
def test_not_can_run_eyaml(self, quiet_logger):
processor = EYAMLProcessor(quiet_logger, None)
processor.eyaml = None
assert False == processor._can_run_eyaml()
def test_happy_set_eyaml_value(self, quiet_logger, eyamldata_f, old_eyaml_keys, yaml_path, compare, mustexist, output_format):
processor = EYAMLProcessor(quiet_logger, eyamldata_f, privatekey=old_eyaml_keys[0], publickey=old_eyaml_keys[1])
# Set the test value
processor.set_eyaml_value(yaml_path, compare, output_format, mustexist)
# Ensure the new value is encrypted
encvalue = None
for encnode in processor.get_nodes(yaml_path):
encvalue = unwrap_node_coords(encnode)
break
assert EYAMLProcessor.is_eyaml_value(encvalue)
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
def test_find_eyaml_paths(self, quiet_logger, eyamldata_f):
processor = EYAMLProcessor(quiet_logger, eyamldata_f)
expected = [
"aliases[&secretIdentity]",
"aliases[&secretPhrase]",
"anchored::secrets.aliased_values.ident",
"anchored::secrets.aliased_values.phrase",
"anchored::secrets.array_of_array_idents[0][0]",
"aliased::secrets.novel_values.ident",
"aliased::secrets.novel_values.phrase",
"aliased::secrets.string_values.ident",
"aliased::secrets.string_values.phrase",
]
actual = []
for path in processor.find_eyaml_paths():
actual.append(str(path))
assert actual == expected
+ " 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:
# 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)
def main():
"""Main code."""
# Process any command-line arguments
args = processcli()
log = ConsolePrinter(args)
validateargs(args, log)
processor = EYAMLProcessor(log, None, binary=args.eyaml)
# Prep the YAML parser
yaml = get_yaml_editor()
# Process the input file(s)
in_file_count = len(args.yaml_files)
exit_state = 0
for yaml_file in args.yaml_files:
file_changed = False
backup_file = yaml_file + ".bak"
seen_anchors = []
# Each YAML_FILE must actually be a file
if not isfile(yaml_file):
log.error("Not a file: {}".format(yaml_file))
exit_state = 2
search_values = False
search_keys = True
elif args.keynames:
search_keys = True
if args.include_aliases is IncludeAliases.INCLUDE_ALL_ALIASES:
include_key_aliases = True
include_value_aliases = True
elif args.include_aliases is IncludeAliases.INCLUDE_KEY_ALIASES:
include_key_aliases = True
elif args.include_aliases is IncludeAliases.INCLUDE_VALUE_ALIASES:
include_value_aliases = True
# Prepare the YAML processor
yaml = get_yaml_editor()
processor = EYAMLProcessor(
log, None, binary=args.eyaml,
publickey=args.publickey, privatekey=args.privatekey)
# Process the input file(s)
exit_state = 0
# pylint: disable=too-many-nested-blocks
for yaml_file in args.yaml_files:
# Try to open the file
yaml_data = get_yaml_data(yaml, log, yaml_file)
if yaml_data is None:
# An error message has already been logged
exit_state = 3
continue
# Process all searches