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 random jwt secret when not custom #1897

Merged
merged 4 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ Thanks to these wonderful people, welcome to join us:
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/LLP2333"><img src="https://avatars.githubusercontent.com/u/61670545?v=4?s=100" width="100px;" alt="llp2333"/><br /><sub><b>llp2333</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=LLP2333" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/HeartLinked"><img src="https://avatars.githubusercontent.com/u/78212101?v=4?s=100" width="100px;" alt="feiyang li"/><br /><sub><b>feiyang li</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=HeartLinked" title="Documentation">📖</a></td>
</tr>
</tbody>
</table>
Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ Thanks these wonderful people, welcome to join us:
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/LLP2333"><img src="https://avatars.githubusercontent.com/u/61670545?v=4?s=100" width="100px;" alt="llp2333"/><br /><sub><b>llp2333</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=LLP2333" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/HeartLinked"><img src="https://avatars.githubusercontent.com/u/78212101?v=4?s=100" width="100px;" alt="feiyang li"/><br /><sub><b>feiyang li</b></sub></a><br /><a href="https://github.com/apache/hertzbeat/commits?author=HeartLinked" title="Documentation">📖</a></td>
</tr>
</tbody>
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.slf4j.LoggerFactory;

/**
* Derived from Apache Dubbo org.apache.dubbo.common.timer.HashedWheelTimer
* <a href="https://github.com/apache/dubbo/blob/3.2/dubbo-common/src/main/java/org/apache/dubbo/common/timer/HashedWheelTimer.java">HashedWheelTimer.java</a>
* A {@link Timer} optimized for approximated I/O timeout scheduling.
*
* <h3>Tick Duration</h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package org.apache.hertzbeat.collector.dispatch.timer;

/**
* Derived from Apache Dubbo org.apache.dubbo.common.timer.Timeout
* <a href="https://github.com/apache/dubbo/blob/3.2/dubbo-common/src/main/java/org/apache/dubbo/common/timer/Timeout.java">Timeout.java</a>
* A handle associated with a {@link TimerTask} that is returned by a
* {@link Timer}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.concurrent.TimeUnit;

/**
* Derived from Apache Dubbo org.apache.dubbo.common.timer.Timer
* <a href="https://github.com/apache/dubbo/blob/3.2/dubbo-common/src/main/java/org/apache/dubbo/common/timer/Timer.java">Timer.java</a>
* Schedules {@link TimerTask}s for one-time future execution in a background
* thread.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.concurrent.TimeUnit;

/**
* Derived from Apache Dubbo org.apache.dubbo.common.timer.TimerTask
* <a href="https://github.com/apache/dubbo/blob/3.2/dubbo-common/src/main/java/org/apache/dubbo/common/timer/TimerTask.java">TimerTask.java</a>
* A task which is executed after the delay specified with
* {@link Timer#newTimeout(TimerTask, long, TimeUnit)} (TimerTask, long, TimeUnit)}.
*/
Expand Down
5 changes: 5 additions & 0 deletions home/src/pages/team/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,11 @@ export default function () {
<td align="center" valign="top" width="14.28%"><a href="https://github.com/LLP2333"><img
src="https://avatars.githubusercontent.com/u/61670545?v=4?s=100" width="100px;" alt="llp2333"/><br/><sub><b>llp2333</b></sub></a><br/><a
href="https://github.com/apache/hertzbeat/commits?author=LLP2333" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/HeartLinked"><img
src="https://avatars.githubusercontent.com/u/78212101?v=4?s=100" width="100px;"
alt="feiyang li"/><br/><sub><b>feiyang li</b></sub></a><br/><a
href="https://github.com/apache/hertzbeat/commits?author=HeartLinked" title="Documentation">📖</a>
</td>
</tr>
</tbody>
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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) {
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<SystemSecret> {


/**
*
* <p>Constructor, passing in GeneralConfigDao, ObjectMapper and type.</p>
*
* @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<SystemSecret> getTypeReference() {
return new TypeReference<>() {
@Override
public Type getType() {
return SystemSecret.class;
}
};
}
}
8 changes: 0 additions & 8 deletions remoting/README.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.hertzbeat.remoting;

/**
* Derived from Apache Rocketmq org.apache.rocketmq.remoting.RemotingService
* remoting service interface
* @see <a href="https://github.com/apache/rocketmq/blob/develop/remoting/src/main/java/org/apache/rocketmq/remoting/RemotingService.java">RemotingService</a>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://github.com/apache/rocketmq/blob/develop/remoting/src/main/java/org/apache/rocketmq/remoting/netty/NettyRemotingAbstract.java">NettyRemotingAbstract</a>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.hertzbeat.remoting.event.NettyEventListener;

/**
* Derived from Apache Rocketmq org.apache.rocketmq.remoting.netty.NettyRemotingClient
* netty client
* @see <a href="https://github.com/apache/rocketmq/blob/develop/remoting/src/main/java/org/apache/rocketmq/remoting/netty/NettyRemotingClient.java">NettyRemotingClient</a>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://github.com/apache/rocketmq/blob/develop/remoting/src/main/java/org/apache/rocketmq/remoting/netty/NettyRequestProcessor.java">NettyRequestProcessor</a>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.apache.hertzbeat.remoting.event.NettyEventListener;

/**
* Derived from Apache Rocketmq org.apache.rocketmq.remoting.netty.NettyRemotingServer
* netty server
* @see <a href="https://github.com/apache/rocketmq/blob/develop/remoting/src/main/java/org/apache/rocketmq/remoting/netty/NettyRemotingServer.java">NettyRemotingServer</a>
*/
Expand Down
Loading