Skip to content

Commit ae9eb75

Browse files
committed
fix issue where bootstrap account is created when there are already administrators
1 parent 9187a06 commit ae9eb75

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

horreum-backend/src/main/java/io/hyperfoil/tools/horreum/server/SecurityBootstrap.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.hyperfoil.tools.horreum.entity.user.UserInfo;
2424
import io.hyperfoil.tools.horreum.entity.user.UserRole;
2525
import io.hyperfoil.tools.horreum.svc.Roles;
26+
import io.hyperfoil.tools.horreum.svc.UserServiceImpl;
2627
import io.hyperfoil.tools.horreum.svc.user.UserBackEnd;
2728
import io.quarkus.logging.Log;
2829
import io.quarkus.runtime.LaunchMode;
@@ -128,8 +129,6 @@ private void addTeamMembership(UserInfo userInfo, String teamName, TeamRole role
128129
* Create an admin account if there are no accounts in the system.
129130
* The account should be removed once other accounts are created.
130131
*/
131-
@WithRoles(extras = BOOTSTRAP_ACCOUNT)
132-
@Transactional
133132
public void checkBootstrapAccount() {
134133
// checks the list of administrators. a user cannot remove himself nor create the bootstrap account (restricted namespace)
135134
List<String> administrators = backend.get().administrators().stream().map(userData -> userData.username).toList();
@@ -149,20 +148,17 @@ public void checkBootstrapAccount() {
149148
backend.get().updateTeamMembers("dev-team",
150149
Map.of(BOOTSTRAP_ACCOUNT, List.of(Roles.MANAGER, Roles.TESTER, Roles.UPLOADER, Roles.VIEWER)));
151150

152-
// create db entry, if not existent, like in UserService.createLocalUser()
153-
UserInfo userInfo = UserInfo.<UserInfo> findByIdOptional(BOOTSTRAP_ACCOUNT).orElse(new UserInfo(BOOTSTRAP_ACCOUNT));
154-
userInfo.defaultTeam = "dev-team";
155-
userInfo.persist();
151+
UserServiceImpl.createLocalUser(BOOTSTRAP_ACCOUNT, "dev-team");
156152

157153
Log.infov("\n>>>\n>>> Created temporary account {0} with password {1}\n>>>", BOOTSTRAP_ACCOUNT, user.password);
158154
} else if (administrators.size() > 1 && administrators.contains(BOOTSTRAP_ACCOUNT)) {
159155
Log.warnv("The temporary account {0} can be removed", BOOTSTRAP_ACCOUNT);
160156
}
161157
}
162158

163-
public static String generateRandomPassword(int lenght) {
164-
StringBuilder builder = new StringBuilder(lenght);
165-
new SecureRandom().ints(lenght, 0, RANDOM_PASSWORD_CHARS.length).mapToObj(i -> RANDOM_PASSWORD_CHARS[i])
159+
public static String generateRandomPassword(int length) {
160+
StringBuilder builder = new StringBuilder(length);
161+
new SecureRandom().ints(length, 0, RANDOM_PASSWORD_CHARS.length).mapToObj(i -> RANDOM_PASSWORD_CHARS[i])
166162
.forEach(builder::append);
167163
return builder.toString();
168164
}

horreum-backend/src/main/java/io/hyperfoil/tools/horreum/svc/UserServiceImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ private static String validateTeamName(String unsafeTeam) {
223223
*/
224224
@Transactional
225225
@WithRoles(fromParams = FirstParameter.class)
226-
void createLocalUser(String username, String defaultTeam) {
226+
public static void createLocalUser(String username, String defaultTeam) {
227227
UserInfo userInfo = UserInfo.<UserInfo> findByIdOptional(username).orElse(new UserInfo(username));
228228
if (defaultTeam != null) {
229229
userInfo.defaultTeam = defaultTeam;

0 commit comments

Comments
 (0)