-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
… and present in the DB Signed-off-by: dseurotech <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Eurotech - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.kapua.service.authorization.domain.shiro; | ||
|
||
import org.eclipse.kapua.KapuaException; | ||
import org.eclipse.kapua.commons.populators.DataPopulator; | ||
import org.eclipse.kapua.commons.security.KapuaSecurityUtils; | ||
import org.eclipse.kapua.model.domain.Actions; | ||
import org.eclipse.kapua.model.domain.Domain; | ||
import org.eclipse.kapua.service.authorization.access.AccessPermissionRepository; | ||
import org.eclipse.kapua.service.authorization.domain.DomainRepository; | ||
import org.eclipse.kapua.service.authorization.role.RolePermissionRepository; | ||
import org.eclipse.kapua.storage.TxContext; | ||
import org.eclipse.kapua.storage.TxManager; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.inject.Inject; | ||
import java.util.ArrayList; | ||
import java.util.EnumSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class DomainsAligner implements DataPopulator { | ||
private final TxManager txManager; | ||
private final DomainRepository domainRepository; | ||
private final AccessPermissionRepository accessPermissionRepository; | ||
private final RolePermissionRepository rolePermissionRepository; | ||
private final Set<Domain> knownDomains; | ||
private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
Check warning on line 42 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L42
|
||
|
||
@Inject | ||
public DomainsAligner(TxManager txManager, | ||
DomainRepository domainRepository, | ||
AccessPermissionRepository accessPermissionRepository, RolePermissionRepository rolePermissionRepository, | ||
Set<Domain> knownDomains) { | ||
this.txManager = txManager; | ||
this.domainRepository = domainRepository; | ||
this.accessPermissionRepository = accessPermissionRepository; | ||
this.rolePermissionRepository = rolePermissionRepository; | ||
this.knownDomains = knownDomains; | ||
} | ||
Check warning on line 54 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L48-L54
|
||
|
||
@Override | ||
public void populate() { | ||
logger.info("Domain alignment commencing. Found {} domain declarations in wiring", knownDomains.size()); | ||
final Map<String, Domain> knownDomainsByName = knownDomains | ||
.stream() | ||
.collect(Collectors.toMap(d -> d.getName(), d -> d)); | ||
final List<String> declaredDomainsNotInDb = new ArrayList<>(knownDomainsByName.keySet()); | ||
Check warning on line 62 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L58-L62
|
||
try { | ||
KapuaSecurityUtils.doPrivileged(() -> { | ||
txManager.execute(tx -> { | ||
final List<org.eclipse.kapua.service.authorization.domain.Domain> dbDomainEntries = domainRepository.query(tx, new DomainQueryImpl()).getItems(); | ||
logger.info("Found {} domain declarations in database", dbDomainEntries.size()); | ||
Check warning on line 67 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L64-L67
|
||
|
||
for (final org.eclipse.kapua.service.authorization.domain.Domain dbDomainEntry : dbDomainEntries) { | ||
if (!knownDomainsByName.containsKey(dbDomainEntry.getName())) { | ||
//Leave it be. As we share the database with other components, it might have been created by such components and be hidden from us | ||
logger.warn("Domain '{}' is only present in the database but has no current declaration! Details: {}", dbDomainEntry.getName(), dbDomainEntry.getDomain()); | ||
continue; | ||
Check warning on line 73 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L72-L73
|
||
} | ||
//Good news, it's both declared in wiring and present in the db! | ||
declaredDomainsNotInDb.remove(dbDomainEntry.getName()); | ||
Check warning on line 76 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L76
|
||
//Trigger fetch of Actions collection from db, otherwise the toString would not show the details | ||
dbDomainEntry.getActions(); | ||
final Domain wiredDomain = knownDomainsByName.get(dbDomainEntry.getName()); | ||
Check warning on line 79 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L78-L79
|
||
if (dbDomainEntry.getDomain().equals(wiredDomain)) { | ||
//We are happy! | ||
logger.debug("Domain '{}' is ok: {}", dbDomainEntry.getName(), dbDomainEntry.getDomain()); | ||
continue; | ||
Check warning on line 83 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L82-L83
|
||
} | ||
//Align them! | ||
alignDomains(tx, dbDomainEntry, wiredDomain); | ||
} | ||
Check warning on line 87 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L86-L87
|
||
// createMissingDomains(tx, declaredDomainsNotInDb, knownDomainsByName); | ||
logger.info("Domain alignment complete!"); | ||
return null; | ||
Check warning on line 90 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L89-L90
|
||
}); | ||
}); | ||
} catch (KapuaException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
Check warning on line 96 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L92-L96
|
||
|
||
private void createMissingDomains(TxContext tx, List<String> declaredDomainsNotInDb, Map<String, Domain> knownDomainsByName) throws KapuaException { | ||
if (declaredDomainsNotInDb.size() > 0) { | ||
logger.info("Found {} declared domains that have no counterpart in the database!", declaredDomainsNotInDb.size()); | ||
Check warning on line 100 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L100
|
||
//Create wired domains not present in the db | ||
for (final String declaredOnlyName : declaredDomainsNotInDb) { | ||
final Domain expected = knownDomainsByName.get(declaredOnlyName); | ||
createDomainInDb(tx, expected); | ||
} | ||
Check warning on line 105 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L103-L105
|
||
} | ||
} | ||
Check warning on line 107 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L107
|
||
|
||
private void createDomainInDb(TxContext tx, Domain expected) throws KapuaException { | ||
logger.info("To be added: {}", expected); | ||
final org.eclipse.kapua.service.authorization.domain.Domain newEntity = new DomainImpl(); | ||
newEntity.setName(expected.getName()); | ||
newEntity.setActions(expected.getActions()); | ||
newEntity.setGroupable(expected.getGroupable()); | ||
newEntity.setServiceName(expected.getServiceName()); | ||
domainRepository.create(tx, newEntity); | ||
} | ||
Check warning on line 117 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L110-L117
|
||
|
||
private void alignDomains(TxContext tx, org.eclipse.kapua.service.authorization.domain.Domain dbDomainEntry, Domain wiredDomain) throws KapuaException { | ||
logger.error("Domain mismatch for name '{}'! Details:" + | ||
Check warning on line 120 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L120
|
||
"\n\tDb entry: '{}', " + | ||
"\n\texpected: '{}'", | ||
dbDomainEntry.getName(), | ||
dbDomainEntry.getDomain(), | ||
Check warning on line 124 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L123-L124
|
||
wiredDomain); | ||
|
||
final EnumSet<Actions> actionsInExcessOnTheDb = EnumSet.copyOf(dbDomainEntry.getActions()); | ||
actionsInExcessOnTheDb.removeAll(wiredDomain.getActions()); | ||
removeActionsInExcess(tx, dbDomainEntry.getName(), actionsInExcessOnTheDb); | ||
Check warning on line 129 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L127-L129
|
||
//Remove the action from the domain. Thank you JPA for autoupdating the entity on transaction close | ||
dbDomainEntry.getActions().removeAll(actionsInExcessOnTheDb); | ||
Check warning on line 131 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L131
|
||
|
||
final EnumSet<Actions> actionsMissingInTheDb = EnumSet.copyOf(wiredDomain.getActions()); | ||
actionsMissingInTheDb.removeAll(dbDomainEntry.getActions()); | ||
Check warning on line 134 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L133-L134
|
||
//Add the actions to the domain. Thank you JPA for autoupdating the entity on transaction close | ||
if (actionsMissingInTheDb.isEmpty()) { | ||
return; | ||
Check warning on line 137 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L137
|
||
} | ||
dbDomainEntry.getActions().addAll(actionsMissingInTheDb); | ||
} | ||
Check warning on line 140 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L139-L140
|
||
|
||
private void removeActionsInExcess(TxContext tx, String domainName, EnumSet<Actions> actionsInExcessOnTheDb) throws KapuaException { | ||
for (final Actions actionToDelete : actionsInExcessOnTheDb) { | ||
logger.info("Removing action '{}' from domain '{}'", actionToDelete, domainName); | ||
accessPermissionRepository.deleteAllByDomainAndAction(tx, domainName, actionToDelete); | ||
rolePermissionRepository.deleteAllByDomainAndAction(tx, domainName, actionToDelete); | ||
} | ||
} | ||
Check warning on line 148 in service/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java Codecov / codecov/patchservice/security/shiro/src/main/java/org/eclipse/kapua/service/authorization/domain/shiro/DomainsAligner.java#L144-L148
|
||
} |