Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/remove-animal-sniffer
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjgonzalez authored Nov 22, 2023
2 parents 8baca72 + 2615e1a commit e4441dc
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com)

## Added

- #3162 - Renovator MCP: ensure old source path is removed
- #3205 - HttpClientFactory: Expose a method to customize the underlying HttpClient
- #3209 - WARN org.apache.sling.models.impl.ModelAdapterFactory - Cannot provide default for java.util.List<java.lang.String>
- #3197 - Encrypt user credentials in ACS Content Sync
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
import com.adobe.acs.commons.contentsync.ConfigurationUtils;
import com.adobe.granite.crypto.CryptoSupport;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.SlingPostProcessor;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import javax.jcr.Property;
import javax.jcr.Session;
import java.util.List;

/**
Expand All @@ -42,21 +43,22 @@ public class EncryptPasswordPostProcessor implements SlingPostProcessor {

@Override
public void process(SlingHttpServletRequest slingRequest, List<Modification> changes) throws Exception {
Session session = slingRequest.getResourceResolver().adaptTo(Session.class);
for (Modification mod : changes) {
String path = mod.getSource();
if (!path.startsWith(ConfigurationUtils.HOSTS_PATH)) {
continue;
}
switch (mod.getType()) {
case MODIFY:
case CREATE:
String path = mod.getSource();
if (path.startsWith(ConfigurationUtils.HOSTS_PATH)) {
ModifiableValueMap vm = slingRequest.getResource().adaptTo(ModifiableValueMap.class);

String password = vm.get(PASSWORD_PROPERTY, String.class);
if (path.endsWith("/" + PASSWORD_PROPERTY) && session.propertyExists(path)) {
Property property = session.getProperty(path);
String password = property.getString();
// encrypt the password property if it is not already protected
if(password != null && !crypto.isProtected(password)) {
if (!crypto.isProtected(password)) {
String encrypted = crypto.protect(password);
vm.put(PASSWORD_PROPERTY, encrypted);

slingRequest.getResourceResolver().commit();
property.setValue(encrypted);
}
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ public void findReferences(ResourceResolver rr, String referenceSearchRoot, int
.filter(p -> isActivated(rr, p.getPagePath()))
.map(ReferenceSearch.Info::getPagePath)
.collect(Collectors.toCollection(() -> publishedReferences));
if(isActivated(rr, sourcePath)){
publishedReferences.add(destinationPath);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void moveOrClonePage(ResourceResolver rr, PageManager manager, String co
manager.move(source,
getDestinationPath(),
getPreviousSibling(),
true,
false,
true,
listToStringArray(getAllReferences()),
listToStringArray(getPublishedReferences()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.sling.api.resource.Resource;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.ModificationType;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -40,7 +41,7 @@

public class TestEncryptPasswordPostProcessor {
@Rule
public AemContext context = new AemContext();
public AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);

private CryptoSupport crypto;

Expand All @@ -62,22 +63,22 @@ public void testProtectPassword() throws Exception {
"host", "http://localhost:4502", "username", "admin", "password", "admin");
context.request().setResource(resource);
List<Modification> changes = new ArrayList<>();
changes.add(new Modification(ModificationType.CREATE, resource.getPath(), resource.getPath()));
changes.add(new Modification(ModificationType.CREATE, resource.getPath() + "/password", resource.getPath()));
postProcessor.process(context.request(), changes);

ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
assertEquals("admin-encrypted", resource.getValueMap().get("password"));
verify(crypto, times(1)).isProtected(captor.capture());
verify(crypto, times(1)).protect(captor.capture());
}
}

@Test
public void testSkipProtectedPassword() throws Exception {
Resource resource = context.create().resource(HOSTS_PATH + "/host1",
"host", "http://localhost:4502", "username", "admin", "password", "admin-encrypted");
context.request().setResource(resource);
List<Modification> changes = new ArrayList<>();
changes.add(new Modification(ModificationType.MODIFY, resource.getPath(), resource.getPath()));
changes.add(new Modification(ModificationType.MODIFY, resource.getPath() + "/password", resource.getPath()));

postProcessor.process(context.request(), changes);

Expand All @@ -89,7 +90,7 @@ public void testSkipProtectedPassword() throws Exception {

@Test
public void testIgnoreNonContentSyncPaths() throws Exception {
Resource resource = context.create().resource( "/var/unknown/host1",
Resource resource = context.create().resource("/var/unknown/host1",
"host", "http://localhost:4502", "username", "admin", "password", "admin");
context.request().setResource(resource);
List<Modification> changes = new ArrayList<>();
Expand All @@ -105,7 +106,7 @@ public void testIgnoreNonContentSyncPaths() throws Exception {

@Test
public void testIgnoreNullPassword() throws Exception {
Resource resource = context.create().resource( HOSTS_PATH + "/host1",
Resource resource = context.create().resource(HOSTS_PATH + "/host1",
"host", "http://localhost:4502", "username", "admin");
context.request().setResource(resource);
List<Modification> changes = new ArrayList<>();
Expand Down

0 comments on commit e4441dc

Please sign in to comment.