Skip to content

Commit 1f6773a

Browse files
committed
Fix keycloak deployment in dev profile
1 parent 2851657 commit 1f6773a

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

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

+12-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.hyperfoil.tools.horreum.svc.Roles;
1010
import io.hyperfoil.tools.horreum.svc.user.UserBackEnd;
1111
import io.quarkus.logging.Log;
12+
import io.quarkus.runtime.LaunchMode;
1213
import io.quarkus.runtime.StartupEvent;
1314
import jakarta.enterprise.context.ApplicationScoped;
1415
import jakarta.enterprise.event.Observes;
@@ -25,8 +26,6 @@
2526
import java.util.Map;
2627
import java.util.Optional;
2728

28-
import static io.quarkus.runtime.configuration.ProfileManager.getLaunchMode;
29-
3029
@ApplicationScoped public class SecurityBootstrap {
3130

3231
@ConfigProperty(name = "quarkus.keycloak.admin-client.server-url") Optional<String> keycloakURL;
@@ -39,7 +38,7 @@
3938
private static final String MIGRATION_PROVIDER = "database";
4039
private static final String BOOTSTRAP_ACCOUNT = "horreum.bootstrap";
4140

42-
private static final char[] RANDOM_PASSWRORD_CHARS = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789").toCharArray();
41+
private static final char[] RANDOM_PASSWORD_CHARS = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789").toCharArray();
4342
private static final int RANDOM_PASSWORD_DEFAULT_LENGTH = 16;
4443

4544
@Inject RoleManager roleManager;
@@ -115,15 +114,16 @@ private void addTeamMembership(UserInfo userInfo, String teamName, TeamRole role
115114
* Create an admin account if there are no accounts in the system.
116115
* The account should be removed once other accounts are created.
117116
*/
118-
public void checkBootstrapAccount() {
117+
@WithRoles(extras = BOOTSTRAP_ACCOUNT)
118+
@Transactional public void checkBootstrapAccount() {
119119
// checks the list of administrators. a user cannot remove himself nor create the bootstrap account (restricted namespace)
120120
List<String> administrators = backend.get().administrators().stream().map(userData -> userData.username).toList();
121121
if (administrators.isEmpty()) {
122122
UserService.NewUser user = new UserService.NewUser();
123-
user.user = new UserService.UserData("", BOOTSTRAP_ACCOUNT, "Bootstrap", "Acount", "[email protected]");
124-
user.password = providedBootstrapPassword.orElseGet(() -> getLaunchMode().isDevOrTest() ? "secret" : generateRandomPassword(RANDOM_PASSWORD_DEFAULT_LENGTH));
123+
user.user = new UserService.UserData("", BOOTSTRAP_ACCOUNT, "Bootstrap", "Account", "[email protected]");
124+
user.password = providedBootstrapPassword.orElseGet(() -> LaunchMode.current().isDevOrTest() ? "secret" : generateRandomPassword(RANDOM_PASSWORD_DEFAULT_LENGTH));
125125

126-
// create bootstrap acconut with admin role
126+
// create bootstrap account with admin role
127127
backend.get().createUser(user);
128128
backend.get().setPassword(BOOTSTRAP_ACCOUNT, user.password); // KeycloakUserBackend.createUser() creates a temp password, with this call the password is usable
129129
backend.get().updateAdministrators(List.of(BOOTSTRAP_ACCOUNT));
@@ -132,6 +132,10 @@ public void checkBootstrapAccount() {
132132
backend.get().addTeam("dev-team");
133133
backend.get().updateTeamMembers("dev-team", Map.of(BOOTSTRAP_ACCOUNT, List.of(Roles.MANAGER, Roles.TESTER, Roles.UPLOADER, Roles.VIEWER)));
134134

135+
// create db entry, if not existent, like in UserService.createLocalUser()
136+
UserInfo userInfo = UserInfo.<UserInfo>findByIdOptional(BOOTSTRAP_ACCOUNT).orElse(new UserInfo(BOOTSTRAP_ACCOUNT));
137+
userInfo.defaultTeam = "dev-team";
138+
135139
Log.infov("\n>>>\n>>> Created temporary account {0} with password {1}\n>>>", BOOTSTRAP_ACCOUNT, user.password);
136140
} else if (administrators.size() > 1 && administrators.contains(BOOTSTRAP_ACCOUNT)) {
137141
Log.warnv("The temporary account {0} can be removed", BOOTSTRAP_ACCOUNT);
@@ -140,7 +144,7 @@ public void checkBootstrapAccount() {
140144

141145
public static String generateRandomPassword(int lenght) {
142146
StringBuilder builder = new StringBuilder(lenght);
143-
new SecureRandom().ints(lenght, 0, RANDOM_PASSWRORD_CHARS.length).mapToObj(i -> RANDOM_PASSWRORD_CHARS[i]).forEach(builder::append);
147+
new SecureRandom().ints(lenght, 0, RANDOM_PASSWORD_CHARS.length).mapToObj(i -> RANDOM_PASSWORD_CHARS[i]).forEach(builder::append);
144148
return builder.toString();
145149
}
146150

horreum-backend/src/main/resources/application.properties

+1-3
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ quarkus.oidc.client-id=horreum
100100

101101
## Do not un comment the line below, there appears to be a bug in Quarkus 3.4.1 where dev services do not overwrite the config property
102102
quarkus.oidc.credentials.secret=overridden-in-file-dot-env
103+
%dev.quarkus.oidc.credentials.secret=**********
103104

104105
# This option lets HorreumAuthorizationFilter transform app keys sent as tokens
105106
quarkus.http.auth.proactive=false
@@ -113,9 +114,6 @@ quarkus.keycloak.admin-client.client-id=horreum
113114
quarkus.keycloak.admin-client.client-secret=${quarkus.oidc.credentials.secret}
114115
quarkus.keycloak.admin-client.realm=${horreum.keycloak.realm}
115116
quarkus.keycloak.admin-client.grant-type=CLIENT_CREDENTIALS
116-
%dev.quarkus.keycloak.admin-client.client-id=admin-cli
117-
%dev.quarkus.keycloak.admin-client.realm=master
118-
%dev.quarkus.keycloak.admin-client.grant-type=PASSWORD
119117

120118
# Secret used to sign database rows
121119
horreum.db.secret=secret

0 commit comments

Comments
 (0)