Skip to content

Commit 317559d

Browse files
committed
Supports only service account based authentication. removed other authentication strategy.
1 parent f307c5f commit 317559d

11 files changed

+307
-509
lines changed

src/main/java/cd/go/contrib/elasticagent/KubernetesClientFactory.java

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@
2020
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
2121
import io.fabric8.kubernetes.client.KubernetesClient;
2222

23-
import java.util.Base64;
24-
2523
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
26-
import static cd.go.contrib.elasticagent.model.AuthenticationStrategy.CLUSTER_CERTS;
27-
import static cd.go.contrib.elasticagent.model.AuthenticationStrategy.OAUTH_TOKEN;
2824
import static java.text.MessageFormat.format;
2925

3026
public class KubernetesClientFactory {
@@ -45,29 +41,16 @@ public synchronized KubernetesClient client(PluginSettings pluginSettings) {
4541
LOG.debug(format("Creating a new client because {0}.", (client == null) ? "client is null" : "plugin setting is changed"));
4642
this.pluginSettings = pluginSettings;
4743
this.client = createClientFor(pluginSettings);
48-
LOG.debug(format("New client is created using authentication strategy {0}.", pluginSettings.getAuthenticationStrategy()));
44+
LOG.debug("New client is created.");
4945
return this.client;
5046
}
5147

5248
private KubernetesClient createClientFor(PluginSettings pluginSettings) {
53-
LOG.debug(format("Creating config using authentication strategy {0}.", pluginSettings.getAuthenticationStrategy().name()));
54-
final ConfigBuilder configBuilder = new ConfigBuilder();
55-
56-
if (pluginSettings.getAuthenticationStrategy() == OAUTH_TOKEN) {
57-
configBuilder.withOauthToken(pluginSettings.getOauthToken());
58-
59-
} else if (pluginSettings.getAuthenticationStrategy() == CLUSTER_CERTS) {
60-
configBuilder
61-
.withMasterUrl(pluginSettings.getClusterUrl())
62-
.withCaCertData(pluginSettings.getCaCertData())
63-
.withClientKeyData(encodeToBase64(pluginSettings.getClientKeyData()))
64-
.withClientCertData(pluginSettings.getClientCertData());
65-
}
49+
final ConfigBuilder configBuilder = new ConfigBuilder()
50+
.withOauthToken(pluginSettings.getOauthToken())
51+
.withMasterUrl(pluginSettings.getClusterUrl())
52+
.withCaCertData(pluginSettings.getCaCertData());
6653

6754
return new DefaultKubernetesClient(configBuilder.build());
6855
}
69-
70-
private String encodeToBase64(String stringToEncode) {
71-
return Base64.getEncoder().encodeToString(stringToEncode.getBytes());
72-
}
7356
}

src/main/java/cd/go/contrib/elasticagent/PluginSettings.java

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package cd.go.contrib.elasticagent;
1818

19-
import cd.go.contrib.elasticagent.model.AuthenticationStrategy;
2019
import com.google.gson.annotations.Expose;
2120
import com.google.gson.annotations.SerializedName;
2221
import org.joda.time.Period;
@@ -37,29 +36,17 @@ public class PluginSettings {
3736
private Integer maxPendingPods = 10;
3837

3938
@Expose
40-
@SerializedName("authentication_strategy")
41-
private String authenticationStrategy = AuthenticationStrategy.OAUTH_TOKEN.name();
39+
@SerializedName("kubernetes_cluster_url")
40+
private String clusterUrl;
4241

4342
@Expose
4443
@SerializedName("oauth_token")
4544
private String oauthToken;
4645

47-
@Expose
48-
@SerializedName("kubernetes_cluster_url")
49-
private String clusterUrl;
50-
5146
@Expose
5247
@SerializedName("kubernetes_cluster_ca_cert")
5348
private String clusterCACertData;
5449

55-
@Expose
56-
@SerializedName("client_key_data")
57-
private String clientKeyData;
58-
59-
@Expose
60-
@SerializedName("client_cert_data")
61-
private String clientCertData;
62-
6350
private Period autoRegisterPeriod;
6451

6552
public PluginSettings() {
@@ -94,10 +81,6 @@ public String getGoServerUrl() {
9481
return goServerUrl;
9582
}
9683

97-
public AuthenticationStrategy getAuthenticationStrategy() {
98-
return AuthenticationStrategy.from(authenticationStrategy);
99-
}
100-
10184
public String getOauthToken() {
10285
return oauthToken;
10386
}
@@ -110,14 +93,6 @@ public String getCaCertData() {
11093
return clusterCACertData;
11194
}
11295

113-
public String getClientKeyData() {
114-
return clientKeyData;
115-
}
116-
117-
public String getClientCertData() {
118-
return clientCertData;
119-
}
120-
12196
@Override
12297
public boolean equals(Object o) {
12398
if (this == o) return true;
@@ -130,28 +105,19 @@ public boolean equals(Object o) {
130105
return false;
131106
if (maxPendingPods != null ? !maxPendingPods.equals(that.maxPendingPods) : that.maxPendingPods != null)
132107
return false;
133-
if (authenticationStrategy != null ? !authenticationStrategy.equals(that.authenticationStrategy) : that.authenticationStrategy != null)
134-
return false;
135108
if (clusterUrl != null ? !clusterUrl.equals(that.clusterUrl) : that.clusterUrl != null) return false;
136-
if (clusterCACertData != null ? !clusterCACertData.equals(that.clusterCACertData) : that.clusterCACertData != null)
137-
return false;
138109
if (oauthToken != null ? !oauthToken.equals(that.oauthToken) : that.oauthToken != null) return false;
139-
if (clientKeyData != null ? !clientKeyData.equals(that.clientKeyData) : that.clientKeyData != null)
140-
return false;
141-
return clientCertData != null ? clientCertData.equals(that.clientCertData) : that.clientCertData == null;
110+
return clusterCACertData != null ? clusterCACertData.equals(that.clusterCACertData) : that.clusterCACertData == null;
142111
}
143112

144113
@Override
145114
public int hashCode() {
146115
int result = goServerUrl != null ? goServerUrl.hashCode() : 0;
147116
result = 31 * result + (autoRegisterTimeout != null ? autoRegisterTimeout.hashCode() : 0);
148117
result = 31 * result + (maxPendingPods != null ? maxPendingPods.hashCode() : 0);
149-
result = 31 * result + (authenticationStrategy != null ? authenticationStrategy.hashCode() : 0);
150118
result = 31 * result + (clusterUrl != null ? clusterUrl.hashCode() : 0);
151-
result = 31 * result + (clusterCACertData != null ? clusterCACertData.hashCode() : 0);
152119
result = 31 * result + (oauthToken != null ? oauthToken.hashCode() : 0);
153-
result = 31 * result + (clientKeyData != null ? clientKeyData.hashCode() : 0);
154-
result = 31 * result + (clientCertData != null ? clientCertData.hashCode() : 0);
120+
result = 31 * result + (clusterCACertData != null ? clusterCACertData.hashCode() : 0);
155121
return result;
156122
}
157123
}

src/main/java/cd/go/contrib/elasticagent/executors/GetPluginConfigurationExecutor.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,17 @@ public class GetPluginConfigurationExecutor implements RequestExecutor {
3131
public static final Field GO_SERVER_URL = new GoServerUrlField("go_server_url", "GoCD server URL", false, "0");
3232
public static final Field AUTO_REGISTER_TIMEOUT = new PositiveNumberField("auto_register_timeout", "Agent auto-register timeout (in minutes)", "10", false, false, "1");
3333
public static final Field MAX_PENDING_PODS = new PositiveNumberField("pending_pods_count", "Maximum pending pods", "10", false, false, "2");
34-
public static final Field AUTHENTICATION_STRATEGY = new NonBlankField("authentication_strategy", "Authentication strategy", false, "3");
35-
public static final Field OAUTH_TOKEN = new Field("oauth_token", "Oauth token", null, false, true, "4");
36-
public static final Field CLUSTER_URL = new SecureURLField("kubernetes_cluster_url", "Cluster URL", false, "5");
37-
public static final Field CLUSTER_CA_CERT = new Field("kubernetes_cluster_ca_cert", "Cluster ca certificate", null, false, true, "6");
38-
public static final Field CLIENT_KEY_DATA = new Field("client_key_data", "Client key data", null, false, true, "7");
39-
public static final Field CLIENT_CERT_DATA = new Field("client_cert_data", "client cert data", null, false, true, "8");
34+
public static final Field OAUTH_TOKEN = new NonBlankField("oauth_token", "Oauth token", true, "3");
35+
public static final Field CLUSTER_URL = new SecureURLField("kubernetes_cluster_url", "Cluster URL", true, "4");
36+
public static final Field CLUSTER_CA_CERT = new Field("kubernetes_cluster_ca_cert", "Cluster ca certificate", null, false, true, "5");
4037

4138
static {
4239
FIELDS.put(GO_SERVER_URL.key(), GO_SERVER_URL);
4340
FIELDS.put(AUTO_REGISTER_TIMEOUT.key(), AUTO_REGISTER_TIMEOUT);
4441
FIELDS.put(MAX_PENDING_PODS.key(), MAX_PENDING_PODS);
4542
FIELDS.put(CLUSTER_URL.key(), CLUSTER_URL);
4643
FIELDS.put(CLUSTER_CA_CERT.key(), CLUSTER_CA_CERT);
47-
FIELDS.put(AUTHENTICATION_STRATEGY.key(), AUTHENTICATION_STRATEGY);
4844
FIELDS.put(OAUTH_TOKEN.key(), OAUTH_TOKEN);
49-
FIELDS.put(CLIENT_KEY_DATA.key(), CLIENT_KEY_DATA);
50-
FIELDS.put(CLIENT_CERT_DATA.key(), CLIENT_CERT_DATA);
5145
}
5246

5347
public GoPluginApiResponse execute() {

src/main/java/cd/go/contrib/elasticagent/executors/ValidateConfigurationExecutor.java

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,21 @@
1919
import cd.go.contrib.elasticagent.PluginRequest;
2020
import cd.go.contrib.elasticagent.RequestExecutor;
2121
import cd.go.contrib.elasticagent.ServerRequestFailedException;
22-
import cd.go.contrib.elasticagent.model.AuthenticationStrategy;
2322
import cd.go.contrib.elasticagent.model.Field;
2423
import cd.go.contrib.elasticagent.model.ServerInfo;
2524
import cd.go.contrib.elasticagent.requests.ValidatePluginSettings;
2625
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
2726
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
28-
import org.apache.commons.lang3.StringUtils;
2927

3028
import java.util.ArrayList;
3129
import java.util.HashMap;
3230
import java.util.List;
3331
import java.util.Map;
3432

3533
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
36-
import static cd.go.contrib.elasticagent.executors.GetPluginConfigurationExecutor.*;
34+
import static cd.go.contrib.elasticagent.executors.GetPluginConfigurationExecutor.FIELDS;
35+
import static cd.go.contrib.elasticagent.executors.GetPluginConfigurationExecutor.GO_SERVER_URL;
3736
import static cd.go.contrib.elasticagent.utils.Util.GSON;
38-
import static java.text.MessageFormat.format;
3937
import static org.apache.commons.lang3.StringUtils.isBlank;
4038

4139
public class ValidateConfigurationExecutor implements RequestExecutor {
@@ -61,8 +59,6 @@ public GoPluginApiResponse execute() throws ServerRequestFailedException {
6159
}
6260

6361
validateGoServerUrl();
64-
validateAuthenticationDataBasedOnAuthenticationStrategy();
65-
System.out.println(GSON.toJson(result));
6662

6763
return DefaultGoPluginApiResponse.success(GSON.toJson(result));
6864
}
@@ -77,44 +73,6 @@ private void validateGoServerUrl() {
7773
}
7874
}
7975

80-
private void validateAuthenticationDataBasedOnAuthenticationStrategy() {
81-
try {
82-
final String authenticationStrategyStr = settings.get(AUTHENTICATION_STRATEGY.key());
83-
if (StringUtils.isBlank(authenticationStrategyStr)) {
84-
return;
85-
}
86-
87-
final AuthenticationStrategy authenticationStrategy = AuthenticationStrategy.from(authenticationStrategyStr);
88-
if (authenticationStrategy == AuthenticationStrategy.OAUTH_TOKEN && StringUtils.isBlank(settings.get(OAUTH_TOKEN.key()))) {
89-
result.add(error(OAUTH_TOKEN.key(), errorMessageWithAuthenticationStrategy(authenticationStrategy, OAUTH_TOKEN.displayName())));
90-
}
91-
92-
if (authenticationStrategy == AuthenticationStrategy.CLUSTER_CERTS) {
93-
if (StringUtils.isBlank(settings.get(CLUSTER_CA_CERT.key()))) {
94-
result.add(error(CLUSTER_CA_CERT.key(), errorMessageWithAuthenticationStrategy(authenticationStrategy, CLUSTER_CA_CERT.displayName())));
95-
}
96-
97-
if (StringUtils.isBlank(settings.get(CLIENT_KEY_DATA.key()))) {
98-
result.add(error(CLIENT_KEY_DATA.key(), errorMessageWithAuthenticationStrategy(authenticationStrategy, CLIENT_KEY_DATA.displayName())));
99-
}
100-
101-
if (StringUtils.isBlank(settings.get(CLIENT_CERT_DATA.key()))) {
102-
result.add(error(CLIENT_CERT_DATA.key(), errorMessageWithAuthenticationStrategy(authenticationStrategy, CLIENT_CERT_DATA.displayName())));
103-
}
104-
105-
if (StringUtils.isBlank(settings.get(CLUSTER_URL.key()))) {
106-
result.add(error(CLUSTER_URL.key(), errorMessageWithAuthenticationStrategy(authenticationStrategy, CLUSTER_URL.displayName())));
107-
}
108-
}
109-
} catch (Exception e) {
110-
result.add(error(AUTHENTICATION_STRATEGY.key(), e.getMessage()));
111-
}
112-
}
113-
114-
private String errorMessageWithAuthenticationStrategy(AuthenticationStrategy authenticationStrategy, String fieldName) {
115-
return format("{0} is required when authentication strategy is set to {1}.", fieldName, authenticationStrategy.name());
116-
}
117-
11876
private Map<String, String> error(String key, String errorMessage) {
11977
Map<String, String> error = new HashMap<>();
12078
error.put("key", key);

src/main/java/cd/go/contrib/elasticagent/model/AuthenticationStrategy.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/main/resources/plugin-settings.template.html

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@
7878
</div>
7979

8080
<div class="row">
81-
<label>Agent auto-register timeout (in minutes)<span class='asterix'>*</span></label>
81+
<label>Agent auto-register timeout (in minutes)</label>
8282
<input type="text" ng-model="auto_register_timeout" ng-required="true"/>
8383
<span class="form_error" ng-show="GOINPUTNAME[auto_register_timeout].$error.server">{{GOINPUTNAME[auto_register_timeout].$error.server}}</span>
8484
<label class="form-help-content">Defaults to <code>10 minutes</code>.</label>
8585
</div>
8686

8787
<div class="row">
88-
<label>Maximum pending pods<span class='asterix'>*</span></label>
88+
<label>Maximum pending pods</label>
8989
<input type="text" ng-model="pending_pods_count" ng-required="true"/>
9090
<span class="form_error" ng-show="GOINPUTNAME[pending_pods_count].$error.server">{{GOINPUTNAME[pending_pods_count].$error.server}}</span>
9191
<label class="form-help-content">Defaults to <code>10 pods</code>.</label>
@@ -94,68 +94,34 @@
9494
<fieldset>
9595
<legend>Cluster credentials</legend>
9696
<div class="row">
97-
<div class="columns small-3 medium-2 larger-2 no-padding">
98-
<label>Authentication Strategy</label>
99-
</div>
100-
<div class="columns small-9 medium-10 larger-10 no-padding"
101-
ng-init="authentication_strategy = (authentication_strategy || 'OAUTH_TOKEN')">
102-
<input type="radio" ng-model="authentication_strategy" value="OAUTH_TOKEN" id="oauth-token"
103-
ng-checked="(authentication_strategy == 'OAUTH_TOKEN')"/>
104-
<label for="oauth-token">OAuth token</label>
105-
106-
<input type="radio" ng-model="authentication_strategy" value="CLUSTER_CERTS"
107-
id="cluster-certs"/>
108-
<label for="cluster-certs">Cluster credentials</label>
109-
<span class="form_error form-error" ng-show="GOINPUTNAME[authentication_strategy].$error.server">
110-
{{GOINPUTNAME[authentication_strategy].$error.server}}
111-
</span>
112-
</div>
97+
<label>Cluster URL<span class='asterix'>*</span></label>
98+
<input type="text" ng-model="kubernetes_cluster_url" ng-required="true"/>
99+
<span class="form_error" ng-show="GOINPUTNAME[kubernetes_cluster_url].$error.server">{{GOINPUTNAME[kubernetes_cluster_url].$error.server}}</span>
100+
<label class="form-help-content">
101+
Kubernetes Cluster URL. Can be obtained by running <code>kubectl cluster-info</code>
102+
</label>
113103
</div>
114104

115-
<div ng-show="authentication_strategy == 'OAUTH_TOKEN'" class="row">
105+
<div class="row">
116106
<label>OAuth token
117107
<span class='asterix'>*</span>
118108
</label>
119109
<textarea rows="5" ng-model="oauth_token"></textarea>
120110
<span class="form_error form-error" ng-show="GOINPUTNAME[oauth_token].$error.server">{{GOINPUTNAME[oauth_token].$error.server}}</span>
111+
<label class="form-help-content">
112+
Get the service account token by running following command <code>kubectl describe secret
113+
TOKEN_NAME</code> and copy the value of token here.
114+
</label>
121115
</div>
122116

123-
<div ng-show="authentication_strategy == 'CLUSTER_CERTS'">
124-
<div class="row">
125-
<label>Cluster URL<span class='asterix'>*</span></label>
126-
<input type="text" ng-model="kubernetes_cluster_url" ng-required="true"/>
127-
<span class="form_error" ng-show="GOINPUTNAME[kubernetes_cluster_url].$error.server">{{GOINPUTNAME[kubernetes_cluster_url].$error.server}}</span>
128-
<label class="form-help-content">
129-
Kubernetes Cluster URL. Can be obtained by running <code>kubectl cluster-info</code>
130-
</label>
131-
</div>
132117

133-
<div class="row">
134-
<label>Cluster ca certificate data<span class='asterix'>*</span></label>
135-
<textarea ng-model="kubernetes_cluster_ca_cert" rows="7"></textarea>
136-
<span class="form_error" ng-show="GOINPUTNAME[kubernetes_cluster_ca_cert].$error.server">{{GOINPUTNAME[kubernetes_cluster_ca_cert].$error.server}}</span>
137-
<label class="form-help-content">
138-
Kubernetes cluster ca certificate data.
139-
</label>
140-
</div>
141-
142-
<div class="row">
143-
<label>Cluster client key data<span class='asterix'>*</span></label>
144-
<textarea ng-model="client_key_data" rows="7"></textarea>
145-
<span class="form_error" ng-show="GOINPUTNAME[client_key_data].$error.server">{{GOINPUTNAME[client_key_data].$error.server}}</span>
146-
<label class="form-help-content">
147-
Kubernetes cluster client key data.
148-
</label>
149-
</div>
150-
151-
<div class="row">
152-
<label>Cluster client key<span class='asterix'>*</span></label>
153-
<textarea ng-model="client_cert_data" rows="7"></textarea>
154-
<span class="form_error" ng-show="GOINPUTNAME[client_cert_data].$error.server">{{GOINPUTNAME[client_cert_data].$error.server}}</span>
155-
<label class="form-help-content">
156-
Kubernetes cluster client certificate data.
157-
</label>
158-
</div>
118+
<div class="row">
119+
<label>Cluster ca certificate data<span class='asterix'>*</span></label>
120+
<textarea ng-model="kubernetes_cluster_ca_cert" rows="7"></textarea>
121+
<span class="form_error" ng-show="GOINPUTNAME[kubernetes_cluster_ca_cert].$error.server">{{GOINPUTNAME[kubernetes_cluster_ca_cert].$error.server}}</span>
122+
<label class="form-help-content">
123+
Kubernetes cluster ca certificate data.
124+
</label>
159125
</div>
160126
</fieldset>
161127
</div>

0 commit comments

Comments
 (0)