Skip to content

Commit

Permalink
Add secure socket support
Browse files Browse the repository at this point in the history
  • Loading branch information
aashikam committed Dec 20, 2023
1 parent 0688ce2 commit af4c768
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
9 changes: 9 additions & 0 deletions ballerina/client.bal
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,18 @@ public isolated client class Client {
#
# + datasourceName - The driver class name to be used to get the connection
# + properties - The database properties, which should be applied when getting the connection
# + ssl - SSL configurations to be used
public type Options record {|
string? datasourceName = ();
map<anydata>? properties = ();
SecureSocket ssl?;
|};

# The SSL configurations to be used when connecting to the Redshift server.
#
# + rootcert - File path of the SSL root certificate
public type SecureSocket record {|
string rootcert?;
|};

# An additional set of configurations for the JDBC Client to be passed internally within the module.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,17 @@ public static final class ClientConfiguration {
public static final BString CONNECTION_POOL_OPTIONS = StringUtils.fromString("connectionPool");
public static final BString OPTIONS = StringUtils.fromString("options");
public static final BString PROPERTIES = StringUtils.fromString("properties");
public static final BString SECURE_SOCKET = StringUtils.fromString("ssl");
}

public static final class SSL {
public static final BString SSL_MODE = StringUtils.fromString("ssl");
public static final BString ROOT_CERT = StringUtils.fromString("rootcert");
public static final BString SSL_FACTORY_ARG = StringUtils.fromString("sslfactory");
public static final BString SSL_FACTORY_VALUE = StringUtils.fromString("com.amazon.redshift.ssl.NonValidatingFactory");
public static final BString SSL_ROOT_CERT_ARG = StringUtils.fromString("sslfactoryarg");

}
public static final String CONNECT_TIMEOUT = ".*(connect).*(timeout).*";
public static final String POOL_CONNECTION_TIMEOUT = "ConnectionTimeout";

Expand Down
41 changes: 41 additions & 0 deletions native/src/main/java/io/ballerina/lib/aws/redshift/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2023 WSO2 LLC. (https://www.wso2.com) All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.ballerina.lib.aws.redshift;

import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BString;

/**
* This class includes utility functions.
*/
public class Utils {

public static void addSSLOptions(BMap secureSocket, BMap<BString, Object> options) {
if (secureSocket == null) {
options.put(Constants.SSL.SSL_MODE, false);
} else {
options.put(Constants.SSL.SSL_MODE, true);
BString sslrootcert = secureSocket.getStringValue(Constants.SSL.ROOT_CERT);
options.put(Constants.SSL.SSL_FACTORY_ARG, Constants.SSL.SSL_FACTORY_VALUE.getValue());
if (sslrootcert != null) {
options.put(Constants.SSL.SSL_ROOT_CERT_ARG, sslrootcert);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package io.ballerina.lib.aws.redshift.nativeimpl;

import io.ballerina.lib.aws.redshift.Constants;
import io.ballerina.lib.aws.redshift.Utils;
import io.ballerina.runtime.api.creators.ValueCreator;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BObject;
Expand Down Expand Up @@ -57,11 +58,8 @@ public static Object createClient(BObject client, BMap<BString, Object> clientCo
properties = options.getMapValue(Constants.ClientConfiguration.PROPERTIES);
BString dataSourceNamVal = options.getStringValue(Constants.ClientConfiguration.DATASOURCE_NAME);
datasourceName = dataSourceNamVal == null ? null : dataSourceNamVal.getValue();
BString requestGeneratedKeysVal = options.getStringValue(
Constants.ClientConfiguration.REQUEST_GENERATED_KEYS);
requestGeneratedKeys = requestGeneratedKeysVal == null ?
Constants.RequestGeneratedKeysValues.ALL : requestGeneratedKeysVal.getValue();
if (properties != null) {
Utils.addSSLOptions(options.getMapValue(Constants.ClientConfiguration.SECURE_SOCKET), properties);
for (Object propKey : properties.getKeys()) {
if (propKey.toString().toLowerCase(Locale.ENGLISH).matches(Constants.CONNECT_TIMEOUT)) {
poolProperties = new Properties();
Expand Down

0 comments on commit af4c768

Please sign in to comment.