Skip to content

Commit 03e92cb

Browse files
committed
Removed username and password from plugin settings page
1 parent 0f79663 commit 03e92cb

File tree

8 files changed

+163
-181
lines changed

8 files changed

+163
-181
lines changed

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
import io.fabric8.kubernetes.client.ConfigBuilder;
2121
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
2222
import io.fabric8.kubernetes.client.KubernetesClient;
23-
import org.apache.commons.lang3.StringUtils;
23+
24+
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
2425

2526
public class KubernetesClientFactory {
2627
private static final KubernetesClientFactory KUBERNETES_CLIENT_FACTORY = new KubernetesClientFactory();
@@ -31,29 +32,22 @@ public static KubernetesClientFactory instance() {
3132
return KUBERNETES_CLIENT_FACTORY;
3233
}
3334

34-
private static KubernetesClient createClient(PluginSettings pluginSettings) throws Exception {
35-
ConfigBuilder configBuilder = new ConfigBuilder().withMasterUrl(pluginSettings.getKubernetesClusterUrl());
36-
if (StringUtils.isNotBlank(pluginSettings.getKubernetesClusterUsername())) {
37-
configBuilder.withUsername(pluginSettings.getKubernetesClusterUsername());
38-
}
39-
40-
if (StringUtils.isNotBlank(pluginSettings.getKubernetesClusterPassword())) {
41-
configBuilder.withPassword(pluginSettings.getKubernetesClusterPassword());
42-
}
43-
44-
if (StringUtils.isNotBlank(pluginSettings.getKubernetesClusterCACert())) {
45-
configBuilder.withCaCertData(pluginSettings.getKubernetesClusterCACert());
46-
}
35+
private static KubernetesClient createClient(PluginSettings pluginSettings) {
36+
Config config = new ConfigBuilder()
37+
.withMasterUrl(pluginSettings.getKubernetesClusterUrl())
38+
.withCaCertData(pluginSettings.getKubernetesClusterCACert())
39+
.build();
4740

48-
Config build = configBuilder.build();
49-
return new DefaultKubernetesClient(build);
41+
return new DefaultKubernetesClient(config);
5042
}
5143

52-
public synchronized KubernetesClient kubernetes(PluginSettings pluginSettings) throws Exception {
44+
public synchronized KubernetesClient kubernetes(PluginSettings pluginSettings) {
5345
if (pluginSettings.equals(this.pluginSettings) && this.client != null) {
46+
LOG.debug("Using previously created client.");
5447
return this.client;
5548
}
5649

50+
LOG.debug("Client is null or plugin setting has been changed. Creating new client...");
5751
this.pluginSettings = pluginSettings;
5852
this.client = createClient(pluginSettings);
5953
return this.client;

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,16 @@ public PluginSettings getPluginSettings() throws ServerRequestFailedException {
5252
throw ServerRequestFailedException.getPluginSettings(response);
5353
}
5454

55-
final PluginSettings pluginSettings = PluginSettings.fromJSON(response.responseBody());
55+
PluginSettings pluginSettings = PluginSettings.fromJSON(response.responseBody());
56+
57+
if (pluginSettings == null) {
58+
pluginSettings = PluginSettings.fromEnv();
59+
}
60+
5661
if (pluginSettings == null) {
5762
throw new PluginSettingsNotConfiguredException();
5863
}
64+
5965
return pluginSettings;
6066
}
6167

@@ -71,7 +77,7 @@ public Agents listAgents() throws ServerRequestFailedException {
7177
}
7278

7379
public void disableAgents(Collection<Agent> toBeDisabled) throws ServerRequestFailedException {
74-
LOG.debug("[Server Ping] Disabling Agents:"+toBeDisabled.toString());
80+
LOG.debug("[Server Ping] Disabling Agents:" + toBeDisabled.toString());
7581
if (toBeDisabled.isEmpty()) {
7682
return;
7783
}
@@ -87,7 +93,7 @@ public void disableAgents(Collection<Agent> toBeDisabled) throws ServerRequestFa
8793
}
8894

8995
public void deleteAgents(Collection<Agent> toBeDeleted) throws ServerRequestFailedException {
90-
LOG.debug("[Server Ping] Deleting Agents:"+toBeDeleted.toString());
96+
LOG.debug("[Server Ping] Deleting Agents:" + toBeDeleted.toString());
9197
if (toBeDeleted.isEmpty()) {
9298
return;
9399
}

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

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
import com.google.gson.annotations.Expose;
2020
import com.google.gson.annotations.SerializedName;
21+
import org.apache.commons.lang3.StringUtils;
2122
import org.joda.time.Period;
2223

24+
import static cd.go.contrib.elasticagent.executors.GetPluginConfigurationExecutor.*;
2325
import static cd.go.contrib.elasticagent.utils.Util.GSON;
2426

2527
public class PluginSettings {
@@ -39,48 +41,23 @@ public class PluginSettings {
3941
@SerializedName("kubernetes_cluster_url")
4042
private String kubernetesClusterUrl;
4143

42-
@Expose
43-
@SerializedName("kubernetes_cluster_username")
44-
private String kubernetesClusterUsername;
45-
46-
@Expose
47-
@SerializedName("kubernetes_cluster_password")
48-
private String kubernetesClusterPassword;
49-
5044
@Expose
5145
@SerializedName("kubernetes_cluster_ca_cert")
5246
private String kubernetesClusterCACert;
5347

5448
private Period autoRegisterPeriod;
5549

56-
public static PluginSettings fromJSON(String json) {
57-
return GSON.fromJson(json, PluginSettings.class);
50+
public PluginSettings() {
5851
}
5952

60-
@Override
61-
public boolean equals(Object o) {
62-
if (this == o) return true;
63-
if (!(o instanceof PluginSettings)) return false;
64-
PluginSettings that = (PluginSettings) o;
65-
66-
if (goServerUrl != null ? !goServerUrl.equals(that.goServerUrl) : that.goServerUrl != null) return false;
67-
if (autoRegisterTimeout != null ? !autoRegisterTimeout.equals(that.autoRegisterTimeout) : that.autoRegisterTimeout != null)
68-
return false;
69-
if (pendingPodsCount != null ? !pendingPodsCount.equals(that.pendingPodsCount) : that.pendingPodsCount != null)
70-
return false;
71-
if (kubernetesClusterUrl != null ? !kubernetesClusterUrl.equals(that.kubernetesClusterUrl) : that.kubernetesClusterUrl != null)
72-
return false;
73-
return autoRegisterPeriod != null ? autoRegisterPeriod.equals(that.autoRegisterPeriod) : that.autoRegisterPeriod == null;
53+
public PluginSettings(String goServerUrl, String clusterUrl, String clusterCACert) {
54+
this.goServerUrl = goServerUrl;
55+
this.kubernetesClusterUrl = clusterUrl;
56+
this.kubernetesClusterCACert = clusterCACert;
7457
}
7558

76-
@Override
77-
public int hashCode() {
78-
int result = goServerUrl != null ? goServerUrl.hashCode() : 0;
79-
result = 31 * result + (autoRegisterTimeout != null ? autoRegisterTimeout.hashCode() : 0);
80-
result = 31 * result + (pendingPodsCount != null ? pendingPodsCount.hashCode() : 0);
81-
result = 31 * result + (kubernetesClusterUrl != null ? kubernetesClusterUrl.hashCode() : 0);
82-
result = 31 * result + (autoRegisterPeriod != null ? autoRegisterPeriod.hashCode() : 0);
83-
return result;
59+
public static PluginSettings fromJSON(String json) {
60+
return GSON.fromJson(json, PluginSettings.class);
8461
}
8562

8663
public Period getAutoRegisterPeriod() {
@@ -113,19 +90,53 @@ public String getKubernetesClusterUrl() {
11390
return kubernetesClusterUrl;
11491
}
11592

116-
public String getKubernetesClusterUsername() {
117-
return kubernetesClusterUsername;
118-
}
119-
120-
public String getKubernetesClusterPassword() {
121-
return kubernetesClusterPassword;
122-
}
123-
12493
public String getKubernetesClusterCACert() {
12594
return kubernetesClusterCACert;
12695
}
12796

12897
public void setGoServerUrl(String goServerUrl) {
12998
this.goServerUrl = goServerUrl;
13099
}
100+
101+
public static PluginSettings fromEnv() {
102+
final String goServerUrl = System.getenv(GO_SERVER_URL.key());
103+
final String clusterUrl = System.getenv(CLUSTER_URL.key());
104+
final String clusterCACert = System.getenv(CLUSTER_CA_CERT.key());
105+
106+
if (StringUtils.isAnyBlank(goServerUrl, clusterUrl, clusterCACert)) {
107+
return null;
108+
}
109+
110+
return new PluginSettings(goServerUrl, clusterUrl, clusterCACert);
111+
}
112+
113+
@Override
114+
public boolean equals(Object o) {
115+
if (this == o) return true;
116+
if (!(o instanceof PluginSettings)) return false;
117+
118+
PluginSettings that = (PluginSettings) o;
119+
120+
if (goServerUrl != null ? !goServerUrl.equals(that.goServerUrl) : that.goServerUrl != null) return false;
121+
if (autoRegisterTimeout != null ? !autoRegisterTimeout.equals(that.autoRegisterTimeout) : that.autoRegisterTimeout != null)
122+
return false;
123+
if (pendingPodsCount != null ? !pendingPodsCount.equals(that.pendingPodsCount) : that.pendingPodsCount != null)
124+
return false;
125+
if (kubernetesClusterUrl != null ? !kubernetesClusterUrl.equals(that.kubernetesClusterUrl) : that.kubernetesClusterUrl != null)
126+
return false;
127+
if (kubernetesClusterCACert != null ? !kubernetesClusterCACert.equals(that.kubernetesClusterCACert) : that.kubernetesClusterCACert != null)
128+
return false;
129+
return autoRegisterPeriod != null ? autoRegisterPeriod.equals(that.autoRegisterPeriod) : that.autoRegisterPeriod == null;
130+
}
131+
132+
@Override
133+
public int hashCode() {
134+
int result = goServerUrl != null ? goServerUrl.hashCode() : 0;
135+
result = 31 * result + (autoRegisterTimeout != null ? autoRegisterTimeout.hashCode() : 0);
136+
result = 31 * result + (pendingPodsCount != null ? pendingPodsCount.hashCode() : 0);
137+
result = 31 * result + (kubernetesClusterUrl != null ? kubernetesClusterUrl.hashCode() : 0);
138+
result = 31 * result + (kubernetesClusterCACert != null ? kubernetesClusterCACert.hashCode() : 0);
139+
result = 31 * result + (autoRegisterPeriod != null ? autoRegisterPeriod.hashCode() : 0);
140+
return result;
141+
}
131142
}

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,19 @@
3030
import static cd.go.contrib.elasticagent.utils.Util.GSON;
3131

3232
public class GetPluginConfigurationExecutor implements RequestExecutor {
33-
public static final Field GO_SERVER_URL = new GoServerUrlField("go_server_url", "Go Server URL", false, "0");
34-
public static final Field AUTOREGISTER_TIMEOUT = new PositiveNumberField("auto_register_timeout", "Agent auto-register Timeout (in minutes)", "10", true, false, "1");
35-
public static final Field MAXIMUM_PENDING_PODS_COUNT = new PositiveNumberField("pending_pods_count", "Maximum Pending Kuberneted Pods Count", "10", true, false, "2");
36-
public static final Field KUBERNETES_CLUSTER_URL = new SecureURLField("kubernetes_cluster_url", "Kubernetes Cluster URL", true, "3");
37-
public static final Field KUBERNETES_CLUSTER_USERNAME = new Field("kubernetes_cluster_username", "Kubernetes Cluster Username", null, false, false, "4");
38-
public static final Field KUBERNETES_CLUSTER_PASSWORD = new Field("kubernetes_cluster_password", "Kubernetes Cluster Password", null, false, true, "5");
39-
public static final Field KUBERNETES_CLUSTER_CA_CERT = new Field("kubernetes_cluster_ca_cert", "Kubernetes Cluster CA Certificate", null, false, true, "6");
33+
public static final Field GO_SERVER_URL = new GoServerUrlField("go_server_url", "GoCD server URL", false, "0");
34+
public static final Field AUTOREGISTER_TIMEOUT = new PositiveNumberField("auto_register_timeout", "Agent auto-register timeout (in minutes)", "10", true, false, "1");
35+
public static final Field MAXIMUM_PENDING_PODS_COUNT = new PositiveNumberField("pending_pods_count", "Maximum pending pods", "10", true, false, "2");
36+
public static final Field CLUSTER_URL = new SecureURLField("kubernetes_cluster_url", "Cluster URL", true, "3");
37+
public static final Field CLUSTER_CA_CERT = new Field("kubernetes_cluster_ca_cert", "Cluster ca-certificate", null, true, true, "4");
4038
public static final Map<String, Field> FIELDS = new LinkedHashMap<>();
4139

4240
static {
4341
FIELDS.put(GO_SERVER_URL.key(), GO_SERVER_URL);
4442
FIELDS.put(AUTOREGISTER_TIMEOUT.key(), AUTOREGISTER_TIMEOUT);
4543
FIELDS.put(MAXIMUM_PENDING_PODS_COUNT.key(), MAXIMUM_PENDING_PODS_COUNT);
46-
47-
FIELDS.put(KUBERNETES_CLUSTER_URL.key(), KUBERNETES_CLUSTER_URL);
48-
49-
FIELDS.put(KUBERNETES_CLUSTER_USERNAME.key(), KUBERNETES_CLUSTER_USERNAME);
50-
FIELDS.put(KUBERNETES_CLUSTER_PASSWORD.key(), KUBERNETES_CLUSTER_PASSWORD);
51-
FIELDS.put(KUBERNETES_CLUSTER_CA_CERT.key(), KUBERNETES_CLUSTER_CA_CERT);
44+
FIELDS.put(CLUSTER_URL.key(), CLUSTER_URL);
45+
FIELDS.put(CLUSTER_CA_CERT.key(), CLUSTER_CA_CERT);
5246
}
5347

5448
public GoPluginApiResponse execute() {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
import cd.go.contrib.elasticagent.requests.ValidatePluginSettings;
2525
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
2626
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
27-
import org.apache.commons.lang3.StringUtils;
2827

2928
import java.util.ArrayList;
3029
import java.util.HashMap;
3130
import java.util.Map;
3231

3332
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
3433
import static cd.go.contrib.elasticagent.utils.Util.GSON;
34+
import static org.apache.commons.lang3.StringUtils.isBlank;
3535

3636
public class ValidateConfigurationExecutor implements RequestExecutor {
3737
private final ValidatePluginSettings settings;
@@ -55,9 +55,9 @@ public GoPluginApiResponse execute() throws ServerRequestFailedException {
5555
}
5656
}
5757

58-
if(StringUtils.isBlank(settings.get("go_server_url"))) {
58+
if (isBlank(settings.get("go_server_url"))) {
5959
ServerInfo severInfo = pluginRequest.getSeverInfo();
60-
if(StringUtils.isBlank(severInfo.getSecureSiteUrl())) {
60+
if (isBlank(severInfo.getSecureSiteUrl())) {
6161
HashMap<String, String> error = new HashMap<>();
6262
error.put("key", "go_server_url");
6363
error.put("message", "Secure site url is not configured. Please specify Go Server Url.");

0 commit comments

Comments
 (0)