{"record":{"id":"8b26c27becc180ee","repo":"perwendel/spark","slug":"must-provide-a-keystore-file-to-run-secured","errorCode":null,"errorMessage":"Must provide a keystore file to run secured","messagePattern":"Must provide a keystore file to run secured","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/spark/Service.java","lineNumber":293,"sourceCode":"     * @param truststoreFile     the truststore file location as string, leave null to reuse\n     *                           keystore\n     * @param needsClientCert    Whether to require client certificate to be supplied in\n     *                           request\n     * @param truststorePassword the trust store password\n     * @return the object with connection set to be secure\n     */\n    public synchronized Service secure(String keystoreFile,\n                                       String keystorePassword,\n                                       String certAlias,\n                                       String truststoreFile,\n                                       String truststorePassword,\n                                       boolean needsClientCert) {\n        if (initialized) {\n            throwBeforeRouteMappingException();\n        }\n\n        if (keystoreFile == null) {\n            throw new IllegalArgumentException(\n                    \"Must provide a keystore file to run secured\");\n        }\n\n        sslStores = SslStores.create(keystoreFile, keystorePassword, certAlias, truststoreFile, truststorePassword, needsClientCert);\n        return this;\n    }\n\n    /**\n     * Configures the embedded web server's thread pool.\n     *\n     * @param maxThreads max nbr of threads.\n     * @return the object with the embedded web server's thread pool configured\n     */\n    public synchronized Service threadPool(int maxThreads) {\n        return threadPool(maxThreads, -1, -1);\n    }\n\n    /**","sourceCodeStart":275,"sourceCodeEnd":311,"githubUrl":"https://github.com/perwendel/spark/blob/1973e402f5d4c1442ad34a1d38ed0758079f7773/src/main/java/spark/Service.java#L275-L311","documentation":"Service.secure(...) switches the embedded server to HTTPS. A keystore file holding the server certificate/key is mandatory for TLS; without it Spark cannot build its SslStores. Spark throws IllegalArgumentException when keystoreFile is null, before any route mapping exception check would even apply.","triggerScenarios":"Calling secure(null, ...) — i.e. passing a null keystore path — or a variable holding the keystore location that was never populated (config not loaded, property missing, env var empty).","commonSituations":"Keystore path read from application.properties/yml that is missing; conditional config where only truststore was set; forgetting the keystore argument when upgrading from an older secure() overload.","solutions":["Pass a valid, existing keystore file path as the first argument to secure(), e.g. secure(\"certs/keystore.jks\", \"password\", null, null, null).","Load the keystore path from config and fail early with a clear message if it is missing before calling secure().","Verify the config/env providing the keystore location is actually loaded at startup."],"exampleFix":"// before\nString keystore = System.getProperty(\"keystore\"); // null\nSpark.secure(keystore, \"pass\", null, null);\n// after\nString keystore = Objects.requireNonNull(System.getProperty(\"keystore\"), \"keystore path required\");\nSpark.secure(keystore, \"pass\", null, null);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(keystoreFile, \"Must provide a keystore file to run secured\");\nif (!java.nio.file.Files.exists(java.nio.file.Paths.get(keystoreFile))) {\n    throw new IllegalArgumentException(\"Keystore not found: \" + keystoreFile);\n}\nSpark.secure(keystoreFile, password, null, null);","typeGuard":"boolean isConfiguredSsl(String keystoreFile) { return keystoreFile != null && !keystoreFile.isEmpty(); }","tryCatchPattern":"try {\n    Spark.secure(keystoreFile, password, null, null);\n} catch (IllegalArgumentException e) {\n    LOG.error(\"SSL misconfiguration: {}\", e.getMessage());\n    throw new IllegalStateException(\"Aborting startup: keystore missing\", e);\n}","preventionTips":["Fail fast at config-load time if the keystore path property is missing.","Validate keystore file existence before calling secure().","Keep SSL config in one well-tested bootstrap method."],"tags":["java","ssl","config"],"backgroundTag":"missing-required-config-field","analyzedSha":"1973e402f5d4c1442ad34a1d38ed0758079f7773","analyzedAt":"2026-09-10T14:38:22.866Z","contentChangedAt":"2026-09-10T14:38:22.866Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}