1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */1819package org.apache.giraph.comm.netty.handler;
2021import org.apache.giraph.comm.netty.NettyServer;
22import org.apache.giraph.comm.netty.SaslNettyServer;
23import org.apache.log4j.Logger;
2425import io.netty.channel.ChannelInboundHandlerAdapter;
26import io.netty.channel.ChannelHandlerContext;
27/**28 * Authorize or deny client requests based on existence and completeness29 * of client's SASL authentication.30 */31publicclassAuthorizeServerHandlerextends32 ChannelInboundHandlerAdapter {
33/** Class logger */34privatestaticfinal Logger LOG =
35 Logger.getLogger(AuthorizeServerHandler.class);
3637/**38 * Constructor.39 */40publicAuthorizeServerHandler() {
41 }
4243 @Override
44publicvoid channelRead(ChannelHandlerContext ctx, Object msg)
45throws Exception {
46if (LOG.isDebugEnabled()) {
47 LOG.debug("messageReceived: Got " + msg.getClass());
48 }
49// Authorize: client is allowed to doRequest() if and only if the client50// has successfully authenticated with this server.51SaslNettyServer saslNettyServer =
52 ctx.attr(NettyServer.CHANNEL_SASL_NETTY_SERVERS).get();
53if (saslNettyServer == null) {
54 LOG.warn("messageReceived: This client is *NOT* authorized to perform " +
55"this action since there's no saslNettyServer to " +
56"authenticate the client: " +
57"refusing to perform requested action: " + msg);
58return;
59 }
6061if (!saslNettyServer.isComplete()) {
62 LOG.warn("messageReceived: This client is *NOT* authorized to perform " +
63"this action because SASL authentication did not complete: " +
64"refusing to perform requested action: " + msg);
65// Return now *WITHOUT* sending upstream here, since client66// not authorized.67return;
68 }
69if (LOG.isDebugEnabled()) {
70 LOG.debug("messageReceived: authenticated client: " +
71 saslNettyServer.getUserName() + " is authorized to do request " +
72"on server.");
73 }
74// We call fireChannelRead since the client is allowed to perform this75// request. The client's request will now proceed to the next76// pipeline component, namely, RequestServerHandler.77 ctx.fireChannelRead(msg);
78 }
79 }