use of org.motechproject.security.domain.MotechURLSecurityRule in project motech by motech.
the class SecurityRuleLoaderServiceImpl method updateSecurityConfig.
/**
* Updates existing Security config with new rules
* if there're already rules with the same origin
* as the first one since it means that it was
* already loaded. Also update won't happen if
* {@link org.motechproject.security.domain.MotechSecurityConfiguration}
* cannot be set
*
* @param newRules list that contains new rules
*/
private void updateSecurityConfig(List<MotechURLSecurityRule> newRules) {
LOGGER.debug("Updating security config");
String origin = newRules.get(0).getOrigin();
String version = newRules.get(0).getVersion();
LOGGER.debug("Rules origin: {}, version: {}", origin, version);
List<MotechURLSecurityRule> moduleRules = allSecurityRules.getRulesByOriginAndVersion(origin, version);
if (moduleRules.size() > 0) {
// Don't update security if rules from this origin and the same version have already been loaded
LOGGER.debug("Rules from the origin {} [version: {}] have already been loaded", origin, version);
return;
}
LOGGER.debug("Updating config with rules from origin: {}", origin);
MotechSecurityConfiguration securityConfig = allSecurityRules.getMotechSecurityConfiguration();
if (securityConfig == null) {
LOGGER.error("No security config found in the database");
securityConfig = new MotechSecurityConfiguration();
}
List<MotechURLSecurityRule> oldRules = securityConfig.getSecurityRules();
LOGGER.debug("Found " + oldRules.size() + " old rules in the database");
newRules.addAll(rulesWithDifferentOrigin(oldRules, origin));
LOGGER.debug("Saving rules from origin {} in the database", origin);
securityConfig.setSecurityRules(newRules);
allSecurityRules.addOrUpdate(securityConfig);
LOGGER.debug("Initializing chain after security config update");
proxyManager.initializeProxyChain();
}
use of org.motechproject.security.domain.MotechURLSecurityRule in project motech by motech.
the class SecurityRuleBuilderTest method testShouldRequireMethodsSupported.
@Test
public void testShouldRequireMethodsSupported() {
configException.expect(SecurityConfigException.class);
configException.expectMessage(SecurityRuleBuilder.NO_METHODS_REQUIRED_EXCEPTION_MESSAGE);
MotechURLSecurityRule securityRule = new MotechURLSecurityRule();
securityRule.setPattern("pattern");
securityRule.setProtocol(HTTP);
securityRule.setSupportedSchemes(Arrays.asList(USERNAME_PASSWORD));
securityBuilder.buildSecurityChain(securityRule, GET);
}
use of org.motechproject.security.domain.MotechURLSecurityRule in project motech by motech.
the class MotechURLSecurityServiceImpl method toSecurityRuleDtoList.
private List<SecurityRuleDto> toSecurityRuleDtoList(List<MotechURLSecurityRule> rules) {
List<SecurityRuleDto> list = new ArrayList<>();
if (null != rules) {
for (MotechURLSecurityRule rule : rules) {
SecurityRuleDto dto = new SecurityRuleDto();
dto.setId(rule.getId());
dto.setActive(rule.isActive());
dto.setDeleted(rule.isDeleted());
dto.setOrigin(rule.getOrigin());
dto.setPattern(rule.getPattern());
dto.setPriority(rule.getPriority());
if (null != rule.getProtocol()) {
dto.setProtocol(rule.getProtocol().toString());
}
dto.setRest(rule.isRest());
dto.setVersion(rule.getVersion());
dto.setPermissionAccess(rule.getPermissionAccess());
dto.setUserAccess(rule.getUserAccess());
if (null != rule.getMethodsRequired()) {
dto.setMethodsRequired(new ArrayList<String>());
for (HTTPMethod method : rule.getMethodsRequired()) {
dto.getMethodsRequired().add(method.toString());
}
}
if (null != rule.getSupportedSchemes()) {
dto.setSupportedSchemes(new ArrayList<String>());
for (Scheme scheme : rule.getSupportedSchemes()) {
dto.getSupportedSchemes().add(scheme.toString());
}
}
list.add(dto);
}
}
return list;
}
use of org.motechproject.security.domain.MotechURLSecurityRule in project motech by motech.
the class SecurityRuleLoaderServiceImpl method loadRules.
@Transactional
public synchronized void loadRules(ApplicationContext applicationContext) {
LOGGER.debug("Loading rules from {}", applicationContext.getDisplayName());
Resource securityResource = applicationContext.getResource(CONFIG_LOCATION);
if (securityResource.exists()) {
LOGGER.debug("File {} exists in {}", CONFIG_LOCATION, applicationContext.getDisplayName());
try (InputStream in = securityResource.getInputStream()) {
List<MotechURLSecurityRule> rules = (List<MotechURLSecurityRule>) motechJsonReader.readFromStream(in, new TypeToken<List<MotechURLSecurityRule>>() {
}.getType());
if (rules.size() > 0) {
updateSecurityConfig(rules);
}
} catch (IOException e) {
LOGGER.error("Unable to load security rules from " + applicationContext.getDisplayName(), e);
}
}
LOGGER.debug("Rules loaded from {}", applicationContext.getDisplayName());
}
use of org.motechproject.security.domain.MotechURLSecurityRule in project motech by motech.
the class MotechSecurityRulesDao method getRules.
/**
* Returns all MotechURLSecurityRules
*
* @return list that contains rules
*/
@Transactional
public List<MotechURLSecurityRule> getRules() {
List<MotechURLSecurityRule> rules = dataService.retrieveAll();
Iterator<MotechURLSecurityRule> iterator = rules.iterator();
while (iterator.hasNext()) {
MotechURLSecurityRule rule = iterator.next();
if (rule.isDeleted()) {
iterator.remove();
}
}
return rules;
}
Aggregations