-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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
feature: support server authentication blacklist configuration #4052
base: 2.x
Are you sure you want to change the base?
Changes from 6 commits
5d8564c
a81e996
3f484aa
90a647d
18fcab7
84affbe
e9b5989
07e8142
1ffee93
5071e61
5a6bfd0
3d2363f
d2932e2
d2f315c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* 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 io.seata.server.auth; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
import io.seata.config.ConfigurationChangeEvent; | ||
import io.seata.config.ConfigurationChangeListener; | ||
import io.seata.config.ConfigurationFactory; | ||
import io.seata.core.constants.ConfigurationKeys; | ||
|
||
public class Blacklist { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 为什么不是BlackList |
||
|
||
private static final long DEFAULT_CONFIG_TIMEOUT = 5000; | ||
|
||
private static final String IP_CONFIG_SPLIT_CHAR = ";"; | ||
funky-eyes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private List<String> ipList = new CopyOnWriteArrayList<>(); | ||
|
||
public Blacklist(String blacklistConfig) { | ||
String ips = ConfigurationFactory.getInstance().getConfig(blacklistConfig); | ||
if (ips != null) { | ||
String[] ipArray = ips.split(IP_CONFIG_SPLIT_CHAR); | ||
Collections.addAll(ipList, ipArray); | ||
} | ||
|
||
ConfigurationFactory.getInstance().addConfigListener(blacklistConfig, new ConfigurationChangeListener() { | ||
@Override | ||
public void onChangeEvent(ConfigurationChangeEvent event) { | ||
String currentIps = event.getNewValue(); | ||
clear(); | ||
if (currentIps == null) { | ||
return; | ||
} | ||
String[] currentIpArray = currentIps.split(IP_CONFIG_SPLIT_CHAR); | ||
Collections.addAll(ipList, currentIpArray); | ||
} | ||
}); | ||
} | ||
|
||
public void setIpList(List<String> ipList) { | ||
this.ipList = ipList; | ||
} | ||
|
||
public List<String> getIpList() { | ||
return ipList; | ||
} | ||
|
||
public void clear() { | ||
ipList.clear(); | ||
} | ||
|
||
public boolean contains(String address) { | ||
return ipList.contains(address); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,10 @@ | |
*/ | ||
package io.seata.server.auth; | ||
|
||
import io.netty.channel.ChannelHandlerContext; | ||
import io.seata.common.loader.LoadLevel; | ||
import io.seata.common.util.NetUtil; | ||
import io.seata.core.constants.ConfigurationKeys; | ||
import io.seata.core.protocol.RegisterRMRequest; | ||
import io.seata.core.protocol.RegisterTMRequest; | ||
|
||
|
@@ -25,13 +28,23 @@ | |
@LoadLevel(name = "defaultCheckAuthHandler", order = 100) | ||
public class DefaultCheckAuthHandler extends AbstractCheckAuthHandler { | ||
|
||
Blacklist blacklist = new Blacklist(ConfigurationKeys.BLACKLIST); | ||
|
||
@Override | ||
public boolean doRegTransactionManagerCheck(RegisterTMRequest request) { | ||
public boolean doRegTransactionManagerCheck(RegisterTMRequest request, ChannelHandlerContext ctx) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 写个动态代理,代理DefaultCheckAuthHandler吧,避免在代码里出现大量横切性问题 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 按照我的理解,jdk或cglib动态代理DefaultCheckAuthHandler后,会变为spi加载获得对象A,进一步通过动态代理获得增强的对象B,这样对于上层使用DefaultCheckAuthHandler会不会逻辑有些复杂。 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
可以 |
||
String ip = NetUtil.toStringAddress(ctx.channel().remoteAddress()).split(":")[0]; | ||
if (blacklist.contains(ip)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean doRegResourceManagerCheck(RegisterRMRequest request) { | ||
public boolean doRegResourceManagerCheck(RegisterRMRequest request, ChannelHandlerContext ctx) { | ||
String ip = NetUtil.toStringAddress(ctx.channel().remoteAddress()).split(":")[0]; | ||
if (blacklist.contains(ip)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
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.
The RegisterTMRequest parameter needs to be reserved. The parameter is used to verify the validity of client information.