| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// ------------------------------------------------------------------------ |
| 5 |
// All rights reserved. This program and the accompanying materials |
| 6 |
// are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
// and Apache License v2.0 which accompanies this distribution. |
| 8 |
// |
| 9 |
// The Eclipse Public License is available at |
| 10 |
// http://www.eclipse.org/legal/epl-v10.html |
| 11 |
// |
| 12 |
// The Apache License v2.0 is available at |
| 13 |
// http://www.opensource.org/licenses/apache2.0.php |
| 14 |
// |
| 15 |
// You may elect to redistribute this code under either of these licenses. |
| 16 |
// ======================================================================== |
| 17 |
// |
| 18 |
|
| 19 |
package org.eclipse.jetty; |
| 20 |
|
| 21 |
import java.io.ByteArrayOutputStream; |
| 22 |
import java.io.File; |
| 23 |
import java.io.FileOutputStream; |
| 24 |
import java.io.IOException; |
| 25 |
import java.io.OutputStream; |
| 26 |
import java.net.URI; |
| 27 |
import java.net.URLDecoder; |
| 28 |
import java.util.Collections; |
| 29 |
import java.util.HashSet; |
| 30 |
import java.util.Set; |
| 31 |
import javax.servlet.ServletException; |
| 32 |
import javax.servlet.ServletInputStream; |
| 33 |
import javax.servlet.http.HttpServletRequest; |
| 34 |
import javax.servlet.http.HttpServletResponse; |
| 35 |
|
| 36 |
import org.eclipse.jetty.security.ConstraintMapping; |
| 37 |
import org.eclipse.jetty.security.ConstraintSecurityHandler; |
| 38 |
import org.eclipse.jetty.security.LoginService; |
| 39 |
import org.eclipse.jetty.security.authentication.BasicAuthenticator; |
| 40 |
import org.eclipse.jetty.server.Handler; |
| 41 |
import org.eclipse.jetty.server.Server; |
| 42 |
import org.eclipse.jetty.server.handler.AbstractHandler; |
| 43 |
import org.eclipse.jetty.server.handler.HandlerCollection; |
| 44 |
import org.eclipse.jetty.servlet.DefaultServlet; |
| 45 |
import org.eclipse.jetty.servlet.ServletContextHandler; |
| 46 |
import org.eclipse.jetty.servlet.ServletHolder; |
| 47 |
import org.eclipse.jetty.toolchain.test.FS; |
| 48 |
import org.eclipse.jetty.util.IO; |
| 49 |
import org.eclipse.jetty.util.security.Constraint; |
| 50 |
import org.slf4j.Logger; |
| 51 |
import org.slf4j.LoggerFactory; |
| 52 |
import org.testcontainers.containers.MariaDBContainer; |
| 53 |
import org.testcontainers.containers.output.Slf4jLogConsumer; |
| 54 |
|
| 55 |
/** |
| 56 |
* DatabaseLoginServiceTestServer |
| 57 |
*/ |
| 58 |
public class DatabaseLoginServiceTestServer |
| 59 |
{ |
| 60 |
private static final Logger LOG = LoggerFactory.getLogger(DatabaseLoginServiceTestServer.class); |
| 61 |
private static final Logger MARIADB_LOG = LoggerFactory.getLogger("org.eclipse.jetty.security.MariaDbLogs"); |
| 62 |
|
| 63 |
static MariaDBContainer MARIA_DB; |
| 64 |
|
| 65 |
protected static final String MARIA_DB_USER = "beer"; |
| 66 |
protected static final String MARIA_DB_PASSWORD = "pacific_ale"; |
| 67 |
public static String MARIA_DB_DRIVER_CLASS; |
| 68 |
public static String MARIA_DB_URL; |
| 69 |
public static String MARIA_DB_FULL_URL; |
| 70 |
|
| 71 |
protected Server _server; |
| 72 |
protected static String _protocol; |
| 73 |
protected static URI _baseUri; |
| 74 |
protected LoginService _loginService; |
| 75 |
protected String _resourceBase; |
| 76 |
protected TestHandler _handler; |
| 77 |
protected static String _requestContent; |
| 78 |
|
| 79 |
protected static File _dbRoot; |
| 80 |
|
| 81 |
static |
| 82 |
{ |
| 83 |
try |
| 84 |
{ |
| 85 |
MARIA_DB = |
| 86 |
new MariaDBContainer("mariadb:" + System.getProperty("mariadb.docker.version", "10.3.6")) |
| 87 |
.withUsername(MARIA_DB_USER) |
| 88 |
.withPassword(MARIA_DB_PASSWORD); |
| 89 |
MARIA_DB = (MariaDBContainer)MARIA_DB.withInitScript("createdb.sql"); |
| 90 |
MARIA_DB = (MariaDBContainer)MARIA_DB.withLogConsumer(new Slf4jLogConsumer(MARIADB_LOG)); |
| 91 |
MARIA_DB.start(); |
| 92 |
String containerIpAddress = MARIA_DB.getContainerIpAddress(); |
| 93 |
int mariadbPort = MARIA_DB.getMappedPort(3306); |
| 94 |
MARIA_DB_URL = MARIA_DB.getJdbcUrl(); |
| 95 |
MARIA_DB_FULL_URL = MARIA_DB_URL + "?user=" + MARIA_DB_USER + "&password=" + MARIA_DB_PASSWORD; |
| 96 |
MARIA_DB_DRIVER_CLASS = MARIA_DB.getDriverClassName(); |
| 97 |
} |
| 98 |
catch (Exception e) |
| 99 |
{ |
| 100 |
throw new RuntimeException(e.getMessage(), e); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public static class TestHandler extends AbstractHandler |
| 105 |
{ |
| 106 |
private final String _resourcePath; |
| 107 |
private String _requestContent; |
| 108 |
|
| 109 |
public TestHandler(String repositoryPath) |
| 110 |
{ |
| 111 |
_resourcePath = repositoryPath; |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, |
| 116 |
HttpServletRequest request, HttpServletResponse response) |
| 117 |
throws IOException, ServletException |
| 118 |
{ |
| 119 |
if (baseRequest.isHandled()) |
| 120 |
{ |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
OutputStream out = null; |
| 125 |
|
| 126 |
if (baseRequest.getMethod().equals("PUT")) |
| 127 |
{ |
| 128 |
baseRequest.setHandled(true); |
| 129 |
|
| 130 |
File file = new File(_resourcePath, URLDecoder.decode(request.getPathInfo(), "utf-8")); |
| 131 |
FS.ensureDirExists(file.getParentFile()); |
| 132 |
|
| 133 |
out = new FileOutputStream(file); |
| 134 |
|
| 135 |
response.setStatus(HttpServletResponse.SC_CREATED); |
| 136 |
} |
| 137 |
|
| 138 |
if (baseRequest.getMethod().equals("POST")) |
| 139 |
{ |
| 140 |
baseRequest.setHandled(true); |
| 141 |
out = new ByteArrayOutputStream(); |
| 142 |
|
| 143 |
response.setStatus(HttpServletResponse.SC_OK); |
| 144 |
} |
| 145 |
|
| 146 |
if (out != null) |
| 147 |
{ |
| 148 |
try (ServletInputStream in = request.getInputStream()) |
| 149 |
{ |
| 150 |
IO.copy(in, out); |
| 151 |
} |
| 152 |
finally |
| 153 |
{ |
| 154 |
out.close(); |
| 155 |
} |
| 156 |
|
| 157 |
if (!(out instanceof FileOutputStream)) |
| 158 |
_requestContent = out.toString(); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
public String getRequestContent() |
| 163 |
{ |
| 164 |
return _requestContent; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
public DatabaseLoginServiceTestServer() |
| 169 |
{ |
| 170 |
_server = new Server(0); |
| 171 |
} |
| 172 |
|
| 173 |
public void setLoginService(LoginService loginService) |
| 174 |
{ |
| 175 |
_loginService = loginService; |
| 176 |
} |
| 177 |
|
| 178 |
public void setResourceBase(String resourceBase) |
| 179 |
{ |
| 180 |
_resourceBase = resourceBase; |
| 181 |
} |
| 182 |
|
| 183 |
public void start() throws Exception |
| 184 |
{ |
| 185 |
configureServer(); |
| 186 |
_server.start(); |
| 187 |
//_server.dumpStdErr(); |
| 188 |
_baseUri = _server.getURI(); |
| 189 |
} |
| 190 |
|
| 191 |
public void stop() throws Exception |
| 192 |
{ |
| 193 |
_server.stop(); |
| 194 |
} |
| 195 |
|
| 196 |
public URI getBaseUri() |
| 197 |
{ |
| 198 |
return _baseUri; |
| 199 |
} |
| 200 |
|
| 201 |
public TestHandler getTestHandler() |
| 202 |
{ |
| 203 |
return _handler; |
| 204 |
} |
| 205 |
|
| 206 |
public Server getServer() |
| 207 |
{ |
| 208 |
return _server; |
| 209 |
} |
| 210 |
|
| 211 |
protected void configureServer() throws Exception |
| 212 |
{ |
| 213 |
_protocol = "http"; |
| 214 |
_server.addBean(_loginService); |
| 215 |
|
| 216 |
ConstraintSecurityHandler security = new ConstraintSecurityHandler(); |
| 217 |
_server.setHandler(security); |
| 218 |
|
| 219 |
Constraint constraint = new Constraint(); |
| 220 |
constraint.setName("auth"); |
| 221 |
constraint.setAuthenticate(true); |
| 222 |
constraint.setRoles(new String[]{"user", "admin"}); |
| 223 |
|
| 224 |
ConstraintMapping mapping = new ConstraintMapping(); |
| 225 |
mapping.setPathSpec("/*"); |
| 226 |
mapping.setConstraint(constraint); |
| 227 |
|
| 228 |
Set<String> knownRoles = new HashSet<>(); |
| 229 |
knownRoles.add("user"); |
| 230 |
knownRoles.add("admin"); |
| 231 |
|
| 232 |
security.setConstraintMappings(Collections.singletonList(mapping), knownRoles); |
| 233 |
security.setAuthenticator(new BasicAuthenticator()); |
| 234 |
security.setLoginService(_loginService); |
| 235 |
|
| 236 |
ServletContextHandler root = new ServletContextHandler(); |
| 237 |
root.setContextPath("/"); |
| 238 |
root.setResourceBase(_resourceBase); |
| 239 |
ServletHolder servletHolder = new ServletHolder(new DefaultServlet()); |
| 240 |
servletHolder.setInitParameter("gzip", "true"); |
| 241 |
root.addServlet(servletHolder, "/*"); |
| 242 |
|
| 243 |
_handler = new TestHandler(_resourceBase); |
| 244 |
|
| 245 |
HandlerCollection handlers = new HandlerCollection(); |
| 246 |
handlers.setHandlers(new Handler[]{_handler, root}); |
| 247 |
security.setHandler(handlers); |
| 248 |
} |
| 249 |
} |