mirror of
https://github.com/apache/cloudstack.git
synced 2025-11-02 20:02:29 +01:00
createSshKeys command - fixed the case when admin couldn't create a key for another user via 8096 port.
registerSshKeys command - added ability for admin to register key for another user Fixed NPE in registerUserKeys command - used to happen in failure case, when invalid publicKey was specified
This commit is contained in:
parent
6137a6582d
commit
bb1f000414
@ -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///////////////////
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user