2020package com .cloud .utils .crypt ;
2121
2222import java .io .BufferedReader ;
23- import java .io .File ;
24- import java .io .FileNotFoundException ;
25- import java .io .FileReader ;
2623import java .io .IOException ;
24+ import java .io .InputStream ;
2725import java .io .InputStreamReader ;
2826import java .io .PrintWriter ;
2927import java .net .ServerSocket ;
@@ -45,8 +43,8 @@ public class EncryptionSecretKeyChecker {
4543 private static final Logger s_logger = Logger .getLogger (EncryptionSecretKeyChecker .class );
4644
4745 // Two possible locations with the new packaging naming
48- private static final String s_altKeyFile = "/etc/cloudstack/management/ key" ;
49- private static final String s_keyFile = "/etc/cloudstack/management/ key" ;
46+ private static final String s_altKeyFile = "key" ;
47+ private static final String s_keyFile = "key" ;
5048 private static final String s_envKey = "CLOUD_SECRET_KEY" ;
5149 private static StandardPBEStringEncryptor s_encryptor = new StandardPBEStringEncryptor ();
5250 private static boolean s_useEncryption = false ;
@@ -78,17 +76,18 @@ public void check(Properties dbProps) throws IOException {
7876 SimpleStringPBEConfig stringConfig = new SimpleStringPBEConfig ();
7977
8078 if (encryptionType .equals ("file" )) {
81- File keyFile = new File (s_keyFile );
82- if (!keyFile .exists ()) {
83- keyFile = new File (s_altKeyFile );
79+ InputStream is = this .getClass ().getClassLoader ().getResourceAsStream (s_keyFile );
80+ if (is == null ) {
81+ is = this .getClass ().getClassLoader ().getResourceAsStream (s_altKeyFile );
82+ }
83+ if (is == null ) { //This is means we are not able to load key file from the classpath.
84+ throw new CloudRuntimeException (s_keyFile + " File containing secret key not found in the classpath: " );
8485 }
8586 BufferedReader in = null ;
8687 try {
87- in = new BufferedReader (new FileReader ( keyFile ));
88+ in = new BufferedReader (new InputStreamReader ( is ));
8889 secretKey = in .readLine ();
8990 //Check for null or empty secret key
90- } catch (FileNotFoundException e ) {
91- throw new CloudRuntimeException ("File containing secret key not found: " + s_keyFile , e );
9291 } catch (IOException e ) {
9392 throw new CloudRuntimeException ("Error while reading secret key from: " + s_keyFile , e );
9493 } finally {
0 commit comments