-
-
Notifications
You must be signed in to change notification settings - Fork 144
BE: Auth: Support LDAPS for AD #840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
90 changes: 90 additions & 0 deletions
90
api/src/main/java/io/kafbat/ui/util/CustomSslSocketFactory.java
This file contains hidden or 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,90 @@ | ||
package io.kafbat.ui.util; | ||
|
||
import java.io.IOException; | ||
import java.net.InetAddress; | ||
import java.net.Socket; | ||
import java.security.SecureRandom; | ||
import java.security.cert.X509Certificate; | ||
import javax.net.SocketFactory; | ||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLSocketFactory; | ||
import javax.net.ssl.TrustManager; | ||
import javax.net.ssl.X509TrustManager; | ||
|
||
public class CustomSslSocketFactory extends SSLSocketFactory { | ||
private final SSLSocketFactory socketFactory; | ||
|
||
public CustomSslSocketFactory() { | ||
try { | ||
SSLContext ctx = SSLContext.getInstance("TLS"); | ||
ctx.init(null, new TrustManager[] { new DisabledX509TrustManager() }, new SecureRandom()); | ||
Haarolean marked this conversation as resolved.
Show resolved
Hide resolved
|
||
socketFactory = ctx.getSocketFactory(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static SocketFactory getDefault() { | ||
return new CustomSslSocketFactory(); | ||
} | ||
|
||
@Override | ||
public String[] getDefaultCipherSuites() { | ||
return socketFactory.getDefaultCipherSuites(); | ||
} | ||
|
||
@Override | ||
public String[] getSupportedCipherSuites() { | ||
return socketFactory.getSupportedCipherSuites(); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(Socket socket, String string, int i, boolean bln) throws IOException { | ||
return socketFactory.createSocket(socket, string, i, bln); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(String string, int i) throws IOException { | ||
return socketFactory.createSocket(string, i); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(String string, int i, InetAddress ia, int i1) throws IOException { | ||
return socketFactory.createSocket(string, i, ia, i1); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(InetAddress ia, int i) throws IOException { | ||
return socketFactory.createSocket(ia, i); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(InetAddress ia, int i, InetAddress ia1, int i1) throws IOException { | ||
return socketFactory.createSocket(ia, i, ia1, i1); | ||
} | ||
|
||
@Override | ||
public Socket createSocket() throws IOException { | ||
return socketFactory.createSocket(); | ||
} | ||
|
||
private static class DisabledX509TrustManager implements X509TrustManager { | ||
/** Empty certificate array. */ | ||
private static final X509Certificate[] CERTS = new X509Certificate[0]; | ||
|
||
@Override | ||
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) { | ||
|
||
// No-op, all clients are trusted. | ||
} | ||
|
||
@Override | ||
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) { | ||
|
||
// No-op, all servers are trusted. | ||
} | ||
|
||
@Override | ||
public X509Certificate[] getAcceptedIssuers() { | ||
return CERTS; | ||
} | ||
} | ||
} |
This file contains hidden or 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
35 changes: 35 additions & 0 deletions
35
api/src/test/java/io/kafbat/ui/ActiveDirectoryLdapTest.java
This file contains hidden or 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,35 @@ | ||
package io.kafbat.ui; | ||
|
||
import static io.kafbat.ui.container.ActiveDirectoryContainer.DOMAIN; | ||
|
||
import io.kafbat.ui.container.ActiveDirectoryContainer; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.springframework.context.ApplicationContextInitializer; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
@ContextConfiguration(initializers = {ActiveDirectoryLdapTest.Initializer.class}) | ||
public class ActiveDirectoryLdapTest extends AbstractActiveDirectoryIntegrationTest { | ||
wernerdv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private static final ActiveDirectoryContainer ACTIVE_DIRECTORY = new ActiveDirectoryContainer(false); | ||
|
||
@BeforeAll | ||
public static void setup() { | ||
ACTIVE_DIRECTORY.start(); | ||
} | ||
|
||
@AfterAll | ||
public static void shutdown() { | ||
ACTIVE_DIRECTORY.stop(); | ||
} | ||
|
||
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { | ||
@Override | ||
public void initialize(@NotNull ConfigurableApplicationContext context) { | ||
System.setProperty("spring.ldap.urls", ACTIVE_DIRECTORY.getLdapUrl()); | ||
System.setProperty("oauth2.ldap.activeDirectory", "true"); | ||
System.setProperty("oauth2.ldap.activeDirectory.domain", DOMAIN); | ||
} | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
api/src/test/java/io/kafbat/ui/ActiveDirectoryLdapsTest.java
This file contains hidden or 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,100 @@ | ||
package io.kafbat.ui; | ||
|
||
import static io.kafbat.ui.container.ActiveDirectoryContainer.CONTAINER_CERT_PATH; | ||
import static io.kafbat.ui.container.ActiveDirectoryContainer.CONTAINER_KEY_PATH; | ||
import static io.kafbat.ui.container.ActiveDirectoryContainer.DOMAIN; | ||
import static io.kafbat.ui.container.ActiveDirectoryContainer.PASSWORD; | ||
import static org.testcontainers.utility.MountableFile.forHostPath; | ||
|
||
import io.kafbat.ui.container.ActiveDirectoryContainer; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.OutputStreamWriter; | ||
import java.net.InetAddress; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.KeyPair; | ||
import java.security.PrivateKey; | ||
import java.security.cert.X509Certificate; | ||
import java.util.Map; | ||
import org.apache.kafka.common.config.types.Password; | ||
import org.apache.kafka.test.TestSslUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.springframework.context.ApplicationContextInitializer; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.testcontainers.shaded.org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator; | ||
import org.testcontainers.shaded.org.bouncycastle.openssl.jcajce.JcaPKCS8Generator; | ||
import org.testcontainers.shaded.org.bouncycastle.util.io.pem.PemWriter; | ||
|
||
@ContextConfiguration(initializers = {ActiveDirectoryLdapsTest.Initializer.class}) | ||
public class ActiveDirectoryLdapsTest extends AbstractActiveDirectoryIntegrationTest { | ||
private static final ActiveDirectoryContainer ACTIVE_DIRECTORY = new ActiveDirectoryContainer(true); | ||
|
||
private static File certPem = null; | ||
private static File privateKeyPem = null; | ||
|
||
@BeforeAll | ||
public static void setup() throws Exception { | ||
generateCerts(); | ||
|
||
ACTIVE_DIRECTORY.withCopyFileToContainer(forHostPath(certPem.getAbsolutePath()), CONTAINER_CERT_PATH); | ||
ACTIVE_DIRECTORY.withCopyFileToContainer(forHostPath(privateKeyPem.getAbsolutePath()), CONTAINER_KEY_PATH); | ||
|
||
ACTIVE_DIRECTORY.start(); | ||
} | ||
|
||
@AfterAll | ||
public static void shutdown() { | ||
ACTIVE_DIRECTORY.stop(); | ||
} | ||
|
||
private static void generateCerts() throws Exception { | ||
File truststore = File.createTempFile("truststore", ".jks"); | ||
|
||
truststore.deleteOnExit(); | ||
|
||
String host = "localhost"; | ||
KeyPair clientKeyPair = TestSslUtils.generateKeyPair("RSA"); | ||
|
||
X509Certificate clientCert = new TestSslUtils.CertificateBuilder(365, "SHA256withRSA") | ||
.sanDnsNames(host) | ||
.sanIpAddress(InetAddress.getByName(host)) | ||
.generate("O=Samba Administration, OU=Samba, CN=" + host, clientKeyPair); | ||
|
||
TestSslUtils.createTrustStore(truststore.getPath(), new Password(PASSWORD), Map.of("client", clientCert)); | ||
|
||
certPem = File.createTempFile("cert", ".pem"); | ||
wernerdv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try (FileWriter fw = new FileWriter(certPem)) { | ||
fw.write(certOrKeyToString(clientCert)); | ||
} | ||
|
||
privateKeyPem = File.createTempFile("key", ".pem"); | ||
try (FileWriter fw = new FileWriter(privateKeyPem)) { | ||
fw.write(certOrKeyToString(clientKeyPair.getPrivate())); | ||
} | ||
} | ||
|
||
private static String certOrKeyToString(Object certOrKey) throws Exception { | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
try (PemWriter pemWriter = new PemWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8))) { | ||
wernerdv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (certOrKey instanceof X509Certificate) { | ||
pemWriter.writeObject(new JcaMiscPEMGenerator(certOrKey)); | ||
} else { | ||
pemWriter.writeObject(new JcaPKCS8Generator((PrivateKey) certOrKey, null)); | ||
} | ||
} | ||
return out.toString(StandardCharsets.UTF_8); | ||
} | ||
|
||
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { | ||
@Override | ||
public void initialize(@NotNull ConfigurableApplicationContext context) { | ||
System.setProperty("spring.ldap.urls", ACTIVE_DIRECTORY.getLdapUrl()); | ||
System.setProperty("oauth2.ldap.activeDirectory", "true"); | ||
System.setProperty("oauth2.ldap.activeDirectory.domain", DOMAIN); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.