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

Add ability to have proxy rewrite API_ROOT w/ header #5013

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

gerrod3
Copy link
Contributor

@gerrod3 gerrod3 commented Feb 1, 2024

fixes: #4207

@@ -1254,8 +1254,7 @@ class RepositoryVersionContentDetails(models.Model):
)
count = models.IntegerField()

@property
def content_href(self):
def get_content_href(self, request=None):
Copy link
Member

Choose a reason for hiding this comment

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

This always feels like request would be the perfect candidate for a context var...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you think I should do so in this PR?

Copy link
Member

Choose a reason for hiding this comment

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

I am a little worried that we are changing a public interface here. But is this class actually public?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The interface isn't changed though, further down I re-create the content_href property and have it call this "new" method.

@@ -1254,8 +1254,7 @@ class RepositoryVersionContentDetails(models.Model):
)
count = models.IntegerField()

@property
def content_href(self):
def get_content_href(self, request=None):
Copy link
Member

Choose a reason for hiding this comment

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

I am a little worried that we are changing a public interface here. But is this class actually public?

pulpcore/app/path_api_urls.py Outdated Show resolved Hide resolved
PATH_API_ROOT = "<path:api_root>/" + API_ROOT.split(settings.API_ROOT[1:])[1]
dup_urls = special_views + docs_and_status
for router in all_routers:
dup_urls.extend(router.urls)
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need to dupe the url patterns? Can't we just also handle the default paths with the global match pattern?

Copy link
Member

Choose a reason for hiding this comment

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

Also do plugins with individual urlpattern opt into this? Do we get the whole "domain enabled" story all over again?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now using a global pattern placed into V3_API_ROOT. Plugins that use this for extending their urlpatterns will automatically get support for this feature.

self.reverse = _reverse(self.reverse, request, obj)
return super().get_url(obj, view_name, None, *args, **kwargs)
# Use the Pulp reverse method to display relative hrefs.
self.reverse = reverse
Copy link
Member

Choose a reason for hiding this comment

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

At least some consolidating codepaths. ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Might have to change this back to a patched reverse (albeit using our new Pulp reverse). The tests are failing because super().get_url doesn't accept kwargs to pass onto reverse, so the domain can't be specified. For serializing fields we need to know if the object has a domain relationship to use the correct domain name, the requests domain is not good enough.

@@ -113,6 +114,7 @@
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"pulpcore.middleware.DomainMiddleware",
"pulpcore.middleware.APIRootRewriteMiddleware",
Copy link
Member

Choose a reason for hiding this comment

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

Didn't you only want to include this when configured?
I saw the raising of MiddlewareNotUsed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's part of django's middelware api: https://docs.djangoproject.com/en/5.0/topics/http/middleware/#marking-middleware-as-unused. You list it here and then on launch the middleware's init will determine if it should be used or not.

@gerrod3 gerrod3 marked this pull request as draft February 6, 2024 14:01
@gerrod3 gerrod3 force-pushed the proxy-base-path branch 5 times, most recently from bfe1c84 to 89bc79e Compare March 18, 2024 23:46
@@ -126,7 +126,7 @@ if [ "$TEST" = "azure" ]; then
- ./azurite:/etc/pulp\
command: "azurite-blob --blobHost 0.0.0.0 --cert /etc/pulp/azcert.pem --key /etc/pulp/azkey.pem"' vars/main.yaml
sed -i -e '$a azure_test: true\
pulp_scenario_settings: {"domain_enabled": true}\
pulp_scenario_settings: {"api_root_rewrite_header": "X-API-Root", "domain_enabled": true}\
Copy link
Member

Choose a reason for hiding this comment

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

Just wondering, can we infer the original relative path the user sent this request to from the request object?
Or is there some apache/nginx rewrite we going on we cannot see anymore?
(I was wondering, because drf used to use the request object to create uris, where we dumbed it down to do hrefs.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't think so, unless the reverse proxy sends along that info inside some header we are completely clueless where the original path came from.

@gerrod3 gerrod3 force-pushed the proxy-base-path branch 4 times, most recently from d2e8f2f to 3fcde86 Compare March 25, 2024 14:55
@gerrod3 gerrod3 marked this pull request as ready for review March 25, 2024 14:56
@gerrod3 gerrod3 force-pushed the proxy-base-path branch 7 times, most recently from a2745ad to ba404f7 Compare April 4, 2024 06:14
if [ "$TEST" = "azure" ]; then
cmd_stdin_prefix bash -c "cat > /etc/nginx/pulp/api_root_rewrite.conf" < pulpcore/tests/functional/assets/api_root_rewrite.conf
Copy link
Member

Choose a reason for hiding this comment

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

Is this related to your recent oci images change with a templated nginx config?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No it is not.

@gerrod3
Copy link
Contributor Author

gerrod3 commented May 10, 2024

@newswangerd If you have any free time would love to hear feedback on the design of this feature and if it accomplishes what you need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support base path proxy rewrites
2 participants