Skip to content

add support for etcd client authentication #5269

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

Merged
merged 2 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,16 @@ object RichListenableFuture {

object EtcdClient {
// hostAndPorts format: {HOST}:{PORT}[,{HOST}:{PORT},{HOST}:{PORT}, ...]
def apply(hostAndPorts: String)(implicit ece: ExecutionContextExecutor): EtcdClient = {
require(hostAndPorts != null)
val client: Client = Client.forEndpoints(hostAndPorts).withPlainText().build()
new EtcdClient(client)(ece)
def apply(config: EtcdConfig)(implicit ece: ExecutionContextExecutor): EtcdClient = {
require(config.hosts != null)
require(
(config.username.nonEmpty && config.password.nonEmpty) || (config.username.isEmpty && config.password.isEmpty))
val clientBuilder = Client.forEndpoints(config.hosts).withPlainText()
if (config.username.nonEmpty && config.password.nonEmpty) {
new EtcdClient(clientBuilder.withCredentials(config.username.get, config.password.get).build())
} else {
new EtcdClient(clientBuilder.build())(ece)
}
}

def apply(client: Client)(implicit ece: ExecutionContextExecutor): EtcdClient = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import pureconfig.loadConfigOrThrow
import scala.language.implicitConversions
import scala.util.Try

case class EtcdConfig(hosts: String)
case class EtcdConfig(hosts: String, username: Option[String], password: Option[String])

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ object FPCPoolBalancer extends LoadBalancerProvider {
}
}

val etcd = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd).hosts)
val etcd = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd))

new FPCPoolBalancer(whiskConfig, instance, etcd, feedFactory)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class FPCInvokerReactive(config: WhiskConfig,
private val logsProvider = SpiLoader.get[LogStoreProvider].instance(actorSystem)
logging.info(this, s"LogStoreProvider: ${logsProvider.getClass}")

private val etcdClient = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd).hosts)
private val etcdClient = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd))

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Scheduler(schedulerId: SchedulerInstanceId, schedulerEndpoints: SchedulerE
val producer = msgProvider.getProducer(config, Some(ActivationEntityLimit.MAX_ACTIVATION_LIMIT))

val maxPeek = loadConfigOrThrow[Int](ConfigKeys.schedulerMaxPeek)
val etcdClient = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd).hosts)
val etcdClient = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd))
val watcherService: ActorRef = actorSystem.actorOf(WatcherService.props(etcdClient))
val leaseService =
actorSystem.actorOf(LeaseKeepAliveService.props(etcdClient, schedulerId, watcherService))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.apache.openwhisk.common.etcd

import common.WskActorSystem
import org.apache.openwhisk.core.etcd.{EtcdClient, EtcdConfig}
import org.junit.runner.RunWith
import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.junit.JUnitRunner

import scala.concurrent.ExecutionContextExecutor


@RunWith(classOf[JUnitRunner])
class EtcdConfigTests
extends FlatSpec
with Matchers
with WskActorSystem {
behavior of "EtcdConfig"

implicit val ece: ExecutionContextExecutor = actorSystem.dispatcher

it should "create client when no auth is supplied through config" in {
val config = EtcdConfig("localhost:2379", None, None)

val client = EtcdClient(config)
client.close()
}

it should "create client when auth is supplied through config" in {
val config = EtcdConfig("localhost:2379", Some("username"), Some("password"))

val client = EtcdClient(config)
client.close()
}

it should "fail to create client when one of username or password is supplied in config" in {
val config = EtcdConfig("localhost:2379", None, Some("password"))

assertThrows[IllegalArgumentException](EtcdClient(config))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class FPCPoolBalancerTests

private implicit val transId = TransactionId.testing
implicit val ece: ExecutionContextExecutor = actorSystem.dispatcher
private val etcd = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd).hosts)
private val etcd = EtcdClient(loadConfigOrThrow[EtcdConfig](ConfigKeys.etcd))

private val testInvocationNamespace = "test-invocation-namespace"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ class ContainerManager2Tests
with DbUtils {

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

override def afterAll(): Unit = {
Expand Down