Skip to content
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

InetAddressResolver works on jdk21 #574

Merged
merged 8 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ apply plugin: 'com.palantir.baseline'
apply plugin: 'com.palantir.baseline-java-versions'

javaVersions {
libraryTarget = 8
libraryTarget = 11
mswintermeyer marked this conversation as resolved.
Show resolved Hide resolved
runtime = 17
}

Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-574.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: InetAddressResolver works compiled against jdk21
links:
- https://github.com/palantir/docker-proxy-rule/pull/574
19 changes: 19 additions & 0 deletions docker-proxy-rule-core-jdk21/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apply plugin: 'com.palantir.external-publish-jar'

dependencies {
api project(':docker-proxy-rule-core')

testImplementation group: 'junit', name: 'junit'
testImplementation group: 'org.assertj', name: 'assertj-core'
testImplementation group: 'org.mockito', name: 'mockito-core'
testRuntimeOnly group: 'org.mockito', name: 'mockito-inline'
}

javaVersion {
target = 21
runtime = 21
}

moduleJvmArgs {
opens 'java.base/java.net'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved.
*
* Licensed 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 com.palantir.docker.proxy;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.spi.InetAddressResolver;
import java.util.Arrays;
import java.util.function.Supplier;
import java.util.stream.Stream;

public final class DockerProxyInetAddressResolver implements InetAddressResolver {
private final Supplier<DockerNameService> dockerNameService;

public DockerProxyInetAddressResolver(Supplier<DockerNameService> dockerNameService) {
this.dockerNameService = dockerNameService;
}

@Override
public Stream<InetAddress> lookupByName(String host, LookupPolicy _lookupPolicy) throws UnknownHostException {
return Arrays.stream(dockerNameService.get().lookupAllHostAddr(host));
}

@Override
public String lookupByAddress(byte[] addr) throws UnknownHostException {
return dockerNameService.get().getHostByAddr(addr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved.
*
* Licensed 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 com.palantir.docker.proxy;

import java.net.spi.InetAddressResolver;
import java.net.spi.InetAddressResolverProvider;

public final class DockerProxyInetAddressResolverProvider extends InetAddressResolverProvider {

@Override
public InetAddressResolver get(Configuration configuration) {
return new ForwardingInetAddressResolver(
new DockerProxyInetAddressResolver(DockerProxyManager::getDockerNameService),
configuration.builtinResolver(),
() -> DockerProxyManager.getDockerNameService() != null);
}

@Override
public String name() {
return "DockerProxyInetAddressResolverProvider";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved.
*
* Licensed 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 com.palantir.docker.proxy;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.spi.InetAddressResolver;
import java.util.function.Supplier;
import java.util.stream.Stream;

class ForwardingInetAddressResolver implements InetAddressResolver {
private final InetAddressResolver delegate;
private final InetAddressResolver fallback;
private final Supplier<Boolean> delegateEnabled;
mswintermeyer marked this conversation as resolved.
Show resolved Hide resolved

ForwardingInetAddressResolver(
InetAddressResolver delegate, InetAddressResolver fallback, Supplier<Boolean> delegateEnabled) {
this.delegate = delegate;
this.fallback = fallback;
this.delegateEnabled = delegateEnabled;
}

@Override
public Stream<InetAddress> lookupByName(String host, LookupPolicy lookupPolicy) throws UnknownHostException {
if (!delegateEnabled.get()) {
return fallback.lookupByName(host, lookupPolicy);
}

try {
return delegate.lookupByName(host, lookupPolicy);
} catch (UnknownHostException e) {
if (fallback != null) {
return fallback.lookupByName(host, lookupPolicy);
}
throw e;
}
}

@Override
public String lookupByAddress(byte[] addr) throws UnknownHostException {
if (!delegateEnabled.get()) {
return fallback.lookupByAddress(addr);
}

try {
return delegate.lookupByAddress(addr);
} catch (UnknownHostException e) {
if (fallback != null) {
return fallback.lookupByAddress(addr);
}
throw e;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.palantir.docker.proxy.DockerProxyManager$DockerProxyInetAddressResolverProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved.
*
* Licensed 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 com.palantir.docker.proxy;

import static com.palantir.docker.proxy.DockerContainerInfoUtils.IP_FORMAT_STRING;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.palantir.docker.compose.execution.DockerExecutable;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import org.junit.Test;

public class DockerContainerInfoUtilsTest {
private static final String CONTAINER_ID = "container-id";

private final Process response = mock(Process.class);
private final DockerExecutable dockerExecutable = mock(DockerExecutable.class);

@Test
public void getContainerIpFromIdDoesNotThrowWhenContainerIsStopped() throws IOException, InterruptedException {
when(response.getInputStream()).thenReturn(getDockerOutputForStoppedContainer());
when(response.waitFor(anyLong(), any(TimeUnit.class))).thenReturn(true);
when(response.exitValue()).thenReturn(0);
when(dockerExecutable.execute("inspect", "--format", IP_FORMAT_STRING, CONTAINER_ID))
.thenReturn(response);

Optional<String> ip = DockerContainerInfoUtils.getContainerIpFromId(dockerExecutable, CONTAINER_ID);
assertThat(ip).isNotPresent();
}

private static InputStream getDockerOutputForStoppedContainer() {
return new ByteArrayInputStream("\n".getBytes(StandardCharsets.UTF_8));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* (c) Copyright 2017 Palantir Technologies Inc. All rights reserved.
*
* Licensed 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 com.palantir.docker.proxy;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.common.net.InetAddresses;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Optional;
import org.junit.Test;

public class DockerNameServiceTest {
private static final String HOST_NAME = "host";
private static final String HOST_IP = "172.0.2.5";
private static final InetAddress HOST_IP_INET = InetAddresses.forString("172.0.2.5");

private final DockerContainerInfo containerInfo = mock(DockerContainerInfo.class);
private final DockerNameService dockerNameService = new DockerNameService(containerInfo);

@Test
public void shouldReturnIpOfHost() throws UnknownHostException {
when(containerInfo.getIpForHost(HOST_NAME)).thenReturn(Optional.of(HOST_IP));

InetAddress[] hostAddresses = dockerNameService.lookupAllHostAddr(HOST_NAME);

assertThat(hostAddresses).containsExactly(HOST_IP_INET);
}

@Test
public void shouldOnlyQueryTheSupplierOncePerLookupCall() throws UnknownHostException {
when(containerInfo.getIpForHost(HOST_NAME)).thenReturn(Optional.of(HOST_IP));

dockerNameService.lookupAllHostAddr(HOST_NAME);

verify(containerInfo, times(1)).getIpForHost(HOST_NAME);
}

@Test
public void shouldGetIpOfHostFromSupplierEveryTime() throws UnknownHostException {
when(containerInfo.getIpForHost(HOST_NAME)).thenReturn(Optional.of(HOST_IP));

dockerNameService.lookupAllHostAddr(HOST_NAME);
dockerNameService.lookupAllHostAddr(HOST_NAME);

verify(containerInfo, times(2)).getIpForHost(HOST_NAME);
}

@Test(expected = UnknownHostException.class)
public void shouldThrowUnknownHostExceptionWhenNoIpForHost() throws UnknownHostException {
when(containerInfo.getIpForHost(HOST_NAME)).thenReturn(Optional.empty());

dockerNameService.lookupAllHostAddr(HOST_NAME);
}

@Test
public void shouldGetHostFromIp() throws UnknownHostException {
when(containerInfo.getHostForIp(HOST_IP)).thenReturn(Optional.of(HOST_NAME));

String host = dockerNameService.getHostByAddr(HOST_IP_INET.getAddress());

assertThat(host).isEqualTo(HOST_NAME);
}

@Test
public void shouldOnlyQueryTheSupplierOncePerHostByAddrCall() throws UnknownHostException {
when(containerInfo.getHostForIp(HOST_IP)).thenReturn(Optional.of(HOST_NAME));

dockerNameService.getHostByAddr(HOST_IP_INET.getAddress());

verify(containerInfo, times(1)).getHostForIp(HOST_IP);
}

@Test
public void shouldGetHostOfIpFromSupplierEveryTime() throws UnknownHostException {
when(containerInfo.getHostForIp(HOST_IP)).thenReturn(Optional.of(HOST_NAME));

dockerNameService.getHostByAddr(HOST_IP_INET.getAddress());
dockerNameService.getHostByAddr(HOST_IP_INET.getAddress());

verify(containerInfo, times(2)).getHostForIp(HOST_IP);
}

@Test(expected = UnknownHostException.class)
public void shouldThrowUnknownHostExceptionWhenNoHostForIp() throws UnknownHostException {
when(containerInfo.getHostForIp(HOST_IP)).thenReturn(Optional.empty());

dockerNameService.getHostByAddr(HOST_IP_INET.getAddress());
}
}
Loading