Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix None type scenario in get_actions_for_service #246

Merged
merged 8 commits into from Oct 12, 2020
Merged

fix None type scenario in get_actions_for_service #246

merged 8 commits into from Oct 12, 2020

Conversation

reetasingh
Copy link
Contributor

@reetasingh reetasingh commented Oct 11, 2020

What does this PR do?

Address NoneType error raised as part of issue #239

What gif best describes this PR or how it makes you feel?

Completion checklist

@reetasingh reetasingh changed the title fix exception handling in get_service_prefix fix exception handling in get_service_prefix_data Oct 11, 2020
@reetasingh
Copy link
Contributor Author

@kmcquade please review , this is my first PR for this repo. Hoping to make more contributions soon

@reetasingh
Copy link
Contributor Author

Python 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from policy_sentry.querying.actions import get_actions_for_service
>>>
>>>
>>> def example():
...     actions = get_actions_for_service('abc')  # Then you can leverage any method that requires access to the database.
...     for action in actions:
...         print(action)
...
>>> if __name__ == '__main__':
...     example()
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "<stdin>", line 2, in example
  File "C:\Users\rakes\git\policy_sentry\policy_sentry\querying\actions.py", line 26, in get_actions_for_service
    service_prefix_data = get_service_prefix_data(service_prefix)
  File "C:\Users\rakes\git\policy_sentry\policy_sentry\shared\iam_data.py", line 27, in get_service_prefix_data
    raise Exception(f"Service prefix {service_prefix} not found.")
Exception: Service prefix abc not found.
>>>

@kmcquade
Copy link
Collaborator

Hey @reetasingh! Thanks for making a PR.

I think that you might have made the fix in the wrong file.

  File "/usr/local/Cellar/policy_sentry/0.8.7/libexec/lib/python3.8/site-packages/policy_sentry/command/query.py", line 84, in action_table
    query_action_table(name, service, access_level, condition, wildcard_only, fmt)
  File "/usr/local/Cellar/policy_sentry/0.8.7/libexec/lib/python3.8/site-packages/policy_sentry/command/query.py", line 163, in query_action_table
    output = get_actions_for_service(service)
  File "/usr/local/Cellar/policy_sentry/0.8.7/libexec/lib/python3.8/site-packages/policy_sentry/querying/actions.py", line 28, in get_actions_for_service
    for item in service_prefix_data["privileges"]:
TypeError: 'NoneType' object is not subscriptable

The above indicates two different options to investigate:

  1. policy_sentry.shared.iam_data.get_service_prefix_data
  2. policy_sentry.querying.actions.get_actions_for_service

get_service_prefix_data

The section you chose - policy_sentry.shared.iam_data.get_service_prefix_data - is working as intended, actually. Some background:

I use Policy Sentry as a library, as do several others that I'm aware of - most notably in Cloudsplaining. Cloudsplaining pulls live AWS IAM Policies for analysis. Sometimes these IAM Policies reference AWS Services that have not yet been included in the IAM Definition that we store at policy_sentry/shared/data/iam-definition.json. Our IAM definition gets updated once a month.

But for Cloudsplaining, we need to make sure that if a service does not exist, that Policy Sentry just logs the issue and does not break the scan by throwing an exception. I've encountered this issue a few times - where Policy Sentry gets a new version at the beginning of the month, and AWS releases 4 new service prefixes within that same month, before we have time to release a new version of Policy Sentry. That would break the scan.

So, given the above - get_service_prefix_data is working as intended.

get_actions_for_service

I think this is the one that actually needs fixing. The function below looks like it will still try to add an object of type None to the list of results. I think that is what causes the issue I mentioned.

service_prefix_data = get_service_prefix_data(service_prefix)
results = []
for item in service_prefix_data["privileges"]:
results.append(f"{service_prefix}:{item}")
return results

I think this means you would have to change your PR to be focused on get_actions_for_service instead of get_service_prefix_data. Might want to just start with a fresh PR though and close this one - that should be cleaner.

@reetasingh
Copy link
Contributor Author

reetasingh commented Oct 12, 2020

service_prefix_data["privileges"]

Thanks for the background @kmcquade
Now i understand why raising an exception is not feasible here.

actually, service_prefix_data["privileges"] is the one which is failing because service_prefix_data is None and we are trying to find privileges in a None type

do you want me to add a check for not None before calling service_prefix_data["privileges"]

also, given that service_prefix_data["privileges"] is being called in multiple places, that check should be everywhere where we are using service_prefix_data["privileges"]

@kmcquade
Copy link
Collaborator

Yeah that works! Thank you!

@reetasingh
Copy link
Contributor Author

@kmcquade addressed the review comments

@reetasingh reetasingh changed the title fix exception handling in get_service_prefix_data fix None handling case in get_actions_for_servic Oct 12, 2020
@reetasingh reetasingh changed the title fix None handling case in get_actions_for_servic fix None type case in get_actions_for_servic Oct 12, 2020
@reetasingh reetasingh changed the title fix None type case in get_actions_for_servic fix None type case in get_actions_for_service Oct 12, 2020
@reetasingh reetasingh changed the title fix None type case in get_actions_for_service fix None type scenario in get_actions_for_service Oct 12, 2020
@kmcquade kmcquade self-requested a review October 12, 2020 22:30
Copy link
Collaborator

@kmcquade kmcquade left a comment

Choose a reason for hiding this comment

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

Good stuff. Thanks!

@kmcquade kmcquade merged commit fa55d7c into salesforce:master Oct 12, 2020
saikirankv pushed a commit to saikirankv/policy_sentry that referenced this pull request Nov 18, 2020
* fix NoneType error in get_actions_for_service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants