Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cloud_governance/main/environment_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,17 +392,18 @@ def get_aws_account_alias_name(self):
def get_env(var: str, defval=''):
lcvar = var.lower()
dashvar = lcvar.replace('_', '-')
env_value = os.environ.get(var) or os.environ.get(var.upper()) or os.environ.get(var.lower()) or defval

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Empty string handling bug in case-insensitive lookup.

The or chaining treats empty strings as falsy, which means an explicitly set environment variable like ES_PORT='' would be skipped in favor of checking other case variations or the default value. While unlikely in practice, this could cause confusing behavior where an explicitly set value is ignored.

🔧 Proposed fix using explicit existence checks
-        env_value = os.environ.get(var) or os.environ.get(var.upper()) or os.environ.get(var.lower()) or defval
+        for key in (var, var.upper(), var.lower()):
+            if key in os.environ:
+                env_value = os.environ[key]
+                break
+        else:
+            env_value = defval

This approach respects explicitly set empty strings while still providing the case-insensitive fallback behavior.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
env_value = os.environ.get(var) or os.environ.get(var.upper()) or os.environ.get(var.lower()) or defval
for key in (var, var.upper(), var.lower()):
if key in os.environ:
env_value = os.environ[key]
break
else:
env_value = defval
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cloud_governance/main/environment_variables.py` at line 395, The line
assigning env_value currently treats empty strings as falsy and can skip an
explicitly set empty env var; update the lookup in the scope where env_value is
assigned (the expression using os.environ.get(var) / var.upper() / var.lower()
in environment_variables.py) to perform explicit existence checks (e.g., check
"if var in os.environ" then use os.environ[var], else check var.upper(),
var.lower(), else use defval) so that an explicitly set empty string is
preserved while still providing case-insensitive fallbacks and finally the
default.

parser = argparse.ArgumentParser(description='Run CloudGovernance', allow_abbrev=False)
if lcvar == dashvar:
parser.add_argument(f"--{lcvar}", default=os.environ.get(var, defval), type=str, metavar='String', help=var)
parser.add_argument(f"--{lcvar}", default=env_value, type=str, metavar='String', help=var)
else:
parser.add_argument(f"--{lcvar}", f"--{dashvar}", default=os.environ.get(var, defval), type=str,
parser.add_argument(f"--{lcvar}", f"--{dashvar}", default=env_value, type=str,
metavar='String', help=var)
args, ignore = parser.parse_known_args()
if hasattr(args, lcvar):
return getattr(args, lcvar)
else:
return os.environ.get(var, defval)
return env_value

@staticmethod
def get_boolean_from_environment(var: str, defval: bool):
Expand Down