From 777415731dda378bca0f196f0c6a21b4a68548ae Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Mon, 11 Jan 2021 12:08:55 +0100 Subject: [PATCH] fix: handle custom yaml tag '!unsafe' --- ansiblelater/utils/rulehelper.py | 7 +++++++ ansiblelater/utils/yamlhelper.py | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/ansiblelater/utils/rulehelper.py b/ansiblelater/utils/rulehelper.py index 62690cf..3a85ce2 100644 --- a/ansiblelater/utils/rulehelper.py +++ b/ansiblelater/utils/rulehelper.py @@ -15,6 +15,7 @@ from .yamlhelper import action_tasks from .yamlhelper import normalize_task from .yamlhelper import normalized_yaml from .yamlhelper import parse_yaml_linenumbers +from .yamlhelper import UnsafeTag def get_tasks(candidate, settings): @@ -160,6 +161,9 @@ def get_raw_yaml(candidate, settings): if not candidate.faulty: try: with codecs.open(candidate.path, mode="rb", encoding="utf-8") as f: + yaml.add_constructor( + UnsafeTag.yaml_tag, UnsafeTag.yaml_constructor, Loader=yaml.SafeLoader + ) content = yaml.safe_load(f) except yaml.YAMLError as e: errors.append( @@ -176,6 +180,9 @@ def run_yamllint(candidate, options="extends: default"): if not candidate.faulty: try: with codecs.open(candidate.path, mode="rb", encoding="utf-8") as f: + yaml.add_constructor( + UnsafeTag.yaml_tag, UnsafeTag.yaml_constructor, Loader=yaml.SafeLoader + ) yaml.safe_load(f) for problem in linter.run(f, YamlLintConfig(options)): diff --git a/ansiblelater/utils/yamlhelper.py b/ansiblelater/utils/yamlhelper.py index c2d01ee..43d7015 100644 --- a/ansiblelater/utils/yamlhelper.py +++ b/ansiblelater/utils/yamlhelper.py @@ -576,3 +576,16 @@ def normalized_yaml(file, options): except (yaml.parser.ParserError, yaml.scanner.ScannerError) as e: raise LaterError("syntax error", e) return lines + + +class UnsafeTag: + """Handle custom yaml unsafe tag.""" + + yaml_tag = u"!unsafe" + + def __init__(self, value): + self.unsafe = value + + @staticmethod + def yaml_constructor(loader, node): + return loader.construct_scalar(node)