From 0dda5b3a280ca5efed0ede05f338e1f97f5d003e Mon Sep 17 00:00:00 2001 From: Reuben Cartwright Date: Wed, 4 Sep 2024 09:52:35 +0000 Subject: [PATCH] tools: Fix script overwrite credentials behaviour This commit makes the script correctly ask before overwriting credentials. Previously, the script checked for 'dummy_thingname' and the default endpoint in 'aws_clientcredential.h' and only asked if these defaults were not present. These strings occur in more than one place in the header file so so this check is not sufficient to say the credentials #define'd have not been modified by the user. This commit makes the script check the entirety of the #define line. Signed-off-by: Reuben Cartwright --- tools/scripts/createIoTThings.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tools/scripts/createIoTThings.py b/tools/scripts/createIoTThings.py index 141451d..1bffb25 100644 --- a/tools/scripts/createIoTThings.py +++ b/tools/scripts/createIoTThings.py @@ -510,17 +510,12 @@ def _write_aws_clientcredential_h(flags): Parameters: flags (Flags): contains metadata needed for AWS and OTA updates (e.g. credentials). - fileDir (str): the project's base directory. E.g. contains top-level README.md. """ fileDir = os.path.dirname(os.path.realpath("__file__")) template_has_correct_format = ( lambda contents: "clientcredentialMQTT_BROKER_ENDPOINT " in contents and "clientcredentialIOT_THING_NAME " in contents ) - template_has_been_edited = ( - lambda contents: "dummy.endpointid.amazonaws.com" not in contents - or "dummy_thingname" not in contents - ) # Try to find a template for 'aws_clientcredential.h' template = "" # We assume flags.targetApplication is not ApplicationType.UNDEFINED @@ -550,7 +545,22 @@ def _write_aws_clientcredential_h(flags): ) logging.warning(err_msg) return False - if template_has_been_edited(template): + # if template has been edited, ask before overwriting. + DEFAULT_ENDPOINT = ( + "#define clientcredentialMQTT_BROKER_ENDPOINT " + + 'dummy.endpointid.amazonaws.com"' + ) + DEFAULT_THING_NAME = ( + "#define clientcredentialIOT_THING_NAME " '"dummy_thingname"' + ) + # Remove spaces for robustness. + template_without_spaces = template.replace(" ", "") + if ( + DEFAULT_ENDPOINT.replace(" ", "") in template_without_spaces + and DEFAULT_THING_NAME.replace(" ", "") in template_without_spaces + ): + logging.debug("aws_clientcredential.h has not been edited. Overwriting.") + else: warn_msg = ( "Your aws_clientcredential.h file at " + credentialFileTemplate