From c6588c0e25a0a9c98845620b1e07e69a0ac012cd Mon Sep 17 00:00:00 2001 From: abhishek Date: Tue, 26 Oct 2010 12:03:15 -0700 Subject: [PATCH] adding some error return handling to api dispatcher, and cleaning up some of the code paths to return the right error messages --- server/src/com/cloud/api/ApiDispatcher.java | 3 ++ .../commands/UploadCustomCertificateCmd.java | 37 +++++++++++++---- .../consoleproxy/ConsoleProxyManagerImpl.java | 40 ++++++++++--------- .../com/cloud/server/ManagementServer.java | 2 +- .../cloud/server/ManagementServerImpl.java | 9 ++++- setup/db/create-schema.sql | 2 +- 6 files changed, 64 insertions(+), 29 deletions(-) diff --git a/server/src/com/cloud/api/ApiDispatcher.java b/server/src/com/cloud/api/ApiDispatcher.java index 12e45a8df8a..822a7cdee17 100644 --- a/server/src/com/cloud/api/ApiDispatcher.java +++ b/server/src/com/cloud/api/ApiDispatcher.java @@ -36,6 +36,7 @@ import com.cloud.configuration.ConfigurationManager; import com.cloud.consoleproxy.ConsoleProxyManager; import com.cloud.exception.InvalidParameterValueException; import com.cloud.exception.PermissionDeniedException; +import com.cloud.exception.ResourceAllocationException; import com.cloud.network.DomainRouterService; import com.cloud.network.NetworkManager; import com.cloud.network.security.NetworkGroupManager; @@ -154,6 +155,8 @@ public class ApiDispatcher { throw new ServerApiException(BaseCmd.PARAM_ERROR, cause.getMessage()); } else if (cause instanceof PermissionDeniedException) { throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, cause.getMessage()); + } else if (cause instanceof ResourceAllocationException){ + throw new ServerApiException(BaseCmd.UNSUPPORTED_ACTION_ERROR, cause.getMessage()); } s_logger.warn("Exception executing method " + methodName + " for command " + cmd.getClass().getSimpleName(), ite); throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Unable to execute method " + methodName + " for command " + cmd.getClass().getSimpleName() + ", internal error in the implementation."); diff --git a/server/src/com/cloud/api/commands/UploadCustomCertificateCmd.java b/server/src/com/cloud/api/commands/UploadCustomCertificateCmd.java index 887f4676ded..53f3066f3e9 100644 --- a/server/src/com/cloud/api/commands/UploadCustomCertificateCmd.java +++ b/server/src/com/cloud/api/commands/UploadCustomCertificateCmd.java @@ -19,13 +19,15 @@ package com.cloud.api.commands; import org.apache.log4j.Logger; -import com.cloud.api.BaseCmd; +import com.cloud.api.BaseAsyncCmd; import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.response.StatusResponse; +import com.cloud.event.EventTypes; +import com.cloud.user.Account; @Implementation(method="uploadCertificate") -public class UploadCustomCertificateCmd extends BaseCmd { +public class UploadCustomCertificateCmd extends BaseAsyncCmd { public static final Logger s_logger = Logger.getLogger(UploadCustomCertificateCmd.class.getName()); private static final String s_name = "uploadcustomcertificateresponse"; @@ -37,11 +39,7 @@ public class UploadCustomCertificateCmd extends BaseCmd { return path; } - @Override - public String getName() { - return s_name; - } - + @Override @SuppressWarnings("unchecked") public StatusResponse getResponse() { Boolean status = (Boolean)getResponseObject(); @@ -51,4 +49,29 @@ public class UploadCustomCertificateCmd extends BaseCmd { response.setResponseName(getName()); return response; } + + @Override + public String getEventType() { + return EventTypes.EVENT_VOLUME_CREATE; + } + + @Override + public String getEventDescription() { + return ("Uploading custom certificate to the db, and applying it to the cpvm"); + } + + @Override + public String getName() { + return s_name; + } + + public static String getResultObjectName() { + return "volume"; + } + + @Override + public long getAccountId() { + return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked + } + } diff --git a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java index 5bee57d0f71..69e094bcccd 100644 --- a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java +++ b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java @@ -2448,25 +2448,27 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, VirtualMach long proxyVmId = ((StartupProxyCommand)cmd).getProxyVmId(); ConsoleProxyVO consoleProxy = _consoleProxyDao.findById(proxyVmId); //find corresponding host - HostVO consoleProxyHost = _hostDao.findConsoleProxyHost(consoleProxy.getName(), Type.ConsoleProxy); - //now send a command to console proxy - UpdateCertificateCommand certCmd = new UpdateCertificateCommand(certStr); - try { - Answer updateCertAns = _agentMgr.send(consoleProxyHost.getId(), certCmd); - if(updateCertAns.getResult() == true) - { - //we have the cert copied over on cpvm - long eventId = saveScheduledEvent(User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM, EventTypes.EVENT_PROXY_REBOOT, "rebooting console proxy with Id: "+consoleProxy.getId()); - rebootProxy(consoleProxy.getId(), eventId); - //when cp reboots, the context will be reinit with the new cert - s_logger.info("Successfully rebooted console proxy resource after custom certificate application"); - } - } catch (AgentUnavailableException e) { - s_logger.warn("Unable to send update certificate command to the console proxy resource", e); - return false; - } catch (OperationTimedoutException e) { - s_logger.warn("Unable to send update certificate command to the console proxy resource", e); - return false; + if(consoleProxy!=null){ + HostVO consoleProxyHost = _hostDao.findConsoleProxyHost(consoleProxy.getName(), Type.ConsoleProxy); + //now send a command to console proxy + UpdateCertificateCommand certCmd = new UpdateCertificateCommand(certStr); + try { + Answer updateCertAns = _agentMgr.send(consoleProxyHost.getId(), certCmd); + if(updateCertAns.getResult() == true) + { + //we have the cert copied over on cpvm + long eventId = saveScheduledEvent(User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM, EventTypes.EVENT_PROXY_REBOOT, "rebooting console proxy with Id: "+consoleProxy.getId()); + rebootProxy(consoleProxy.getId(), eventId); + //when cp reboots, the context will be reinit with the new cert + s_logger.info("Successfully rebooted console proxy resource after custom certificate application"); + } + } catch (AgentUnavailableException e) { + s_logger.warn("Unable to send update certificate command to the console proxy resource", e); + return false; + } catch (OperationTimedoutException e) { + s_logger.warn("Unable to send update certificate command to the console proxy resource", e); + return false; + } } }else{ return false;//no cert diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index e620d53ef25..8ac62e001f1 100755 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -1219,5 +1219,5 @@ public interface ManagementServer { */ String[] getHypervisors(ListHypervisorsCmd cmd); - boolean uploadCertificate(UploadCustomCertificateCmd cmd); + boolean uploadCertificate(UploadCustomCertificateCmd cmd) throws ResourceAllocationException; } diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index c57b91e4c70..dbd6b488b0b 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -26,6 +26,7 @@ import java.net.URLEncoder; import java.net.UnknownHostException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.security.cert.Certificate; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; @@ -148,6 +149,7 @@ import com.cloud.async.dao.AsyncJobDao; import com.cloud.async.executor.ExtractJobResultObject; import com.cloud.capacity.CapacityVO; import com.cloud.capacity.dao.CapacityDao; +import com.cloud.certificate.CertificateVO; import com.cloud.certificate.dao.CertificateDao; import com.cloud.configuration.Config; import com.cloud.configuration.ConfigurationManager; @@ -6946,7 +6948,12 @@ public class ManagementServerImpl implements ManagementServer { } @Override - public boolean uploadCertificate(UploadCustomCertificateCmd cmd) { + public boolean uploadCertificate(UploadCustomCertificateCmd cmd) throws ResourceAllocationException { + //limit no.of certs uploaded to 1 + if(_certDao.listAll().size()>0){ + throw new ResourceAllocationException("There is already a custom certificate in the db"); + } + String certificatePath = cmd.getPath(); Long certVOId = _certDao.persistCustomCertToDb(certificatePath);//0 implies failure diff --git a/setup/db/create-schema.sql b/setup/db/create-schema.sql index 69b0c5d800c..3f2b97dff4d 100755 --- a/setup/db/create-schema.sql +++ b/setup/db/create-schema.sql @@ -118,7 +118,7 @@ CREATE TABLE `cloud`.`account_network_ref` ( CREATE TABLE `cloud`.`certificate` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id', - `certificate` text NOT NULL UNIQUE COMMENT 'the actual custom certificate being stored in the db', + `certificate` text COMMENT 'the actual custom certificate being stored in the db', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;