use of org.eclipse.kura.KuraException in project kura by eclipse.
the class CryptoServiceImpl method getKeyStorePassword.
@Override
public char[] getKeyStorePassword(String keyStorePath) {
Properties props = new Properties();
char[] password = null;
FileInputStream fis = null;
File f = new File(this.m_keystorePasswordPath);
if (!f.exists()) {
return "changeit".toCharArray();
}
try {
fis = new FileInputStream(this.m_keystorePasswordPath);
props.load(fis);
Object value = props.get(keyStorePath);
if (value != null) {
String encryptedPassword = (String) value;
password = decryptAes(encryptedPassword.toCharArray());
}
} catch (FileNotFoundException e) {
logger.warn("File not found exception while getting keystore password - ", e);
} catch (IOException e) {
logger.warn("IOException while getting keystore password - ", e);
} catch (KuraException e) {
logger.warn("KuraException while getting keystore password - ", e);
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
logger.warn("IOException while closing source - ", e);
}
}
}
return password;
}
use of org.eclipse.kura.KuraException in project kura by eclipse.
the class CryptoServiceImpl method setKeyStorePassword.
@Override
public void setKeyStorePassword(String keyStorePath, char[] password) throws KuraException {
Properties props = new Properties();
char[] encryptedPassword = encryptAes(password);
props.put(keyStorePath, new String(encryptedPassword));
FileOutputStream fos = null;
try {
fos = new FileOutputStream(this.m_keystorePasswordPath);
props.store(fos, "Do not edit this file. It's automatically generated by Kura");
} catch (FileNotFoundException e) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
} catch (IOException e) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
logger.warn("IOException while closing destination - ", e);
}
}
}
}
use of org.eclipse.kura.KuraException in project kura by eclipse.
the class CloudDeploymentHandlerV2 method doExecInstall.
private void doExecInstall(KuraRequestPayload request, KuraResponsePayload response) {
final DeploymentPackageInstallOptions options;
try {
options = new DeploymentPackageInstallOptions(request);
options.setClientId(this.m_dataTransportService.getClientId());
} catch (Exception ex) {
s_logger.error("Malformed install request!");
response.setResponseCode(KuraResponsePayload.RESPONSE_CODE_ERROR);
response.setTimestamp(new Date());
try {
response.setBody("Malformed install request".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
// Ignore
}
response.setException(ex);
return;
}
this.m_installOptions = options;
boolean alreadyDownloaded = false;
try {
alreadyDownloaded = s_downloadImplementation.isAlreadyDownloaded();
} catch (KuraException ex) {
response.setResponseCode(KuraResponsePayload.RESPONSE_CODE_ERROR);
response.setException(ex);
response.setTimestamp(new Date());
try {
response.setBody("Error checking download status".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
}
return;
}
if (alreadyDownloaded && !this.m_isInstalling) {
try {
this.m_isInstalling = true;
final File dpFile = DownloadFileUtilities.getDpDownloadFile(options);
s_installImplementation.setOptions(options);
// if yes, install
this.installerFuture = executor.submit(new Runnable() {
@Override
public void run() {
try {
installDownloadedFile(dpFile, CloudDeploymentHandlerV2.this.m_installOptions);
} catch (KuraException e) {
s_logger.error("Impossible to send an exception message to the cloud platform");
if (dpFile != null) {
dpFile.delete();
}
} finally {
CloudDeploymentHandlerV2.this.m_installOptions = null;
CloudDeploymentHandlerV2.this.m_isInstalling = false;
}
}
});
} catch (IOException e) {
response.setResponseCode(KuraResponsePayload.RESPONSE_CODE_ERROR);
response.setException(e);
response.setTimestamp(new Date());
try {
response.setBody("Exception during install".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e1) {
}
}
} else {
response.setResponseCode(KuraResponsePayload.RESPONSE_CODE_ERROR);
response.setException(new KuraException(KuraErrorCode.INTERNAL_ERROR));
response.setTimestamp(new Date());
try {
response.setBody("Already installing/uninstalling".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
}
return;
}
}
use of org.eclipse.kura.KuraException in project kura by eclipse.
the class CloudDeploymentHandlerV2 method doExecUninstall.
private void doExecUninstall(KuraRequestPayload request, KuraResponsePayload response) {
final DeploymentPackageUninstallOptions options;
try {
options = new DeploymentPackageUninstallOptions(request);
options.setClientId(this.m_dataTransportService.getClientId());
} catch (Exception ex) {
s_logger.error("Malformed uninstall request!");
response.setResponseCode(KuraResponsePayload.RESPONSE_CODE_ERROR);
response.setTimestamp(new Date());
try {
response.setBody("Malformed uninstall request".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
// Ignore
}
response.setException(ex);
return;
}
final String packageName = options.getDpName();
// We only allow one request at a time
if (!this.m_isInstalling && this.m_pendingUninstPackageName != null) {
s_logger.info("Another request seems still pending: {}. Checking if stale...", this.m_pendingUninstPackageName);
response = new KuraResponsePayload(KuraResponsePayload.RESPONSE_CODE_ERROR);
response.setTimestamp(new Date());
try {
response.setBody("Only one request at a time is allowed".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
// Ignore
}
} else {
s_logger.info("About to uninstall package {}", packageName);
try {
this.m_isInstalling = true;
this.m_pendingUninstPackageName = packageName;
s_uninstallImplementation = new UninstallImpl(this, this.m_deploymentAdmin);
s_logger.info("Uninstalling package...");
this.installerFuture = executor.submit(new Runnable() {
@Override
public void run() {
try {
s_uninstallImplementation.uninstaller(options, packageName);
} catch (Exception e) {
try {
s_uninstallImplementation.uninstallFailedAsync(options, packageName, e);
} catch (KuraException e1) {
}
} finally {
CloudDeploymentHandlerV2.this.m_installOptions = null;
CloudDeploymentHandlerV2.this.m_isInstalling = false;
}
}
});
} catch (Exception e) {
s_logger.error("Failed to uninstall package {}: {}", packageName, e);
response = new KuraResponsePayload(KuraResponsePayload.RESPONSE_CODE_ERROR);
response.setTimestamp(new Date());
try {
response.setBody(e.getMessage().getBytes("UTF-8"));
} catch (UnsupportedEncodingException uee) {
// Ignore
}
} finally {
this.m_isInstalling = false;
this.m_pendingUninstPackageName = null;
}
}
}
use of org.eclipse.kura.KuraException in project kura by eclipse.
the class DownloadImpl method incrementalDownloadFromURL.
// ----------------------------------------------------------------
//
// Private methods
//
// ----------------------------------------------------------------
private void incrementalDownloadFromURL(File dpFile, String url, int downloadIndex) throws Exception {
OutputStream os = null;
try {
os = new FileOutputStream(dpFile);
DownloadOptions downloadOptions = new DownloadOptions();
downloadOptions.setOut(os);
downloadOptions.setRequestOptions(this.options);
downloadOptions.setCallback(this);
downloadOptions.setSslManagerService(this.sslManagerService);
downloadOptions.setDownloadURL(url);
downloadOptions.setAlreadyDownloaded(downloadIndex);
this.downloadHelper = DownloadFactory.getDownloadInstance(this.options.getDownloadProtocol(), downloadOptions);
this.downloadHelper.startWork();
this.downloadHelper.close();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e1) {
s_logger.error("Exception while trying to close stream.", e1);
}
}
}
if (this.options.getHash() != null) {
String[] hashAlgorithmValue = this.options.getHash().split(":");
String hashAlgorithm = null;
String hashValue = null;
if (hashAlgorithmValue.length == 2) {
hashAlgorithm = hashAlgorithmValue[0].trim();
hashValue = hashAlgorithmValue[1].trim();
}
s_logger.info("--> Going to verify hash signature!");
try {
String checksum = HashUtil.hash(hashAlgorithm, dpFile);
if (hashAlgorithm == null || "".equals(hashAlgorithm) || hashValue == null || "".equals(hashValue) || checksum == null || !checksum.equals(hashValue)) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, null, "Failed to verify checksum with algorithm: " + hashAlgorithm);
}
} catch (Exception e) {
dpFile.delete();
throw e;
}
}
}
Aggregations