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

Proxy D2L files using python #845

Merged
merged 2 commits into from
Jan 11, 2023
Merged

Proxy D2L files using python #845

merged 2 commits into from
Jan 11, 2023

Conversation

marcospri
Copy link
Member

@marcospri marcospri commented Dec 21, 2022

Needs hypothesis/h-vialib#42 merged and published before CI is green.

For

Testing

Follow the instructions over: hypothesis/lms#4792

@marcospri marcospri marked this pull request as ready for review December 21, 2022 13:27
@marcospri marcospri changed the title [WIP] D2l proxing D2L files proxing Dec 21, 2022
@marcospri marcospri changed the title D2L files proxing Proxy D2L files using python Dec 21, 2022
"""Add headers for 3rd party providers which we access data from."""

# Pass our abuse policy in request headers for third-party site admins.
headers["X-Abuse-Policy"] = "https://web.hypothes.is/abuse-policy/"
headers["X-Complaints-To"] = "https://web.hypothes.is/report-abuse/"

if request and "via.secret.headers" in request.params:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this here so every request benefits from it (at least the python ones).

def _is_d2l_url(cls, url):
return any(regexp.match(url) for regexp in cls._D2L_URL)

def _proxy_python_pdf(self, url, route="proxy_python_pdf"):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making this method a bit generic instead of copy/paste a similar version.

)
config.add_route("proxy_d2l_pdf", "/d2l/proxied.pdf", factory=QueryURLResource)
config.add_route("proxy_python_pdf", "proxied.pdf", factory=QueryURLResource)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This last route is a bit extra and not used but I reckon it leads to a nice "just proxy these with python" feature in the future without too much noise.

url = context.url_from_query()

content_iterable = request.find_service(HTTPService).stream(
url, headers=add_request_headers({})
url, headers=add_request_headers({}, request=request)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing the request here get the original headers.

return self._proxy_onedrive_pdf(url)
return self._proxy_python_pdf(url, route="proxy_onedrive_pdf")

if self._is_d2l_url(url):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I briefly tried the nginx approach but not sure if it was possible to:

  • Pass the headers to nginx in a secure way
  • Do that in a way that involved sane code.

Copy link
Contributor

@seanh seanh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not much to say, this all looks sensible.

It might be worth having a check in New Relic at some point to see how Via's performance is doing with now proxying lots of files in Python instead of NGINX.

Decrypt and proxy the contents of `via.secret.headers` as headers to
the downstream server.
Use the python proxying infrastructure to proxy D2L files.

Note that the actual proxying process is the same as for MS OneDrive
files but we keep multiple url/routes in case we need reporting in both
or different proxying requirements at another level (eg CloudFlare).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants