github-actions[bot] commented on code in PR #4504: URL: https://github.com/apache/iggy/pull/4504#discussion_r4256749552
########## foreign/java/java-sdk/src/main/java/org/apache/iggy/builder/TcpConnectionString.java: ########## @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iggy.builder; + +import org.apache.iggy.exception.IggyInvalidArgumentException; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * A parsed {@code iggy://} connection string for the TCP transport. + * + * <p>Follows {@code parseConnectionString} in the Node SDK, limited to what the Java + * TCP builders support: {@code username:password} credentials and the {@code tls} option. + * Error messages never include the connection string, because it contains the password. + */ +record TcpConnectionString(String host, int port, String username, String password, boolean tls) { + + private static final String DEFAULT_SCHEME = "iggy"; + private static final String SCHEME_PREFIX = "iggy+"; + private static final String TCP_TRANSPORT = "tcp"; + private static final int MAX_PORT = 65535; + + // Matches both "[ipv6]:port" and "host:port", where an unbracketed host may not + // contain a colon. Multi-colon authorities such as "2001:db8::1:8090" are rejected. + private static final Pattern ADDRESS = Pattern.compile("^(?:\\[([^\\]]+)\\]|([^:\\[\\]]+)):(\\d{1,5})$"); + + static TcpConnectionString parse(String connectionString) { + if (connectionString == null || connectionString.isEmpty()) { + throw new IggyInvalidArgumentException("Connection string must not be empty"); + } + + String[] schemeAndRest = connectionString.split("://", -1); + if (schemeAndRest.length != 2) { + throw invalid("expected <scheme>://<username>:<password>@<host>:<port>"); + } + parseScheme(schemeAndRest[0]); + + String[] credentialsAndServer = schemeAndRest[1].split("@", -1); + if (credentialsAndServer.length != 2) { + throw invalid("expected exactly one '@' between the credentials and the server address"); + } + String[] credentials = parseCredentials(credentialsAndServer[0]); + + String[] addressAndOptions = credentialsAndServer[1].split("\\?", -1); + if (addressAndOptions.length > 2) { + throw invalid("expected at most one '?' before the options"); + } + Matcher address = parseAddress(addressAndOptions[0]); + String host = address.group(1) != null ? address.group(1) : address.group(2); + int port = Integer.parseInt(address.group(3)); + + boolean tls = addressAndOptions.length == 2 && parseTlsOption(addressAndOptions[1]); + return new TcpConnectionString(host, port, credentials[0], credentials[1], tls); + } + + private static String[] parseCredentials(String credentials) { + String[] usernameAndPassword = credentials.split(":", -1); + if (usernameAndPassword.length == 1) { + throw invalid("personal access tokens are not supported, use <username>:<password>"); + } + if (usernameAndPassword.length != 2 || usernameAndPassword[0].isEmpty() || usernameAndPassword[1].isEmpty()) { + throw invalid("credentials must be <username>:<password>"); + } + return usernameAndPassword; + } + + private static Matcher parseAddress(String serverAddress) { + Matcher address = ADDRESS.matcher(serverAddress); + if (!address.matches()) { + throw invalid("server address must be <host>:<port> or [<ipv6>]:<port>"); + } + if (Integer.parseInt(address.group(3)) > MAX_PORT) { + throw invalid("port must be at most " + MAX_PORT); + } + return address; + } + + private static void parseScheme(String scheme) { + if (DEFAULT_SCHEME.equals(scheme)) { + return; + } + if (!scheme.startsWith(SCHEME_PREFIX)) { + throw invalid("scheme must be iggy:// or iggy+tcp://"); + } + String transport = scheme.substring(SCHEME_PREFIX.length()); + if (!TCP_TRANSPORT.equals(transport)) { + throw new IggyInvalidArgumentException( + "Unsupported transport '" + transport + "' in connection string, only tcp is supported"); Review Comment: critical: `parseScheme()` copies everything after `iggy+` into the exception message, so a string like `iggy+user:secret@host:8090?x=a://b` prints the password. Echo the transport only when every character is an ASCII letter, as the C# parser at `foreign/csharp/Iggy_SDK/Configuration/ConnectionString.cs` does. ########## foreign/java/java-sdk/src/main/java/org/apache/iggy/builder/TcpConnectionString.java: ########## @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iggy.builder; + +import org.apache.iggy.exception.IggyInvalidArgumentException; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * A parsed {@code iggy://} connection string for the TCP transport. + * + * <p>Follows {@code parseConnectionString} in the Node SDK, limited to what the Java + * TCP builders support: {@code username:password} credentials and the {@code tls} option. + * Error messages never include the connection string, because it contains the password. + */ +record TcpConnectionString(String host, int port, String username, String password, boolean tls) { + + private static final String DEFAULT_SCHEME = "iggy"; + private static final String SCHEME_PREFIX = "iggy+"; + private static final String TCP_TRANSPORT = "tcp"; + private static final int MAX_PORT = 65535; + + // Matches both "[ipv6]:port" and "host:port", where an unbracketed host may not + // contain a colon. Multi-colon authorities such as "2001:db8::1:8090" are rejected. + private static final Pattern ADDRESS = Pattern.compile("^(?:\\[([^\\]]+)\\]|([^:\\[\\]]+)):(\\d{1,5})$"); + + static TcpConnectionString parse(String connectionString) { + if (connectionString == null || connectionString.isEmpty()) { + throw new IggyInvalidArgumentException("Connection string must not be empty"); + } + + String[] schemeAndRest = connectionString.split("://", -1); + if (schemeAndRest.length != 2) { + throw invalid("expected <scheme>://<username>:<password>@<host>:<port>"); + } + parseScheme(schemeAndRest[0]); + + String[] credentialsAndServer = schemeAndRest[1].split("@", -1); + if (credentialsAndServer.length != 2) { + throw invalid("expected exactly one '@' between the credentials and the server address"); + } + String[] credentials = parseCredentials(credentialsAndServer[0]); + + String[] addressAndOptions = credentialsAndServer[1].split("\\?", -1); + if (addressAndOptions.length > 2) { + throw invalid("expected at most one '?' before the options"); + } + Matcher address = parseAddress(addressAndOptions[0]); + String host = address.group(1) != null ? address.group(1) : address.group(2); + int port = Integer.parseInt(address.group(3)); + + boolean tls = addressAndOptions.length == 2 && parseTlsOption(addressAndOptions[1]); + return new TcpConnectionString(host, port, credentials[0], credentials[1], tls); + } + + private static String[] parseCredentials(String credentials) { + String[] usernameAndPassword = credentials.split(":", -1); + if (usernameAndPassword.length == 1) { + throw invalid("personal access tokens are not supported, use <username>:<password>"); Review Comment: warning: The parser rejects a token before the `@`, so `iggy://iggypat-123@localhost:8090` works in Rust, Node and C# but fails here. The Java client can log in with a token, so give `IggyTcpClientBuilder` a token setter, or state the Java-only limitation in `foreign/java/README.md`. ########## foreign/java/java-sdk/src/main/java/org/apache/iggy/builder/TcpConnectionString.java: ########## @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iggy.builder; + +import org.apache.iggy.exception.IggyInvalidArgumentException; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * A parsed {@code iggy://} connection string for the TCP transport. + * + * <p>Follows {@code parseConnectionString} in the Node SDK, limited to what the Java + * TCP builders support: {@code username:password} credentials and the {@code tls} option. + * Error messages never include the connection string, because it contains the password. + */ +record TcpConnectionString(String host, int port, String username, String password, boolean tls) { + + private static final String DEFAULT_SCHEME = "iggy"; + private static final String SCHEME_PREFIX = "iggy+"; + private static final String TCP_TRANSPORT = "tcp"; + private static final int MAX_PORT = 65535; + + // Matches both "[ipv6]:port" and "host:port", where an unbracketed host may not + // contain a colon. Multi-colon authorities such as "2001:db8::1:8090" are rejected. + private static final Pattern ADDRESS = Pattern.compile("^(?:\\[([^\\]]+)\\]|([^:\\[\\]]+)):(\\d{1,5})$"); Review Comment: nit: The host pattern allows spaces and control characters, so `iggy://iggy:iggy@local host:8090` parses here and fails later in name resolution. Exclude whitespace and control characters in both host alternatives, as the C# parser does. ########## foreign/java/java-sdk/src/main/java/org/apache/iggy/builder/TcpConnectionString.java: ########## @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iggy.builder; + +import org.apache.iggy.exception.IggyInvalidArgumentException; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * A parsed {@code iggy://} connection string for the TCP transport. + * + * <p>Follows {@code parseConnectionString} in the Node SDK, limited to what the Java + * TCP builders support: {@code username:password} credentials and the {@code tls} option. + * Error messages never include the connection string, because it contains the password. + */ +record TcpConnectionString(String host, int port, String username, String password, boolean tls) { + + private static final String DEFAULT_SCHEME = "iggy"; + private static final String SCHEME_PREFIX = "iggy+"; + private static final String TCP_TRANSPORT = "tcp"; + private static final int MAX_PORT = 65535; + + // Matches both "[ipv6]:port" and "host:port", where an unbracketed host may not + // contain a colon. Multi-colon authorities such as "2001:db8::1:8090" are rejected. + private static final Pattern ADDRESS = Pattern.compile("^(?:\\[([^\\]]+)\\]|([^:\\[\\]]+)):(\\d{1,5})$"); + + static TcpConnectionString parse(String connectionString) { + if (connectionString == null || connectionString.isEmpty()) { + throw new IggyInvalidArgumentException("Connection string must not be empty"); + } + + String[] schemeAndRest = connectionString.split("://", -1); + if (schemeAndRest.length != 2) { + throw invalid("expected <scheme>://<username>:<password>@<host>:<port>"); + } + parseScheme(schemeAndRest[0]); + + String[] credentialsAndServer = schemeAndRest[1].split("@", -1); + if (credentialsAndServer.length != 2) { + throw invalid("expected exactly one '@' between the credentials and the server address"); + } + String[] credentials = parseCredentials(credentialsAndServer[0]); + + String[] addressAndOptions = credentialsAndServer[1].split("\\?", -1); + if (addressAndOptions.length > 2) { + throw invalid("expected at most one '?' before the options"); + } + Matcher address = parseAddress(addressAndOptions[0]); + String host = address.group(1) != null ? address.group(1) : address.group(2); + int port = Integer.parseInt(address.group(3)); + + boolean tls = addressAndOptions.length == 2 && parseTlsOption(addressAndOptions[1]); + return new TcpConnectionString(host, port, credentials[0], credentials[1], tls); + } + + private static String[] parseCredentials(String credentials) { + String[] usernameAndPassword = credentials.split(":", -1); + if (usernameAndPassword.length == 1) { + throw invalid("personal access tokens are not supported, use <username>:<password>"); + } + if (usernameAndPassword.length != 2 || usernameAndPassword[0].isEmpty() || usernameAndPassword[1].isEmpty()) { + throw invalid("credentials must be <username>:<password>"); + } + return usernameAndPassword; + } + + private static Matcher parseAddress(String serverAddress) { + Matcher address = ADDRESS.matcher(serverAddress); + if (!address.matches()) { + throw invalid("server address must be <host>:<port> or [<ipv6>]:<port>"); + } + if (Integer.parseInt(address.group(3)) > MAX_PORT) { Review Comment: simplification: `parseAddress()` parses the port for the bound check and returns the `Matcher`, so `parse()` parses the same digits again at line 68. Return the parsed host and port from `parseAddress()`, or move the bound check next to the single parse in `parse()`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
