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 // Enable SPNEGO authentication 081 if ("kerberos".equalsIgnoreCase(c.get(HttpServer.HTTP_UI_AUTHENTICATION, null))) { 082 builder.setUsernameConfKey(HttpServer.HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_KEY) 083 .setKeytabConfKey(HttpServer.HTTP_SPNEGO_AUTHENTICATION_KEYTAB_KEY) 084 .setKerberosNameRulesKey(HttpServer.HTTP_SPNEGO_AUTHENTICATION_KRB_NAME_KEY) 085 .setSignatureSecretFileKey(HttpServer.HTTP_AUTHENTICATION_SIGNATURE_SECRET_FILE_KEY) 086 .setSecurityEnabled(true); 087 088 // Set an admin ACL on sensitive webUI endpoints 089 AccessControlList acl = buildAdminAcl(c); 090 builder.setACL(acl); 091 } 092 this.httpServer = builder.build(); 093 } 094 095 /** 096 * Builds an ACL that will restrict the users who can issue commands to endpoints on the UI which 097 * are meant only for administrators. 098 */ 099 AccessControlList buildAdminAcl(Configuration conf) { 100 final String userGroups = conf.get(HttpServer.HTTP_SPNEGO_AUTHENTICATION_ADMIN_USERS_KEY, null); 101 final String adminGroups = 102 conf.get(HttpServer.HTTP_SPNEGO_AUTHENTICATION_ADMIN_GROUPS_KEY, null); 103 if (userGroups == null && adminGroups == null) { 104 // Backwards compatibility - if the user doesn't have anything set, allow all users in. 105 return new AccessControlList("*", null); 106 } 107 return new AccessControlList(userGroups, adminGroups); 108 } 109 110 /** 111 * Explicitly invoke {@link #addPrivilegedServlet(String, String, Class)} or 112 * {@link #addUnprivilegedServlet(String, String, Class)} instead of this method. This method will 113 * add a servlet which any authenticated user can access. 114 * @deprecated Use {@link #addUnprivilegedServlet(String, String, Class)} or 115 * {@link #addPrivilegedServlet(String, String, Class)} instead of this method which 116 * does not state outwardly what kind of authz rules will be applied to this servlet. 117 */ 118 @Deprecated 119 public void addServlet(String name, String pathSpec, Class<? extends HttpServlet> clazz) { 120 addUnprivilegedServlet(name, pathSpec, clazz); 121 } 122 123 /** 124 * Adds a servlet in the server that any user can access. 125 * @see HttpServer#addUnprivilegedServlet(String, String, Class) 126 */ 127 public void addUnprivilegedServlet(String name, String pathSpec, 128 Class<? extends HttpServlet> clazz) { 129 this.httpServer.addUnprivilegedServlet(name, pathSpec, clazz); 130 } 131 132 /** 133 * Adds a servlet in the server that any user can access. 134 * @see HttpServer#addUnprivilegedServlet(String, ServletHolder) 135 */ 136 public void addUnprivilegedServlet(String name, String pathSpec, ServletHolder holder) { 137 if (name != null) { 138 holder.setName(name); 139 } 140 this.httpServer.addUnprivilegedServlet(pathSpec, holder); 141 } 142 143 /** 144 * Adds a servlet in the server that any user can access. 145 * @see HttpServer#addPrivilegedServlet(String, String, Class) 146 */ 147 public void addPrivilegedServlet(String name, String pathSpec, 148 Class<? extends HttpServlet> clazz) { 149 this.httpServer.addPrivilegedServlet(name, pathSpec, clazz); 150 } 151 152 public void setAttribute(String name, Object value) { 153 this.httpServer.setAttribute(name, value); 154 } 155 156 public void start() throws IOException { 157 this.httpServer.start(); 158 } 159 160 /** 161 * @return the port of the info server 162 * @deprecated Since 0.99.0 163 */ 164 @Deprecated 165 public int getPort() { 166 return this.httpServer.getPort(); 167 } 168 169 public void stop() throws Exception { 170 this.httpServer.stop(); 171 } 172 173 /** 174 * Returns true if and only if UI authentication (spnego) is enabled, UI authorization is enabled, 175 * and the requesting user is defined as an administrator. If the UI is set to readonly, this 176 * method always returns false. 177 */ 178 public static boolean canUserModifyUI(HttpServletRequest req, ServletContext ctx, 179 Configuration conf) { 180 if (conf.getBoolean("hbase.master.ui.readonly", false)) { 181 return false; 182 } 183 String remoteUser = req.getRemoteUser(); 184 if ( 185 "kerberos".equalsIgnoreCase(conf.get(HttpServer.HTTP_UI_AUTHENTICATION)) 186 && conf.getBoolean(CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION, false) 187 && remoteUser != null 188 ) { 189 return HttpServer.userHasAdministratorAccess(ctx, remoteUser); 190 } 191 return false; 192 } 193}