Skip to content

Commit 4b9283e

Browse files
authored
Merge pull request #22 from rundeck-plugins/RSE-925
RSE-925: SSHJ executor does not honor keep-alive-interval
2 parents fde4753 + a316c97 commit 4b9283e

File tree

5 files changed

+48
-7
lines changed

5 files changed

+48
-7
lines changed

src/main/java/com/plugin/sshjplugin/SSHJFileCopierPlugin.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class SSHJFileCopierPlugin extends BaseFileCopier implements MultiFileCop
5050
.property(SSHJNodeExecutorPlugin.SSH_AUTH_TYPE_PROP)
5151
.property(SSHJNodeExecutorPlugin.SSH_PASSPHRASE_STORAGE_PROP)
5252
.property(SSHJNodeExecutorPlugin.SSH_KEEP_ALIVE_INTERVAL)
53+
.property(SSHJNodeExecutorPlugin.SSH_KEEP_ALIVE_MAX_ALIVE_COUNT)
5354
.property(SSHJNodeExecutorPlugin.SSH_RETRY_COUNTER)
5455

5556
.mapping(SSHJNodeExecutorPlugin.CONFIG_KEYPATH, SSHJNodeExecutorPlugin.PROJ_PROP_SSH_KEYPATH)
@@ -64,6 +65,8 @@ public class SSHJFileCopierPlugin extends BaseFileCopier implements MultiFileCop
6465
.frameworkMapping(SSHJNodeExecutorPlugin.CONFIG_PASSPHRASE_STORE_PATH, SSHJNodeExecutorPlugin.FWK_PROP_SSH_KEY_PASSPHRASE_STORAGE_PATH)
6566
.mapping(SSHJNodeExecutorPlugin.CONFIG_KEEP_ALIVE_INTERVAL, SSHJNodeExecutorPlugin.PROJ_PROP_SSH_KEEP_ALIVE)
6667
.frameworkMapping(SSHJNodeExecutorPlugin.CONFIG_KEEP_ALIVE_INTERVAL, SSHJNodeExecutorPlugin.FWK_PROP_SSH_KEEP_ALIVE)
68+
.mapping(SSHJNodeExecutorPlugin.CONFIG_KEEP_ALIVE_MAX_ALIVE_COUNT, SSHJNodeExecutorPlugin.PROJ_PROP_SSH_KEEP_ALIVE_MAX_COUNT)
69+
.frameworkMapping(SSHJNodeExecutorPlugin.CONFIG_KEEP_ALIVE_MAX_ALIVE_COUNT, SSHJNodeExecutorPlugin.FWK_PROP_SSH_KEEP_ALIVE_MAX_ALIVE_COUNT)
6770
.mapping(SSHJNodeExecutorPlugin.CONFIG_RETRY_ENABLE, SSHJNodeExecutorPlugin.PROJ_PROP_RETRY_ENABLE)
6871
.frameworkMapping(SSHJNodeExecutorPlugin.CONFIG_RETRY_ENABLE, SSHJNodeExecutorPlugin.FWK_PROP_RETRY_ENABLE)
6972
.mapping(SSHJNodeExecutorPlugin.CONFIG_RETRY_COUNTER, SSHJNodeExecutorPlugin.PROJ_PROP_RETRY_COUNTER)

src/main/java/com/plugin/sshjplugin/SSHJNodeExecutorPlugin.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public class SSHJNodeExecutorPlugin implements NodeExecutor, ProxySecretBundleCr
4545
public static final String CONFIG_AUTHENTICATION = "authentication";
4646
public static final String CONFIG_PASSPHRASE_STORE_PATH = "passphrasestoragepath";
4747
public static final String CONFIG_KEEP_ALIVE_INTERVAL = "keepAliveInterval";
48+
49+
public static final String CONFIG_KEEP_ALIVE_MAX_ALIVE_COUNT = "keepAliveMaxAliveCount";
50+
4851
public static final String CONFIG_RETRY_ENABLE = "retryEnable";
4952
public static final String CONFIG_RETRY_COUNTER = "retryCounter";
5053

@@ -66,6 +69,9 @@ public class SSHJNodeExecutorPlugin implements NodeExecutor, ProxySecretBundleCr
6669
public static final String NODE_ATTR_SSH_KEY_PASSPHRASE_OPTION = "ssh-key-passphrase-option";
6770
public static final String NODE_ATTR_SUDO_PASSWORD_OPTION = "password-option";
6871
public static final String NODE_ATTR_SSH_KEEP_ALIVE = "keep-alive-interval";
72+
73+
public static final String NODE_ATTR_SSH_KEEP_MAX_ALIVE_COUNT = "keep-alive-max-alive-count";
74+
6975
public static final String NODE_ATTR_RETRY_COUNTER = "retry-counter";
7076
public static final String NODE_ATTR_RETRY_ENABLE = "retry-enable";
7177

@@ -82,7 +88,13 @@ public class SSHJNodeExecutorPlugin implements NodeExecutor, ProxySecretBundleCr
8288
public static final String FWK_PROP_SSH_KEY_PASSPHRASE_STORAGE_PATH = FWK_PROP_PREFIX + NODE_ATTR_SSH_KEY_PASSPHRASE_STORAGE_PATH;
8389
public static final String PROJ_PROP_SSH_KEY_PASSPHRASE_STORAGE_PATH = PROJ_PROP_PREFIX + NODE_ATTR_SSH_KEY_PASSPHRASE_STORAGE_PATH;
8490
public static final String FWK_PROP_SSH_KEEP_ALIVE = FWK_PROP_PREFIX + NODE_ATTR_SSH_KEEP_ALIVE;
91+
public static final String FWK_PROP_SSH_KEEP_ALIVE_COUNT = FWK_PROP_PREFIX + NODE_ATTR_SSH_KEEP_MAX_ALIVE_COUNT;
92+
93+
public static final String FWK_PROP_SSH_KEEP_ALIVE_MAX_ALIVE_COUNT = FWK_PROP_PREFIX + NODE_ATTR_SSH_KEEP_MAX_ALIVE_COUNT;
94+
8595
public static final String PROJ_PROP_SSH_KEEP_ALIVE = PROJ_PROP_PREFIX + NODE_ATTR_SSH_KEEP_ALIVE;
96+
public static final String PROJ_PROP_SSH_KEEP_ALIVE_MAX_COUNT = PROJ_PROP_PREFIX + NODE_ATTR_SSH_KEEP_MAX_ALIVE_COUNT;
97+
8698
public static final String FWK_PROP_RETRY_COUNTER = FWK_PROP_PREFIX + NODE_ATTR_RETRY_COUNTER;
8799
public static final String PROJ_PROP_RETRY_COUNTER = PROJ_PROP_PREFIX + NODE_ATTR_RETRY_COUNTER;
88100
public static final String FWK_PROP_RETRY_ENABLE = FWK_PROP_PREFIX + NODE_ATTR_RETRY_ENABLE;
@@ -146,9 +158,13 @@ public class SSHJNodeExecutorPlugin implements NodeExecutor, ProxySecretBundleCr
146158
.build();
147159

148160
static final Property SSH_KEEP_ALIVE_INTERVAL = PropertyUtil.string(CONFIG_KEEP_ALIVE_INTERVAL, "Keep Alive Interval",
149-
"Keep Alive Interval",
161+
"Keep Alive Interval. Can be overridden by a Node attribute named 'keep-alive-interval'. ",
150162
false, null);
151163

164+
static final Property SSH_KEEP_ALIVE_MAX_ALIVE_COUNT = PropertyUtil.string(CONFIG_KEEP_ALIVE_MAX_ALIVE_COUNT, "Keep Alive Max Alive Count",
165+
"Keep Alive Max Alive Count (default 5). Can be overridden by a Node attribute named 'keep-alive-max-alive-count'.",
166+
false, "5");
167+
152168
static final Property SSH_RETRY_COUNTER = PropertyUtil.string(CONFIG_RETRY_COUNTER, "Number of retries",
153169
"Set retries limit in case the connection fail (just for Transport Exceptions)",
154170
false, "3");
@@ -182,6 +198,7 @@ public Description getDescription() {
182198
builder.property(SSH_KEY_STORAGE_PROP);
183199
builder.property(SSH_PASSPHRASE_STORAGE_PROP);
184200
builder.property(SSH_KEEP_ALIVE_INTERVAL);
201+
builder.property(SSH_KEEP_ALIVE_MAX_ALIVE_COUNT);
185202
builder.property(SSH_RETRY_ENABLE);
186203
builder.property(SSH_RETRY_COUNTER);
187204

@@ -199,8 +216,8 @@ public Description getDescription() {
199216
builder.mapping(CONFIG_KEEP_ALIVE_INTERVAL, PROJ_PROP_SSH_KEEP_ALIVE);
200217
builder.frameworkMapping(CONFIG_KEEP_ALIVE_INTERVAL, FWK_PROP_SSH_KEEP_ALIVE);
201218

202-
builder.mapping(CONFIG_KEEP_ALIVE_INTERVAL, PROJ_PROP_SSH_KEEP_ALIVE);
203-
builder.frameworkMapping(CONFIG_KEEP_ALIVE_INTERVAL, FWK_PROP_SSH_KEEP_ALIVE);
219+
builder.mapping(CONFIG_KEEP_ALIVE_MAX_ALIVE_COUNT, PROJ_PROP_SSH_KEEP_ALIVE_MAX_COUNT);
220+
builder.frameworkMapping(CONFIG_KEEP_ALIVE_MAX_ALIVE_COUNT, FWK_PROP_SSH_KEEP_ALIVE_COUNT);
204221

205222
builder.mapping(CONFIG_RETRY_COUNTER, PROJ_PROP_RETRY_COUNTER);
206223
builder.frameworkMapping(CONFIG_RETRY_COUNTER, FWK_PROP_RETRY_COUNTER);

src/main/java/com/plugin/sshjplugin/model/SSHJBase.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import com.plugin.sshjplugin.SSHJBuilder;
55
import com.plugin.sshjplugin.SSHJDefaultConfig;
66
import com.plugin.sshjplugin.SSHJPluginLoggerFactory;
7+
import net.schmizz.keepalive.KeepAlive;
78
import net.schmizz.keepalive.KeepAliveProvider;
9+
import net.schmizz.keepalive.KeepAliveRunner;
810
import net.schmizz.sshj.DefaultConfig;
911
import net.schmizz.sshj.SSHClient;
1012
import net.schmizz.sshj.transport.TransportException;
@@ -63,6 +65,7 @@ public void connect(SSHClient ssh){
6365
int connectTimeout = sshjConnection.getConnectTimeout();
6466
int commandTimeout = sshjConnection.getCommandTimeout();
6567
int keepAliveInterval = sshjConnection.getKeepAliveInterval();
68+
int keepAliveCount = sshjConnection.getKeepAliveMaxAlive();
6669
boolean retry = sshjConnection.isRetryEnabled();
6770
int retryCount = sshjConnection.getRetryCounter();
6871

@@ -72,6 +75,7 @@ public void connect(SSHClient ssh){
7275
pluginLogger.log(3, "["+getPluginName()+"] getConnectTimeout timeout: " + connectTimeout);
7376
pluginLogger.log(3, "["+getPluginName()+"] getTimeout timeout: " + commandTimeout);
7477
pluginLogger.log(3, "["+getPluginName()+"] keepAliveInterval: " + keepAliveInterval);
78+
pluginLogger.log(3, "["+getPluginName()+"] keepAliveMaxCount: " + keepAliveCount);
7579
pluginLogger.log(3, "["+getPluginName()+"] retry: " + retry);
7680
pluginLogger.log(3, "["+getPluginName()+"] retryCount: " + retryCount);
7781

@@ -94,6 +98,17 @@ public void connect(SSHClient ssh){
9498
retryCount=0;
9599
}
96100

101+
if (keepAliveInterval != 0) {
102+
KeepAliveRunner keepAlive = (KeepAliveRunner)ssh.getConnection().getKeepAlive();
103+
keepAlive.setKeepAliveInterval(keepAliveInterval);
104+
105+
if(keepAliveCount != 0){
106+
keepAlive.setMaxAliveCount(keepAliveCount);
107+
}
108+
}
109+
110+
111+
97112
while(count <= retryCount) {
98113
try {
99114
pluginLogger.log(3, "["+getPluginName()+"] open connection");
@@ -107,10 +122,6 @@ public void connect(SSHClient ssh){
107122

108123
authentication.authenticate(ssh);
109124

110-
if (keepAliveInterval != 0) {
111-
ssh.getConnection().getKeepAlive().setKeepAliveInterval(keepAliveInterval); //every 60sec
112-
}
113-
114125
pluginLogger.log(3, "["+getPluginName()+"] authentication set");
115126

116127

src/main/java/com/plugin/sshjplugin/model/SSHJConnection.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ static enum AuthenticationType {
5151

5252
int getKeepAliveInterval();
5353

54+
int getKeepAliveMaxAlive();
55+
5456
int getRetryCounter();
5557

5658
boolean isRetryEnabled();

src/main/java/com/plugin/sshjplugin/model/SSHJConnectionParameters.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ public int getKeepAliveInterval() {
216216

217217
}
218218

219+
@Override
220+
public int getKeepAliveMaxAlive() {
221+
return propertyResolver.resolveLongFwk(
222+
SSHJNodeExecutorPlugin.NODE_ATTR_SSH_KEEP_MAX_ALIVE_COUNT,
223+
SSHJNodeExecutorPlugin.FWK_PROP_SSH_KEEP_ALIVE_MAX_ALIVE_COUNT,
224+
0).intValue();
225+
}
226+
219227
@Override
220228
public int getRetryCounter() {
221229
return propertyResolver.resolveLongFwk(

0 commit comments

Comments
 (0)