forked from helidon-io/helidon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include requested URI config in ListenerConfig; fix config keys (heli…
…don-io#8371) * Include requested URI config in ListenerConfig; fix config keys * Forward-port requested URI discovery tests from 3.x * Review comments
- Loading branch information
Showing
9 changed files
with
552 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
http/http/src/test/java/io/helidon/http/RequestedUriConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.http; | ||
|
||
import io.helidon.config.Config; | ||
import io.helidon.config.ConfigSources; | ||
import io.helidon.http.RequestedUriDiscoveryContext.RequestedUriDiscoveryType; | ||
import io.helidon.http.RequestedUriDiscoveryContext.UnsafeRequestedUriSettingsException; | ||
|
||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static io.helidon.http.RequestedUriDiscoveryContext.Builder.REQUESTED_URI_DISCOVERY_CONFIG_KEY; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.allOf; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class RequestedUriConfigTest { | ||
|
||
private static Config config; | ||
|
||
@BeforeAll | ||
static void initConfig() { | ||
config = Config.just(ConfigSources.classpath("/requestUriDiscovery.yaml")); | ||
} | ||
|
||
@Test | ||
void checkUnsafeDetectionOnEnable() { | ||
Config c = config.get("test-enabled-no-details.server." + REQUESTED_URI_DISCOVERY_CONFIG_KEY); | ||
assertThrows(UnsafeRequestedUriSettingsException.class, () -> | ||
RequestedUriDiscoveryContext.builder() | ||
.config(c) | ||
.build(), | ||
"defaulted non-HOST discovery type with no proxy settings"); | ||
} | ||
|
||
@Test | ||
void checkExplicitTypesNoDetails() { | ||
Config c = config.get("test-explicit-types-no-details.server." + REQUESTED_URI_DISCOVERY_CONFIG_KEY); | ||
assertThrows(UnsafeRequestedUriSettingsException.class, () -> | ||
RequestedUriDiscoveryContext.builder() | ||
.config(c) | ||
.build(), | ||
"explicit non-HOST discovery types with no proxy settings"); | ||
} | ||
|
||
@Test | ||
void checkEnum() { | ||
String v = "x-forwarded"; | ||
Class<?> eClass = RequestedUriDiscoveryType.class; | ||
if (eClass.isEnum()) { | ||
RequestedUriDiscoveryType type = null; | ||
for (Object o : eClass.getEnumConstants()) { | ||
if (((Enum<?>) o).name().equalsIgnoreCase((v.replace('-', '_')))) { | ||
type = (RequestedUriDiscoveryType) o; | ||
break; | ||
} | ||
} | ||
assertThat("Mapped string to discovery type", | ||
type, | ||
allOf(notNullValue(),is(RequestedUriDiscoveryType.X_FORWARDED))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# | ||
# Copyright (c) 2023, 2024 Oracle and/or its affiliates. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
# Should throw unsafe exception - defaulted non-HOST type without trusted proxy settings. | ||
test-enabled-no-details: | ||
server: | ||
port: 0 | ||
requested-uri-discovery: | ||
enabled: true | ||
|
||
# Should be OK - HOST does not require trusted proxies. | ||
test-enabled-choose-host: | ||
server: | ||
port: 0 | ||
requested-uri-discovery: | ||
types: host,forwarded | ||
trusted-proxies: | ||
allow: | ||
exact: trust.com | ||
deny: | ||
exact: otherBadProxy.com | ||
|
||
|
||
# Should throw unsafe exception - non-HOST type without trusted proxy settings. | ||
test-explicit-types-no-details: | ||
server: | ||
port: 0 | ||
requested-uri-discovery: | ||
types: forwarded,x-forwarded | ||
|
||
# Should be OK - trusted-proxies is set with non-HOST discovery type. | ||
test-defaulted-discovery-type: | ||
server: | ||
port: 0 | ||
requested-uri-discovery: | ||
trusted-proxies: | ||
allow: | ||
exact: trust.com | ||
|
||
# Should reflect only the Forwarded header, not the X-Forwarded family. | ||
test-forwarded-only: | ||
server: | ||
port: 0 | ||
requested-uri-discovery: | ||
types: forwarded | ||
trusted-proxies: | ||
allow: | ||
all: true | ||
deny: | ||
exact: otherUntrustedProxy.com,untrustedProxy.com | ||
|
||
# Should reflect only the X-Forwarded headers, not Forwarded. | ||
test-x-forwarded-only: | ||
server: | ||
port: 0 | ||
requested-uri-discovery: | ||
types: x-forwarded | ||
trusted-proxies: | ||
allow: | ||
all: true | ||
deny: | ||
exact: otherUntrustedProxy.com,untrustedProxy.com | ||
|
||
# Should reflect only the X-Forwarded headers, not Forwarded. | ||
test-both-x-forwarded-first: | ||
server: | ||
port: 0 | ||
requested-uri-discovery: | ||
types: x-forwarded,forwarded | ||
trusted-proxies: | ||
allow: | ||
all: true | ||
deny: | ||
exact: otherUntrustedProxy.com,untrustedProxy.com | ||
|
||
# Should reflect only the X-Forwarded headers, not Forwarded. | ||
test-both-forwarded-first: | ||
server: | ||
port: 0 | ||
requested-uri-discovery: | ||
types: forwarded,x-forwarded | ||
trusted-proxies: | ||
allow: | ||
all: true | ||
deny: | ||
exact: otherUntrustedProxy.com,untrustedProxy.com | ||
|
||
# Should reflect the Host header by default. | ||
test-default-discovery: | ||
server: | ||
port: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.