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);
}
}
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);
}
}
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;
}
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");
}
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");
}
Aggregations