This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 3c91355d30f1 CAMEL-27367: camel-smb - NullPointerException when no
password is set (#30582)
3c91355d30f1 is described below
commit 3c91355d30f17842984dd60ed0b07307a74234d2
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 22 11:32:45 2026 +0100
CAMEL-27367: camel-smb - NullPointerException when no password is set
(#30582)
A username without a password now logs in with an empty password, so
username=Guest without a password is a guest login when the server
allows it. Without a username the connection fails with a clear message
instead of a NullPointerException: smbj's anonymous login fails with a
NullPointerException against Samba (SMB 3.1.1), so it is not offered.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Claus Ibsen <[email protected]>
---
.../apache/camel/catalog/docs/smb-component.adoc | 9 +++
.../camel-smb/src/main/docs/smb-component.adoc | 9 +++
.../apache/camel/component/smb/SmbOperations.java | 25 +++++--
.../smb/SmbAuthenticationContextTest.java | 79 ++++++++++++++++++++++
.../camel/component/smb/SmbNoPasswordIT.java | 60 ++++++++++++++++
5 files changed, 178 insertions(+), 4 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/smb-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/smb-component.adoc
index 8e5b44695b71..8b39d74a1704 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/smb-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/smb-component.adoc
@@ -156,6 +156,15 @@ To maintain backward compatibility, a new Camel Header
`CamelSmbUncPath` has bee
====
+== Login
+
+The component logs in with the `username`, `password` and `domain` options. A
`username` without a `password`
+logs in with an empty password, so `username=Guest` without a password is a
guest login, when the server allows
+guest access (for Samba: `map to guest` and `guest ok = yes` on the share).
+
+The `username` is required: an anonymous login (no username) is not supported,
and the connection fails with
+a message that no username is set.
+
== Permissions
The Camel SMB component requires the authenticated user to have appropriate
permissions on the SMB share.
diff --git a/components/camel-smb/src/main/docs/smb-component.adoc
b/components/camel-smb/src/main/docs/smb-component.adoc
index 8e5b44695b71..8b39d74a1704 100644
--- a/components/camel-smb/src/main/docs/smb-component.adoc
+++ b/components/camel-smb/src/main/docs/smb-component.adoc
@@ -156,6 +156,15 @@ To maintain backward compatibility, a new Camel Header
`CamelSmbUncPath` has bee
====
+== Login
+
+The component logs in with the `username`, `password` and `domain` options. A
`username` without a `password`
+logs in with an empty password, so `username=Guest` without a password is a
guest login, when the server allows
+guest access (for Samba: `map to guest` and `guest ok = yes` on the share).
+
+The `username` is required: an anonymous login (no username) is not supported,
and the connection fails with
+a message that no username is set.
+
== Permissions
The Camel SMB component requires the authenticated user to have appropriate
permissions on the SMB share.
diff --git
a/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java
b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java
index f1b695c33828..87cf545824ae 100644
---
a/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java
+++
b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java
@@ -96,11 +96,8 @@ public class SmbOperations implements SmbFileOperations {
disconnect();
// Establish fresh connections
+ AuthenticationContext ac =
createAuthenticationContext(configuration);
connection = smbClient.connect(configuration.getHostname(),
configuration.getPort());
- AuthenticationContext ac = new AuthenticationContext(
- configuration.getUsername(),
- configuration.getPassword().toCharArray(),
- configuration.getDomain());
session = connection.authenticate(ac);
share = (DiskShare)
session.connectShare(configuration.getShareName());
@@ -117,6 +114,26 @@ public class SmbOperations implements SmbFileOperations {
}
}
+ /**
+ * The credentials to log in with. A username without a password logs in
with an empty password, so the user
+ * {@code Guest} without a password is a guest login. Without a username
it fails: smbj's anonymous login fails with
+ * a NullPointerException against Samba (SMB 3.1.1), so it is not offered.
+ */
+ static AuthenticationContext createAuthenticationContext(SmbConfiguration
configuration) {
+ String username = configuration.getUsername();
+ if (ObjectHelper.isEmpty(username)) {
+ throw new GenericFileOperationFailedException(
+ "Cannot connect to: " + configuration.getHostname() + ":"
+ configuration.getPort()
+ + " as no username
is set. Set the username and password of the share,"
+ + " or
username=Guest for guest access.");
+ }
+ String password = configuration.getPassword();
+ return new AuthenticationContext(
+ username,
+ password != null ? password.toCharArray() : new char[0],
+ configuration.getDomain());
+ }
+
@Override
public boolean isConnected() throws GenericFileOperationFailedException {
return loggedIn &&
diff --git
a/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbAuthenticationContextTest.java
b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbAuthenticationContextTest.java
new file mode 100644
index 000000000000..b6e90cd57451
--- /dev/null
+++
b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbAuthenticationContextTest.java
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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 org.apache.camel.component.smb;
+
+import com.hierynomus.smbj.auth.AuthenticationContext;
+import org.apache.camel.component.file.GenericFileOperationFailedException;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.NullAndEmptySource;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The credentials the smb component logs in with when the username or the
password is not set.
+ */
+class SmbAuthenticationContextTest {
+
+ @Test
+ void usernameAndPassword() {
+ AuthenticationContext ac =
SmbOperations.createAuthenticationContext(configuration("scott", "tiger",
"WORKGROUP"));
+
+ assertEquals("scott", ac.getUsername());
+ assertArrayEquals("tiger".toCharArray(), ac.getPassword());
+ assertEquals("WORKGROUP", ac.getDomain());
+ }
+
+ @Test
+ void usernameWithoutPasswordHasEmptyPassword() {
+ AuthenticationContext ac =
SmbOperations.createAuthenticationContext(configuration("scott", null, null));
+
+ assertEquals("scott", ac.getUsername());
+ assertEquals(0, ac.getPassword().length);
+ }
+
+ @Test
+ void guestWithoutPasswordIsGuestLogin() {
+ AuthenticationContext ac =
SmbOperations.createAuthenticationContext(configuration("Guest", null, null));
+
+ assertTrue(ac.isGuest());
+ }
+
+ @ParameterizedTest
+ @NullAndEmptySource
+ void noUsernameFails(String username) {
+ SmbConfiguration configuration = configuration(username, "tiger",
null);
+
+ GenericFileOperationFailedException e =
assertThrows(GenericFileOperationFailedException.class,
+ () ->
SmbOperations.createAuthenticationContext(configuration));
+ assertEquals("Cannot connect to: localhost:445 as no username is set.
Set the username and password of the share,"
+ + " or username=Guest for guest access.",
+ e.getMessage());
+ }
+
+ private static SmbConfiguration configuration(String username, String
password, String domain) {
+ SmbConfiguration configuration = new SmbConfiguration();
+ configuration.setHostname("localhost");
+ configuration.setUsername(username);
+ configuration.setPassword(password);
+ configuration.setDomain(domain);
+ return configuration;
+ }
+}
diff --git
a/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbNoPasswordIT.java
b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbNoPasswordIT.java
new file mode 100644
index 000000000000..6a22eaf8423d
--- /dev/null
+++
b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbNoPasswordIT.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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 org.apache.camel.component.smb;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.Exchange;
+import org.apache.camel.component.file.GenericFileOperationFailedException;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * An endpoint without a password, or without a username, fails with a clear
error instead of a NullPointerException.
+ * The test server does not map unknown users to guest, so the guest login
fails too.
+ */
+class SmbNoPasswordIT extends SmbServerTestSupport {
+
+ @Test
+ void usernameWithoutPassword() {
+ assertConnectFails(String.format("smb:%s/%s?username=%s",
service.address(), service.shareName(), service.username()),
+ "Authentication failed");
+ }
+
+ @Test
+ void guestWithoutPassword() {
+ assertConnectFails(String.format("smb:%s/%s?username=Guest",
service.address(), service.shareName()),
+ "Authentication failed");
+ }
+
+ @Test
+ void noUsername() {
+ assertConnectFails(String.format("smb:%s/%s?password=secret",
service.address(), service.shareName()),
+ "as no username is set");
+ }
+
+ private void assertConnectFails(String uri, String reason) {
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBodyAndHeader(uri, "Hello World",
Exchange.FILE_NAME, "no-password.txt"));
+
+ GenericFileOperationFailedException cause =
assertInstanceOf(GenericFileOperationFailedException.class, e.getCause());
+ assertTrue(cause.getMessage().startsWith("Cannot connect to: "),
cause.getMessage());
+ assertTrue(cause.getMessage().contains(reason), cause.getMessage());
+ }
+}