-
Notifications
You must be signed in to change notification settings - Fork 580
refactor: adjust the related filters of sofa-bolt #2735
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
Changes from all commits
e073dea
4b22861
fd1c444
53ddc15
c1ce40b
0597d55
4b14a3a
05ac044
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,84 @@ | ||
| /* | ||
| * 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.hugegraph.pd.raft.auth; | ||
|
|
||
| import java.net.InetSocketAddress; | ||
| import java.util.Collections; | ||
| import java.util.Set; | ||
|
|
||
| import io.netty.channel.ChannelDuplexHandler; | ||
| import io.netty.channel.ChannelHandler; | ||
| import io.netty.channel.ChannelHandlerContext; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Slf4j | ||
|
Check warning on line 29 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/auth/IpAuthHandler.java
|
||
| @ChannelHandler.Sharable | ||
| public class IpAuthHandler extends ChannelDuplexHandler { | ||
|
|
||
| private final Set<String> allowedIps; | ||
| private static volatile IpAuthHandler instance; | ||
|
|
||
| private IpAuthHandler(Set<String> allowedIps) { | ||
| this.allowedIps = Collections.unmodifiableSet(allowedIps); | ||
| } | ||
|
Check warning on line 38 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/auth/IpAuthHandler.java
|
||
|
|
||
| public static IpAuthHandler getInstance(Set<String> allowedIps) { | ||
haohao0103 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (instance == null) { | ||
| synchronized (IpAuthHandler.class) { | ||
|
Check warning on line 42 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/auth/IpAuthHandler.java
|
||
| if (instance == null) { | ||
| instance = new IpAuthHandler(allowedIps); | ||
|
Check warning on line 44 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/auth/IpAuthHandler.java
|
||
| } | ||
| } | ||
|
Check warning on line 46 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/auth/IpAuthHandler.java
|
||
| } | ||
| return instance; | ||
|
Check warning on line 48 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/auth/IpAuthHandler.java
|
||
| } | ||
|
|
||
| @Override | ||
| public void channelActive(ChannelHandlerContext ctx) throws Exception { | ||
| String clientIp = getClientIp(ctx); | ||
|
Check warning on line 53 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/auth/IpAuthHandler.java
|
||
| if (!isIpAllowed(clientIp)) { | ||
| log.warn("Blocked connection from {}", clientIp); | ||
| ctx.close(); | ||
| return; | ||
|
Check warning on line 57 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/auth/IpAuthHandler.java
|
||
| } | ||
| super.channelActive(ctx); | ||
| } | ||
|
Check warning on line 60 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/auth/IpAuthHandler.java
|
||
|
|
||
| private static String getClientIp(ChannelHandlerContext ctx) { | ||
| InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress(); | ||
| return remoteAddress.getAddress().getHostAddress(); | ||
|
Check warning on line 64 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/auth/IpAuthHandler.java
|
||
| } | ||
|
|
||
| private boolean isIpAllowed(String ip) { | ||
| return allowedIps.isEmpty() || allowedIps.contains(ip); | ||
| } | ||
|
|
||
| @Override | ||
| public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { | ||
| String clientIp = getClientIp(ctx); | ||
| log.warn("Client : {} connection exception : {}", clientIp, cause); | ||
|
Check warning on line 74 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/auth/IpAuthHandler.java
|
||
| if (ctx.channel().isActive()) { | ||
| ctx.close().addListener(future -> { | ||
|
Check warning on line 76 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/auth/IpAuthHandler.java
|
||
| if (!future.isSuccess()) { | ||
| log.warn("Client: {} connection closed failed: {}", | ||
| clientIp, future.cause().getMessage()); | ||
|
Check warning on line 79 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/auth/IpAuthHandler.java
|
||
| } | ||
| }); | ||
|
Check warning on line 81 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/auth/IpAuthHandler.java
|
||
| } | ||
| } | ||
|
Check warning on line 83 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/auth/IpAuthHandler.java
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| /* | ||
| * 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.hugegraph.pd.raft.serializer; | ||
|
|
||
| import com.caucho.hessian.io.Deserializer; | ||
| import com.caucho.hessian.io.HessianProtocolException; | ||
| import com.caucho.hessian.io.Serializer; | ||
| import com.caucho.hessian.io.SerializerFactory; | ||
|
|
||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| import java.text.SimpleDateFormat; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Calendar; | ||
| import java.util.Date; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.TreeMap; | ||
| import java.util.TreeSet; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentMap; | ||
| import java.util.concurrent.ConcurrentSkipListMap; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| @Slf4j | ||
|
Check warning on line 55 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
| public class HugegraphHessianSerializerFactory extends SerializerFactory { | ||
|
|
||
| private static final HugegraphHessianSerializerFactory INSTANCE = new HugegraphHessianSerializerFactory(); | ||
|
Check warning on line 58 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
|
|
||
| private HugegraphHessianSerializerFactory() { | ||
| super(); | ||
| initWhitelist(); | ||
| } | ||
|
Check warning on line 63 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
|
|
||
| public static HugegraphHessianSerializerFactory getInstance() { | ||
| return INSTANCE; | ||
|
Check warning on line 66 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
| } | ||
|
|
||
| private final Set<String> whitelist = new HashSet<>(); | ||
|
Check warning on line 69 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
|
|
||
| private void initWhitelist() { | ||
| allowBasicType(); | ||
| allowCollections(); | ||
| allowConcurrent(); | ||
| allowTime(); | ||
| allowBusinessClasses(); | ||
| } | ||
|
Check warning on line 77 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
|
|
||
| private void allowBasicType() { | ||
| addToWhitelist( | ||
|
Check warning on line 80 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
| boolean.class, byte.class, char.class, double.class, | ||
| float.class, int.class, long.class, short.class, | ||
| Boolean.class, Byte.class, Character.class, Double.class, | ||
| Float.class, Integer.class, Long.class, Short.class, | ||
| String.class, Class.class, Number.class | ||
| ); | ||
| } | ||
|
Check warning on line 87 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
|
|
||
| private void allowCollections() { | ||
| addToWhitelist( | ||
|
Check warning on line 90 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
| List.class, ArrayList.class, LinkedList.class, | ||
| Set.class, HashSet.class, LinkedHashSet.class, TreeSet.class, | ||
| Map.class, HashMap.class, LinkedHashMap.class, TreeMap.class | ||
| ); | ||
| } | ||
|
Check warning on line 95 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
|
|
||
| private void allowConcurrent() { | ||
| addToWhitelist( | ||
|
Check warning on line 98 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
| AtomicBoolean.class, AtomicInteger.class, AtomicLong.class, AtomicReference.class, | ||
| ConcurrentMap.class, ConcurrentHashMap.class, ConcurrentSkipListMap.class, CopyOnWriteArrayList.class | ||
| ); | ||
| } | ||
|
Check warning on line 102 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
|
|
||
| private void allowTime() { | ||
| addToWhitelist( | ||
|
Check warning on line 105 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
| Date.class, Calendar.class, TimeUnit.class, | ||
| SimpleDateFormat.class, DateTimeFormatter.class | ||
| ); | ||
| tryAddClass("java.time.LocalDate"); | ||
| tryAddClass("java.time.LocalDateTime"); | ||
| tryAddClass("java.time.Instant"); | ||
| } | ||
|
Check warning on line 112 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
|
|
||
| private void allowBusinessClasses() { | ||
| addToWhitelist( | ||
|
Check warning on line 115 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
| org.apache.hugegraph.pd.raft.KVOperation.class, | ||
| byte[].class | ||
| ); | ||
| } | ||
|
Check warning on line 119 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
|
|
||
| private void addToWhitelist(Class<?>... classes) { | ||
| for (Class<?> clazz : classes) { | ||
| whitelist.add(clazz.getName()); | ||
|
Check warning on line 123 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
| } | ||
| } | ||
|
Check warning on line 125 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
|
|
||
| private void tryAddClass(String className) { | ||
| try { | ||
| Class.forName(className); | ||
| whitelist.add(className); | ||
| } catch (ClassNotFoundException e) { | ||
| log.warn("Failed to load class {}", className); | ||
| } | ||
| } | ||
|
Check warning on line 134 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
|
|
||
| @Override | ||
| public Serializer getSerializer(Class cl) throws HessianProtocolException { | ||
| checkWhitelist(cl); | ||
| return super.getSerializer(cl); | ||
|
Check warning on line 139 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
| } | ||
|
|
||
| @Override | ||
| public Deserializer getDeserializer(Class cl) throws HessianProtocolException { | ||
| checkWhitelist(cl); | ||
| return super.getDeserializer(cl); | ||
|
Check warning on line 145 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
| } | ||
|
|
||
| private void checkWhitelist(Class cl) { | ||
| String className = cl.getName(); | ||
|
Check warning on line 149 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
| if (!whitelist.contains(className)) { | ||
| log.warn("Security alert: Blocked unauthorized class [{}] at {}", | ||
|
Check warning on line 151 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
| className, new Date()); | ||
| throw new SecurityException("hessian serialize unauthorized class: " + className); | ||
|
Check warning on line 153 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
| } | ||
| } | ||
|
Check warning on line 155 in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/serializer/HugegraphHessianSerializerFactory.java
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
How does the raft group member change? If the raft member changed, the whiteList ip should been sync changed
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.
1、The whitelist IPs are sourced from the peer-list configuration file. The current solution rejects all Raft-related requests from nodes not in the peer-list.
2、I understand that the members of the Raft group are a subset of the peer-list. Please correct me if there are any errors in this understanding.
thanks