| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.gsi; |
| 16 |
|
| 17 |
import static org.junit.Assert.assertFalse; |
| 18 |
import static org.junit.Assert.assertTrue; |
| 19 |
|
| 20 |
import java.io.StringReader; |
| 21 |
import java.util.List; |
| 22 |
import java.util.Map; |
| 23 |
import java.util.Vector; |
| 24 |
import java.util.regex.Pattern; |
| 25 |
|
| 26 |
import javax.security.auth.x500.X500Principal; |
| 27 |
|
| 28 |
import org.globus.gsi.SigningPolicy; |
| 29 |
import org.globus.gsi.testutils.FileSetupUtil; |
| 30 |
import org.junit.After; |
| 31 |
import org.junit.Before; |
| 32 |
import org.junit.Test; |
| 33 |
|
| 34 |
public class SigningPolicyParserTest { |
| 35 |
|
| 36 |
FileSetupUtil successFile; |
| 37 |
FileSetupUtil singleAllowedDn; |
| 38 |
FileSetupUtil[] tabTestFiles; |
| 39 |
|
| 40 |
@Before |
| 41 |
public void setup() throws Exception { |
| 42 |
|
| 43 |
this.successFile = |
| 44 |
new FileSetupUtil( |
| 45 |
"certificateUtilTest/samplePolicy.signing_policy"); |
| 46 |
|
| 47 |
this.singleAllowedDn = |
| 48 |
new FileSetupUtil("certificateUtilTest/5aba75cb.signing_policy"); |
| 49 |
|
| 50 |
this.tabTestFiles = new FileSetupUtil[3]; |
| 51 |
this.tabTestFiles[0] = |
| 52 |
new FileSetupUtil("certificateUtilTest/afe55e66.signing_policy"); |
| 53 |
this.tabTestFiles[1] = |
| 54 |
new FileSetupUtil("certificateUtilTest/cf4ba8c8.signing_policy"); |
| 55 |
this.tabTestFiles[2] = |
| 56 |
new FileSetupUtil("certificateUtilTest/49f18420.signing_policy"); |
| 57 |
} |
| 58 |
|
| 59 |
@Test |
| 60 |
public void testPatternMatching() throws Exception { |
| 61 |
|
| 62 |
// test getPattern method |
| 63 |
// no wildcards or question marks |
| 64 |
String patternStr = "CN=abcdefgh"; |
| 65 |
String patternR = (SigningPolicyParser.getPattern(patternStr)) |
| 66 |
.pattern(); |
| 67 |
assertTrue("CN=abcdefgh".equals(patternR)); |
| 68 |
|
| 69 |
// first character wildcard and question marks |
| 70 |
String pattern1Str = "CN=*def?gh?"; |
| 71 |
Pattern pattern1 = SigningPolicyParser.getPattern(pattern1Str); |
| 72 |
String pattern1R = pattern1.pattern(); |
| 73 |
assertTrue(("CN=" + SigningPolicyParser.WILDCARD_PATTERN + "def" + |
| 74 |
SigningPolicyParser.SINGLE_PATTERN + "gh" + |
| 75 |
SigningPolicyParser.SINGLE_PATTERN).equals(pattern1R)); |
| 76 |
|
| 77 |
// only wild cards |
| 78 |
String pattern2Str = "/CN=abc*def*gh"; |
| 79 |
Pattern pattern2 = SigningPolicyParser.getPattern(pattern2Str); |
| 80 |
String pattern2R = pattern2.pattern(); |
| 81 |
assertTrue( |
| 82 |
("/CN=abc" + SigningPolicyParser.WILDCARD_PATTERN + "def" + |
| 83 |
SigningPolicyParser.WILDCARD_PATTERN + "gh").equals( |
| 84 |
pattern2R)); |
| 85 |
|
| 86 |
// test isValidDN methods |
| 87 |
// Add patern2, wildcards in middle |
| 88 |
Vector<Pattern> allowed = new Vector(); |
| 89 |
allowed.add(pattern2); |
| 90 |
X500Principal fooPrincipal = new X500Principal("CN=foo"); |
| 91 |
SigningPolicy policy = new SigningPolicy(fooPrincipal, allowed); |
| 92 |
|
| 93 |
X500Principal subject21 = new X500Principal("CN=abc12DEF34defdef56gh"); |
| 94 |
assertTrue(policy.isValidSubject(subject21)); |
| 95 |
|
| 96 |
X500Principal subject22 = |
| 97 |
new X500Principal("CN=123abc12def34defdef56gh"); |
| 98 |
assertFalse(policy.isValidSubject(subject22)); |
| 99 |
|
| 100 |
X500Principal subject23 = |
| 101 |
new X500Principal("CN=abc12def34defdef56gh123"); |
| 102 |
assertFalse(policy.isValidSubject(subject23)); |
| 103 |
|
| 104 |
// wildcard as first and last character |
| 105 |
String pattern3Str = "*abc*def*gh*"; |
| 106 |
Pattern pattern3 = SigningPolicyParser.getPattern(pattern3Str); |
| 107 |
allowed.clear(); |
| 108 |
allowed.add(pattern3); |
| 109 |
policy = new SigningPolicy(fooPrincipal, allowed); |
| 110 |
|
| 111 |
X500Principal subject31 = new X500Principal("CN=ABC12def34defdef56gh"); |
| 112 |
assertTrue(policy.isValidSubject(subject31)); |
| 113 |
|
| 114 |
X500Principal subject32 = |
| 115 |
new X500Principal("CN=123abc12def34defdef56gh555"); |
| 116 |
assertTrue(policy.isValidSubject(subject32)); |
| 117 |
|
| 118 |
// use of space and slashes, from old signing policy file |
| 119 |
String pattern4Str = "/C=US/O=Globus/*"; |
| 120 |
Pattern pattern4 = SigningPolicyParser.getPattern(pattern4Str); |
| 121 |
allowed.clear(); |
| 122 |
allowed.add(pattern4); |
| 123 |
|
| 124 |
policy = new SigningPolicy(fooPrincipal, allowed); |
| 125 |
|
| 126 |
X500Principal subject41 = |
| 127 |
new X500Principal( |
| 128 |
"CN=Globus Certification Authority, O=Globus, C=US"); |
| 129 |
assertTrue(policy.isValidSubject(subject41)); |
| 130 |
|
| 131 |
// wildcard as first character, question mark |
| 132 |
String pattern5Str = "/*C=US/O=Globus/CN=foo-?/CN=*"; |
| 133 |
Pattern pattern5 = SigningPolicyParser.getPattern(pattern5Str); |
| 134 |
allowed.clear(); |
| 135 |
allowed.add(pattern5); |
| 136 |
policy = new SigningPolicy(fooPrincipal, allowed); |
| 137 |
|
| 138 |
X500Principal subject51 = |
| 139 |
new X500Principal( |
| 140 |
"CN=Globus Certification Authority, O=Globus, O=US"); |
| 141 |
assertFalse(policy.isValidSubject(subject51)); |
| 142 |
X500Principal subject52 = |
| 143 |
new X500Principal( |
| 144 |
"CN=test space,CN=a12b,CN=foo-1,O=Globus,C=US,C=SOME"); |
| 145 |
assertTrue(policy.isValidSubject(subject52)); |
| 146 |
X500Principal subject53 = |
| 147 |
new X500Principal("CN=,CN=foo-k,O=Globus,C=US"); |
| 148 |
assertTrue(policy.isValidSubject(subject53)); |
| 149 |
X500Principal subject54 = |
| 150 |
new X500Principal("CN= , CN=foo-1, O=Globus, C=US"); |
| 151 |
assertTrue(policy.isValidSubject(subject54)); |
| 152 |
|
| 153 |
X500Principal subject55 = |
| 154 |
new X500Principal("C=US,O=Globus,CN=foo-123,CN="); |
| 155 |
assertFalse(policy.isValidSubject(subject55)); |
| 156 |
|
| 157 |
// multiple question mark with punctuation |
| 158 |
String pattern6Str = "/C=US/O=global/CN=*/CN=user-??"; |
| 159 |
Pattern pattern6 = SigningPolicyParser.getPattern(pattern6Str); |
| 160 |
allowed.clear(); |
| 161 |
allowed.add(pattern6); |
| 162 |
policy = new SigningPolicy(fooPrincipal, allowed); |
| 163 |
|
| 164 |
X500Principal subject61 = |
| 165 |
new X500Principal("CN=user-12,CN=foo,O=Globus,C=US"); |
| 166 |
assertFalse(policy.isValidSubject(subject61)); |
| 167 |
X500Principal subject62 = |
| 168 |
new X500Principal("CN=user-12,CN=foo,O=Global,C=US"); |
| 169 |
assertTrue(policy.isValidSubject(subject62)); |
| 170 |
X500Principal subject63 = |
| 171 |
new X500Principal("CN=user-12,CN=bar 1,CN=foo ,O=global,C=US"); |
| 172 |
assertTrue(policy.isValidSubject(subject63)); |
| 173 |
|
| 174 |
// add multiple patterns and test validity if atleast one matches |
| 175 |
String pattern7Str = "/C=US/O=Globus/CN=*/CN=user-??"; |
| 176 |
Pattern pattern7 = SigningPolicyParser.getPattern(pattern7Str); |
| 177 |
allowed.add(pattern7); |
| 178 |
policy = new SigningPolicy(fooPrincipal, allowed); |
| 179 |
|
| 180 |
X500Principal subject71 = |
| 181 |
new X500Principal("CN=user-12, CN=bar 1, CN=foo , O=Globus,C=US"); |
| 182 |
assertTrue(policy.isValidSubject(subject71)); |
| 183 |
assertTrue(policy.isValidSubject(subject63)); |
| 184 |
} |
| 185 |
|
| 186 |
// JGLOBUS-103 |
| 187 |
@Test |
| 188 |
public void testFileSuccess() throws Exception { |
| 189 |
|
| 190 |
this.successFile.copyFileToTemp(); |
| 191 |
|
| 192 |
SigningPolicyParser parser = new SigningPolicyParser(); |
| 193 |
Map<X500Principal, SigningPolicy> map = |
| 194 |
parser.parse(this.successFile.getAbsoluteFilename()); |
| 195 |
|
| 196 |
assertTrue(map != null); |
| 197 |
|
| 198 |
SigningPolicy policy = |
| 199 |
map.get(new X500Principal( |
| 200 |
"CN=Globus Certification Authority,O=Globus,C=US")); |
| 201 |
|
| 202 |
assertTrue(policy != null); |
| 203 |
List<Pattern> allowedDN = policy.getAllowedDNs(); |
| 204 |
assertTrue(allowedDN != null); |
| 205 |
assertTrue(allowedDN.size() == 2); |
| 206 |
|
| 207 |
List<String> patterns = new Vector(2); |
| 208 |
patterns.add((allowedDN.get(0)).pattern()); |
| 209 |
patterns.add((allowedDN.get(1)).pattern()); |
| 210 |
|
| 211 |
// given the getPattern method is already tested, assuming it |
| 212 |
// works here. |
| 213 |
Pattern p1 = SigningPolicyParser.getPattern("/C=us/O=Globus/*"); |
| 214 |
assertTrue(patterns.contains(p1.pattern())); |
| 215 |
p1 = SigningPolicyParser.getPattern("/C=US/O=Globus/*"); |
| 216 |
assertTrue(patterns.contains(p1.pattern())); |
| 217 |
p1 = SigningPolicyParser |
| 218 |
.getPattern("/C=us/O=National Computational Science Alliance/*"); |
| 219 |
assertFalse(patterns.contains(p1.pattern())); |
| 220 |
|
| 221 |
policy = map.get(new X500Principal( |
| 222 |
"CN=Globus Certification Authority,O=National Computational Science Alliance,C=US")); |
| 223 |
assertTrue(policy != null); |
| 224 |
allowedDN = policy.getAllowedDNs(); |
| 225 |
assertTrue(allowedDN != null); |
| 226 |
assertTrue(allowedDN.size() == 1); |
| 227 |
patterns.clear(); |
| 228 |
patterns.add(((Pattern) allowedDN.get(0)).pattern()); |
| 229 |
p1 = SigningPolicyParser |
| 230 |
.getPattern("/C=us/O=National Computational Science Alliance/*"); |
| 231 |
assertTrue(patterns.contains(p1.pattern())); |
| 232 |
|
| 233 |
// test file with single allows DN without double quotes |
| 234 |
this.singleAllowedDn.copyFileToTemp(); |
| 235 |
map.clear(); |
| 236 |
map = parser.parse(this.singleAllowedDn.getAbsoluteFilename()); |
| 237 |
|
| 238 |
policy = map.get(new X500Principal( |
| 239 |
"OU=Certification Authority,O=National Computational Science Alliance,C=US")); |
| 240 |
|
| 241 |
assertTrue(policy != null); |
| 242 |
allowedDN.clear(); |
| 243 |
allowedDN = policy.getAllowedDNs(); |
| 244 |
assertTrue(allowedDN != null); |
| 245 |
assertTrue(allowedDN.size() == 1); |
| 246 |
|
| 247 |
patterns = new Vector(1); |
| 248 |
patterns.add(((Pattern) allowedDN.get(0)).pattern()); |
| 249 |
|
| 250 |
p1 = SigningPolicyParser |
| 251 |
.getPattern("/C=US/O=National Computational Science Alliance/*"); |
| 252 |
assertTrue(patterns.contains(p1.pattern())); |
| 253 |
} |
| 254 |
|
| 255 |
@Test |
| 256 |
public void testFilesWithTab() throws Exception { |
| 257 |
|
| 258 |
this.tabTestFiles[0].copyFileToTemp(); |
| 259 |
SigningPolicyParser parser = new SigningPolicyParser(); |
| 260 |
|
| 261 |
Map<X500Principal, SigningPolicy> map = |
| 262 |
parser.parse(this.tabTestFiles[0].getAbsoluteFilename()); |
| 263 |
|
| 264 |
SigningPolicy policy = |
| 265 |
map.get(new X500Principal("CN=CyGridCA,O=HPCL,O=CyGrid,C=CY")); |
| 266 |
assertTrue(policy != null); |
| 267 |
List<Pattern> allowedDN = policy.getAllowedDNs(); |
| 268 |
assertTrue(allowedDN != null); |
| 269 |
assertTrue(allowedDN.size() == 1); |
| 270 |
|
| 271 |
allowedDN.clear(); |
| 272 |
map.clear(); |
| 273 |
this.tabTestFiles[1].copyFileToTemp(); |
| 274 |
map = parser.parse(this.tabTestFiles[1].getAbsoluteFilename()); |
| 275 |
policy = map.get(new X500Principal("CN=CNRS,O=CNRS,C=FR")); |
| 276 |
assertTrue(policy != null); |
| 277 |
allowedDN = policy.getAllowedDNs(); |
| 278 |
assertTrue(allowedDN != null); |
| 279 |
assertTrue(allowedDN.size() == 2); |
| 280 |
|
| 281 |
Vector patterns = new Vector(2); |
| 282 |
patterns.add(((Pattern) allowedDN.get(0)).pattern()); |
| 283 |
patterns.add(((Pattern) allowedDN.get(1)).pattern()); |
| 284 |
|
| 285 |
// given the getPattern method is already tested, assuming it |
| 286 |
// works here. |
| 287 |
Pattern p1 = SigningPolicyParser |
| 288 |
.getPattern("/C=FR/O=CNRS/CN=CNRS-Projets"); |
| 289 |
assertTrue(patterns.contains(p1.pattern())); |
| 290 |
p1 = SigningPolicyParser.getPattern("/C=FR/O=CNRS/CN=CNRS"); |
| 291 |
assertTrue(patterns.contains(p1.pattern())); |
| 292 |
|
| 293 |
|
| 294 |
allowedDN.clear(); |
| 295 |
map.clear(); |
| 296 |
this.tabTestFiles[2].copyFileToTemp(); |
| 297 |
map = parser.parse(this.tabTestFiles[2].getAbsoluteFilename()); |
| 298 |
|
| 299 |
policy = map.get( |
| 300 |
new X500Principal("CN=INFN Certification Authority,O=INFN,C=IT")); |
| 301 |
assertTrue(policy != null); |
| 302 |
allowedDN = policy.getAllowedDNs(); |
| 303 |
assertTrue(allowedDN != null); |
| 304 |
assertTrue(allowedDN.size() == 2); |
| 305 |
|
| 306 |
patterns.clear(); |
| 307 |
patterns.add(((Pattern) allowedDN.get(0)).pattern()); |
| 308 |
patterns.add(((Pattern) allowedDN.get(1)).pattern()); |
| 309 |
|
| 310 |
// given the getPattern method is already tested, assuming it |
| 311 |
// works here. |
| 312 |
p1 = SigningPolicyParser.getPattern("/C=it/O=INFN/*"); |
| 313 |
assertTrue(patterns.contains(p1.pattern())); |
| 314 |
p1 = SigningPolicyParser.getPattern("/C=IT/O=INFN/*"); |
| 315 |
assertTrue(patterns.contains(p1.pattern())); |
| 316 |
} |
| 317 |
|
| 318 |
@Test(expected = SigningPolicyException.class) |
| 319 |
public void testFileFailure() throws Exception { |
| 320 |
SigningPolicyParser parser = new SigningPolicyParser(); |
| 321 |
parser.parse("Foo"); |
| 322 |
} |
| 323 |
|
| 324 |
@Test |
| 325 |
public void testParsingFailure() throws Exception { |
| 326 |
|
| 327 |
SigningPolicyParser parser = new SigningPolicyParser(); |
| 328 |
|
| 329 |
// not x509 |
| 330 |
String error1 = |
| 331 |
"access_id_CA notX509 '/C=US/O=Globus/CN=Globus " + |
| 332 |
"Certification Authority'\n pos_rights globus CA:sign\n" + |
| 333 |
" cond_subjects globus '\"/C=us/O=Globus/*\" \"/C=US/O=Globus/*\"'"; |
| 334 |
|
| 335 |
|
| 336 |
Map<X500Principal, SigningPolicy> map = null; |
| 337 |
boolean worked = false; |
| 338 |
try { |
| 339 |
map = parser.parse(new StringReader(error1)); |
| 340 |
} catch (IllegalArgumentException e) { |
| 341 |
worked = true; |
| 342 |
} |
| 343 |
assertTrue(worked); |
| 344 |
|
| 345 |
// not globus |
| 346 |
error1 = |
| 347 |
"access_id_CA X509 '/C=US/O=Globus/CN=Globus " + |
| 348 |
"Certification Authority'\n pos_rights notglobus " + |
| 349 |
"CA:sign\n cond_subjects globus '\"/C=us/O=Globus/*\" " + |
| 350 |
"\"/C=US/O=Globus/*\"'"; |
| 351 |
map = parser.parse(new StringReader(error1)); |
| 352 |
|
| 353 |
|
| 354 |
// order of rights matter, atleast one positive right implies |
| 355 |
// allowed DN |
| 356 |
error1 = |
| 357 |
"access_id_CA X509 '/C=US/O=Globus/CN=Globus Certification " + |
| 358 |
"Authority'\n pos_rights globus CA:sign\n cond_subjects" + |
| 359 |
" globus '\"/C=us/O=Globus/*\" \"/C=US/O=Globus/*\"' \n " + |
| 360 |
"neg_rights notglobus some:right"; |
| 361 |
map = parser.parse(new StringReader(error1)); |
| 362 |
SigningPolicy policy = map.get(new X500Principal( |
| 363 |
"CN=Globus Certification Authority,O=Globus,C=US")); |
| 364 |
assertTrue(policy != null); |
| 365 |
List<Pattern> allowedDN = policy.getAllowedDNs(); |
| 366 |
assertTrue(allowedDN != null); |
| 367 |
assertTrue(allowedDN.size() == 2); |
| 368 |
|
| 369 |
// incorrect start |
| 370 |
error1 = |
| 371 |
"X509 '/C=US/O=Globus/CN=Globus Certification Authority'\n" + |
| 372 |
" pos_rights notglobus CA:sign\n cond_subjects " + |
| 373 |
"globus \'\"/C=us/O=Globus/*\" \"/C=US/O=Globus/*\"\'"; |
| 374 |
boolean exception = false; |
| 375 |
try { |
| 376 |
map = parser.parse(new StringReader(error1)); |
| 377 |
} catch (SigningPolicyException exp) { |
| 378 |
exception = true; |
| 379 |
} |
| 380 |
assertTrue(exception); |
| 381 |
|
| 382 |
// erroneous quote |
| 383 |
error1 = |
| 384 |
"access_id_CA X509 '/C=US/O=Globus/CN=Globus Certification " + |
| 385 |
"Authority\n pos_rights notglobus CA:sign\n " + |
| 386 |
"cond_subjects globus \'\"/C=us/O=Globus/*\" " + |
| 387 |
"\"/C=US/O=Globus/*\"\'"; |
| 388 |
exception = false; |
| 389 |
try { |
| 390 |
map = parser.parse(new StringReader(error1)); |
| 391 |
} catch (SigningPolicyException exp) { |
| 392 |
if ((exp.getMessage().indexOf("invalid")) != -1) { |
| 393 |
exception = true; |
| 394 |
} |
| 395 |
} |
| 396 |
assertTrue(exception); |
| 397 |
|
| 398 |
// neg rights rather than restrictions |
| 399 |
error1 = |
| 400 |
"access_id_CA X509 '/C=US/O=Globus/CN=Globus " + |
| 401 |
"Certification Authority'\n pos_rights globus " + |
| 402 |
"CA:sign\n neg_rights notglobus some:right"; |
| 403 |
exception = false; |
| 404 |
try { |
| 405 |
map = parser.parse(new StringReader(error1)); |
| 406 |
} catch (SigningPolicyException exp) { |
| 407 |
// if ((exp.getMessage().indexOf("File format is incorrect") != -1) && |
| 408 |
// (exp.getMessage(). |
| 409 |
// indexOf("neg_rights cannot be used here") != -1)) { |
| 410 |
exception = true; |
| 411 |
// } |
| 412 |
} |
| 413 |
assertTrue(exception); |
| 414 |
|
| 415 |
// first pos_rights is all that matters |
| 416 |
error1 = |
| 417 |
"access_id_CA X509 '/C=US/O=Globus/CN=Globus Certification " + |
| 418 |
"Authority'\n pos_rights globus CA:sign\n " + |
| 419 |
"cond_subjects globus '\"/C=us/O=Globus/*\" " + |
| 420 |
"\"/C=US/O=Globus/*\"' \n cond_subjects globus " + |
| 421 |
"'\"/C=us/O=Globus/*\"'"; |
| 422 |
map = parser.parse(new StringReader(error1)); |
| 423 |
policy = map.get(new X500Principal( |
| 424 |
"CN=Globus Certification Authority,O=Globus,C=US")); |
| 425 |
assertTrue(policy != null); |
| 426 |
allowedDN = policy.getAllowedDNs(); |
| 427 |
assertTrue(allowedDN != null); |
| 428 |
assertTrue(allowedDN.size() == 2); |
| 429 |
} |
| 430 |
|
| 431 |
@After |
| 432 |
public void cleanUp() throws Exception { |
| 433 |
|
| 434 |
this.singleAllowedDn.deleteFile(); |
| 435 |
this.successFile.deleteFile(); |
| 436 |
this.tabTestFiles[0].deleteFile(); |
| 437 |
this.tabTestFiles[1].deleteFile(); |
| 438 |
this.tabTestFiles[2].deleteFile(); |
| 439 |
|
| 440 |
} |
| 441 |
} |