Skip to content

Commit

Permalink
Fix integration builder issues (#427)
Browse files Browse the repository at this point in the history
- fix error when getting a response that is unexpected (not JSON)
- fix error when trying to edit a sync item
- fix masking oauth secrets
  • Loading branch information
GDay authored Feb 8, 2024
1 parent caaddc8 commit 8e71532
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
4 changes: 4 additions & 0 deletions back/admin/integrations/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ class FailedPaginatedResponseError(Exception):

class KeyIsNotInDataError(Exception):
pass


class DataIsNotJSONError(Exception):
pass
10 changes: 10 additions & 0 deletions back/admin/integrations/mixins.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import logging
from json.decoder import JSONDecodeError

from django.utils.translation import gettext_lazy as _

from admin.integrations.exceptions import (
DataIsNotJSONError,
FailedPaginatedResponseError,
KeyIsNotInDataError,
)
Expand Down Expand Up @@ -37,6 +39,14 @@ def extract_data_from_list_response(self, response):
"response": self.integration.clean_response(response.json()),
}
)
except JSONDecodeError:
raise DataIsNotJSONError(
_("Response is not JSON: %(response)s")
% {
"response": self.integration.clean_response(response.text),
}
)

data_structure = self.integration.manifest["data_structure"]
user_details = []
for user_data in users:
Expand Down
22 changes: 19 additions & 3 deletions back/admin/integrations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,9 +771,25 @@ def clean_response(self, response) -> str:
response = str(response)

for name, value in self.extra_args.items():
response = response.replace(
str(value), _("***Secret value for %(name)s***") % {"name": name}
)
if isinstance(value, dict):
for inner_name, inner_value in value.items():
response = response.replace(
str(inner_value),
_("***Secret value for %(name)s***")
% {"name": name + "." + inner_name},
)
else:
response = response.replace(
str(value), _("***Secret value for %(name)s***") % {"name": name}
)

if name == "Authorization" and value.startswith("Basic"):
response.replace(
base64.b64encode(value.split(" ", 1)[1].encode("ascii")).decode(
"ascii"
),
"BASE64 ENCODED SECRET",
)

return response

Expand Down
3 changes: 3 additions & 0 deletions back/admin/integrations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ def get_context_data(self, **kwargs):
if pk := self.kwargs.get("pk", False):
integration = get_object_or_404(Integration, id=pk)
manifest = integration.manifest
if manifest.get("form", None) is None:
manifest["form"] = []

context["object"] = integration
if not manifest.get("exists", {}):
manifest["exists"] = {
Expand Down
7 changes: 6 additions & 1 deletion back/admin/people/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from admin.admin_tasks.models import AdminTask
from admin.integrations.exceptions import (
DataIsNotJSONError,
FailedPaginatedResponseError,
KeyIsNotInDataError,
)
Expand Down Expand Up @@ -416,7 +417,11 @@ def get(self, request, pk, *args, **kwargs):
# we are passing in the user who is requesting it, but we likely don't need
# them.
users = SyncUsers(integration).get_import_user_candidates()
except (KeyIsNotInDataError, FailedPaginatedResponseError) as e:
except (
KeyIsNotInDataError,
FailedPaginatedResponseError,
DataIsNotJSONError,
) as e:
return render(request, "_import_user_table.html", {"error": e})

return render(
Expand Down

0 comments on commit 8e71532

Please sign in to comment.