-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-5643: [FlightRPC] Add ability to override SSL hostname checking #4608
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
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 |
|---|---|---|
|
|
@@ -675,5 +675,23 @@ TEST_F(TestTls, DoAction) { | |
| ASSERT_EQ(result->body->ToString(), "Hello, world!"); | ||
| } | ||
|
|
||
| TEST_F(TestTls, OverrideHostname) { | ||
| std::unique_ptr<FlightClient> client; | ||
| auto client_options = FlightClientOptions(); | ||
| client_options.override_hostname = "fakehostname"; | ||
| CertKeyPair root_cert; | ||
| ASSERT_OK(ExampleTlsCertificateRoot(&root_cert)); | ||
| client_options.tls_root_certs = root_cert.pem_cert; | ||
| ASSERT_OK(FlightClient::Connect(server_->location(), client_options, &client)); | ||
|
|
||
| FlightCallOptions options; | ||
| options.timeout = TimeoutDuration{5.0}; | ||
| Action action; | ||
| action.type = "test"; | ||
| action.body = Buffer::FromString(""); | ||
| std::unique_ptr<ResultStream> results; | ||
| ASSERT_RAISES(IOError, client->DoAction(options, action, &results)); | ||
|
||
| } | ||
|
|
||
| } // namespace flight | ||
| } // namespace arrow | ||
| 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() { | ||
| } | ||
| } | ||
| } |
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.
Hmm... interesting. The certificate error is not raised at connect time?
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.
gRPC doesn't actually try to make a connection until an RPC is issued, generally.