Skip to content

Commit

Permalink
Merge pull request #27 from wwkimball/bugfix/eyaml_encryption_bad_format
Browse files Browse the repository at this point in the history
Fix: EYAML blocks forgotten by yaml-set v1.0.4
  • Loading branch information
wwkimball authored May 28, 2019
2 parents ba6ebd5 + c7dcc13 commit 32fe6be
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
18 changes: 13 additions & 5 deletions bin/yaml-set
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ from ruamel.yaml.parser import ParserError

from yamlpath import YAMLPath
from yamlpath.exceptions import YAMLPathException
from yamlpath.enums import YAMLValueFormats, PathSeperators
from yamlpath.eyaml.exceptions import EYAMLCommandException
from yamlpath.eyaml.enums import EYAMLOutputFormats
from yamlpath.eyaml import EYAMLProcessor
from yamlpath.enums import YAMLValueFormats, PathSeperators

# pylint: disable=locally-disabled,unused-import
import yamlpath.patches
from yamlpath.func import clone_node
from yamlpath.wrappers import ConsolePrinter

# Implied Constants
MY_VERSION = "1.0.4"
MY_VERSION = "1.0.5"

def processcli():
"""Process command-line arguments."""
Expand Down Expand Up @@ -237,6 +239,12 @@ def main():
if not change_nodes:
log.warning("Nothing to do!")
exit(0)
elif len(change_nodes) == 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_nodes[0])

log.debug("Collected nodes:")
log.debug(change_nodes)
Expand Down Expand Up @@ -292,7 +300,6 @@ def main():
# format, despite being properly typed. To restore the original
# written form, reverse the conversion, here.
old_value = change_nodes[0]
old_format = YAMLValueFormats.from_node(old_value)
if (
(old_format is YAMLValueFormats.FOLDED
or old_format is YAMLValueFormats.LITERAL
Expand All @@ -311,7 +318,6 @@ def main():
# Set the requested value
log.verbose("Setting the new value for {}.".format(change_path))
if args.eyamlcrypt:
output_type = "string"
try:
format_type = YAMLValueFormats.from_str(args.format)
except NameError:
Expand All @@ -324,8 +330,10 @@ def main():
if format_type is YAMLValueFormats.DEFAULT:
format_type = old_format

output_type = EYAMLOutputFormats.STRING
if format_type in [YAMLValueFormats.FOLDED, YAMLValueFormats.LITERAL]:
output_type = "block"
output_type = EYAMLOutputFormats.BLOCK

try:
processor.set_eyaml_value(
change_path, new_value
Expand Down
19 changes: 19 additions & 0 deletions tests/test_eyaml_eyamlprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ruamel.yaml import YAML

import yamlpath.patches
from yamlpath.enums import YAMLValueFormats
from yamlpath.eyaml.enums import EYAMLOutputFormats
from yamlpath.eyaml import EYAMLProcessor
from yamlpath.wrappers import ConsolePrinter
Expand Down Expand Up @@ -249,6 +250,24 @@ def test_happy_set_eyaml_value(self, logger_f, eyamldata_f, eyamlkeys, yaml_path

assert EYAMLProcessor.is_eyaml_value(encvalue)

@requireseyaml
@pytest.mark.parametrize("yaml_path,newval,eoformat,yvformat", [
("/aliased::secrets/novel_values/ident", "New, novel, encrypted identity in BLOCK format", EYAMLOutputFormats.BLOCK, YAMLValueFormats.FOLDED),
("/aliased::secrets/string_values/ident", "New, novel, encrypted identity in STRING format", EYAMLOutputFormats.STRING, YAMLValueFormats.BARE),
])
def test_preserve_old_blockiness(self, logger_f, eyamldata_f, eyamlkeys, yaml_path, newval, eoformat, yvformat):
processor = EYAMLProcessor(logger_f, eyamldata_f, privatekey=eyamlkeys[0], publickey=eyamlkeys[1])
processor.set_eyaml_value(yaml_path, newval, output=eoformat)

encvalue = None
encformat = YAMLValueFormats.DEFAULT
for encnode in processor.get_nodes(yaml_path):
encvalue = encnode
encformat = YAMLValueFormats.from_node(encvalue)
break

assert EYAMLProcessor.is_eyaml_value(encvalue) and yvformat == encformat

def test_none_eyaml_value(self):
assert False == EYAMLProcessor.is_eyaml_value(None)

Expand Down

0 comments on commit 32fe6be

Please sign in to comment.