-
Notifications
You must be signed in to change notification settings - Fork 8
/
PdpEngineTest.java
134 lines (109 loc) · 6.33 KB
/
PdpEngineTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package pdp;
import org.apache.openaz.xacml.api.Decision;
import org.apache.openaz.xacml.api.Response;
import org.apache.openaz.xacml.api.Result;
import org.apache.openaz.xacml.std.json.JSONResponse;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.util.StringUtils;
import pdp.domain.*;
import pdp.policies.PolicyLoader;
import pdp.teams.TeamsPIP;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static pdp.teams.VootClientConfig.URN_COLLAB_PERSON_EXAMPLE_COM_ADMIN;
import static pdp.xacml.PdpPolicyDefinitionParser.*;
/**
* Note this class is slow. it starts up the entire Spring boot app.
* <p>
* If you want to test policies quickly then see StandAlonePdpEngineTest
*/
@ActiveProfiles("dev")
public class PdpEngineTest extends AbstractPdpIntegrationTest {
private TestRestTemplate restTemplate = new TestRestTemplate();
@Test
@Ignore
public void testAllPolicies() throws Exception {
addShibHeaders();
JsonPolicyRequest policyRequest = getJsonPolicyRequest();
List<PdpPolicy> policies = policyLoader.getPolicies();
//lambda's are poor with error handling as the stacktrace gets lost
for (PdpPolicy policy : policies) {
doTestPolicy(policyRequest, policy);
}
}
@Test
public void testCrsfConfiguration() throws Exception {
Map<String, Object> jsonResponse = postForObject("/internal/decide/policy", getJsonPolicyRequest(), new ParameterizedTypeReference<>() {
});
assertEquals(403, jsonResponse.get("status"));
}
@Override
public TestRestTemplate getRestTemplate() {
return restTemplate;
}
/**
* Test for the policy all possible scenario's mangling the JSON request
*/
private void doTestPolicy(JsonPolicyRequest policyRequest, PdpPolicy policy) throws Exception {
PdpPolicyDefinition definition = policyDefinitionParser.parse(policy);
JsonPolicyRequest permitPolicyRequest = policyRequest.copy();
// We don't want INDETERMINATE Decisions based on the Target of the policy so we ensure the SP and - optional - IDP is set on the JSON request
permitPolicyRequest.addOrReplaceResourceAttribute(SP_ENTITY_ID, definition.getServiceProviderIds().get(0));
if (definition.getIdentityProviderIds().isEmpty()) {
permitPolicyRequest.deleteAttribute(IDP_ENTITY_ID);
} else {
permitPolicyRequest.addOrReplaceResourceAttribute(IDP_ENTITY_ID, definition.getIdentityProviderIds().get(0));
}
JsonPolicyRequest denyPolicyRequest = permitPolicyRequest.copy();
JsonPolicyRequest denyIndeterminatePolicyRequest = permitPolicyRequest.copy();
Set<Map.Entry<String, List<PdpAttribute>>> entries = definition.getAttributes().stream().collect(Collectors.groupingBy(PdpAttribute::getName)).entrySet();
entries.forEach(entry -> {
//The permitPolicyRequest is mangled so every required attribute from the Policy is included
permitPolicyRequest.addOrReplaceAccessSubjectAttribute(entry.getKey(), entry.getValue().get(0).getValue());
denyPolicyRequest.addOrReplaceAccessSubjectAttribute(entry.getKey(), "will-not-match-" + UUID.randomUUID().toString());
denyIndeterminatePolicyRequest.deleteAttribute(entry.getKey());
});
//See VootClientConfig#mockVootClient for groups to be returned and VootClientConfig#URN_COLLAB_PERSON_EXAMPLE_COM_ADMIN for the user it expects
if (definition.getAttributes().stream().anyMatch(attr -> attr.getName().equalsIgnoreCase(TeamsPIP.GROUP_URN))) {
permitPolicyRequest.addOrReplaceAccessSubjectAttribute(NAME_ID, URN_COLLAB_PERSON_EXAMPLE_COM_ADMIN);
}
JsonPolicyRequest notApplicablePolicyRequest = permitPolicyRequest.copy();
notApplicablePolicyRequest.addOrReplaceResourceAttribute(SP_ENTITY_ID, UUID.randomUUID().toString());
notApplicablePolicyRequest.addOrReplaceResourceAttribute(IDP_ENTITY_ID, UUID.randomUUID().toString());
becomeAnApiClientSoWeDontNeedACSRFToken();
// We can't use Transactional rollback as the Application runs in a different process.
postDecide(policy, permitPolicyRequest, definition.isDenyRule() ? Decision.DENY : Decision.PERMIT, "urn:oasis:names:tc:xacml:1.0:status:ok");
postDecide(policy, denyPolicyRequest, definition.isDenyRule() ? Decision.PERMIT : Decision.DENY, "urn:oasis:names:tc:xacml:1.0:status:ok");
postDecide(policy, denyIndeterminatePolicyRequest,
definition.isDenyRule() ? Decision.INDETERMINATE : Decision.DENY,
definition.isDenyRule() ? "urn:oasis:names:tc:xacml:1.0:status:missing-attribute" : "urn:oasis:names:tc:xacml:1.0:status:ok");
postDecide(policy, notApplicablePolicyRequest, Decision.NOTAPPLICABLE, "urn:oasis:names:tc:xacml:1.0:status:ok");
}
private void becomeAnApiClientSoWeDontNeedACSRFToken() {
restTemplate = new TestRestTemplate("pdp_admin", "secret");
impersonate(PolicyLoader.authenticatingAuthority, "urn:collab:person:example.com:mary.doe", "Mary Doe");
}
private void postDecide(PdpPolicy policy, JsonPolicyRequest policyRequest, Decision expectedDecision, String statusCodeValue) throws Exception {
String jsonResponse = post("/decide/policy", policyRequest).getBody();
Response response = JSONResponse.load(jsonResponse);
assertThat(policy.getName(), response.getResults(), hasSize(1));
Result result = response.getResults().iterator().next();
assertEquals(policy.getName(), expectedDecision, result.getDecision());
assertEquals(policy.getName(), statusCodeValue, result.getStatus().getStatusCode().getStatusCodeValue().getUri().toString());
}
private boolean isValid(PdpPolicyViolation violation) {
return violation.getPolicy() != null
&& StringUtils.hasText(violation.getJsonRequest())
&& StringUtils.hasText(violation.getResponse());
}
}