001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with this
004 * work for additional information regarding copyright ownership. The ASF
005 * licenses this file to you under the Apache License, Version 2.0 (the
006 * "License"); you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 * 
009 * http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014 * License for the specific language governing permissions and limitations under
015 * the License.
016 */
017package org.apache.hadoop.hdfs.server.datanode;
018
019import java.net.InetSocketAddress;
020import java.net.ServerSocket;
021import java.nio.channels.ServerSocketChannel;
022
023import org.apache.commons.daemon.Daemon;
024import org.apache.commons.daemon.DaemonContext;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hdfs.DFSConfigKeys;
027import org.apache.hadoop.hdfs.DFSUtil;
028import org.apache.hadoop.hdfs.HdfsConfiguration;
029import org.apache.hadoop.hdfs.server.common.HdfsServerConstants;
030import org.apache.hadoop.http.HttpConfig;
031import org.apache.hadoop.http.HttpServer2;
032import org.apache.hadoop.security.SecurityUtil;
033import org.apache.hadoop.security.UserGroupInformation;
034import org.mortbay.jetty.Connector;
035
036import com.google.common.annotations.VisibleForTesting;
037
038/**
039 * Utility class to start a datanode in a secure cluster, first obtaining 
040 * privileged resources before main startup and handing them to the datanode.
041 */
042public class SecureDataNodeStarter implements Daemon {
043  /**
044   * Stash necessary resources needed for datanode operation in a secure env.
045   */
046  public static class SecureResources {
047    private final ServerSocket streamingSocket;
048    private final Connector listener;
049    public SecureResources(ServerSocket streamingSocket,
050        Connector listener) {
051
052      this.streamingSocket = streamingSocket;
053      this.listener = listener;
054    }
055
056    public ServerSocket getStreamingSocket() { return streamingSocket; }
057
058    public Connector getListener() { return listener; }
059  }
060  
061  private String [] args;
062  private SecureResources resources;
063
064  @Override
065  public void init(DaemonContext context) throws Exception {
066    System.err.println("Initializing secure datanode resources");
067    // Create a new HdfsConfiguration object to ensure that the configuration in
068    // hdfs-site.xml is picked up.
069    Configuration conf = new HdfsConfiguration();
070    
071    // Stash command-line arguments for regular datanode
072    args = context.getArguments();
073    resources = getSecureResources(conf);
074  }
075
076  @Override
077  public void start() throws Exception {
078    System.err.println("Starting regular datanode initialization");
079    DataNode.secureMain(args, resources);
080  }
081
082  @Override public void destroy() {}
083  @Override public void stop() throws Exception { /* Nothing to do */ }
084
085  /**
086   * Acquire privileged resources (i.e., the privileged ports) for the data
087   * node. The privileged resources consist of the port of the RPC server and
088   * the port of HTTP (not HTTPS) server.
089   */
090  @VisibleForTesting
091  public static SecureResources getSecureResources(Configuration conf)
092      throws Exception {
093    HttpConfig.Policy policy = DFSUtil.getHttpPolicy(conf);
094    boolean isSecure = UserGroupInformation.isSecurityEnabled();
095
096    // Obtain secure port for data streaming to datanode
097    InetSocketAddress streamingAddr  = DataNode.getStreamingAddr(conf);
098    int socketWriteTimeout = conf.getInt(
099        DFSConfigKeys.DFS_DATANODE_SOCKET_WRITE_TIMEOUT_KEY,
100        HdfsServerConstants.WRITE_TIMEOUT);
101
102    ServerSocket ss = (socketWriteTimeout > 0) ? 
103        ServerSocketChannel.open().socket() : new ServerSocket();
104    ss.bind(streamingAddr, 0);
105
106    // Check that we got the port we need
107    if (ss.getLocalPort() != streamingAddr.getPort()) {
108      throw new RuntimeException(
109          "Unable to bind on specified streaming port in secure "
110              + "context. Needed " + streamingAddr.getPort() + ", got "
111              + ss.getLocalPort());
112    }
113
114    if (!SecurityUtil.isPrivilegedPort(ss.getLocalPort()) && isSecure) {
115      throw new RuntimeException(
116        "Cannot start secure datanode with unprivileged RPC ports");
117    }
118
119    System.err.println("Opened streaming server at " + streamingAddr);
120
121    // Bind a port for the web server. The code intends to bind HTTP server to
122    // privileged port only, as the client can authenticate the server using
123    // certificates if they are communicating through SSL.
124    Connector listener = null;
125    if (policy.isHttpEnabled()) {
126      listener = HttpServer2.createDefaultChannelConnector();
127      InetSocketAddress infoSocAddr = DataNode.getInfoAddr(conf);
128      listener.setHost(infoSocAddr.getHostName());
129      listener.setPort(infoSocAddr.getPort());
130      // Open listener here in order to bind to port as root
131      listener.open();
132      if (listener.getPort() != infoSocAddr.getPort()) {
133        throw new RuntimeException("Unable to bind on specified info port in secure " +
134            "context. Needed " + streamingAddr.getPort() + ", got " + ss.getLocalPort());
135      }
136      System.err.println("Successfully obtained privileged resources (streaming port = "
137          + ss + " ) (http listener port = " + listener.getConnection() +")");
138
139      if (listener.getPort() > 1023 && isSecure) {
140        throw new RuntimeException(
141            "Cannot start secure datanode with unprivileged HTTP ports");
142      }
143      System.err.println("Opened info server at " + infoSocAddr);
144    }
145
146    return new SecureResources(ss, listener);
147  }
148
149}