Search in sources :

Example 1 with ServerConnection

use of io.undertow.server.ServerConnection in project undertow by undertow-io.

the class GSSAPIAuthenticationMechanism method authenticate.

@Override
public AuthenticationMechanismOutcome authenticate(final HttpServerExchange exchange, final SecurityContext securityContext) {
    ServerConnection connection = exchange.getConnection();
    NegotiationContext negContext = connection.getAttachment(NegotiationContext.ATTACHMENT_KEY);
    if (negContext != null) {
        UndertowLogger.SECURITY_LOGGER.debugf("Existing negotiation context found for %s", exchange);
        exchange.putAttachment(NegotiationContext.ATTACHMENT_KEY, negContext);
        if (negContext.isEstablished()) {
            IdentityManager identityManager = getIdentityManager(securityContext);
            final Account account = identityManager.verify(new GSSContextCredential(negContext.getGssContext()));
            if (account != null) {
                securityContext.authenticationComplete(account, name, false);
                UndertowLogger.SECURITY_LOGGER.debugf("Authenticated as user %s with existing GSSAPI negotiation context for %s", account.getPrincipal().getName(), exchange);
                return AuthenticationMechanismOutcome.AUTHENTICATED;
            } else {
                UndertowLogger.SECURITY_LOGGER.debugf("Failed to authenticate with existing GSSAPI negotiation context for %s", exchange);
                return AuthenticationMechanismOutcome.NOT_AUTHENTICATED;
            }
        }
    }
    List<String> authHeaders = exchange.getRequestHeaders().get(AUTHORIZATION);
    if (authHeaders != null) {
        for (String current : authHeaders) {
            if (current.startsWith(NEGOTIATE_PREFIX)) {
                String base64Challenge = current.substring(NEGOTIATE_PREFIX.length());
                try {
                    ByteBuffer challenge = FlexBase64.decode(base64Challenge);
                    return runGSSAPI(exchange, challenge, securityContext);
                } catch (IOException e) {
                }
                // it was not correctly structured.
                return AuthenticationMechanismOutcome.NOT_AUTHENTICATED;
            }
        }
    }
    // No suitable header was found so authentication was not even attempted.
    return AuthenticationMechanismOutcome.NOT_ATTEMPTED;
}
Also used : Account(io.undertow.security.idm.Account) IdentityManager(io.undertow.security.idm.IdentityManager) ServerConnection(io.undertow.server.ServerConnection) GSSContextCredential(io.undertow.security.idm.GSSContextCredential) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 2 with ServerConnection

use of io.undertow.server.ServerConnection in project undertow by undertow-io.

the class LoadBalancerConnectionPoolingTestCase method before.

@BeforeClass
public static void before() throws Exception {
    ProxyHandler proxyHandler = new ProxyHandler(new LoadBalancingProxyClient().setConnectionsPerThread(1).setSoftMaxConnectionsPerThread(0).setTtl(TTL).setMaxQueueSize(1000).addHost(new URI("http", null, host, port, null, null, null), "s1"), 10000, ResponseCodeHandler.HANDLE_404);
    // Default server uses 8 io threads which is hard to test against
    undertow = Undertow.builder().setIoThreads(1).addHttpListener(port + 1, host).setHandler(proxyHandler).build();
    undertow.start();
    DefaultServer.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            final ServerConnection con = exchange.getConnection();
            if (!activeConnections.contains(con)) {
                System.out.println("added " + con);
                activeConnections.add(con);
                con.addCloseListener(new ServerConnection.CloseListener() {

                    @Override
                    public void closed(ServerConnection connection) {
                        System.out.println("Closed " + connection);
                        activeConnections.remove(connection);
                    }
                });
            }
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) ServerConnection(io.undertow.server.ServerConnection) URI(java.net.URI) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 3 with ServerConnection

use of io.undertow.server.ServerConnection in project undertow by undertow-io.

the class LoadBalancingProxyClient method getConnection.

@Override
public void getConnection(ProxyTarget target, HttpServerExchange exchange, final ProxyCallback<ProxyConnection> callback, long timeout, TimeUnit timeUnit) {
    final ExclusiveConnectionHolder holder = exchange.getConnection().getAttachment(exclusiveConnectionKey);
    if (holder != null && holder.connection.getConnection().isOpen()) {
        // Something has already caused an exclusive connection to be allocated so keep using it.
        callback.completed(exchange, holder.connection);
        return;
    }
    final Host host = selectHost(exchange);
    if (host == null) {
        callback.couldNotResolveBackend(exchange);
    } else {
        exchange.addToAttachmentList(ATTEMPTED_HOSTS, host);
        if (holder != null || (exclusivityChecker != null && exclusivityChecker.isExclusivityRequired(exchange))) {
            // If we have a holder, even if the connection was closed we now exclusivity was already requested so our client
            // may be assuming it still exists.
            host.connectionPool.connect(target, exchange, new ProxyCallback<ProxyConnection>() {

                @Override
                public void completed(HttpServerExchange exchange, ProxyConnection result) {
                    if (holder != null) {
                        holder.connection = result;
                    } else {
                        final ExclusiveConnectionHolder newHolder = new ExclusiveConnectionHolder();
                        newHolder.connection = result;
                        ServerConnection connection = exchange.getConnection();
                        connection.putAttachment(exclusiveConnectionKey, newHolder);
                        connection.addCloseListener(new ServerConnection.CloseListener() {

                            @Override
                            public void closed(ServerConnection connection) {
                                ClientConnection clientConnection = newHolder.connection.getConnection();
                                if (clientConnection.isOpen()) {
                                    safeClose(clientConnection);
                                }
                            }
                        });
                    }
                    callback.completed(exchange, result);
                }

                @Override
                public void queuedRequestFailed(HttpServerExchange exchange) {
                    callback.queuedRequestFailed(exchange);
                }

                @Override
                public void failed(HttpServerExchange exchange) {
                    UndertowLogger.PROXY_REQUEST_LOGGER.proxyFailedToConnectToBackend(exchange.getRequestURI(), host.uri);
                    callback.failed(exchange);
                }

                @Override
                public void couldNotResolveBackend(HttpServerExchange exchange) {
                    callback.couldNotResolveBackend(exchange);
                }
            }, timeout, timeUnit, true);
        } else {
            host.connectionPool.connect(target, exchange, callback, timeout, timeUnit, false);
        }
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) ServerConnection(io.undertow.server.ServerConnection) ClientConnection(io.undertow.client.ClientConnection)

Example 4 with ServerConnection

use of io.undertow.server.ServerConnection in project undertow by undertow-io.

the class ModClusterProxyClient method getConnection.

public void getConnection(final ProxyTarget target, final HttpServerExchange exchange, final ProxyCallback<ProxyConnection> callback, final long timeout, final TimeUnit timeUnit) {
    final ExclusiveConnectionHolder holder = exchange.getConnection().getAttachment(exclusiveConnectionKey);
    if (holder != null && holder.connection.getConnection().isOpen()) {
        // Something has already caused an exclusive connection to be
        // allocated so keep using it.
        callback.completed(exchange, holder.connection);
        return;
    }
    if (!(target instanceof ModClusterProxyTarget)) {
        callback.couldNotResolveBackend(exchange);
        return;
    }
    // Resolve the node
    final ModClusterProxyTarget proxyTarget = (ModClusterProxyTarget) target;
    final Context context = proxyTarget.resolveContext(exchange);
    if (context == null) {
        callback.couldNotResolveBackend(exchange);
    } else {
        if (holder != null || (exclusivityChecker != null && exclusivityChecker.isExclusivityRequired(exchange))) {
            // If we have a holder, even if the connection was closed we now
            // exclusivity was already requested so our client
            // may be assuming it still exists.
            final ProxyCallback<ProxyConnection> wrappedCallback = new ProxyCallback<ProxyConnection>() {

                @Override
                public void completed(HttpServerExchange exchange, ProxyConnection result) {
                    if (holder != null) {
                        holder.connection = result;
                    } else {
                        final ExclusiveConnectionHolder newHolder = new ExclusiveConnectionHolder();
                        newHolder.connection = result;
                        ServerConnection connection = exchange.getConnection();
                        connection.putAttachment(exclusiveConnectionKey, newHolder);
                        connection.addCloseListener(new ServerConnection.CloseListener() {

                            @Override
                            public void closed(ServerConnection connection) {
                                ClientConnection clientConnection = newHolder.connection.getConnection();
                                if (clientConnection.isOpen()) {
                                    safeClose(clientConnection);
                                }
                            }
                        });
                    }
                    callback.completed(exchange, result);
                }

                @Override
                public void queuedRequestFailed(HttpServerExchange exchange) {
                    callback.queuedRequestFailed(exchange);
                }

                @Override
                public void failed(HttpServerExchange exchange) {
                    callback.failed(exchange);
                }

                @Override
                public void couldNotResolveBackend(HttpServerExchange exchange) {
                    callback.couldNotResolveBackend(exchange);
                }
            };
            context.handleRequest(proxyTarget, exchange, wrappedCallback, timeout, timeUnit, true);
        } else {
            context.handleRequest(proxyTarget, exchange, callback, timeout, timeUnit, false);
        }
    }
}
Also used : ProxyCallback(io.undertow.server.handlers.proxy.ProxyCallback) HttpServerExchange(io.undertow.server.HttpServerExchange) ProxyConnection(io.undertow.server.handlers.proxy.ProxyConnection) ServerConnection(io.undertow.server.ServerConnection) ClientConnection(io.undertow.client.ClientConnection)

Example 5 with ServerConnection

use of io.undertow.server.ServerConnection in project undertow by undertow-io.

the class PushBuilderImpl method push.

@Override
public void push() {
    if (path == null) {
        throw UndertowServletMessages.MESSAGES.pathWasNotSet();
    }
    ServerConnection con = servletRequest.getExchange().getConnection();
    if (con.isPushSupported()) {
        HeaderMap newHeaders = new HeaderMap();
        for (HeaderValues entry : headers) {
            newHeaders.addAll(entry.getHeaderName(), entry);
        }
        if (conditional) {
            if (etag != null) {
                newHeaders.put(Headers.IF_NONE_MATCH, etag);
            } else if (lastModified != null) {
                newHeaders.put(Headers.IF_MODIFIED_SINCE, lastModified);
            }
        }
        if (sessionId != null) {
            //TODO: do this properly, may be a different tracking method or a different cookie name
            newHeaders.put(Headers.COOKIE, "JSESSIONID=" + sessionId);
        }
        String path = this.path;
        if (queryString != null && !queryString.isEmpty()) {
            path += "?" + queryString;
        }
        con.pushResource(path, new HttpString(method), newHeaders);
    }
    path = null;
    etag = null;
    lastModified = null;
}
Also used : HeaderMap(io.undertow.util.HeaderMap) HeaderValues(io.undertow.util.HeaderValues) ServerConnection(io.undertow.server.ServerConnection) HttpString(io.undertow.util.HttpString) HttpString(io.undertow.util.HttpString)

Aggregations

ServerConnection (io.undertow.server.ServerConnection)5 HttpServerExchange (io.undertow.server.HttpServerExchange)3 ClientConnection (io.undertow.client.ClientConnection)2 IOException (java.io.IOException)2 Account (io.undertow.security.idm.Account)1 GSSContextCredential (io.undertow.security.idm.GSSContextCredential)1 IdentityManager (io.undertow.security.idm.IdentityManager)1 HttpHandler (io.undertow.server.HttpHandler)1 ProxyCallback (io.undertow.server.handlers.proxy.ProxyCallback)1 ProxyConnection (io.undertow.server.handlers.proxy.ProxyConnection)1 HeaderMap (io.undertow.util.HeaderMap)1 HeaderValues (io.undertow.util.HeaderValues)1 HttpString (io.undertow.util.HttpString)1 URI (java.net.URI)1 ByteBuffer (java.nio.ByteBuffer)1 BeforeClass (org.junit.BeforeClass)1