diff --git a/agent/src/com/cloud/agent/resource/consoleproxy/ConsoleProxyResource.java b/agent/src/com/cloud/agent/resource/consoleproxy/ConsoleProxyResource.java index 483cfd7c3ab..6f9b8c9bc69 100644 --- a/agent/src/com/cloud/agent/resource/consoleproxy/ConsoleProxyResource.java +++ b/agent/src/com/cloud/agent/resource/consoleproxy/ConsoleProxyResource.java @@ -109,36 +109,69 @@ public class ConsoleProxyResource extends ServerResourceBase implements ServerRe protected Answer execute(final UpdateCertificateCommand cmd) { boolean success = false; + String errorStr = null; + String successStr = null; try { String certificate = cmd.getCertificate(); //write the cert to /etc/cloud/consoleproxy/cert/ boolean dirCreated = false; - String strDirectoy = "/etc/cloud/consoleproxy/cert/"; - dirCreated = (new File(strDirectoy)).mkdirs(); - if (dirCreated) - { + boolean dirExists = false; + boolean forNewProxy = cmd.isForNewProxy(); + String strDirectory = "/etc/cloud/consoleproxy/cert/"; + String filePath = "/etc/cloud/consoleproxy/cert/customcert"; + if(forNewProxy){ + dirCreated = (new File(strDirectory)).mkdirs(); if(s_logger.isDebugEnabled()) - s_logger.debug("Directory: " + strDirectoy + " created"); - //copy cert to the dir - FileWriter fstream = new FileWriter("/etc/cloud/consoleproxy/cert/customcert"); - BufferedWriter out = new BufferedWriter(fstream); - out.write(certificate); - //Close the output stream - out.close(); - success = true; - } + s_logger.debug("Directory: " + strDirectory + " created"); + if(dirCreated){ + //copy cert to the dir + FileWriter fstream = new FileWriter(filePath); + BufferedWriter out = new BufferedWriter(fstream); + out.write(certificate); + //Close the output stream + out.close(); + success = true; + successStr = "Successfully created cert at /etc/cloud/consoleproxy/cert/ from the listener flow for new console proxy starting up"; + } + } + else{ + File dir = new File(strDirectory); + dirExists = dir.exists(); + if(!dirExists){ + dirCreated = (new File(strDirectory)).mkdirs(); + if(s_logger.isDebugEnabled()) + s_logger.debug("Directory: " + strDirectory + " created"); + } + if (dirExists || dirCreated) + { + //copy cert to the dir + FileWriter fstream = new FileWriter(filePath); + BufferedWriter out = new BufferedWriter(fstream); + out.write(certificate); + //Close the output stream + out.close(); + success = true; + successStr = "Successfully created cert at /etc/cloud/consoleproxy/cert/ from the UploadCustomCert cmd flow for existing console proxy"; + } + } }catch (SecurityException se){ - s_logger.error("Unable to read the cert string in console proxy resource due to directory creation failure",se); + errorStr = "Unable to upload cert in console proxy resource due to directory creation failure"; + s_logger.error(errorStr,se); success = false; + }catch (IOException ioe){ + errorStr = "Unable to write cert to the location /etc/cloud/consoleproxy/cert/ "; + s_logger.error(errorStr,ioe); + success = false; } catch (Exception e) { - s_logger.error("Unable to read the cert string in console proxy resource",e); + errorStr = "Unable to upload cert in console proxy resource"; + s_logger.error(errorStr,e); success = false; } - return new Answer(cmd, success, "Custom certificate response from the updatecertificate flow"); + return new Answer(cmd, success, errorStr!=null?errorStr:successStr); } protected Answer execute(final CheckConsoleProxyLoadCommand cmd) { diff --git a/core/src/com/cloud/agent/api/proxy/UpdateCertificateCommand.java b/core/src/com/cloud/agent/api/proxy/UpdateCertificateCommand.java index 23be10d3c26..daddef0b3ab 100644 --- a/core/src/com/cloud/agent/api/proxy/UpdateCertificateCommand.java +++ b/core/src/com/cloud/agent/api/proxy/UpdateCertificateCommand.java @@ -25,19 +25,30 @@ package com.cloud.agent.api.proxy; public class UpdateCertificateCommand extends ProxyCommand { private String certificate; //certificate to be applied + private boolean forNewProxy; //denotes if this is called from the listener flow public UpdateCertificateCommand() { + this.forNewProxy = false; } - public UpdateCertificateCommand(String certificate) { + public UpdateCertificateCommand(String certificate, boolean forNewProxy) { this.certificate = certificate; + this.forNewProxy = forNewProxy; } public String getCertificate() { return this.certificate; } - @Override + public boolean isForNewProxy() { + return forNewProxy; + } + + public void setForNewProxy(boolean forNewProxy) { + this.forNewProxy = forNewProxy; + } + + @Override public boolean executeInSequence() { return false; } diff --git a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java index e952e5c9c55..2502b36c687 100644 --- a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java +++ b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java @@ -2456,7 +2456,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, VirtualMach if(consoleProxy!=null){ HostVO consoleProxyHost = _hostDao.findConsoleProxyHost(consoleProxy.getName(), Type.ConsoleProxy); //now send a command to console proxy - UpdateCertificateCommand certCmd = new UpdateCertificateCommand(certStr); + UpdateCertificateCommand certCmd = new UpdateCertificateCommand(certStr, true); try { Answer updateCertAns = _agentMgr.send(consoleProxyHost.getId(), certCmd); if(updateCertAns.getResult() == true) diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index c1e09bca7dd..dce7f296bdf 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -5908,7 +5908,7 @@ public class ManagementServerImpl implements ManagementServer { { Long cpHostId = hostNameToHostIdMap.get(cp.getName()); //now send a command to each console proxy - UpdateCertificateCommand certCmd = new UpdateCertificateCommand(_certDao.findById(certVOId).getCertificate()); + UpdateCertificateCommand certCmd = new UpdateCertificateCommand(_certDao.findById(certVOId).getCertificate(), false); try { Answer updateCertAns = _agentMgr.send(cpHostId, certCmd); if(updateCertAns.getResult() == true)