Skip to content

Commit b3e0154

Browse files
Updating jason parsing of sku to account for empty string and whitespace (#3820)
* Updating jason parsing of sku to account for empty string and whitespace * Changed comment to better represent return value and made changes to comply with linter * Fixed tabbing inconsistency
1 parent 661d7cc commit b3e0154

File tree

1 file changed

+33
-11
lines changed
  • microsoft/testsuites/security

1 file changed

+33
-11
lines changed

microsoft/testsuites/security/fips.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
class FipsTests(TestSuite):
3131
@TestCaseMetadata(
3232
description="""
33-
Ensures that an AZL machine is in the correct FIPS mode.
33+
Ensures that an AZL machine is in the correct FIPS mode.
3434
""",
3535
priority=3,
3636
requirement=simple_requirement(
@@ -114,15 +114,15 @@ def _get_expected_fips_mode(
114114
log (Logger): The logger instance for logging messages.
115115
node (Node): The node object representing the target machine.
116116
variables (Dict[str, Any]): A dictionary of variables containing the
117-
'testing_fips_image' key.
117+
'testing_fips_image' key.
118118
119119
Returns:
120120
bool: The expected FIPS mode (True for enabled,
121-
False for disabled or None if we can't determine.).
121+
False for disabled or None if we can't determine.).
122122
"""
123123
log.debug(f"get_expected_fips_mode: variables is '{variables}'")
124124

125-
# First, see if the test runner specified the FIPS image type in the variables.
125+
# First, check FIPS image type in variables
126126
testing_fips_image = variables.get("testing_fips_image", None)
127127
if testing_fips_image is not None:
128128
log.debug(
@@ -131,10 +131,10 @@ def _get_expected_fips_mode(
131131
)
132132
return to_bool(testing_fips_image)
133133

134-
# Fall back to checking the image SKU from the azure metadata endpoint.
134+
# Fall back to checking image SKU from azure metadata endpoint
135135
log.debug(
136-
"get_expected_fips_mode: testing_fips_image is not set; falling back to "
137-
"marketplace image sku."
136+
"get_expected_fips_mode: testing_fips_image is not set; "
137+
"falling back to marketplace image sku."
138138
)
139139
response = node.tools[Curl].fetch(
140140
arg="--max-time 2 --header Metadata:true --silent",
@@ -143,17 +143,39 @@ def _get_expected_fips_mode(
143143
url=METADATA_ENDPOINT,
144144
)
145145

146-
# If we successfully fetched the metadata, check the image SKU.
146+
# If metadata fetch successful, check image SKU
147147
if response.exit_code == 0:
148148
log.debug(
149149
"get_expected_fips_mode: successfully fetched metadata; "
150150
"checking image SKU."
151151
)
152152
json_response = json.loads(response.stdout)
153-
return "fips" in json_response["compute"]["sku"]
154153

155-
# If we couldn't determine the FIPS mode, return False as a default.
154+
# Safely get compute and sku with default empty values
155+
compute = json_response.get("compute", {})
156+
sku = compute.get("sku", "")
157+
158+
# Ensure SKU is a string type before processing
159+
if not isinstance(sku, str):
160+
log.debug(
161+
f"get_expected_fips_mode: Expected string for SKU, "
162+
f"got {type(sku)}"
163+
)
164+
return None
165+
166+
# Skip empty or whitespace-only SKUs
167+
if not sku.strip():
168+
log.debug(
169+
"get_expected_fips_mode: SKU is empty or contains only whitespace"
170+
)
171+
return None
172+
173+
# Check if SKU contains 'fips' (case-insensitive)
174+
return "fips" in sku.lower()
175+
176+
# If we couldn't determine the FIPS mode, return None as a default.
156177
log.debug(
157-
"get_expected_fips_mode: could not determine the FIPS mode; returning None."
178+
"get_expected_fips_mode: could not determine the FIPS mode; "
179+
"returning None."
158180
)
159181
return None

0 commit comments

Comments
 (0)