-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ISSUE#3315]Nacos client support https #3654
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2391453
[ISSUE #3315] nacos client support https
wangweizZZ a837c70
[ISSUE #3315] nacos client support https
wangweizZZ 78a9f62
[ISSUE #3315] nacos client support https
wangweizZZ 85ffdde
Merge remote-tracking branch 'alibaba/develop' into feature_https
wangweizZZ 9b654e4
format code
wangweizZZ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
common/src/main/java/com/alibaba/nacos/common/tls/PemReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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.alibaba.nacos.common.tls; | ||
|
||
import com.alibaba.nacos.common.codec.Base64; | ||
import com.alibaba.nacos.common.utils.IoUtils; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.security.KeyException; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Reads a PEM file and converts it into a list of DERs. Support PKCS #8 format | ||
* | ||
* @author wangwei | ||
* @date 2020/8/20 9:32 AM | ||
*/ | ||
final class PemReader { | ||
|
||
/** | ||
* Header + Base64 text + Footer. | ||
*/ | ||
private static final Pattern KEY_PATTERN = Pattern.compile( | ||
"-+BEGIN\\s+.*PRIVATE\\s+KEY[^-]*-+(?:\\s|\\r|\\n)+" + "([a-z0-9+/=\\r\\n]+)" | ||
+ "-+END\\s+.*PRIVATE\\s+KEY[^-]*-+", Pattern.CASE_INSENSITIVE); | ||
|
||
private static final String ENCODE_US_ASCII = "US-ASCII"; | ||
|
||
static byte[] readPrivateKey(String keyPath) throws KeyException { | ||
try { | ||
InputStream in = new FileInputStream(keyPath); | ||
try { | ||
String content; | ||
try { | ||
content = IoUtils.toString(in, ENCODE_US_ASCII); | ||
} catch (IOException e) { | ||
throw new KeyException("failed to read key input stream", e); | ||
} | ||
|
||
Matcher m = KEY_PATTERN.matcher(content); | ||
if (!m.find()) { | ||
throw new KeyException("could not find a PKCS #8 private key in input stream"); | ||
} | ||
return Base64.decodeBase64(m.group(1).getBytes()); | ||
} finally { | ||
IoUtils.closeQuietly(in); | ||
} | ||
} catch (FileNotFoundException e) { | ||
throw new KeyException("could not fine key file: " + keyPath); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
common/src/main/java/com/alibaba/nacos/common/tls/SelfHostnameVerifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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.alibaba.nacos.common.tls; | ||
|
||
import com.alibaba.nacos.common.utils.IpUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.net.ssl.HostnameVerifier; | ||
import javax.net.ssl.SSLSession; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* A HostnameVerifier verify ipv4 and localhost. | ||
* | ||
* @author wangwei | ||
*/ | ||
|
||
public final class SelfHostnameVerifier implements HostnameVerifier { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(SelfHostnameVerifier.class); | ||
|
||
private final HostnameVerifier hv; | ||
|
||
private static ConcurrentHashMap<String, Boolean> hosts = new ConcurrentHashMap<String, Boolean>(); | ||
|
||
private static final String[] LOCALHOST_HOSTNAME = new String[] {"localhost", "127.0.0.1"}; | ||
|
||
public SelfHostnameVerifier(HostnameVerifier hv) { | ||
this.hv = hv; | ||
} | ||
|
||
@Override | ||
public boolean verify(String hostname, SSLSession session) { | ||
if (LOCALHOST_HOSTNAME[0].equalsIgnoreCase(hostname) || LOCALHOST_HOSTNAME[1].equals(hostname)) { | ||
return true; | ||
} | ||
if (isIpv4(hostname)) { | ||
return true; | ||
} | ||
return hv.verify(hostname, session); | ||
} | ||
|
||
private static boolean isIpv4(String host) { | ||
if (host == null || host.isEmpty()) { | ||
LOGGER.warn("host is empty, isIPv4 = false"); | ||
return false; | ||
} | ||
Boolean cacheHostVerify = hosts.get(host); | ||
if (cacheHostVerify != null) { | ||
return cacheHostVerify; | ||
} | ||
boolean isIp = IpUtils.isIpv4(host); | ||
hosts.putIfAbsent(host, isIp); | ||
return isIp; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的逻辑是不是可以独立为一个函数,看起来相对简洁点。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是指tlsFileWatcher这一段吗?
addSSLContextChangeListener(FileChangeListener Listener)
?独立成一个函数,这样至少对使用AbstractHttpClientFactory.java 类的开发者来说屏蔽一些
tlsClientTrustCertPath
这样的细节。是好点,我改下。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
从51行开始到68行,感觉这里可以独立为一个函数。