Skip to content

Commit 18fd1c9

Browse files
committed
Apply comment
1 parent f52499f commit 18fd1c9

File tree

3 files changed

+72
-40
lines changed

3 files changed

+72
-40
lines changed

.github/workflows/build.yml

-6
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,13 @@ jobs:
6666
# step 5.1
6767
- name: "Test, Check style, Check PMD, Check license with Maven and Java8"
6868
if: matrix.java == '8'
69-
env:
70-
SEATA_CONSOLE_USERNAME: seata
71-
SEATA_CONSOLE_PASSWORD: seata
7269
run: |
7370
./mvnw -T 4C clean test \
7471
-Dcheckstyle.skip=false -Dpmd.skip=false -Dlicense.skip=false -DredisCaseEnabled=true \
7572
-e -B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn;
7673
# step 5.2
7774
- name: "Test with Maven and Java${{ matrix.java }}"
7875
if: matrix.java != '8'
79-
env:
80-
SEATA_CONSOLE_USERNAME: seata
81-
SEATA_CONSOLE_PASSWORD: seata
8276
run: |
8377
./mvnw -T 4C clean test \
8478
-e -B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn;

console/src/main/java/org/apache/seata/console/security/CustomUserDetailsServiceImpl.java

+24-34
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
*/
1717
package org.apache.seata.console.security;
1818

19-
import java.io.BufferedReader;
20-
import java.io.Console;
21-
import java.io.IOException;
22-
import java.io.InputStreamReader;
23-
import java.util.Arrays;
19+
import java.util.UUID;
2420
import javax.annotation.PostConstruct;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
import org.springframework.beans.factory.annotation.Value;
2524
import org.springframework.security.core.userdetails.UserDetails;
2625
import org.springframework.security.core.userdetails.UserDetailsService;
2726
import org.springframework.security.core.userdetails.UsernameNotFoundException;
@@ -34,47 +33,38 @@
3433
@Service
3534
public class CustomUserDetailsServiceImpl implements UserDetailsService {
3635

37-
private Console console;
36+
private static final Logger LOGGER = LoggerFactory.getLogger(CustomUserDetailsServiceImpl.class);
37+
38+
@Value("${console.user.username:seata}")
39+
private String username;
40+
41+
@Value("${console.user.password:}")
42+
private String password;
43+
3844
private User user;
3945

4046
/**
4147
* Init.
4248
*/
4349
@PostConstruct
44-
public void init() throws IOException {
45-
String envUsername = System.getenv("SEATA_CONSOLE_USERNAME");
46-
String envPassword = System.getenv("SEATA_CONSOLE_PASSWORD");
47-
48-
if (envUsername != null && envPassword != null) {
49-
user = new User(envUsername, envPassword);
50-
return;
51-
}
52-
53-
console = System.console();
54-
if (console == null) {
55-
// In an IDE, 'System.console()' returns 'null', so 'BufferedReader' is used instead.
56-
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
57-
String username = promptInput(reader, "Username: ");
58-
String password = promptInput(reader, "Password: ");
59-
user = new User(username, password);
60-
} else {
61-
String username = getUsername();
62-
String password = getUserPassword();
50+
public void init() {
51+
if (!password.isEmpty()) {
6352
user = new User(username, password);
53+
return;
6454
}
65-
}
6655

67-
private String promptInput(BufferedReader reader, String message) throws IOException {
68-
System.out.print(message);
69-
return reader.readLine();
70-
}
56+
password = generateRandomPassword();
57+
LOGGER.info(
58+
"No password was configured. A random password has been generated for security purposes. You may either:\n"
59+
+ "1. Use the auto-generated password: [{}]\n"
60+
+ "2. Set a custom password in the configuration.",
61+
password);
7162

72-
private String getUserPassword() {
73-
return Arrays.toString(console.readPassword("Password: "));
63+
user = new User(username, password);
7464
}
7565

76-
private String getUsername() {
77-
return console.readLine("Username: ");
66+
private String generateRandomPassword() {
67+
return UUID.randomUUID().toString().replace("-", "").substring(0, 8);
7868
}
7969

8070
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.seata.namingserver;
19+
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
23+
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.api.extension.ExtendWith;
25+
import org.springframework.boot.SpringApplication;
26+
import org.springframework.boot.test.system.CapturedOutput;
27+
import org.springframework.boot.test.system.OutputCaptureExtension;
28+
29+
@ExtendWith(OutputCaptureExtension.class)
30+
class NamingControllerSmokeTest {
31+
32+
@Test
33+
void processShouldPrintLogAndGeneratePasswordWhenDefaultPasswordIsNotDefined(CapturedOutput output) {
34+
SpringApplication.run(NamingserverApplication.class, "--server.port=0");
35+
36+
String logs = output.getOut();
37+
assertTrue(logs.contains("No password was configured."));
38+
}
39+
40+
@Test
41+
void processShouldNotPrintLogWhenDefaultPasswordIsDefined(CapturedOutput output) {
42+
SpringApplication.run(NamingserverApplication.class,
43+
"--console.user.password=test123", "--server.port=0");
44+
45+
String logs = output.getOut();
46+
assertFalse(logs.contains("No password was configured."));
47+
}
48+
}

0 commit comments

Comments
 (0)