CLOUDSTACK-6354 : removing the hard coding of key path in EncryptionSecretKeyChecker

This commit is contained in:
Damodar Reddy 2014-04-09 17:56:05 +05:30 committed by Kishan Kavala
parent 0615d4e6a5
commit 1d0b14673d
2 changed files with 55 additions and 11 deletions

View File

@ -20,10 +20,8 @@
package com.cloud.utils.crypt;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
@ -45,8 +43,8 @@ public class EncryptionSecretKeyChecker {
private static final Logger s_logger = Logger.getLogger(EncryptionSecretKeyChecker.class);
// Two possible locations with the new packaging naming
private static final String s_altKeyFile = "/etc/cloudstack/management/key";
private static final String s_keyFile = "/etc/cloudstack/management/key";
private static final String s_altKeyFile = "key";
private static final String s_keyFile = "key";
private static final String s_envKey = "CLOUD_SECRET_KEY";
private static StandardPBEStringEncryptor s_encryptor = new StandardPBEStringEncryptor();
private static boolean s_useEncryption = false;
@ -78,17 +76,18 @@ public class EncryptionSecretKeyChecker {
SimpleStringPBEConfig stringConfig = new SimpleStringPBEConfig();
if (encryptionType.equals("file")) {
File keyFile = new File(s_keyFile);
if (!keyFile.exists()) {
keyFile = new File(s_altKeyFile);
InputStream is = this.getClass().getClassLoader().getResourceAsStream(s_keyFile);
if (is == null) {
is = this.getClass().getClassLoader().getResourceAsStream(s_altKeyFile);
}
if(is == null) { //This is means we are not able to load key file from the classpath.
throw new CloudRuntimeException(s_keyFile + " File containing secret key not found in the classpath: ");
}
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(keyFile));
in = new BufferedReader(new InputStreamReader(is));
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);
} finally {

View File

@ -0,0 +1,45 @@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
package com.cloud.utils.crypto;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Properties;
import org.junit.Assert;
import org.junit.Test;
import com.cloud.utils.crypt.EncryptionSecretKeyChecker;
import com.cloud.utils.db.DbProperties;
import com.cloud.utils.exception.CloudRuntimeException;
public class EncryptionSecretKeyCheckerTest {
private EncryptionSecretKeyChecker checker = new EncryptionSecretKeyChecker();
@Test(expected = CloudRuntimeException.class)
public void testKeyFileDoesNotExists() throws IOException, URISyntaxException {
Assert.assertNotNull(checker);
Properties properties = DbProperties.getDbProperties();
properties.setProperty("db.cloud.encryption.type", "file");
checker.check(properties);
}
}