Search in sources :

Example 26 with HttpOperationResult

use of com.thoughtworks.go.server.service.result.HttpOperationResult in project gocd by gocd.

the class AgentServiceIntegrationTest method shouldReturn200WhenAnAlreadyEnableAgentIsEnabled.

@Test
public void shouldReturn200WhenAnAlreadyEnableAgentIsEnabled() {
    String agentName = "agentName";
    String agentId = DatabaseAccessHelper.AGENT_UUID;
    AgentConfig agentConfig = new AgentConfig(agentId, agentName, "127.0.0.9");
    addAgent(agentConfig);
    HttpOperationResult operationResult = new HttpOperationResult();
    agentService.enableAgents(USERNAME, operationResult, Arrays.asList(agentId));
    assertThat(operationResult.httpCode(), is(200));
    assertThat(operationResult.message(), is("Enabled 1 agent(s)"));
}
Also used : HttpOperationResult(com.thoughtworks.go.server.service.result.HttpOperationResult) Test(org.junit.Test)

Example 27 with HttpOperationResult

use of com.thoughtworks.go.server.service.result.HttpOperationResult in project gocd by gocd.

the class AgentServiceIntegrationTest method shouldNotAllowDisablingAgentWhenNotAdmin.

@Test
public void shouldNotAllowDisablingAgentWhenNotAdmin() throws IOException {
    String agentId = "agent-id";
    HttpOperationResult operationResult = new HttpOperationResult();
    CONFIG_HELPER.enableSecurity();
    CONFIG_HELPER.addAdmins("admin1");
    agentService.disableAgents(new Username(new CaseInsensitiveString("not-admin")), operationResult, Arrays.asList(agentId));
    assertThat(operationResult.httpCode(), is(401));
    assertThat(operationResult.message(), is("Unauthorized to operate on agent"));
}
Also used : HttpOperationResult(com.thoughtworks.go.server.service.result.HttpOperationResult) Username(com.thoughtworks.go.server.domain.Username) Test(org.junit.Test)

Example 28 with HttpOperationResult

use of com.thoughtworks.go.server.service.result.HttpOperationResult in project gocd by gocd.

the class AgentServiceIntegrationTest method shouldNotAllowUpdatingEnvironmentsWhenNotAdmin.

@Test
public void shouldNotAllowUpdatingEnvironmentsWhenNotAdmin() throws IOException {
    String agentId = "agent-id";
    HttpOperationResult operationResult = new HttpOperationResult();
    CONFIG_HELPER.enableSecurity();
    CONFIG_HELPER.addAdmins("admin1");
    agentService.modifyEnvironments(new Username(new CaseInsensitiveString("not-admin")), operationResult, Arrays.asList(UUID), Arrays.asList(new TriStateSelection("uat", TriStateSelection.Action.remove)));
    assertThat(operationResult.httpCode(), is(401));
    assertThat(operationResult.message(), is("Unauthorized to operate on agent"));
}
Also used : HttpOperationResult(com.thoughtworks.go.server.service.result.HttpOperationResult) Username(com.thoughtworks.go.server.domain.Username) TriStateSelection(com.thoughtworks.go.presentation.TriStateSelection) Test(org.junit.Test)

Example 29 with HttpOperationResult

use of com.thoughtworks.go.server.service.result.HttpOperationResult in project gocd by gocd.

the class AgentServiceIntegrationTest method testShouldNotUpdateHostnameOrResourcesOrEnvironmentsIfNoneAreSpecified.

@Test
public void testShouldNotUpdateHostnameOrResourcesOrEnvironmentsIfNoneAreSpecified() throws Exception {
    createEnvironment("a", "b");
    AgentConfig agent = createDisabledAndIdleAgent(UUID);
    String originalHostname = agent.getHostname();
    HttpOperationResult operationResult = new HttpOperationResult();
    agentService.modifyEnvironments(USERNAME, operationResult, Arrays.asList(UUID), Arrays.asList(new TriStateSelection("a", TriStateSelection.Action.add), new TriStateSelection("b", TriStateSelection.Action.add)));
    goConfigDao.load();
    agentConfigService.updateAgentResources(agent.getUuid(), new ResourceConfigs("linux,java"));
    assertThat(agentService.agents().size(), is(1));
    operationResult = new HttpOperationResult();
    agentService.updateAgentAttributes(USERNAME, operationResult, UUID, null, null, null, TriState.UNSET);
    assertThat(operationResult.httpCode(), is(200));
    assertThat(operationResult.message(), is("Updated agent with uuid uuid."));
    assertThat(agentService.agents().size(), is(1));
    assertThat(getFirstAgent().getHostname(), is(originalHostname));
    assertThat(getFirstAgent().getResourceConfigs().resourceNames(), is(new ResourceConfigs("linux,java").resourceNames()));
    assertEquals(getEnvironments(getFirstAgent().getUuid()), new HashSet<>(Arrays.asList("a", "b")));
}
Also used : HttpOperationResult(com.thoughtworks.go.server.service.result.HttpOperationResult) TriStateSelection(com.thoughtworks.go.presentation.TriStateSelection) Test(org.junit.Test)

Example 30 with HttpOperationResult

use of com.thoughtworks.go.server.service.result.HttpOperationResult in project gocd by gocd.

the class AgentRegistrationController method agentRequest.

@RequestMapping(value = "/admin/agent", method = RequestMethod.POST)
public ResponseEntity agentRequest(@RequestParam("hostname") String hostname, @RequestParam("uuid") String uuid, @RequestParam("location") String location, @RequestParam("usablespace") String usablespaceAsString, @RequestParam("operatingSystem") String operatingSystem, @RequestParam("agentAutoRegisterKey") String agentAutoRegisterKey, @RequestParam("agentAutoRegisterResources") String agentAutoRegisterResources, @RequestParam("agentAutoRegisterEnvironments") String agentAutoRegisterEnvironments, @RequestParam("agentAutoRegisterHostname") String agentAutoRegisterHostname, @RequestParam("elasticAgentId") String elasticAgentId, @RequestParam("elasticPluginId") String elasticPluginId, @RequestParam(value = "supportsBuildCommandProtocol", required = false, defaultValue = "false") boolean supportsBuildCommandProtocol, @RequestParam("token") String token, HttpServletRequest request) throws IOException {
    final String ipAddress = request.getRemoteAddr();
    LOG.debug("Processing registration request from agent [{}/{}]", hostname, ipAddress);
    Registration keyEntry;
    String preferredHostname = hostname;
    try {
        if (!Base64.encodeBase64String(hmac().doFinal(uuid.getBytes())).equals(token)) {
            return new ResponseEntity<>("Not a valid token.", FORBIDDEN);
        }
        if (goConfigService.serverConfig().shouldAutoRegisterAgentWith(agentAutoRegisterKey)) {
            preferredHostname = getPreferredHostname(agentAutoRegisterHostname, hostname);
            LOG.info("[Agent Auto Registration] Auto registering agent with uuid {} ", uuid);
        } else {
            if (elasticAgentAutoregistrationInfoPresent(elasticAgentId, elasticPluginId)) {
                return new ResponseEntity<>(String.format("Elastic agent registration requires an auto-register agent key to be setup on the server. Agent-id: [%s], Plugin-id: [%s]", elasticAgentId, elasticPluginId), UNPROCESSABLE_ENTITY);
            }
        }
        AgentConfig agentConfig = new AgentConfig(uuid, preferredHostname, ipAddress);
        if (partialElasticAgentAutoregistrationInfo(elasticAgentId, elasticPluginId)) {
            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);
        }
        if (goConfigService.serverConfig().shouldAutoRegisterAgentWith(agentAutoRegisterKey)) {
            LOG.info("[Agent Auto Registration] Auto registering agent with uuid {} ", uuid);
            GoConfigDao.CompositeConfigCommand compositeConfigCommand = new GoConfigDao.CompositeConfigCommand(new AgentConfigService.AddAgentCommand(agentConfig), new UpdateResourceCommand(uuid, agentAutoRegisterResources), new UpdateEnvironmentsCommand(uuid, agentAutoRegisterEnvironments));
            HttpOperationResult result = new HttpOperationResult();
            agentConfig = agentConfigService.updateAgent(compositeConfigCommand, uuid, result, agentService.agentUsername(uuid, ipAddress, preferredHostname));
            if (!result.isSuccess()) {
                List<ConfigErrors> errors = ErrorCollector.getAllErrors(agentConfig);
                ConfigErrors e = new ConfigErrors();
                e.add("resultMessage", result.detailedMessage());
                errors.add(e);
                throw new GoConfigInvalidException(null, new AllConfigErrors(errors).asString());
            }
        }
        boolean registeredAlready = goConfigService.hasAgent(uuid);
        long usableSpace = Long.parseLong(usablespaceAsString);
        AgentRuntimeInfo agentRuntimeInfo = AgentRuntimeInfo.fromServer(agentConfig, registeredAlready, location, usableSpace, operatingSystem, supportsBuildCommandProtocol);
        if (elasticAgentAutoregistrationInfoPresent(elasticAgentId, elasticPluginId)) {
            agentRuntimeInfo = ElasticAgentRuntimeInfo.fromServer(agentRuntimeInfo, elasticAgentId, elasticPluginId);
        }
        keyEntry = agentService.requestRegistration(agentService.agentUsername(uuid, ipAddress, preferredHostname), agentRuntimeInfo);
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        return new ResponseEntity<>(RegistrationJSONizer.toJson(keyEntry), httpHeaders, keyEntry.isValid() ? OK : ACCEPTED);
    } catch (Exception e) {
        LOG.error("Error occurred during agent registration process: ", e);
        return new ResponseEntity<>(String.format("Error occurred during agent registration process: %s", getErrorMessage(e)), UNPROCESSABLE_ENTITY);
    }
}
Also used : HttpOperationResult(com.thoughtworks.go.server.service.result.HttpOperationResult) HttpHeaders(org.springframework.http.HttpHeaders) UpdateEnvironmentsCommand(com.thoughtworks.go.config.update.UpdateEnvironmentsCommand) GoConfigDao(com.thoughtworks.go.config.GoConfigDao) GoConfigInvalidException(com.thoughtworks.go.config.exceptions.GoConfigInvalidException) AllConfigErrors(com.thoughtworks.go.domain.AllConfigErrors) GoConfigInvalidException(com.thoughtworks.go.config.exceptions.GoConfigInvalidException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) AgentConfig(com.thoughtworks.go.config.AgentConfig) ResponseEntity(org.springframework.http.ResponseEntity) Registration(com.thoughtworks.go.security.Registration) UpdateResourceCommand(com.thoughtworks.go.config.update.UpdateResourceCommand) AllConfigErrors(com.thoughtworks.go.domain.AllConfigErrors) ConfigErrors(com.thoughtworks.go.domain.ConfigErrors) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

HttpOperationResult (com.thoughtworks.go.server.service.result.HttpOperationResult)146 Test (org.junit.jupiter.api.Test)64 Test (org.junit.Test)53 Username (com.thoughtworks.go.server.domain.Username)34 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)23 TriStateSelection (com.thoughtworks.go.presentation.TriStateSelection)12 Pagination (com.thoughtworks.go.server.util.Pagination)7 SvnMaterial (com.thoughtworks.go.config.materials.svn.SvnMaterial)6 StageStatusCache (com.thoughtworks.go.domain.activity.StageStatusCache)6 MaterialConfig (com.thoughtworks.go.domain.materials.MaterialConfig)6 StageStatusTopic (com.thoughtworks.go.server.messaging.StageStatusTopic)6 GitMaterialConfig (com.thoughtworks.go.config.materials.git.GitMaterialConfig)5 AgentInstance (com.thoughtworks.go.domain.AgentInstance)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 PipelineConfig (com.thoughtworks.go.config.PipelineConfig)4 MaterialConfigs (com.thoughtworks.go.config.materials.MaterialConfigs)4 NullStage (com.thoughtworks.go.domain.NullStage)4 BuildCause (com.thoughtworks.go.domain.buildcause.BuildCause)4 PipelineStatusModel (com.thoughtworks.go.presentation.PipelineStatusModel)4 Stage (com.thoughtworks.go.domain.Stage)3