Search in sources :

Example 1 with Registration

use of com.thoughtworks.go.security.Registration in project gocd by gocd.

the class AgentInstanceTest method shouldAssignCertificateToApprovedAgent.

@Test
public void shouldAssignCertificateToApprovedAgent() {
    AgentInstance agentInstance = AgentInstance.createFromConfig(agentConfig, systemEnvironment);
    agentInstance.update(new AgentRuntimeInfo(agentConfig.getAgentIdentifier(), AgentRuntimeStatus.Idle, currentWorkingDirectory(), "cookie", false));
    Registration entry = agentInstance.assignCertification();
    assertThat(entry.getChain().length, is(not(0)));
}
Also used : AgentRuntimeInfo(com.thoughtworks.go.server.service.AgentRuntimeInfo) Registration(com.thoughtworks.go.security.Registration) Test(org.junit.Test)

Example 2 with Registration

use of com.thoughtworks.go.security.Registration in project gocd by gocd.

the class AgentRegistrationController method agentRequest.

@RequestMapping(value = "/admin/agent", method = RequestMethod.POST)
public ModelAndView 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, HttpServletRequest request) throws IOException {
    final String ipAddress = request.getRemoteAddr();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Processing registration request from agent [{}/{}]", hostname, ipAddress);
    }
    Registration keyEntry;
    String preferredHostname = hostname;
    try {
        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)) {
                throw new RuntimeException(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));
            }
        }
        AgentConfig agentConfig = new AgentConfig(uuid, preferredHostname, ipAddress);
        if (partialElasticAgentAutoregistrationInfo(elasticAgentId, elasticPluginId)) {
            throw new RuntimeException("Elastic agents must submit both elasticAgentId and elasticPluginId");
        }
        if (elasticAgentAutoregistrationInfoPresent(elasticAgentId, elasticPluginId)) {
            agentConfig.setElasticAgentId(elasticAgentId);
            agentConfig.setElasticPluginId(elasticPluginId);
        }
        if (goConfigService.serverConfig().shouldAutoRegisterAgentWith(agentAutoRegisterKey)) {
            LOG.info(String.format("[Agent Auto Registration] Auto registering agent with uuid %s ", 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 = com.thoughtworks.go.config.ErrorCollector.getAllErrors(agentConfig);
                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);
    } catch (Exception e) {
        keyEntry = Registration.createNullPrivateKeyEntry();
        LOG.error("Error occured during agent registration process: ", e);
    }
    return render(keyEntry);
}
Also used : HttpOperationResult(com.thoughtworks.go.server.service.result.HttpOperationResult) 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) AgentConfig(com.thoughtworks.go.config.AgentConfig) 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)

Example 3 with Registration

use of com.thoughtworks.go.security.Registration in project gocd by gocd.

the class AgentService method requestRegistration.

public Registration requestRegistration(Username username, AgentRuntimeInfo agentRuntimeInfo) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Agent is requesting registration " + agentRuntimeInfo);
    }
    AgentInstance agentInstance = agentInstances.register(agentRuntimeInfo);
    Registration registration = agentInstance.assignCertification();
    if (agentInstance.isRegistered()) {
        agentConfigService.saveOrUpdateAgent(agentInstance, username);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("New Agent approved " + agentRuntimeInfo);
        }
    }
    return registration;
}
Also used : AgentInstance(com.thoughtworks.go.domain.AgentInstance) Registration(com.thoughtworks.go.security.Registration)

Example 4 with Registration

use of com.thoughtworks.go.security.Registration in project gocd by gocd.

the class AgentInstanceTest method shouldNotAssignCertificateToPendingAgent.

@Test
public void shouldNotAssignCertificateToPendingAgent() {
    AgentRuntimeInfo agentRuntimeInfo = AgentRuntimeInfo.fromServer(agentConfig, false, "/var/lib", 0L, "linux", false);
    AgentInstance agentInstance = AgentInstance.createFromLiveAgent(agentRuntimeInfo, systemEnvironment);
    Registration entry = agentInstance.assignCertification();
    assertThat(entry.getChain().length, is(0));
}
Also used : AgentRuntimeInfo(com.thoughtworks.go.server.service.AgentRuntimeInfo) Registration(com.thoughtworks.go.security.Registration) Test(org.junit.Test)

Example 5 with Registration

use of com.thoughtworks.go.security.Registration in project gocd by gocd.

the class SslInfrastructureService method register.

private void register(AgentAutoRegistrationProperties agentAutoRegistrationProperties) throws Exception {
    String hostName = SystemUtil.getLocalhostNameOrRandomNameIfNotFound();
    Registration keyEntry = Registration.createNullPrivateKeyEntry();
    while (!keyEntry.isValid()) {
        try {
            keyEntry = remoteRegistrationRequester.requestRegistration(hostName, agentAutoRegistrationProperties);
        } catch (Exception e) {
            LOGGER.error("[Agent Registration] There was a problem registering with the go server.", e);
            throw e;
        }
        if ((!keyEntry.isValid())) {
            try {
                LOGGER.debug("[Agent Registration] Retrieved agent key from Go server is not valid.");
                Thread.sleep(REGISTER_RETRY_INTERVAL);
            } catch (InterruptedException e) {
            // Ok
            }
        }
    }
    LOGGER.info("[Agent Registration] Retrieved registration from Go server.");
    storeChainIntoAgentStore(keyEntry);
    agentAutoRegistrationProperties.scrubRegistrationProperties();
}
Also used : Registration(com.thoughtworks.go.security.Registration) IOException(java.io.IOException)

Aggregations

Registration (com.thoughtworks.go.security.Registration)7 AgentRuntimeInfo (com.thoughtworks.go.server.service.AgentRuntimeInfo)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 AgentConfig (com.thoughtworks.go.config.AgentConfig)1 GoConfigDao (com.thoughtworks.go.config.GoConfigDao)1 GoConfigInvalidException (com.thoughtworks.go.config.exceptions.GoConfigInvalidException)1 UpdateEnvironmentsCommand (com.thoughtworks.go.config.update.UpdateEnvironmentsCommand)1 UpdateResourceCommand (com.thoughtworks.go.config.update.UpdateResourceCommand)1 AgentInstance (com.thoughtworks.go.domain.AgentInstance)1 AllConfigErrors (com.thoughtworks.go.domain.AllConfigErrors)1 ConfigErrors (com.thoughtworks.go.domain.ConfigErrors)1 X509CertificateGenerator (com.thoughtworks.go.security.X509CertificateGenerator)1 HttpOperationResult (com.thoughtworks.go.server.service.result.HttpOperationResult)1 SystemEnvironment (com.thoughtworks.go.util.SystemEnvironment)1 FileNotFoundException (java.io.FileNotFoundException)1 Before (org.junit.Before)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1