Ejemplo n.º 1
0
  public void start() throws IOException {
    final String infoHost = bindAddress.getHostName();
    int infoPort = bindAddress.getPort();

    httpServer =
        new HttpServer(
            "hdfs",
            infoHost,
            infoPort,
            infoPort == 0,
            conf,
            new AccessControlList(conf.get(DFS_ADMIN, " "))) {
          {
            // Add SPNEGO support to NameNode
            if (UserGroupInformation.isSecurityEnabled()) {
              initSpnego(
                  conf,
                  DFSConfigKeys.DFS_NAMENODE_INTERNAL_SPNEGO_USER_NAME_KEY,
                  DFSUtil.getSpnegoKeytabKey(conf, DFSConfigKeys.DFS_NAMENODE_KEYTAB_FILE_KEY));
            }
            if (WebHdfsFileSystem.isEnabled(conf, LOG)) {
              // add SPNEGO authentication filter for webhdfs
              final String name = "SPNEGO";
              final String classname = AuthFilter.class.getName();
              final String pathSpec = WebHdfsFileSystem.PATH_PREFIX + "/*";
              Map<String, String> params = getAuthFilterParams(conf);
              defineFilter(webAppContext, name, classname, params, new String[] {pathSpec});
              LOG.info("Added filter '" + name + "' (class=" + classname + ")");

              // add webhdfs packages
              addJerseyResourcePackage(
                  NamenodeWebHdfsMethods.class.getPackage().getName()
                      + ";"
                      + Param.class.getPackage().getName(),
                  pathSpec);
            }
          }

          private Map<String, String> getAuthFilterParams(Configuration conf) throws IOException {
            Map<String, String> params = new HashMap<String, String>();
            String principalInConf =
                conf.get(DFSConfigKeys.DFS_WEB_AUTHENTICATION_KERBEROS_PRINCIPAL_KEY);
            if (principalInConf != null && !principalInConf.isEmpty()) {
              params.put(
                  DFSConfigKeys.DFS_WEB_AUTHENTICATION_KERBEROS_PRINCIPAL_KEY,
                  SecurityUtil.getServerPrincipal(principalInConf, bindAddress.getHostName()));
            } else if (UserGroupInformation.isSecurityEnabled()) {
              LOG.error(
                  "WebHDFS and security are enabled, but configuration property '"
                      + DFSConfigKeys.DFS_WEB_AUTHENTICATION_KERBEROS_PRINCIPAL_KEY
                      + "' is not set.");
            }
            String httpKeytab =
                conf.get(
                    DFSUtil.getSpnegoKeytabKey(conf, DFSConfigKeys.DFS_NAMENODE_KEYTAB_FILE_KEY));
            if (httpKeytab != null && !httpKeytab.isEmpty()) {
              params.put(DFSConfigKeys.DFS_WEB_AUTHENTICATION_KERBEROS_KEYTAB_KEY, httpKeytab);
            } else if (UserGroupInformation.isSecurityEnabled()) {
              LOG.error(
                  "WebHDFS and security are enabled, but configuration property '"
                      + DFSConfigKeys.DFS_WEB_AUTHENTICATION_KERBEROS_KEYTAB_KEY
                      + "' is not set.");
            }
            return params;
          }
        };

    boolean certSSL = conf.getBoolean(DFSConfigKeys.DFS_HTTPS_ENABLE_KEY, false);
    if (certSSL) {
      boolean needClientAuth = conf.getBoolean("dfs.https.need.client.auth", false);
      InetSocketAddress secInfoSocAddr =
          NetUtils.createSocketAddr(
              infoHost
                  + ":"
                  + conf.get(DFSConfigKeys.DFS_NAMENODE_HTTPS_PORT_KEY, infoHost + ":" + 0));
      Configuration sslConf = new Configuration(false);
      if (certSSL) {
        sslConf.addResource(
            conf.get(DFSConfigKeys.DFS_SERVER_HTTPS_KEYSTORE_RESOURCE_KEY, "ssl-server.xml"));
      }
      httpServer.addSslListener(secInfoSocAddr, sslConf, needClientAuth);
      // assume same ssl port for all datanodes
      InetSocketAddress datanodeSslPort =
          NetUtils.createSocketAddr(
              conf.get(DFSConfigKeys.DFS_DATANODE_HTTPS_ADDRESS_KEY, infoHost + ":" + 50475));
      httpServer.setAttribute(DFSConfigKeys.DFS_DATANODE_HTTPS_PORT_KEY, datanodeSslPort.getPort());
    }
    httpServer.setAttribute(NAMENODE_ATTRIBUTE_KEY, nn);
    httpServer.setAttribute(JspHelper.CURRENT_CONF, conf);
    setupServlets(httpServer, conf);
    httpServer.start();
    httpAddress = new InetSocketAddress(bindAddress.getAddress(), httpServer.getPort());
  }