From f9106bee94214c51887803afc20639ec8ddb620c Mon Sep 17 00:00:00 2001 From: Marko Urh Date: Fri, 3 May 2024 18:11:48 +0200 Subject: [PATCH 1/8] feat: support default superuser --- .../manager/PulsarApplicationListener.java | 79 +++++++++++++++++-- src/main/resources/application.properties | 10 +++ 2 files changed, 84 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java b/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java index 9f3941af..9b9b3c2c 100644 --- a/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java +++ b/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java @@ -38,6 +38,8 @@ public class PulsarApplicationListener implements ApplicationListener environmentEntities = environmentsRepository - .getEnvironmentsList(1, 1); + + seedDefaultSuperuser(); + seedDefaultEnvironment(); + } + + private void seedDefaultSuperuser() { + UserInfoEntity userInfoEntity = new UserInfoEntity(); + userInfoEntity.setName(defaultSuperuserName); + userInfoEntity.setPassword(defaultSuperuserPassword); + userInfoEntity.setDescription(defaultSuperuserDescription); + userInfoEntity.setLocation(defaultSuperuserLocation); + userInfoEntity.setCompany(defaultSuperuserCompany); + userInfoEntity.setPhoneNumber(defaultSuperuserPhoneNumber); + userInfoEntity.setEmail(defaultSuperuserEmail); + + Map userValidateResult = usersService.validateUserInfo(userInfoEntity); + if (userValidateResult.get("error") != null) { + log.error("Superuser seed failed.", userValidateResult.get("error")); + System.exit(-1); + } + if (StringUtils.isBlank(userInfoEntity.getPassword())) { + log.error("Superuser seed failed. Password is required."); + System.exit(-1); + } + + Optional optionalUserEntity = usersRepository.findByUserName(userInfoEntity.getName()); + if (optionalUserEntity.isPresent()) { + log.error("Superuser already exists."); + return; + } + + userInfoEntity.setPassword(DigestUtils.sha256Hex(userInfoEntity.getPassword())); + usersRepository.save(userInfoEntity); + + log.info("Successfully added a default superuser: name = {}, email = {}, password = {}.", + defaultSuperuserName, defaultSuperuserEmail, defaultSuperuserPassword); + } + + private void seedDefaultEnvironment() { + Page environmentEntities = environmentsRepository.getEnvironmentsList(1, 1); + if (environmentEntities.getResult().size() <= 0) { - Optional environmentEntityOptional = environmentsRepository - .findByName(defaultEnvironmentName); + Optional environmentEntityOptional = environmentsRepository.findByName(defaultEnvironmentName); + if (defaultEnvironmentName != null && defaultEnvironmentServiceUrl != null && defaultEnvironmentName.length() > 0 @@ -89,6 +157,7 @@ public void onApplicationEvent(ContextRefreshedEvent event) { log.warn("The default environment already exists."); } } + log.debug("Environments already exist."); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index b038a25b..c01c0091 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -132,6 +132,16 @@ spring.thymeleaf.mode=HTML5 default.environment.name= default.environment.service_url= default.environment.bookie_url= + +# default superuser configuration +default.superuser.name= +default.superuser.password= +default.superuser.description= +default.superuser.location= +default.superuser.company= +default.superuser.phoneNumber= +default.superuser.email= + # enable tls encryption # keytool -import -alias test-keystore -keystore ca-certs -file certs/ca.cert.pem tls.enabled=false From 60e6ae611b0e22e730ec536ff518acd30502655b Mon Sep 17 00:00:00 2001 From: Marko Urh Date: Fri, 3 May 2024 18:41:53 +0200 Subject: [PATCH 2/8] seed only necessary props add enable flag --- .../manager/PulsarApplicationListener.java | 33 +++++++------------ src/main/resources/application.properties | 5 +-- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java b/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java index 9b9b3c2c..b6b716b8 100644 --- a/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java +++ b/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java @@ -49,27 +49,17 @@ public class PulsarApplicationListener implements ApplicationListener userValidateResult = usersService.validateUserInfo(userInfoEntity); if (userValidateResult.get("error") != null) { @@ -112,7 +103,7 @@ private void seedDefaultSuperuser() { Optional optionalUserEntity = usersRepository.findByUserName(userInfoEntity.getName()); if (optionalUserEntity.isPresent()) { - log.error("Superuser already exists."); + log.warn("Superuser already exists."); return; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index c01c0091..9d6b7a61 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -134,12 +134,9 @@ default.environment.service_url= default.environment.bookie_url= # default superuser configuration +default.superuser.enable= default.superuser.name= default.superuser.password= -default.superuser.description= -default.superuser.location= -default.superuser.company= -default.superuser.phoneNumber= default.superuser.email= # enable tls encryption From 27e2f28ecd0d5128ecc9fd976d990a75667d7cee Mon Sep 17 00:00:00 2001 From: Marko Urh Date: Fri, 3 May 2024 18:51:36 +0200 Subject: [PATCH 3/8] remove obsolete pulsar-manager.account --- src/README.md | 16 +++++++--------- .../manager/controller/LoginController.java | 6 ------ .../manager/controller/UsersController.java | 6 ------ src/main/resources/application.properties | 5 ----- 4 files changed, 7 insertions(+), 26 deletions(-) diff --git a/src/README.md b/src/README.md index 319f4f7b..96892091 100644 --- a/src/README.md +++ b/src/README.md @@ -8,15 +8,13 @@ Pulsar manager backend is a supplement and improvement to Pulsar broker. ### Supported configurations of backend -| Name | Default |Description -| ------- | ------- | ------- | -| `server.port` | 7750 | Port of backend service | -| `pulsar-manager.account` | pulsar | Login account | -| `pulsar-manager.password` | pulsar | Login password | -| `redirect.host` | localhost | IP address of front-end service | -| `redirect.port` | 9527 | Port of front-end service | -| `insert.stats.interval` | 30000ms | Time interval for collecting statistical information | -| `clear.stats.interval` | 300000ms | Time interval for cleaning statistics | +| Name | Default | Description | +| ----------------------- | --------- | ---------------------------------------------------- | +| `server.port` | 7750 | Port of backend service | +| `redirect.host` | localhost | IP address of front-end service | +| `redirect.port` | 9527 | Port of front-end service | +| `insert.stats.interval` | 30000ms | Time interval for collecting statistical information | +| `clear.stats.interval` | 300000ms | Time interval for cleaning statistics | ### How to set parameters when starting back-end services diff --git a/src/main/java/org/apache/pulsar/manager/controller/LoginController.java b/src/main/java/org/apache/pulsar/manager/controller/LoginController.java index 08e8563a..893ed026 100644 --- a/src/main/java/org/apache/pulsar/manager/controller/LoginController.java +++ b/src/main/java/org/apache/pulsar/manager/controller/LoginController.java @@ -65,12 +65,6 @@ public LoginController(JwtService jwtService) { @Autowired private CasdoorAuthService casdoorAuthService; - @Value("${pulsar-manager.account}") - private String account; - - @Value("${pulsar-manager.password}") - private String password; - @ApiOperation(value = "Login pulsar manager") @ApiResponses({@ApiResponse(code = 200, message = "ok"), @ApiResponse(code = 500, message = "Internal server error")}) @RequestMapping(value = "/login", method = RequestMethod.POST) diff --git a/src/main/java/org/apache/pulsar/manager/controller/UsersController.java b/src/main/java/org/apache/pulsar/manager/controller/UsersController.java index b4996cab..d6443c79 100644 --- a/src/main/java/org/apache/pulsar/manager/controller/UsersController.java +++ b/src/main/java/org/apache/pulsar/manager/controller/UsersController.java @@ -54,12 +54,6 @@ @Api(description = "Functions under this class are available to super user.") public class UsersController { - @Value("${user.management.enable}") - private boolean userManagementEnable; - - @Value("${pulsar-manager.account}") - private String account; - private final UsersRepository usersRepository; private final UsersService usersService; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 9d6b7a61..80828a28 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -84,11 +84,6 @@ backend.broker.pulsarAdmin.tlsEnableHostnameVerification=false jwt.secret=dab1c8ba-b01b-11e9-b384-186590e06885 jwt.sessionTime=2592000 -# If user.management.enable is true, the following account and password will no longer be valid. -pulsar-manager.account=pulsar -pulsar-manager.password=pulsar -# If true, the database is used for user management -user.management.enable=true # Optional -> SECRET, PRIVATE, default -> PRIVATE, empty -> disable auth # SECRET mode -> bin/pulsar tokens create --secret-key file:///path/to/my-secret.key --subject test-user From 3e1f64a976d5e0ec933461e8f3f3fee39d670e31 Mon Sep 17 00:00:00 2001 From: Marko Urh Date: Fri, 3 May 2024 20:07:25 +0200 Subject: [PATCH 4/8] import --- .../org/apache/pulsar/manager/PulsarApplicationListener.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java b/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java index b6b716b8..079e3ec1 100644 --- a/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java +++ b/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java @@ -18,6 +18,7 @@ import org.apache.pulsar.client.admin.PulsarAdminException; import org.apache.pulsar.manager.entity.EnvironmentEntity; import org.apache.pulsar.manager.entity.EnvironmentsRepository; +import org.apache.pulsar.manager.entity.UsersRepository; import org.apache.pulsar.manager.service.PulsarAdminService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; From 9061187dbea17ed18d1b00698aad21efeb1a521c Mon Sep 17 00:00:00 2001 From: Marko Urh Date: Fri, 3 May 2024 21:01:08 +0200 Subject: [PATCH 5/8] fix build - imports --- .../manager/PulsarApplicationListener.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java b/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java index 079e3ec1..3fd49786 100644 --- a/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java +++ b/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java @@ -13,20 +13,28 @@ */ package org.apache.pulsar.manager; -import com.github.pagehelper.Page; -import lombok.extern.slf4j.Slf4j; +import java.util.Map; +import java.util.Optional; + +import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.lang.StringUtils; import org.apache.pulsar.client.admin.PulsarAdminException; import org.apache.pulsar.manager.entity.EnvironmentEntity; import org.apache.pulsar.manager.entity.EnvironmentsRepository; +import org.apache.pulsar.manager.entity.UserInfoEntity; import org.apache.pulsar.manager.entity.UsersRepository; import org.apache.pulsar.manager.service.PulsarAdminService; +import org.apache.pulsar.manager.service.UsersService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; -import java.util.Optional; +import com.github.pagehelper.Page; + +import lombok.extern.slf4j.Slf4j; + /** * PulsarApplicationListener do something after the spring framework initialization is complete. @@ -41,6 +49,8 @@ public class PulsarApplicationListener implements ApplicationListener Date: Fri, 3 May 2024 21:36:08 +0200 Subject: [PATCH 6/8] disable superuser seed flag by default --- .../org/apache/pulsar/manager/PulsarApplicationListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java b/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java index 3fd49786..6c4961c7 100644 --- a/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java +++ b/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java @@ -61,7 +61,7 @@ public class PulsarApplicationListener implements ApplicationListener Date: Fri, 3 May 2024 21:50:00 +0200 Subject: [PATCH 7/8] queue pr checks From fb83fc025de5280d25e837f0acb1715b49a97a8c Mon Sep 17 00:00:00 2001 From: Marko Urh Date: Fri, 3 May 2024 22:30:56 +0200 Subject: [PATCH 8/8] adjust check --- .../org/apache/pulsar/manager/PulsarApplicationListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java b/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java index 6c4961c7..912061e3 100644 --- a/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java +++ b/src/main/java/org/apache/pulsar/manager/PulsarApplicationListener.java @@ -94,7 +94,7 @@ public void onApplicationEvent(ContextRefreshedEvent event) { } private void seedDefaultSuperuser() { - if(defaultSuperuserEnable) { + if(defaultSuperuserEnable == false) { log.debug("Superuser seed disabled"); return; }