diff --git a/api/src/com/cloud/api/commands/RegisterSSHKeyPairCmd.java b/api/src/com/cloud/api/commands/RegisterSSHKeyPairCmd.java index b7c5945c1a4..243df8b54c1 100644 --- a/api/src/com/cloud/api/commands/RegisterSSHKeyPairCmd.java +++ b/api/src/com/cloud/api/commands/RegisterSSHKeyPairCmd.java @@ -24,6 +24,7 @@ import com.cloud.api.ApiConstants; import com.cloud.api.BaseCmd; import com.cloud.api.Implementation; import com.cloud.api.Parameter; +import com.cloud.api.BaseCmd.CommandType; import com.cloud.api.response.SSHKeyPairResponse; import com.cloud.user.Account; import com.cloud.user.SSHKeyPair; @@ -45,6 +46,12 @@ public class RegisterSSHKeyPairCmd extends BaseCmd { @Parameter(name="publickey", type=CommandType.STRING, required=true, description="Public key material of the keypair") private String publicKey; + //Owner information + @Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="an optional account for the ssh key. Must be used with domainId.") + private String accountName; + + @Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, description="an optional domainId for the ssh key. If the account parameter is used, domainId must also be used.") + private Long domainId; ///////////////////////////////////////////////////// /////////////////// Accessors /////////////////////// @@ -58,6 +65,13 @@ public class RegisterSSHKeyPairCmd extends BaseCmd { return publicKey; } + public String getAccountName() { + return accountName; + } + + public Long getDomainId() { + return domainId; + } ///////////////////////////////////////////////////// /////////////// API Implementation/////////////////// diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 8ea5a1dc8bf..54115ec8989 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -4613,7 +4613,7 @@ public class ManagementServerImpl implements ManagementServer { String fingerprint = keys.getPublicKeyFingerPrint(); String privateKey = keys.getPrivateKey(); - return createAndSaveSSHKeyPair(name, fingerprint, publicKey, privateKey); + return createAndSaveSSHKeyPair(name, fingerprint, publicKey, privateKey, owner); } @Override @@ -4685,29 +4685,32 @@ public class ManagementServerImpl implements ManagementServer { @Override public SSHKeyPair registerSSHKeyPair(RegisterSSHKeyPairCmd cmd) { - Account account = UserContext.current().getCaller(); - SSHKeyPairVO s = _sshKeyPairDao.findByName(account.getAccountId(), account.getDomainId(), cmd.getName()); + Account caller = UserContext.current().getCaller(); + + Account owner = _accountMgr.finalizeOwner(caller, cmd.getAccountName(), cmd.getDomainId()); + + SSHKeyPairVO s = _sshKeyPairDao.findByName(owner.getAccountId(), owner.getDomainId(), cmd.getName()); if (s != null) { throw new InvalidParameterValueException("A key pair with name '" + cmd.getName() + "' already exists."); } String name = cmd.getName(); String publicKey = SSHKeysHelper.getPublicKeyFromKeyMaterial(cmd.getPublicKey()); - String fingerprint = SSHKeysHelper.getPublicKeyFingerprint(publicKey); - + if (publicKey == null) { throw new InvalidParameterValueException("Public key is invalid"); } + + String fingerprint = SSHKeysHelper.getPublicKeyFingerprint(publicKey); - return createAndSaveSSHKeyPair(name, fingerprint, publicKey, null); + return createAndSaveSSHKeyPair(name, fingerprint, publicKey, null, owner); } - private SSHKeyPair createAndSaveSSHKeyPair(String name, String fingerprint, String publicKey, String privateKey) { - Account account = UserContext.current().getCaller(); + private SSHKeyPair createAndSaveSSHKeyPair(String name, String fingerprint, String publicKey, String privateKey, Account owner) { SSHKeyPairVO newPair = new SSHKeyPairVO(); - newPair.setAccountId(account.getAccountId()); - newPair.setDomainId(account.getDomainId()); + newPair.setAccountId(owner.getAccountId()); + newPair.setDomainId(owner.getDomainId()); newPair.setName(name); newPair.setFingerprint(fingerprint); newPair.setPublicKey(publicKey);