ConfigPostProcessor.java 3.31 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.crypto.CryptoException
 *  com.adobe.granite.crypto.CryptoSupport
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.servlets.post.Modification
 *  org.apache.sling.servlets.post.ModificationType
 *  org.apache.sling.servlets.post.SlingPostProcessor
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.replication.impl;

import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.replication.impl.AgentConfigImpl;
import java.util.List;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.ModificationType;
import org.apache.sling.servlets.post.SlingPostProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service(value={SlingPostProcessor.class})
public class ConfigPostProcessor
implements SlingPostProcessor {
    private static final Logger log = LoggerFactory.getLogger(ConfigPostProcessor.class);
    @Reference
    private CryptoSupport crypto;

    public void process(SlingHttpServletRequest request, List<Modification> changes) throws Exception {
        Session session = null;
        for (Modification mod : changes) {
            switch (mod.getType()) {
                case MODIFY: 
                case CREATE: {
                    String path = mod.getSource();
                    if (!path.startsWith("/etc/replication/") || !this.isPasswordProp(path)) break;
                    if (session == null) {
                        session = (Session)request.getResourceResolver().adaptTo(Session.class);
                    }
                    this.fix(session.getProperty(path));
                }
            }
        }
    }

    private void fix(Property p) throws RepositoryException {
        String pwd = p.getString();
        if (pwd.length() > 0 && !this.crypto.isProtected(pwd)) {
            String dec = AgentConfigImpl.legacyDecrypt(pwd);
            if (dec != null) {
                pwd = dec;
            }
            try {
                p.setValue(this.crypto.protect(pwd));
            }
            catch (CryptoException e) {
                log.error("Error during protecting password: {}", (Object)e.toString());
            }
        }
    }

    private boolean isPasswordProp(String path) {
        return path.endsWith("transportPassword") || path.endsWith("proxyPassword");
    }

    protected void bindCrypto(CryptoSupport cryptoSupport) {
        this.crypto = cryptoSupport;
    }

    protected void unbindCrypto(CryptoSupport cryptoSupport) {
        if (this.crypto == cryptoSupport) {
            this.crypto = null;
        }
    }

}