diff --git a/utils/src/com/cloud/utils/crypt/EncryptionSecretKeyChecker.java b/utils/src/com/cloud/utils/crypt/EncryptionSecretKeyChecker.java index 0e995c4fc65..b28b067b926 100755 --- a/utils/src/com/cloud/utils/crypt/EncryptionSecretKeyChecker.java +++ b/utils/src/com/cloud/utils/crypt/EncryptionSecretKeyChecker.java @@ -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 { diff --git a/utils/test/com/cloud/utils/crypto/EncryptionSecretKeyCheckerTest.java b/utils/test/com/cloud/utils/crypto/EncryptionSecretKeyCheckerTest.java new file mode 100644 index 00000000000..0f3f0587590 --- /dev/null +++ b/utils/test/com/cloud/utils/crypto/EncryptionSecretKeyCheckerTest.java @@ -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); + } + +} \ No newline at end of file