|
1 | 1 | package org.cloudfoundry.autoscaler.scheduler; |
2 | 2 |
|
| 3 | +import org.bouncycastle.crypto.fips.FipsStatus; |
| 4 | +import org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider; |
3 | 5 | import org.cloudfoundry.autoscaler.scheduler.conf.MetricsConfig; |
4 | 6 | import org.slf4j.Logger; |
5 | 7 | import org.slf4j.LoggerFactory; |
|
22 | 24 | import org.springframework.boot.context.properties.ConfigurationPropertiesScan; |
23 | 25 | import org.springframework.context.event.EventListener; |
24 | 26 |
|
| 27 | +import java.security.Security; |
| 28 | + |
25 | 29 | @ConfigurationPropertiesScan(basePackageClasses = MetricsConfig.class) |
26 | 30 | @SpringBootApplication( |
27 | 31 | exclude = { |
|
41 | 45 | }) |
42 | 46 | public class SchedulerApplication { |
43 | 47 |
|
44 | | - private Logger logger = LoggerFactory.getLogger(this.getClass()); |
| 48 | + private static final Logger logger = LoggerFactory.getLogger(SchedulerApplication.class); |
| 49 | + private static final int FIPS_ERROR_EXIT_CODE = 140; |
45 | 50 |
|
46 | 51 | @EventListener |
47 | 52 | public void onApplicationReady(ApplicationReadyEvent event) { |
48 | 53 | logger.info("Scheduler is ready to start"); |
49 | 54 | } |
50 | 55 |
|
| 56 | + /** |
| 57 | + * Initializes and validates FIPS mode for the application. |
| 58 | + * This is equivalent to checking crypto/fips140.Enabled in Go. |
| 59 | + * Exits with error code 140 if FIPS mode is not enabled or cannot be initialized. |
| 60 | + */ |
| 61 | + private static void initializeAndValidateFipsMode() { |
| 62 | + try { |
| 63 | + logger.info("Initializing FIPS 140-2 compliant cryptographic provider..."); |
| 64 | + |
| 65 | + // Register Bouncy Castle FIPS provider as the primary security provider |
| 66 | + Security.insertProviderAt(new BouncyCastleFipsProvider(), 1); |
| 67 | + |
| 68 | + // Check if Bouncy Castle FIPS is ready and in approved mode (equivalent to crypto/fips140.Enabled) |
| 69 | + if (!FipsStatus.isReady()) { |
| 70 | + logger.error("FIPS mode is not ready. Application requires FIPS 140-3 compliance."); |
| 71 | + System.exit(FIPS_ERROR_EXIT_CODE); |
| 72 | + } |
| 73 | + |
| 74 | + // Verify that BC-FIPS provider is now installed and available |
| 75 | + if (Security.getProvider("BCFIPS") == null) { |
| 76 | + logger.error("Bouncy Castle FIPS provider (BCFIPS) failed to register."); |
| 77 | + System.exit(FIPS_ERROR_EXIT_CODE); |
| 78 | + } |
| 79 | + |
| 80 | + // Configure FIPS-compatible system properties for SSL/TLS |
| 81 | + configureFipsCompatibleSystemProperties(); |
| 82 | + |
| 83 | + logger.info("FIPS mode initialization successful - running in FIPS 140-3 mode"); |
| 84 | + logger.info("Active security provider: {}", Security.getProvider("BCFIPS").getName()); |
| 85 | + logger.info("FIPS Status - Ready: {}", |
| 86 | + FipsStatus.isReady()); |
| 87 | + |
| 88 | + } catch (Exception e) { |
| 89 | + logger.error("Failed to initialize FIPS mode: {}", e.getMessage(), e); |
| 90 | + System.exit(FIPS_ERROR_EXIT_CODE); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Configures system properties for FIPS-compatible SSL/TLS operations. |
| 96 | + * This prevents issues with XDH key exchange algorithms that are not supported by BC-FIPS. |
| 97 | + */ |
| 98 | + private static void configureFipsCompatibleSystemProperties() { |
| 99 | + // Disable XDH algorithms (X25519, X448) that cause issues with BC-FIPS |
| 100 | + System.setProperty("jdk.tls.namedGroups", "secp256r1,secp384r1,secp521r1"); |
| 101 | + System.setProperty("jdk.tls.disabledAlgorithms", |
| 102 | + "SSLv3, RC4, DES, MD5withRSA, DH keySize < 1024, " + |
| 103 | + "EC keySize < 224, 3DES_EDE_CBC, anon, NULL, " + |
| 104 | + "X25519, X448, XDH"); |
| 105 | + |
| 106 | + // Ensure FIPS-compatible cipher suites are preferred |
| 107 | + System.setProperty("jdk.tls.client.cipherSuites", |
| 108 | + "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384," + |
| 109 | + "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256," + |
| 110 | + "TLS_RSA_WITH_AES_256_GCM_SHA384," + |
| 111 | + "TLS_RSA_WITH_AES_128_GCM_SHA256"); |
| 112 | + |
| 113 | + logger.info("Configured FIPS-compatible SSL/TLS system properties"); |
| 114 | + } |
| 115 | + |
51 | 116 | public static void main(String[] args) { |
| 117 | + // Initialize and validate FIPS mode before starting the application (equivalent to crypto/fips140.Enabled check) |
| 118 | + initializeAndValidateFipsMode(); |
| 119 | + |
| 120 | + logger.info("Starting Scheduler application with FIPS 140-2 compliance enforced"); |
52 | 121 | SpringApplication.run(SchedulerApplication.class, args); |
53 | 122 | } |
54 | 123 | } |
0 commit comments