NIFI-10702 Clear server list on connection error in SMB processors

This closes #6620.

Signed-off-by: Peter Turcsanyi <turcsanyi@apache.org>
This commit is contained in:
Gabor Kulik 2022-11-04 15:23:43 +01:00 committed by Peter Turcsanyi
parent 9c21e26e63
commit 1bd4169558
7 changed files with 22 additions and 5 deletions

View File

@ -53,6 +53,10 @@
<groupId>com.hierynomus</groupId> <groupId>com.hierynomus</groupId>
<artifactId>smbj</artifactId> <artifactId>smbj</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
</dependency>
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>

View File

@ -545,6 +545,7 @@ public class GetSmbFile extends AbstractProcessor {
} catch (Exception e) { } catch (Exception e) {
logger.error("Could not establish smb connection because of error {}", new Object[]{e}); logger.error("Could not establish smb connection because of error {}", new Object[]{e});
context.yield(); context.yield();
smbClient.getServerList().unregister(hostname);
} }
} }
} }

View File

@ -401,6 +401,7 @@ public class PutSmbFile extends AbstractProcessor {
} catch (Exception e) { } catch (Exception e) {
session.transfer(flowFiles, REL_FAILURE); session.transfer(flowFiles, REL_FAILURE);
logger.error("Could not establish smb connection because of error {}", new Object[]{e}); logger.error("Could not establish smb connection because of error {}", new Object[]{e});
smbClient.getServerList().unregister(hostname);
} }
} }
} }

View File

@ -21,6 +21,7 @@ import com.hierynomus.mssmb2.SMB2ShareAccess;
import com.hierynomus.smbj.SMBClient; import com.hierynomus.smbj.SMBClient;
import com.hierynomus.smbj.auth.AuthenticationContext; import com.hierynomus.smbj.auth.AuthenticationContext;
import com.hierynomus.smbj.connection.Connection; import com.hierynomus.smbj.connection.Connection;
import com.hierynomus.smbj.server.ServerList;
import com.hierynomus.smbj.session.Session; import com.hierynomus.smbj.session.Session;
import com.hierynomus.smbj.share.DiskEntry; import com.hierynomus.smbj.share.DiskEntry;
import com.hierynomus.smbj.share.DiskShare; import com.hierynomus.smbj.share.DiskShare;
@ -59,6 +60,7 @@ public class PutSmbFileTest {
private DiskShare diskShare; private DiskShare diskShare;
private DiskEntry diskEntry; private DiskEntry diskEntry;
private File smbfile; private File smbfile;
private ServerList serverList;
private ByteArrayOutputStream baOutputStream; private ByteArrayOutputStream baOutputStream;
private final static String HOSTNAME = "smbhostname"; private final static String HOSTNAME = "smbhostname";
@ -80,9 +82,12 @@ public class PutSmbFileTest {
diskShare = mock(DiskShare.class); diskShare = mock(DiskShare.class);
diskEntry = mock(DiskEntry.class); diskEntry = mock(DiskEntry.class);
smbfile = mock(File.class); smbfile = mock(File.class);
serverList = mock(ServerList.class);
baOutputStream = new ByteArrayOutputStream(); baOutputStream = new ByteArrayOutputStream();
when(smbClient.connect(any(String.class))).thenReturn(connection); when(smbClient.connect(any(String.class))).thenReturn(connection);
when(smbClient.getServerList()).thenReturn(serverList);
when(connection.authenticate(any(AuthenticationContext.class))).thenReturn(session); when(connection.authenticate(any(AuthenticationContext.class))).thenReturn(session);
when(session.connectShare(SHARE)).thenReturn(diskShare); when(session.connectShare(SHARE)).thenReturn(diskShare);
when(diskShare.openFile( when(diskShare.openFile(

View File

@ -49,6 +49,10 @@
<groupId>com.hierynomus</groupId> <groupId>com.hierynomus</groupId>
<artifactId>smbj</artifactId> <artifactId>smbj</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.testcontainers</groupId> <groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId> <artifactId>testcontainers</artifactId>

View File

@ -121,14 +121,16 @@ public class SmbjClientProviderService extends AbstractControllerService impleme
@Override @Override
public SmbClientService getClient() throws IOException { public SmbClientService getClient() throws IOException {
Connection connection = smbClient.connect(hostname, port); Connection connection = null;
try { try {
connection = smbClient.connect(hostname, port);
return connectToShare(connection); return connectToShare(connection);
} catch (IOException e) { } catch (IOException e) {
getLogger().debug("Closing stale connection and trying to create a new one for share " + getServiceLocation()); getLogger().debug("Closing stale connection and trying to create a new one for share " + getServiceLocation());
closeConnection(connection); closeConnection(connection);
unregisterHost();
connection = smbClient.connect(hostname, port); connection = smbClient.connect(hostname, port);
return connectToShare(connection); return connectToShare(connection);
@ -160,6 +162,10 @@ public class SmbjClientProviderService extends AbstractControllerService impleme
return new SmbjClientService(session, (DiskShare) share, getServiceLocation()); return new SmbjClientService(session, (DiskShare) share, getServiceLocation());
} }
private void unregisterHost() {
smbClient.getServerList().unregister(hostname);
}
private void closeConnection(Connection connection) { private void closeConnection(Connection connection) {
try { try {
if (connection != null) { if (connection != null) {

View File

@ -47,10 +47,6 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
</dependency>
<dependency> <dependency>
<groupId>net.engio</groupId> <groupId>net.engio</groupId>
<artifactId>mbassador</artifactId> <artifactId>mbassador</artifactId>