Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void beforeClass() throws IOException {
this.policySet = PolicySets.loadFromFile(file);
}

@Test(enabled = true)
@Test
public void testLoadPolicySet() throws Exception {
Assert.assertNotNull(this.policySet);
Assert.assertEquals(this.policySet.getPolicies().size(), 7);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.ge.predix.acs.attribute.connectors;

import java.util.Set;

import com.ge.predix.acs.model.Attribute;

public interface AttributeReader {
Set<Attribute> getAttributes(String identifier);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.ge.predix.acs.attribute.connectors;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ge.predix.acs.model.Attribute;
import com.ge.predix.acs.privilege.management.PrivilegeManagementService;
import com.ge.predix.acs.rest.BaseResource;

@Component
public class DefaultResourceAttributeReader implements AttributeReader {
@Autowired
private PrivilegeManagementService privilegeManagementService;

@Override
public Set<Attribute> getAttributes(final String identifier) {
Set<Attribute> resourceAttributes = Collections.emptySet();
BaseResource resource =
this.privilegeManagementService.getByResourceIdentifierWithInheritedAttributes(identifier);
if (null != resource) {
resourceAttributes = Collections.unmodifiableSet(new HashSet<>(resource.getAttributes()));
}
return resourceAttributes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.ge.predix.acs.attribute.connectors;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ge.predix.acs.model.Attribute;
import com.ge.predix.acs.privilege.management.PrivilegeManagementService;
import com.ge.predix.acs.rest.BaseSubject;

@Component
public class DefaultSubjectAttributeReader implements SubjectAttributeReader {
@Autowired
private PrivilegeManagementService privilegeManagementService;

@Override
public Set<Attribute> getAttributes(final String identifier) {
return this.getAttributesByScope(identifier, Collections.emptySet());
}

@Override
public Set<Attribute> getAttributesByScope(final String identifier, final Set<Attribute> scopes) {
Set<Attribute> subjectAttributes = Collections.emptySet();
BaseSubject subject = this.privilegeManagementService.getBySubjectIdentifierAndScopes(identifier, scopes);
if (null != subject) {
subjectAttributes = Collections.unmodifiableSet(new HashSet<>(subject.getAttributes()));
}
return subjectAttributes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.ge.predix.acs.attribute.connectors;

import java.util.Set;

import com.ge.predix.acs.model.Attribute;

public interface SubjectAttributeReader extends AttributeReader {
Set<Attribute> getAttributesByScope(String identifier, Set<Attribute> scopes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@
import com.ge.predix.acs.policy.evaluation.cache.PolicyEvaluationCacheCircuitBreaker;
import com.ge.predix.acs.policy.evaluation.cache.PolicyEvaluationRequestCacheKey;
import com.ge.predix.acs.policy.evaluation.cache.PolicyEvaluationRequestCacheKey.Builder;
import com.ge.predix.acs.privilege.management.PrivilegeManagementService;
import com.ge.predix.acs.privilege.management.dao.AttributeLimitExceededException;
import com.ge.predix.acs.rest.BaseSubject;
import com.ge.predix.acs.rest.PolicyEvaluationRequestV1;
import com.ge.predix.acs.rest.PolicyEvaluationResult;
import com.ge.predix.acs.service.policy.admin.PolicyManagementService;
Expand All @@ -77,8 +75,6 @@ public class PolicyEvaluationServiceImpl implements PolicyEvaluationService {
@Autowired
private PolicySetValidator policySetValidator;
@Autowired
private PrivilegeManagementService privilegeService;
@Autowired
private ZoneResolver zoneResolver;

@Override
Expand Down Expand Up @@ -311,15 +307,6 @@ boolean evaluateConditions(final Set<Attribute> subjectAttributes, final Set<Att
return result;
}

Set<Attribute> getSubjectAttributes(final String subjectIdentifier) {
Set<Attribute> subjectAttributes = Collections.emptySet();
BaseSubject subject = this.privilegeService.getBySubjectIdentifier(subjectIdentifier);
if (subject != null) {
subjectAttributes = subject.getAttributes();
}
return subjectAttributes;
}

private Map<String, Object> getAttributeBindingsMap(final Set<Attribute> subjectAttributes,
final Set<Attribute> resourceAttributes, final String resourceURI, final String resourceURITemplate) {
SubjectHandler subjectHandler = new SubjectHandler(subjectAttributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@
import org.apache.commons.lang.StringUtils;
import org.springframework.web.util.UriTemplate;

import com.ge.predix.acs.attribute.connectors.AttributeReader;
import com.ge.predix.acs.model.Attribute;
import com.ge.predix.acs.model.Policy;
import com.ge.predix.acs.privilege.management.PrivilegeManagementService;
import com.ge.predix.acs.rest.BaseResource;
import com.ge.predix.acs.service.policy.matcher.UriTemplateVariableResolver;

public class ResourceAttributeResolver {

public static final String ATTRIBUTE_URI_TEMPLATE_VARIABLE = "attribute_uri";
private static final String ATTRIBUTE_URI_TEMPLATE_VARIABLE = "attribute_uri";

private final Map<String, Set<Attribute>> resourceAttributeMap = new HashMap<>();
private final PrivilegeManagementService privilegeService;
private final AttributeReader resourceAttributeReader;
private final Set<Attribute> supplementalResourceAttributes;
private final String requestResourceUri;
private final UriTemplateVariableResolver uriTemplateVariableResolver = new UriTemplateVariableResolver();
Expand All @@ -44,9 +43,9 @@ public class ResourceAttributeResolver {
* @param requestResourceUri
* URI of the resource from the policy evaluation request
*/
public ResourceAttributeResolver(final PrivilegeManagementService privilegeService, final String requestResourceUri,
public ResourceAttributeResolver(final AttributeReader resourceAttributeReader, final String requestResourceUri,
final Set<Attribute> supplementalResourceAttributes) {
this.privilegeService = privilegeService;
this.resourceAttributeReader = resourceAttributeReader;
this.requestResourceUri = requestResourceUri;
if (null == supplementalResourceAttributes) {
this.supplementalResourceAttributes = Collections.emptySet();
Expand All @@ -56,34 +55,19 @@ public ResourceAttributeResolver(final PrivilegeManagementService privilegeServi
}

public ResourceAttributeResolverResult getResult(final Policy policy) {
Set<Attribute> resourceAttributes;
String resovledResourceUri = resolveResourceURI(policy);
String resolvedResourceUri = resolveResourceURI(policy);
boolean uriTemplateExists = true;
if (null == resovledResourceUri) {
resovledResourceUri = this.requestResourceUri;
if (null == resolvedResourceUri) {
resolvedResourceUri = this.requestResourceUri;
uriTemplateExists = false;
}
resourceAttributes = this.resourceAttributeMap.get(resovledResourceUri);
Set<Attribute> resourceAttributes = this.resourceAttributeMap.get(resolvedResourceUri);
if (null == resourceAttributes) {
resourceAttributes = getResourceAttributes(resovledResourceUri);
resourceAttributes = new HashSet<>(this.resourceAttributeReader.getAttributes(resolvedResourceUri));
resourceAttributes.addAll(this.supplementalResourceAttributes);
this.resourceAttributeMap.put(resovledResourceUri, resourceAttributes);
this.resourceAttributeMap.put(resolvedResourceUri, resourceAttributes);
}
return new ResourceAttributeResolverResult(resourceAttributes, resovledResourceUri, uriTemplateExists);
}

public Set<Attribute> getResourceAttributes(final Policy policy) {
return getResult(policy).getResourceAttributes();
}

private Set<Attribute> getResourceAttributes(final String resovledResourceUri) {
Set<Attribute> resourceAttributes = new HashSet<>();
BaseResource resource = this.privilegeService
.getByResourceIdentifierWithInheritedAttributes(resovledResourceUri);
if (null != resource) {
resourceAttributes.addAll(resource.getAttributes());
}
return resourceAttributes;
return new ResourceAttributeResolverResult(resourceAttributes, resolvedResourceUri, uriTemplateExists);
}

String resolveResourceURI(final Policy policy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@
import java.util.Map;
import java.util.Set;

import com.ge.predix.acs.attribute.connectors.SubjectAttributeReader;
import com.ge.predix.acs.model.Attribute;
import com.ge.predix.acs.privilege.management.PrivilegeManagementService;
import com.ge.predix.acs.rest.BaseSubject;

public class SubjectAttributeResolver {

private final Map<String, Set<Attribute>> subjectAttributeMap = new HashMap<>();
private final PrivilegeManagementService privilegeService;
private final SubjectAttributeReader subjectAttributeReader;
private final String subjectIdentifier;
private final Set<Attribute> supplementalSubjectAttributes;

public SubjectAttributeResolver(final PrivilegeManagementService privilegeService, final String subjectIdentifier,
public SubjectAttributeResolver(final SubjectAttributeReader subjectAttributeReader, final String subjectIdentifier,
final Set<Attribute> supplementalSubjectAttributes) {
this.privilegeService = privilegeService;
this.subjectAttributeReader = subjectAttributeReader;
this.subjectIdentifier = subjectIdentifier;
if (null == supplementalSubjectAttributes) {
this.supplementalSubjectAttributes = Collections.emptySet();
Expand All @@ -31,12 +30,8 @@ public SubjectAttributeResolver(final PrivilegeManagementService privilegeServic
public Set<Attribute> getResult(final Set<Attribute> scopes) {
Set<Attribute> subjectAttributes = this.subjectAttributeMap.get(this.subjectIdentifier);
if (null == subjectAttributes) {
subjectAttributes = new HashSet<>();
BaseSubject subject = this.privilegeService
.getBySubjectIdentifierAndScopes(this.subjectIdentifier, scopes);
if (null != subject) {
subjectAttributes.addAll(subject.getAttributes());
}
subjectAttributes = new HashSet<>(
this.subjectAttributeReader.getAttributesByScope(this.subjectIdentifier, scopes));
subjectAttributes.addAll(this.supplementalSubjectAttributes);
this.subjectAttributeMap.put(this.subjectIdentifier, subjectAttributes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ge.predix.acs.attribute.connectors.DefaultResourceAttributeReader;
import com.ge.predix.acs.attribute.connectors.DefaultSubjectAttributeReader;
import com.ge.predix.acs.commons.web.UriTemplateUtils;
import com.ge.predix.acs.model.Attribute;
import com.ge.predix.acs.model.Policy;
import com.ge.predix.acs.privilege.management.PrivilegeManagementService;
import com.ge.predix.acs.service.policy.evaluation.MatchedPolicy;
import com.ge.predix.acs.service.policy.evaluation.ResourceAttributeResolver;
import com.ge.predix.acs.service.policy.evaluation.ResourceAttributeResolver.ResourceAttributeResolverResult;
Expand All @@ -47,7 +48,10 @@ public class PolicyMatcherImpl implements PolicyMatcher {
private static final Logger LOGGER = LoggerFactory.getLogger(PolicyMatcherImpl.class);

@Autowired
private PrivilegeManagementService privilegeManagementService;
private DefaultResourceAttributeReader resourceAttributeReader;

@Autowired
private DefaultSubjectAttributeReader subjectAttributeReader;

@Override
public List<MatchedPolicy> match(final PolicyMatchCandidate candidate, final List<Policy> policies) {
Expand All @@ -57,11 +61,10 @@ public List<MatchedPolicy> match(final PolicyMatchCandidate candidate, final Lis
@Override
public MatchResult matchForResult(final PolicyMatchCandidate candidate, final List<Policy> policies) {
ResourceAttributeResolver resourceAttributeResolver = new ResourceAttributeResolver(
this.privilegeManagementService, candidate.getResourceURI(),
this.resourceAttributeReader, candidate.getResourceURI(),
candidate.getSupplementalResourceAttributes());
SubjectAttributeResolver subjectAttributeResolver = new SubjectAttributeResolver(
this.privilegeManagementService, candidate.getSubjectIdentifier(),
candidate.getSupplementalSubjectAttributes());
SubjectAttributeResolver subjectAttributeResolver = new SubjectAttributeResolver(this.subjectAttributeReader,
candidate.getSubjectIdentifier(), candidate.getSupplementalSubjectAttributes());

List<MatchedPolicy> matchedPolicies = new ArrayList<>();
Set<String> resolvedResourceUris = new HashSet<>();
Expand All @@ -82,7 +85,7 @@ public MatchResult matchForResult(final PolicyMatchCandidate candidate, final Li
/**
* @param candidate
* policy match candidate
* @param policiy
* @param policy
* to match
* @return true if the policy meets the criteria
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.ge.predix.acs.attribute.connectors;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;

import java.util.HashSet;
import java.util.Set;

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.ge.predix.acs.model.Attribute;
import com.ge.predix.acs.privilege.management.PrivilegeManagementService;
import com.ge.predix.acs.rest.BaseResource;

@Test
public class DefaultResourceAttributeReaderTest {
@Mock
private PrivilegeManagementService privilegeManagementService;

@Autowired
@InjectMocks
private DefaultResourceAttributeReader defaultResourceAttributeReader;

@BeforeMethod
public void beforeMethod() {
MockitoAnnotations.initMocks(this);
}

@Test
public void testGetAttributes() throws Exception {
Set<Attribute> resourceAttributes = new HashSet<>();
resourceAttributes.add(new Attribute("https://acs.attributes.int", "site", "sanramon"));
BaseResource testResource = new BaseResource("/test/resource", resourceAttributes);

when(this.privilegeManagementService.getByResourceIdentifierWithInheritedAttributes(any()))
.thenReturn(testResource);
Assert.assertTrue(this.defaultResourceAttributeReader.getAttributes(testResource.getResourceIdentifier())
.containsAll(resourceAttributes));
}

@Test
public void testGetAttributesForNonExistentResource() throws Exception {
when(this.privilegeManagementService.getByResourceIdentifierWithInheritedAttributes(any())).thenReturn(null);
Assert.assertTrue(this.defaultResourceAttributeReader.getAttributes("nonexistentResource").isEmpty());
}
}
Loading