*
* A user can call this REST function any number of times, on each call the X509 certificate
* simply over writes any previously stored value.
*/
private void setCertificate(HttpServletRequest request, HttpServletResponse response) throws Exception {
TransactionLegacy txn = null;
try {
// [A] Pull the cert and cloud AccessKey from the request
String[] certificate = request.getParameterValues("cert");
if (null == certificate || 0 == certificate.length) {
response.sendError(530, "Missing cert parameter");
return;
}
// logger.debug( "SetCertificate cert: [" + certificate[0] + "]" );
String[] accessKey = request.getParameterValues("AWSAccessKeyId");
if (null == accessKey || 0 == accessKey.length) {
response.sendError(530, "Missing AWSAccessKeyId parameter");
return;
}
// [B] Open our keystore
FileInputStream fsIn = new FileInputStream(pathToKeystore);
KeyStore certStore = KeyStore.getInstance("JKS");
certStore.load(fsIn, keystorePassword.toCharArray());
// -> use the Cloud API key to save the cert in the keystore
// -> write the cert into the keystore on disk
Certificate userCert = null;
CertificateFactory cf = CertificateFactory.getInstance("X.509");
ByteArrayInputStream bs = new ByteArrayInputStream(certificate[0].getBytes());
while (bs.available() > 0)
userCert = cf.generateCertificate(bs);
certStore.setCertificateEntry(accessKey[0], userCert);
FileOutputStream fsOut = new FileOutputStream(pathToKeystore);
certStore.store(fsOut, keystorePassword.toCharArray());
// [C] Associate the cert's uniqueId with the Cloud API keys
String uniqueId = AuthenticationUtils.X509CertUniqueId(userCert);
logger.debug("SetCertificate, uniqueId: " + uniqueId);
txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB);
txn.start();
UserCredentialsVO user = ucDao.getByAccessKey(accessKey[0]);
user.setCertUniqueId(uniqueId);
ucDao.update(user.getId(), user);
response.setStatus(200);
endResponse(response, "User certificate set successfully");
txn.commit();
} catch (NoSuchObjectException e) {
logger.error("SetCertificate exception " + e.getMessage(), e);
response.sendError(404, "SetCertificate exception " + e.getMessage());
} catch (Exception e) {
logger.error("SetCertificate exception " + e.getMessage(), e);
response.sendError(500, "SetCertificate exception " + e.getMessage());
} finally {
txn.close();
}
}