-
Notifications
You must be signed in to change notification settings - Fork 8
/
SNEXACMLValidationTest.java
executable file
·317 lines (261 loc) · 13.1 KB
/
SNEXACMLValidationTest.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* SNE-XACML demo project: illustrate how to use SNE-XACML engine.
*
* Copyright (C) 2013 Canh T. Ngo <canhnt@gmail.com>
* System and Network Engineering Group, University of Amsterdam.
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package nl.uva.sne.xacml.evaltest;
import com.sun.xacml.PDP;
import com.sun.xacml.PDPConfig;
import com.sun.xacml.UnknownIdentifierException;
import com.sun.xacml.combine.PermitOverridesPolicyAlg;
import com.sun.xacml.ctx.ResponseCtx;
import com.sun.xacml.ctx.Result;
import com.sun.xacml.finder.AttributeFinder;
import com.sun.xacml.finder.AttributeFinderModule;
import com.sun.xacml.finder.PolicyFinder;
import com.sun.xacml.finder.PolicyFinderModule;
import com.sun.xacml.finder.impl.CurrentEnvModule;
import com.sun.xacml.finder.impl.SelectorModule;
import com.sun.xacml.support.finder.StaticPolicyFinderModule;
import com.sun.xacml.support.finder.StaticRefPolicyFinderModule;
import nl.uva.sne.midd.MIDDException;
import nl.uva.sne.xacml.evaltest.sunxacml.RequestGenerator23;
import nl.uva.sne.xacml.evaltest.sunxacml.SunXACMLUtil;
import nl.uva.sne.xacml.policy.parsers.MIDDParsingException;
import nl.uva.sne.xacml.policy.parsers.XACMLParsingException;
import nl.uva.sne.xacml.util.XACMLUtil;
import oasis.names.tc.xacml._2_0.context.schema.os.RequestType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.*;
import org.junit.Test;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.*;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import static org.junit.Assert.assertTrue;
public class SNEXACMLValidationTest {
public static final String[] STATIC_XACML2_POLICIES = {
"policies/continue-a.xml"
};
public static final String[] STATIC_XACML2_REF_POLICIES = {
};
private static final String LOGGING_FILE = "sne-xacml-validation-logging.txt";
// the policy that has 'resource-id' attribute, not 'resource-class'
private static final String XACML3_POLICY_FILE = "policies/continue-a-xacml3.xml";
private static final int RANDOM_REQUESTS = 1000;
PrintWriter logger;
// The generator to generate both identical XACML 2.0 & 3.0 requests
private RequestGenerator23 generator;
public SNEXACMLValidationTest() throws SecurityException, IOException {
logger = new PrintWriter(LOGGING_FILE);
}
private PDP initSunXACMLPDP() throws URISyntaxException, UnknownIdentifierException {
//Initialization
StaticPolicyFinderModule staticModule = new StaticPolicyFinderModule(
PermitOverridesPolicyAlg.algId, Arrays.asList(STATIC_XACML2_POLICIES));
StaticRefPolicyFinderModule staticRefModule = new StaticRefPolicyFinderModule(
Arrays.asList(STATIC_XACML2_REF_POLICIES));
Set<PolicyFinderModule> policyModules = new HashSet<PolicyFinderModule>();
policyModules.add(staticModule);
policyModules.add(staticRefModule);
PDPConfig config = createPDPConfig(policyModules);
PDP sunPDP = new PDP(config);
return sunPDP;
}
@Test
public void runTest() throws ParserConfigurationException, SAXException, IOException, URISyntaxException, UnknownIdentifierException, XACMLParsingException, MIDDException {
SNEXACMLValidationTest instance = new SNEXACMLValidationTest();
assertTrue(instance.validate(RANDOM_REQUESTS));
}
/**
* Validate random requests against two PDP: SunXACML & SNE-XACML
*
* @param NumberOfRequests
* @throws URISyntaxException
* @throws UnknownIdentifierException
* @throws MIDDParsingException
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
* @throws XACMLParsingException
* @throws MIDDException
*/
private boolean validate(int NumberOfRequests) throws URISyntaxException, UnknownIdentifierException, ParserConfigurationException, SAXException, IOException, XACMLParsingException, MIDDException {
turnOffLogger();
initGenerator();
//create a SunXACML PDP
PDP pdp2 = initSunXACMLPDP();
// create a SNE-XACML PDP
nl.uva.sne.xacml.PDP pdp3 = initSNEXACMLPDP();
RequestType request2 = null;
oasis.names.tc.xacml._3_0.core.schema.wd_17.RequestType request3 = null;
ResponseCtx response2;
ResponseType response3;
int cntInvalidResponses = 0;
for (int i = 0; i < NumberOfRequests; i++) {
// Generate two identical requests
request2 = this.generator.generate();
request3 = generator.getRequest3();
// get response from SunXACML PDP
response2 = pdp2.evaluate(request2);
// get response from SNE-XACML PDP
response3 = pdp3.evaluate(request3);
if (response2 == null) {
throw new RuntimeException("Invalid SunXACML evaluation for the request:" + print(request3));
} else if (response3 == null) {
throw new RuntimeException("Invalid SNEXACML evaluation for the request:" + print(request3));
} else if (!compareResponses(response2, response3)) {
String xacml3Request = print(request3);
String xacml2Request = print(request2);
logger.println("Inconsistent SNEXACML response for the request:\n" + xacml2Request + "\n" + xacml3Request);
cntInvalidResponses++;
}
}
String outputMsg = "Performed validating " + NumberOfRequests + " random requests. Unmatched requests are: " + cntInvalidResponses;
System.out.println(outputMsg);
logger.println(outputMsg);
return cntInvalidResponses == 0;
}
private String print(RequestType request2) {
return SunXACMLUtil.toString(SunXACMLUtil.marshall(request2));
}
private boolean compareResponses(ResponseCtx response2,
ResponseType response3) {
if (response3.getResult() == null || response3.getResult().size() == 0) {
return false;
}
ResultType result3 = response3.getResult().get(0);
Iterator it = response2.getResults().iterator();
// only use the first result
if (!it.hasNext()) {
System.out.println("Not Decision found in response2");
return false;
}
Result result2 = (Result) it.next();
if (result3.getDecision() == DecisionType.PERMIT && result2.getDecision() == Result.DECISION_PERMIT) {
return true;
}
if (result3.getDecision() == DecisionType.DENY && result2.getDecision() == Result.DECISION_DENY) {
return true;
}
if (result3.getDecision() == DecisionType.NOT_APPLICABLE && result2.getDecision() == Result.DECISION_NOT_APPLICABLE) {
return true;
}
if (result3.getDecision() == DecisionType.INDETERMINATE && result2.getDecision() == Result.DECISION_INDETERMINATE) {
return true;
}
return false;
}
private String print(
oasis.names.tc.xacml._3_0.core.schema.wd_17.RequestType request3) {
OutputStream os = new ByteArrayOutputStream();
XACMLUtil.print((new ObjectFactory()).createRequest(request3),
oasis.names.tc.xacml._3_0.core.schema.wd_17.RequestType.class, os);
return os.toString();
}
private nl.uva.sne.xacml.PDP initSNEXACMLPDP() throws ParserConfigurationException, SAXException, IOException, MIDDParsingException, XACMLParsingException, MIDDException {
PolicySetType policyset = XACMLUtil.unmarshalPolicySetType(XACML3_POLICY_FILE);
nl.uva.sne.xacml.PDP pdp = new nl.uva.sne.xacml.PDP(policyset, null);
pdp.initialize();
return pdp;
}
public void turnOffLogger() {
LogManager.getLogManager().reset();
Logger globalLogger = Logger.getLogger(java.util.logging.Logger.GLOBAL_LOGGER_NAME);
globalLogger.setLevel(java.util.logging.Level.OFF);
}
/**
* Create a basic PDPConfig object from a set of PolicyFinderModule
*
* @param policyModules
* @return
*/
private PDPConfig createPDPConfig(
Set<PolicyFinderModule> policyModules) {
AttributeFinder attributeFinder = new AttributeFinder();
List<AttributeFinderModule> attrModules = new ArrayList<AttributeFinderModule>();
attrModules.add(new CurrentEnvModule());
attrModules.add(new SelectorModule());
attributeFinder.setModules(attrModules);
PolicyFinder policyFinder = new PolicyFinder();
policyFinder.setModules(policyModules);
// ResourceFinder resourceFinder = new ResourceFinder();
// return new PDPConfig(attributeFinder, policyFinder, resourceFinder);
// do not use attribute finder for performance evaluation
return new PDPConfig(null, policyFinder, null);
}
private void initGenerator() {
generator = new RequestGenerator23();
String[] ACTION_TYPE_VALUES = {
"create",
"delete",
"read",
"write"
};
String[] RESOURCE_CLASS_VALUES = {
"conferenceInfo_rc",
"conference_rc",
"isMeetingFlag_rc",
"paper-assignments_rc",
"paper-conflicts_rc",
"paper-decision_rc",
"paper_rc",
"paper-review-content-commentsAll_rc",
"paper-review-content-commentsPc_rc",
"paper-review-content-rating_rc",
"paper-review-content_rc",
"paper-review-info_rc",
"paper-review-info-reviewer_rc",
"paper-review-info-submissionStatus_rc",
"paper-review_rc",
"paper-submission-file_rc",
"paper-submission-info_rc",
"paper-submission_rc",
"pcMember-assignmentCount_rc",
"pcMember-assignments_rc",
"pcMember-conflicts_rc",
"pcMember-info-isChairFlag_rc",
"pcMember-info-password_rc",
"pcMember-info_rc",
"pcMember_rc"
};
generator.addSubjectAttribute("role", "http://www.w3.org/2001/XMLSchema#string", Arrays.asList(new String[]{"admin", "pc-chair", "pc-member", "subreviewer"}));
generator.addSubjectAttribute("subjReviewsThisResPaper", "http://www.w3.org/2001/XMLSchema#string", Arrays.asList(new String[]{"true", "false"}));
generator.addSubjectAttribute("isSubjectsMeeting", "http://www.w3.org/2001/XMLSchema#string", Arrays.asList(new String[]{"true", "false"}));
generator.addSubjectAttribute("isEq-subjUserId-resUserId", "http://www.w3.org/2001/XMLSchema#string", Arrays.asList(new String[]{"true", "false"}));
generator.addSubjectAttribute("isMeeting", "http://www.w3.org/2001/XMLSchema#string", Arrays.asList(new String[]{"true", "false"}));
generator.addSubjectAttribute("isConflicted", "http://www.w3.org/2001/XMLSchema#string", Arrays.asList(new String[]{"true", "false"}));
generator.addSubjectAttribute("isReviewContentInPlace", "http://www.w3.org/2001/XMLSchema#string", Arrays.asList(new String[]{"true", "false"}));
generator.addSubjectAttribute("hasSubmittedReviewForResPaper", "http://www.w3.org/2001/XMLSchema#string", Arrays.asList(new String[]{"true", "false"}));
generator.addResourceAttribute("isEq-meetingPaper-resId", "http://www.w3.org/2001/XMLSchema#string", Arrays.asList(new String[]{"true", "false"}));
generator.addResourceAttribute("isPending", "http://www.w3.org/2001/XMLSchema#string", Arrays.asList(new String[]{"true", "false"}));
generator.addResourceAttribute("isSeeUnassignedAllowed", "http://www.w3.org/2001/XMLSchema#string", Arrays.asList(new String[]{"true", "false"}));
generator.addResourceAttribute("phase", "http://www.w3.org/2001/XMLSchema#string", Arrays.asList(new String[]{"discussion", "non-discussion"}));
generator.addResourceAttribute("urn:oasis:names:tc:xacml:1.0:resource:resource-id",
"http://www.w3.org/2001/XMLSchema#string", Arrays.asList(RESOURCE_CLASS_VALUES));
generator.addActionAttribute("urn:oasis:names:tc:xacml:1.0:action:action-id",
"http://www.w3.org/2001/XMLSchema#string", Arrays.asList(ACTION_TYPE_VALUES));
}
}