-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #735 from Netcentric/feature/732-validation-to-ens…
…ure-groups-with-externalId-are-not-used-in-isMemberOf Validation to ensure groups with externalId set are not used in isMemberOf
- Loading branch information
Showing
10 changed files
with
258 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...n/java/biz/netcentric/cq/tools/actool/validators/ExternalGroupsInIsMemberOfValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* (C) Copyright 2024 Netcentric AG. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package biz.netcentric.cq.tools.actool.validators; | ||
|
||
import java.util.Arrays; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import biz.netcentric.cq.tools.actool.configmodel.AcConfiguration; | ||
import biz.netcentric.cq.tools.actool.configmodel.AuthorizableConfigBean; | ||
import biz.netcentric.cq.tools.actool.configmodel.AuthorizablesConfig; | ||
import biz.netcentric.cq.tools.actool.configmodel.GlobalConfiguration; | ||
import biz.netcentric.cq.tools.actool.history.impl.PersistableInstallationLogger; | ||
import biz.netcentric.cq.tools.actool.validators.exceptions.InvalidExternalGroupUsageValidationException; | ||
|
||
@Component(service = ExternalGroupsInIsMemberOfValidator.class) | ||
public class ExternalGroupsInIsMemberOfValidator { | ||
private static final Logger LOG = LoggerFactory.getLogger(ExternalGroupsInIsMemberOfValidator.class); | ||
|
||
public void validateIsMemberOfConfig(AcConfiguration acConfiguration, PersistableInstallationLogger installLog, | ||
GlobalConfiguration globalConfiguration) throws InvalidExternalGroupUsageValidationException { | ||
List<String> externalGroupsValidationResults = checkIsMemberOfConfigsOfAllAuthorizables(acConfiguration); | ||
if (!externalGroupsValidationResults.isEmpty()) { | ||
|
||
externalGroupsValidationResults.stream().forEach(m -> installLog.addWarning(LOG, m)); | ||
|
||
String validationMsg = "Found " + externalGroupsValidationResults.size() + " authorizable(s) that use external groups in isMemberOf. "; | ||
|
||
if (Boolean.TRUE.equals(globalConfiguration.getAllowExternalGroupsInIsMemberOf())) { | ||
installLog.addWarning(LOG, validationMsg); | ||
installLog.addWarning(LOG, "Found global config 'allowExternalGroupsInIsMemberOf: true': PLEASE REFACTOR your groups structure to not use external groups in isMemberOf."); | ||
} else { | ||
installLog.addError(LOG, validationMsg + " If absolutely needed, use 'allowExternalGroupsInIsMemberOf: true' in global configuration, but prefer to refactor your groups structure to not use isMemberOf together with external groups.", null); | ||
throw new InvalidExternalGroupUsageValidationException(validationMsg); | ||
} | ||
} | ||
} | ||
|
||
private List<String> checkIsMemberOfConfigsOfAllAuthorizables(AcConfiguration acConfiguration) { | ||
|
||
List<String> validationErrors = new LinkedList<>(); | ||
|
||
AuthorizablesConfig authorizablesConfig = acConfiguration.getAuthorizablesConfig(); | ||
for (AuthorizableConfigBean authorizableConfigBean : authorizablesConfig) { | ||
if(authorizableConfigBean.getIsMemberOf() == null) { | ||
continue; | ||
} | ||
|
||
List<String> groupIdsWithExternalIdSet = Arrays.stream(authorizableConfigBean.getIsMemberOf()) | ||
.map(aId -> acConfiguration.getAuthorizablesConfig().getAuthorizableConfig(aId)) | ||
.filter(Objects::nonNull) | ||
.filter(authBean -> StringUtils.isNotBlank(authBean.getExternalId())) | ||
.map(AuthorizableConfigBean::getAuthorizableId) | ||
.collect(Collectors.toList()); | ||
|
||
if (!groupIdsWithExternalIdSet.isEmpty()) { | ||
validationErrors.add("The authorizable " + authorizableConfigBean.getAuthorizableId() | ||
+ " cannot use external group(s) in isMemberOf list: " + StringUtils.join(groupIdsWithExternalIdSet, ",")); | ||
} | ||
} | ||
|
||
return validationErrors; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...c/cq/tools/actool/validators/exceptions/InvalidExternalGroupUsageValidationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* (C) Copyright 2015 Netcentric AG. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package biz.netcentric.cq.tools.actool.validators.exceptions; | ||
|
||
public class InvalidExternalGroupUsageValidationException extends AcConfigBeanValidationException { | ||
public InvalidExternalGroupUsageValidationException(String message) { | ||
super(message); | ||
} | ||
|
||
public InvalidExternalGroupUsageValidationException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...va/biz/netcentric/cq/tools/actool/validators/ExternalGroupsInIsMemberOfValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package biz.netcentric.cq.tools.actool.validators; | ||
|
||
import static biz.netcentric.cq.tools.actool.configreader.YamlConfigurationMergerTest.getAcConfigurationForFile; | ||
import static biz.netcentric.cq.tools.actool.configreader.YamlConfigurationMergerTest.getConfigurationMerger; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import javax.jcr.Session; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
|
||
import biz.netcentric.cq.tools.actool.configmodel.AcConfiguration; | ||
import biz.netcentric.cq.tools.actool.validators.exceptions.InvalidExternalGroupUsageValidationException; | ||
|
||
class ExternalGroupsInIsMemberOfValidatorTest { | ||
|
||
@Mock | ||
Session session; | ||
|
||
@Test | ||
public void testExternalIdSetCorrectlyOnRoleOnly() throws Exception { | ||
AcConfiguration acConfigurationForFile = getAcConfigurationForFile(getConfigurationMerger(), session, | ||
"externalIds/test-externalId-set-correctly-on-role-only.yaml"); | ||
|
||
assertEquals(3, acConfigurationForFile.getAuthorizablesConfig().size()); | ||
} | ||
|
||
@Test | ||
public void testExternalIdSetOnFragmentOverruledByConfigToBeAlllowed() throws Exception { | ||
AcConfiguration acConfigurationForFile = getAcConfigurationForFile(getConfigurationMerger(), session, | ||
"externalIds/test-externalId-set-on-fragment-overruled-by-config-to-be-allowed.yaml"); | ||
|
||
assertEquals(3, acConfigurationForFile.getAuthorizablesConfig().size()); | ||
} | ||
|
||
@Test | ||
public void testExternalIdSetOnFragmentInvalid() { | ||
assertThrows(InvalidExternalGroupUsageValidationException.class, | ||
() -> getAcConfigurationForFile(getConfigurationMerger(), session, | ||
"externalIds/test-externalId-set-on-fragment-invalid.yaml")); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...ool-bundle/src/test/resources/externalIds/test-externalId-set-correctly-on-role-only.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# | ||
# (C) Copyright 2024 Netcentric AG. | ||
# | ||
# All rights reserved. This program and the accompanying materials | ||
# are made available under the terms of the Eclipse Public License v1.0 | ||
# which accompanies this distribution, and is available at | ||
# http://www.eclipse.org/legal/epl-v10.html | ||
# | ||
|
||
- global_config: | ||
|
||
# default - no extra config set | ||
# allowExternalGroupsInIsMemberOf: true | ||
|
||
- group_config: | ||
|
||
- fragment-1: | ||
- name: "Fragment 1" | ||
|
||
- fragment-2: | ||
- name: "Fragment 2" | ||
|
||
- role-1: | ||
- name: "Role 1" | ||
isMemberOf: fragment-1, fragment-2 | ||
externalId: role-1;ims | ||
|
28 changes: 28 additions & 0 deletions
28
...oltool-bundle/src/test/resources/externalIds/test-externalId-set-on-fragment-invalid.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# | ||
# (C) Copyright 2024 Netcentric AG. | ||
# | ||
# All rights reserved. This program and the accompanying materials | ||
# are made available under the terms of the Eclipse Public License v1.0 | ||
# which accompanies this distribution, and is available at | ||
# http://www.eclipse.org/legal/epl-v10.html | ||
# | ||
|
||
- global_config: | ||
|
||
# default - no extra config set | ||
# allowExternalGroupsInIsMemberOf: true | ||
|
||
- group_config: | ||
|
||
- fragment-1: | ||
- name: "Fragment 1" | ||
|
||
- fragment-2: | ||
- name: "Fragment 2" | ||
externalId: fragment-2;ims | ||
|
||
- role-1: | ||
- name: "Role 1" | ||
isMemberOf: fragment-1, fragment-2 | ||
|
||
|
27 changes: 27 additions & 0 deletions
27
...ources/externalIds/test-externalId-set-on-fragment-overruled-by-config-to-be-allowed.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# | ||
# (C) Copyright 2024 Netcentric AG. | ||
# | ||
# All rights reserved. This program and the accompanying materials | ||
# are made available under the terms of the Eclipse Public License v1.0 | ||
# which accompanies this distribution, and is available at | ||
# http://www.eclipse.org/legal/epl-v10.html | ||
# | ||
|
||
- global_config: | ||
|
||
allowExternalGroupsInIsMemberOf: true | ||
|
||
- group_config: | ||
|
||
- fragment-1: | ||
- name: "Fragment 1" | ||
|
||
- fragment-2: | ||
- name: "Fragment 2" | ||
externalId: fragment-2;ims | ||
|
||
- role-1: | ||
- name: "Role 1" | ||
isMemberOf: fragment-1, fragment-2 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters