Search in sources :

Example 6 with KuraException

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;
}
Also used : KuraException(org.eclipse.kura.KuraException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 7 with KuraException

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);
            }
        }
    }
}
Also used : KuraException(org.eclipse.kura.KuraException) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Properties(java.util.Properties)

Example 8 with KuraException

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;
    }
}
Also used : DeploymentPackageInstallOptions(org.eclipse.kura.core.deployment.install.DeploymentPackageInstallOptions) KuraException(org.eclipse.kura.KuraException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) File(java.io.File) ComponentException(org.osgi.service.component.ComponentException) KuraException(org.eclipse.kura.KuraException) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Date(java.util.Date)

Example 9 with KuraException

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;
        }
    }
}
Also used : UninstallImpl(org.eclipse.kura.core.deployment.uninstall.UninstallImpl) KuraException(org.eclipse.kura.KuraException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KuraResponsePayload(org.eclipse.kura.message.KuraResponsePayload) DeploymentPackageUninstallOptions(org.eclipse.kura.core.deployment.uninstall.DeploymentPackageUninstallOptions) ComponentException(org.osgi.service.component.ComponentException) KuraException(org.eclipse.kura.KuraException) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Date(java.util.Date)

Example 10 with KuraException

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;
        }
    }
}
Also used : DeploymentPackageDownloadOptions(org.eclipse.kura.core.deployment.download.DeploymentPackageDownloadOptions) DownloadOptions(org.eclipse.kura.core.deployment.download.DownloadOptions) KuraException(org.eclipse.kura.KuraException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DownloadCountingOutputStream(org.eclipse.kura.core.deployment.download.DownloadCountingOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) KuraException(org.eclipse.kura.KuraException)

Aggregations

KuraException (org.eclipse.kura.KuraException)315 IOException (java.io.IOException)191 CommConnection (org.eclipse.kura.comm.CommConnection)54 SafeProcess (org.eclipse.kura.core.util.SafeProcess)49 ArrayList (java.util.ArrayList)46 File (java.io.File)44 BufferedReader (java.io.BufferedReader)42 InputStreamReader (java.io.InputStreamReader)30 UnknownHostException (java.net.UnknownHostException)30 FileNotFoundException (java.io.FileNotFoundException)25 NetConfig (org.eclipse.kura.net.NetConfig)23 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)20 SupportedUsbModemInfo (org.eclipse.kura.linux.net.modem.SupportedUsbModemInfo)19 UsbModemDevice (org.eclipse.kura.usb.UsbModemDevice)19 Properties (java.util.Properties)18 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)17 GwtKuraException (org.eclipse.kura.web.shared.GwtKuraException)17 StringTokenizer (java.util.StringTokenizer)16 FileOutputStream (java.io.FileOutputStream)15 IP4Address (org.eclipse.kura.net.IP4Address)15