Skip to content

Commit

Permalink
Grpc insecure mode option added
Browse files Browse the repository at this point in the history
  • Loading branch information
niklastheman committed Dec 4, 2023
1 parent b2052e1 commit 4e03300
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class FednClient(
private val name: String? = null,
private val heartbeatInterval: Long = 5000,
private val port: Int = 443,
private val secureGrpcConnection: Boolean = true,
private val defaultDispatcher: CoroutineDispatcher = Dispatchers.IO,
private var _httpHandler: IHttpHandler? = null,
private var _grpcHandler: IGrpcHandler? = null
Expand Down Expand Up @@ -140,7 +141,14 @@ class FednClient(

val fqdn = response.fqdn

grpcHandler = GrpcHandler(clientName, fqdn, port, token, response.host)
grpcHandler = GrpcHandler(
clientName,
fqdn,
port,
token,
response.host,
secureGrpcConnection = secureGrpcConnection
)

attached = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import com.google.protobuf.ByteString
import io.grpc.ManagedChannel
import io.grpc.ManagedChannelBuilder
import io.grpc.Metadata
import io.grpc.NameResolver
import io.grpc.StatusException
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
Expand All @@ -29,6 +28,7 @@ const val CHUNK_SIZE: Int = 1024 * 1024
enum class ModelUpdateState {
LISTENER_INITIALIZED, SERVER_MODEL_DOWNLOADED, TRAINING_STARTED, TRAINING_COMPLETED, MODEL_UPLOADED, TRAINING_ROUND_FINISHED
}

interface IGrpcHandler : Closeable {
suspend fun sendHeartbeat()
suspend fun listenToModelUpdateRequestStream(
Expand All @@ -43,7 +43,8 @@ internal class GrpcHandler(
private val url: String,
private val port: Int,
private val token: String,
private val combinerName: String
private val combinerName: String,
private val secureGrpcConnection: Boolean = true
) : IGrpcHandler {

private val headers = Metadata().apply {
Expand All @@ -62,7 +63,10 @@ internal class GrpcHandler(
if (_managedChannel == null) {

_managedChannel =
ManagedChannelBuilder.forAddress(url, port).useTransportSecurity().build()
if (secureGrpcConnection)
ManagedChannelBuilder.forAddress(url, port).useTransportSecurity().build()
else
ManagedChannelBuilder.forAddress(url, port).usePlaintext().build()
}

return _managedChannel ?: throw AssertionError("Set to null by another thread")
Expand Down
Binary file modified FednKotlin/app/dependencies/fedn-client.jar
Binary file not shown.
17 changes: 12 additions & 5 deletions FednKotlin/app/src/main/kotlin/fednkotlin/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package fednkotlin

import com.example.fedn_client.FednClient
import com.example.fedn_client.IFednClient
import java.util.UUID
import kotlinx.coroutines.runBlocking

val runTrainingProcess: (ByteArray) -> ByteArray = { modelIn ->
Expand All @@ -18,18 +19,24 @@ fun main() {
runBlocking {
val url: String? = System.getenv("FEDN_URL")
val token: String? = System.getenv("FEDN_TOKEN")
val name: String? = System.getenv("FEDN_NAME")
var name: String? = System.getenv("FEDN_NAME")

if (url == null || token == null || name == null) {
println("FEDN_URL, FEDN_TOKEN and FEDN_NAME must be set")
if (url == null || token == null) {
println("FEDN_URL and FEDN_TOKEN must be set")
return@runBlocking
}

println("FEDN_URL: $url")
println("FEDN_TOKEN: $token")

if (name == null) {
println("FEDN_NAME not set, using random name")
name = UUID.randomUUID().toString()
}
println("FEDN_NAME: $name")

val fednClient: IFednClient = FednClient(url, token, name = name)
val fednClient: IFednClient =
FednClient(url, token, name = name, secureGrpcConnection = false)

val result =
fednClient.runProcess(
Expand All @@ -41,7 +48,7 @@ fun main() {
timeoutAfterMillis = 60000
)

println("Result: ${result?.first}")
println("Result: ${result.first}")
}

println("Program ran to completion")
Expand Down

0 comments on commit 4e03300

Please sign in to comment.