cloudtools / troposphere

troposphere - Python library to create AWS CloudFormation descriptions
BSD 2-Clause "Simplified" License
4.92k stars 1.45k forks source link

policy validator exception #2157

Closed GergelyMolnar closed 11 months ago

GergelyMolnar commented 1 year ago

Hi,

First of all, good job guys, thanks for the effort. I only used your tool for a few days but it helped me to catch some mistakes early.

I found a bug that produces a very confusing error. Essentially the bug hides a helpful message. When an incorrect policy document format is used the validator is supposed to tell the given and the expected format(s). But instead, it throws a TypeError caused by the validator code itself. E.g.

<class 'troposphere.iam.Policy'>: None.PolicyDocument function validator 'policytypes' threw exception:
Traceback (most recent call last):
  File "/home/user/cf_project/eval.cf_policy.py", line 93, in <module>
    iam.Policy(
  File "/home/user/cf_project/venv/lib/python3.10/site-packages/troposphere/__init__.py", line 455, in __init__
    super().__init__(title, **kwargs)
  File "/home/user/cf_project/venv/lib/python3.10/site-packages/troposphere/__init__.py", line 210, in __init__
    self.__setattr__(k, v)
  File "/home/user/cf_project/venv/lib/python3.10/site-packages/troposphere/__init__.py", line 266, in __setattr__
    value = expected_type(value)
  File "/home/user/cf_project/venv/lib/python3.10/site-packages/troposphere/validators/iam.py", line 88, in policytypes
    return validate_policytype(policy)
  File "/home/user/cf_project/venv/lib/python3.10/site-packages/troposphere/compat.py", line 21, in validate_policytype
    "Invalid policy type: is %s, expected %s" % type(policy), policytypes
TypeError: not enough arguments for format string

With the issue fixed the validator actually shows the intended message, which is actually helpful.

<class 'troposphere.iam.Policy'>: None.PolicyDocument function validator 'policytypes' threw exception:
Traceback (most recent call last):
  File "/home/user/cf_project/eval.parameter_injection.py", line 123, in <module>
    iam.Policy(
  File "/home/user/cf_project/venv/lib/python3.10/site-packages/troposphere/__init__.py", line 455, in __init__
    super().__init__(title, **kwargs)
  File "/home/user/cf_project/venv/lib/python3.10/site-packages/troposphere/__init__.py", line 210, in __init__
    self.__setattr__(k, v)
  File "/home/user/cf_project/venv/lib/python3.10/site-packages/troposphere/__init__.py", line 266, in __setattr__
    value = expected_type(value)
  File "/home/user/cf_project/venv/lib/python3.10/site-packages/troposphere/validators/iam.py", line 88, in policytypes
    return validate_policytype(policy)
  File "/home/user/cf_project/venv/lib/python3.10/site-packages/troposphere/compat.py", line 26, in validate_policytype
    raise TypeError(
TypeError: Invalid policy type: is <class 'str'>, expected (<class 'dict'>, <class 'awacs.aws.Policy'>, <class 'awacs.aws.PolicyDocument'>)

I made the changes, it's trivial, but I can't push it to the repo.

Cheers!

markpeek commented 11 months ago

Thank you for the issue, fixed now.