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

docs: update docs on seeding default superuser from app props #566

Closed
wants to merge 7 commits into from
Closed
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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ After running these steps, the Pulsar Manager is running locally at http://127.0
* Account: `pulsar`
* Password: `pulsar`

#### Setting up super-user

##### Acquiring token and seeding super-user manually
If you are deploying Pulsar Manager using the latest code, you can create a super-user using the following command. Then you can use the super user credentials to log in the Pulsar Manager UI.

```
Expand All @@ -188,6 +191,22 @@ After running these steps, the Pulsar Manager is running locally at http://127.0

* `backend-service`: The IP address or domain name of the backend service.
* `password`: The password should be more than or equal to 6 digits.

#### Seeding default super-user via application.properties

Application properties expose variables which allow you to configure default super-user that will be seeded on application startup.

Mark `default.superuser.enable` flag as true and configure the rest of the required fields.

Keep in mind that passwords adhere to standard password policies.

```
# default superuser configuration
default.superuser.enable=
default.superuser.name=
default.superuser.password=
default.superuser.email=
```

2. Create an environment.

Expand Down
16 changes: 7 additions & 9 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +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.
Expand All @@ -38,6 +47,10 @@ public class PulsarApplicationListener implements ApplicationListener<ContextRef

private final PulsarAdminService pulsarAdminService;

private final UsersRepository usersRepository;

private final UsersService usersService;

@Value("${default.environment.name}")
private String defaultEnvironmentName;

Expand All @@ -47,20 +60,79 @@ public class PulsarApplicationListener implements ApplicationListener<ContextRef
@Value("${default.environment.bookie_url}")
private String defaultEnvironmentBookieUrl;

@Value("${default.superuser.enable}")
private Boolean defaultSuperuserEnable = false;

@Value("${default.superuser.name}")
private String defaultSuperuserName;

@Value("${default.superuser.email}")
private String defaultSuperuserEmail;

@Value("${default.superuser.password}")
private String defaultSuperuserPassword;

@Autowired
public PulsarApplicationListener(EnvironmentsRepository environmentsRepository, PulsarAdminService pulsarAdminService) {
public PulsarApplicationListener(
EnvironmentsRepository environmentsRepository,
PulsarAdminService pulsarAdminService,
UsersRepository usersRepository,
UsersService usersService
) {
this.environmentsRepository = environmentsRepository;
this.pulsarAdminService = pulsarAdminService;
this.usersRepository = usersRepository;
this.usersService = usersService;
}

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
log.info("Start onApplicationEvent");
Page<EnvironmentEntity> environmentEntities = environmentsRepository
.getEnvironmentsList(1, 1);

seedDefaultSuperuser();
seedDefaultEnvironment();
}

private void seedDefaultSuperuser() {
if(defaultSuperuserEnable) {
log.debug("Superuser seed disabled");
return;
}

UserInfoEntity userInfoEntity = new UserInfoEntity();
userInfoEntity.setName(defaultSuperuserName);
userInfoEntity.setEmail(defaultSuperuserEmail);
userInfoEntity.setPassword(defaultSuperuserPassword);

Map<String, String> 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<UserInfoEntity> optionalUserEntity = usersRepository.findByUserName(userInfoEntity.getName());
if (optionalUserEntity.isPresent()) {
log.warn("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<EnvironmentEntity> environmentEntities = environmentsRepository.getEnvironmentsList(1, 1);

if (environmentEntities.getResult().size() <= 0) {
Optional<EnvironmentEntity> environmentEntityOptional = environmentsRepository
.findByName(defaultEnvironmentName);
Optional<EnvironmentEntity> environmentEntityOptional = environmentsRepository.findByName(defaultEnvironmentName);

if (defaultEnvironmentName != null
&& defaultEnvironmentServiceUrl != null
&& defaultEnvironmentName.length() > 0
Expand Down Expand Up @@ -89,6 +161,7 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
log.warn("The default environment already exists.");
}
}

log.debug("Environments already exist.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -132,6 +127,13 @@ spring.thymeleaf.mode=HTML5
default.environment.name=
default.environment.service_url=
default.environment.bookie_url=

# default superuser configuration
default.superuser.enable=
default.superuser.name=
default.superuser.password=
default.superuser.email=

# enable tls encryption
# keytool -import -alias test-keystore -keystore ca-certs -file certs/ca.cert.pem
tls.enabled=false
Expand Down