Skip to content

Commit 670df79

Browse files
committed
Re-use SSL connections by specifying a user principal as part of all remoting connections.
1 parent 3a1d64d commit 670df79

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

agent/resources/applicationContext.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<property name="httpInvokerRequestExecutor">
4747
<bean class="com.thoughtworks.go.agent.GoHttpClientHttpInvokerRequestExecutor">
4848
<constructor-arg ref="httpClient"/>
49+
<constructor-arg ref="systemEnvironment"/>
4950
</bean>
5051
</property>
5152
</bean>

agent/src/com/thoughtworks/go/agent/GoHttpClientHttpInvokerRequestExecutor.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
package com.thoughtworks.go.agent;
1818

1919
import com.thoughtworks.go.agent.common.ssl.GoAgentServerHttpClient;
20+
import com.thoughtworks.go.util.SystemEnvironment;
2021
import org.apache.http.Header;
2122
import org.apache.http.HttpResponse;
2223
import org.apache.http.NoHttpResponseException;
2324
import org.apache.http.StatusLine;
2425
import org.apache.http.client.methods.CloseableHttpResponse;
2526
import org.apache.http.client.methods.HttpPost;
27+
import org.apache.http.client.protocol.HttpClientContext;
2628
import org.apache.http.entity.ByteArrayEntity;
29+
import org.apache.http.protocol.BasicHttpContext;
2730
import org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor;
2831
import org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration;
2932
import org.springframework.remoting.support.RemoteInvocationResult;
@@ -35,9 +38,11 @@
3538

3639
public class GoHttpClientHttpInvokerRequestExecutor extends AbstractHttpInvokerRequestExecutor {
3740
private final GoAgentServerHttpClient goAgentServerHttpClient;
41+
private final SystemEnvironment environment;
3842

39-
public GoHttpClientHttpInvokerRequestExecutor(GoAgentServerHttpClient goAgentServerHttpClient) {
43+
public GoHttpClientHttpInvokerRequestExecutor(GoAgentServerHttpClient goAgentServerHttpClient, SystemEnvironment environment) {
4044
this.goAgentServerHttpClient = goAgentServerHttpClient;
45+
this.environment = environment;
4146
}
4247

4348
@Override
@@ -48,7 +53,14 @@ protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration
4853
entity.setContentType(getContentType());
4954
postMethod.setEntity(entity);
5055

51-
try (CloseableHttpResponse response = goAgentServerHttpClient.execute(postMethod)) {
56+
BasicHttpContext context = null;
57+
58+
if (environment.useSslContext()) {
59+
context = new BasicHttpContext();
60+
context.setAttribute(HttpClientContext.USER_TOKEN, goAgentServerHttpClient.principal());
61+
}
62+
63+
try (CloseableHttpResponse response = goAgentServerHttpClient.execute(postMethod, context)) {
5264
validateResponse(response);
5365
InputStream responseBody = getResponseBody(response);
5466
return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());

base/src/com/thoughtworks/go/agent/common/ssl/GoAgentServerHttpClient.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.http.impl.client.CloseableHttpClient;
2525
import org.apache.http.protocol.HttpContext;
2626

27+
import javax.security.auth.x500.X500Principal;
2728
import java.io.Closeable;
2829
import java.io.IOException;
2930

@@ -32,14 +33,17 @@ public class GoAgentServerHttpClient implements Closeable {
3233

3334
private final SystemEnvironment systemEnvironment;
3435
private CloseableHttpClient client;
36+
private X500Principal principal;
3537

3638
public GoAgentServerHttpClient(SystemEnvironment systemEnvironment) {
3739
this.systemEnvironment = systemEnvironment;
3840
}
3941

4042
// called by spring
4143
public void init() throws Exception {
42-
this.client = new GoAgentServerHttpClientBuilder(systemEnvironment).httpClient();
44+
GoAgentServerHttpClientBuilder builder = new GoAgentServerHttpClientBuilder(systemEnvironment);
45+
this.client = builder.httpClient();
46+
this.principal = builder.principal();
4347
}
4448

4549

@@ -71,4 +75,8 @@ public synchronized void close() {
7175
public void reset() throws IOException {
7276
close();
7377
}
78+
79+
public X500Principal principal() {
80+
return this.principal;
81+
}
7482
}

base/src/com/thoughtworks/go/agent/common/ssl/GoAgentServerHttpClientBuilder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.http.ssl.SSLContextBuilder;
2929

3030
import javax.net.ssl.HostnameVerifier;
31+
import javax.security.auth.x500.X500Principal;
3132
import java.io.*;
3233
import java.security.*;
3334
import java.security.cert.CertificateException;
@@ -134,4 +135,15 @@ private InputStream keyStoreInputStream() throws FileNotFoundException {
134135
return !keyStoreFile.exists() ? null : new FileInputStream(keyStoreFile);
135136
}
136137

138+
public X500Principal principal() {
139+
try {
140+
KeyStore keyStore = agentKeystore();
141+
if (keyStore.containsAlias("agent")) {
142+
return ((X509Certificate) keyStore.getCertificate("agent")).getSubjectX500Principal();
143+
}
144+
} catch (Exception e) {
145+
// ignore
146+
}
147+
return null;
148+
}
137149
}

base/src/com/thoughtworks/go/util/SystemEnvironment.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public class SystemEnvironment implements Serializable, ConfigDirProvider {
193193
public static GoSystemProperty<Integer> MAX_PENDING_AGENTS_ALLOWED = new GoIntSystemProperty("max.pending.agents.allowed", 100);
194194
public static GoSystemProperty<Boolean> CHECK_AND_REMOVE_DUPLICATE_MODIFICATIONS = new GoBooleanSystemProperty("go.modifications.removeDuplicates", true);
195195
public static GoSystemProperty<String> GO_AGENT_KEYSTORE_PASSWORD = new GoStringSystemProperty("go.agent.keystore.password", "agent5s0repa55w0rd");
196-
196+
private static final GoSystemProperty<Boolean> GO_AGENT_USE_SSL_CONTEXT = new GoBooleanSystemProperty("go.agent.reuse.ssl.context", true);
197197
public static final GoSystemProperty<? extends Boolean> ENABLE_BUILD_COMMAND_PROTOCOL = new GoBooleanSystemProperty("go.agent.enableBuildCommandProtocol", false);
198198

199199
private final static Map<String, String> GIT_ALLOW_PROTOCOL;
@@ -765,6 +765,10 @@ public boolean isApiSafeModeEnabled() {
765765
return GO_API_WITH_SAFE_MODE.getValue();
766766
}
767767

768+
public boolean useSslContext() {
769+
return GO_AGENT_USE_SSL_CONTEXT.getValue();
770+
}
771+
768772
public static abstract class GoSystemProperty<T> {
769773
private String propertyName;
770774
private T defaultValue;

0 commit comments

Comments
 (0)