Skip to content
Permalink
Browse files

Do not allow agent to register with duplicate elastic agent id (#3828)

[Agent Registration] Do not allow agent registration if agent with same elastic agent id already exists
[XSL Migration] Add an xslt transformation to remove an agent with duplicate elastic agent id.
  • Loading branch information...
GaneshSPatil committed Dec 27, 2017
1 parent 694674a commit 8971a71ae0bd0ff0a3c32e70e48ad192f06b5567
@@ -57,7 +57,7 @@

public static final String PRODUCT_NAME = "go";

public static final int CONFIG_SCHEMA_VERSION = 102;
public static final int CONFIG_SCHEMA_VERSION = 103;

public static final String APPROVAL_SUCCESS = "success";
public static final String APPROVAL_MANUAL = "manual";
@@ -82,7 +82,7 @@
</xsd:unique>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="schemaVersion" type="xsd:int" use="required" fixed="102"/>
<xsd:attribute name="schemaVersion" type="xsd:int" use="required" fixed="103"/>
</xsd:complexType>
<xsd:unique name="uniquePipelines">
<xsd:selector xpath="pipelines"/>
@@ -1086,3 +1086,4 @@




Large diffs are not rendered by default.

@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<!--
~ Copyright 2017 ThoughtWorks, Inc.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/cruise/@schemaVersion">
<xsl:attribute name="schemaVersion">103</xsl:attribute>
</xsl:template>
<!-- Copy everything -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="agent[@elasticAgentId = preceding-sibling::agent/@elasticAgentId]"/>
</xsl:stylesheet>
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<cruise xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="cruise-config.xsd" schemaVersion="102">
<cruise xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="cruise-config.xsd" schemaVersion="103">
<server artifactsdir="artifacts" agentAutoRegisterKey="323040d4-f2e4-4b8a-8394-7a2d122054d1" webhookSecret="3d5cd2f5-7fe7-43c0-ba34-7e01678ba8b6" commandRepositoryLocation="default" serverId="60f5f682-5248-4ba9-bb35-72c92841bd75" tokenGenerationKey="8c3c8dc9-08bf-4cd7-ac80-cecb3e7ae86c">
<security>
<authConfigs>
@@ -233,6 +233,10 @@ public ResponseEntity agentRequest(@RequestParam("hostname") String hostname,
return new ResponseEntity<>("Elastic agents must submit both elasticAgentId and elasticPluginId.", UNPROCESSABLE_ENTITY);
}

if (elasticAgentIdAlreadyRegistered(elasticAgentId, elasticPluginId)) {
return new ResponseEntity<>("Duplicate Elastic agent Id used to register elastic agent.", UNPROCESSABLE_ENTITY);
}

if (elasticAgentAutoregistrationInfoPresent(elasticAgentId, elasticPluginId)) {
agentConfig.setElasticAgentId(elasticAgentId);
agentConfig.setElasticPluginId(elasticPluginId);
@@ -278,6 +282,10 @@ public ResponseEntity agentRequest(@RequestParam("hostname") String hostname,
}
}

private boolean elasticAgentIdAlreadyRegistered(String elasticAgentId, String elasticPluginId) {
return agentService.findElasticAgent(elasticAgentId, elasticPluginId) != null;
}

private String getErrorMessage(Exception e) {
if (e instanceof GoConfigInvalidException) {
return ((GoConfigInvalidException) e).getAllErrorMessages();
@@ -1854,6 +1854,22 @@ public void shouldSkipParamResoulutionForElasticConfig_asPartOf100To101Migration
);
}


@Test
public void shouldRemoveAgentWithDuplicateElasticAgentId_asPartOf102To103Migration() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
String configXml = "<cruise schemaVersion='102'>" +
"<agents>\n" +
" <agent hostname=\"hostname\" ipaddress=\"127.0.0.1\" uuid=\"c46a08a7-921c-4e77-b748-6128975a3e7d\" elasticAgentId=\"16649813-4cb3-4682-8702-8e202824dd73\" elasticPluginId=\"elastic-plugin-id\" />\n" +
" <agent hostname=\"hostname\" ipaddress=\"127.0.0.1\" uuid=\"c46a08a7-921c-4e77-b748-6128975a3e7e\" elasticAgentId=\"16649813-4cb3-4682-8702-8e202824dd73\" elasticPluginId=\"elastic-plugin-id\" />\n" +
" <agent hostname=\"hostname\" ipaddress=\"127.0.0.1\" uuid=\"537d36f9-bf4b-48b2-8d09-5d20357d4f16\" elasticAgentId=\"a38d2559-0703-4e69-a30d-a21245d740af\" elasticPluginId=\"elastic-plugin-id\" />\n" +
" </agents>" +
"</cruise>";

assertThat(configXml, containsString("c46a08a7-921c-4e77-b748-6128975a3e7e"));
String migratedContent = migrateXmlString(configXml, 102);
assertThat(migratedContent, not(containsString("c46a08a7-921c-4e77-b748-6128975a3e7e")));
}

private void assertStringsIgnoringCarriageReturnAreEqual(String expected, String actual) {
assertEquals(expected.replaceAll("\\r", ""), actual.replaceAll("\\r", ""));
}
@@ -50,9 +50,7 @@

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
import static org.springframework.http.HttpStatus.CONFLICT;
import static org.springframework.http.HttpStatus.FORBIDDEN;
import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY;
import static org.springframework.http.HttpStatus.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
@@ -99,6 +97,77 @@ public void shouldRegisterLocalAgent() throws Exception {
assertTrue(RegistrationJSONizer.fromJson(responseEntity.getBody().toString()).isValid());
}

@Test
public void shouldRegisterElasticAgent() throws Exception {
String autoRegisterKey = goConfigService.serverConfig().getAgentAutoRegisterKey();
String uuid = UUID.randomUUID().toString();
String elasticAgentId = UUID.randomUUID().toString();
MockHttpServletRequest request = new MockHttpServletRequest();
final ResponseEntity responseEntity = controller.agentRequest("elastic-agent-hostname",
uuid,
"sandbox",
"100",
"Alpine Linux v3.5",
autoRegisterKey,
"",
"",
"hostname",
elasticAgentId,
"elastic-plugin-id",
false,
token(uuid, goConfigService.serverConfig().getTokenGenerationKey()),
request);
AgentConfig agentConfig = goConfigService.agentByUuid(uuid);

assertTrue(agentConfig.isElastic());
assertThat(responseEntity.getStatusCode(), is(HttpStatus.OK));
assertThat(responseEntity.getHeaders().getContentType(), is(MediaType.APPLICATION_JSON));
assertTrue(RegistrationJSONizer.fromJson(responseEntity.getBody().toString()).isValid());
}

@Test
public void shouldNotRegisterElasticAgentWithDuplicateElasticAgentID() throws Exception {
String autoRegisterKey = goConfigService.serverConfig().getAgentAutoRegisterKey();
String uuid = UUID.randomUUID().toString();
String elasticAgentId = UUID.randomUUID().toString();
MockHttpServletRequest request = new MockHttpServletRequest();

controller.agentRequest("elastic-agent-hostname",
uuid,
"sandbox",
"100",
"Alpine Linux v3.5",
autoRegisterKey,
"",
"",
"hostname",
elasticAgentId,
"elastic-plugin-id",
false,
token(uuid, goConfigService.serverConfig().getTokenGenerationKey()),
request);
AgentConfig agentConfig = goConfigService.agentByUuid(uuid);
assertTrue(agentConfig.isElastic());

final ResponseEntity responseEntity = controller.agentRequest("elastic-agent-hostname",
uuid,
"sandbox",
"100",
"Alpine Linux v3.5",
autoRegisterKey,
"",
"",
"hostname",
elasticAgentId,
"elastic-plugin-id",
false,
token(uuid, goConfigService.serverConfig().getTokenGenerationKey()),
request);

assertThat(responseEntity.getStatusCode(), is(UNPROCESSABLE_ENTITY));
assertThat(responseEntity.getBody(), is("Duplicate Elastic agent Id used to register elastic agent."));
}

@Test
public void shouldAddAgentInPendingState() throws Exception {
System.setProperty(SystemEnvironment.AUTO_REGISTER_LOCAL_AGENT_ENABLED.propertyName(), "false");
@@ -205,7 +274,7 @@ public void shouldReIssueCertificateIfRegisteredAgentAsksForRegistration() throw
assertTrue(agentService.findAgent(uuid).getAgentConfigStatus().equals(AgentConfigStatus.Enabled));

MockHttpServletRequest request = new MockHttpServletRequest();
final ResponseEntity responseEntity = controller.agentRequest("hostname", uuid, "sandbox", "100", null, null, null, null, null, null, null, false, token(uuid,goConfigService.serverConfig().getTokenGenerationKey()), request);
final ResponseEntity responseEntity = controller.agentRequest("hostname", uuid, "sandbox", "100", null, null, null, null, null, null, null, false, token(uuid, goConfigService.serverConfig().getTokenGenerationKey()), request);

AgentInstance agentInstance = agentService.findAgent(uuid);

0 comments on commit 8971a71

Please sign in to comment.
You can’t perform that action at this time.