|
1 | 1 | package io.okro.kafka;
|
2 | 2 |
|
3 |
| -import java.util.concurrent.TimeUnit; |
| 3 | +import org.apache.kafka.common.security.auth.KafkaPrincipal; |
| 4 | +import org.apache.kafka.common.security.auth.PlaintextAuthenticationContext; |
| 5 | +import org.apache.kafka.common.security.auth.SslAuthenticationContext; |
| 6 | +import org.junit.Test; |
4 | 7 |
|
5 |
| -import java.io.ByteArrayInputStream; |
| 8 | +import javax.net.ssl.SSLPeerUnverifiedException; |
| 9 | +import javax.net.ssl.SSLSession; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.net.InetAddress; |
| 12 | +import java.net.UnknownHostException; |
6 | 13 | import java.security.cert.Certificate;
|
| 14 | +import java.security.cert.CertificateException; |
7 | 15 | import java.security.cert.CertificateFactory;
|
8 | 16 | import java.security.cert.X509Certificate;
|
9 |
| -import java.net.InetAddress; |
10 |
| -import javax.net.ssl.SSLSession; |
11 |
| - |
12 |
| -import org.apache.kafka.common.security.auth.*; |
13 | 17 |
|
14 |
| -import org.apache.commons.io.IOUtils; |
15 |
| - |
16 |
| -import org.easymock.EasyMock; |
17 |
| -import org.easymock.EasyMockSupport; |
18 |
| -import org.junit.Test; |
19 | 18 | import static org.junit.Assert.assertEquals;
|
20 |
| -import static org.junit.Assert.assertNotNull; |
21 |
| - |
22 |
| -public class SpiffePrincipalBuilderTest extends EasyMockSupport { |
| 19 | +import static org.mockito.Mockito.mock; |
| 20 | +import static org.mockito.Mockito.when; |
23 | 21 |
|
24 |
| - private X509Certificate getResourceAsCert(String resourcePath) |
25 |
| - throws java.io.IOException, java.security.cert.CertificateException { |
| 22 | +public class SpiffePrincipalBuilderTest { |
26 | 23 |
|
| 24 | + private SslAuthenticationContext mockedSslContext(String certPath) throws CertificateException, SSLPeerUnverifiedException, UnknownHostException { |
| 25 | + // load cert |
27 | 26 | ClassLoader classLoader = getClass().getClassLoader();
|
28 |
| - try { |
29 |
| - // Read cert |
30 |
| - ByteArrayInputStream certInputStream = |
31 |
| - new ByteArrayInputStream(IOUtils.toByteArray(classLoader.getResourceAsStream(resourcePath))); |
32 |
| - |
33 |
| - // Parse as X509 certificate |
34 |
| - CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); |
35 |
| - return (X509Certificate) certificateFactory.generateCertificate(certInputStream); |
36 |
| - |
37 |
| - } catch (java.io.IOException | java.security.cert.CertificateException e) { |
38 |
| - System.out.println("Problem with reading the certificate file. " + e.toString()); |
39 |
| - throw e; |
40 |
| - } |
| 27 | + InputStream in = classLoader.getResourceAsStream(certPath); |
| 28 | + CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); |
| 29 | + X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(in); |
| 30 | + |
| 31 | + // mock ssl session |
| 32 | + SSLSession session = mock(SSLSession.class); |
| 33 | + when(session.getPeerCertificates()).thenReturn(new Certificate[]{cert}); |
| 34 | + return new SslAuthenticationContext(session, InetAddress.getLocalHost()); |
41 | 35 | }
|
42 | 36 |
|
| 37 | + /** |
| 38 | + * X509 V3 with a SPIFFE-based SAN extension. |
| 39 | + * Should result in 'SPIFFE:[spiffe://uri]' |
| 40 | + */ |
43 | 41 | @Test
|
44 |
| - public void TestSubjectOnlyCert() { |
45 |
| - // Tests an X509 V1 certificate with no SAN extension |
| 42 | + public void TestSpiffeCert() throws CertificateException, SSLPeerUnverifiedException, UnknownHostException { |
| 43 | + SslAuthenticationContext context = mockedSslContext("spiffe-cert.pem"); |
| 44 | + KafkaPrincipal principal = new SpiffePrincipalBuilder().build(context); |
46 | 45 |
|
47 |
| - try { |
48 |
| - X509Certificate cert = getResourceAsCert("subject-only-cert.pem"); |
49 |
| - |
50 |
| - // Mock SSLSession getPeerCertificates(), we bypass alllll the handshake parts because... out of scope. |
51 |
| - SSLSession session = mock(SSLSession.class); |
52 |
| - EasyMock.expect(session.getPeerCertificates()).andReturn(new Certificate[] {cert}); |
53 |
| - |
54 |
| - replayAll(); |
55 |
| - |
56 |
| - // Build KafkaPrincipal |
57 |
| - SpiffePrincipalBuilder builder = new SpiffePrincipalBuilder(); |
58 |
| - |
59 |
| - KafkaPrincipal principal = builder.build( |
60 |
| - new SslAuthenticationContext(session, InetAddress.getLocalHost())); |
61 |
| - |
62 |
| - // Identity type should be "User" |
63 |
| - assertEquals(KafkaPrincipal.USER_TYPE, principal.getPrincipalType()); |
64 |
| - |
65 |
| - // Identity should be a string |
66 |
| - assertNotNull(principal.getName()); |
67 |
| - |
68 |
| - System.out.println("Principal: " + principal.toString()); |
69 |
| - |
70 |
| - } catch (java.io.IOException | java.security.cert.CertificateException e) { |
71 |
| - System.out.println("Problem with reading the certificate file. " + e.toString()); |
72 |
| - } |
| 46 | + assertEquals("SPIFFE", principal.getPrincipalType()); |
| 47 | + assertEquals(principal.getName(), "spiffe://srv1.okro.io"); |
73 | 48 | }
|
74 | 49 |
|
| 50 | + /** |
| 51 | + * X509 V1 certificate with no SAN extension. |
| 52 | + * Should fall back to 'User:CN=[CN]' |
| 53 | + */ |
75 | 54 | @Test
|
76 |
| - public void TestSpiffeCert() { |
77 |
| - // Tests an X509 V3 with SAN extension holding a SPIFFE ID |
78 |
| - |
79 |
| - try { |
80 |
| - X509Certificate cert = getResourceAsCert("spiffe-cert.pem"); |
81 |
| - |
82 |
| - // Mock SSLSession getPeerCertificates(), we bypass alllll the handshake parts because... out of scope. |
83 |
| - SSLSession session = mock(SSLSession.class); |
84 |
| - EasyMock.expect(session.getPeerCertificates()).andReturn(new Certificate[] {cert}); |
| 55 | + public void TestSubjectOnlyCert() throws CertificateException, SSLPeerUnverifiedException, UnknownHostException { |
| 56 | + SslAuthenticationContext context = mockedSslContext("subject-only-cert.pem"); |
| 57 | + KafkaPrincipal principal = new SpiffePrincipalBuilder().build(context); |
85 | 58 |
|
86 |
| - replayAll(); |
87 |
| - |
88 |
| - // Build KafkaPrincipal |
89 |
| - SpiffePrincipalBuilder builder = new SpiffePrincipalBuilder(); |
90 |
| - |
91 |
| - KafkaPrincipal principal = builder.build( |
92 |
| - new SslAuthenticationContext(session, InetAddress.getLocalHost())); |
93 |
| - |
94 |
| - // Identity type should be "SPIFFE" |
95 |
| - assertEquals("SPIFFE", principal.getPrincipalType()); |
96 |
| - |
97 |
| - // Identity should be a string |
98 |
| - assertNotNull(principal.getName()); |
99 |
| - |
100 |
| - System.out.println("Principal: " + principal.toString()); |
101 |
| - |
102 |
| - } catch (java.io.IOException | java.security.cert.CertificateException e) { |
103 |
| - System.out.println("Problem with reading the certificate file. " + e.toString()); |
104 |
| - } |
| 59 | + assertEquals(KafkaPrincipal.USER_TYPE, principal.getPrincipalType()); |
| 60 | + assertEquals(principal.getName(), "CN=srv2,OU=architects,O=okro.io,L=Tel-Aviv,ST=Tel-Aviv,C=IL"); |
105 | 61 | }
|
106 | 62 |
|
| 63 | + /** |
| 64 | + * X509 V3 with a non-SPIFFE SAN extension. |
| 65 | + * Should fall back to 'User:CN=[CN]' |
| 66 | + */ |
107 | 67 | @Test
|
108 |
| - public void TestSanNoSpiffeCert() { |
109 |
| - // Tests an X509 V3 with SAN extension holding a regular FQDN |
110 |
| - |
111 |
| - try { |
112 |
| - X509Certificate cert = getResourceAsCert("san-no-spiffe-cert.pem"); |
113 |
| - |
114 |
| - // Mock SSLSession getPeerCertificates(), we bypass alllll the handshake parts because... out of scope. |
115 |
| - SSLSession session = mock(SSLSession.class); |
116 |
| - EasyMock.expect(session.getPeerCertificates()).andReturn(new Certificate[] {cert}); |
117 |
| - |
118 |
| - replayAll(); |
119 |
| - |
120 |
| - // Build KafkaPrincipal |
121 |
| - SpiffePrincipalBuilder builder = new SpiffePrincipalBuilder(); |
122 |
| - |
123 |
| - KafkaPrincipal principal = builder.build( |
124 |
| - new SslAuthenticationContext(session, InetAddress.getLocalHost())); |
| 68 | + public void TestSanNoSpiffeCert() throws CertificateException, SSLPeerUnverifiedException, UnknownHostException { |
| 69 | + SslAuthenticationContext context = mockedSslContext("san-no-spiffe-cert.pem"); |
| 70 | + KafkaPrincipal principal = new SpiffePrincipalBuilder().build(context); |
125 | 71 |
|
126 |
| - // Identity type should be "User" |
127 |
| - assertEquals(KafkaPrincipal.USER_TYPE, principal.getPrincipalType()); |
128 |
| - |
129 |
| - // Identity should be a string |
130 |
| - assertNotNull(principal.getName()); |
131 |
| - |
132 |
| - System.out.println("Principal: " + principal.toString()); |
133 |
| - |
134 |
| - } catch (java.io.IOException | java.security.cert.CertificateException e) { |
135 |
| - System.out.println("Problem with reading the certificate file. " + e.toString()); |
136 |
| - } |
| 72 | + assertEquals(KafkaPrincipal.USER_TYPE, principal.getPrincipalType()); |
| 73 | + assertEquals(principal.getName(), "CN=srv3,OU=architects,O=okro.io,L=Tel-Aviv,ST=Tel-Aviv,C=IL"); |
137 | 74 | }
|
138 | 75 |
|
| 76 | + /** |
| 77 | + * Non-SSL context. |
| 78 | + * Should be unauthenticated. |
| 79 | + */ |
139 | 80 | @Test
|
140 | 81 | public void TestNoSSLContext() throws java.net.UnknownHostException {
|
141 |
| - // Tests non-SSL context behavior |
142 |
| - |
143 |
| - SpiffePrincipalBuilder builder = new SpiffePrincipalBuilder(); |
144 |
| - |
145 |
| - KafkaPrincipal principal = builder.build( |
146 |
| - new PlaintextAuthenticationContext(InetAddress.getLocalHost())); |
| 82 | + PlaintextAuthenticationContext context = new PlaintextAuthenticationContext(InetAddress.getLocalHost()); |
| 83 | + KafkaPrincipal principal = new SpiffePrincipalBuilder().build(context); |
147 | 84 |
|
148 |
| - // Identity type should be KafkaPrincipal.ANONYMOUS |
149 | 85 | assertEquals(KafkaPrincipal.ANONYMOUS, principal);
|
150 |
| - |
151 |
| - System.out.println("Principal: " + principal.toString()); |
152 |
| - } |
153 |
| - |
154 |
| - @Test |
155 |
| - public void TestAwareness() throws InterruptedException { |
156 |
| - // Tests a reviewer's awareness |
157 |
| - TimeUnit.SECONDS.sleep(1); |
158 |
| - |
159 |
| - // Identity type should be KafkaPrincipal.ANONYMOUS |
160 |
| - assertEquals(42, 42); |
161 | 86 | }
|
162 | 87 | }
|
0 commit comments