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

enhancement!: use server.globalProxy value for creating a global http/https proxy #20416

Merged
merged 13 commits into from
Jun 17, 2024

Conversation

innerdvations
Copy link
Contributor

@innerdvations innerdvations commented May 31, 2024

What does it do?

  • Move server.proxy to server.proxy.koa
  • Move server.globalProxy to server.proxy.global
  • add server.proxy.fetch to act only on Fetch requests (defaults to proxy.global if set)
  • add server.proxy.http to act on http requests (defaults to proxy.global if set)
  • add server.proxy.https to act on https requests (defaults to proxy.global if set)

This

Why is it needed?

Allows proxy of all http and https requests globally (including anything that uses http/https internally, such as axios, passport-oauth2, etc, giving Strapi an actual global proxy to solve issues like proxied SSO

How to test it?

  • set server.proxy.global (or ad individual value) in your config
  • use something like npx http-echo-server to create an echo proxy
  • set up SSO (or whatever feature you want to test proxied)
  • when you use SSO, you should see it proxied through your echo server
  • fetch requests (such as telemetry) should still come through to the echo proxy

Related issue(s)/PR(s)

Fixes #17317

DX-989

Documentation PR: strapi/documentation#2124

TODO:

  • write a codemod to migrate server.proxy -> server.proxy.koa and server.globalProxy -> server.proxy.global which will come in a later PR

@innerdvations innerdvations added pr: enhancement This PR adds or updates some part of the codebase or features source: core:core Source is core/core labels May 31, 2024
Copy link

vercel bot commented May 31, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
contributor-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 13, 2024 9:40am

@derrickmehaffy
Copy link
Member

Building exp release: https://github.com/strapi/strapi/actions/runs/9322440851

@derrickmehaffy
Copy link
Member

@innerdvations looks like we are getting build failures with @strapi/core:

image

@innerdvations
Copy link
Contributor Author

@innerdvations looks like we are getting build failures with @strapi/core:

image

Ugh, very sorry! I missed a last minute version bump on undici. It builds now.

@derrickmehaffy
Copy link
Member

Can you run a new exp version build Ben and I'll at in the morning

@derrickmehaffy
Copy link
Member

@derrickmehaffy
Copy link
Member

exp version: 0.0.0-experimental.c3e9d4b26f9fd3d9eb530b5c11f9baa1d09b13ad

@derrickmehaffy
Copy link
Member

Something I noticed so far is that image thumbails loaded by the media lib are not passing through the proxy:

image

Will continue to test for patching of SSO

@innerdvations
Copy link
Contributor Author

innerdvations commented Jun 5, 2024

Something I noticed so far is that image thumbails loaded by the media lib are not passing through the proxy:

image

I haven't looked into how they're loaded. If they're essentially an img src so it would make sense not to pass through the proxy. If we have something where the server is pulling them from the remote bucket and then sending them to the user, that should go through the proxy, but I'll consider it a separate issue and address it later.

@derrickmehaffy
Copy link
Member

Something I noticed so far is that image thumbails loaded by the media lib are not passing through the proxy:

image

I haven't looked into how they're loaded. If they're essentially an img src so it would make sense not to pass through the proxy. If we have something where the server is pulling them from the remote bucket and then sending them to the user, that should go through the proxy, but I'll consider it a separate issue and address it later.

Ah yeah your probably right ignore me

Copy link
Member

@derrickmehaffy derrickmehaffy left a comment

Choose a reason for hiding this comment

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

Looking good! these are the logs from my squid proxy:

image

@innerdvations innerdvations marked this pull request as ready for review June 13, 2024 09:19
Copy link
Member

@Convly Convly left a comment

Choose a reason for hiding this comment

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

The code LGTM, one small comment.

I couldn't test the actual behavior as I don't have the correct setup.

@@ -17,7 +17,7 @@ const healthCheck: Core.MiddlewareHandler = async (ctx) => {

const createServer = (strapi: Core.Strapi): Modules.Server.Server => {
const app = createKoaApp({
proxy: strapi.config.get('server.proxy'),
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this change have a dedicated codemod?

If it's meant for another PR, could we add a TODO so that we don't forget?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, already created a jira ticket but I'll mention it here too

Copy link
Member

Choose a reason for hiding this comment

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

Hum if we just have strapi.proxy set can't we consider that it's should be the proxy applied for everything like a shorthand version ? that could be super convenient 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have time to write tests for it before I'm off, but I did this as a follow-up convenience feature, and someone can hopefully finish it and update the docs: #20537

Copy link
Contributor Author

@innerdvations innerdvations Jun 18, 2024

Choose a reason for hiding this comment

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

Oh wait, I think I completely misunderstood. You mean the strapi.proxy could be like string | { fetch, http, https, koa } and if it's a string it is treated as global?

@innerdvations
Copy link
Contributor Author

I couldn't test the actual behavior as I don't have the correct setup.

You can test it generally using http-proxy-server, for example that the fetch option still works. Also, if you write some custom code and send a request using the http or https modules, those should also get proxied.

For the more advanced stuff like testing that SSO works through a proxy, I'm hoping @derrickmehaffy will be able to give it one final QA once the code has settled before we merge.

Copy link
Member

@Bassel17 Bassel17 left a comment

Choose a reason for hiding this comment

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

LGTM code wise, just left a small suggestion

const globalProxy =
strapi.config.get<ConstructorParameters<typeof ProxyAgent>[0]>('server.globalProxy');
const proxy =
strapi.config.get<ConstructorParameters<typeof ProxyAgent>[0]>('server.proxy.fetch') ||
Copy link
Member

Choose a reason for hiding this comment

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

I was wondering if it will be clearer to add a type alias for this ConstructorParameters<typeof ProxyAgent>[0]
will leave it for you to decided otherwise looks good

@innerdvations innerdvations merged commit 60e1f8b into v5/main Jun 17, 2024
75 of 83 checks passed
@innerdvations innerdvations deleted the enhancement/sso-proxy branch June 17, 2024 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flag: 💥 Breaking change This PR contains breaking changes and should not be merged pr: enhancement This PR adds or updates some part of the codebase or features source: core:core Source is core/core
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

5 participants