SlingJavaScriptHandlerHealthCheck.java 4.9 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.PropertyUnbounded
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.apache.sling.hc.api.HealthCheck
 *  org.apache.sling.hc.api.Result
 *  org.apache.sling.hc.api.ResultLog
 *  org.apache.sling.hc.util.FormattingResultLog
 *  org.osgi.framework.InvalidSyntaxException
 *  org.osgi.service.cm.Configuration
 *  org.osgi.service.cm.ConfigurationAdmin
 */
package com.adobe.granite.bundles.hc.impl;

import java.io.IOException;
import java.util.Dictionary;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.hc.api.HealthCheck;
import org.apache.sling.hc.api.Result;
import org.apache.sling.hc.api.ResultLog;
import org.apache.sling.hc.util.FormattingResultLog;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;

@Component(metatype=1, label="Apache Sling Java Script Handler Health Check", description="Checks if the Sling Java Script Handler configuration follows the security guidelines.")
@Properties(value={@Property(name="hc.name", value={"Sling Java Script Handler"}, label="Name", description="Name of the health check."), @Property(name="hc.tags", unbounded=PropertyUnbounded.ARRAY, value={"sling", "security", "production"}, label="Tags", description="Tags for the health check."), @Property(name="hc.mbean.name", value={"slingJavaScriptHandler"}, label="MBean Name", description="Name of the JMX mbean to register for this check.")})
@Service(value={HealthCheck.class})
public class SlingJavaScriptHandlerHealthCheck
implements HealthCheck {
    @Reference
    private ConfigurationAdmin configurationAdmin;
    private static final String SLING_JAVA_HANDLER_PID = "org.apache.sling.scripting.java.impl.JavaScriptEngineFactory";
    private static final String DEBUG_INFO_PROP = "java.classdebuginfo";

    public Result execute() {
        FormattingResultLog resultLog = new FormattingResultLog();
        boolean success = true;
        String filter = "(service.pid=org.apache.sling.scripting.java.impl.JavaScriptEngineFactory)";
        try {
            Configuration[] slingGetServletCfgs = this.configurationAdmin.listConfigurations(filter);
            if (slingGetServletCfgs == null) {
                resultLog.warn("The Sling Java Script Handler uses the default configuration. The system might be exposed if used as a publish environment.", new Object[0]);
                success = false;
            } else {
                for (Configuration config : slingGetServletCfgs) {
                    Dictionary properties = config.getProperties();
                    boolean debugInfoEnabled = PropertiesUtil.toBoolean(properties.get("java.classdebuginfo"), (boolean)false);
                    if (!debugInfoEnabled) continue;
                    resultLog.warn("The Sling Java Script Handler generates debug information. The system might be exposed if used as a publish environment.", new Object[0]);
                    success = false;
                }
            }
            if (success) {
                resultLog.debug("The Sling Java Script Handler does not generate debug information.", new Object[0]);
            } else {
                resultLog.debug("[You can stop the Sling Java Script Handler from generating debug information by unchecking 'Generate Debug Info' in its configuration.]({})", new Object[]{"/system/console/configMgr/org.apache.sling.scripting.java.impl.JavaScriptEngineFactory"});
                resultLog.debug("[Check OSGI Settings in the security guidelines.](https://www.adobe.com/go/aem6_2_docs_security_osgi_en)", new Object[0]);
            }
        }
        catch (IOException e) {
            resultLog.warn("Could not access Sling Java Script Handler configuration.", new Object[0]);
        }
        catch (InvalidSyntaxException e) {
            resultLog.warn("Could not access Sling Java Script Handler configuration.", new Object[0]);
        }
        return new Result((ResultLog)resultLog);
    }

    protected void bindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
        this.configurationAdmin = configurationAdmin;
    }

    protected void unbindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
        if (this.configurationAdmin == configurationAdmin) {
            this.configurationAdmin = null;
        }
    }
}