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

Zendesk request override get tasks #5429

Merged
merged 14 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The types of changes are:

### Changed
- Added a security setting that must be set to true to enable the access request download feature [#5451](https://github.com/ethyca/fides/pull/5451)
- Preventing erasures for the Zendesk integration if there are any open tickets [#5429](https://github.com/ethyca/fides/pull/5429)

### Developer Experience
- Added Carbon Icons to FidesUI [#5416](https://github.com/ethyca/fides/pull/5416)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from typing import Any, Dict, List
from urllib import parse
from urllib.parse import urlsplit

import pydash

from fides.api.common_exceptions import FidesopsException
from fides.api.graph.traversal import TraversalNode
from fides.api.models.policy import Policy
from fides.api.models.privacy_request import PrivacyRequest
from fides.api.schemas.policy import ActionType
from fides.api.schemas.saas.shared_schemas import HTTPMethod, SaaSRequestParams
from fides.api.service.connectors.saas.authenticated_client import AuthenticatedClient
from fides.api.service.saas_request.saas_request_override_factory import (
SaaSRequestType,
register,
)
from fides.api.util.collection_util import Row


def _check_tickets(tickets: List[Row], policy: Policy) -> None:
"""
Raises an exception if there are any "open" tickets to halt the privacy request.
Will only halt if the policy contains an erasure action.
"""
if policy.get_rules_for_action(action_type=ActionType.erasure):
for ticket in tickets:
if ticket["status"] != "closed":
raise FidesopsException("User still has open tickets, halting request")

Check warning on line 29 in src/fides/api/service/saas_request/override_implementations/zendesk_request_overrides.py

View check run for this annotation

Codecov / codecov/patch

src/fides/api/service/saas_request/override_implementations/zendesk_request_overrides.py#L29

Added line #L29 was not covered by tests


@register("zendesk_tickets_read", [SaaSRequestType.READ])
def zendesk_tickets_read(
client: AuthenticatedClient,
node: TraversalNode,
policy: Policy,
privacy_request: PrivacyRequest,
input_data: Dict[str, List[Any]],
secrets: Dict[str, Any],
) -> List[Row]:

processed_data: List[Row] = []

Check warning on line 42 in src/fides/api/service/saas_request/override_implementations/zendesk_request_overrides.py

View check run for this annotation

Codecov / codecov/patch

src/fides/api/service/saas_request/override_implementations/zendesk_request_overrides.py#L42

Added line #L42 was not covered by tests

for user_id in input_data.get("user_id", []):
# initial request using user_id
response = client.send(

Check warning on line 46 in src/fides/api/service/saas_request/override_implementations/zendesk_request_overrides.py

View check run for this annotation

Codecov / codecov/patch

src/fides/api/service/saas_request/override_implementations/zendesk_request_overrides.py#L46

Added line #L46 was not covered by tests
SaaSRequestParams(
method=HTTPMethod.GET,
path=f"/api/v2/users/{user_id}/tickets/requested.json",
query_params={"page[size]": 100},
)
)

tickets = pydash.get(response.json(), "tickets")
_check_tickets(tickets, policy)
processed_data.extend(tickets)

Check warning on line 56 in src/fides/api/service/saas_request/override_implementations/zendesk_request_overrides.py

View check run for this annotation

Codecov / codecov/patch

src/fides/api/service/saas_request/override_implementations/zendesk_request_overrides.py#L54-L56

Added lines #L54 - L56 were not covered by tests

# paginate using links.next and check each page of tickets for a chance to terminate the request early
while pydash.get(response.json(), "links.next"):
next_link = response.json()["links"]["next"]
response = client.send(

Check warning on line 61 in src/fides/api/service/saas_request/override_implementations/zendesk_request_overrides.py

View check run for this annotation

Codecov / codecov/patch

src/fides/api/service/saas_request/override_implementations/zendesk_request_overrides.py#L60-L61

Added lines #L60 - L61 were not covered by tests
SaaSRequestParams(
method=HTTPMethod.GET,
path=urlsplit(next_link).path,
query_params=dict(parse.parse_qsl(urlsplit(next_link).query)),
)
)
tickets = pydash.get(response.json(), "tickets")
_check_tickets(tickets, policy)
processed_data.extend(tickets)

Check warning on line 70 in src/fides/api/service/saas_request/override_implementations/zendesk_request_overrides.py

View check run for this annotation

Codecov / codecov/patch

src/fides/api/service/saas_request/override_implementations/zendesk_request_overrides.py#L68-L70

Added lines #L68 - L70 were not covered by tests

return processed_data

Check warning on line 72 in src/fides/api/service/saas_request/override_implementations/zendesk_request_overrides.py

View check run for this annotation

Codecov / codecov/patch

src/fides/api/service/saas_request/override_implementations/zendesk_request_overrides.py#L72

Added line #L72 was not covered by tests
47 changes: 39 additions & 8 deletions tests/fixtures/saas/zendesk_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def zendesk_erasure_identity_email() -> str:
class ZendeskClient:
def __init__(self, secrets: Dict[str, Any]):
self.base_url = f"https://{secrets['domain']}"
self.auth = secrets["username"], secrets["api_key"]
self.auth = secrets["username"] , secrets["api_key"]

def create_user(self, email):
return requests.post(
Expand All @@ -62,20 +62,34 @@ def get_user(self, email):
params={"email": email},
)

def create_ticket(self, user_id: str):
return requests.post(
url=f"{self.base_url}/api/v2/tickets",
auth=self.auth,
json={
def create_ticket(self, user_id: str, closed: bool):
if closed:
json = {
"ticket": {
"comment": {"body": "Test Comment"},
"priority": "urgent",
"subject": "Test Ticket",
"requester_id": user_id,
"submitter_id": user_id,
"description": "Test Description",
"status": "closed",
}
},
}
else:
json = {
"ticket": {
"comment": {"body": "Test Comment"},
"priority": "urgent",
"subject": "Test Ticket",
"requester_id": user_id,
"submitter_id": user_id,
"description": "Test Description",
}
}
return requests.post(
url=f"{self.base_url}/api/v2/tickets",
auth=self.auth,
json=json,
)

def get_ticket(self, ticket_id: str):
Expand All @@ -101,7 +115,24 @@ def zendesk_erasure_data(
user = response.json()["user"]

# ticket
response = zendesk_client.create_ticket(user["id"])
response = zendesk_client.create_ticket(user["id"], True)
assert response.ok
ticket = response.json()["ticket"]
yield ticket, user


@pytest.fixture
def zendesk_erasure_data_with_open_comments(
zendesk_client: ZendeskClient,
zendesk_erasure_identity_email: str,
) -> Generator:
# customer
response = zendesk_client.create_user(zendesk_erasure_identity_email)
assert response.ok
user = response.json()["user"]

# ticket
response = zendesk_client.create_ticket(user["id"], False)
assert response.ok
ticket = response.json()["ticket"]
yield ticket, user
Expand Down
23 changes: 22 additions & 1 deletion tests/ops/integration_tests/saas/test_zendesk_task.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import pytest

from fides.api.common_exceptions import FidesopsException
from fides.api.models.policy import Policy
from tests.ops.integration_tests.saas.connector_runner import ConnectorRunner


@pytest.mark.skip(reason="No active account")
@pytest.mark.skip(reason="move to plus in progress")
@pytest.mark.integration_saas
class TestZendeskConnector:
def test_connection(self, zendesk_runner: ConnectorRunner):
Expand Down Expand Up @@ -87,3 +88,23 @@ async def test_non_strict_erasure_request(
response = zendesk_client.get_ticket(ticket["id"])
# Since ticket is deleted, it won't be available so response is 404
assert response.status_code == 404

async def test_non_strict_erasure_request_fails_with_open_tickets(
self,
request,
zendesk_runner: ConnectorRunner,
policy: Policy,
erasure_policy_string_rewrite: Policy,
zendesk_erasure_identity_email: str,
zendesk_erasure_data_with_open_comments,
zendesk_client,
):

with pytest.raises(
FidesopsException, match="User still has open tickets, halting request"
):
galvana marked this conversation as resolved.
Show resolved Hide resolved
await zendesk_runner.non_strict_erasure_request(
access_policy=policy,
erasure_policy=erasure_policy_string_rewrite,
identities={"email": zendesk_erasure_identity_email},
)
Loading