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

fix datetime JSON bug affecting upstream proxying #43

Merged
merged 3 commits into from
Dec 26, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/42-upstream-proxy-json.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- upstream proxying - a previous change to remove deprecated JSON encoder usage in Flask inadvertently broke upstream proxying due a bad import (https://github.com/briantist/galactory/issues/42).
13 changes: 1 addition & 12 deletions galactory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,15 @@
# (c) 2022 Brian Scholer (@briantist)

import logging
import typing as t

from datetime import datetime
from flask import Flask, request
from flask.json.provider import DefaultJSONProvider
from configargparse import ArgParser, ArgumentError, Action
from artifactory import ArtifactoryPath

from .api import bp as api
from .download import bp as download
from .health import bp as health


class DateTimeIsoFormatJSONProvider(DefaultJSONProvider):
@staticmethod
def default(o: t.Any) -> t.Any:
if isinstance(o, datetime):
return o.isoformat()

return super().default(o)
from .utilities import DateTimeIsoFormatJSONProvider


def create_app(**config):
Expand Down
6 changes: 2 additions & 4 deletions galactory/upstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from flask import current_app, abort, Response

from . import constants as C
from .utilities import _session_with_retries
from .utilities import _session_with_retries, DateTimeIsoFormatJSONProvider

class _CacheEntry:
_raw = {}
Expand Down Expand Up @@ -140,12 +140,10 @@ def _set_cache(self, request, cache) -> None:
if not self._write_cache:
return

from . import DateTimeIsoFormatJSONEncoder

path = self._repository / self._cache_path / request.path / 'data.json'
with StringIO() as buffer:
cache.update()
json.dump(cache._to_serializable_dict(), buffer, cls=DateTimeIsoFormatJSONEncoder)
json.dump(cache._to_serializable_dict(), buffer, default=DateTimeIsoFormatJSONProvider.default)
buffer.seek(0)
path.deploy(buffer)

Expand Down
16 changes: 14 additions & 2 deletions galactory/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,34 @@
import hashlib
import gzip

from typing import Dict, Any
import typing as t

from datetime import datetime
from tempfile import SpooledTemporaryFile
from urllib.request import urlopen
from urllib3 import Retry
from requests.adapters import HTTPAdapter
from requests import Session

from flask import url_for, request, current_app, abort, Response, Request
from flask.json.provider import DefaultJSONProvider
from artifactory import ArtifactoryPath, ArtifactoryException
from dohq_artifactory.auth import XJFrogArtApiAuth

from . import constants as C
from .iter_tar import iter_tar


class DateTimeIsoFormatJSONProvider(DefaultJSONProvider):
@staticmethod
def default(o: t.Any) -> t.Any:
if isinstance(o, datetime):
return o.isoformat()

return super().default(o)



def _session_with_retries(retry=None, auth=None) -> Session:
if retry is None:
retry = Retry(connect=5, read=3, redirect=2, status=6, other=3, backoff_factor=0.1, raise_on_status=False)
Expand Down Expand Up @@ -228,7 +240,7 @@ def _chunk_to_temp(fsrc, iterator=None, spool_size=5*1024*1024, seek_to_zero=Tru
return HashedTempFile(tmp, md5sum.hexdigest(), sha1sum.hexdigest(), sha256sum.hexdigest(), close=close)


def upload_collection_from_hashed_tempfile(artifact: ArtifactoryPath, tmpfile: HashedTempFile, property_fallback: bool = False) -> Dict[str, Any]:
def upload_collection_from_hashed_tempfile(artifact: ArtifactoryPath, tmpfile: HashedTempFile, property_fallback: bool = False) -> t.Dict[str, t.Any]:
try:
manifest = load_manifest_from_archive(tmpfile.handle)
except Exception:
Expand Down