From 9bcf517ad4ef86803f299d6a0e523ebe49b94caa Mon Sep 17 00:00:00 2001 From: Kevin Zou <17015060+nkzou@users.noreply.github.com> Date: Wed, 12 Jul 2023 10:28:31 -0400 Subject: [PATCH] add alphanumeric check to app/api key config creation (#781) * add alphanumeric check to app/api key config creation * refactor api/app key reading and handle EOF from pipe --- datadog/dogshell/common.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/datadog/dogshell/common.py b/datadog/dogshell/common.py index 0d9dc6311..251e6582b 100644 --- a/datadog/dogshell/common.py +++ b/datadog/dogshell/common.py @@ -76,14 +76,22 @@ def load(self, config_file, api_key, app_key, api_host): response = get_input("%s does not exist. Would you like to" " create it? [Y/n] " % config_file) if response.strip().lower() in ["", "y"]: # Read the api and app keys from stdin - api_key = get_input( - "What is your api key? (Get it here: " - "https://app.datadoghq.com/account/settings#api) " - ) - app_key = get_input( - "What is your application key? (Generate one here: " - "https://app.datadoghq.com/account/settings#api) " - ) + while True: + api_key = get_input( + "What is your api key? (Get it here: " + "https://app.datadoghq.com/account/settings#api) " + ) + if api_key.isalnum(): + break + print("Datadog api keys can only contain alphanumeric characters.") + while True: + app_key = get_input( + "What is your app key? (Get it here: " + "https://app.datadoghq.com/account/settings#api) " + ) + if app_key.isalnum(): + break + print("Datadog app keys can only contain alphanumeric characters.") # Write the config file config.add_section("Connection") @@ -98,7 +106,7 @@ def load(self, config_file, api_key, app_key, api_host): # Abort print_err("Exiting\n") sys.exit(1) - except KeyboardInterrupt: + except (KeyboardInterrupt, EOFError): # Abort print_err("\nExiting") sys.exit(1)