- 
                Notifications
    You must be signed in to change notification settings 
- Fork 184
Move HttpClientFactory to common to expose to other components #4175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
11aedb5
              0032106
              3630ebc
              88c4d30
              5f61eff
              d4df6d6
              a513f52
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -3,9 +3,11 @@ | |
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|  | ||
| package org.opensearch.ml.engine.httpclient; | ||
| package org.opensearch.ml.common.httpclient; | ||
|  | ||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertThrows; | ||
|  | ||
| import java.time.Duration; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|  | @@ -33,6 +35,39 @@ public void test_getSdkAsyncHttpClient_success() { | |
| assertNotNull(client); | ||
| } | ||
|  | ||
| @Test | ||
| public void test_invalidIP_localHost_privateIPDisabled() { | ||
| IllegalArgumentException e1 = assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> MLHttpClientFactory.validate(HTTP, "127.0.0.1", 80, PRIVATE_IP_DISABLED) | ||
| ); | ||
| assertEquals("Remote inference host name has private ip address: 127.0.0.1", e1.getMessage()); | ||
|  | ||
| IllegalArgumentException e2 = assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> MLHttpClientFactory.validate(HTTP, "192.168.0.1", 80, PRIVATE_IP_DISABLED) | ||
| ); | ||
| assertEquals("Remote inference host name has private ip address: 192.168.0.1", e2.getMessage()); | ||
|  | ||
| IllegalArgumentException e3 = assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> MLHttpClientFactory.validate(HTTP, "169.254.0.1", 80, PRIVATE_IP_DISABLED) | ||
| ); | ||
| assertEquals("Remote inference host name has private ip address: 169.254.0.1", e3.getMessage()); | ||
|  | ||
| IllegalArgumentException e4 = assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> MLHttpClientFactory.validate(HTTP, "172.16.0.1", 80, PRIVATE_IP_DISABLED) | ||
| ); | ||
| assertEquals("Remote inference host name has private ip address: 172.16.0.1", e4.getMessage()); | ||
|  | ||
| IllegalArgumentException e5 = assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> MLHttpClientFactory.validate(HTTP, "172.31.0.1", 80, PRIVATE_IP_DISABLED) | ||
| ); | ||
| assertEquals("Remote inference host name has private ip address: 172.31.0.1", e5.getMessage()); | ||
| } | ||
|  | ||
| @Test | ||
| public void test_validateIp_validIp_noException() throws Exception { | ||
| MLHttpClientFactory.validate(HTTP, TEST_HOST, 80, PRIVATE_IP_DISABLED); | ||
|  | @@ -43,6 +78,8 @@ public void test_validateIp_validIp_noException() throws Exception { | |
| MLHttpClientFactory.validate(HTTP, "177.0.1.1", 80, PRIVATE_IP_DISABLED); | ||
| MLHttpClientFactory.validate(HTTP, "177.0.0.2", 80, PRIVATE_IP_DISABLED); | ||
| MLHttpClientFactory.validate(HTTP, "::ffff", 80, PRIVATE_IP_DISABLED); | ||
| MLHttpClientFactory.validate(HTTP, "172.32.0.1", 80, PRIVATE_IP_ENABLED); | ||
| MLHttpClientFactory.validate(HTTP, "172.2097152", 80, PRIVATE_IP_ENABLED); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the  | ||
| } | ||
|  | ||
| @Test | ||
|  | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance concern: This method attempts octal and hex parsing for every byte comparison, which will frequently throw NumberFormatException for common IP octets (192, 168, 127, etc.) since they contain invalid octal digits. Consider using simple decimal comparison since IP addresses are standardized as decimal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested, these parsing are indeed not necessary as when
byte[] bytes = ip.getAddress();runs, it already parsed the strange ip address to correct numbers, so those parsing code are not get executed.