|
1 | 1 | package io.spiffe.svid.x509svid; |
2 | 2 |
|
| 3 | +import io.spiffe.exception.InvalidSpiffeIdException; |
3 | 4 | import io.spiffe.exception.X509SvidException; |
4 | 5 | import io.spiffe.spiffeid.SpiffeId; |
5 | 6 | import io.spiffe.spiffeid.TrustDomain; |
| 7 | +import io.spiffe.utils.CertAndKeyPair; |
6 | 8 | import org.junit.jupiter.api.Test; |
7 | 9 | import org.junit.jupiter.params.ParameterizedTest; |
8 | 10 | import org.junit.jupiter.params.provider.Arguments; |
|
18 | 20 | import java.util.stream.Stream; |
19 | 21 |
|
20 | 22 | import static io.spiffe.utils.TestUtils.toUri; |
| 23 | +import static io.spiffe.utils.X509CertificateTestUtils.createCertificate; |
| 24 | +import static io.spiffe.utils.X509CertificateTestUtils.createRootCA; |
21 | 25 | import static org.junit.jupiter.api.Assertions.assertEquals; |
22 | 26 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
23 | 28 | import static org.junit.jupiter.api.Assertions.fail; |
24 | 29 |
|
25 | 30 | class X509SvidTest { |
@@ -315,6 +320,38 @@ void testGetChainArray() throws URISyntaxException, X509SvidException { |
315 | 320 | assertEquals(x509Svid.getChain().get(1), x509CertificatesArray[1]); |
316 | 321 | } |
317 | 322 |
|
| 323 | + @Test |
| 324 | + void parseRaw_leafSpiffeIdWithoutPath_isRejected() throws Exception { |
| 325 | + CertAndKeyPair rootCa = createRootCA("C = US, O = SPIFFE", "spiffe://example.org"); |
| 326 | + CertAndKeyPair leaf = createCertificate("C = US, O = SPIRE", "C = US, O = SPIFFE", "spiffe://example.org", rootCa, false); |
| 327 | + |
| 328 | + byte[] certBytes = leaf.getCertificate().getEncoded(); |
| 329 | + byte[] keyBytes = leaf.getKeyPair().getPrivate().getEncoded(); |
| 330 | + |
| 331 | + X509SvidException exception = assertThrows( |
| 332 | + X509SvidException.class, |
| 333 | + () -> X509Svid.parseRaw(certBytes, keyBytes) |
| 334 | + ); |
| 335 | + |
| 336 | + assertEquals("Leaf certificate SPIFFE ID must have a non-root path", exception.getMessage()); |
| 337 | + } |
| 338 | + |
| 339 | + @Test |
| 340 | + void parseRaw_leafSpiffeIdWithRootOnlyPath_isRejected() throws Exception { |
| 341 | + CertAndKeyPair rootCa = createRootCA("C = US, O = SPIFFE", "spiffe://example.org"); |
| 342 | + CertAndKeyPair leaf = createCertificate("C = US, O = SPIRE", "C = US, O = SPIFFE", "spiffe://example.org/", rootCa, false); |
| 343 | + |
| 344 | + byte[] certBytes = leaf.getCertificate().getEncoded(); |
| 345 | + byte[] keyBytes = leaf.getKeyPair().getPrivate().getEncoded(); |
| 346 | + |
| 347 | + InvalidSpiffeIdException exception = assertThrows( |
| 348 | + InvalidSpiffeIdException.class, |
| 349 | + () -> X509Svid.parseRaw(certBytes, keyBytes) |
| 350 | + ); |
| 351 | + |
| 352 | + assertEquals("Path cannot have a trailing slash", exception.getMessage()); |
| 353 | + } |
| 354 | + |
318 | 355 | @ParameterizedTest |
319 | 356 | @MethodSource("provideX509SvidScenarios") |
320 | 357 | void parseX509Svid(TestCase testCase) { |
|
0 commit comments