From 5392ed09f125aff7ecc64831db679be9e7563831 Mon Sep 17 00:00:00 2001 From: Simon Kheng Date: Mon, 13 Jun 2022 17:34:56 +0200 Subject: [PATCH] [Blueprint] Prevent hidden artifacts parsing Blueprint extension was parsing all files in the artifacts folder including hidden files. This unwanted behavior can also raise errors (eg. UnicodeDecodeError). This commit fixes this by preventing hidden Linux and Windows files from being parsed. closes #4984 --- src/blueprint/azext_blueprint/custom.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/blueprint/azext_blueprint/custom.py b/src/blueprint/azext_blueprint/custom.py index 5a15c98b849..b34992598c2 100644 --- a/src/blueprint/azext_blueprint/custom.py +++ b/src/blueprint/azext_blueprint/custom.py @@ -7,6 +7,7 @@ import json import os +import stat from knack.util import CLIError from azure.cli.core.util import user_confirmation from azure.core.exceptions import HttpResponseError @@ -48,12 +49,15 @@ def import_blueprint_with_artifacts(cmd, for filename in os.listdir(os.path.join(input_path, 'artifacts')): artifact_name = filename.split('.')[0] filepath = os.path.join(input_path, 'artifacts', filename) - with open(filepath) as artifact_file: - try: - artifact = json.load(artifact_file) - art_dict[artifact_name] = artifact - except json.decoder.JSONDecodeError as ex: - raise CLIError('JSON decode error for {}: {}'.format(filepath, str(ex))) from ex + # skip hidden files + if ((os.name != 'nt' and not filename.startswith('.')) or + (os.name == 'nt' and not bool(os.stat(filepath).st_file_attributes & stat.FILE_ATTRIBUTE_HIDDEN))): + with open(filepath) as artifact_file: + try: + artifact = json.load(artifact_file) + art_dict[artifact_name] = artifact + except json.decoder.JSONDecodeError as ex: + raise CLIError('JSON decode error for {}: {}'.format(filepath, str(ex))) from ex except FileNotFoundError as ex: raise CLIError('File not Found: {}'.format(str(ex))) from ex