Skip to content
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

Apply review suggestions for the ldaps support #67

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerina"
name = "ldap"
version = "1.0.2"
version = "1.1.0"
authors = ["Ballerina"]
export=["ldap"]
keywords = ["ldap"]
Expand All @@ -15,8 +15,8 @@ graalvmCompatible = true
[[platform.java17.dependency]]
groupId = "io.ballerina.lib"
artifactId = "ldap-native"
version = "1.0.2-SNAPSHOT"
path = "../native/build/libs/ldap-native-1.0.2-SNAPSHOT.jar"
version = "1.1.0-SNAPSHOT"
path = "../native/build/libs/ldap-native-1.1.0-SNAPSHOT.jar"

[[platform.java17.dependency]]
groupId = "com.unboundid"
Expand Down
4 changes: 2 additions & 2 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.10.0"
distribution-version = "2201.9.0"

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -65,7 +65,7 @@ scope = "testOnly"
[[package]]
org = "ballerina"
name = "ldap"
version = "1.0.2"
version = "1.1.0"
dependencies = [
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "jballerina.java"},
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
org.gradle.caching=true
group=io.ballerina.lib
version=1.0.2-SNAPSHOT
ballerinaLangVersion=2201.10.0
version=1.1.0-SNAPSHOT
ballerinaLangVersion=2201.9.0

checkstylePluginVersion=10.12.0
spotbugsPluginVersion=5.0.14
Expand Down
26 changes: 16 additions & 10 deletions native/src/main/java/io/ballerina/lib/ldap/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ public final class Client {
private static final BString SECURE_SOCKET_CONFIG_TRUSTSTORE_FILE_PATH = StringUtils.fromString("path");
private static final BString SECURE_SOCKET_CONFIG_TRUSTSTORE_PASSWORD = StringUtils.fromString("password");
private static final BString SECURE_SOCKET_CONFIG_CERT = StringUtils.fromString("cert");
public static final String PKCS_12 = "PKCS12";
public static final String PEM = "PEM";
public static final String TRUST_STORE_INITIALIZATION_ERROR = "Error occurred while initializing trust store";
public static final String UNSUPPORTED_TRUST_STORE_TYPE_ERROR = "Unsupported trust store type";
public static final String EMPTY_TRUST_STORE_FILE_PATH_ERROR = "Truststore file path cannot be empty";
public static final String EMPTY_TRUST_STORE_PASSWORD_ERROR = "Truststore password cannot be empty";
public static final String EMPTY_CERTIFICATE_FILE_PATH_ERROR = "Certificate file path cannot be empty";

private Client() {
}
Expand Down Expand Up @@ -174,45 +181,44 @@ private static void evaluateCertField(Object cert, SSLConfig sslConfiguration) {
String trustStoreFile = trustStore.getStringValue(SECURE_SOCKET_CONFIG_TRUSTSTORE_FILE_PATH).getValue();
String trustStorePassword = trustStore.getStringValue(SECURE_SOCKET_CONFIG_TRUSTSTORE_PASSWORD).getValue();
if (trustStoreFile.isBlank()) {
throw new IllegalArgumentException("Truststore file path cannot be empty");
throw new IllegalArgumentException(EMPTY_TRUST_STORE_FILE_PATH_ERROR);
}
if (trustStorePassword.isBlank()) {
throw new IllegalArgumentException("Truststore password cannot be empty");
throw new IllegalArgumentException(EMPTY_TRUST_STORE_PASSWORD_ERROR);
}
sslConfiguration.setTrustStoreFile(trustStoreFile);
sslConfiguration.setTrustStorePass(trustStorePassword);
sslConfiguration.setTLSStoreType("PKCS12");
sslConfiguration.setTLSStoreType(PKCS_12);
} else {
String certFile = ((BString) cert).getValue();
if (certFile.isBlank()) {
throw new IllegalArgumentException("Certificate file path cannot be empty");
throw new IllegalArgumentException(EMPTY_CERTIFICATE_FILE_PATH_ERROR);
}
sslConfiguration.setTrustStoreFile(certFile);
sslConfiguration.setTLSStoreType("PEM");
sslConfiguration.setTLSStoreType(PEM);
}
}

private static AggregateTrustManager buildAggregatedTrustManager(SSLConfig sslConfiguration) {
if (sslConfiguration.getTLSStoreType().equals("PEM")) {
if (sslConfiguration.getTLSStoreType().equals(PEM)) {
try {
PEMFileTrustManager pemFileTrustManager = new PEMFileTrustManager(
sslConfiguration.getTrustStore());
return new AggregateTrustManager(false,
JVMDefaultTrustManager.getInstance(),
pemFileTrustManager);
} catch (KeyStoreException e) {
throw new IllegalArgumentException("Error occurred while initializing trust store"
+ e.getMessage());
throw new IllegalArgumentException(TRUST_STORE_INITIALIZATION_ERROR + e.getMessage());
}
} else if (sslConfiguration.getTLSStoreType().equals("PKCS12")) {
} else if (sslConfiguration.getTLSStoreType().equals(PKCS_12)) {
TrustStoreTrustManager trustStoreManager = new TrustStoreTrustManager(sslConfiguration.getTrustStore(),
sslConfiguration.getTrustStorePass().toCharArray(),
sslConfiguration.getTLSStoreType(), true);
return new AggregateTrustManager(false,
JVMDefaultTrustManager.getInstance(),
trustStoreManager);
} else {
throw new IllegalArgumentException("Unsupported trust store type");
throw new IllegalArgumentException(UNSUPPORTED_TRUST_STORE_TYPE_ERROR);
}
}

Expand Down
Loading