Skip to content

Commit

Permalink
feat: add oauth method to scan target group (#122)
Browse files Browse the repository at this point in the history
* feat: add oauth method to scan target group

* style: parse lint
  • Loading branch information
epalhares-tenchi committed Jun 3, 2024
1 parent 6409f4c commit c2ad3c1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
31 changes: 21 additions & 10 deletions zanshinsdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,11 +1110,10 @@ def check_organization_scan_target(
# Scan Target OAuth
###################################################

def get_kind_oauth_link(
def get_scan_target_oauth_link(
self,
organization_id: Union[UUID, str],
scan_target_id: Union[UUID, str],
kind: Union[ScanTargetKind, OAuthTargetKind],
) -> Dict:
"""
Retrieve a link to authorize zanshin to read info from their target.
Expand All @@ -1129,19 +1128,31 @@ def get_kind_oauth_link(
* SALESFORCE
:return: a dict with the link
"""
if kind.value not in [member.value for member in OAuthTargetKind]:
raise ValueError(f"{repr(kind.value)} is not eligible for OAuth link")

scan_type = (
"scanTargetGroupId"
if kind in [ScanTargetKind.BITBUCKET, ScanTargetKind.GITLAB]
else "scanTargetId"
path = (
f"/oauth/link"
f"?organizationId={validate_uuid(organization_id)}"
f"&scanTargetId={validate_uuid(scan_target_id)}"
)

return self._request("GET", path).json()

def get_scan_target_group_oauth_link(
self,
organization_id: Union[UUID, str],
scan_target_group_id: Union[UUID, str],
):
"""
Retrieve a link to authorize zanshin to read info from their target group.
Mandatory for scan target groups of kind:
* BITBUCKET
* GITLAB
:return: a dict with the link
"""
path = (
f"/oauth/link"
f"?organizationId={validate_uuid(organization_id)}"
f"&{scan_type}={validate_uuid(scan_target_id)}"
f"&scanTargetGroupId={validate_uuid(scan_target_group_id)}"
)

return self._request("GET", path).json()
Expand Down
22 changes: 6 additions & 16 deletions zanshinsdk/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1031,26 +1031,24 @@ def test_check_organization_scan_target(self):
f"/organizations/{organization_id}/scantargets/{scan_target_id}/check",
)

def test_get_kind_oauth_link_should_call_api_with_scan_target_id(self):
def test_get_scan_target_group_oauth_link_should_call_apiß(self):
organization_id = "822f4225-43e9-4922-b6b8-8b0620bdb1e3"
scan_target_id = "e22f4225-43e9-4922-b6b8-8b0620bdb110"
kind = zanshinsdk.ScanTargetKind.GITLAB
scan_target_group_id = "e22f4225-43e9-4922-b6b8-8b0620bdb110"

self.sdk.get_kind_oauth_link(organization_id, scan_target_id, kind)
self.sdk.get_scan_target_group_oauth_link(organization_id, scan_target_group_id)

self.sdk._request.assert_called_once_with(
"GET",
f"/oauth/link?"
f"organizationId={organization_id}"
f"&scanTargetGroupId={scan_target_id}",
f"&scanTargetGroupId={scan_target_group_id}",
)

def test_get_kind_oauth_link_should_call_api_with_scan_target_group_id(self):
def test_get_scan_target_oauth_link_should_call_api_with_scan_target_group_id(self):
organization_id = "822f4225-43e9-4922-b6b8-8b0620bdb1e3"
scan_target_id = "e22f4225-43e9-4922-b6b8-8b0620bdb110"
kind = zanshinsdk.ScanTargetKind.JIRA

self.sdk.get_kind_oauth_link(organization_id, scan_target_id, kind)
self.sdk.get_scan_target_oauth_link(organization_id, scan_target_id)

self.sdk._request.assert_called_once_with(
"GET",
Expand All @@ -1059,14 +1057,6 @@ def test_get_kind_oauth_link_should_call_api_with_scan_target_group_id(self):
f"&scanTargetId={scan_target_id}",
)

def test_get_kind_oauth_link_should_raise_exception_with_invalid_kind(self):
organization_id = "822f4225-43e9-4922-b6b8-8b0620bdb1e3"
scan_target_id = "e22f4225-43e9-4922-b6b8-8b0620bdb110"
kind = zanshinsdk.ScanTargetKind.AWS

with self.assertRaises(ValueError):
self.sdk.get_kind_oauth_link(organization_id, scan_target_id, kind)

def test_get_gworkspace_oauth_link(self):
organization_id = "822f4225-43e9-4922-b6b8-8b0620bdb1e3"
scan_target_id = "e22f4225-43e9-4922-b6b8-8b0620bdb110"
Expand Down

0 comments on commit c2ad3c1

Please sign in to comment.