Skip to content

Commit bd4323d

Browse files
committed
Scala client working
1 parent 81f5e53 commit bd4323d

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

sql/connect/common/src/main/scala/org/apache/spark/sql/connect/client/SparkConnectClient.scala

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,8 @@ object SparkConnectClient {
730730
grpcMaxMessageSize: Int = ConnectCommon.CONNECT_GRPC_MAX_MESSAGE_SIZE,
731731
grpcMaxRecursionLimit: Int = ConnectCommon.CONNECT_GRPC_MARSHALLER_RECURSION_LIMIT) {
732732

733+
private def isLocal = host.equals("localhost")
734+
733735
def userContext: proto.UserContext = {
734736
val builder = proto.UserContext.newBuilder()
735737
if (userId != null) {
@@ -742,7 +744,7 @@ object SparkConnectClient {
742744
}
743745

744746
def credentials: ChannelCredentials = {
745-
if (isSslEnabled.contains(true)) {
747+
if (isSslEnabled.contains(true) || (token.isDefined && !isLocal)) {
746748
token match {
747749
case Some(t) =>
748750
// With access token added in the http header.
@@ -753,22 +755,23 @@ object SparkConnectClient {
753755
TlsChannelCredentials.create()
754756
}
755757
} else {
756-
token match {
757-
case Some(t) =>
758-
CompositeChannelCredentials.create(
759-
InsecureChannelCredentials.create(),
760-
new AccessTokenCallCredentials(t))
761-
case None =>
762-
InsecureChannelCredentials.create()
763-
}
758+
InsecureChannelCredentials.create()
764759
}
765760
}
766761

767762
def createChannel(): ManagedChannel = {
768-
val channelBuilder = Grpc.newChannelBuilderForAddress(host, port, credentials)
763+
val creds = credentials
764+
val channelBuilder = Grpc.newChannelBuilderForAddress(host, port, creds)
765+
766+
// Workaround LocalChannelCredentials are added in
767+
// https://github.com/grpc/grpc-java/issues/9900
768+
var metadataWithOptionalToken = metadata
769+
if (!isSslEnabled.contains(true) && isLocal && token.isDefined) {
770+
metadataWithOptionalToken = metadata + (("Authorization", s"Bearer ${token.get}"))
771+
}
769772

770-
if (metadata.nonEmpty) {
771-
channelBuilder.intercept(new MetadataHeaderClientInterceptor(metadata))
773+
if (metadataWithOptionalToken.nonEmpty) {
774+
channelBuilder.intercept(new MetadataHeaderClientInterceptor(metadataWithOptionalToken))
772775
}
773776

774777
interceptors.foreach(channelBuilder.intercept(_))
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
package org.apache.spark.sql.connect.service
18+
19+
// import io.grpc.StatusRuntimeException
20+
21+
import org.apache.spark.SparkException
22+
import org.apache.spark.sql.connect.{SparkConnectServerTest, SparkSession}
23+
import org.apache.spark.sql.connect.service.SparkConnectService
24+
25+
class SparkConnectAuthSuite extends SparkConnectServerTest {
26+
override protected def sparkConf = {
27+
super.sparkConf.set("spark.connect.authenticate.token", "deadbeef")
28+
}
29+
30+
test("Test local authentication") {
31+
val session = SparkSession
32+
.builder()
33+
.remote(s"sc://localhost:${SparkConnectService.localPort}/;token=deadbeef")
34+
.create()
35+
session.range(5).collect()
36+
37+
val invalidSession = SparkSession
38+
.builder()
39+
.remote(s"sc://localhost:${SparkConnectService.localPort}/;token=invalid")
40+
.create()
41+
val exception = intercept[SparkException] {
42+
invalidSession.range(5).collect()
43+
}
44+
assert(exception.getMessage.contains("Invalid authentication token"))
45+
}
46+
}

0 commit comments

Comments
 (0)