|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.utils.server; |
| 18 | + |
| 19 | +import com.cloud.utils.crypt.EncryptionSecretKeyChecker; |
| 20 | +import org.apache.commons.io.IOUtils; |
| 21 | +import org.apache.log4j.Logger; |
| 22 | +import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; |
| 23 | +import org.jasypt.properties.EncryptableProperties; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.InputStream; |
| 27 | +import java.util.Properties; |
| 28 | + |
| 29 | +public class ServerProperties { |
| 30 | + private static final Logger LOG = Logger.getLogger(ServerProperties.class); |
| 31 | + |
| 32 | + private static Properties properties = new Properties(); |
| 33 | + private static boolean loaded = false; |
| 34 | + public static final String passwordEncryptionType = "password.encryption.type"; |
| 35 | + |
| 36 | + public synchronized static Properties getServerProperties(InputStream inputStream) { |
| 37 | + if (!loaded) { |
| 38 | + Properties serverProps = new Properties(); |
| 39 | + try { |
| 40 | + serverProps.load(inputStream); |
| 41 | + |
| 42 | + EncryptionSecretKeyChecker checker = new EncryptionSecretKeyChecker(); |
| 43 | + checker.check(serverProps, passwordEncryptionType); |
| 44 | + |
| 45 | + if (EncryptionSecretKeyChecker.useEncryption()) { |
| 46 | + StandardPBEStringEncryptor encryptor = EncryptionSecretKeyChecker.getEncryptor(); |
| 47 | + EncryptableProperties encrServerProps = new EncryptableProperties(encryptor); |
| 48 | + encrServerProps.putAll(serverProps); |
| 49 | + serverProps = encrServerProps; |
| 50 | + } |
| 51 | + } catch (IOException e) { |
| 52 | + throw new IllegalStateException("Failed to load server.properties", e); |
| 53 | + } finally { |
| 54 | + IOUtils.closeQuietly(inputStream); |
| 55 | + } |
| 56 | + |
| 57 | + properties = serverProps; |
| 58 | + loaded = true; |
| 59 | + } |
| 60 | + |
| 61 | + return properties; |
| 62 | + } |
| 63 | +} |
0 commit comments