Skip to content
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

Open
wants to merge 14 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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 @@ -449,6 +449,11 @@ public interface ConfigurationKeys {
*/
String DISTRIBUTED_LOCK_EXPIRE_TIME = SERVER_PREFIX + "distributedLockExpireTime";

/**
* The constant BLACKLIST
*/
String BLACKLIST = SERVER_PREFIX + "blacklist";

/**
* The constant MIN_SERVER_POOL_SIZE.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void onRegRmMessage(RpcMessage request, ChannelHandlerContext ctx, Regist
boolean isSuccess = false;
String errorInfo = StringUtils.EMPTY;
try {
if (checkAuthHandler == null || checkAuthHandler.regResourceManagerCheckAuth(message)) {
if (checkAuthHandler == null || checkAuthHandler.regResourceManagerCheckAuth(message, ctx)) {
ChannelManager.registerRMChannel(message, ctx.channel());
Version.putChannelVersion(ctx.channel(), message.getVersion());
isSuccess = true;
Expand Down Expand Up @@ -144,7 +144,7 @@ public void onRegTmMessage(RpcMessage request, ChannelHandlerContext ctx, Regist
boolean isSuccess = false;
String errorInfo = StringUtils.EMPTY;
try {
if (checkAuthHandler == null || checkAuthHandler.regTransactionManagerCheckAuth(message)) {
if (checkAuthHandler == null || checkAuthHandler.regTransactionManagerCheckAuth(message, ctx)) {
ChannelManager.registerTMChannel(message, ctx.channel());
Version.putChannelVersion(ctx.channel(), message.getVersion());
isSuccess = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.seata.core.rpc;

import io.netty.channel.ChannelHandlerContext;
import io.seata.core.protocol.RegisterRMRequest;
import io.seata.core.protocol.RegisterTMRequest;

Expand All @@ -29,15 +30,17 @@ public interface RegisterCheckAuthHandler {
* Reg transaction manager check auth boolean.
*
* @param request the request
* @param ctx the ctx
* @return the boolean
*/
boolean regTransactionManagerCheckAuth(RegisterTMRequest request);
Copy link
Member

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.

boolean regTransactionManagerCheckAuth(RegisterTMRequest request, ChannelHandlerContext ctx);

/**
* Reg resource manager check auth boolean.
*
* @param request the request
* @param ctx the ctx
* @return the boolean
*/
boolean regResourceManagerCheckAuth(RegisterRMRequest request);
boolean regResourceManagerCheckAuth(RegisterRMRequest request, ChannelHandlerContext ctx);
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void onRegRmMessage(ChannelHandlerContext ctx, RpcMessage rpcMessage) {
boolean isSuccess = false;
String errorInfo = StringUtils.EMPTY;
try {
if (null == checkAuthHandler || checkAuthHandler.regResourceManagerCheckAuth(message)) {
if (null == checkAuthHandler || checkAuthHandler.regResourceManagerCheckAuth(message, ctx)) {
ChannelManager.registerRMChannel(message, ctx.channel());
Version.putChannelVersion(ctx.channel(), message.getVersion());
isSuccess = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void onRegTmMessage(ChannelHandlerContext ctx, RpcMessage rpcMessage) {
boolean isSuccess = false;
String errorInfo = StringUtils.EMPTY;
try {
if (null == checkAuthHandler || checkAuthHandler.regTransactionManagerCheckAuth(message)) {
if (null == checkAuthHandler || checkAuthHandler.regTransactionManagerCheckAuth(message, ctx)) {
ChannelManager.registerTMChannel(message, ctx.channel());
Version.putChannelVersion(ctx.channel(), message.getVersion());
isSuccess = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class ServerProperties {
private Boolean enableCheckAuth = true;
private Integer retryDeadThreshold = 130000;
private Integer servicePort;
private String blacklist = null;

public Duration getMaxCommitRetryTimeout() {
return maxCommitRetryTimeout;
Expand Down Expand Up @@ -88,4 +89,13 @@ public ServerProperties setServicePort(Integer servicePort) {
this.servicePort = servicePort;
return this;
}

public String getBlacklist() {
return blacklist;
}

public ServerProperties setBlacklist(String blacklist) {
this.blacklist = blacklist;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.seata.server.auth;

import io.netty.channel.ChannelHandlerContext;
import io.seata.config.ConfigurationFactory;
import io.seata.core.constants.ConfigurationKeys;
import io.seata.core.protocol.RegisterRMRequest;
Expand All @@ -32,22 +33,22 @@ public abstract class AbstractCheckAuthHandler implements RegisterCheckAuthHandl
ConfigurationKeys.SERVER_ENABLE_CHECK_AUTH, DEFAULT_SERVER_ENABLE_CHECK_AUTH);

@Override
public boolean regTransactionManagerCheckAuth(RegisterTMRequest request) {
public boolean regTransactionManagerCheckAuth(RegisterTMRequest request, ChannelHandlerContext ctx) {
if (!ENABLE_CHECK_AUTH) {
return true;
}
return doRegTransactionManagerCheck(request);
return doRegTransactionManagerCheck(request, ctx);
}

public abstract boolean doRegTransactionManagerCheck(RegisterTMRequest request);
public abstract boolean doRegTransactionManagerCheck(RegisterTMRequest request, ChannelHandlerContext ctx);

@Override
public boolean regResourceManagerCheckAuth(RegisterRMRequest request) {
public boolean regResourceManagerCheckAuth(RegisterRMRequest request, ChannelHandlerContext ctx) {
if (!ENABLE_CHECK_AUTH) {
return true;
}
return doRegResourceManagerCheck(request);
return doRegResourceManagerCheck(request, ctx);
}

public abstract boolean doRegResourceManagerCheck(RegisterRMRequest request);
public abstract boolean doRegResourceManagerCheck(RegisterRMRequest request, ChannelHandlerContext ctx);
}
72 changes: 72 additions & 0 deletions server/src/main/java/io/seata/server/auth/Blacklist.java
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 = ";";

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
Expand Up @@ -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;

Expand All @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

写个动态代理,代理DefaultCheckAuthHandler吧,避免在代码里出现大量横切性问题

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

按照我的理解,jdk或cglib动态代理DefaultCheckAuthHandler后,会变为spi加载获得对象A,进一步通过动态代理获得增强的对象B,这样对于上层使用DefaultCheckAuthHandler会不会逻辑有些复杂。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果把横切代码提取成方法,虽然横切问题还会存在,但代码会简洁一点,这样是否可行。

Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}
}
1 change: 1 addition & 0 deletions server/src/main/resources/application.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ seata:
undo:
log-save-days: 7
log-delete-period: 86400000
blacklist: 1.1.1.1;2.2.2.2
store:
# support: file 、 db 、 redis
mode: file
Expand Down