CrxdeSupportBundleHealthCheck.java 4.28 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Deactivate
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.PropertyUnbounded
 *  org.apache.felix.scr.annotations.Service
 *  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.Bundle
 *  org.osgi.framework.BundleContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.bundles.hc.impl;

import java.util.Dictionary;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
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.Service;
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.Bundle;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1, label="Adobe Granite CRXDE Support Bundle Health Check", description="This health check checks if the crxde-support bundle is active.")
@Properties(value={@Property(name="hc.name", value={"CRXDE Support"}, propertyPrivate=1), @Property(name="hc.tags", unbounded=PropertyUnbounded.ARRAY, value={"bundles", "security", "production"}, label="Tags", description="Tags for this check to be used by composite health checks."), @Property(name="hc.mbean.name", value={"crxdeSupportBundle"}, propertyPrivate=1)})
@Service(value={HealthCheck.class})
public class CrxdeSupportBundleHealthCheck
implements HealthCheck {
    Logger log;
    private BundleContext bundleContext;
    private static final String[] CRXDE_PACKAGE_NAMES = new String[]{"com.day.crx.crxde-support", "com.adobe.granite.crx-explorer", "com.adobe.granite.crxde-lite"};

    public CrxdeSupportBundleHealthCheck() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    @Activate
    protected void activate(BundleContext bundleContext) {
        this.bundleContext = bundleContext;
    }

    @Deactivate
    protected void deactivate() {
        this.bundleContext = null;
    }

    public Result execute() {
        Bundle[] bundles;
        boolean found = false;
        FormattingResultLog resultLog = new FormattingResultLog();
        for (Bundle bundle : bundles = this.bundleContext.getBundles()) {
            String symbolicName;
            Dictionary headers = bundle.getHeaders();
            if (headers == null || (symbolicName = (String)headers.get("Bundle-SymbolicName")) == null || !this.isCrxDeBundle(symbolicName)) continue;
            if (bundle.getState() == 32 || bundle.getState() == 8 && CrxdeSupportBundleHealthCheck.isLazyActivatian(bundle)) {
                found = true;
                resultLog.warn("The {} bundle is active.", new Object[]{symbolicName});
                continue;
            }
            resultLog.debug("The {} bundle is not active.", new Object[]{symbolicName});
        }
        resultLog.debug("[The CRX Development Bundles should be disabled on production systems.]( )", new Object[0]);
        if (found) {
            resultLog.warn("[You can disable the CRX Development Bundles in the administration console.]({})", new Object[]{"/system/console/bundles"});
            resultLog.warn("[See Disable CRXDE Support in the security guidelines.](https://www.adobe.com/go/aem6_2_docs_security_crxde_en)", new Object[0]);
        }
        return new Result((ResultLog)resultLog);
    }

    private static boolean isLazyActivatian(Bundle b) {
        return "lazy".equals(b.getHeaders().get("Bundle-ActivationPolicy"));
    }

    private boolean isCrxDeBundle(String bundleName) {
        for (int i = 0; i < CRXDE_PACKAGE_NAMES.length; ++i) {
            if (!CRXDE_PACKAGE_NAMES[i].equals(bundleName)) continue;
            return true;
        }
        return false;
    }
}