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 x-forwarded-prefix header #382

Merged
merged 9 commits into from
Aug 14, 2018
Merged

Conversation

ryandawsonuk
Copy link
Contributor

fixes #314

New PR after closing #377 - this time just using data available in the gw implementation. The idea is that if the path of the url that the gw is routing to is a subset of the url that it is routing from then the difference is the prefix. Also applies if the path of the url being routed to is empty and the routed-from url path is non-empty.

Included quite a few null checks as it should allow for the various different route definition styles and types of request. I've been using this in k8s by overriding the class and found the null checks to be necessary.

Was thinking it could be disabled by default as that's how it is for nginx ingress but I haven't thought of any specific reason not to enable by default.

@codecov-io
Copy link

codecov-io commented Jun 21, 2018

Codecov Report

Merging #382 into 2.0.x will decrease coverage by 2.98%.
The diff coverage is 84.61%.

Impacted file tree graph

@@             Coverage Diff             @@
##              2.0.x    #382      +/-   ##
===========================================
- Coverage     71.48%   68.5%   -2.99%     
===========================================
  Files           128     124       -4     
  Lines          3574    3483      -91     
  Branches        247     239       -8     
===========================================
- Hits           2555    2386     -169     
- Misses          893     964      +71     
- Partials        126     133       +7
Impacted Files Coverage Δ Complexity Δ
...ateway/filter/headers/XForwardedHeadersFilter.java 85.32% <84.61%> (+0.79%) 0 <0> (-33) ⬇️
...ay/handler/predicate/ReadBodyPredicateFactory.java 8.33% <0%> (-70.4%) 0% <0%> (-7%)
...ork/cloud/gateway/support/BodyInserterContext.java 0% <0%> (-66.67%) 0% <0%> (-4%)
...cloud/gateway/support/CachedBodyOutputMessage.java 0% <0%> (-65%) 0% <0%> (-7%)
...ud/gateway/filter/AdaptCachedBodyGlobalFilter.java 50% <0%> (-50%) 0% <0%> (-4%)
...rk/cloud/gateway/support/DefaultServerRequest.java 0% <0%> (-31.67%) 0% <0%> (-8%)
...ork/cloud/gateway/route/builder/PredicateSpec.java 52.27% <0%> (-20.07%) 0% <0%> (-28%)
.../handler/predicate/QueryRoutePredicateFactory.java 68.42% <0%> (-17.3%) 0% <0%> (-8%)
...handler/predicate/HeaderRoutePredicateFactory.java 72.22% <0%> (-16.67%) 0% <0%> (-7%)
...cloud/gateway/route/builder/GatewayFilterSpec.java 54.02% <0%> (-7.84%) 0% <0%> (-35%)
... and 19 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 63fb4bf...74c5805. Read the comment docs.

@ryandawsonuk
Copy link
Contributor Author

Best not to merge yet as I am seeing intermittent 'bad gateway' errors in k8s and haven't yet determined cause.

@ryandawsonuk
Copy link
Contributor Author

ryandawsonuk commented Jun 21, 2018

Ok the bad gateway errors only happen when I fail to explicitly set a log level for the gateway so not related to this PR. (I guess probably related to logback but haven't investigated further.) This might mean not all null checks necessary as I may have been tricked by the logging problem. Perhaps safest to keep them anyway.

@RobMaskell
Copy link

@ryandawsonuk nice work on the PR, I've tried to test it for my use-case but despite trying jitpack and trying to build locally I just can't get it to build. I don't think this has anything to do with your PR just gradle knowledge probably. I'll test it if this gets into a snapshot though, that I can do...

@ryandawsonuk
Copy link
Contributor Author

ryandawsonuk commented Jun 22, 2018

@RobMaskell if it helps I'd think it would work to just include this one class in your project - your examples would be great for further testing this. I'm just overriding the class (https://github.com/ryandawsonuk/activiti-cloud-gateway/tree/develop/src/main/java/org/springframework/cloud/gateway/filter/headers) but I'm not using gradle.

@RobMaskell
Copy link

@ryandawsonuk yup that works great thanks. I'd vote for prefix enable property to be true by default, if I had a vote. Hope this PR gets merged.

Copy link
Member

@spencergibb spencergibb left a comment

Choose a reason for hiding this comment

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

Some formatting fixes and a requested comment.

@@ -187,6 +219,32 @@ public HttpHeaders filter(HttpHeaders input, ServerWebExchange exchange) {
write(updated, X_FORWARDED_PROTO_HEADER, proto, isProtoAppend());
}

if(isPrefixEnabled()){
Copy link
Member

Choose a reason for hiding this comment

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

Some spacing issues if (...) { and more below.

LinkedHashSet<URI> originalUris = exchange.getAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
URI requestUri = exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR);

if(originalUris != null && requestUri != null) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a comment with a general overview of the algorithm?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added now

@ryandawsonuk
Copy link
Contributor Author

Based on the comments I assume this is labelled waiting for feedback as it's waiting for the security team and not for anything from me?

Copy link
Member

@spencergibb spencergibb left a comment

Choose a reason for hiding this comment

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

Can you target 2.0.x now that master is 2.1.x (Greenwich release train)?

String prefix = originalUri.getPath();

//strip trailing slashes before checking if request path is end of original path
String originalUriPath = originalUri.getPath().substring(0, originalUri.getPath().length() - (originalUri.getPath().endsWith("/") ? 1 : 0));
Copy link
Member

Choose a reason for hiding this comment

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

No need to do a substring if path doesn't end with /. Refactor to something like

	private String stripTrailingSlash(URI uri) {
		if (uri.getPath().endsWith("/")) {
			return uri.getPath().substring(0, uri.getPath().length() - 1);
		} else {
			return uri.getPath();
		}
	}

@ryandawsonuk ryandawsonuk changed the base branch from master to 2.0.x August 13, 2018 16:33
@ryandawsonuk
Copy link
Contributor Author

ryandawsonuk commented Aug 13, 2018

@spencergibb Now updated to target 2.0.x

@spencergibb spencergibb merged commit bc1e549 into spring-cloud:2.0.x Aug 14, 2018
Johnny850807 pushed a commit to Johnny850807/spring-cloud-gateway that referenced this pull request Dec 14, 2020
Adds protected getters for context and scope.

Adds new refreshEnvironment() method.

fixes spring-cloudgh-382
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.

4 participants