Search in sources :

Example 1 with AuthPacket

use of org.apache.zookeeper.proto.AuthPacket in project zookeeper by apache.

the class ClientCnxn method addAuthInfo.

public void addAuthInfo(String scheme, byte[] auth) {
    if (!state.isAlive()) {
        return;
    }
    authInfo.add(new AuthData(scheme, auth));
    queuePacket(new RequestHeader(-4, OpCode.auth), null, new AuthPacket(0, scheme, auth), null, null, null, null, null, null);
}
Also used : AuthPacket(org.apache.zookeeper.proto.AuthPacket) RequestHeader(org.apache.zookeeper.proto.RequestHeader)

Example 2 with AuthPacket

use of org.apache.zookeeper.proto.AuthPacket in project zookeeper by apache.

the class ZooKeeperServer method processPacket.

public void processPacket(ServerCnxn cnxn, ByteBuffer incomingBuffer) throws IOException {
    // We have the request, now process and setup for next
    InputStream bais = new ByteBufferInputStream(incomingBuffer);
    BinaryInputArchive bia = BinaryInputArchive.getArchive(bais);
    RequestHeader h = new RequestHeader();
    h.deserialize(bia, "header");
    // Through the magic of byte buffers, txn will not be
    // pointing
    // to the start of the txn
    incomingBuffer = incomingBuffer.slice();
    if (h.getType() == OpCode.auth) {
        LOG.info("got auth packet " + cnxn.getRemoteSocketAddress());
        AuthPacket authPacket = new AuthPacket();
        ByteBufferInputStream.byteBuffer2Record(incomingBuffer, authPacket);
        String scheme = authPacket.getScheme();
        ServerAuthenticationProvider ap = ProviderRegistry.getServerProvider(scheme);
        Code authReturn = KeeperException.Code.AUTHFAILED;
        if (ap != null) {
            try {
                authReturn = ap.handleAuthentication(new ServerAuthenticationProvider.ServerObjs(this, cnxn), authPacket.getAuth());
            } catch (RuntimeException e) {
                LOG.warn("Caught runtime exception from AuthenticationProvider: " + scheme + " due to " + e);
                authReturn = KeeperException.Code.AUTHFAILED;
            }
        }
        if (authReturn == KeeperException.Code.OK) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Authentication succeeded for scheme: " + scheme);
            }
            LOG.info("auth success " + cnxn.getRemoteSocketAddress());
            ReplyHeader rh = new ReplyHeader(h.getXid(), 0, KeeperException.Code.OK.intValue());
            cnxn.sendResponse(rh, null, null);
        } else {
            if (ap == null) {
                LOG.warn("No authentication provider for scheme: " + scheme + " has " + ProviderRegistry.listProviders());
            } else {
                LOG.warn("Authentication failed for scheme: " + scheme);
            }
            // send a response...
            ReplyHeader rh = new ReplyHeader(h.getXid(), 0, KeeperException.Code.AUTHFAILED.intValue());
            cnxn.sendResponse(rh, null, null);
            // ... and close connection
            cnxn.sendBuffer(ServerCnxnFactory.closeConn);
            cnxn.disableRecv();
        }
        return;
    } else {
        if (h.getType() == OpCode.sasl) {
            Record rsp = processSasl(incomingBuffer, cnxn);
            ReplyHeader rh = new ReplyHeader(h.getXid(), 0, KeeperException.Code.OK.intValue());
            // not sure about 3rd arg..what is it?
            cnxn.sendResponse(rh, rsp, "response");
        } else {
            Request si = new Request(cnxn, cnxn.getSessionId(), h.getXid(), h.getType(), incomingBuffer, cnxn.getAuthInfo());
            si.setOwner(ServerCnxn.me);
            // Always treat packet from the client as a possible
            // local request.
            setLocalSessionFlag(si);
            submitRequest(si);
        }
    }
    cnxn.incrOutstandingRequests(h);
}
Also used : ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) InputStream(java.io.InputStream) ConnectRequest(org.apache.zookeeper.proto.ConnectRequest) GetSASLRequest(org.apache.zookeeper.proto.GetSASLRequest) ServerAuthenticationProvider(org.apache.zookeeper.server.auth.ServerAuthenticationProvider) Code(org.apache.zookeeper.KeeperException.Code) OpCode(org.apache.zookeeper.ZooDefs.OpCode) BinaryInputArchive(org.apache.jute.BinaryInputArchive) AuthPacket(org.apache.zookeeper.proto.AuthPacket) RequestHeader(org.apache.zookeeper.proto.RequestHeader) Record(org.apache.jute.Record)

Aggregations

AuthPacket (org.apache.zookeeper.proto.AuthPacket)2 RequestHeader (org.apache.zookeeper.proto.RequestHeader)2 InputStream (java.io.InputStream)1 BinaryInputArchive (org.apache.jute.BinaryInputArchive)1 Record (org.apache.jute.Record)1 Code (org.apache.zookeeper.KeeperException.Code)1 OpCode (org.apache.zookeeper.ZooDefs.OpCode)1 ConnectRequest (org.apache.zookeeper.proto.ConnectRequest)1 GetSASLRequest (org.apache.zookeeper.proto.GetSASLRequest)1 ReplyHeader (org.apache.zookeeper.proto.ReplyHeader)1 ServerAuthenticationProvider (org.apache.zookeeper.server.auth.ServerAuthenticationProvider)1