001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.http; 019 020import java.io.IOException; 021import java.net.URI; 022import javax.servlet.ServletContext; 023import javax.servlet.http.HttpServlet; 024import javax.servlet.http.HttpServletRequest; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.fs.CommonConfigurationKeys; 027import org.apache.hadoop.hbase.HBaseConfiguration; 028import org.apache.hadoop.security.authorize.AccessControlList; 029import org.apache.yetus.audience.InterfaceAudience; 030 031import org.apache.hbase.thirdparty.com.google.common.net.HostAndPort; 032import org.apache.hbase.thirdparty.org.eclipse.jetty.servlet.ServletHolder; 033 034/** 035 * Create a Jetty embedded server to answer http requests. The primary goal is to serve up status 036 * information for the server. There are three contexts: "/stacks/" -> points to stack trace 037 * "/static/" -> points to common static files (src/hbase-webapps/static) "/" -> the jsp 038 * server code from (src/hbase-webapps/<name>) 039 */ 040@InterfaceAudience.Private 041public class InfoServer { 042 private static final String HBASE_APP_DIR = "hbase-webapps"; 043 private final org.apache.hadoop.hbase.http.HttpServer httpServer; 044 045 /** 046 * Create a status server on the given port. The jsp scripts are taken from 047 * src/hbase-webapps/<code>name</code>. 048 * @param name The name of the server 049 * @param bindAddress address to bind to 050 * @param port The port to use on the server 051 * @param findPort whether the server should start at the given port and increment by 1 until 052 * it finds a free port. 053 * @param c the {@link Configuration} to build the server 054 * @throws IOException if getting one of the password fails or the server cannot be created 055 */ 056 public InfoServer(String name, String bindAddress, int port, boolean findPort, 057 final Configuration c) throws IOException { 058 HttpConfig httpConfig = new HttpConfig(c); 059 HttpServer.Builder builder = new org.apache.hadoop.hbase.http.HttpServer.Builder(); 060 061 builder.setName(name) 062 .addEndpoint(URI 063 .create(httpConfig.getSchemePrefix() + HostAndPort.fromParts(bindAddress, port).toString())) 064 .setAppDir(HBASE_APP_DIR).setFindPort(findPort).setConf(c); 065 String logDir = System.getProperty("hbase.log.dir"); 066 if (logDir != null) { 067 builder.setLogDir(logDir); 068 } 069 if (httpConfig.isSecure()) { 070 builder 071 .keyPassword(HBaseConfiguration.getPassword(c, "ssl.server.keystore.keypassword", null)) 072 .keyStore(c.get("ssl.server.keystore.location"), 073 HBaseConfiguration.getPassword(c, "ssl.server.keystore.password", null), 074 c.get("ssl.server.keystore.type", "jks")) 075 .trustStore(c.get("ssl.server.truststore.location"), 076 HBaseConfiguration.getPassword(c, "ssl.server.truststore.password", null), 077 c.get("ssl.server.truststore.type", "jks")); 078 builder.excludeCiphers(c.get("ssl.server.exclude.cipher.list")); 079 } 080 081 final String httpAuthType = c.get(HttpServer.HTTP_UI_AUTHENTICATION, "").toLowerCase(); 082 // Enable SPNEGO authentication 083 if ("kerberos".equals(httpAuthType)) { 084 builder.setUsernameConfKey(HttpServer.HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_KEY) 085 .setKeytabConfKey(HttpServer.HTTP_SPNEGO_AUTHENTICATION_KEYTAB_KEY) 086 .setKerberosNameRulesKey(HttpServer.HTTP_SPNEGO_AUTHENTICATION_KRB_NAME_KEY) 087 .setSignatureSecretFileKey(HttpServer.HTTP_AUTHENTICATION_SIGNATURE_SECRET_FILE_KEY) 088 .setSecurityEnabled(true); 089 } 090 091 // Set an admin ACL on sensitive webUI endpoints (works only if SPNEGO or LDAP is enabled) 092 if ("ldap".equals(httpAuthType) || "kerberos".equals(httpAuthType)) { 093 AccessControlList acl = buildAdminAcl(c); 094 builder.setACL(acl); 095 } 096 097 this.httpServer = builder.build(); 098 } 099 100 /** 101 * Builds an ACL that will restrict the users who can issue commands to endpoints on the UI which 102 * are meant only for administrators. 103 */ 104 static AccessControlList buildAdminAcl(Configuration conf) { 105 // Initialize admin users based on whether http ui auth is set to ldap or kerberos 106 String httpAuthType = conf.get(HttpServer.HTTP_UI_AUTHENTICATION, "").toLowerCase(); 107 final String adminUsers = getAdminUsers(conf, httpAuthType); 108 final String adminGroups = 109 conf.get(HttpServer.HTTP_SPNEGO_AUTHENTICATION_ADMIN_GROUPS_KEY, null); 110 if (adminUsers == null && adminGroups == null) { 111 // Backwards compatibility - if the user doesn't have anything set, allow all users in. 112 return new AccessControlList("*", null); 113 } 114 return new AccessControlList(adminUsers, adminGroups); 115 } 116 117 private static String getAdminUsers(Configuration conf, String httpAuthType) { 118 if ("kerberos".equals(httpAuthType)) { 119 return conf.get(HttpServer.HTTP_SPNEGO_AUTHENTICATION_ADMIN_USERS_KEY, null); 120 } else if ("ldap".equals(httpAuthType)) { 121 return conf.get(HttpServer.HTTP_LDAP_AUTHENTICATION_ADMIN_USERS_KEY, null); 122 } 123 // If the auth type is not kerberos or ldap, return null 124 return null; 125 } 126 127 /** 128 * Explicitly invoke {@link #addPrivilegedServlet(String, String, Class)} or 129 * {@link #addUnprivilegedServlet(String, String, Class)} instead of this method. This method will 130 * add a servlet which any authenticated user can access. 131 * @deprecated Use {@link #addUnprivilegedServlet(String, String, Class)} or 132 * {@link #addPrivilegedServlet(String, String, Class)} instead of this method which 133 * does not state outwardly what kind of authz rules will be applied to this servlet. 134 */ 135 @Deprecated 136 public void addServlet(String name, String pathSpec, Class<? extends HttpServlet> clazz) { 137 addUnprivilegedServlet(name, pathSpec, clazz); 138 } 139 140 /** 141 * Adds a servlet in the server that any user can access. 142 * @see HttpServer#addUnprivilegedServlet(String, String, Class) 143 */ 144 public void addUnprivilegedServlet(String name, String pathSpec, 145 Class<? extends HttpServlet> clazz) { 146 this.httpServer.addUnprivilegedServlet(name, pathSpec, clazz); 147 } 148 149 /** 150 * Adds a servlet in the server that any user can access. 151 * @see HttpServer#addUnprivilegedServlet(String, ServletHolder) 152 */ 153 public void addUnprivilegedServlet(String name, String pathSpec, ServletHolder holder) { 154 if (name != null) { 155 holder.setName(name); 156 } 157 this.httpServer.addUnprivilegedServlet(pathSpec, holder); 158 } 159 160 /** 161 * Adds a servlet in the server that any user can access. 162 * @see HttpServer#addPrivilegedServlet(String, String, Class) 163 */ 164 public void addPrivilegedServlet(String name, String pathSpec, 165 Class<? extends HttpServlet> clazz) { 166 this.httpServer.addPrivilegedServlet(name, pathSpec, clazz); 167 } 168 169 public void setAttribute(String name, Object value) { 170 this.httpServer.setAttribute(name, value); 171 } 172 173 public void start() throws IOException { 174 this.httpServer.start(); 175 } 176 177 /** 178 * @return the port of the info server 179 * @deprecated Since 0.99.0 180 */ 181 @Deprecated 182 public int getPort() { 183 return this.httpServer.getPort(); 184 } 185 186 public void stop() throws Exception { 187 this.httpServer.stop(); 188 } 189 190 /** 191 * Returns true if and only if UI authentication (spnego) is enabled, UI authorization is enabled, 192 * and the requesting user is defined as an administrator. If the UI is set to readonly, this 193 * method always returns false. 194 */ 195 public static boolean canUserModifyUI(HttpServletRequest req, ServletContext ctx, 196 Configuration conf) { 197 if (conf.getBoolean("hbase.master.ui.readonly", false)) { 198 return false; 199 } 200 String remoteUser = req.getRemoteUser(); 201 if ( 202 "kerberos".equalsIgnoreCase(conf.get(HttpServer.HTTP_UI_AUTHENTICATION)) 203 && conf.getBoolean(CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION, false) 204 && remoteUser != null 205 ) { 206 return HttpServer.userHasAdministratorAccess(ctx, remoteUser); 207 } 208 return false; 209 } 210}