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 com.google.common.annotations.VisibleForTesting;
020import org.apache.commons.daemon.Daemon;
021import org.apache.commons.daemon.DaemonContext;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hdfs.DFSConfigKeys;
024import org.apache.hadoop.hdfs.DFSUtil;
025import org.apache.hadoop.hdfs.HdfsConfiguration;
026import org.apache.hadoop.hdfs.server.common.HdfsServerConstants;
027import org.apache.hadoop.http.HttpConfig;
028import org.apache.hadoop.security.SecurityUtil;
029import org.apache.hadoop.security.UserGroupInformation;
030
031import java.net.InetSocketAddress;
032import java.net.ServerSocket;
033import java.nio.channels.ServerSocketChannel;
034
035/**
036 * Utility class to start a datanode in a secure cluster, first obtaining 
037 * privileged resources before main startup and handing them to the datanode.
038 */
039public class SecureDataNodeStarter implements Daemon {
040  /**
041   * Stash necessary resources needed for datanode operation in a secure env.
042   */
043  public static class SecureResources {
044    private final ServerSocket streamingSocket;
045    private final ServerSocketChannel httpServerSocket;
046    public SecureResources(ServerSocket streamingSocket, ServerSocketChannel httpServerSocket) {
047      this.streamingSocket = streamingSocket;
048      this.httpServerSocket = httpServerSocket;
049    }
050
051    public ServerSocket getStreamingSocket() { return streamingSocket; }
052
053    public ServerSocketChannel getHttpServerChannel() {
054      return httpServerSocket;
055    }
056  }
057  
058  private String [] args;
059  private SecureResources resources;
060
061  @Override
062  public void init(DaemonContext context) throws Exception {
063    System.err.println("Initializing secure datanode resources");
064    // Create a new HdfsConfiguration object to ensure that the configuration in
065    // hdfs-site.xml is picked up.
066    Configuration conf = new HdfsConfiguration();
067    
068    // Stash command-line arguments for regular datanode
069    args = context.getArguments();
070    resources = getSecureResources(conf);
071  }
072
073  @Override
074  public void start() throws Exception {
075    System.err.println("Starting regular datanode initialization");
076    DataNode.secureMain(args, resources);
077  }
078
079  @Override public void destroy() {}
080  @Override public void stop() throws Exception { /* Nothing to do */ }
081
082  /**
083   * Acquire privileged resources (i.e., the privileged ports) for the data
084   * node. The privileged resources consist of the port of the RPC server and
085   * the port of HTTP (not HTTPS) server.
086   */
087  @VisibleForTesting
088  public static SecureResources getSecureResources(Configuration conf)
089      throws Exception {
090    HttpConfig.Policy policy = DFSUtil.getHttpPolicy(conf);
091    boolean isSecure = UserGroupInformation.isSecurityEnabled();
092
093    // Obtain secure port for data streaming to datanode
094    InetSocketAddress streamingAddr  = DataNode.getStreamingAddr(conf);
095    int socketWriteTimeout = conf.getInt(
096        DFSConfigKeys.DFS_DATANODE_SOCKET_WRITE_TIMEOUT_KEY,
097        HdfsServerConstants.WRITE_TIMEOUT);
098
099    ServerSocket ss = (socketWriteTimeout > 0) ? 
100        ServerSocketChannel.open().socket() : new ServerSocket();
101    ss.bind(streamingAddr, 0);
102
103    // Check that we got the port we need
104    if (ss.getLocalPort() != streamingAddr.getPort()) {
105      throw new RuntimeException(
106          "Unable to bind on specified streaming port in secure "
107              + "context. Needed " + streamingAddr.getPort() + ", got "
108              + ss.getLocalPort());
109    }
110
111    if (!SecurityUtil.isPrivilegedPort(ss.getLocalPort()) && isSecure) {
112      throw new RuntimeException(
113        "Cannot start secure datanode with unprivileged RPC ports");
114    }
115
116    System.err.println("Opened streaming server at " + streamingAddr);
117
118    // Bind a port for the web server. The code intends to bind HTTP server to
119    // privileged port only, as the client can authenticate the server using
120    // certificates if they are communicating through SSL.
121    final ServerSocketChannel httpChannel;
122    if (policy.isHttpEnabled()) {
123      httpChannel = ServerSocketChannel.open();
124      InetSocketAddress infoSocAddr = DataNode.getInfoAddr(conf);
125      httpChannel.socket().bind(infoSocAddr);
126      InetSocketAddress localAddr = (InetSocketAddress) httpChannel.socket()
127        .getLocalSocketAddress();
128
129      if (localAddr.getPort() != infoSocAddr.getPort()) {
130        throw new RuntimeException("Unable to bind on specified info port in secure " +
131            "context. Needed " + streamingAddr.getPort() + ", got " + ss.getLocalPort());
132      }
133      System.err.println("Successfully obtained privileged resources (streaming port = "
134          + ss + " ) (http listener port = " + localAddr.getPort() +")");
135
136      if (localAddr.getPort() > 1023 && isSecure) {
137        throw new RuntimeException(
138            "Cannot start secure datanode with unprivileged HTTP ports");
139      }
140      System.err.println("Opened info server at " + infoSocAddr);
141    } else {
142      httpChannel = null;
143    }
144
145    return new SecureResources(ss, httpChannel);
146  }
147
148}