diff --git a/security/integration/jersey/src/main/java/io/helidon/security/integration/jersey/SecurityPreMatchingFilter.java b/security/integration/jersey/src/main/java/io/helidon/security/integration/jersey/SecurityPreMatchingFilter.java index 72961c18a8e..1bcebeb0617 100644 --- a/security/integration/jersey/src/main/java/io/helidon/security/integration/jersey/SecurityPreMatchingFilter.java +++ b/security/integration/jersey/src/main/java/io/helidon/security/integration/jersey/SecurityPreMatchingFilter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2020 Oracle and/or its affiliates. + * Copyright (c) 2018, 2023 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. @@ -58,13 +58,19 @@ class SecurityPreMatchingFilter extends SecurityFilterCommon implements Containe public void filter(ContainerRequestContext request) { SecurityTracing tracing = SecurityTracing.get(); - // create a new security context - SecurityContext securityContext = security() - .contextBuilder(Integer.toString(CONTEXT_COUNTER.incrementAndGet(), Character.MAX_RADIX)) - .tracingSpan(tracing.findParent().orElse(null)) - .build(); - - Contexts.context().ifPresent(ctx -> ctx.register(securityContext)); + SecurityContext securityContext = Contexts.context() + .flatMap(context -> context.get(SecurityContext.class)) + .orElse(null); + + if (securityContext == null) { + // create a new security context + securityContext = security() + .contextBuilder(Integer.toString(CONTEXT_COUNTER.incrementAndGet(), Character.MAX_RADIX)) + .tracingSpan(tracing.findParent().orElse(null)) + .build(); + SecurityContext finalSecurityContext = securityContext; + Contexts.context().ifPresent(ctx -> ctx.register(finalSecurityContext)); + } injectionManager.>getInstance((new GenericType>() { }).getType()) .set(securityContext); diff --git a/tests/integration/security/pom.xml b/tests/integration/security/pom.xml index f1a14f90fd2..64610df3ce5 100644 --- a/tests/integration/security/pom.xml +++ b/tests/integration/security/pom.xml @@ -40,5 +40,6 @@ gh2455 path-params security-response-mapper + security-context-not-overridden diff --git a/tests/integration/security/security-context-not-overridden/pom.xml b/tests/integration/security/security-context-not-overridden/pom.xml new file mode 100644 index 00000000000..58a2d073b8f --- /dev/null +++ b/tests/integration/security/security-context-not-overridden/pom.xml @@ -0,0 +1,52 @@ + + + + + 4.0.0 + + helidon-tests-integration-security + io.helidon.tests.integration + 2.6.3-SNAPSHOT + + + helidon-tests-integration-security-context-not-overridden + Helidon Tests Integration Security Response Mappers + + + + io.helidon.microprofile.bundles + helidon-microprofile + + + org.junit.jupiter + junit-jupiter-api + test + + + org.hamcrest + hamcrest-all + test + + + io.helidon.microprofile.tests + helidon-microprofile-tests-junit5 + test + + + \ No newline at end of file diff --git a/tests/integration/security/security-context-not-overridden/src/main/java/io/helidon/tests/integration/context/TestEndpointResource.java b/tests/integration/security/security-context-not-overridden/src/main/java/io/helidon/tests/integration/context/TestEndpointResource.java new file mode 100644 index 00000000000..699fafef789 --- /dev/null +++ b/tests/integration/security/security-context-not-overridden/src/main/java/io/helidon/tests/integration/context/TestEndpointResource.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 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.tests.integration.context; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; + +import io.helidon.security.SecurityContext; + +/** + * Simple test endpoint. + */ +@Path("/test-endpoint") +public class TestEndpointResource { + + /** + * Return user greeting. + * + * @return {@link String} + */ + @GET + @Produces(MediaType.TEXT_PLAIN) + public String getDefaultMessage(@Context SecurityContext securityContext) { + return "Hello " + securityContext.userName(); + } + +} diff --git a/tests/integration/security/security-context-not-overridden/src/main/java/io/helidon/tests/integration/context/TestProvider.java b/tests/integration/security/security-context-not-overridden/src/main/java/io/helidon/tests/integration/context/TestProvider.java new file mode 100644 index 00000000000..6139b1b692d --- /dev/null +++ b/tests/integration/security/security-context-not-overridden/src/main/java/io/helidon/tests/integration/context/TestProvider.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 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.tests.integration.context; + +import io.helidon.security.AuthenticationResponse; +import io.helidon.security.ProviderRequest; +import io.helidon.security.spi.AuthenticationProvider; +import io.helidon.security.spi.SynchronousProvider; + +class TestProvider extends SynchronousProvider implements AuthenticationProvider{ + + @Override + protected AuthenticationResponse syncAuthenticate(ProviderRequest providerRequest) { + return AuthenticationResponse.abstain(); + } + +} diff --git a/tests/integration/security/security-context-not-overridden/src/main/java/io/helidon/tests/integration/context/TestProviderService.java b/tests/integration/security/security-context-not-overridden/src/main/java/io/helidon/tests/integration/context/TestProviderService.java new file mode 100644 index 00000000000..9ff8f3b32f0 --- /dev/null +++ b/tests/integration/security/security-context-not-overridden/src/main/java/io/helidon/tests/integration/context/TestProviderService.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 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.tests.integration.context; + +import io.helidon.config.Config; +import io.helidon.security.spi.SecurityProvider; +import io.helidon.security.spi.SecurityProviderService; + +public class TestProviderService implements SecurityProviderService { + @Override + public String providerConfigKey() { + return "test"; + } + + @Override + public Class providerClass() { + return TestProvider.class; + } + + @Override + public SecurityProvider providerInstance(Config config) { + return new TestProvider(); + } + +} diff --git a/tests/integration/security/security-context-not-overridden/src/main/resources/META-INF/services/io.helidon.security.spi.SecurityProviderService b/tests/integration/security/security-context-not-overridden/src/main/resources/META-INF/services/io.helidon.security.spi.SecurityProviderService new file mode 100644 index 00000000000..90da5d7d4ab --- /dev/null +++ b/tests/integration/security/security-context-not-overridden/src/main/resources/META-INF/services/io.helidon.security.spi.SecurityProviderService @@ -0,0 +1 @@ +io.helidon.tests.integration.context.TestProviderService \ No newline at end of file diff --git a/tests/integration/security/security-context-not-overridden/src/main/resources/application.yaml b/tests/integration/security/security-context-not-overridden/src/main/resources/application.yaml new file mode 100644 index 00000000000..f5cf6dd90e3 --- /dev/null +++ b/tests/integration/security/security-context-not-overridden/src/main/resources/application.yaml @@ -0,0 +1,31 @@ +# +# Copyright (c) 2023 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. +# +security: + providers: + - abac: + - test: + optional: true + - http-basic-auth: + realm: "helidon" + users: + - login: "test" + password: "password" + roles: ["user"] + web-server: + paths: + - path: "/test-endpoint[/{*}]" + authenticate: true + authenticator: "http-basic-auth" diff --git a/tests/integration/security/security-context-not-overridden/src/main/resources/logging.properties b/tests/integration/security/security-context-not-overridden/src/main/resources/logging.properties new file mode 100644 index 00000000000..8046694e624 --- /dev/null +++ b/tests/integration/security/security-context-not-overridden/src/main/resources/logging.properties @@ -0,0 +1,23 @@ +# +# Copyright (c) 2023 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. +# + +handlers = java.util.logging.ConsoleHandler + +java.util.logging.ConsoleHandler.level = FINEST +java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter +java.util.logging.SimpleFormatter.format = [%1$tc] %4$s: %2$s - %5$s %6$s%n + +.level = INFO diff --git a/tests/integration/security/security-context-not-overridden/src/test/java/io/helidon/tests/integration/context/SecurityTest.java b/tests/integration/security/security-context-not-overridden/src/test/java/io/helidon/tests/integration/context/SecurityTest.java new file mode 100644 index 00000000000..97416c60442 --- /dev/null +++ b/tests/integration/security/security-context-not-overridden/src/test/java/io/helidon/tests/integration/context/SecurityTest.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 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.tests.integration.context; + +import javax.inject.Inject; +import javax.ws.rs.client.WebTarget; + +import io.helidon.microprofile.tests.junit5.AddBean; +import io.helidon.microprofile.tests.junit5.HelidonTest; + +import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +@HelidonTest +@AddBean(TestEndpointResource.class) +public class SecurityTest { + + private final WebTarget target; + + @Inject + SecurityTest(WebTarget target) { + this.target = target; + } + + @Test + void testNotPropagatedSecurityContext() { + String response = target.register(HttpAuthenticationFeature.basic("test", "password")) + .path("/test-endpoint") + .request() + .get(String.class); + assertThat(response, is("Hello test")); + } + + +}