Skip to content

Commit d80bca7

Browse files
author
Brendan Doyle
committed
add support for etcd client authentication
1 parent 052d4d2 commit d80bca7

File tree

8 files changed

+56
-10
lines changed

8 files changed

+56
-10
lines changed

common/scala/src/main/scala/org/apache/openwhisk/core/etcd/EtcdClient.scala

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,16 @@ object RichListenableFuture {
4848

4949
object EtcdClient {
5050
// hostAndPorts format: {HOST}:{PORT}[,{HOST}:{PORT},{HOST}:{PORT}, ...]
51-
def apply(hostAndPorts: String)(implicit ece: ExecutionContextExecutor): EtcdClient = {
52-
require(hostAndPorts != null)
53-
val client: Client = Client.forEndpoints(hostAndPorts).withPlainText().build()
54-
new EtcdClient(client)(ece)
51+
def apply(config: EtcdConfig)(implicit ece: ExecutionContextExecutor): EtcdClient = {
52+
require(config.hosts != null)
53+
require(
54+
(config.username.nonEmpty && config.password.nonEmpty) || (config.username.isEmpty && config.password.isEmpty))
55+
val clientBuilder = Client.forEndpoints(config.hosts).withPlainText()
56+
if (config.username.nonEmpty && config.password.nonEmpty) {
57+
new EtcdClient(clientBuilder.withCredentials(config.username.get, config.password.get).build())
58+
} else {
59+
new EtcdClient(clientBuilder.build())(ece)
60+
}
5561
}
5662

5763
def apply(client: Client)(implicit ece: ExecutionContextExecutor): EtcdClient = {

common/scala/src/main/scala/org/apache/openwhisk/core/etcd/EtcdUtils.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import pureconfig.loadConfigOrThrow
3030
import scala.language.implicitConversions
3131
import scala.util.Try
3232

33-
case class EtcdConfig(hosts: String)
33+
case class EtcdConfig(hosts: String, username: Option[String], password: Option[String])
3434

3535
case class EtcdException(msg: String) extends Exception(msg)
3636

core/controller/src/main/scala/org/apache/openwhisk/core/loadBalancer/FPCPoolBalancer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ object FPCPoolBalancer extends LoadBalancerProvider {
714714
}
715715
}
716716

717-
val etcd = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd).hosts)
717+
val etcd = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd))
718718

719719
new FPCPoolBalancer(whiskConfig, instance, etcd, feedFactory)
720720
}

core/invoker/src/main/scala/org/apache/openwhisk/core/invoker/FPCInvokerReactive.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class FPCInvokerReactive(config: WhiskConfig,
8282
private val logsProvider = SpiLoader.get[LogStoreProvider].instance(actorSystem)
8383
logging.info(this, s"LogStoreProvider: ${logsProvider.getClass}")
8484

85-
private val etcdClient = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd).hosts)
85+
private val etcdClient = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd))
8686

8787
private val grpcConfig = loadConfigOrThrow[GrpcServiceConfig](ConfigKeys.schedulerGrpcService)
8888

core/scheduler/src/main/scala/org/apache/openwhisk/core/scheduler/Scheduler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Scheduler(schedulerId: SchedulerInstanceId, schedulerEndpoints: SchedulerE
6565
val producer = msgProvider.getProducer(config, Some(ActivationEntityLimit.MAX_ACTIVATION_LIMIT))
6666

6767
val maxPeek = loadConfigOrThrow[Int](ConfigKeys.schedulerMaxPeek)
68-
val etcdClient = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd).hosts)
68+
val etcdClient = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd))
6969
val watcherService: ActorRef = actorSystem.actorOf(WatcherService.props(etcdClient))
7070
val leaseService =
7171
actorSystem.actorOf(LeaseKeepAliveService.props(etcdClient, schedulerId, watcherService))
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.apache.openwhisk.common.etcd
2+
3+
import common.WskActorSystem
4+
import org.apache.openwhisk.core.etcd.{EtcdClient, EtcdConfig}
5+
import org.junit.runner.RunWith
6+
import org.scalatest.{FlatSpec, Matchers}
7+
import org.scalatest.junit.JUnitRunner
8+
9+
import scala.concurrent.ExecutionContextExecutor
10+
11+
12+
@RunWith(classOf[JUnitRunner])
13+
class EtcdConfigTests
14+
extends FlatSpec
15+
with Matchers
16+
with WskActorSystem {
17+
behavior of "EtcdConfig"
18+
19+
implicit val ece: ExecutionContextExecutor = actorSystem.dispatcher
20+
21+
it should "create client when no auth is supplied through config" in {
22+
val config = EtcdConfig("localhost:2379", None, None)
23+
24+
val client = EtcdClient(config)
25+
client.close()
26+
}
27+
28+
it should "create client when auth is supplied through config" in {
29+
val config = EtcdConfig("localhost:2379", Some("username"), Some("password"))
30+
31+
val client = EtcdClient(config)
32+
client.close()
33+
}
34+
35+
it should "fail to create client when one of username or password is supplied in config" in {
36+
val config = EtcdConfig("localhost:2379", None, Some("password"))
37+
38+
assertThrows[IllegalArgumentException](EtcdClient(config))
39+
}
40+
}

tests/src/test/scala/org/apache/openwhisk/core/loadBalancer/test/FPCPoolBalancerTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class FPCPoolBalancerTests
6363

6464
private implicit val transId = TransactionId.testing
6565
implicit val ece: ExecutionContextExecutor = actorSystem.dispatcher
66-
private val etcd = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd).hosts)
66+
private val etcd = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd))
6767

6868
private val testInvocationNamespace = "test-invocation-namespace"
6969

tests/src/test/scala/org/apache/openwhisk/core/scheduler/container/test/ContainerManagerTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ class ContainerManager2Tests
11701170
with DbUtils {
11711171

11721172
implicit val dispatcher = actorSystem.dispatcher
1173-
val etcdClient = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd).hosts)
1173+
val etcdClient = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd))
11741174
val testInvocationNamespace = "test-invocation-namespace"
11751175

11761176
override def afterAll(): Unit = {

0 commit comments

Comments
 (0)