diff options
5 files changed, 101 insertions, 41 deletions
diff --git a/policy-endpoints/pom.xml b/policy-endpoints/pom.xml index 97426c06..d9e1e323 100644 --- a/policy-endpoints/pom.xml +++ b/policy-endpoints/pom.xml @@ -120,16 +120,16 @@ <artifactId>jetty-server</artifactId> </dependency> <dependency> - <groupId>org.eclipse.jetty</groupId> - <artifactId>jetty-servlet</artifactId> + <groupId>org.eclipse.jetty.ee10</groupId> + <artifactId>jetty-ee10-servlet</artifactId> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-util</artifactId> </dependency> <dependency> - <groupId>org.eclipse.jetty.toolchain</groupId> - <artifactId>jetty-jakarta-servlet-api</artifactId> + <groupId>jakarta.servlet</groupId> + <artifactId>jakarta.servlet-api</artifactId> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyJerseyServer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyJerseyServer.java index d4c392b9..3668a31e 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyJerseyServer.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyJerseyServer.java @@ -3,7 +3,7 @@ * policy-endpoints * ================================================================================ * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019-2020,2023 Nordix Foundation. + * Modifications Copyright (C) 2019-2020, 2023, 2025 Nordix Foundation. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,14 +17,16 @@ * 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. + * + * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ package org.onap.policy.common.endpoints.http.server.internal; import org.apache.commons.lang3.StringUtils; -import org.eclipse.jetty.servlet.ServletContextHandler; -import org.eclipse.jetty.servlet.ServletHolder; +import org.eclipse.jetty.ee10.servlet.ServletContextHandler; +import org.eclipse.jetty.ee10.servlet.ServletHolder; import org.glassfish.jersey.server.ServerProperties; import org.glassfish.jersey.servlet.ServletContainer; import org.onap.policy.common.endpoints.http.server.JsonExceptionMapper; @@ -246,7 +248,7 @@ public class JettyJerseyServer extends JettyServletServer { @Override public String toString() { - return "JettyJerseyServer [Jerseyservlets=" + servlets + return "JettyJerseyServer [JerseyServlets=" + servlets + ", swaggerId=" + swaggerId + ", toString()=" + super.toString() + "]"; diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java index ed392119..b92f8cde 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java @@ -33,9 +33,12 @@ import java.util.Map; import lombok.Getter; import lombok.NonNull; import lombok.ToString; -import org.eclipse.jetty.security.ConstraintMapping; -import org.eclipse.jetty.security.ConstraintSecurityHandler; +import org.apache.commons.lang3.StringUtils; +import org.eclipse.jetty.ee10.servlet.ServletContextHandler; +import org.eclipse.jetty.ee10.servlet.ServletHolder; +import org.eclipse.jetty.security.Constraint; import org.eclipse.jetty.security.HashLoginService; +import org.eclipse.jetty.security.SecurityHandler; import org.eclipse.jetty.security.UserStore; import org.eclipse.jetty.security.authentication.BasicAuthenticator; import org.eclipse.jetty.server.CustomRequestLog; @@ -45,9 +48,6 @@ import org.eclipse.jetty.server.SecureRequestCustomizer; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.Slf4jRequestLogWriter; -import org.eclipse.jetty.servlet.ServletContextHandler; -import org.eclipse.jetty.servlet.ServletHolder; -import org.eclipse.jetty.util.security.Constraint; import org.eclipse.jetty.util.security.Credential; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.onap.policy.common.endpoints.http.server.HttpServletServer; @@ -150,16 +150,15 @@ public abstract class JettyServletServer implements HttpServletServer, Runnable /** * Constructor. * - * @param name server name - * @param host server host - * @param port server port + * @param name server name + * @param host server host + * @param port server port * @param sniHostCheck SNI Host checking flag - * @param contextPath context path - * + * @param contextPath context path * @throws IllegalArgumentException if invalid parameters are passed in */ protected JettyServletServer(String name, boolean https, String host, int port, boolean sniHostCheck, - String contextPath) { + String contextPath) { String srvName = name; if (srvName == null || srvName.isEmpty()) { @@ -211,10 +210,6 @@ public abstract class JettyServletServer implements HttpServletServer, Runnable this.jettyServer.setHandler(context); } - protected JettyServletServer(String name, String host, int port, boolean sniHostCheck, String contextPath) { - this(name, false, host, port, sniHostCheck, contextPath); - } - @Override public void addFilterClass(String filterPath, String filterClass) { if (filterClass == null || filterClass.isEmpty()) { @@ -287,11 +282,11 @@ public abstract class JettyServletServer implements HttpServletServer, Runnable public void setBasicAuthentication(String user, String password, String servletPath) { String srvltPath = servletPath; - if (user == null || user.isEmpty() || password == null || password.isEmpty()) { + if (StringUtils.isBlank(user) || StringUtils.isBlank(password)) { throw new IllegalArgumentException("Missing user and/or password"); } - if (srvltPath == null || srvltPath.isEmpty()) { + if (StringUtils.isBlank(srvltPath)) { srvltPath = "/*"; } @@ -303,21 +298,12 @@ public abstract class JettyServletServer implements HttpServletServer, Runnable hashLoginService.setUserStore(userStore); hashLoginService.setName(this.connector.getName() + "-login-service"); - var constraint = new Constraint(); - constraint.setName(Constraint.__BASIC_AUTH); - constraint.setRoles(new String[] { - "user" - }); - constraint.setAuthenticate(true); - - var constraintMapping = new ConstraintMapping(); - constraintMapping.setConstraint(constraint); - constraintMapping.setPathSpec(srvltPath); + var constraint = Constraint.from("user"); - var securityHandler = new ConstraintSecurityHandler(); + var securityHandler = new SecurityHandler.PathMapped(); + securityHandler.put(srvltPath, constraint); securityHandler.setAuthenticator(new BasicAuthenticator()); securityHandler.setRealmName(this.connector.getName() + "-realm"); - securityHandler.addConstraintMapping(constraintMapping); securityHandler.setLoginService(hashLoginService); this.context.setSecurityHandler(securityHandler); @@ -332,7 +318,7 @@ public abstract class JettyServletServer implements HttpServletServer, Runnable @Override public void run() { try { - logger.info("{}: STARTING", this); + logger.info("{}: RUN", this); this.jettyServer.start(); diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyStaticResourceServer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyStaticResourceServer.java index ee2b0540..7456cdad 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyStaticResourceServer.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyStaticResourceServer.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2020, 2023-2024 Nordix Foundation. + * Copyright (C) 2020, 2023-2025 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,8 +23,8 @@ package org.onap.policy.common.endpoints.http.server.internal; import lombok.ToString; import org.apache.commons.lang3.StringUtils; -import org.eclipse.jetty.servlet.DefaultServlet; -import org.eclipse.jetty.servlet.ServletHolder; +import org.eclipse.jetty.ee10.servlet.DefaultServlet; +import org.eclipse.jetty.ee10.servlet.ServletHolder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServerTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServerTest.java new file mode 100644 index 00000000..823c2fe6 --- /dev/null +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServerTest.java @@ -0,0 +1,72 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2025 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.endpoints.http.server.internal; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +class JettyServletServerTest { + + @Test + void testJettyServletServer() { + var server = new TestServlet("", false, "", 8080, false, ""); + assertNotNull(server); + assertEquals("http-8080", server.getName()); + assertEquals("localhost", server.getHost()); + assertEquals(8080, server.getPort()); + assertEquals("/", server.contextPath); + } + + @Test + void testPortExceptions() { + assertThrows(IllegalArgumentException.class, + () -> new TestServlet("", false, "", -1, false, "")); + assertThrows(IllegalArgumentException.class, + () -> new TestServlet("", false, "", 65535, false, "")); + } + + @Test + void testBasicAuthServletPath() { + var server = new TestServlet("", false, "", 8080, false, ""); + assertDoesNotThrow(() -> server.setBasicAuthentication("user", "password", "/path")); + } + + @Test + void testBasicAuthException() { + var server = new TestServlet("", false, "", 8080, false, ""); + assertThrows(IllegalArgumentException.class, () -> server.setBasicAuthentication("", "pass", "path")); + assertThrows(IllegalArgumentException.class, () -> server.setBasicAuthentication("user", "", "path")); + } + + public static class TestServlet extends JettyServletServer { + + public TestServlet(String name, boolean https, String host, int port, boolean sniHostCheck, + String contextPath) { + super(name, https, host, port, sniHostCheck, contextPath); + } + } +} |