1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.tomcat.websocket;
import java.net.URI;
import java.util.Queue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import jakarta.websocket.ClientEndpointConfig;
import jakarta.websocket.ContainerProvider;
import jakarta.websocket.Session;
import jakarta.websocket.WebSocketContainer;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.Context;
import org.apache.catalina.authenticator.AuthenticatorBase;
import org.apache.catalina.servlets.DefaultServlet;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.descriptor.web.LoginConfig;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.apache.tomcat.websocket.TesterMessageCountClient.BasicText;
import org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint;
/*
* Tests WebSocket connections via a forward proxy.
*
* These tests have been successfully used with Apache Web Server (httpd)
* configured with the following:
*
* Listen 8888
* <VirtualHost *:8888>
* ProxyRequests On
* ProxyVia On
* AllowCONNECT 0-65535
* </VirtualHost>
*
* Listen 8889
* <VirtualHost *:8889>
* ProxyRequests On
* ProxyVia On
* AllowCONNECT 0-65535
* <Proxy *>
* Order deny,allow
* Allow from all
* AuthType Basic
* AuthName "Proxy Password Required"
* AuthUserFile password.file
* Require valid-user
* </Proxy>
* </VirtualHost>
*
* and
* # htpasswd -c password.file proxy
* New Password: proxy-pass
*
*/
public class TesterWebSocketClientProxy extends WebSocketBaseTest {
private static final String MESSAGE_STRING = "proxy-test-message";
private static final String PROXY_ADDRESS = "192.168.0.200";
private static final String PROXY_PORT_NO_AUTH = "8888";
private static final String PROXY_PORT_AUTH = "8889";
// The IP address of the test instance that is reachable from the proxy
private static final String TOMCAT_ADDRESS = "192.168.0.100";
private static final String TOMCAT_USER = "tomcat";
private static final String TOMCAT_PASSWORD = "tomcat-pass";
private static final String TOMCAT_ROLE = "tomcat-role";
private static final String PROXY_USER = "proxy";
private static final String PROXY_PASSWORD = "proxy-pass";
@Test
public void testConnectToServerViaProxyWithNoAuthentication() throws Exception {
doTestConnectToServerViaProxy(false, false);
}
@Test
public void testConnectToServerViaProxyWithServerAuthentication() throws Exception {
doTestConnectToServerViaProxy(true, false);
}
@Test
public void testConnectToServerViaProxyWithProxyAuthentication() throws Exception {
doTestConnectToServerViaProxy(false, true);
}
@Test
public void testConnectToServerViaProxyWithServerAndProxyAuthentication() throws Exception {
doTestConnectToServerViaProxy(true, true);
}
private void doTestConnectToServerViaProxy(boolean serverAuthentication, boolean proxyAuthentication)
throws Exception {
// Configure the proxy
System.setProperty("http.proxyHost", PROXY_ADDRESS);
if (proxyAuthentication) {
System.setProperty("http.proxyPort", PROXY_PORT_AUTH);
} else {
System.setProperty("http.proxyPort", PROXY_PORT_NO_AUTH);
}
Tomcat tomcat = getTomcatInstance();
// Need to listen on all addresses, not just loop-back
tomcat.getConnector().setProperty("address", "0.0.0.0");
// No file system docBase required
Context ctx = getProgrammaticRootContext();
ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
if (serverAuthentication) {
// Configure Realm
tomcat.addUser(TOMCAT_USER, TOMCAT_PASSWORD);
tomcat.addRole(TOMCAT_USER, TOMCAT_ROLE);
// Configure security constraints
SecurityCollection securityCollection = new SecurityCollection();
securityCollection.addPatternDecoded("/*");
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.addAuthRole(TOMCAT_ROLE);
securityConstraint.addCollection(securityCollection);
ctx.addConstraint(securityConstraint);
// Configure authenticator
LoginConfig loginConfig = new LoginConfig();
loginConfig.setAuthMethod(BasicAuthenticator.schemeName);
ctx.setLoginConfig(loginConfig);
AuthenticatorBase basicAuthenticator = new org.apache.catalina.authenticator.BasicAuthenticator();
ctx.getPipeline().addValve(basicAuthenticator);
}
tomcat.start();
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
// Configure the client
if (serverAuthentication) {
clientEndpointConfig.getUserProperties().put(Constants.WS_AUTHENTICATION_USER_NAME, TOMCAT_USER);
clientEndpointConfig.getUserProperties().put(Constants.WS_AUTHENTICATION_PASSWORD, TOMCAT_PASSWORD);
}
if (proxyAuthentication) {
clientEndpointConfig.getUserProperties().put(Constants.WS_AUTHENTICATION_PROXY_USER_NAME, PROXY_USER);
clientEndpointConfig.getUserProperties().put(Constants.WS_AUTHENTICATION_PROXY_PASSWORD, PROXY_PASSWORD);
}
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig,
new URI("ws://" + TOMCAT_ADDRESS + ":" + getPort() + TesterEchoServer.Config.PATH_ASYNC));
CountDownLatch latch = new CountDownLatch(1);
BasicText handler = new BasicText(latch);
wsSession.addMessageHandler(handler);
wsSession.getBasicRemote().sendText(MESSAGE_STRING);
boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
Assert.assertTrue(latchResult);
Queue<String> messages = handler.getMessages();
Assert.assertEquals(1, messages.size());
Assert.assertEquals(MESSAGE_STRING, messages.peek());
}
}
|