From 36159b47b62d4d9b0e434350baa12f5f1ebafec2 Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Wed, 1 May 2024 13:12:35 +0800 Subject: [PATCH 1/3] support random jwt secret when not custom Signed-off-by: tomsun28 --- .../dispatch/timer/HashedWheelTimer.java | 2 + .../collector/dispatch/timer/Timeout.java | 2 + .../collector/dispatch/timer/Timer.java | 2 + .../collector/dispatch/timer/TimerTask.java | 2 + .../config/CommonCommandLineRunner.java | 43 +++++++++++++- .../manager/pojo/dto/SystemSecret.java | 38 ++++++++++++ .../service/impl/SystemSecretServiceImpl.java | 59 +++++++++++++++++++ remoting/README.md | 8 --- .../hertzbeat/remoting/RemotingClient.java | 3 +- .../hertzbeat/remoting/RemotingServer.java | 3 +- .../hertzbeat/remoting/RemotingService.java | 1 + .../remoting/netty/NettyRemotingAbstract.java | 1 + .../remoting/netty/NettyRemotingClient.java | 1 + .../netty/NettyRemotingProcessor.java | 1 + .../remoting/netty/NettyRemotingServer.java | 1 + 15 files changed, 154 insertions(+), 13 deletions(-) create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/SystemSecret.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SystemSecretServiceImpl.java delete mode 100644 remoting/README.md diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/timer/HashedWheelTimer.java b/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/timer/HashedWheelTimer.java index d03809b3a25..e68fc5434b5 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/timer/HashedWheelTimer.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/timer/HashedWheelTimer.java @@ -37,6 +37,8 @@ import org.slf4j.LoggerFactory; /** + * Derived from Apache Dubbo org.apache.dubbo.common.timer.HashedWheelTimer + * HashedWheelTimer.java * A {@link Timer} optimized for approximated I/O timeout scheduling. * *

Tick Duration

diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/timer/Timeout.java b/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/timer/Timeout.java index 0098c63c326..2e22a1da872 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/timer/Timeout.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/timer/Timeout.java @@ -18,6 +18,8 @@ package org.apache.hertzbeat.collector.dispatch.timer; /** + * Derived from Apache Dubbo org.apache.dubbo.common.timer.Timeout + * Timeout.java * A handle associated with a {@link TimerTask} that is returned by a * {@link Timer}. */ diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/timer/Timer.java b/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/timer/Timer.java index cfc5842b0af..6007d0a79e0 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/timer/Timer.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/timer/Timer.java @@ -21,6 +21,8 @@ import java.util.concurrent.TimeUnit; /** + * Derived from Apache Dubbo org.apache.dubbo.common.timer.Timer + * Timer.java * Schedules {@link TimerTask}s for one-time future execution in a background * thread. */ diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/timer/TimerTask.java b/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/timer/TimerTask.java index a6d27fac789..77b953d502f 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/timer/TimerTask.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/timer/TimerTask.java @@ -20,6 +20,8 @@ import java.util.concurrent.TimeUnit; /** + * Derived from Apache Dubbo org.apache.dubbo.common.timer.TimerTask + * TimerTask.java * A task which is executed after the delay specified with * {@link Timer#newTimeout(TimerTask, long, TimeUnit)} (TimerTask, long, TimeUnit)}. */ diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/config/CommonCommandLineRunner.java b/manager/src/main/java/org/apache/hertzbeat/manager/config/CommonCommandLineRunner.java index c483cab68f4..3717188787d 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/config/CommonCommandLineRunner.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/config/CommonCommandLineRunner.java @@ -18,21 +18,27 @@ package org.apache.hertzbeat.manager.config; import com.fasterxml.jackson.databind.ObjectMapper; +import com.usthe.sureness.util.JsonWebTokenUtil; import jakarta.annotation.Resource; import java.util.Locale; +import java.util.Random; import java.util.TimeZone; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.manager.GeneralConfig; import org.apache.hertzbeat.manager.dao.GeneralConfigDao; import org.apache.hertzbeat.manager.pojo.dto.SystemConfig; +import org.apache.hertzbeat.manager.pojo.dto.SystemSecret; import org.apache.hertzbeat.manager.pojo.dto.TemplateConfig; import org.apache.hertzbeat.manager.service.AppService; import org.apache.hertzbeat.manager.service.impl.SystemGeneralConfigServiceImpl; +import org.apache.hertzbeat.manager.service.impl.SystemSecretServiceImpl; import org.apache.hertzbeat.manager.service.impl.TemplateConfigServiceImpl; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; /** * Common CommandLineRunner class @@ -43,9 +49,20 @@ public class CommonCommandLineRunner implements CommandLineRunner { private static final Integer LANG_REGION_LENGTH = 2; + private static final String DEFAULT_JWT_SECRET = "CyaFv0bwq2Eik0jdrKUtsA6bx3sDJeFV643R " + + "LnfKefTjsIfJLBa2YkhEqEGtcHDTNe4CU6+9 " + + "8tVt4bisXQ13rbN0oxhUZR73M6EByXIO+SV5 " + + "dKhaX0csgOCTlCxq20yhmUea6H6JIpSE2Rwp"; + + @Value("${sureness.jwt.secret:" + DEFAULT_JWT_SECRET + "}") + private String currentJwtSecret; + @Resource private SystemGeneralConfigServiceImpl systemGeneralConfigService; + @Resource + private SystemSecretServiceImpl systemSecretService; + @Resource private TemplateConfigServiceImpl templateConfigService; @@ -60,6 +77,7 @@ public class CommonCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { + // for system config SystemConfig systemConfig = systemGeneralConfigService.getConfig(); if (systemConfig != null) { if (systemConfig.getTimeZoneId() != null) { @@ -86,8 +104,31 @@ public void run(String... args) throws Exception { .build(); generalConfigDao.save(generalConfig2Save); } - // flush the template config in db to memory + // for template config, flush the template config in db to memory TemplateConfig templateConfig = templateConfigService.getConfig(); appService.updateCustomTemplateConfig(templateConfig); + // for system secrets + if (DEFAULT_JWT_SECRET.equals(currentJwtSecret)) { + // use the random jwt secret + SystemSecret systemSecret = systemSecretService.getConfig(); + if (systemSecret == null || !StringUtils.hasText(systemSecret.getJwtSecret())) { + char[] chars = DEFAULT_JWT_SECRET.toCharArray(); + Random rand = new Random(); + for (int i = 0; i < chars.length; i++) { + int index = rand.nextInt(chars.length); + char temp = chars[i]; + chars[i] = chars[index]; + chars[index] = temp; + } + currentJwtSecret = new String(chars); + systemSecret = SystemSecret.builder().jwtSecret(currentJwtSecret).build(); + systemSecretService.saveConfig(systemSecret); + } else { + currentJwtSecret = systemSecret.getJwtSecret(); + } + } + // else use the user custom jwt secret + // set the jwt secret token in util + JsonWebTokenUtil.setDefaultSecretKey(currentJwtSecret); } } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/SystemSecret.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/SystemSecret.java new file mode 100644 index 00000000000..7f6e2c96ebe --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/SystemSecret.java @@ -0,0 +1,38 @@ +/* + * 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.hertzbeat.manager.pojo.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * System Secret Config + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class SystemSecret { + + /** + * secret key for jwt + */ + private String jwtSecret; +} diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SystemSecretServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SystemSecretServiceImpl.java new file mode 100644 index 00000000000..9e2ad1fde3b --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SystemSecretServiceImpl.java @@ -0,0 +1,59 @@ +/* + * 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.hertzbeat.manager.service.impl; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.lang.reflect.Type; +import org.apache.hertzbeat.manager.dao.GeneralConfigDao; +import org.apache.hertzbeat.manager.pojo.dto.SystemSecret; +import org.springframework.stereotype.Service; + +/** + * system config service impl + */ +@Service +public class SystemSecretServiceImpl extends AbstractGeneralConfigServiceImpl { + + + /** + * + *

Constructor, passing in GeneralConfigDao, ObjectMapper and type.

+ * + * @param generalConfigDao ConfigDao object + * @param objectMapper JSON tool object + */ + protected SystemSecretServiceImpl(GeneralConfigDao generalConfigDao, ObjectMapper objectMapper) { + super(generalConfigDao, objectMapper); + } + + @Override + public String type() { + return "secret"; + } + + @Override + protected TypeReference getTypeReference() { + return new TypeReference<>() { + @Override + public Type getType() { + return SystemSecret.class; + } + }; + } +} diff --git a/remoting/README.md b/remoting/README.md deleted file mode 100644 index 790dc6f1f40..00000000000 --- a/remoting/README.md +++ /dev/null @@ -1,8 +0,0 @@ -### Remoting -This is a server-client interaction implemented using the Netty framework, primarily intended for use in distributed scenarios, especially for facilitating communication between Manager and Collector components. - -**Implementation is referenced from the [remoting module](https://github.com/apache/rocketmq/tree/develop/remoting) of [RocketMQ](https://github.com/apache/rocketmq)** - -**Special thanks to RocketMQ and Netty for providing the open-source code.** - - diff --git a/remoting/src/main/java/org/apache/hertzbeat/remoting/RemotingClient.java b/remoting/src/main/java/org/apache/hertzbeat/remoting/RemotingClient.java index e814a7af507..dc5b4a557ca 100644 --- a/remoting/src/main/java/org/apache/hertzbeat/remoting/RemotingClient.java +++ b/remoting/src/main/java/org/apache/hertzbeat/remoting/RemotingClient.java @@ -26,8 +26,7 @@ public interface RemotingClient extends RemotingService { /** - * register remoting processor - * 根据不同的type注册不同的processor + * register remoting processor by type * @param messageType type * @param processor remoting processor */ diff --git a/remoting/src/main/java/org/apache/hertzbeat/remoting/RemotingServer.java b/remoting/src/main/java/org/apache/hertzbeat/remoting/RemotingServer.java index a8798f7c23a..0927e441423 100644 --- a/remoting/src/main/java/org/apache/hertzbeat/remoting/RemotingServer.java +++ b/remoting/src/main/java/org/apache/hertzbeat/remoting/RemotingServer.java @@ -29,8 +29,7 @@ public interface RemotingServer extends RemotingService { /** - * register remoting processor - * 根据不同的type注册不同的processor + * register remoting processor by type * @param messageType type * @param processor remoting processor */ diff --git a/remoting/src/main/java/org/apache/hertzbeat/remoting/RemotingService.java b/remoting/src/main/java/org/apache/hertzbeat/remoting/RemotingService.java index b71538a1738..a5528d9952b 100644 --- a/remoting/src/main/java/org/apache/hertzbeat/remoting/RemotingService.java +++ b/remoting/src/main/java/org/apache/hertzbeat/remoting/RemotingService.java @@ -18,6 +18,7 @@ package org.apache.hertzbeat.remoting; /** + * Derived from Apache Rocketmq org.apache.rocketmq.remoting.RemotingService * remoting service interface * @see RemotingService */ diff --git a/remoting/src/main/java/org/apache/hertzbeat/remoting/netty/NettyRemotingAbstract.java b/remoting/src/main/java/org/apache/hertzbeat/remoting/netty/NettyRemotingAbstract.java index ba87a58ceaf..09b4dc4f4d2 100644 --- a/remoting/src/main/java/org/apache/hertzbeat/remoting/netty/NettyRemotingAbstract.java +++ b/remoting/src/main/java/org/apache/hertzbeat/remoting/netty/NettyRemotingAbstract.java @@ -32,6 +32,7 @@ import org.apache.hertzbeat.remoting.event.NettyEventListener; /** + * Derived from Apache Rocketmq org.apache.rocketmq.remoting.netty.NettyRemotingAbstract * netty remote abstract * @see NettyRemotingAbstract */ diff --git a/remoting/src/main/java/org/apache/hertzbeat/remoting/netty/NettyRemotingClient.java b/remoting/src/main/java/org/apache/hertzbeat/remoting/netty/NettyRemotingClient.java index ebb8498aca1..779be270f62 100644 --- a/remoting/src/main/java/org/apache/hertzbeat/remoting/netty/NettyRemotingClient.java +++ b/remoting/src/main/java/org/apache/hertzbeat/remoting/netty/NettyRemotingClient.java @@ -43,6 +43,7 @@ import org.apache.hertzbeat.remoting.event.NettyEventListener; /** + * Derived from Apache Rocketmq org.apache.rocketmq.remoting.netty.NettyRemotingClient * netty client * @see NettyRemotingClient */ diff --git a/remoting/src/main/java/org/apache/hertzbeat/remoting/netty/NettyRemotingProcessor.java b/remoting/src/main/java/org/apache/hertzbeat/remoting/netty/NettyRemotingProcessor.java index 8f4ca831e80..27c68dc1ff3 100644 --- a/remoting/src/main/java/org/apache/hertzbeat/remoting/netty/NettyRemotingProcessor.java +++ b/remoting/src/main/java/org/apache/hertzbeat/remoting/netty/NettyRemotingProcessor.java @@ -21,6 +21,7 @@ import org.apache.hertzbeat.common.entity.message.ClusterMsg; /** + * Derived from Apache Rocketmq org.apache.rocketmq.remoting.netty.NettyRequestProcessor * netty remoting processor * @see NettyRequestProcessor */ diff --git a/remoting/src/main/java/org/apache/hertzbeat/remoting/netty/NettyRemotingServer.java b/remoting/src/main/java/org/apache/hertzbeat/remoting/netty/NettyRemotingServer.java index fd4b343d86b..a25b9b90157 100644 --- a/remoting/src/main/java/org/apache/hertzbeat/remoting/netty/NettyRemotingServer.java +++ b/remoting/src/main/java/org/apache/hertzbeat/remoting/netty/NettyRemotingServer.java @@ -50,6 +50,7 @@ import org.apache.hertzbeat.remoting.event.NettyEventListener; /** + * Derived from Apache Rocketmq org.apache.rocketmq.remoting.netty.NettyRemotingServer * netty server * @see NettyRemotingServer */ From 7cde942d3991a678d9b48953a4ae5a01239a6135 Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Wed, 1 May 2024 13:13:08 +0800 Subject: [PATCH 2/3] Add @HeartLinked as a contributor --- .all-contributorsrc | 9 +++++++++ README.md | 1 + README_CN.md | 1 + 3 files changed, 11 insertions(+) diff --git a/.all-contributorsrc b/.all-contributorsrc index 07ec7cbe8a2..2a5fd2cc27d 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -1790,6 +1790,15 @@ "contributions": [ "code" ] + }, + { + "login": "HeartLinked", + "name": "feiyang li", + "avatar_url": "https://avatars.githubusercontent.com/u/78212101?v=4", + "profile": "https://github.com/HeartLinked", + "contributions": [ + "doc" + ] } ], "contributorsPerLine": 7, diff --git a/README.md b/README.md index dbf8bd8170e..d90973490cb 100644 --- a/README.md +++ b/README.md @@ -438,6 +438,7 @@ Thanks to these wonderful people, welcome to join us: llp2333
llp2333

💻 + feiyang li
feiyang li

📖 diff --git a/README_CN.md b/README_CN.md index 9e8f0d216ef..7b0daed3e2f 100644 --- a/README_CN.md +++ b/README_CN.md @@ -437,6 +437,7 @@ Thanks these wonderful people, welcome to join us: llp2333
llp2333

💻 + feiyang li
feiyang li

📖 From f0777bc91f36418138d5f0540a044acbeebd5f3d Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Wed, 1 May 2024 13:14:23 +0800 Subject: [PATCH 3/3] support random jwt secret when not custom Signed-off-by: tomsun28 --- home/src/pages/team/index.jsx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/home/src/pages/team/index.jsx b/home/src/pages/team/index.jsx index be74f735661..be3b4573134 100644 --- a/home/src/pages/team/index.jsx +++ b/home/src/pages/team/index.jsx @@ -1018,6 +1018,11 @@ export default function () { llp2333
llp2333

💻 + feiyang li
feiyang li

📖 +