{"record":{"id":"873afb4fdbfe98af","repo":"testcontainers/testcontainers-java","slug":"service-token-cannot-have-leading-or-trailing-whitespace","errorCode":null,"errorMessage":"Service token cannot have leading or trailing whitespace","messagePattern":"Service token cannot have leading or trailing whitespace","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/KibanaContainer.java","lineNumber":240,"sourceCode":"     *\n     * @param token the service account token\n     * @return this container instance\n     * @throws IllegalStateException if username/password credentials are already configured\n     * @throws IllegalArgumentException if token is blank\n     */\n    public KibanaContainer withElasticsearchServiceAccountToken(String token) {\n        if (elasticsearchUsername != null) {\n            throw new IllegalStateException(\n                \"Conflicting Elasticsearch credentials: provide either a service account token \" +\n                \"or a username/password pair, not both.\"\n            );\n        }\n        if (StringUtils.isBlank(token)) {\n            throw new IllegalArgumentException(\"Service account token cannot be empty\");\n        }\n\n        if (!token.equals(token.trim())) {\n            throw new IllegalArgumentException(\"Service token cannot have leading or trailing whitespace\");\n        }\n        this.elasticsearchServiceAccountToken = token;\n        return this;\n    }\n\n    /**\n     * Configures the Elasticsearch CA certificate for HTTPS connections.\n     *\n     * @param caCertificate the CA certificate in PEM format\n     * @return this container instance\n     * @throws IllegalArgumentException if certificate is empty\n     */\n    public KibanaContainer withElasticsearchCaCertificate(byte[] caCertificate) {\n        if (caCertificate == null || caCertificate.length == 0) {\n            throw new IllegalArgumentException(\"Elasticsearch CA certificate cannot be empty\");\n        }\n        this.elasticsearchCaCertificate = caCertificate;\n        return this;","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/KibanaContainer.java#L222-L258","documentation":"Thrown by withElasticsearchServiceAccountToken when the token differs from its trimmed value, i.e. it has leading or trailing whitespace. Such whitespace would corrupt the base64 credential string sent to Elasticsearch and cause authentication failures, so the library rejects it up front.","triggerScenarios":"Passing a token read from a file/CLI argument that includes a trailing newline or stray spaces, e.g. token = new String(Files.readAllBytes(path)).","commonSituations":"Reading a token from a text file without trimming the newline; copying a token from a terminal/pager with trailing spaces; shell command substitution preserving newlines.","solutions":["Trim the token before passing it: withElasticsearchServiceAccountToken(token.trim()).","Read files with a newline-stripping helper (e.g. stripping trailing \\n/\\r\\n).","Fix whatever produces the token so it is emitted without surrounding whitespace."],"exampleFix":"// before\ncontainer.withElasticsearchServiceAccountToken(token);\n// after\ncontainer.withElasticsearchServiceAccountToken(token.trim());","handlingStrategy":"validation","validationCode":"if (!token.equals(token.trim())) token = token.trim();\ncontainer.withElasticsearchServiceAccountToken(token);","typeGuard":"String normalizeToken(String t) { return t == null ? null : t.trim(); }","tryCatchPattern":"try { container.withElasticsearchServiceAccountToken(token); } catch (IllegalArgumentException e) { container.withElasticsearchServiceAccountToken(token.trim()); }","preventionTips":["Always .trim() credentials read from files or CLI input","Read file-based tokens with a helper that strips trailing \\n / \\r\\n","Never interpolate tokens inside whitespace-sensitive shell strings"],"tags":["validation","elasticsearch","testcontainers","whitespace"],"backgroundTag":"invalid-argument-format","analyzedSha":"8e549514e3f01c57d70546fbb8599d138f3903e5","analyzedAt":"2026-09-12T14:56:41.227Z","contentChangedAt":"2026-09-12T14:56:41.227Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}