75.4 Ловушка почты: catch-all только для адресов без своего ящика, файл с паролями
- MailRouter: существующий ящик получает свою почту, всё остальное (и чужие домены) — в ящик MAIL_CATCH_ALL; раньше ловушка забирала и письма между ящиками стенда - DataInitializer: ящик-ловушка создаётся при старте; случайные пароли — в MAIL_PASSWORDS_FILE (права 600, в образе /app/data/initial-passwords.txt) - docker-compose.jambotron.yml: MAIL_CATCH_ALL=trap@mail.test по умолчанию - README: ловушка, файл паролей, что нужно для staging Jambotron (TCP-проброс SMTP) - тесты: MailRouterTest, DataInitializerTest (33 всего) α Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,12 +9,27 @@ import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.nio.file.attribute.PosixFilePermissions;
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Instant;
|
||||
import java.util.Base64;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* First mailbox on an empty database: {@code mailserver.admin.email} (default admin@<first domain>) with
|
||||
* {@code mailserver.admin.password}; no password given — a random one, printed to the log once.
|
||||
* Mailboxes the server needs on start:
|
||||
* <ul>
|
||||
* <li>the first mailbox on an empty database — {@code mailserver.admin.email} (default admin@<first domain>)
|
||||
* with {@code mailserver.admin.password};</li>
|
||||
* <li>the catch-all mailbox ({@code mailserver.catch-all}) if it does not exist yet — mail to unknown addresses is
|
||||
* stored there, so it must exist.</li>
|
||||
* </ul>
|
||||
* A generated password is appended to {@code mailserver.passwords-file} (mode 600, in the data volume) — read it once
|
||||
* and change the password. Without the file setting it is printed to the log once.
|
||||
*/
|
||||
@Configuration
|
||||
public class DataInitializer {
|
||||
@@ -24,23 +39,52 @@ public class DataInitializer {
|
||||
@Bean
|
||||
public CommandLineRunner initDatabase(UserService userService, UserRepository userRepository, MailDomains domains,
|
||||
@Value("${mailserver.admin.email:}") String email,
|
||||
@Value("${mailserver.admin.password:}") String password) {
|
||||
@Value("${mailserver.admin.password:}") String password,
|
||||
@Value("${mailserver.passwords-file:}") String passwordsFile) {
|
||||
return args -> {
|
||||
if (userRepository.count().block() > 0) {
|
||||
return;
|
||||
Path file = passwordsFile.isBlank() ? null : Path.of(passwordsFile);
|
||||
if (userRepository.count().block() == 0) {
|
||||
String address = email.isBlank() ? "admin@" + domains.domains().iterator().next() : email.trim().toLowerCase(Locale.ROOT);
|
||||
create(userService, address, password, "Administrator", file);
|
||||
}
|
||||
String address = email.isBlank() ? "admin@" + domains.domains().iterator().next() : email.trim().toLowerCase();
|
||||
boolean generated = password.isBlank();
|
||||
String secret = generated ? randomPassword() : password;
|
||||
userService.register(address, secret, "Administrator").block();
|
||||
if (generated) {
|
||||
log.warn("First mailbox created: {} — password: {} (shown once, change it)", address, secret);
|
||||
} else {
|
||||
log.info("First mailbox created: {}", address);
|
||||
String catchAll = domains.catchAll();
|
||||
if (catchAll != null && !Boolean.TRUE.equals(userRepository.existsByEmail(catchAll).block())) {
|
||||
create(userService, catchAll, "", "Mail trap", file);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static void create(UserService userService, String address, String password, String name, Path file)
|
||||
throws IOException {
|
||||
boolean generated = password.isBlank();
|
||||
String secret = generated ? randomPassword() : password;
|
||||
userService.register(address, secret, name).block();
|
||||
if (!generated) {
|
||||
log.info("Mailbox created: {}", address);
|
||||
} else if (file != null) {
|
||||
appendPassword(file, address, secret);
|
||||
log.warn("Mailbox created: {} — password in {} (change it)", address, file);
|
||||
} else {
|
||||
log.warn("Mailbox created: {} — password: {} (shown once, change it)", address, secret);
|
||||
}
|
||||
}
|
||||
|
||||
static void appendPassword(Path file, String address, String password) throws IOException {
|
||||
if (file.getParent() != null) {
|
||||
Files.createDirectories(file.getParent());
|
||||
}
|
||||
if (!Files.exists(file)) {
|
||||
Files.createFile(file);
|
||||
try {
|
||||
Files.setPosixFilePermissions(file, PosixFilePermissions.fromString("rw-------"));
|
||||
} catch (UnsupportedOperationException ignored) {
|
||||
// not a POSIX file system (Windows) — nothing to restrict
|
||||
}
|
||||
}
|
||||
Files.writeString(file, Instant.now() + " " + address + " " + password + System.lineSeparator(),
|
||||
StandardCharsets.UTF_8, StandardOpenOption.APPEND);
|
||||
}
|
||||
|
||||
private static String randomPassword() {
|
||||
byte[] bytes = new byte[18];
|
||||
new SecureRandom().nextBytes(bytes);
|
||||
|
||||
Reference in New Issue
Block a user