Skip to content

Commit c9f6e15

Browse files
committed
Added authentication strategy
1 parent 1a7b935 commit c9f6e15

24 files changed

+467
-179
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public KubernetesAgentInstances(KubernetesClientFactory factory, KubernetesInsta
5858

5959
@Override
6060
public KubernetesInstance create(CreateAgentRequest request, PluginSettings settings, PluginRequest pluginRequest) throws Exception {
61-
final Integer maxAllowedContainers = settings.getMaximumPendingAgentsCount();
61+
final Integer maxAllowedContainers = settings.getMaxPendingPods();
6262
synchronized (instances) {
6363
doWithLockOnSemaphore(new SetupSemaphore(maxAllowedContainers, instances, semaphore));
6464

@@ -84,7 +84,7 @@ private KubernetesInstance createKubernetesInstance(CreateAgentRequest request,
8484
return null;
8585
}
8686

87-
KubernetesClient client = factory.kubernetes(settings);
87+
KubernetesClient client = factory.client(settings);
8888
KubernetesInstance instance = kubernetesInstanceFactory.create(request, settings, client, pluginRequest, isUsingPodYaml(request));
8989
register(instance);
9090

@@ -109,7 +109,7 @@ private boolean isUsingPodYaml(CreateAgentRequest request) {
109109
public void terminate(String agentId, PluginSettings settings) throws Exception {
110110
KubernetesInstance instance = instances.get(agentId);
111111
if (instance != null) {
112-
KubernetesClient client = factory.kubernetes(settings);
112+
KubernetesClient client = factory.client(settings);
113113
instance.terminate(client);
114114
} else {
115115
LOG.warn("Requested to terminate an instance that does not exist " + agentId);
@@ -149,7 +149,7 @@ public Agents instancesCreatedAfterTimeout(PluginSettings settings, Agents agent
149149
@Override
150150
public void refreshAll(PluginRequest pluginRequest) throws Exception {
151151
LOG.debug("[Refresh Instances]. Syncing k8s elastic agent pod information");
152-
KubernetesClient client = factory.kubernetes(pluginRequest.getPluginSettings());
152+
KubernetesClient client = factory.client(pluginRequest.getPluginSettings());
153153
PodList list = client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE).list();
154154

155155
for (Pod pod : list.getItems()) {
@@ -174,7 +174,7 @@ private void register(KubernetesInstance instance) {
174174
private KubernetesAgentInstances unregisteredAfterTimeout(PluginSettings settings, Agents knownAgents) throws Exception {
175175
Period period = settings.getAutoRegisterPeriod();
176176
KubernetesAgentInstances unregisteredInstances = new KubernetesAgentInstances();
177-
KubernetesClient client = factory.kubernetes(settings);
177+
KubernetesClient client = factory.client(settings);
178178

179179
for (String instanceName : instances.keySet()) {
180180
if (knownAgents.containsAgentWithId(instanceName)) {

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
package cd.go.contrib.elasticagent;
1818

19-
import io.fabric8.kubernetes.client.Config;
2019
import io.fabric8.kubernetes.client.ConfigBuilder;
2120
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
2221
import io.fabric8.kubernetes.client.KubernetesClient;
2322

2423
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
24+
import static cd.go.contrib.elasticagent.model.AuthenticationStrategy.CLUSTER_CERTS;
25+
import static cd.go.contrib.elasticagent.model.AuthenticationStrategy.OAUTH_TOKEN;
26+
import static java.text.MessageFormat.format;
2527

2628
public class KubernetesClientFactory {
2729
private static final KubernetesClientFactory KUBERNETES_CLIENT_FACTORY = new KubernetesClientFactory();
@@ -32,24 +34,34 @@ public static KubernetesClientFactory instance() {
3234
return KUBERNETES_CLIENT_FACTORY;
3335
}
3436

35-
private static KubernetesClient createClient(PluginSettings pluginSettings) {
36-
Config config = new ConfigBuilder()
37-
.withMasterUrl(pluginSettings.getKubernetesClusterUrl())
38-
.withCaCertData(pluginSettings.getKubernetesClusterCACert())
39-
.build();
40-
41-
return new DefaultKubernetesClient(config);
42-
}
43-
44-
public synchronized KubernetesClient kubernetes(PluginSettings pluginSettings) {
37+
public synchronized KubernetesClient client(PluginSettings pluginSettings) {
4538
if (pluginSettings.equals(this.pluginSettings) && this.client != null) {
4639
LOG.debug("Using previously created client.");
4740
return this.client;
4841
}
4942

50-
LOG.debug("Client is null or plugin setting has been changed. Creating new client...");
43+
LOG.debug(format("Creating a new client because {0}.", (client == null) ? "client is null" : "plugin setting is changed"));
5144
this.pluginSettings = pluginSettings;
52-
this.client = createClient(pluginSettings);
45+
this.client = createClientFor(pluginSettings);
46+
LOG.debug(format("New client is created using authentication strategy {0}.", pluginSettings.getAuthenticationStrategy()));
5347
return this.client;
5448
}
49+
50+
private KubernetesClient createClientFor(PluginSettings pluginSettings) {
51+
LOG.debug(format("Creating config using authentication strategy {0}.", pluginSettings.getAuthenticationStrategy().name()));
52+
final ConfigBuilder configBuilder = new ConfigBuilder();
53+
54+
if (pluginSettings.getAuthenticationStrategy() == OAUTH_TOKEN) {
55+
configBuilder.withOauthToken(pluginSettings.getOauthToken());
56+
57+
} else if (pluginSettings.getAuthenticationStrategy() == CLUSTER_CERTS) {
58+
configBuilder
59+
.withMasterUrl(pluginSettings.getClusterUrl())
60+
.withCaCertData(pluginSettings.getCaCertData())
61+
.withClientKeyData(pluginSettings.getClientKeyData())
62+
.withClientCertData(pluginSettings.getClientCertData());
63+
}
64+
65+
return new DefaultKubernetesClient(configBuilder.build());
66+
}
5567
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ public PluginSettings getPluginSettings() throws ServerRequestFailedException {
5353
}
5454

5555
PluginSettings pluginSettings = PluginSettings.fromJSON(response.responseBody());
56-
57-
if (pluginSettings == null) {
58-
pluginSettings = PluginSettings.fromEnv();
59-
}
60-
6156
if (pluginSettings == null) {
6257
throw new PluginSettingsNotConfiguredException();
6358
}

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

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616

1717
package cd.go.contrib.elasticagent;
1818

19+
import cd.go.contrib.elasticagent.model.AuthenticationStrategy;
1920
import com.google.gson.annotations.Expose;
2021
import com.google.gson.annotations.SerializedName;
21-
import org.apache.commons.lang3.StringUtils;
2222
import org.joda.time.Period;
2323

24-
import static cd.go.contrib.elasticagent.executors.GetPluginConfigurationExecutor.*;
2524
import static cd.go.contrib.elasticagent.utils.Util.GSON;
2625

2726
public class PluginSettings {
@@ -31,29 +30,45 @@ public class PluginSettings {
3130

3231
@Expose
3332
@SerializedName("auto_register_timeout")
34-
private String autoRegisterTimeout;
33+
private Integer autoRegisterTimeout = 10;
3534

3635
@Expose
3736
@SerializedName("pending_pods_count")
38-
private String pendingPodsCount;
37+
private Integer maxPendingPods = 10;
38+
39+
@Expose
40+
@SerializedName("authentication_strategy")
41+
private String authenticationStrategy = AuthenticationStrategy.OAUTH_TOKEN.name();
42+
43+
@Expose
44+
@SerializedName("oauth_token")
45+
private String oauthToken;
3946

4047
@Expose
4148
@SerializedName("kubernetes_cluster_url")
42-
private String kubernetesClusterUrl;
49+
private String clusterUrl;
4350

4451
@Expose
4552
@SerializedName("kubernetes_cluster_ca_cert")
46-
private String kubernetesClusterCACert;
53+
private String clusterCACertData;
54+
55+
@Expose
56+
@SerializedName("client_key_data")
57+
private String clientKeyData;
58+
59+
@Expose
60+
@SerializedName("client_cert_data")
61+
private String clientCertData;
4762

4863
private Period autoRegisterPeriod;
4964

5065
public PluginSettings() {
5166
}
5267

53-
public PluginSettings(String goServerUrl, String clusterUrl, String clusterCACert) {
68+
public PluginSettings(String goServerUrl, String clusterUrl, String clusterCACertData) {
5469
this.goServerUrl = goServerUrl;
55-
this.kubernetesClusterUrl = clusterUrl;
56-
this.kubernetesClusterCACert = clusterCACert;
70+
this.clusterUrl = clusterUrl;
71+
this.clusterCACertData = clusterCACertData;
5772
}
5873

5974
public static PluginSettings fromJSON(String json) {
@@ -62,52 +77,45 @@ public static PluginSettings fromJSON(String json) {
6277

6378
public Period getAutoRegisterPeriod() {
6479
if (this.autoRegisterPeriod == null) {
65-
this.autoRegisterPeriod = new Period().withMinutes(Integer.parseInt(getAutoRegisterTimeout()));
80+
this.autoRegisterPeriod = new Period().withMinutes(getAutoRegisterTimeout());
6681
}
6782
return this.autoRegisterPeriod;
6883
}
6984

70-
String getAutoRegisterTimeout() {
71-
if (autoRegisterTimeout == null) {
72-
autoRegisterTimeout = "10";
73-
}
85+
Integer getAutoRegisterTimeout() {
7486
return autoRegisterTimeout;
7587
}
7688

77-
public Integer getMaximumPendingAgentsCount() {
78-
if (pendingPodsCount == null) {
79-
pendingPodsCount = "10";
80-
}
81-
82-
return Integer.valueOf(pendingPodsCount);
89+
public Integer getMaxPendingPods() {
90+
return Integer.valueOf(maxPendingPods);
8391
}
8492

8593
public String getGoServerUrl() {
8694
return goServerUrl;
8795
}
8896

89-
public String getKubernetesClusterUrl() {
90-
return kubernetesClusterUrl;
97+
public AuthenticationStrategy getAuthenticationStrategy() {
98+
return AuthenticationStrategy.from(authenticationStrategy);
9199
}
92100

93-
public String getKubernetesClusterCACert() {
94-
return kubernetesClusterCACert;
101+
public String getOauthToken() {
102+
return oauthToken;
95103
}
96104

97-
public void setGoServerUrl(String goServerUrl) {
98-
this.goServerUrl = goServerUrl;
105+
public String getClusterUrl() {
106+
return clusterUrl;
99107
}
100108

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());
109+
public String getCaCertData() {
110+
return clusterCACertData;
111+
}
105112

106-
if (StringUtils.isAnyBlank(goServerUrl, clusterUrl, clusterCACert)) {
107-
return null;
108-
}
113+
public String getClientKeyData() {
114+
return clientKeyData;
115+
}
109116

110-
return new PluginSettings(goServerUrl, clusterUrl, clusterCACert);
117+
public String getClientCertData() {
118+
return clientCertData;
111119
}
112120

113121
@Override
@@ -120,23 +128,30 @@ public boolean equals(Object o) {
120128
if (goServerUrl != null ? !goServerUrl.equals(that.goServerUrl) : that.goServerUrl != null) return false;
121129
if (autoRegisterTimeout != null ? !autoRegisterTimeout.equals(that.autoRegisterTimeout) : that.autoRegisterTimeout != null)
122130
return false;
123-
if (pendingPodsCount != null ? !pendingPodsCount.equals(that.pendingPodsCount) : that.pendingPodsCount != null)
131+
if (maxPendingPods != null ? !maxPendingPods.equals(that.maxPendingPods) : that.maxPendingPods != null)
132+
return false;
133+
if (authenticationStrategy != null ? !authenticationStrategy.equals(that.authenticationStrategy) : that.authenticationStrategy != null)
124134
return false;
125-
if (kubernetesClusterUrl != null ? !kubernetesClusterUrl.equals(that.kubernetesClusterUrl) : that.kubernetesClusterUrl != null)
135+
if (clusterUrl != null ? !clusterUrl.equals(that.clusterUrl) : that.clusterUrl != null) return false;
136+
if (clusterCACertData != null ? !clusterCACertData.equals(that.clusterCACertData) : that.clusterCACertData != null)
126137
return false;
127-
if (kubernetesClusterCACert != null ? !kubernetesClusterCACert.equals(that.kubernetesClusterCACert) : that.kubernetesClusterCACert != null)
138+
if (oauthToken != null ? !oauthToken.equals(that.oauthToken) : that.oauthToken != null) return false;
139+
if (clientKeyData != null ? !clientKeyData.equals(that.clientKeyData) : that.clientKeyData != null)
128140
return false;
129-
return autoRegisterPeriod != null ? autoRegisterPeriod.equals(that.autoRegisterPeriod) : that.autoRegisterPeriod == null;
141+
return clientCertData != null ? clientCertData.equals(that.clientCertData) : that.clientCertData == null;
130142
}
131143

132144
@Override
133145
public int hashCode() {
134146
int result = goServerUrl != null ? goServerUrl.hashCode() : 0;
135147
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);
148+
result = 31 * result + (maxPendingPods != null ? maxPendingPods.hashCode() : 0);
149+
result = 31 * result + (authenticationStrategy != null ? authenticationStrategy.hashCode() : 0);
150+
result = 31 * result + (clusterUrl != null ? clusterUrl.hashCode() : 0);
151+
result = 31 * result + (clusterCACertData != null ? clusterCACertData.hashCode() : 0);
152+
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);
140155
return result;
141156
}
142157
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public GoPluginApiResponse execute() throws Exception {
3939
String elasticAgentId = request.getElasticAgentId();
4040
JobIdentifier jobIdentifier = request.getJobIdentifier();
4141
LOG.info(String.format("[status-report] Generating status report for agent: %s with job: %s", elasticAgentId, jobIdentifier));
42-
KubernetesClient client = factory.kubernetes(pluginRequest.getPluginSettings());
42+
KubernetesClient client = factory.client(pluginRequest.getPluginSettings());
4343

4444
try {
4545
Pod pod;

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
package cd.go.contrib.elasticagent.executors;
1818

1919
import cd.go.contrib.elasticagent.RequestExecutor;
20-
import cd.go.contrib.elasticagent.model.Field;
21-
import cd.go.contrib.elasticagent.model.GoServerUrlField;
22-
import cd.go.contrib.elasticagent.model.PositiveNumberField;
23-
import cd.go.contrib.elasticagent.model.SecureURLField;
20+
import cd.go.contrib.elasticagent.model.*;
2421
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
2522
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
2623

@@ -30,19 +27,27 @@
3027
import static cd.go.contrib.elasticagent.utils.Util.GSON;
3128

3229
public class GetPluginConfigurationExecutor implements RequestExecutor {
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");
3830
public static final Map<String, Field> FIELDS = new LinkedHashMap<>();
31+
public static final Field GO_SERVER_URL = new GoServerUrlField("go_server_url", "GoCD server URL", false, "0");
32+
public static final Field AUTO_REGISTER_TIMEOUT = new PositiveNumberField("auto_register_timeout", "Agent auto-register timeout (in minutes)", "10", false, false, "1");
33+
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");
3940

4041
static {
4142
FIELDS.put(GO_SERVER_URL.key(), GO_SERVER_URL);
42-
FIELDS.put(AUTOREGISTER_TIMEOUT.key(), AUTOREGISTER_TIMEOUT);
43-
FIELDS.put(MAXIMUM_PENDING_PODS_COUNT.key(), MAXIMUM_PENDING_PODS_COUNT);
43+
FIELDS.put(AUTO_REGISTER_TIMEOUT.key(), AUTO_REGISTER_TIMEOUT);
44+
FIELDS.put(MAX_PENDING_PODS.key(), MAX_PENDING_PODS);
4445
FIELDS.put(CLUSTER_URL.key(), CLUSTER_URL);
4546
FIELDS.put(CLUSTER_CA_CERT.key(), CLUSTER_CA_CERT);
47+
FIELDS.put(AUTHENTICATION_STRATEGY.key(), AUTHENTICATION_STRATEGY);
48+
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);
4651
}
4752

4853
public GoPluginApiResponse execute() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public StatusReportExecutor(PluginRequest pluginRequest, KubernetesClientFactory
4848

4949
public GoPluginApiResponse execute() throws Exception {
5050
LOG.info("[status-report] Generating status report");
51-
KubernetesClient client = factory.kubernetes(pluginRequest.getPluginSettings());
51+
KubernetesClient client = factory.client(pluginRequest.getPluginSettings());
5252
final KubernetesCluster kubernetesCluster = new KubernetesCluster(client);
5353
final Template template = statusReportViewBuilder.getTemplate("status-report.template.ftlh");
5454
final String statusReportView = statusReportViewBuilder.build(template, kubernetesCluster);

0 commit comments

Comments
 (0)