framework: don't use raw SQL statements to save certificate in KeystoreDaoImpl

Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
(cherry picked from commit fb88a11f8228a3ff4798333a46c5c72b6b5ad88c)
Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
This commit is contained in:
Rohit Yadav 2015-05-29 18:32:40 +02:00
parent ca3ac68517
commit ab3b3c7fa1

View File

@ -16,23 +16,17 @@
// under the License. // under the License.
package org.apache.cloudstack.framework.security.keystore; package org.apache.cloudstack.framework.security.keystore;
import java.sql.PreparedStatement;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.ejb.Local;
import org.springframework.stereotype.Component;
import com.cloud.utils.crypt.DBEncryptionUtil;
import com.cloud.utils.db.DB; import com.cloud.utils.db.DB;
import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.SearchCriteria.Op;
import com.cloud.utils.db.TransactionLegacy; import org.springframework.stereotype.Component;
import com.cloud.utils.exception.CloudRuntimeException;
import javax.ejb.Local;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
@Component @Component
@Local(value = {KeystoreDao.class}) @Local(value = {KeystoreDao.class})
@ -96,26 +90,19 @@ public class KeystoreDaoImpl extends GenericDaoBase<KeystoreVO, Long> implements
@Override @Override
@DB @DB
public void save(String name, String certificate, String key, String domainSuffix) { public void save(String name, String certificate, String key, String domainSuffix) {
TransactionLegacy txn = TransactionLegacy.currentTxn(); KeystoreVO keystore = findByName(name);
try { if (keystore != null) {
txn.start(); keystore.setCertificate(certificate);
keystore.setKey(key);
String sql = keystore.setDomainSuffix(domainSuffix);
"INSERT INTO keystore (`name`, `certificate`, `key`, `domain_suffix`) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE `certificate`=?, `key`=?, `domain_suffix`=?"; this.update(keystore.getId(), keystore);
PreparedStatement pstmt = txn.prepareAutoCloseStatement(sql); } else {
pstmt.setString(1, name); keystore = new KeystoreVO();
pstmt.setString(2, certificate); keystore.setName(name);
pstmt.setString(3, DBEncryptionUtil.encrypt(key)); keystore.setCertificate(certificate);
pstmt.setString(4, domainSuffix); keystore.setKey(key);
pstmt.setString(5, certificate); keystore.setDomainSuffix(domainSuffix);
pstmt.setString(6, DBEncryptionUtil.encrypt(key)); this.persist(keystore);
pstmt.setString(7, domainSuffix);
pstmt.executeUpdate();
txn.commit();
} catch (Exception e) {
txn.rollback();
throw new CloudRuntimeException("Unable to save certificate under name " + name + " due to exception", e);
} }
} }
@ -130,12 +117,12 @@ public class KeystoreDaoImpl extends GenericDaoBase<KeystoreVO, Long> implements
ks.setDomainSuffix(domainSuffix); ks.setDomainSuffix(domainSuffix);
this.update(ks.getId(), ks); this.update(ks.getId(), ks);
} else { } else {
KeystoreVO newks = new KeystoreVO(); ks = new KeystoreVO();
newks.setCertificate(certificate); ks.setCertificate(certificate);
newks.setName(alias); ks.setName(alias);
newks.setIndex(index); ks.setIndex(index);
newks.setDomainSuffix(domainSuffix); ks.setDomainSuffix(domainSuffix);
persist(newks); this.persist(ks);
} }
} }
} }