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

Exception handler - Okta, AWS IAM #29063

Merged
merged 10 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions Packs/AWS-IAM/Integrations/AWS-IAM/AWS-IAM.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import demistomock as demisto # noqa: F401
from CommonServerPython import * # noqa: F401
import botocore.exceptions

from datetime import datetime, date
import urllib3.util

Expand Down Expand Up @@ -82,7 +81,15 @@ def create_login_profile(args, client): # pragma: no cover


def get_user(args, client): # pragma: no cover
response = client.get_user(UserName=args.get('userName'))
try:
response = client.get_user(UserName=args.get('userName'))
except Exception as e:
if 'NoSuchEntity' in str(e):
return_outputs(f'User {args.get("userName")} was not found.')
return
else:
raise e

user = response['User']
data = ({
'UserName': user['UserName'],
Expand Down
38 changes: 38 additions & 0 deletions Packs/AWS-IAM/Integrations/AWS-IAM/AWS-IAM_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ def untag_user(self):
def get_access_key_last_used(self):
pass

def get_user(self):
pass

@property
def exceptions(self):
raise NoSuchEntityException


class Mock_Exceptions(BaseException):
def NoSuchEntityException(self, UserName):
raise NoSuchEntityException


class NoSuchEntityException(BaseException):
pass


@pytest.mark.parametrize('args, res', PAGINATION_CHECK)
def test_list_user_policies(mocker, args, res):
Expand Down Expand Up @@ -430,3 +446,25 @@ def test_create_tag_dicts_list(tags_ls, expected_output):
"""
tags_dicts_ls = AWS_IAM.create_tag_dicts_list(tags_ls)
assert expected_output == tags_dicts_ls


def test_get_user_not_found_user_exception(mocker):
"""
Given:
- user_name - user name to retrieve policies for.
When:
- After running a get_user command
Then:
- Ensure that no exception was raised, and assert the readable output.
"""
client = Boto3Client()
args = {'userName': 'userName'}
mocker.patch.object(Boto3Client, "get_user", side_effect=Exception('An error occurred (NoSuchEntity) when calling the '
'GetUser operation: The user with name yayyyy cannot '
'be found.'))
mocker.patch.object(demisto, 'results')

AWS_IAM.get_user(args, client)
contents = demisto.results.call_args[0][0]

assert 'User userName was not found.' in contents.get('HumanReadable')
7 changes: 7 additions & 0 deletions Packs/AWS-IAM/ReleaseNotes/1_1_38.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

#### Integrations

##### AWS - Identity and Access Management

- Fixed an issue where the ***aws-iam-get-user*** command returned an error when a user was not found.
- Updated the Docker image to: *demisto/boto3py3:1.0.0.71685*.
2 changes: 1 addition & 1 deletion Packs/AWS-IAM/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Amazon Web Services Identity and Access Management (IAM)",
"support": "xsoar",
"author": "Cortex XSOAR",
"currentVersion": "1.1.37",
"currentVersion": "1.1.38",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
"created": "2020-04-14T00:00:00Z",
Expand Down
9 changes: 8 additions & 1 deletion Packs/Okta/Integrations/Okta_v2/Okta_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,14 @@ def get_user_command(client, args):
if not (args.get('username') or args.get('userId')):
raise Exception("You must supply either 'Username' or 'userId")
user_term = args.get('userId') if args.get('userId') else args.get('username')
raw_response = client.get_user(user_term)

try:
raw_response = client.get_user(user_term)
except Exception as e:
if '404' in str(e):
return (f'User {args.get("username")} was not found.', {}, {})
raise e

verbose = args.get('verbose')

user_context = client.get_users_context(raw_response)
Expand Down
2 changes: 1 addition & 1 deletion Packs/Okta/Integrations/Okta_v2/Okta_v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,7 @@ script:
name: appName
description: Assign a group to an application
name: okta-assign-group-to-app
dockerimage: demisto/python3:3.10.12.66339
dockerimage: demisto/python3:3.10.12.68714
runonce: false
script: '-'
subtype: python3
Expand Down
17 changes: 17 additions & 0 deletions Packs/Okta/Integrations/Okta_v2/Okta_v2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,23 @@ def test_get_user_command(mocker, args, expected_context, expected_readable):
assert expected_readable in readable


def test_get_user_command_not_found_user(mocker):
"""
Given:
- Username.

When:
- running get_user_command.

Then:
- Ensure that no exception was raised, and assert the readable output.
"""
args = {"username": "test@this.com"}
mocker.patch.object(client, 'get_user', side_effect=Exception('Error in API call [404] - Not found'))
readable, _, _ = get_user_command(client, args)
assert 'User test@this.com was not found.' in readable


@pytest.mark.parametrize(
# Write and define the expected
"args ,expected_context, expected_readable",
Expand Down
7 changes: 7 additions & 0 deletions Packs/Okta/ReleaseNotes/3_1_27.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

#### Integrations

##### Okta v2

- Fixed an issue where the ***okta-get-user*** command returned an error when a user was not found.
- Updated the Docker image to: *demisto/python3:3.10.12.68714*.
2 changes: 1 addition & 1 deletion Packs/Okta/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Okta",
"description": "Integration with Okta's cloud-based identity management service.",
"support": "xsoar",
"currentVersion": "3.1.26",
"currentVersion": "3.1.27",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down