|
| 1 | +package devkor.ontime_back.config; |
| 2 | + |
| 3 | +import jakarta.annotation.PostConstruct; |
| 4 | +import lombok.RequiredArgsConstructor; |
| 5 | +import org.springframework.context.annotation.Profile; |
| 6 | +import org.springframework.core.env.Environment; |
| 7 | +import org.springframework.stereotype.Component; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Locale; |
| 12 | + |
| 13 | +@Component |
| 14 | +@Profile("prod") |
| 15 | +@RequiredArgsConstructor |
| 16 | +public class ProductionSecretValidator { |
| 17 | + |
| 18 | + private final Environment environment; |
| 19 | + |
| 20 | + @PostConstruct |
| 21 | + public void validate() { |
| 22 | + List<String> errors = new ArrayList<>(); |
| 23 | + |
| 24 | + requireSecret(errors, "spring.datasource.url"); |
| 25 | + requireSecret(errors, "spring.datasource.username"); |
| 26 | + requireSecret(errors, "spring.datasource.password"); |
| 27 | + requireSecret(errors, "jwt.secret.key"); |
| 28 | + requireSecret(errors, "google.web.client-id"); |
| 29 | + requireSecret(errors, "google.app.client-id"); |
| 30 | + requireSecret(errors, "spring.security.oauth2.client.registration.google.client-secret"); |
| 31 | + requireSecret(errors, "apple.client.id"); |
| 32 | + requireSecret(errors, "apple.team.id"); |
| 33 | + requireSecret(errors, "apple.login.key"); |
| 34 | + requireSecret(errors, "apple.private-key.base64"); |
| 35 | + requireSecret(errors, "firebase.credentials.base64"); |
| 36 | + |
| 37 | + validateDatabase(errors); |
| 38 | + validateJwt(errors); |
| 39 | + rejectLegacySecretSource(errors, "apple.client.secret"); |
| 40 | + rejectLegacySecretSource(errors, "apple.private-key"); |
| 41 | + rejectLegacySecretSource(errors, "firebase.credentials.json"); |
| 42 | + rejectLegacySecretSource(errors, "firebase.credentials.path"); |
| 43 | + rejectLegacySecretSource(errors, "google.application.credentials"); |
| 44 | + |
| 45 | + if (!errors.isEmpty()) { |
| 46 | + throw new IllegalStateException("Unsafe production secret configuration: " + String.join("; ", errors)); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private void validateDatabase(List<String> errors) { |
| 51 | + String username = property("spring.datasource.username"); |
| 52 | + if ("root".equalsIgnoreCase(username)) { |
| 53 | + errors.add("spring.datasource.username must not be root"); |
| 54 | + } |
| 55 | + |
| 56 | + String databaseUrl = property("spring.datasource.url").toLowerCase(Locale.ROOT); |
| 57 | + if (databaseUrl.contains("allowpublickeyretrieval=true")) { |
| 58 | + errors.add("spring.datasource.url must not enable allowPublicKeyRetrieval"); |
| 59 | + } |
| 60 | + if (databaseUrl.contains("createdatabaseifnotexist=true")) { |
| 61 | + errors.add("spring.datasource.url must not create databases at startup"); |
| 62 | + } |
| 63 | + if (databaseUrl.contains("usessl=false")) { |
| 64 | + errors.add("spring.datasource.url must not disable TLS"); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private void validateJwt(List<String> errors) { |
| 69 | + String jwtSecret = property("jwt.secret.key"); |
| 70 | + if (hasText(jwtSecret) && jwtSecret.length() < 64) { |
| 71 | + errors.add("jwt.secret.key must be at least 64 characters"); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private void requireSecret(List<String> errors, String propertyName) { |
| 76 | + String value = property(propertyName); |
| 77 | + if (!hasText(value)) { |
| 78 | + errors.add(propertyName + " is required"); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + if (looksLikePlaceholder(value)) { |
| 83 | + errors.add(propertyName + " must not use placeholder or sample values"); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private void rejectLegacySecretSource(List<String> errors, String propertyName) { |
| 88 | + if (hasText(property(propertyName))) { |
| 89 | + errors.add(propertyName + " is not allowed in prod; use the base64 environment secret"); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private String property(String propertyName) { |
| 94 | + try { |
| 95 | + return environment.getProperty(propertyName, ""); |
| 96 | + } catch (IllegalArgumentException e) { |
| 97 | + return ""; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private boolean hasText(String value) { |
| 102 | + return value != null && !value.isBlank(); |
| 103 | + } |
| 104 | + |
| 105 | + private boolean looksLikePlaceholder(String value) { |
| 106 | + String normalized = value.toLowerCase(Locale.ROOT); |
| 107 | + return normalized.contains("your_") |
| 108 | + || normalized.contains("your-") |
| 109 | + || normalized.contains("change-me") |
| 110 | + || normalized.contains("changeme") |
| 111 | + || normalized.contains("placeholder") |
| 112 | + || normalized.contains("dummy") |
| 113 | + || normalized.contains("fake") |
| 114 | + || normalized.contains("sample") |
| 115 | + || normalized.contains("example") |
| 116 | + || normalized.startsWith("test-") |
| 117 | + || normalized.startsWith("test_") |
| 118 | + || normalized.contains("test_secret") |
| 119 | + || normalized.contains("my_secret_key_for_ontime_back_application_development_environment") |
| 120 | + || normalized.contains("ontime1234"); |
| 121 | + } |
| 122 | +} |
0 commit comments