Search in sources :

Example 11 with MotechURLSecurityRule

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();
}
Also used : MotechSecurityConfiguration(org.motechproject.security.domain.MotechSecurityConfiguration) MotechURLSecurityRule(org.motechproject.security.domain.MotechURLSecurityRule)

Example 12 with MotechURLSecurityRule

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);
}
Also used : MotechURLSecurityRule(org.motechproject.security.domain.MotechURLSecurityRule) Test(org.junit.Test)

Example 13 with MotechURLSecurityRule

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;
}
Also used : Scheme(org.motechproject.security.constants.Scheme) HTTPMethod(org.motechproject.security.constants.HTTPMethod) MotechURLSecurityRule(org.motechproject.security.domain.MotechURLSecurityRule) ArrayList(java.util.ArrayList) SecurityRuleDto(org.motechproject.security.model.SecurityRuleDto)

Example 14 with MotechURLSecurityRule

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());
}
Also used : InputStream(java.io.InputStream) MotechURLSecurityRule(org.motechproject.security.domain.MotechURLSecurityRule) Resource(org.springframework.core.io.Resource) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 15 with MotechURLSecurityRule

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;
}
Also used : MotechURLSecurityRule(org.motechproject.security.domain.MotechURLSecurityRule) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

MotechURLSecurityRule (org.motechproject.security.domain.MotechURLSecurityRule)18 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)6 Transactional (org.springframework.transaction.annotation.Transactional)6 HTTPMethod (org.motechproject.security.constants.HTTPMethod)4 MotechSecurityConfiguration (org.motechproject.security.domain.MotechSecurityConfiguration)4 Scheme (org.motechproject.security.constants.Scheme)3 Collection (java.util.Collection)2 SecurityRuleComparator (org.motechproject.security.domain.SecurityRuleComparator)2 SecurityRuleDto (org.motechproject.security.model.SecurityRuleDto)2 SecurityFilterChain (org.springframework.security.web.SecurityFilterChain)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 List (java.util.List)1 TreeSet (java.util.TreeSet)1 InOrder (org.mockito.InOrder)1 Resource (org.springframework.core.io.Resource)1 FilterChainProxy (org.springframework.security.web.FilterChainProxy)1