Production must not ship test users, demo orders, or test1234. Dev and CI still need seeded users for e2e. Prod creates one admin from deploy secrets. - Move V2/V4/V6 seed migrations to db/dev-migration - Add application-prod.yml with schema-only Flyway and ignore-missing for moved seeds - Add AdminBootstrap to create admin from ADMIN_EMAIL and ADMIN_PASSWORD - Wire docker,prod profile, deploy secrets, and localhost:5433 for SSH DB access - Add hashPassword Gradle task for optional manual bcrypt generation
50 lines
1.7 KiB
Java
50 lines
1.7 KiB
Java
package se.bilhalsning.config;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.boot.ApplicationArguments;
|
|
import org.springframework.boot.ApplicationRunner;
|
|
import org.springframework.context.annotation.Profile;
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.util.StringUtils;
|
|
import se.bilhalsning.entity.User;
|
|
import se.bilhalsning.repository.UserRepository;
|
|
|
|
@Component
|
|
@Profile("prod")
|
|
@RequiredArgsConstructor
|
|
@Slf4j
|
|
public class AdminBootstrap implements ApplicationRunner {
|
|
|
|
private final UserRepository userRepository;
|
|
private final PasswordEncoder passwordEncoder;
|
|
|
|
@Value("${app.admin.email:}")
|
|
private String adminEmail;
|
|
|
|
@Value("${app.admin.password:}")
|
|
private String adminPassword;
|
|
|
|
@Override
|
|
public void run(ApplicationArguments args) {
|
|
if (userRepository.existsByRole("admin")) {
|
|
log.info("Admin account already present, skipping bootstrap");
|
|
return;
|
|
}
|
|
|
|
if (!StringUtils.hasText(adminEmail) || !StringUtils.hasText(adminPassword)) {
|
|
throw new IllegalStateException(
|
|
"Production requires ADMIN_EMAIL and ADMIN_PASSWORD when no admin user exists");
|
|
}
|
|
|
|
User admin = new User();
|
|
admin.setEmail(adminEmail.trim());
|
|
admin.setPasswordHash(passwordEncoder.encode(adminPassword));
|
|
admin.setRole("admin");
|
|
userRepository.save(admin);
|
|
|
|
log.info("Created production admin account for {}", admin.getEmail());
|
|
}
|
|
}
|