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.conf;
019
020import java.io.IOException;
021import java.io.Writer;
022import java.util.List;
023import java.util.Map;
024import javax.servlet.ServletException;
025import javax.servlet.http.HttpServlet;
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.hbase.http.HttpServer;
030import org.apache.yetus.audience.InterfaceAudience;
031import org.apache.yetus.audience.InterfaceStability;
032
033import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;
034
035/**
036 * A servlet to print out the running configuration data.
037 */
038@InterfaceAudience.LimitedPrivate({ "HBase" })
039@InterfaceStability.Unstable
040public class ConfServlet extends HttpServlet {
041  private static final long serialVersionUID = 1L;
042
043  private static final String FORMAT_JSON = "json";
044  private static final String FORMAT_XML = "xml";
045  private static final String FORMAT_PARAM = "format";
046  private static final List<String> MASK_PROPERTIES =
047    ImmutableList.of("password", "secret", "superuser");
048  static final String MASKED = "<masked>";
049
050  /**
051   * Return the Configuration of the daemon hosting this servlet. This is populated when the
052   * HttpServer starts.
053   */
054  private Configuration getConfFromContext() {
055    Configuration conf =
056      (Configuration) getServletContext().getAttribute(HttpServer.CONF_CONTEXT_ATTRIBUTE);
057    assert conf != null;
058    return conf;
059  }
060
061  @Override
062  public void doGet(HttpServletRequest request, HttpServletResponse response)
063    throws ServletException, IOException {
064    if (!HttpServer.isInstrumentationAccessAllowed(getServletContext(), request, response)) {
065      return;
066    }
067
068    String format = request.getParameter(FORMAT_PARAM);
069    if (null == format) {
070      format = FORMAT_XML;
071    }
072
073    if (FORMAT_XML.equals(format)) {
074      response.setContentType("text/xml; charset=utf-8");
075    } else if (FORMAT_JSON.equals(format)) {
076      response.setContentType("application/json; charset=utf-8");
077    }
078
079    Writer out = response.getWriter();
080    try {
081      writeResponse(getConfFromContext(), out, format);
082    } catch (BadFormatException bfe) {
083      response.sendError(HttpServletResponse.SC_BAD_REQUEST, bfe.getMessage());
084    }
085    out.close();
086  }
087
088  /**
089   * Guts of the servlet - extracted for easy testing.
090   */
091  static void writeResponse(Configuration conf, Writer out, String format)
092    throws IOException, BadFormatException {
093    Configuration maskedConf = mask(conf);
094    if (FORMAT_JSON.equals(format)) {
095      Configuration.dumpConfiguration(maskedConf, out);
096    } else if (FORMAT_XML.equals(format)) {
097      maskedConf.writeXml(out);
098    } else {
099      throw new BadFormatException("Bad format: " + format);
100    }
101  }
102
103  static Configuration mask(Configuration conf) {
104    Configuration maskedConf = new Configuration(conf);
105    for (Map.Entry<String, String> entry : maskedConf) {
106      String key = entry.getKey();
107      for (String maskProperty : MASK_PROPERTIES) {
108        if (key.toLowerCase().contains(maskProperty)) {
109          maskedConf.set(key, MASKED);
110          break;
111        }
112      }
113    }
114    return maskedConf;
115  }
116
117  public static class BadFormatException extends Exception {
118    private static final long serialVersionUID = 1L;
119
120    public BadFormatException(String msg) {
121      super(msg);
122    }
123  }
124}