From d8b05f4c1a81609451f49a76c3e29d9d15a21f76 Mon Sep 17 00:00:00 2001 From: Mark Goddard Date: Fri, 22 May 2020 19:16:02 +0100 Subject: [PATCH] Improve error reporting in password utilities The kolla-genpwd and kolla-mergepwd commands can be used to manipulate the kolla passwords.yml file. The format is a YAML encoded dict of password variable names to their values. If the format is not a dict, the error messages are unhelpful. In particular, this can happen if the file is encrypted e.g. via Ansible Vault. For kolla-genpwd: AttributeError: 'NoneType' object has no attribute 'items' For kolla-mergepwd: AttributeError: 'NoneType' object has no attribute 'update' This change adds a more friendly message. Change-Id: I27f0835b904e05006ae401adf383090322e1b891 Closes-Bug: #1880220 (cherry picked from commit 620b808cf366596d7cdd521cd5b44a973d5cdeab) --- kolla_ansible/cmd/genpwd.py | 4 ++++ kolla_ansible/cmd/mergepwd.py | 9 +++++++++ .../notes/improve-pwd-errors-7563a3cc941c3091.yaml | 6 ++++++ 3 files changed, 19 insertions(+) create mode 100644 releasenotes/notes/improve-pwd-errors-7563a3cc941c3091.yaml diff --git a/kolla_ansible/cmd/genpwd.py b/kolla_ansible/cmd/genpwd.py index 6e9ec49778..a368e2e340 100755 --- a/kolla_ansible/cmd/genpwd.py +++ b/kolla_ansible/cmd/genpwd.py @@ -59,6 +59,10 @@ def genpwd(passwords_file, length, uuid_keys, ssh_keys, blank_keys, with open(passwords_file, 'r') as f: passwords = yaml.safe_load(f.read()) + if not isinstance(passwords, dict): + print("ERROR: Passwords file not in expected key/value format") + sys.exit(1) + for k, v in passwords.items(): if (k in ssh_keys and (v is None diff --git a/kolla_ansible/cmd/mergepwd.py b/kolla_ansible/cmd/mergepwd.py index 4c697629a0..ffed17b31c 100755 --- a/kolla_ansible/cmd/mergepwd.py +++ b/kolla_ansible/cmd/mergepwd.py @@ -13,6 +13,7 @@ # limitations under the License. import argparse +import sys import yaml @@ -23,6 +24,14 @@ def mergepwd(old, new, final): with open(new, "r") as new_file: new_passwords = yaml.safe_load(new_file) + if not isinstance(old_passwords, dict): + print("ERROR: Old passwords file not in expected key/value format") + sys.exit(1) + + if not isinstance(new_passwords, dict): + print("ERROR: New passwords file not in expected key/value format") + sys.exit(1) + new_passwords.update(old_passwords) with open(final, "w") as destination: diff --git a/releasenotes/notes/improve-pwd-errors-7563a3cc941c3091.yaml b/releasenotes/notes/improve-pwd-errors-7563a3cc941c3091.yaml new file mode 100644 index 0000000000..75f3f48156 --- /dev/null +++ b/releasenotes/notes/improve-pwd-errors-7563a3cc941c3091.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Improves error reporting in ``kolla-genpwd`` and ``kolla-mergepwd`` when + input files are not in the expected format. `LP#1880220 + `__.