-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ARROW-5643: [FlightRPC] Add ability to override SSL hostname checking
Adds the ability to override hostname checks, so you can connect to localhost over TLS but still verify that the certificate is for some other domain. Example: when deploying on Kubernetes with headless services, clients connect directly to backend services and do load balancing themselves. Thus all instances of an application must present a certificate for the same hostname. To do health checks in such an environment, you can't connect to the TLS hostname (which may resolve to a different instance); you need to connect to localhost, and override the hostname check. Also needs apache/arrow-testing#5 Author: David Li <[email protected]> Closes #4608 from lihalite/flight-tls-java and squashes the following commits: 581fc75 <David Li> Add ability to override SSL hostname checking
- Loading branch information
Showing
12 changed files
with
259 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
java/flight/src/test/java/org/apache/arrow/flight/TestTls.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* 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.arrow.flight; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Iterator; | ||
import java.util.function.Consumer; | ||
|
||
import org.apache.arrow.flight.FlightClient.Builder; | ||
import org.apache.arrow.memory.BufferAllocator; | ||
import org.apache.arrow.memory.RootAllocator; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Tests for TLS in Flight. | ||
*/ | ||
public class TestTls { | ||
|
||
/** | ||
* Test a basic request over TLS. | ||
*/ | ||
@Test | ||
public void connectTls() { | ||
test((builder) -> { | ||
try (final InputStream roots = new FileInputStream(FlightTestUtil.exampleTlsRootCert().toFile()); | ||
final FlightClient client = builder.trustedCertificates(roots).build()) { | ||
final Iterator<Result> responses = client.doAction(new Action("hello-world")); | ||
final byte[] response = responses.next().getBody(); | ||
Assert.assertEquals("Hello, world!", new String(response, StandardCharsets.UTF_8)); | ||
Assert.assertFalse(responses.hasNext()); | ||
} catch (InterruptedException | IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Make sure that connections are rejected when the root certificate isn't trusted. | ||
*/ | ||
@Test(expected = io.grpc.StatusRuntimeException.class) | ||
public void rejectInvalidCert() { | ||
test((builder) -> { | ||
try (final FlightClient client = builder.build()) { | ||
final Iterator<Result> responses = client.doAction(new Action("hello-world")); | ||
responses.next().getBody(); | ||
Assert.fail("Call should have failed"); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Make sure that connections are rejected when the hostname doesn't match. | ||
*/ | ||
@Test(expected = io.grpc.StatusRuntimeException.class) | ||
public void rejectHostname() { | ||
test((builder) -> { | ||
try (final InputStream roots = new FileInputStream(FlightTestUtil.exampleTlsRootCert().toFile()); | ||
final FlightClient client = builder.trustedCertificates(roots).overrideHostname("fakehostname") | ||
.build()) { | ||
final Iterator<Result> responses = client.doAction(new Action("hello-world")); | ||
responses.next().getBody(); | ||
Assert.fail("Call should have failed"); | ||
} catch (InterruptedException | IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
|
||
|
||
void test(Consumer<Builder> testFn) { | ||
final FlightTestUtil.CertKeyPair certKey = FlightTestUtil.exampleTlsCerts().get(0); | ||
try ( | ||
BufferAllocator a = new RootAllocator(Long.MAX_VALUE); | ||
Producer producer = new Producer(); | ||
FlightServer s = | ||
FlightTestUtil.getStartedServer( | ||
(port) -> { | ||
try { | ||
return FlightServer.builder(a, Location.forGrpcTls(FlightTestUtil.LOCALHOST, port), producer) | ||
.useTls(certKey.cert, certKey.key) | ||
.build(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
})) { | ||
final Builder builder = FlightClient.builder(a, Location.forGrpcTls(FlightTestUtil.LOCALHOST, s.getPort())); | ||
testFn.accept(builder); | ||
} catch (InterruptedException | IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
static class Producer extends NoOpFlightProducer implements AutoCloseable { | ||
|
||
@Override | ||
public void doAction(CallContext context, Action action, StreamListener<Result> listener) { | ||
if (action.getType().equals("hello-world")) { | ||
listener.onNext(new Result("Hello, world!".getBytes(StandardCharsets.UTF_8))); | ||
listener.onCompleted(); | ||
} | ||
listener.onError(new UnsupportedOperationException("Invalid action " + action.getType())); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.