Skip to content

Commit

Permalink
Merge pull request #1309 from dilanSachi/kerberos-issue
Browse files Browse the repository at this point in the history
Add preferred auth support for listener
  • Loading branch information
dilanSachi authored Jul 24, 2024
2 parents 2d34e8a + 820a3d1 commit 414b205
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 26 deletions.
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 = "ftp"
version = "2.10.0"
version = "2.10.1"
authors = ["Ballerina"]
keywords = ["FTP", "SFTP", "remote file", "file transfer", "client", "service"]
repository = "https://github.com/ballerina-platform/module-ballerina-ftp"
Expand Down Expand Up @@ -33,5 +33,5 @@ path = "./lib/commons-net-3.9.0.jar"
[[platform.java17.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "ftp-native"
version = "2.10.0"
path = "../native/build/libs/ftp-native-2.10.0.jar"
version = "2.10.1"
path = "../native/build/libs/ftp-native-2.10.1-SNAPSHOT.jar"
2 changes: 1 addition & 1 deletion ballerina/CompilerPlugin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ id = "ftp-compiler-plugin"
class = "io.ballerina.stdlib.ftp.plugin.FtpCompilerPlugin"

[[dependency]]
path = "../compiler-plugin/build/libs/ftp-compiler-plugin-2.10.0.jar"
path = "../compiler-plugin/build/libs/ftp-compiler-plugin-2.10.1-SNAPSHOT.jar"
4 changes: 2 additions & 2 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ distribution-version = "2201.8.0"
[[package]]
org = "ballerina"
name = "ftp"
version = "2.10.0"
version = "2.10.1"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
Expand Down Expand Up @@ -112,7 +112,7 @@ modules = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.2.0"
version = "1.2.3"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand Down
12 changes: 12 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ This file contains all the notable changes done to the Ballerina Email package t

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

## Added

- [Added customizable authentication methods in SFTP listener](https://github.com/ballerina-platform/ballerina-library/issues/6771)

## [2.10.0] - 2024-07-23

### Added

- [Added customizable authentication methods in SFTP client](https://github.com/ballerina-platform/ballerina-library/issues/6768)

## [2.9.1] - 2024-02-19

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.ballerina.runtime.api.Environment;
import io.ballerina.runtime.api.Future;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.values.BArray;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BObject;
import io.ballerina.runtime.api.values.BString;
Expand All @@ -42,14 +41,11 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import static io.ballerina.stdlib.ftp.util.FtpConstants.ENDPOINT_CONFIG_PREFERRED_METHODS;
import static io.ballerina.stdlib.ftp.util.FtpConstants.ENTITY_BYTE_STREAM;
import static io.ballerina.stdlib.ftp.util.FtpConstants.NO_AUTH_METHOD_ERROR;
import static io.ballerina.stdlib.ftp.util.FtpConstants.READ_INPUT_STREAM;
import static io.ballerina.stdlib.ftp.util.FtpConstants.VFS_CLIENT_CONNECTOR;
import static io.ballerina.stdlib.ftp.util.FtpUtil.ErrorType.Error;
Expand Down Expand Up @@ -95,17 +91,7 @@ public static Object initClientEndpoint(BObject clientEndpoint, BMap<Object, Obj
ftpConfig.put(FtpConstants.IDENTITY_PASS_PHRASE, privateKeyPassword.getValue());
}
}
final BArray preferredMethods = auth.getArrayValue((StringUtils.fromString(
ENDPOINT_CONFIG_PREFERRED_METHODS)));
if (preferredMethods != null) {
if (preferredMethods.isEmpty()) {
return FtpUtil.createError(NO_AUTH_METHOD_ERROR, Error.errorType());
}
String authMethods = Arrays.stream(preferredMethods.getValues())
.map(FtpClient::getAuthMethod)
.collect(Collectors.joining(","));
ftpConfig.put(ENDPOINT_CONFIG_PREFERRED_METHODS, authMethods);
}
ftpConfig.put(ENDPOINT_CONFIG_PREFERRED_METHODS, FtpUtil.getPreferredMethodsFromAuthConfig(auth));
}
ftpConfig.put(FtpConstants.PASSIVE_MODE, String.valueOf(true));
ftpConfig.put(FtpConstants.USER_DIR_IS_ROOT, String.valueOf(false));
Expand All @@ -128,10 +114,6 @@ public static Object initClientEndpoint(BObject clientEndpoint, BMap<Object, Obj
return null;
}

private static String getAuthMethod(Object authMethodObj) {
return authMethodObj.toString().toLowerCase().replace("_", "-");
}

public static Object getFirst(Environment env, BObject clientConnector, BString filePath) {
clientConnector.addNativeData(ENTITY_BYTE_STREAM, null);
Future balFuture = env.markAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Map;
import java.util.Optional;

import static io.ballerina.stdlib.ftp.util.FtpConstants.ENDPOINT_CONFIG_PREFERRED_METHODS;
import static io.ballerina.stdlib.ftp.util.FtpConstants.FTP_CALLER;
import static io.ballerina.stdlib.ftp.util.FtpConstants.FTP_CLIENT;
import static io.ballerina.stdlib.ftp.util.FtpConstants.FTP_SERVICE_ENDPOINT_CONFIG;
Expand Down Expand Up @@ -92,7 +93,7 @@ public static Object register(BObject ftpListener, BObject service) {
if (listener.getCaller() != null) {
return null;
}
BMap serviceEndpointConfig = (BMap) ftpListener.getNativeData(FTP_SERVICE_ENDPOINT_CONFIG);;
BMap serviceEndpointConfig = (BMap) ftpListener.getNativeData(FTP_SERVICE_ENDPOINT_CONFIG);
BObject caller = createCaller(serviceEndpointConfig);
if (caller instanceof BError) {
return caller;
Expand Down Expand Up @@ -129,6 +130,7 @@ private static Map<String, String> getServerConnectorParamMap(BMap serviceEndpoi
params.put(FtpConstants.IDENTITY_PASS_PHRASE, privateKeyPassword);
}
}
params.put(ENDPOINT_CONFIG_PREFERRED_METHODS, FtpUtil.getPreferredMethodsFromAuthConfig(auth));
}
params.put(FtpConstants.USER_DIR_IS_ROOT, String.valueOf(false));
params.put(FtpConstants.AVOID_PERMISSION_CHECK, String.valueOf(true));
Expand Down
25 changes: 25 additions & 0 deletions native/src/main/java/io/ballerina/stdlib/ftp/util/FtpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.utils.TypeUtils;
import io.ballerina.runtime.api.values.BArray;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BObject;
Expand All @@ -41,17 +42,22 @@
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import static io.ballerina.stdlib.ftp.util.FtpConstants.ENDPOINT_CONFIG_PREFERRED_METHODS;
import static io.ballerina.stdlib.ftp.util.FtpConstants.FTP_ANONYMOUS_PASSWORD;
import static io.ballerina.stdlib.ftp.util.FtpConstants.FTP_ANONYMOUS_USERNAME;
import static io.ballerina.stdlib.ftp.util.FtpConstants.NO_AUTH_METHOD_ERROR;
import static io.ballerina.stdlib.ftp.util.FtpConstants.ON_FILE_CHANGE_REMOTE_FUNCTION;
import static io.ballerina.stdlib.ftp.util.FtpUtil.ErrorType.Error;
import static io.ballerina.stdlib.ftp.util.ModuleUtils.getModule;

/**
Expand Down Expand Up @@ -234,6 +240,25 @@ public static Optional<MethodType> getOnFileChangeMethod(BObject service) {
.findFirst();
}

public static String getAuthMethod(Object authMethodObj) {
return authMethodObj.toString().toLowerCase().replace("_", "-");
}

public static String getPreferredMethodsFromAuthConfig(BMap authenticationConfig) {
final BArray preferredMethods = authenticationConfig.getArrayValue((StringUtils.fromString(
ENDPOINT_CONFIG_PREFERRED_METHODS)));
String preferredAuthMethods = "";
if (preferredMethods != null) {
if (preferredMethods.isEmpty()) {
throw FtpUtil.createError(NO_AUTH_METHOD_ERROR, Error.errorType());
}
preferredAuthMethods = Arrays.stream(preferredMethods.getValues()).limit(preferredMethods.size())
.map(FtpUtil::getAuthMethod)
.collect(Collectors.joining(","));
}
return preferredAuthMethods;
}

/**
* Specifies the error type for ftp package.
*/
Expand Down

0 comments on commit 414b205

Please sign in to comment.