forked from OpenConext/OpenConext-pdp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StandAlonePdpEngineTest.java
294 lines (234 loc) · 12.4 KB
/
StandAlonePdpEngineTest.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package pdp;
import net.jcip.annotations.NotThreadSafe;
import org.apache.commons.io.IOUtils;
import org.apache.openaz.xacml.api.Attribute;
import org.apache.openaz.xacml.api.AttributeCategory;
import org.apache.openaz.xacml.api.Decision;
import org.apache.openaz.xacml.api.IdReference;
import org.apache.openaz.xacml.api.Obligation;
import org.apache.openaz.xacml.api.Request;
import org.apache.openaz.xacml.api.Response;
import org.apache.openaz.xacml.api.Result;
import org.apache.openaz.xacml.api.pdp.PDPEngine;
import org.apache.openaz.xacml.std.json.JSONRequest;
import org.apache.openaz.xacml.std.json.JSONResponse;
import org.apache.openaz.xacml.util.FactoryException;
import org.apache.openaz.xacml.util.XACMLProperties;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import pdp.domain.PdpPolicy;
import pdp.policies.PolicyLoader;
import pdp.repositories.PdpPolicyRepository;
import pdp.sab.SabClient;
import pdp.sab.SabClientConfig;
import pdp.sab.SabPIP;
import pdp.teams.TeamsPIP;
import pdp.teams.VootClient;
import pdp.teams.VootClientConfig;
import pdp.web.PdpController;
import pdp.xacml.OpenConextPDPEngineFactory;
import pdp.xacml.PolicyTemplateEngine;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static pdp.util.StreamUtils.singletonCollector;
@NotThreadSafe
public class StandAlonePdpEngineTest extends AbstractXacmlTest {
private static Logger LOG = LoggerFactory.getLogger(PdpController.class);
private PDPEngine pdpEngine;
private PdpPolicyRepository pdpPolicyRepository;
private static VootClient vootClient = new VootClientConfig().mockVootClient();
private static SabClient sabClient = new SabClientConfig().mockSabClient("user", "password", "http://localhost");
@BeforeClass
public static void beforeClass() throws IOException {
Resource resource = new ClassPathResource("xacml.conext.properties");
String absolutePath = resource.getFile().getAbsolutePath();
//This will be picked up by the XACML bootstrapping when creating a new PDPEngine
System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, absolutePath);
XACMLProperties.reloadProperties();
}
private void setUp(String... policyFiles) throws IOException, FactoryException {
pdpPolicyRepository = mock(PdpPolicyRepository.class);
List<PdpPolicy> pdpPolicies = Arrays.asList(policyFiles).stream().map(policyFile -> loadPolicy(policyFile)).collect(toList());
when(pdpPolicyRepository.findAll()).thenReturn(pdpPolicies);
OpenConextPDPEngineFactory pdpEngineFactory = new OpenConextPDPEngineFactory();
this.pdpEngine = pdpEngineFactory.newEngine(true, false, pdpPolicyRepository, vootClient, sabClient);
}
private PdpPolicy loadPolicy(String policyFile) {
try {
String policyXml = IOUtils.toString(new ClassPathResource("xacml/test-policies/" + policyFile).getInputStream(), "UTF-8");
return new PdpPolicy(policyXml, policyFile, true, "system", PolicyLoader.authenticatingAuthority, "John Doe", true, "reg");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@After
public void after() throws Exception {
super.after();
}
@Test
public void testMultipleAndPolicy() throws Exception {
doDecideTest("test_request_multiple_and.json", Decision.PERMIT, "OpenConext.pdp.test.multiple.and.Policy.xml");
}
@Test
public void testDenyPolicyWithPermit() throws Exception {
doDecideTest("test_request_deny_policy_permit.json", Decision.PERMIT, "OpenConext.pdp.test.deny.Policy.xml");
}
@Test
public void testMulivaluedAttributePolicyWithPermit() throws Exception {
doDecideTest("test_request_multivalued_attribute.json", Decision.PERMIT, "OpenConext.pdp.test.multivalued.attribute.Policy.xml");
}
@Test
public void testDenyPolicyWithDeny() throws Exception {
doDecideTest("test_request_deny_policy_deny.json", Decision.DENY, "OpenConext.pdp.test.deny.Policy.xml");
}
@Test
public void testDenyPolicyWithMissingAttribute() throws Exception {
Result result = doDecideTest("test_request_deny_policy_missing_attribute.json", Decision.INDETERMINATE, "OpenConext.pdp.test.deny.Policy.xml");
assertEquals("Missing required attribute", result.getStatus().getStatusMessage());
}
@Test
public void testDenyPolicyWithNoPolicyFound() throws Exception {
doDecideTest("test_request_no_matching_target.json", Decision.NOTAPPLICABLE, "OpenConext.pdp.test.deny.Policy.xml");
}
@Test
public void testPermitPolicyWithMultipleOr() throws Exception {
doDecideTest("test_request_multiple_or.json", Decision.PERMIT, "OpenConext.pdp.test.multiple.or.Policy.xml");
}
@Test
public void testTeamsPolicyWithAggregatedAttributes() throws Exception {
Result result = doDecideTest("test_request_teams_policy.json", Decision.PERMIT, "OpenConext.pdp.test.teams.Policy.xml");
assertAggregatedAttribute(result, TeamsPIP.GROUP_URN, "urn:collab:group:test.surfteams.nl:nl:surfnet:diensten:managementvo");
}
@Test
public void testSabPolicyWithAggregatedAttributes() throws Exception {
Result result = doDecideTest("test_request_sab_policy.json", Decision.PERMIT, "OpenConext.pdp.test.sab.Policy.xml");
assertAggregatedAttribute(result, SabPIP.SAB_URN, "OperationeelBeheerder");
}
@Test
public void testPermitPolicyWithMultipleIdp() throws Exception {
doDecideTest("test_request_multiple_or.json", Decision.PERMIT, "OpenConext.pdp.test.multiple.or.Policy.xml");
}
@Test
public void testConflictingPolicies() throws Exception {
Result result = doDecideTest("test_request_conflicting_policies.json", Decision.DENY,
"OpenConext.pdp.test.conflicting.policies.1.Policy.xml",
"OpenConext.pdp.test.conflicting.policies.2.Policy.xml"
);
IdReference reference = result.getPolicyIdentifiers().stream().collect(singletonCollector());
//the violated policy
assertEquals(PolicyTemplateEngine.getPolicyId("OpenConext.pdp.test.conflicting.policies.2.Policy.xml"), reference.getId().stringValue());
}
@Test
public void testNotApplicable() throws Exception {
doDecideTest("json_policy_request.json", Decision.NOTAPPLICABLE, "OpenConext.pdp.test.unknown.SP.Policy.xml");
}
@Test
public void testIpRangeObligation() throws Exception {
Result result = doDecideTest("json_policy_ip_range_request.json", Decision.PERMIT, "OpenConext.pdp.test.obligations.Policy.xml");
Collection<Obligation> obligations = result.getObligations();
assertEquals(1, obligations.size());
Obligation obligation = obligations.iterator().next();
assertEquals("http://test2.surfconext.nl/assurance/loa2",
obligation.getAttributeAssignments().iterator().next().getAttributeValue().getValue());
}
@Test
public void testIpRangeMultipleRulesObligation() throws Exception {
Result result = doDecideTest("json_policy_ip_range_request.json", Decision.PERMIT,
"OpenConext.pdp.test.multiple.rules.obligations.Policy.xml");
Collection<Obligation> obligations = result.getObligations();
assertEquals(1, obligations.size());
Obligation obligation = obligations.iterator().next();
assertEquals("http://test2.surfconext.nl/assurance/loa3",
obligation.getAttributeAssignments().iterator().next().getAttributeValue().getValue());
}
@Test
public void testIpRangeObligationNegate() throws Exception {
Result result = doDecideTest("json_policy_ip_range_request.json", Decision.PERMIT,
"OpenConext.pdp.test.obligations.negate.Policy.xml");
Collection<Obligation> obligations = result.getObligations();
assertEquals(0, obligations.size());
}
@Test
public void testIpOutOfRangeObligation() throws Exception {
Result result = doDecideTest("json_policy_ip_not_in_range_request.json", Decision.PERMIT, "OpenConext.pdp.test.obligations.Policy.xml");
Collection<Obligation> obligations = result.getObligations();
assertEquals(0, obligations.size());
}
@Test
public void testIpRangeObligationNotApplicable() throws Exception {
doDecideTest("json_policy_ip_range_not_applicable_request.json", Decision.NOTAPPLICABLE, "OpenConext.pdp.test.obligations.Policy.xml");
}
@Test
public void testIpRangeObligationIndeterminate() throws Exception {
doDecideTest("json_policy_ip_range_indeterminate_request.json", Decision.INDETERMINATE, "OpenConext.pdp.test.obligations.Policy.xml");
}
@Test
public void multipleObligationsCombined() throws Exception {
Result result = doDecideTest("json_policy_ip_range_request.json", Decision.PERMIT,
"OpenConext.pdp.test.obligations.Policy.xml",
"OpenConext.pdp.test.obligations.loa3.Policy.xml");
Collection<Obligation> obligations = result.getObligations();
assertEquals(2, obligations.size());
Obligation obligation = obligations.iterator().next();
assertEquals("http://test2.surfconext.nl/assurance/loa2",
obligation.getAttributeAssignments().iterator().next().getAttributeValue().getValue());
}
@Test
public void testIpRangeAndAffiliationObligation() throws Exception {
Result result = doDecideTest("json_policy_ip_range_and_affiliation_request.json",
Decision.PERMIT, "OpenConext.pdp.test.obligations.multiple.and.Policy.xml");
Collection<Obligation> obligations = result.getObligations();
assertEquals(1, obligations.size());
Obligation obligation = obligations.iterator().next();
assertEquals("http://test2.surfconext.nl/assurance/loa3",
obligation.getAttributeAssignments().iterator().next().getAttributeValue().getValue());
}
@Test
public void testStepupTemplate() throws Exception {
Result result = doDecideTest("json_policy_stepup_request.json",
Decision.PERMIT, "stepup.policy.template.xml");
Collection<Obligation> obligations = result.getObligations();
assertEquals(1, obligations.size());
Obligation obligation = obligations.iterator().next();
assertEquals("http://localhost/assurance/loa3",
obligation.getAttributeAssignments().iterator().next().getAttributeValue().getValue());
}
private Result doDecideTest(final String requestFile, Decision decision, String... policyFiles) throws Exception {
setUp(policyFiles);
String payload = IOUtils.toString(new ClassPathResource("xacml/requests/" + requestFile).getInputStream(), "UTF-8");
Request pdpRequest = JSONRequest.load(payload);
Response pdpResponse = pdpEngine.decide(pdpRequest);
return assertResponse(decision, pdpResponse);
}
private Result assertResponse(Decision decision, Response pdpResponse) throws Exception {
String json = JSONResponse.toString(pdpResponse, true);
if (decision.equals(Decision.DENY)) {
assertTrue(json.contains("\"AttributeId\" : \"IdPOnly\""));
}
assertEquals(1, pdpResponse.getResults().size());
Result result = pdpResponse.getResults().iterator().next();
assertEquals(decision, result.getDecision());
return result;
}
private void assertAggregatedAttribute(Result result, String urn, String attributeValue) {
List<Attribute> attributes = result.getAttributes().stream().map(AttributeCategory::getAttributes).flatMap(Collection::stream).collect(toList());
assertEquals(1, attributes.size());
Attribute attribute = attributes.get(0);
assertEquals(urn, attribute.getAttributeId().getUri().toString());
assertEquals(urn, attribute.getCategory().getUri().toString());
List<String> attributeValues = attribute.getValues().stream().map(attrValue -> (String) attrValue.getValue()).collect(toList());
assertEquals(Arrays.asList(attributeValue), attributeValues);
}
}