Skip to content

Commit 3a04770

Browse files
ago1024tomaswolf
authored andcommitted
[GH-516] SFTP: fix filesystem-id parsing in getFileSystem(URI)
- Allow userinfo without password - Validate hostname is present Also-by: Thomas Wolf <twolf@apache.org>
1 parent c128e0b commit 3a04770

2 files changed

Lines changed: 70 additions & 4 deletions

File tree

sshd-sftp/src/main/java/org/apache/sshd/sftp/client/fs/SftpFileSystemProvider.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,10 +1499,9 @@ public static String getOctalPermissions(Collection<PosixFilePermission> perms)
14991499
* @see #getFileSystemIdentifier(String, int, String)
15001500
*/
15011501
public static String getFileSystemIdentifier(URI uri) {
1502-
String userInfo = ValidateUtils.checkNotNullAndNotEmpty(uri.getUserInfo(), "UserInfo not provided");
1503-
String[] ui = GenericUtils.split(userInfo, ':');
1504-
ValidateUtils.checkTrue(GenericUtils.length(ui) == 2, "Invalid user info: %s", userInfo);
1505-
return getFileSystemIdentifier(uri.getHost(), uri.getPort(), ui[0]);
1502+
String host = ValidateUtils.checkNotNullAndNotEmpty(uri.getHost(), "Host not provided");
1503+
BasicCredentialsProvider credentials = ValidateUtils.checkNotNull(parseCredentials(uri), "UserInfo not provided");
1504+
return getFileSystemIdentifier(host, uri.getPort(), credentials.getUsername());
15061505
}
15071506

15081507
/**
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.sshd.sftp.client.fs;
21+
22+
import java.net.URI;
23+
import java.util.stream.Stream;
24+
25+
import org.apache.sshd.util.test.JUnitTestSupport;
26+
import org.junit.jupiter.api.MethodOrderer.MethodName;
27+
import org.junit.jupiter.api.Tag;
28+
import org.junit.jupiter.api.TestMethodOrder;
29+
import org.junit.jupiter.params.ParameterizedTest;
30+
import org.junit.jupiter.params.provider.Arguments;
31+
import org.junit.jupiter.params.provider.MethodSource;
32+
33+
@TestMethodOrder(MethodName.class)
34+
@Tag("NoIoTestCase")
35+
class SftpFileSystemProviderURITest extends JUnitTestSupport {
36+
37+
static Stream<Arguments> parameters() {
38+
return Stream.of( //
39+
Arguments.of(URI.create("sftp://username:password@host"), "host:22:username"),
40+
Arguments.of(URI.create("sftp://username:password@host:22"), "host:22:username"),
41+
Arguments.of(URI.create("sftp://username@host"), "host:22:username"),
42+
Arguments.of(URI.create("sftp://username:password@host:2222"), "host:2222:username"),
43+
Arguments.of(URI.create("sftp://username:password@host:22/path"), "host:22:username"));
44+
}
45+
46+
@MethodSource("parameters")
47+
@ParameterizedTest(name = "uri={0}")
48+
void getFileSystemIdentifierFromUri(URI uri, String expected) {
49+
assertEquals(expected, SftpFileSystemProvider.getFileSystemIdentifier(uri), "Mismatched filesystem identifier");
50+
}
51+
52+
static Stream<Arguments> invalid() {
53+
return Stream.of( //
54+
Arguments.of(URI.create(""), "Host not provided"),
55+
Arguments.of(URI.create("sftp://host:22"), "UserInfo not provided"),
56+
Arguments.of(URI.create("sftp://@host:22"), "UserInfo not provided"));
57+
}
58+
59+
@MethodSource("invalid")
60+
@ParameterizedTest(name = "uri={0}")
61+
void getFileSystemIdentifierFromInvalidUri(URI uri, String message) {
62+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
63+
() -> SftpFileSystemProvider.getFileSystemIdentifier(uri));
64+
assertEquals(message, exception.getMessage());
65+
}
66+
67+
}

0 commit comments

Comments
 (0)