Skip to content

Commit

Permalink
Advanced Credentials option for RPC_SOCKET_SO_TIMEOUT_NICK
Browse files Browse the repository at this point in the history
JENKINS-31196
  • Loading branch information
p4paul committed Nov 9, 2015
1 parent 0ade8df commit e80c3ea
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 77 deletions.
Expand Up @@ -12,19 +12,22 @@ public class ConnectionConfig implements Serializable {
private final boolean ssl;
private final String serverUri;
private final String trust;
private final int timeout;

public ConnectionConfig(P4BaseCredentials credential) {
this.p4port = credential.getP4port();
this.ssl = credential.isSsl();
this.trust = credential.getTrust();
this.serverUri = toUri();
this.timeout = credential.getTimeout();
}

public ConnectionConfig(String p4port, boolean ssl, String trust) {
this.p4port = p4port;
this.ssl = ssl;
this.trust = trust;
this.serverUri = toUri();
this.timeout = 0;
}

private String toUri() {
Expand All @@ -51,6 +54,10 @@ public String getServerUri() {
return serverUri;
}

public int getTimeout() {
return timeout;
}

public String toString() {
return serverUri;
}
Expand Down
Expand Up @@ -103,7 +103,8 @@ private static IOptionsServer getRawConnection(ConnectionConfig config)
props.put(RpcPropertyDefs.RPC_RELAX_CMD_NAME_CHECKS_NICK, "true");

// disable timeout for slow servers / large db lock times
props.put(RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_NICK, "0");
String timeout = String.valueOf(config.getTimeout());
props.put(RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_NICK, timeout);

// Get a server connection
String serverUri = config.getServerUri();
Expand Down
Expand Up @@ -22,6 +22,9 @@ public abstract class P4BaseCredentials extends BaseStandardCredentials {

@CheckForNull
private final String retry;

@CheckForNull
private final String timeout;

/**
* Constructor.
Expand All @@ -36,12 +39,13 @@ public abstract class P4BaseCredentials extends BaseStandardCredentials {
public P4BaseCredentials(CredentialsScope scope, String id,
String description, @CheckForNull String p4port,
@CheckForNull TrustImpl ssl, @CheckForNull String username,
@CheckForNull String retry) {
@CheckForNull String retry, @CheckForNull String timeout) {
super(scope, id, description);
this.p4port = Util.fixNull(p4port);
this.ssl = ssl;
this.username = Util.fixNull(username);
this.retry = retry;
this.timeout = timeout;
}

@CheckForNull
Expand Down Expand Up @@ -70,4 +74,12 @@ public int getRetry() {
return 0;
}
}

public int getTimeout() {
if (timeout != null && !timeout.isEmpty()) {
return Integer.parseInt(timeout);
} else {
return 0;
}
}
}
@@ -1,9 +1,5 @@
package org.jenkinsci.plugins.p4.credentials;

import hudson.Extension;
import hudson.util.FormValidation;
import hudson.util.Secret;

import java.io.IOException;

import javax.servlet.ServletException;
Expand All @@ -17,6 +13,9 @@
import com.cloudbees.plugins.credentials.CredentialsScope;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.Extension;
import hudson.util.FormValidation;
import hudson.util.Secret;

public class P4PasswordImpl extends P4BaseCredentials {

Expand All @@ -29,12 +28,11 @@ public class P4PasswordImpl extends P4BaseCredentials {
private final Secret password;

@DataBoundConstructor
public P4PasswordImpl(CredentialsScope scope, String id,
String description, @CheckForNull String p4port, TrustImpl ssl,
@CheckForNull String username, @CheckForNull String retry,
public P4PasswordImpl(CredentialsScope scope, String id, String description, @CheckForNull String p4port,
TrustImpl ssl, @CheckForNull String username, @CheckForNull String retry, @CheckForNull String timeout,
@CheckForNull String password) {

super(scope, id, description, p4port, ssl, username, retry);
super(scope, id, description, p4port, ssl, username, retry, timeout);
this.password = Secret.fromString(password);
}

Expand All @@ -44,8 +42,7 @@ public Secret getPassword() {
}

@Extension
public static class DescriptorImpl extends
BaseStandardCredentialsDescriptor {
public static class DescriptorImpl extends BaseStandardCredentialsDescriptor {

@Override
public String getDisplayName() {
Expand All @@ -54,24 +51,19 @@ public String getDisplayName() {

public FormValidation doCheckP4port(@QueryParameter String value) {
if (value != null && value.startsWith("ssl:")) {
return FormValidation
.error("Do not prefix P4PORT with 'ssl:', use the SSL checkbox.");
return FormValidation.error("Do not prefix P4PORT with 'ssl:', use the SSL checkbox.");
}
return FormValidation.ok();
}

public FormValidation doTestConnection(
@QueryParameter("p4port") String p4port,
@QueryParameter("ssl") String ssl,
@QueryParameter("trust") String trust,
@QueryParameter("username") String username,
@QueryParameter("retry") String retry,
@QueryParameter("password") String password)
throws IOException, ServletException {
public FormValidation doTestConnection(@QueryParameter("p4port") String p4port,
@QueryParameter("ssl") String ssl, @QueryParameter("trust") String trust,
@QueryParameter("username") String username, @QueryParameter("retry") String retry,
@QueryParameter("timeout") String timeout, @QueryParameter("password") String password)
throws IOException, ServletException {
try {
// Test connection path to Server
ConnectionConfig config = new ConnectionConfig(p4port,
"true".equals(ssl), trust);
ConnectionConfig config = new ConnectionConfig(p4port, "true".equals(ssl), trust);
FormValidation validation;
validation = ConnectionFactory.testConnection(config);
if (!FormValidation.ok().equals(validation)) {
Expand All @@ -82,8 +74,8 @@ public FormValidation doTestConnection(
TrustImpl sslTrust;
sslTrust = ("true".equals(ssl)) ? new TrustImpl(trust) : null;

P4PasswordImpl test = new P4PasswordImpl(null, null, null,
p4port, sslTrust, username, retry, password);
P4PasswordImpl test = new P4PasswordImpl(null, null, null, p4port, sslTrust, username, retry, timeout,
password);

ConnectionHelper p4 = new ConnectionHelper(test);

Expand All @@ -94,14 +86,12 @@ public FormValidation doTestConnection(
// Test authentication
p4.logout(); // invalidate any earlier ticket before test.
if (!p4.login()) {
return FormValidation
.error("Authentication Error: Unable to login.");
return FormValidation.error("Authentication Error: Unable to login.");
}

// Test minimum server version
if (!p4.checkVersion(20121)) {
return FormValidation
.error("Server version is too old (min 2012.1)");
return FormValidation.error("Server version is too old (min 2012.1)");
}
return FormValidation.ok("Success");
} catch (Exception e) {
Expand Down
@@ -1,8 +1,5 @@
package org.jenkinsci.plugins.p4.credentials;

import hudson.Extension;
import hudson.util.FormValidation;

import java.io.IOException;

import javax.servlet.ServletException;
Expand All @@ -16,6 +13,8 @@
import com.cloudbees.plugins.credentials.CredentialsScope;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.Extension;
import hudson.util.FormValidation;

public class P4TicketImpl extends P4BaseCredentials {

Expand All @@ -28,12 +27,11 @@ public class P4TicketImpl extends P4BaseCredentials {
private final TicketModeImpl ticket;

@DataBoundConstructor
public P4TicketImpl(CredentialsScope scope, String id, String description,
@CheckForNull String p4port, TrustImpl ssl,
@CheckForNull String username, @CheckForNull String retry,
public P4TicketImpl(CredentialsScope scope, String id, String description, @CheckForNull String p4port,
TrustImpl ssl, @CheckForNull String username, @CheckForNull String retry, @CheckForNull String timeout,
TicketModeImpl ticket) {

super(scope, id, description, p4port, ssl, username, retry);
super(scope, id, description, p4port, ssl, username, retry, timeout);
this.ticket = ticket;
}

Expand All @@ -56,8 +54,7 @@ public boolean isTicketPathSet() {
}

@Extension
public static class DescriptorImpl extends
BaseStandardCredentialsDescriptor {
public static class DescriptorImpl extends BaseStandardCredentialsDescriptor {

@Override
public String getDisplayName() {
Expand All @@ -66,26 +63,20 @@ public String getDisplayName() {

public FormValidation doCheckP4port(@QueryParameter String value) {
if (value != null && value.startsWith("ssl:")) {
return FormValidation
.error("Do not prefix P4PORT with 'ssl:', use the SSL checkbox.");
return FormValidation.error("Do not prefix P4PORT with 'ssl:', use the SSL checkbox.");
}
return FormValidation.ok();
}

public FormValidation doTestConnection(
@QueryParameter("p4port") String p4port,
@QueryParameter("ssl") String ssl,
@QueryParameter("trust") String trust,
@QueryParameter("username") String username,
@QueryParameter("retry") String retry,
@QueryParameter("ticket") String value,
@QueryParameter("ticketValue") String ticketValue,
@QueryParameter("ticketPath") String ticketPath)
throws IOException, ServletException {
public FormValidation doTestConnection(@QueryParameter("p4port") String p4port,
@QueryParameter("ssl") String ssl, @QueryParameter("trust") String trust,
@QueryParameter("username") String username, @QueryParameter("retry") String retry,
@QueryParameter("timeout") String timeout, @QueryParameter("ticket") String value,
@QueryParameter("ticketValue") String ticketValue, @QueryParameter("ticketPath") String ticketPath)
throws IOException, ServletException {
try {
// Test connection path to Server
ConnectionConfig config = new ConnectionConfig(p4port,
"true".equals(ssl), trust);
ConnectionConfig config = new ConnectionConfig(p4port, "true".equals(ssl), trust);
FormValidation validation;
validation = ConnectionFactory.testConnection(config);
if (!FormValidation.ok().equals(validation)) {
Expand All @@ -99,8 +90,8 @@ public FormValidation doTestConnection(
TicketModeImpl ticket;
ticket = new TicketModeImpl(value, ticketValue, ticketPath);

P4TicketImpl test = new P4TicketImpl(null, null, null, p4port,
sslTrust, username, retry, ticket);
P4TicketImpl test = new P4TicketImpl(null, null, null, p4port, sslTrust, username, retry, timeout,
ticket);

ConnectionHelper p4 = new ConnectionHelper(test);

Expand All @@ -111,19 +102,16 @@ public FormValidation doTestConnection(
// Test authentication
// Do not logout, before test (preserve tickets)
if (!p4.login()) {
return FormValidation
.error("Authentication Error: Unable to login.");
return FormValidation.error("Authentication Error: Unable to login.");
}

// Test minimum server version
if (!p4.checkVersion(20121)) {
return FormValidation
.error("Server version is too old (min 2012.1)");
return FormValidation.error("Server version is too old (min 2012.1)");
}
return FormValidation.ok("Success");
} catch (Exception e) {
return FormValidation.error("Connection Error: "
+ e.getMessage());
return FormValidation.error("Connection Error: " + e.getMessage());
}
}
}
Expand Down
Expand Up @@ -20,9 +20,15 @@
<f:password/>
</f:entry>

<f:entry title="${%Retry}" field="retry" value="0">
<f:textbox/>
</f:entry>
<f:advanced>
<f:entry title="${%Retry}" field="retry" value="0">
<f:textbox/>
</f:entry>

<f:entry title="${%RPC_SOCKET_SO_TIMEOUT_NICK}" field="timeout" value="0">
<f:textbox/>
</f:entry>
</f:advanced>

<f:validateButton title="${%Test Connection}" progress="${%Testing...}"
method="testConnection" with="p4port,ssl,trust,username,password" />
Expand Down
@@ -0,0 +1,4 @@
<div>
<b>RPC_SOCKET_SO_TIMEOUT_NICK</b>
<p>The number of milliseconds to use for RPC socket read or write timeouts. If set to zero, timeouts are disabled.</p>
</div>
Expand Up @@ -28,9 +28,15 @@
</f:entry>
</f:radioBlock>

<f:entry title="${%Retry}" field="retry" value="0">
<f:textbox/>
</f:entry>
<f:advanced>
<f:entry title="${%Retry}" field="retry" value="0">
<f:textbox/>
</f:entry>

<f:entry title="${%RPC_SOCKET_SO_TIMEOUT_NICK}" field="timeout" value="0">
<f:textbox/>
</f:entry>
</f:advanced>

<f:validateButton title="${%Test Connection}" progress="${%Testing...}"
method="testConnection" with="p4port,ssl,trust,username,ticket,ticketValue,ticketPath" />
Expand Down
@@ -0,0 +1,4 @@
<div>
<b>RPC_SOCKET_SO_TIMEOUT_NICK</b>
<p>The number of milliseconds to use for RPC socket read or write timeouts. If set to zero, timeouts are disabled.</p>
</div>
4 changes: 2 additions & 2 deletions src/test/java/org/jenkinsci/plugins/p4/P4Server.java
Expand Up @@ -66,7 +66,7 @@ public void start() throws Exception {
logger.info("start signal sent...");

P4PasswordImpl auth = new P4PasswordImpl(CredentialsScope.SYSTEM, "id",
"desc", p4port, null, "admin", "0", "Password");
"desc", p4port, null, "admin", "0", "0", "Password");

int retry = 0;
while (retry < 20) {
Expand All @@ -81,7 +81,7 @@ public void start() throws Exception {

public void stop() throws Exception {
P4PasswordImpl auth = new P4PasswordImpl(CredentialsScope.SYSTEM, "id",
"desc", p4port, null, "admin", "0", "Password");
"desc", p4port, null, "admin", "0", "0", "Password");

p4 = new ConnectionHelper(auth);
p4.login();
Expand Down
Expand Up @@ -131,7 +131,7 @@ public static void cleanServer() throws Exception {

private P4PasswordImpl createCredentials() throws IOException {
P4PasswordImpl auth = new P4PasswordImpl(CredentialsScope.SYSTEM,
credential, "desc", P4PORT, null, "jenkins", "0", "jenkins");
credential, "desc", P4PORT, null, "jenkins", "0", "0", "jenkins");
SystemCredentialsProvider.getInstance().getCredentials().add(auth);
SystemCredentialsProvider.getInstance().save();
return auth;
Expand Down Expand Up @@ -189,7 +189,7 @@ public void testFreeStyleProject_buildHead() throws Exception {
assertEquals("Perforce Password Credential", desc.getDisplayName());
P4PasswordImpl.DescriptorImpl impl = (P4PasswordImpl.DescriptorImpl) desc;
FormValidation form = impl.doTestConnection(P4PORT, "false", null,
"jenkins", "0", "jenkins");
"jenkins", "0", "0", "jenkins");
assertEquals(FormValidation.Kind.OK, form.kind);
}

Expand Down

0 comments on commit e80c3ea

Please sign in to comment.