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

Add Roles to GCP IAM policy lookup - EXPANDR 5945 #30477

Conversation

BigEasyJ
Copy link
Contributor

Contributing to Cortex XSOAR Content

Make sure to register your contribution by filling the contribution registration form

The Pull Request will be reviewed only after the contribution registration form is filled.

Status

  • In Progress
  • Ready
  • In Hold - (Reason for hold)

Related Issues

EXPANDR-5945

Description

Updated the gcp_iam_project_iam_policy_get_command command to support filtering by roles.

Must have

  • Tests
  • Documentation

@content-bot content-bot added Contribution Thank you! Contributions are always welcome! External PR Xsoar Support Level Indicates that the contribution is for XSOAR supported pack labels Oct 26, 2023
@content-bot content-bot changed the base branch from master to contrib/PaloAltoNetworks_EXPANDR-5945-GCP-IAM October 26, 2023 17:27
@content-bot
Copy link
Collaborator

Thank you for your contribution. Your generosity and caring are unrivaled! Make sure to register your contribution by filling the Contribution Registration form, so our content wizard @sapirshuker will know the proposed changes are ready to be reviewed.
For your convenience, here is a link to the contributions SLAs document.

@content-bot
Copy link
Collaborator

Hi @BigEasyJ, thanks for contributing to a Cortex XSOAR supported pack. To receive credit for your generous contribution please follow this link.

@@ -87,6 +87,8 @@ script:
- defaultValue: '1'
description: The page number of the results to retrieve. Minimum value is 1.
name: page
- description: 'A list of roles. Must be given as a comma separated list (Ex: "roles/bigquery.admin, roles/editor, roles/owner").'
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- description: 'A list of roles. Must be given as a comma separated list (Ex: "roles/bigquery.admin, roles/editor, roles/owner").'
- description: 'A comma-separated list of roles. (Ex: "roles/bigquery.admin, roles/editor, roles/owner").'

@@ -385,7 +385,8 @@ Retrieves the IAM access control policy for the specified folder.
| --- | --- | --- |
| folder_name | The folder name for which the policy is being requested. For example, folders/12342. | Required |
| limit | The maximum number of results to retrieve. Minimum value is 1. Default is 50. | Optional |
| page | The page number of the results to retrieve. Minimum value is 1. Default is 1. | Optional |
| page | The page number of the results to retrieve. Minimum value is 1. Default is 1. | Optional |
| roles | A comma separated list of roles. Must be given as a comma separated list (Ex: "roles/bigquery.admin, roles/editor, roles/owner"). | Optional |
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
| roles | A comma separated list of roles. Must be given as a comma separated list (Ex: "roles/bigquery.admin, roles/editor, roles/owner"). | Optional |
| roles | A comma-separated list of roles. (Ex: "roles/bigquery.admin, roles/editor, roles/owner"). | Optional |

@ShirleyDenkberg
Copy link
Contributor

@sapirshuker Doc review completed.

Copy link
Contributor

@sapirshuker sapirshuker left a comment

Choose a reason for hiding this comment

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

Hey, @BigEasyJ
Thank you for your contribution!
Good work :)
Please see my comments.
In addition, Please update the docker image, you can see the error here

Comment on lines 90 to 91
- description: 'A list of roles. Must be given as a comma separated list (Ex: "roles/bigquery.admin, roles/editor, roles/owner").'
name: roles
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- description: 'A list of roles. Must be given as a comma separated list (Ex: "roles/bigquery.admin, roles/editor, roles/owner").'
name: roles
- description: 'A list of roles. Must be given as a comma separated list (Ex: "roles/bigquery.admin, roles/editor, roles/owner").'
name: roles
isArray: true
required: false

if roles and bindings:
bindings_roles_only = []
for index, entry in enumerate(bindings):
if entry["role"] in roles:
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use entry.get("role")

outputs["bindings"] = bindings[start:end]
if len(bindings) < 50:
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this change? The limit default value is 50 but could be larger, why do we need to change the header?
Additionally, you check the len of the original list without paging (bindings[start:end]), is that your intention?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The limit default value is 50 but could be larger, why do we need to change the header?
I have updated it to use limit. The default header would come from get_pagination_readable_message which would use limit and that would be an incorrect output when filtering by a list of roles. If this was not changed whe using !gcp-iam-project-iam-policy-get project_name="projects/target_project" roles="roles/editor,roles/owner" the example would look like:

Current page size: 50
Showing page 1 out of others that may exist.

Since there are only 2 roles ( roles/editor, roles/owner), it should be:

Current size: 2
```

> Additionally, you check the len of the original list without paging (bindings[start:end]), is that your intention?

Yes, because I would like to return all possible roles, but I could page the roles if we think a user would input over 50 roles into a list.


##### GCP-IAM

Updated the ***gcp_iam_project_iam_policy_get_command*** command, to support filtering by roles.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Updated the ***gcp_iam_project_iam_policy_get_command*** command, to support filtering by roles.
Updated the ***gcp-iam-project-iam-policy-get*** command, to support filtering by roles.

Comment on lines 1245 to 1247
for index, entry in enumerate(bindings):
if entry["role"] in roles:
bindings_roles_only.append(bindings.pop(index))
Copy link
Contributor

Choose a reason for hiding this comment

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

You're changing the bindings list while going through it, I don't think it's a good idea to do it like that. It could cause bugs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to retrieve index value only.

@@ -1221,7 +1221,7 @@ def update_time_format(data: Union[dict, list], keys: list) -> list:

def generate_iam_policy_command_output(response: dict, resource_name: str = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add the roles arg to the doc string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if entry["role"] in roles:
bindings_roles_only.append(bindings.pop(index))

bindings = bindings_roles_only
Copy link
Contributor

Choose a reason for hiding this comment

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

If you don't have any results after filtering, what happens?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Screenshot 2023-10-31 at 2 06 41 AM

BigEasyJ and others added 3 commits October 30, 2023 23:57
- Update role input description
- Pop to index
- remove explicit conditional
@sapirshuker sapirshuker self-requested a review November 1, 2023 11:54
Copy link
Contributor

@sapirshuker sapirshuker left a comment

Choose a reason for hiding this comment

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

Hey, thanks for making the changes!
Awesome! Let's add some unit tests for this functionality.
Also, I notice that the generate_iam_policy_command_output function is being called from 7 different places. Will changing the readable header have an impact on every call where len(bindings) < limit is true? Just wondering if this is what you intended, because I think it might affect other commands too.

@BigEasyJ
Copy link
Contributor Author

BigEasyJ commented Nov 1, 2023

I notice that the generate_iam_policy_command_output function is being called from 7 different places. Will changing the readable header have an impact on every call where len(bindings) < limit is true? Just wondering if this is what you intended, because I think it might affect other commands too.

There are only 3 places that use limit and page.

  • gcp_iam_project_iam_policy_get_command
  • gcp_iam_folder_iam_policy_get_command
  • gcp_iam_organization_iam_policy_get_command

I have updated the readable header to work for all of them if they do not have more than the limit (which would mean it has less than 1 page). I would have liked to not affect those commands, but this change does work for any paging output with minimal changes to trying to avoid affecting the other commands.

Also, @sapirshuker do I need to make changes to the Flake8 errors that were previously existing?

@content-bot content-bot added Community Contribution Form Filled Whether contribution form filled or not. labels Nov 1, 2023
@sapirshuker
Copy link
Contributor

Hi @BigEasyJ , we haven’t heard from you in a while.
I am waiting to the addition of unit-tests.
Please feel free to reach out to me here or on Slack.
Thanks again for contributing to our repo, hope to hear from you soon

@BigEasyJ
Copy link
Contributor Author

BigEasyJ commented Nov 6, 2023

Sorry, I was expecting a meeting fro the demo, also @sapirshuker I'm unsure what additional unit tests are needed as I have updated the unit test primarily in question.

@sapirshuker sapirshuker added the pending-demo Demo pending label Nov 7, 2023
@sapirshuker
Copy link
Contributor

Hey, @BigEasyJ
I sent you a Slack message about the options to fix the demo issue.

@sapirshuker
Copy link
Contributor

Hey @BigEasyJ
we haven’t heard from you in a while.
Do you need any help with to implement the changes from the demo and fix the validation errors?
Please feel free to reach out to me here or on Slack.
Thank you

@BigEasyJ
Copy link
Contributor Author

Hi @sapirshuker I was out on Friday and not in on Sundays, I will get back to you as soon as I can.

@sapirshuker sapirshuker added the ready-for-instance-test In contribution PRs, this label will cause a trigger of a build with a modified pack from the PR. label Nov 14, 2023
@content-bot
Copy link
Collaborator

content-bot commented Nov 14, 2023

For the Reviewer: Successfully created a pipeline in Gitlab with url: https://code.pan.run/xsoar/content/-/pipelines/6908138

@sapirshuker sapirshuker added ready-for-instance-test In contribution PRs, this label will cause a trigger of a build with a modified pack from the PR. and removed ready-for-instance-test In contribution PRs, this label will cause a trigger of a build with a modified pack from the PR. labels Nov 16, 2023
@sapirshuker
Copy link
Contributor

Build failed due to a known issue CIAC-6419

Copy link
Contributor

@sapirshuker sapirshuker left a comment

Choose a reason for hiding this comment

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

Hey please see my comments

@@ -2959,8 +2970,8 @@ def gcp_iam_service_account_generate_access_token_command(client: Client, args:
CommandResults: outputs, readable outputs and raw response for XSOAR.

"""
service_account_email = args['service_account_email']
lifetime = args['lifetime']
service_account_email = args.get('service_account_email', '')
Copy link
Contributor

Choose a reason for hiding this comment

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

did you do it on purpose?

@sapirshuker sapirshuker self-requested a review November 16, 2023 16:29
Copy link
Contributor

@sapirshuker sapirshuker left a comment

Choose a reason for hiding this comment

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

Great!

@sapirshuker sapirshuker merged commit 735db7a into demisto:contrib/PaloAltoNetworks_EXPANDR-5945-GCP-IAM Nov 16, 2023
20 of 21 checks passed
xsoar-bot pushed a commit to xsoar-contrib/content that referenced this pull request Nov 19, 2023
* Add Roles to GCP IAM policy lookup - EXPANDR 5945 (demisto#30477)

* Update gcp_iam_project_iam_policy_get_command

- Add roles to gcp_iam_project_iam_policy_get_command

* Remove Prints

* Update header

* Update MD header

* Add release notes and metadata

* Some of the suggested changes

* Update role

- Update role input description
- Pop to index
- remove explicit conditional

* Update unit test, docker, and header verbiage

* Update header

* Flake8 fixes

* Update release note

* revert changes

---------

Co-authored-by: John <40349459+BigEasyJ@users.noreply.github.com>
Co-authored-by: sapirshuker <sshuker@paloaltonetworks.com>
Co-authored-by: Sapir Shuker <49246861+sapirshuker@users.noreply.github.com>
sapirshuker added a commit that referenced this pull request Dec 21, 2023
* Add Roles to GCP IAM policy lookup - EXPANDR 5945 (#30477)

* Update gcp_iam_project_iam_policy_get_command

- Add roles to gcp_iam_project_iam_policy_get_command

* Remove Prints

* Update header

* Update MD header

* Add release notes and metadata

* Some of the suggested changes

* Update role

- Update role input description
- Pop to index
- remove explicit conditional

* Update unit test, docker, and header verbiage

* Update header

* Flake8 fixes

* Update release note

* revert changes

---------

Co-authored-by: John <40349459+BigEasyJ@users.noreply.github.com>
Co-authored-by: sapirshuker <sshuker@paloaltonetworks.com>
Co-authored-by: Sapir Shuker <49246861+sapirshuker@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community Contribution Form Filled Whether contribution form filled or not. Contribution Thank you! Contributions are always welcome! docs-approved External PR pending-contributor The PR is pending the response of its creator pending-demo Demo pending post-demo ready-for-instance-test In contribution PRs, this label will cause a trigger of a build with a modified pack from the PR. Xsoar Support Level Indicates that the contribution is for XSOAR supported pack
Projects
None yet
5 participants