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

Inject reverse DNS info when accepting socket connection #540

Draft
wants to merge 5 commits into
base: palantir-cassandra-2.2.18
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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.xml
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@
<dependency groupId="com.github.luben" artifactId="zstd-jni" version="1.5.0-4"/>
<dependency groupId="commons-cli" artifactId="commons-cli" version="1.1"/>
<dependency groupId="commons-codec" artifactId="commons-codec" version="1.2"/>
<dependency groupId="org.apache.commons" artifactId="commons-lang3" version="3.1"/>
<dependency groupId="org.apache.commons" artifactId="commons-lang3" version="3.5"/>
<dependency groupId="org.apache.commons" artifactId="commons-math3" version="3.2"/>
<dependency groupId="com.googlecode.concurrentlinkedhashmap" artifactId="concurrentlinkedhashmap-lru" version="1.4"/>
<dependency groupId="org.antlr" artifactId="antlr" version="3.5.2">
Expand Down
Binary file removed lib/commons-lang3-3.1.jar
Binary file not shown.
Binary file added lib/commons-lang3-3.5.jar
Binary file not shown.
52 changes: 52 additions & 0 deletions src/java/com/palantir/cassandra/net/DefaultDnsClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 com.palantir.cassandra.net;

import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.annotation.Nullable;

public class DefaultDnsClient implements DnsClient
{

public String getLocalHostname()
{
try
{
return InetAddress.getLocalHost().getCanonicalHostName();
}
catch (UnknownHostException e)
{
throw new RuntimeException("Could not resolve local hostname", e);
}
}

@Nullable
public InetAddress maybeGetByName(String hostname)
{
try
{
return InetAddress.getByName(hostname);
}
catch (UnknownHostException _e)
{
return null;
}
}
}
32 changes: 32 additions & 0 deletions src/java/com/palantir/cassandra/net/DnsClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 com.palantir.cassandra.net;

import java.net.InetAddress;
import java.net.UnknownHostException;

public interface DnsClient
{
String getLocalHostname();

/**
* {@link InetAddress#getByName(String)} but returns null instead of throwing {@link UnknownHostException}.
*/
InetAddress maybeGetByName(String hostname);
}
26 changes: 26 additions & 0 deletions src/java/com/palantir/cassandra/net/HostnameResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 com.palantir.cassandra.net;

import java.net.InetAddress;

public interface HostnameResolver
{
String getHostname(InetAddress address);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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 com.palantir.cassandra.net;

import java.net.InetAddress;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;

public class KubernetesHostnameResolver implements HostnameResolver
{
private static final Pattern podHostnamePattern = Pattern.compile("^(.*)-(.*)\\.(.*)\\.(.*)\\.svc\\.cluster\\.local$");

private final Supplier<Integer> endpointCountSupplier;
private final DnsClient dnsClient;

public KubernetesHostnameResolver(Supplier<Integer> endpointCountSupplier)
{
this.endpointCountSupplier = endpointCountSupplier;
this.dnsClient = new DefaultDnsClient();
}

@VisibleForTesting
KubernetesHostnameResolver(Supplier<Integer> endpointCountSupplier, DnsClient dnsClient)
{
this.endpointCountSupplier = endpointCountSupplier;
this.dnsClient = dnsClient;
}

public String getHostname(InetAddress address)
{
String localHostname = dnsClient.getLocalHostname();
Matcher matcher = podHostnamePattern.matcher(localHostname);
Preconditions.checkState(matcher.matches(), "Local hostname does not match expected pattern: %s", localHostname);

String stsName = matcher.group(1);
String namespace = matcher.group(4);

int endpointCount = endpointCountSupplier.get();
for (int i = 0; i < endpointCount; i++)
{
String hostname = getKubernetesPodUri(stsName, namespace, i);
if (address.equals(dnsClient.maybeGetByName(hostname)))
{
return hostname;
}
}

throw new RuntimeException("Could not resolve hostname for address: " + address);
}


private static String getKubernetesPodUri(String stsName, String namespace, int podIdx) {
return String.format("%s-%s.%s.%s.svc.cluster.local", stsName, podIdx, stsName, namespace);
}
}
42 changes: 42 additions & 0 deletions src/java/com/palantir/cassandra/utils/InetAddressUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 com.palantir.cassandra.utils;

import java.net.InetAddress;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.lang3.reflect.MethodUtils;

public final class InetAddressUtils
{
private InetAddressUtils() {}

public static void setHostname(InetAddress address, String hostname) {
try
{
Object inetAddressHolder = MethodUtils.invokeMethod(address, true, "holder");
FieldUtils.writeField(inetAddressHolder, "hostName", hostname, true);
FieldUtils.writeField(inetAddressHolder, "originalHostName", hostname, true);
}
catch (Exception e)
{
throw new RuntimeException("Failed to set hostname for InetAddress", e);
}
}
}
10 changes: 10 additions & 0 deletions src/java/org/apache/cassandra/net/MessagingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@

import com.palantir.cassandra.cvim.CrossVpcIpMappingAck;
import com.palantir.cassandra.cvim.CrossVpcIpMappingSyn;
import com.palantir.cassandra.net.HostnameResolver;
import com.palantir.cassandra.net.KubernetesHostnameResolver;
import com.palantir.cassandra.utils.InetAddressUtils;
import org.apache.cassandra.concurrent.ExecutorLocals;
import org.apache.cassandra.concurrent.ScheduledExecutors;
import org.apache.cassandra.concurrent.Stage;
Expand All @@ -58,6 +61,7 @@
import org.apache.cassandra.gms.GossipDigestAck;
import org.apache.cassandra.gms.GossipDigestAck2;
import org.apache.cassandra.gms.GossipDigestSyn;
import org.apache.cassandra.gms.Gossiper;
import org.apache.cassandra.io.IVersionedSerializer;
import org.apache.cassandra.io.util.DataOutputPlus;
import org.apache.cassandra.io.util.FileUtils;
Expand Down Expand Up @@ -980,6 +984,7 @@ public static class SocketThread extends Thread
private final ServerSocket server;
@VisibleForTesting
public final Set<Closeable> connections = Sets.newConcurrentHashSet();
private final HostnameResolver hostnameResolver = new KubernetesHostnameResolver(() -> Math.max(3, Gossiper.instance.getEndpointStates().size() + 1));

SocketThread(ServerSocket server, String name)
{
Expand All @@ -997,6 +1002,11 @@ public void run()
{
socket = server.accept();
InetAddress remote = socket.getInetAddress();
if (Boolean.getBoolean("palantir_cassandra.use_custom_reverse_dns"))
{
InetAddressUtils.setHostname(remote, hostnameResolver.getHostname(remote));
}

logger.trace("Attempting to accept incoming connection from {}", remote);

if (!authenticate(socket))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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 com.palantir.cassandra.net;

import java.net.InetAddress;

import com.google.common.collect.ImmutableMap;
import com.google.common.net.InetAddresses;
import org.junit.Before;
import org.junit.Test;

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

public class KubernetesHostnameResolverTest
{

private DnsClient dnsClient;

@Before
public void before()
{
dnsClient = mock(DnsClient.class);

when(dnsClient.getLocalHostname()).thenReturn(hostname(0));
when(dnsClient.maybeGetByName(hostname(0))).thenReturn(InetAddresses.forString("10.0.0.1"));
when(dnsClient.maybeGetByName(hostname(1))).thenReturn(InetAddresses.forString("10.0.0.2"));
when(dnsClient.maybeGetByName(hostname(2))).thenReturn(InetAddresses.forString("10.0.0.3"));
}

@Test
public void testResolve()
{
KubernetesHostnameResolver resolver = new KubernetesHostnameResolver(() -> 3, dnsClient);

String actual = resolver.getHostname(InetAddresses.forString("10.0.0.3"));
assertThat(actual).isEqualTo(hostname(2));
}

@Test
public void testAddressNotResolvable()
{
KubernetesHostnameResolver resolver = new KubernetesHostnameResolver(() -> 3, dnsClient);

assertThatThrownBy(() -> resolver.getHostname(InetAddresses.forString("10.0.0.4"))).isInstanceOf(RuntimeException.class);
}

@Test
public void testAddressNotResolvableWhenEndpointCountWrong()
{
KubernetesHostnameResolver resolver = new KubernetesHostnameResolver(() -> 2, dnsClient);

assertThatThrownBy(() -> resolver.getHostname(InetAddresses.forString("10.0.0.3"))).isInstanceOf(RuntimeException.class);
}

@Test
public void testResolvesWhenOtherAddressIsUnresolvable()
{
when(dnsClient.maybeGetByName(hostname(1))).thenReturn(null);

KubernetesHostnameResolver resolver = new KubernetesHostnameResolver(() -> 2, dnsClient);

assertThatThrownBy(() -> resolver.getHostname(InetAddresses.forString("10.0.0.3"))).isInstanceOf(RuntimeException.class);
}

private static String hostname(int podIdx) {
return String.format("stateful-set-name-%d.stateful-set-name.data-namespace.svc.cluster.local", podIdx);
}
}
Loading