]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | package org.apache.arrow.flight.example.integration; | |
19 | ||
20 | import java.nio.charset.StandardCharsets; | |
21 | import java.util.Arrays; | |
22 | import java.util.Optional; | |
23 | ||
24 | import org.apache.arrow.flight.Action; | |
25 | import org.apache.arrow.flight.CallStatus; | |
26 | import org.apache.arrow.flight.FlightClient; | |
27 | import org.apache.arrow.flight.FlightProducer; | |
28 | import org.apache.arrow.flight.FlightRuntimeException; | |
29 | import org.apache.arrow.flight.FlightServer; | |
30 | import org.apache.arrow.flight.FlightStatusCode; | |
31 | import org.apache.arrow.flight.Location; | |
32 | import org.apache.arrow.flight.NoOpFlightProducer; | |
33 | import org.apache.arrow.flight.Result; | |
34 | import org.apache.arrow.flight.auth.BasicClientAuthHandler; | |
35 | import org.apache.arrow.flight.auth.BasicServerAuthHandler; | |
36 | import org.apache.arrow.memory.BufferAllocator; | |
37 | ||
38 | /** | |
39 | * A scenario testing the built-in basic authentication Protobuf. | |
40 | */ | |
41 | final class AuthBasicProtoScenario implements Scenario { | |
42 | ||
43 | static final String USERNAME = "arrow"; | |
44 | static final String PASSWORD = "flight"; | |
45 | ||
46 | @Override | |
47 | public FlightProducer producer(BufferAllocator allocator, Location location) { | |
48 | return new NoOpFlightProducer() { | |
49 | @Override | |
50 | public void doAction(CallContext context, Action action, StreamListener<Result> listener) { | |
51 | listener.onNext(new Result(context.peerIdentity().getBytes(StandardCharsets.UTF_8))); | |
52 | listener.onCompleted(); | |
53 | } | |
54 | }; | |
55 | } | |
56 | ||
57 | @Override | |
58 | public void buildServer(FlightServer.Builder builder) { | |
59 | builder.authHandler(new BasicServerAuthHandler(new BasicServerAuthHandler.BasicAuthValidator() { | |
60 | @Override | |
61 | public byte[] getToken(String username, String password) throws Exception { | |
62 | if (!USERNAME.equals(username) || !PASSWORD.equals(password)) { | |
63 | throw CallStatus.UNAUTHENTICATED.withDescription("Username or password is invalid.").toRuntimeException(); | |
64 | } | |
65 | return ("valid:" + username).getBytes(StandardCharsets.UTF_8); | |
66 | } | |
67 | ||
68 | @Override | |
69 | public Optional<String> isValid(byte[] token) { | |
70 | if (token != null) { | |
71 | final String credential = new String(token, StandardCharsets.UTF_8); | |
72 | if (credential.startsWith("valid:")) { | |
73 | return Optional.of(credential.substring(6)); | |
74 | } | |
75 | } | |
76 | return Optional.empty(); | |
77 | } | |
78 | })); | |
79 | } | |
80 | ||
81 | @Override | |
82 | public void client(BufferAllocator allocator, Location location, FlightClient client) { | |
83 | final FlightRuntimeException e = IntegrationAssertions.assertThrows(FlightRuntimeException.class, () -> { | |
84 | client.listActions().forEach(act -> { | |
85 | }); | |
86 | }); | |
87 | if (!FlightStatusCode.UNAUTHENTICATED.equals(e.status().code())) { | |
88 | throw new AssertionError("Expected UNAUTHENTICATED but found " + e.status().code(), e); | |
89 | } | |
90 | ||
91 | client.authenticate(new BasicClientAuthHandler(USERNAME, PASSWORD)); | |
92 | final Result result = client.doAction(new Action("")).next(); | |
93 | if (!USERNAME.equals(new String(result.getBody(), StandardCharsets.UTF_8))) { | |
94 | throw new AssertionError("Expected " + USERNAME + " but got " + Arrays.toString(result.getBody())); | |
95 | } | |
96 | } | |
97 | } |