{"record":{"id":"361a37323d1b3eb6","repo":"elastic/elasticsearch","slug":"cloudid-does-not-contain-a-valid-port-number","errorCode":null,"errorMessage":"cloudId {} does not contain a valid port number","messagePattern":"cloudId (.+?) does not contain a valid port number","errorType":"validation","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"client/rest/src/main/java/org/elasticsearch/client/RestClient.java","lineNumber":185,"sourceCode":"\n        String decoded = new String(Base64.getDecoder().decode(cloudId), UTF_8);\n        // once decoded the parts are separated by a $ character.\n        // they are respectively domain name and optional port, elasticsearch id, kibana id\n        String[] decodedParts = decoded.split(\"\\\\$\");\n        if (decodedParts.length != 3) {\n            throw new IllegalStateException(\"cloudId \" + cloudId + \" did not decode to a cluster identifier correctly\");\n        }\n\n        // domain name and optional port\n        String[] domainAndMaybePort = decodedParts[0].split(\":\", 2);\n        String domain = domainAndMaybePort[0];\n        int port;\n\n        if (domainAndMaybePort.length == 2) {\n            try {\n                port = Integer.parseInt(domainAndMaybePort[1]);\n            } catch (NumberFormatException nfe) {\n                throw new IllegalStateException(\"cloudId \" + cloudId + \" does not contain a valid port number\");\n            }\n        } else {\n            port = 443;\n        }\n\n        String url = decodedParts[1] + \".\" + domain;\n        return builder(new HttpHost(url, port, \"https\"));\n    }\n\n    /**\n     * Returns a new {@link RestClientBuilder} to help with {@link RestClient} creation.\n     * Creates a new builder instance and sets the hosts that the client will send requests to.\n     * <p>\n     * Prefer this to {@link #builder(HttpHost...)} if you have metadata up front about the nodes.\n     * If you don't either one is fine.\n     */\n    public static RestClientBuilder builder(Node... nodes) {\n        return new RestClientBuilder(nodes == null ? null : Arrays.asList(nodes));","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/client/rest/src/main/java/org/elasticsearch/client/RestClient.java#L167-L203","documentation":"When the decoded cloudId's domain segment contains a ':port' suffix, the port substring must parse as an integer. If Integer.parseInt throws NumberFormatException, RestClient wraps it with this IllegalStateException naming the offending cloudId, since a non-numeric port makes the target HttpHost unconstructable.","triggerScenarios":"A cloudId whose first '$'-segment is 'host:abc' (non-numeric port); a deployment with a malformed port field; a hand-edited cloudId that corrupted the port.","commonSituations":"Custom/edited cloudId; a deployment export bug producing a non-numeric port; locale-specific decimal separators accidentally injected.","solutions":["Re-copy the cloudId from Elastic Cloud — the port is managed by the platform and should always be numeric.","If you must construct the host manually, bypass cloudId and use builder(new HttpHost(host, port, \"https\")).","Validate the port segment parses before calling builder if you synthesize cloudIds programmatically."],"exampleFix":"// before\nRestClient.builder(badCloudIdWithNonNumericPort);\n// after\nRestClient.builder(new HttpHost(\"xyz.found.io\", 9243, \"https\")); // or re-copy the cloudId","handlingStrategy":"validation","validationCode":"// If you synthesize cloudIds, validate the port segment parses before calling builder:\nString decoded = new String(Base64.getDecoder().decode(payload));\nString[] parts = decoded.split(\"\\\\$\");\nif (parts[0].contains(\":\")) Integer.parseInt(parts[0].split(\":\",2)[1]); // throws early with context","typeGuard":null,"tryCatchPattern":"try { RestClient.builder(cloudId); } catch (IllegalStateException e) { /* fall back to builder(new HttpHost(host, port, \"https\")) */ }","preventionTips":["Prefer the platform-provided cloudId; its port is always numeric.","If you need a custom host/port, use builder(HttpHost) directly.","Validate synthesized cloudIds before constructing the client."],"tags":["rest-client","cloud-id","config","validation"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}