Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.servicecomb.config.event.RefreshGovernanceConfigurationEvent;
import org.apache.servicecomb.config.spi.ConfigCenterConfigurationSource;
import org.apache.servicecomb.foundation.bootstrap.BootStrapService;
import org.apache.servicecomb.foundation.common.LegacyPropertyFactory;
import org.apache.servicecomb.foundation.common.event.EventManager;
import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -110,6 +111,7 @@ public ConfigurationSpringInitializer() {
@Override
public void setEnvironment(Environment environment) {
super.setEnvironment(environment);
LegacyPropertyFactory.setEnvironment(environment);
if (isExternalInit()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 org.apache.servicecomb.foundation.common;

import org.springframework.core.env.Environment;

public class LegacyPropertyFactory {
private static Environment environment;

public static void setEnvironment(Environment environment) {
LegacyPropertyFactory.environment = environment;
}

public static Environment getEnvironment() {
return environment;
}

public static String getStringProperty(String key, String defaultValue) {
return environment.getProperty(key, defaultValue);
}

public static int getIntProperty(String key, int defaultValue) {
return environment.getProperty(key, int.class, defaultValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,22 @@
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

import org.apache.servicecomb.foundation.common.LegacyPropertyFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.config.DynamicPropertyFactory;

public class KeyPairUtils {

private static final Logger LOGGER = LoggerFactory.getLogger(KeyPairUtils.class);

private static final String KEY_GENERATOR_ALGORITHM = DynamicPropertyFactory.getInstance()
.getStringProperty("servicecomb.publicKey.accessControl.keyGeneratorAlgorithm", "RSA").get();;
private static final String KEY_GENERATOR_ALGORITHM = LegacyPropertyFactory
.getStringProperty("servicecomb.publicKey.accessControl.keyGeneratorAlgorithm", "RSA");;

private static final String SIGN_ALG = DynamicPropertyFactory.getInstance()
.getStringProperty("servicecomb.publicKey.accessControl.signAlgorithm", "SHA256withRSA").get();
private static final String SIGN_ALG = LegacyPropertyFactory
.getStringProperty("servicecomb.publicKey.accessControl.signAlgorithm", "SHA256withRSA");

private static final int KEY_SIZE = DynamicPropertyFactory.getInstance()
.getIntProperty("servicecomb.publicKey.accessControl.keySize", 2048).get();
private static final int KEY_SIZE = LegacyPropertyFactory
.getIntProperty("servicecomb.publicKey.accessControl.keySize", 2048);

private static final Base64.Encoder encoder = Base64.getEncoder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,30 @@
*/
package org.apache.servicecomb.foundation.common.utils;

import org.apache.servicecomb.foundation.common.LegacyPropertyFactory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.core.env.Environment;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;

public class TestRSAUtil {
@BeforeAll
public static void setUpClass() {
Environment environment = Mockito.mock(Environment.class);
LegacyPropertyFactory.setEnvironment(environment);
Mockito.when(environment.getProperty("servicecomb.publicKey.accessControl.keyGeneratorAlgorithm", "RSA"))
.thenReturn("RSA");
Mockito.when(environment.getProperty("servicecomb.publicKey.accessControl.signAlgorithm", "SHA256withRSA"))
.thenReturn("SHA256withRSA");
Mockito.when(environment.getProperty("servicecomb.publicKey.accessControl.keySize", int.class, 2048))
.thenReturn(2048);
}

@Test
public void testSignVerify()
Expand Down
4 changes: 4 additions & 0 deletions foundations/foundation-ssl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<name>Java Chassis::Foundations::SSL</name>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.archaius</groupId>
<artifactId>archaius-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Properties;

import com.netflix.config.ConcurrentCompositeConfiguration;
import com.netflix.config.DynamicPropertyFactory;
import org.springframework.core.env.Environment;

/**
* SSL配置选项。
Expand Down Expand Up @@ -263,20 +261,10 @@ private static String listToString(Object[] lists) {
return sb.toString();
}

public static String getStringProperty(ConcurrentCompositeConfiguration configSource, String defaultValue,
String... keys) {
public static String getStringProperty(Environment environment, String defaultValue, String... keys) {
String property = null;
for (String key : keys) {
if (configSource != null) {
Object v = configSource.getProperty(key);
if (v instanceof List) {
property = listToString(((List<?>) v).toArray());
} else {
property = (String) configSource.getProperty(key);
}
} else {
property = DynamicPropertyFactory.getInstance().getStringProperty(key, null).get();
}
property = environment.getProperty(key);
if (property != null) {
break;
}
Expand All @@ -289,17 +277,10 @@ public static String getStringProperty(ConcurrentCompositeConfiguration configSo
}
}

private static boolean getBooleanProperty(ConcurrentCompositeConfiguration configSource, boolean defaultValue,
String... keys) {
private static boolean getBooleanProperty(Environment environment, boolean defaultValue, String... keys) {
String property = null;
for (String key : keys) {
if (configSource != null) {
if (configSource.getProperty(key) != null) {
return configSource.getBoolean(key);
}
} else {
property = DynamicPropertyFactory.getInstance().getStringProperty(key, null).get();
}
property = environment.getProperty(key);
if (property != null) {
break;
}
Expand All @@ -312,83 +293,79 @@ private static boolean getBooleanProperty(ConcurrentCompositeConfiguration confi
}
}

public static SSLOption buildFromYaml(String tag, ConcurrentCompositeConfiguration configSource) {
public static SSLOption buildFromYaml(String tag, Environment environment) {
SSLOption option = new SSLOption();
option.engine = getStringProperty(configSource,
option.engine = getStringProperty(environment,
DEFAULT_OPTION.getEngine(),
"ssl." + tag + ".engine",
"ssl.engine");
option.protocols =
getStringProperty(configSource,
getStringProperty(environment,
DEFAULT_OPTION.getProtocols(),
"ssl." + tag + ".protocols",
"ssl.protocols");
option.ciphers =
getStringProperty(configSource, DEFAULT_OPTION.getCiphers(), "ssl." + tag + ".ciphers", "ssl.ciphers");
getStringProperty(environment, DEFAULT_OPTION.getCiphers(), "ssl." + tag + ".ciphers", "ssl.ciphers");
option.authPeer =
getBooleanProperty(configSource, DEFAULT_OPTION.isAuthPeer(), "ssl." + tag + ".authPeer", "ssl.authPeer");
getBooleanProperty(environment, DEFAULT_OPTION.isAuthPeer(), "ssl." + tag + ".authPeer", "ssl.authPeer");
option.checkCNHost =
getBooleanProperty(configSource,
getBooleanProperty(environment,
DEFAULT_OPTION.isCheckCNHost(),
"ssl." + tag + ".checkCN.host",
"ssl.checkCN.host");
option.checkCNWhite =
getBooleanProperty(configSource,
getBooleanProperty(environment,
DEFAULT_OPTION.isCheckCNWhite(),
"ssl." + tag + ".checkCN.white",
"ssl.checkCN.white");
option.checkCNWhiteFile = getStringProperty(configSource,
option.checkCNWhiteFile = getStringProperty(environment,
DEFAULT_OPTION.getCiphers(),
"ssl." + tag + ".checkCN.white.file",
"ssl.checkCN.white.file");
option.allowRenegociate = getBooleanProperty(configSource,
option.allowRenegociate = getBooleanProperty(environment,
DEFAULT_OPTION.isAllowRenegociate(),
"ssl." + tag + ".allowRenegociate",
"ssl.allowRenegociate");
option.storePath =
getStringProperty(configSource,
getStringProperty(environment,
DEFAULT_OPTION.getStorePath(),
"ssl." + tag + ".storePath",
"ssl.storePath");
option.clientAuth =
getStringProperty(configSource,
getStringProperty(environment,
DEFAULT_OPTION.getClientAuth(),
"ssl." + tag + ".storePath",
"ssl.clientAuth");
option.trustStore =
getStringProperty(configSource,
getStringProperty(environment,
DEFAULT_OPTION.getTrustStore(),
"ssl." + tag + ".trustStore",
"ssl.trustStore");
option.trustStoreType = getStringProperty(configSource,
option.trustStoreType = getStringProperty(environment,
DEFAULT_OPTION.getTrustStoreType(),
"ssl." + tag + ".trustStoreType",
"ssl.trustStoreType");
option.trustStoreValue = getStringProperty(configSource,
option.trustStoreValue = getStringProperty(environment,
DEFAULT_OPTION.getTrustStoreValue(),
"ssl." + tag + ".trustStoreValue",
"ssl.trustStoreValue");
option.keyStore =
getStringProperty(configSource, DEFAULT_OPTION.getKeyStore(), "ssl." + tag + ".keyStore", "ssl.keyStore");
getStringProperty(environment, DEFAULT_OPTION.getKeyStore(), "ssl." + tag + ".keyStore", "ssl.keyStore");
option.keyStoreType =
getStringProperty(configSource,
getStringProperty(environment,
DEFAULT_OPTION.getKeyStoreType(),
"ssl." + tag + ".keyStoreType",
"ssl.keyStoreType");
option.keyStoreValue = getStringProperty(configSource,
option.keyStoreValue = getStringProperty(environment,
DEFAULT_OPTION.getKeyStoreValue(),
"ssl." + tag + ".keyStoreValue",
"ssl.keyStoreValue");
option.crl = getStringProperty(configSource, DEFAULT_OPTION.getCrl(), "ssl." + tag + ".crl", "ssl.crl");
option.crl = getStringProperty(environment, DEFAULT_OPTION.getCrl(), "ssl." + tag + ".crl", "ssl.crl");
option.sslCustomClass =
getStringProperty(configSource, null, "ssl." + tag + ".sslCustomClass", "ssl.sslCustomClass");
getStringProperty(environment, null, "ssl." + tag + ".sslCustomClass", "ssl.sslCustomClass");
return option;
}

public static SSLOption buildFromYaml(String tag) {
return buildFromYaml(tag, null);
}

private void fromProperty(Properties props) {
this.protocols = propString(props, "ssl.protocols");
this.ciphers = propString(props, "ssl.ciphers");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

package org.apache.servicecomb.foundation.ssl;

import com.netflix.config.ConcurrentCompositeConfiguration;
import org.springframework.core.env.Environment;

public interface SSLOptionFactory {
static SSLOptionFactory createSSLOptionFactory(String tag, ConcurrentCompositeConfiguration configSource) {
String name = SSLOption.getStringProperty(configSource,
static SSLOptionFactory createSSLOptionFactory(String tag, Environment environment) {
String name = SSLOption.getStringProperty(environment,
null,
"ssl." + tag + ".sslOptionFactory",
"ssl.sslOptionFactory");
Expand Down
Loading
Loading