Search in sources :

Example 1 with ErrorReportValve

use of org.apache.catalina.valves.ErrorReportValve in project ranger by apache.

the class EmbeddedServer method start.

public void start() {
    SSLContext sslContext = getSSLContext();
    if (sslContext != null) {
        SSLContext.setDefault(sslContext);
    }
    final Tomcat server = new Tomcat();
    String logDir = null;
    logDir = EmbeddedServerUtil.getConfig("logdir");
    if (logDir == null) {
        logDir = EmbeddedServerUtil.getConfig("kms.log.dir");
    }
    String servername = EmbeddedServerUtil.getConfig("servername");
    String hostName = EmbeddedServerUtil.getConfig("ranger.service.host");
    int serverPort = EmbeddedServerUtil.getIntConfig("ranger.service.http.port", 6181);
    int sslPort = EmbeddedServerUtil.getIntConfig("ranger.service.https.port", -1);
    int shutdownPort = EmbeddedServerUtil.getIntConfig("ranger.service.shutdown.port", DEFAULT_SHUTDOWN_PORT);
    String shutdownCommand = EmbeddedServerUtil.getConfig("ranger.service.shutdown.command", DEFAULT_SHUTDOWN_COMMAND);
    server.setHostname(hostName);
    server.setPort(serverPort);
    server.getServer().setPort(shutdownPort);
    server.getServer().setShutdown(shutdownCommand);
    boolean isHttpsEnabled = Boolean.valueOf(EmbeddedServerUtil.getConfig("ranger.service.https.attrib.ssl.enabled", "false"));
    boolean ajpEnabled = Boolean.valueOf(EmbeddedServerUtil.getConfig("ajp.enabled", "false"));
    if (ajpEnabled) {
        Connector ajpConnector = new Connector("org.apache.coyote.ajp.AjpNioProtocol");
        ajpConnector.setPort(serverPort);
        ajpConnector.setProperty("protocol", "AJP/1.3");
        server.getService().addConnector(ajpConnector);
        // Making this as a default connector
        server.setConnector(ajpConnector);
        LOG.info("Created AJP Connector");
    } else if ((sslPort > 0) && isHttpsEnabled) {
        Connector ssl = new Connector();
        ssl.setPort(sslPort);
        ssl.setSecure(true);
        ssl.setScheme("https");
        ssl.setAttribute("SSLEnabled", "true");
        ssl.setAttribute("sslProtocol", EmbeddedServerUtil.getConfig("ranger.service.https.attrib.ssl.protocol", "TLSv1.2"));
        ssl.setAttribute("keystoreType", EmbeddedServerUtil.getConfig("ranger.keystore.file.type", RANGER_KEYSTORE_FILE_TYPE_DEFAULT));
        ssl.setAttribute("truststoreType", EmbeddedServerUtil.getConfig("ranger.truststore.file.type", RANGER_TRUSTSTORE_FILE_TYPE_DEFAULT));
        String clientAuth = EmbeddedServerUtil.getConfig("ranger.service.https.attrib.clientAuth", "false");
        if ("false".equalsIgnoreCase(clientAuth)) {
            clientAuth = EmbeddedServerUtil.getConfig("ranger.service.https.attrib.client.auth", "want");
        }
        ssl.setAttribute("clientAuth", clientAuth);
        String providerPath = EmbeddedServerUtil.getConfig("ranger.credential.provider.path");
        String keyAlias = EmbeddedServerUtil.getConfig("ranger.service.https.attrib.keystore.credential.alias", "keyStoreCredentialAlias");
        String keystorePass = null;
        if (providerPath != null && keyAlias != null) {
            keystorePass = CredentialReader.getDecryptedString(providerPath.trim(), keyAlias.trim(), EmbeddedServerUtil.getConfig("ranger.keystore.file.type", RANGER_KEYSTORE_FILE_TYPE_DEFAULT));
            if (StringUtils.isBlank(keystorePass) || "none".equalsIgnoreCase(keystorePass.trim())) {
                keystorePass = EmbeddedServerUtil.getConfig("ranger.service.https.attrib.keystore.pass");
            }
        }
        ssl.setAttribute("keyAlias", EmbeddedServerUtil.getConfig("ranger.service.https.attrib.keystore.keyalias", "rangeradmin"));
        ssl.setAttribute("keystorePass", keystorePass);
        ssl.setAttribute("keystoreFile", getKeystoreFile());
        String defaultEnabledProtocols = "TLSv1.2";
        String enabledProtocols = EmbeddedServerUtil.getConfig("ranger.service.https.attrib.ssl.enabled.protocols", defaultEnabledProtocols);
        ssl.setAttribute("sslEnabledProtocols", enabledProtocols);
        String ciphers = EmbeddedServerUtil.getConfig("ranger.tomcat.ciphers");
        if (StringUtils.isNotBlank(ciphers)) {
            ssl.setAttribute("ciphers", ciphers);
        }
        server.getService().addConnector(ssl);
        // 
        // Making this as a default connector
        // 
        server.setConnector(ssl);
    }
    updateHttpConnectorAttribConfig(server);
    File logDirectory = new File(logDir);
    if (!logDirectory.exists()) {
        logDirectory.mkdirs();
    }
    AccessLogValve valve = new AccessLogValve();
    valve.setRotatable(true);
    valve.setAsyncSupported(true);
    valve.setBuffered(false);
    valve.setEnabled(true);
    valve.setPrefix(EmbeddedServerUtil.getConfig(ACCESS_LOG_PREFIX, "access-" + hostName));
    valve.setFileDateFormat(EmbeddedServerUtil.getConfig(ACCESS_LOG_DATE_FORMAT, "-yyyy-MM-dd.HH"));
    valve.setDirectory(logDirectory.getAbsolutePath());
    valve.setSuffix(".log");
    valve.setRotatable(EmbeddedServerUtil.getBooleanConfig(ACCESS_LOG_ROTATE_ENABLED, true));
    valve.setMaxDays(EmbeddedServerUtil.getIntConfig(ACCESS_LOG_ROTATE_MAX_DAYS, 15));
    valve.setRenameOnRotate(EmbeddedServerUtil.getBooleanConfig(ACCESS_LOG_ROTATE_RENAME_ON_ROTATE, false));
    String defaultAccessLogPattern = servername.equalsIgnoreCase(KMS_SERVER_NAME) ? "%h %l %u %t \"%m %U\" %s %b %D" : "%h %l %u %t \"%r\" %s %b %D";
    String logPattern = EmbeddedServerUtil.getConfig(ACCESS_LOG_PATTERN, defaultAccessLogPattern);
    valve.setPattern(logPattern);
    server.getHost().getPipeline().addValve(valve);
    ErrorReportValve errorReportValve = new ErrorReportValve();
    boolean showServerinfo = Boolean.valueOf(EmbeddedServerUtil.getConfig("ranger.valve.errorreportvalve.showserverinfo", "true"));
    boolean showReport = Boolean.valueOf(EmbeddedServerUtil.getConfig("ranger.valve.errorreportvalve.showreport", "true"));
    errorReportValve.setShowServerInfo(showServerinfo);
    errorReportValve.setShowReport(showReport);
    server.getHost().getPipeline().addValve(errorReportValve);
    try {
        String webapp_dir = EmbeddedServerUtil.getConfig("xa.webapp.dir");
        if (StringUtils.isBlank(webapp_dir)) {
            // If webapp location property is not set, then let's derive
            // from catalina_base
            String catalina_base = EmbeddedServerUtil.getConfig("catalina.base");
            if (StringUtils.isBlank(catalina_base)) {
                LOG.severe("Tomcat Server failed to start: catalina.base and/or xa.webapp.dir is not set");
                System.exit(1);
            }
            webapp_dir = catalina_base + File.separator + "webapp";
            LOG.info("Deriving webapp folder from catalina.base property. folder=" + webapp_dir);
        }
        // String webContextName = getConfig("xa.webapp.contextName", "/");
        String webContextName = EmbeddedServerUtil.getConfig("ranger.contextName", "/");
        if (webContextName == null) {
            webContextName = "/";
        } else if (!webContextName.startsWith("/")) {
            LOG.info("Context Name [" + webContextName + "] is being loaded as [ /" + webContextName + "]");
            webContextName = "/" + webContextName;
        }
        File wad = new File(webapp_dir);
        if (wad.isDirectory()) {
            LOG.info("Webapp file =" + webapp_dir + ", webAppName = " + webContextName);
        } else if (wad.isFile()) {
            File webAppDir = new File(DEFAULT_WEBAPPS_ROOT_FOLDER);
            if (!webAppDir.exists()) {
                webAppDir.mkdirs();
            }
            LOG.info("Webapp file =" + webapp_dir + ", webAppName = " + webContextName);
        }
        LOG.info("Adding webapp [" + webContextName + "] = path [" + webapp_dir + "] .....");
        StandardContext webappCtx = (StandardContext) server.addWebapp(webContextName, new File(webapp_dir).getAbsolutePath());
        String workDirPath = EmbeddedServerUtil.getConfig("ranger.tomcat.work.dir", "");
        if (!workDirPath.isEmpty() && new File(workDirPath).exists()) {
            webappCtx.setWorkDir(workDirPath);
        } else {
            if (LOG.isLoggable(Level.FINE)) {
                LOG.fine("Skipping to set tomcat server work directory, '" + workDirPath + "', as it is blank or directory does not exist.");
            }
        }
        webappCtx.init();
        LOG.info("Finished init of webapp [" + webContextName + "] = path [" + webapp_dir + "].");
    } catch (LifecycleException lce) {
        LOG.severe("Tomcat Server failed to start webapp:" + lce.toString());
        lce.printStackTrace();
    }
    if (servername.equalsIgnoreCase(ADMIN_SERVER_NAME)) {
        String keytab = EmbeddedServerUtil.getConfig(ADMIN_USER_KEYTAB);
        String principal = null;
        try {
            principal = SecureClientLogin.getPrincipal(EmbeddedServerUtil.getConfig(ADMIN_USER_PRINCIPAL), hostName);
        } catch (IOException ignored) {
            LOG.warning("Failed to get ranger.admin.kerberos.principal. Reason: " + ignored.toString());
        }
        String nameRules = EmbeddedServerUtil.getConfig(ADMIN_NAME_RULES);
        if (StringUtils.isBlank(nameRules)) {
            LOG.info("Name is empty. Setting Name Rule as 'DEFAULT'");
            nameRules = DEFAULT_NAME_RULE;
        }
        if (EmbeddedServerUtil.getConfig(AUTHENTICATION_TYPE) != null && EmbeddedServerUtil.getConfig(AUTHENTICATION_TYPE).trim().equalsIgnoreCase(AUTH_TYPE_KERBEROS) && SecureClientLogin.isKerberosCredentialExists(principal, keytab)) {
            try {
                LOG.info("Provided Kerberos Credential : Principal = " + principal + " and Keytab = " + keytab);
                Subject sub = SecureClientLogin.loginUserFromKeytab(principal, keytab, nameRules);
                Subject.doAs(sub, new PrivilegedAction<Void>() {

                    @Override
                    public Void run() {
                        LOG.info("Starting Server using kerberos credential");
                        startServer(server);
                        return null;
                    }
                });
            } catch (Exception e) {
                LOG.severe("Tomcat Server failed to start:" + e.toString());
                e.printStackTrace();
            }
        } else {
            startServer(server);
        }
    } else {
        startServer(server);
    }
}
Also used : Connector(org.apache.catalina.connector.Connector) Tomcat(org.apache.catalina.startup.Tomcat) LifecycleException(org.apache.catalina.LifecycleException) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) AccessLogValve(org.apache.catalina.valves.AccessLogValve) Subject(javax.security.auth.Subject) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ErrorReportValve(org.apache.catalina.valves.ErrorReportValve) StandardContext(org.apache.catalina.core.StandardContext) File(java.io.File)

Example 2 with ErrorReportValve

use of org.apache.catalina.valves.ErrorReportValve in project spring-boot by spring-projects.

the class TomcatWebServerFactoryCustomizerTests method errorReportValveIsConfiguredToNotReportStackTraces.

@Test
void errorReportValveIsConfiguredToNotReportStackTraces() {
    TomcatWebServer server = customizeAndGetServer();
    Valve[] valves = server.getTomcat().getHost().getPipeline().getValves();
    assertThat(valves).hasAtLeastOneElementOfType(ErrorReportValve.class);
    for (Valve valve : valves) {
        if (valve instanceof ErrorReportValve) {
            ErrorReportValve errorReportValve = (ErrorReportValve) valve;
            assertThat(errorReportValve.isShowReport()).isFalse();
            assertThat(errorReportValve.isShowServerInfo()).isFalse();
        }
    }
}
Also used : ErrorReportValve(org.apache.catalina.valves.ErrorReportValve) TomcatWebServer(org.springframework.boot.web.embedded.tomcat.TomcatWebServer) ErrorReportValve(org.apache.catalina.valves.ErrorReportValve) Valve(org.apache.catalina.Valve) AccessLogValve(org.apache.catalina.valves.AccessLogValve) RemoteIpValve(org.apache.catalina.valves.RemoteIpValve) Test(org.junit.jupiter.api.Test)

Example 3 with ErrorReportValve

use of org.apache.catalina.valves.ErrorReportValve in project tomcat by apache.

the class StandardHost method startInternal.

/**
 * Start this component and implement the requirements
 * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected synchronized void startInternal() throws LifecycleException {
    // Set error report valve
    String errorValve = getErrorReportValveClass();
    if ((errorValve != null) && (!errorValve.equals(""))) {
        try {
            boolean found = false;
            Valve[] valves = getPipeline().getValves();
            for (Valve valve : valves) {
                if (errorValve.equals(valve.getClass().getName())) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                Valve valve = ErrorReportValve.class.getName().equals(errorValve) ? new ErrorReportValve() : (Valve) Class.forName(errorValve).getConstructor().newInstance();
                getPipeline().addValve(valve);
            }
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            log.error(sm.getString("standardHost.invalidErrorReportValveClass", errorValve), t);
        }
    }
    super.startInternal();
}
Also used : ErrorReportValve(org.apache.catalina.valves.ErrorReportValve) ErrorReportValve(org.apache.catalina.valves.ErrorReportValve) Valve(org.apache.catalina.Valve)

Example 4 with ErrorReportValve

use of org.apache.catalina.valves.ErrorReportValve in project sonarqube by SonarSource.

the class TomcatErrorHandling method configure.

void configure(Tomcat tomcat) {
    ErrorReportValve valve = new ErrorReportValve();
    valve.setShowServerInfo(false);
    valve.setShowReport(false);
    tomcat.getHost().getPipeline().addValve(valve);
}
Also used : ErrorReportValve(org.apache.catalina.valves.ErrorReportValve)

Example 5 with ErrorReportValve

use of org.apache.catalina.valves.ErrorReportValve in project spring-boot by spring-projects.

the class TomcatWebServerFactoryCustomizer method customizeErrorReportValve.

private void customizeErrorReportValve(ErrorProperties error, ConfigurableTomcatWebServerFactory factory) {
    if (error.getIncludeStacktrace() == IncludeAttribute.NEVER) {
        factory.addContextCustomizers((context) -> {
            ErrorReportValve valve = new ErrorReportValve();
            valve.setShowServerInfo(false);
            valve.setShowReport(false);
            context.getParent().getPipeline().addValve(valve);
        });
    }
}
Also used : ErrorReportValve(org.apache.catalina.valves.ErrorReportValve)

Aggregations

ErrorReportValve (org.apache.catalina.valves.ErrorReportValve)5 Valve (org.apache.catalina.Valve)2 AccessLogValve (org.apache.catalina.valves.AccessLogValve)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 KeyManagementException (java.security.KeyManagementException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 UnrecoverableKeyException (java.security.UnrecoverableKeyException)1 CertificateException (java.security.cert.CertificateException)1 SSLContext (javax.net.ssl.SSLContext)1 Subject (javax.security.auth.Subject)1 LifecycleException (org.apache.catalina.LifecycleException)1 Connector (org.apache.catalina.connector.Connector)1 StandardContext (org.apache.catalina.core.StandardContext)1 Tomcat (org.apache.catalina.startup.Tomcat)1 RemoteIpValve (org.apache.catalina.valves.RemoteIpValve)1 Test (org.junit.jupiter.api.Test)1 TomcatWebServer (org.springframework.boot.web.embedded.tomcat.TomcatWebServer)1