testcontainers/testcontainers-java · error · IllegalArgumentException

Kibana credentials cannot have leading or trailing…

Error message

Kibana credentials cannot have leading or trailing whitespace

What it means

withKibanaUsernameAndPassword rejects usernames or passwords with leading or trailing whitespace. Environment variables and Kibana config files do not strip surrounding whitespace reliably, so such credentials would silently fail authentication; the library fails fast with IllegalArgumentException instead.

Solutions

  1. Trim the values before passing them: username.trim(), password.trim().
  2. Sanitize credentials read from files (strip newlines/BOM/quotes).
  3. Check your secrets source for accidental whitespace (trailing newline is the most common).

Example fix

// before
kibana.withKibanaUsernameAndPassword(username, password);
// after
kibana.withKibanaUsernameAndPassword(username.trim(), password.trim());
Defensive patterns

Strategy: validation

Validate before calling

if (!username.equals(username.trim()) || !password.equals(password.trim())) throw new IllegalArgumentException("Trim credentials before use");

Prevention

When it happens

Trigger: Calling withKibanaUsernameAndPassword where username or password differs from its own trim() — e.g. ' kibana_user' or 'secret\n'.

Common situations: Credentials parsed from files with trailing newline/CRLF; copy-pasted secrets including trailing spaces; YAML/env parsing quirks in CI pipelines.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12). Data as JSON: /api/errors/f6a58be62f073226. Report an issue: GitHub.

Appendix: source

Thrown at modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/KibanaContainer.java:209

     *
     * @param username the Elasticsearch username (cannot be 'elastic')
     * @param password the password
     * @return this container instance
     * @throws IllegalStateException if a service account token is already configured
     * @throws IllegalArgumentException if credentials are invalid
     */
    public KibanaContainer withKibanaUsernameAndPassword(String username, String password) {
        if (elasticsearchServiceAccountToken != null) {
            throw new IllegalStateException(
                "Conflicting Elasticsearch credentials: provide either a service account token " +
                "or a username/password pair, not both."
            );
        }
        if (StringUtils.isAnyBlank(username, password)) {
            throw new IllegalArgumentException("Kibana credentials cannot be blank");
        }
        if (!username.equals(username.trim()) || !password.equals(password.trim())) {
            throw new IllegalArgumentException("Kibana credentials cannot have leading or trailing whitespace");
        }
        if ("elastic".equals(username)) {
            throw new IllegalArgumentException("Username 'elastic' is reserved for internal use by Elasticsearch");
        }

        this.elasticsearchUsername = username;
        this.elasticsearchPassword = password;
        return this;
    }

    /**
     * Configures a service account token for Elasticsearch authentication.
     *
     * @param token the service account token
     * @return this container instance
     * @throws IllegalStateException if username/password credentials are already configured
     * @throws IllegalArgumentException if token is blank
     */

View on GitHub (pinned to 8e549514e3)