Search in sources :

Example 26 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.

the class EncryptionSecretKeyChecker method check.

@Override
public void check() {
    //Get encryption type from db.properties
    final File dbPropsFile = PropertiesUtil.findConfigFile("db.properties");
    final Properties dbProps = new Properties();
    try {
        dbProps.load(new FileInputStream(dbPropsFile));
        final String encryptionType = dbProps.getProperty("db.cloud.encryption.type");
        s_logger.debug("Encryption Type: " + encryptionType);
        if (encryptionType == null || encryptionType.equals("none")) {
            return;
        }
        s_encryptor.setAlgorithm("PBEWithMD5AndDES");
        String secretKey = null;
        SimpleStringPBEConfig stringConfig = new SimpleStringPBEConfig();
        if (encryptionType.equals("file")) {
            try {
                BufferedReader in = new BufferedReader(new FileReader(s_keyFile));
                secretKey = in.readLine();
            //Check for null or empty secret key
            } catch (FileNotFoundException e) {
                throw new CloudRuntimeException("File containing secret key not found: " + s_keyFile, e);
            } catch (IOException e) {
                throw new CloudRuntimeException("Error while reading secret key from: " + s_keyFile, e);
            }
            if (secretKey == null || secretKey.isEmpty()) {
                throw new CloudRuntimeException("Secret key is null or empty in file " + s_keyFile);
            }
        } else if (encryptionType.equals("env")) {
            secretKey = System.getenv(s_envKey);
            if (secretKey == null || secretKey.isEmpty()) {
                throw new CloudRuntimeException("Environment variable " + s_envKey + " is not set or empty");
            }
        } else if (encryptionType.equals("web")) {
            ServerSocket serverSocket = null;
            int port = 8097;
            try {
                serverSocket = new ServerSocket(port);
            } catch (IOException ioex) {
                throw new CloudRuntimeException("Error initializing secret key reciever", ioex);
            }
            s_logger.info("Waiting for admin to send secret key on port " + port);
            Socket clientSocket = null;
            try {
                clientSocket = serverSocket.accept();
            } catch (IOException e) {
                throw new CloudRuntimeException("Accept failed on " + port);
            }
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            String inputLine, outputLine;
            if ((inputLine = in.readLine()) != null) {
                secretKey = inputLine;
            }
            out.close();
            in.close();
            clientSocket.close();
            serverSocket.close();
        } else {
            throw new CloudRuntimeException("Invalid encryption type: " + encryptionType);
        }
        stringConfig.setPassword(secretKey);
        s_encryptor.setConfig(stringConfig);
        s_useEncryption = true;
    } catch (FileNotFoundException e) {
        throw new CloudRuntimeException("File db.properties not found", e);
    } catch (IOException e) {
        throw new CloudRuntimeException("Error while reading db.properties", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) FileNotFoundException(java.io.FileNotFoundException) ServerSocket(java.net.ServerSocket) SimpleStringPBEConfig(org.jasypt.encryption.pbe.config.SimpleStringPBEConfig) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) PrintWriter(java.io.PrintWriter)

Example 27 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.

the class UriUtils method formIscsiUri.

public static String formIscsiUri(String host, String iqn, Integer lun) {
    try {
        String path = iqn;
        if (lun != null) {
            path += "/" + lun.toString();
        }
        URI uri = new URI("iscsi", host, path, null);
        return uri.toString();
    } catch (URISyntaxException e) {
        throw new CloudRuntimeException("Unable to form iscsi URI: " + host + " - " + iqn + " - " + lun);
    }
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 28 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.

the class NetconfHelper method receive.

private String receive() {
    String response = new String("");
    InputStream inputStream = _session.getStdout();
    try {
        Delimiter delimiter = new Delimiter();
        byte[] buffer = new byte[1024];
        int count = 0;
        // Read the input stream till we find the end sequence ']]>]]>'.
        while (true) {
            int data = inputStream.read();
            if (data != -1) {
                byte[] dataStream = delimiter.parse(data);
                if (delimiter.endReached()) {
                    response += new String(buffer, 0, count);
                    break;
                }
                if (dataStream != null) {
                    for (int i = 0; i < dataStream.length; i++) {
                        buffer[count] = dataStream[i];
                        count++;
                        if (count == 1024) {
                            response += new String(buffer, 0, count);
                            count = 0;
                        }
                    }
                }
            } else {
                break;
            }
        }
    } catch (final Exception e) {
        throw new CloudRuntimeException("Error occured while reading from the stream: " + e.getMessage());
    }
    return response;
}
Also used : InputStream(java.io.InputStream) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 29 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.

the class EncryptionSecretKeyChanger method migrateVNCPassword.

private void migrateVNCPassword(Connection conn) {
    System.out.println("Begin migrate VNC password");
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = conn.prepareStatement("select id, vnc_password from vm_instance");
        rs = pstmt.executeQuery();
        while (rs.next()) {
            long id = rs.getLong(1);
            String value = rs.getString(2);
            if (value == null || value.isEmpty()) {
                continue;
            }
            String encryptedValue = migrateValue(value);
            pstmt = conn.prepareStatement("update vm_instance set vnc_password=? where id=?");
            pstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
            pstmt.setLong(2, id);
            pstmt.executeUpdate();
        }
    } catch (SQLException e) {
        throw new CloudRuntimeException("Unable update vm_instance vnc_password ", e);
    } catch (UnsupportedEncodingException e) {
        throw new CloudRuntimeException("Unable update vm_instance vnc_password ", e);
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pstmt != null) {
                pstmt.close();
            }
        } catch (SQLException e) {
        }
    }
    System.out.println("End migrate VNC password");
}
Also used : SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResultSet(java.sql.ResultSet) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PreparedStatement(java.sql.PreparedStatement)

Example 30 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.

the class EncryptionSecretKeyChanger method migrateConfigValues.

private void migrateConfigValues(Connection conn) {
    System.out.println("Begin migrate config values");
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = conn.prepareStatement("select name, value from configuration where category in ('Hidden', 'Secure')");
        rs = pstmt.executeQuery();
        while (rs.next()) {
            String name = rs.getString(1);
            String value = rs.getString(2);
            if (value == null || value.isEmpty()) {
                continue;
            }
            String encryptedValue = migrateValue(value);
            pstmt = conn.prepareStatement("update configuration set value=? where name=?");
            pstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
            pstmt.setString(2, name);
            pstmt.executeUpdate();
        }
    } catch (SQLException e) {
        throw new CloudRuntimeException("Unable to update configuration values ", e);
    } catch (UnsupportedEncodingException e) {
        throw new CloudRuntimeException("Unable to update configuration values ", e);
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pstmt != null) {
                pstmt.close();
            }
        } catch (SQLException e) {
        }
    }
    System.out.println("End migrate config values");
}
Also used : SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResultSet(java.sql.ResultSet) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PreparedStatement(java.sql.PreparedStatement)

Aggregations

CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1279 PreparedStatement (java.sql.PreparedStatement)320 SQLException (java.sql.SQLException)320 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)236 ResultSet (java.sql.ResultSet)217 ArrayList (java.util.ArrayList)217 ConfigurationException (javax.naming.ConfigurationException)171 HashMap (java.util.HashMap)129 DB (com.cloud.utils.db.DB)118 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)115 IOException (java.io.IOException)95 HostVO (com.cloud.host.HostVO)94 Answer (com.cloud.agent.api.Answer)84 Account (com.cloud.user.Account)84 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)82 URISyntaxException (java.net.URISyntaxException)82 ActionEvent (com.cloud.event.ActionEvent)70 TransactionStatus (com.cloud.utils.db.TransactionStatus)65 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)63 InternalErrorException (com.cloud.exception.InternalErrorException)57