Skip to content

Commit 574782b

Browse files
feat(core): default blocked login checker
Added check that none of the default blocked logins is already used.
1 parent 11e1f3f commit 574782b

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cz.metacentrum.perun.core.impl;
2+
3+
import cz.metacentrum.perun.core.api.BeansUtils;
4+
import cz.metacentrum.perun.core.api.ExtSourcesManager;
5+
import cz.metacentrum.perun.core.api.PerunClient;
6+
import cz.metacentrum.perun.core.api.PerunPrincipal;
7+
import cz.metacentrum.perun.core.api.PerunSession;
8+
import cz.metacentrum.perun.core.api.exceptions.InternalErrorException;
9+
import cz.metacentrum.perun.core.bl.PerunBl;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
import java.util.Set;
14+
15+
/**
16+
* This component checks that none of the default blocked logins is already used.
17+
*
18+
* @author Sarka Palkovicova
19+
*/
20+
public class DefaultBlockedLoginChecker {
21+
private static final Logger log = LoggerFactory.getLogger(DefaultBlockedLoginChecker.class);
22+
private final PerunSession sess;
23+
private PerunBl perunBl;
24+
25+
public DefaultBlockedLoginChecker(PerunBl perunBl) {
26+
String synchronizerPrincipal = "perunDefaultBlockedLoginChecker";
27+
this.sess = perunBl.getPerunSession(
28+
new PerunPrincipal(synchronizerPrincipal, ExtSourcesManager.EXTSOURCE_NAME_INTERNAL, ExtSourcesManager.EXTSOURCE_INTERNAL),
29+
new PerunClient());
30+
this.perunBl = perunBl;
31+
}
32+
33+
public PerunBl getPerunBl() {
34+
return perunBl;
35+
}
36+
37+
public void setPerunBl(PerunBl perunBl) {
38+
this.perunBl = perunBl;
39+
}
40+
41+
public void checkDefaultBlockedLogins() {
42+
log.debug("DefaultBlockedLoginChecker starts checking default blocked logins.");
43+
44+
Set<String> logins = BeansUtils.getCoreConfig().getBlockedLogins();
45+
for (String login : logins) {
46+
if (perunBl.getAttributesManagerBl().isLoginAlreadyUsed(sess, login, null)) {
47+
log.error("Login {} can not be blocked by default because it is already used.", login);
48+
throw new InternalErrorException("Login " + login + " can not be blocked by default because it is already used. Please edit the core config!");
49+
}
50+
}
51+
}
52+
53+
}

perun-core/src/main/resources/perun-core.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ http://www.springframework.org/schema/task http://www.springframework.org/schema
466466
<bean id="resourceAssignmentChecker" class="cz.metacentrum.perun.core.impl.ResourceAssignmentChecker" scope="singleton" depends-on="databaseManagerBl">
467467
<constructor-arg ref="perun" />
468468
</bean>
469+
470+
<bean id="defaultBlockedLoginChecker" class="cz.metacentrum.perun.core.impl.DefaultBlockedLoginChecker" scope="singleton" init-method="checkDefaultBlockedLogins" depends-on="attributesManagerBl">
471+
<constructor-arg ref="perun" />
472+
</bean>
473+
469474
<!-- merged from perun-core-transaction-manager.xml -->
470475
<bean id="perunTransactionManager" class="cz.metacentrum.perun.core.impl.PerunTransactionManager">
471476
<property name="dataSource" ref="dataSource"/>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package cz.metacentrum.perun.core.impl;
2+
3+
import cz.metacentrum.perun.core.AbstractPerunIntegrationTest;
4+
import cz.metacentrum.perun.core.api.Attribute;
5+
import cz.metacentrum.perun.core.api.BeansUtils;
6+
import cz.metacentrum.perun.core.api.CoreConfig;
7+
import cz.metacentrum.perun.core.api.User;
8+
import cz.metacentrum.perun.core.api.exceptions.AttributeDefinitionExistsException;
9+
import cz.metacentrum.perun.core.api.exceptions.InternalErrorException;
10+
import cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException;
11+
import cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException;
12+
import cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException;
13+
import org.junit.Before;
14+
import org.junit.Test;
15+
16+
import java.util.ArrayList;
17+
import java.util.Collections;
18+
import java.util.HashSet;
19+
import java.util.List;
20+
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertThrows;
23+
24+
public class DefaultBlockedLoginCheckerTest extends AbstractPerunIntegrationTest {
25+
private final static String CLASS_NAME = "DefaultBlockedLoginChecker.";
26+
private final static String LOGIN = "testLogin";
27+
28+
private User user;
29+
private Attribute attr;
30+
31+
DefaultBlockedLoginChecker defaultBlockedLoginChecker;
32+
33+
@Before
34+
public void setUp() throws Exception {
35+
defaultBlockedLoginChecker = new DefaultBlockedLoginChecker(perun);
36+
37+
setUser();
38+
setLoginNamespaceAttribute();
39+
}
40+
41+
@Test
42+
public void defaultBlockedLoginAlreadyUsed() {
43+
System.out.println(CLASS_NAME + "defaultBlockedLoginIsAlreadyUsed");
44+
45+
List<String> originalAdmins = BeansUtils.getCoreConfig().getAdmins();
46+
try {
47+
// configure admins to contain one login - testLogin
48+
BeansUtils.getCoreConfig().setAdmins(Collections.singletonList(LOGIN));
49+
assertThrows(InternalErrorException.class, () -> defaultBlockedLoginChecker.checkDefaultBlockedLogins());
50+
} finally {
51+
// set admins back to the original admins
52+
BeansUtils.getCoreConfig().setAdmins(originalAdmins);
53+
}
54+
}
55+
56+
@Test
57+
public void defaultBlockedLoginAreNotUsed() {
58+
System.out.println(CLASS_NAME + "defaultBlockedLoginAreNotUsed");
59+
60+
CoreConfig originalConfig = BeansUtils.getCoreConfig();
61+
try {
62+
// set new core config
63+
CoreConfig cfNew = new CoreConfig();
64+
65+
cfNew.setAdmins(new ArrayList<>());
66+
cfNew.setEnginePrincipals(new ArrayList<>());
67+
cfNew.setNotificationPrincipals(new ArrayList<>());
68+
cfNew.setDontLookupUsers(new HashSet<>());
69+
cfNew.setRegistrarPrincipals(new ArrayList<>());
70+
cfNew.setRpcPrincipal(null);
71+
cfNew.setInstanceId("test");
72+
73+
BeansUtils.setConfig(cfNew);
74+
75+
defaultBlockedLoginChecker.checkDefaultBlockedLogins();
76+
} finally {
77+
// set core config back to original
78+
BeansUtils.setConfig(originalConfig);
79+
}
80+
}
81+
82+
private void setUser() {
83+
user = new User();
84+
user.setFirstName("Joe");
85+
user.setLastName("Doe");
86+
user = perun.getUsersManagerBl().createUser(sess, user);
87+
assertNotNull(user);
88+
}
89+
90+
private void setLoginNamespaceAttribute() throws AttributeDefinitionExistsException, WrongAttributeAssignmentException, WrongReferenceAttributeValueException, WrongAttributeValueException {
91+
attr = new Attribute();
92+
attr.setNamespace("urn:perun:user:attribute-def:def");
93+
attr.setFriendlyName("login-namespace:META-login");
94+
attr.setType(String.class.getName());
95+
attr.setValue(LOGIN);
96+
97+
assertNotNull("unable to create login namespace attribute", perun.getAttributesManagerBl().createAttribute(sess, attr));
98+
99+
perun.getAttributesManagerBl().setAttribute(sess, user, attr);
100+
}
101+
}

0 commit comments

Comments
 (0)