-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Traiana/unitests
Unit testing
- Loading branch information
Showing
6 changed files
with
233 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/test/java/io.okro.kafka/SpiffePrincipalBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package io.okro.kafka; | ||
|
||
import org.apache.kafka.common.security.auth.KafkaPrincipal; | ||
import org.apache.kafka.common.security.auth.PlaintextAuthenticationContext; | ||
import org.apache.kafka.common.security.auth.SslAuthenticationContext; | ||
import org.junit.Test; | ||
|
||
import javax.net.ssl.SSLPeerUnverifiedException; | ||
import javax.net.ssl.SSLSession; | ||
import java.io.InputStream; | ||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.security.cert.Certificate; | ||
import java.security.cert.CertificateException; | ||
import java.security.cert.CertificateFactory; | ||
import java.security.cert.X509Certificate; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class SpiffePrincipalBuilderTest { | ||
|
||
private SslAuthenticationContext mockedSslContext(String certPath) throws CertificateException, SSLPeerUnverifiedException, UnknownHostException { | ||
// load cert | ||
ClassLoader classLoader = getClass().getClassLoader(); | ||
InputStream in = classLoader.getResourceAsStream(certPath); | ||
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); | ||
X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(in); | ||
|
||
// mock ssl session | ||
SSLSession session = mock(SSLSession.class); | ||
when(session.getPeerCertificates()).thenReturn(new Certificate[]{cert}); | ||
return new SslAuthenticationContext(session, InetAddress.getLocalHost()); | ||
} | ||
|
||
/** | ||
* X509 V3 with a SPIFFE-based SAN extension. | ||
* Should result in 'SPIFFE:[spiffe://uri]' | ||
*/ | ||
@Test | ||
public void TestSpiffeCert() throws CertificateException, SSLPeerUnverifiedException, UnknownHostException { | ||
SslAuthenticationContext context = mockedSslContext("spiffe-cert.pem"); | ||
KafkaPrincipal principal = new SpiffePrincipalBuilder().build(context); | ||
|
||
assertEquals("SPIFFE", principal.getPrincipalType()); | ||
assertEquals(principal.getName(), "spiffe://srv1.okro.io"); | ||
} | ||
|
||
/** | ||
* X509 V1 certificate with no SAN extension. | ||
* Should fall back to 'User:CN=[CN]' | ||
*/ | ||
@Test | ||
public void TestSubjectOnlyCert() throws CertificateException, SSLPeerUnverifiedException, UnknownHostException { | ||
SslAuthenticationContext context = mockedSslContext("subject-only-cert.pem"); | ||
KafkaPrincipal principal = new SpiffePrincipalBuilder().build(context); | ||
|
||
assertEquals(KafkaPrincipal.USER_TYPE, principal.getPrincipalType()); | ||
assertEquals(principal.getName(), "CN=srv2,OU=architects,O=okro.io,L=Tel-Aviv,ST=Tel-Aviv,C=IL"); | ||
} | ||
|
||
/** | ||
* X509 V3 with a non-SPIFFE SAN extension. | ||
* Should fall back to 'User:CN=[CN]' | ||
*/ | ||
@Test | ||
public void TestSanNoSpiffeCert() throws CertificateException, SSLPeerUnverifiedException, UnknownHostException { | ||
SslAuthenticationContext context = mockedSslContext("san-no-spiffe-cert.pem"); | ||
KafkaPrincipal principal = new SpiffePrincipalBuilder().build(context); | ||
|
||
assertEquals(KafkaPrincipal.USER_TYPE, principal.getPrincipalType()); | ||
assertEquals(principal.getName(), "CN=srv3,OU=architects,O=okro.io,L=Tel-Aviv,ST=Tel-Aviv,C=IL"); | ||
} | ||
|
||
/** | ||
* Non-SSL context. | ||
* Should be unauthenticated. | ||
*/ | ||
@Test | ||
public void TestNoSSLContext() throws java.net.UnknownHostException { | ||
PlaintextAuthenticationContext context = new PlaintextAuthenticationContext(InetAddress.getLocalHost()); | ||
KafkaPrincipal principal = new SpiffePrincipalBuilder().build(context); | ||
|
||
assertEquals(KafkaPrincipal.ANONYMOUS, principal); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIEYTCCAkmgAwIBAgIJAMGzBF0pPMySMA0GCSqGSIb3DQEBCwUAMFoxCzAJBgNV | ||
BAYTAklMMREwDwYDVQQIDAhUZWwtQXZpdjERMA8GA1UEBwwIVGVsLUF2aXYxEDAO | ||
BgNVBAoMB29rcm8uaW8xEzARBgNVBAMMCm9rcm8uaW8gQ0EwHhcNMTgwNzMwMjAy | ||
NjA0WhcNMjgwNzI3MjAyNjA0WjBpMQswCQYDVQQGEwJJTDERMA8GA1UECAwIVGVs | ||
LUF2aXYxETAPBgNVBAcMCFRlbC1Bdml2MRAwDgYDVQQKDAdva3JvLmlvMRMwEQYD | ||
VQQLDAphcmNoaXRlY3RzMQ0wCwYDVQQDDARzcnYzMIIBIjANBgkqhkiG9w0BAQEF | ||
AAOCAQ8AMIIBCgKCAQEA1lWqlsmeDKL1yR4aR1Swvb3nW3VGmQs9VejpVWh1Yh/g | ||
paj51zuSHVXYIhfC26mtOdsJkDq5sNyemxGGKDyhnsJQ6w3RVhtI1t89myut0wjZ | ||
qY/mXNkpfZhPKRI31N/UFx5LMr9PZY51t4+5Q362H3YwRiP497xeJ0SUbG+Hm5z5 | ||
nrsY5ENecHFF4KATL6/topYd0rns4IEjvbaocOIv+yhyOPAXEEyD7KIozGuPf09h | ||
3M50ne5oMjjTlFk7+lJ6gMSZdnyKUMc01sP9Jvu80Pbq6prXqV0akSAWo8BGUX8+ | ||
0RQMktivMyjhyTD/LNUzEdlOPuKrYxODjrtAxTCLpwIDAQABoxswGTAXBgNVHREE | ||
EDAOggxzcnYzLm9rcm8uaW8wDQYJKoZIhvcNAQELBQADggIBAJl/MXI7JVccfaQB | ||
VnnYiLLjX/auW0BWh06Pg0vNWfyfXBbHWJWKps7k1SBtkSBsuqd6ecB4LJAYvbWf | ||
51+j/aw0I71xXw3rXdhuh7mOwi0rg3t/RRNs5EJz/GPk734MecuJcNLrWS/YRq0a | ||
TojJFW22rmlem9vL7OkHPoNDh4LbHN9Wyxgdjt+VmVK+o7Gx4DSHZxo/Dmz6Gp0F | ||
dmKNCUG6yYcxdgDvr0mz4rJeLlqXwb/gjLFmYfnyVWZRB3WOUQuejY7W17W16LYP | ||
11fqlOJSuhyj7E5NekikxO9asOclTlw7qDG7i0PpBTLuLgqDppOo9xZOHrK+DUrf | ||
r3CP9d7P86g2oTv4d6btKUGez3wIU5qfrb4mAZDp6/nngR9hpcFaeGqtjtgJj7Bo | ||
elbJEPy2f3Uwocbatr7nI1szNDJuEoXi0nnAT/h1K4m3EuTRuUsEn8ySJJYwB2fc | ||
ExBhHSrljm111VN/a5TLhT5sZJYKDiVYQ0SJEZRo9rJg8P+G6Xa30JyFa1r6iYOp | ||
UUE8nsWPGcdC6HKu7Tr1YsvbjHTG900O+Rco8at1EWu7s+b278VcYzRJy5z+HM3N | ||
+0/EDFpu9xsSt3PtRGVk57gtf1nAECSV1OQSBCGy+E/MUpBQEqq3VL4vM9H3ANSz | ||
stX902cJF9ZYpG8Hn5Xt4q8AnFIw | ||
-----END CERTIFICATE----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIEajCCAlKgAwIBA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sBh01UHCIxIMdSJTtdGs3PIIgKMlAC5TQR4nqDCiqgzTo0JPCeyPIYmFy8iwigM+ | ||
CC280ZHWIkvOYMOBM4VswcSzDF8XRZG/vIf7tV3jp0XtJ1EddL+GM18eLh4vIWt8 | ||
b7PZnabtIBvITzjjW5Np05kQu1W3riqBveyQHz2fB1IpwTO2mXPyal1vw1Wrj5WQ | ||
QlFJjUTNxk0J4yVNHQbErrtBnGE0Zl4brcq7HNGsd0d0knOJ8F/edSB+USdeqNMH | ||
qBd1GTI1a/dNQ2oC34zvq+cUSzz4L+pK+2nJSeWpb/WAn0V/EgnH6APluxwpUs6i | ||
QqTqyCu2ecrvR0hhEiRNmBZW4+QaZe4aLrLClCEED+/nuvZf/9qLhIdEsq8bbHtP | ||
SIHhTv8pMfT0uej0JxpLKJDLvsmOUUVKCRMW//kArCZHmHKKOPKKwuNCNCRbIzib | ||
EN4HE4RU0BwVRZscMz7nTuSGqucN/Nrae1yC/k9C | ||
-----END CERTIFICATE----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIEPzCCAicCCQDBswRdKTzMkDANBgkqhkiG9w0BAQsFADBaMQswCQYDVQQGEwJJ | ||
TDERMA8GA1UECAwIVGVsLUF2aXYxETAPBgNVBAcMCFRlbC1Bdml2MRAwDgYDVQQK | ||
DAdva3JvLmlvMRMwEQYDVQQDDApva3JvLmlvIENBMB4XDTE4MDcyOTEzMzE1MFoX | ||
DTI4MDcyNjEzMzE1MFowaTELMAkGA1UEBhMCSUwxETAPBgNVBAgMCFRlbC1Bdml2 | ||
MREwDwYDVQQHDAhUZWwtQXZpdjEQMA4GA1UECgwHb2tyby5pbzETMBEGA1UECwwK | ||
YXJjaGl0ZWN0czENMAsGA1UEAwwEc3J2MjCCASIwDQYJKoZ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