Skip to content

Commit 570d162

Browse files
committed
CLOUDSTACK-8537 add check for unique public key and account on ssh keypair registration
Signed-off-by: Daan Hoogland <daan.hoogland@gmail.com>
1 parent ad1fbc1 commit 570d162

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

‎engine/schema/src/com/cloud/user/dao/SSHKeyPairDao.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ public interface SSHKeyPairDao extends GenericDao<SSHKeyPairVO, Long> {
3535

3636
public boolean deleteByName(long accountId, long domainId, String name);
3737

38+
public SSHKeyPairVO findByPublicKey(long accountId, long domainId, String publicKey);
39+
3840
}

‎engine/schema/src/com/cloud/user/dao/SSHKeyPairDaoImpl.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ public SSHKeyPairVO findByPublicKey(String publicKey) {
7272
return findOneBy(sc);
7373
}
7474

75+
@Override
76+
public SSHKeyPairVO findByPublicKey(long accountId, long domainId, String publicKey) {
77+
SearchCriteria<SSHKeyPairVO> sc = createSearchCriteria();
78+
sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId);
79+
sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId);
80+
sc.addAnd("publicKey", SearchCriteria.Op.EQ, publicKey);
81+
return findOneBy(sc);
82+
}
83+
7584
@Override
7685
public boolean deleteByName(long accountId, long domainId, String name) {
7786
SSHKeyPairVO pair = findByName(accountId, domainId, name);

‎server/src/com/cloud/server/ManagementServerImpl.java‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737
import javax.inject.Inject;
3838
import javax.naming.ConfigurationException;
3939

40-
import org.apache.cloudstack.api.command.user.snapshot.UpdateSnapshotPolicyCmd;
41-
import org.apache.commons.codec.binary.Base64;
42-
import org.apache.log4j.Logger;
4340
import org.apache.cloudstack.acl.ControlledEntity;
4441
import org.apache.cloudstack.affinity.AffinityGroupProcessor;
4542
import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao;
@@ -408,6 +405,7 @@
408405
import org.apache.cloudstack.api.command.user.snapshot.ListSnapshotPoliciesCmd;
409406
import org.apache.cloudstack.api.command.user.snapshot.ListSnapshotsCmd;
410407
import org.apache.cloudstack.api.command.user.snapshot.RevertSnapshotCmd;
408+
import org.apache.cloudstack.api.command.user.snapshot.UpdateSnapshotPolicyCmd;
411409
import org.apache.cloudstack.api.command.user.ssh.CreateSSHKeyPairCmd;
412410
import org.apache.cloudstack.api.command.user.ssh.DeleteSSHKeyPairCmd;
413411
import org.apache.cloudstack.api.command.user.ssh.ListSSHKeyPairsCmd;
@@ -511,6 +509,8 @@
511509
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
512510
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
513511
import org.apache.cloudstack.utils.identity.ManagementServerNode;
512+
import org.apache.commons.codec.binary.Base64;
513+
import org.apache.log4j.Logger;
514514

515515
import com.cloud.agent.AgentManager;
516516
import com.cloud.agent.api.GetVncPortAnswer;
@@ -3616,9 +3616,14 @@ public SSHKeyPair registerSSHKeyPair(RegisterSSHKeyPairCmd cmd) {
36163616

36173617
Account owner = _accountMgr.finalizeOwner(caller, cmd.getAccountName(), cmd.getDomainId(), cmd.getProjectId());
36183618

3619-
SSHKeyPairVO s = _sshKeyPairDao.findByName(owner.getAccountId(), owner.getDomainId(), cmd.getName());
3620-
if (s != null) {
3621-
throw new InvalidParameterValueException("A key pair with name '" + cmd.getName() + "' already exists.");
3619+
SSHKeyPairVO existingPair = _sshKeyPairDao.findByName(owner.getAccountId(), owner.getDomainId(), cmd.getName());
3620+
if (existingPair != null) {
3621+
throw new InvalidParameterValueException("A key pair with name '" + cmd.getName() + "' already exists for this account.");
3622+
}
3623+
3624+
existingPair = _sshKeyPairDao.findByPublicKey(owner.getAccountId(), owner.getDomainId(), cmd.getPublicKey());
3625+
if (existingPair != null) {
3626+
throw new InvalidParameterValueException("A key pair with name '" + cmd.getPublicKey() + "' already exists for this account.");
36223627
}
36233628

36243629
String name = cmd.getName();

0 commit comments

Comments
 (0)