Skip to content
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

Issues loading TLS from a string instead of a file #428

Open
brsnik opened this issue Sep 6, 2023 · 0 comments
Open

Issues loading TLS from a string instead of a file #428

brsnik opened this issue Sep 6, 2023 · 0 comments

Comments

@brsnik
Copy link

brsnik commented Sep 6, 2023

The error I get:

    io.grpc.ManagedChannelRegistry$ProviderNotFoundException: io.grpc.okhttp.OkHttpChannelProvider: Unable to load private key: Neither RSA nor EC worked

I am trying to load pass the certificate as a string instead of reading a file, because it is dynamic. In order to establish a gRPC connection.

    class GrpcModule(
            private val certificateRoot: String,
            private val certificateStr: String,
            private val privateKeyStr: String,
    ) {
        val channel: ManagedChannel by lazy {
    
            val pk: InputStream = privateKeyStr.byteInputStream()
            val cu: InputStream = certificateStr.byteInputStream()
            val cr: InputStream = certificateRoot.byteInputStream()
    
            val credentials =
                    TlsChannelCredentials.newBuilder()
                            .keyManager(
                                pk,
                                cu
                            )
                            .trustManager(cr)
                            .build()
    
            Grpc.newChannelBuilder("localhost:50001", credentials)
                    .executor(Dispatchers.IO.asExecutor())
                    .build()
        }
        private val stub = ReceivingGrpcKt.ReceivingCoroutineStub(channel)
    
        suspend fun checkId(id: String) =
                try {
                    val request = checkRequest { this.id = id }
                    val response = stub.check(request)
                    Log.d("Grpc", "Success --- $response")
                } catch (e: Exception) {
                    Log.d("Grpc", "Error --- $e")
                }
    
        fun close() {
            channel.shutdownNow()
        }
    }

And I'll be calling it from a ReactNative module, like so:

    class Grpc(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
    
        override fun getName() = "Grpc"
    
        @ReactMethod
        fun test() {
    
            var g =
                    GrpcModule(
                    """
                         -----BEGIN CERTIFICATE-----
                         -----END CERTIFICATE-----
                    """.trimIndent(),
                        """
                        -----BEGIN CERTIFICATE-----
                        -----END CERTIFICATE-----
                    """.trimIndent(),
                        """
                        -----BEGIN PRIVATE KEY-----
                        -----END PRIVATE KEY-----
                    """.trimIndent(),
                    )
    
            Log.d("Grpc", "--- $g")
    
            runBlocking {
                g.checkId("222")
            }
    
            g.close()
        }
    }

What am I doing wrong here and how can I fix this?

Is this even the right way to go about it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant