jillesvangurp / kt-search

Multi platform kotlin client for Elasticsearch & Opensearch with easily extendable Kotlin DSLs for queries, mappings, bulk, and more.
MIT License
107 stars 23 forks source link

[BUG] Missing authentication crendetials for REST request #19

Closed lengerad closed 2 years ago

lengerad commented 2 years ago

Describe the bug I recently switch from es-kotlin-client but I'm unable to connect to the ES cluster for basic search. I suspect this might be an issue with settings of my ES cluster but since it was working with previous client I'm not sure so posting here first. I found following thread with similar problem here. I guess it might be related to switching to different version of Rest client under the hood? Because all settings (host, port, user) is kept the same from previous usage.

By the observations it seems that it doesn't matter if I put wrong/correct credentials and it still keeps giving the same result - hence it seems it's really not used.

To Reproduce Steps to reproduce the behavior:

I'm using following setup for ES client (as I was used to before):

private val client = SearchClient(
        KtorRestClient(
            host = host,
            port = port,
            https = https,
            user = user,
            password = password
        )
    )

and ten I just want to search by ids

val response = client.search(index) {
            query = ids(*ids)
        }

and result is:

07:42:03.656 [eventLoopGroupProxy-4-1 @call-handler#11] ERROR ktor.application - Unhandled: GET - /v1/document/search/
com.jillesvangurp.ktsearch.RestException: RequestIsWrong 401: {"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication credentials for REST request [/default/_search]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}}],"type":"security_exception","reason":"missing authentication credentials for REST request [/default/_search]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}},"status":401}
    at com.jillesvangurp.ktsearch.RestclientKt.asResult(Restclient.kt:91)
    at com.jillesvangurp.ktsearch.Request_dslKt.post(request-dsl.kt:70)
    at com.jillesvangurp.ktsearch.Request_dslKt$post$1.invokeSuspend(request-dsl.kt)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(SuspendFunctionGun.kt:138)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:112)
    at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(SuspendFunctionGun.kt:14)
    at io.ktor.util.pipeline.SuspendFunctionGun$continuation$1.resumeWith(SuspendFunctionGun.kt:62)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
    at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(SuspendFunctionGun.kt:138)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:112)
    at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(SuspendFunctionGun.kt:14)
    at io.ktor.util.pipeline.SuspendFunctionGun$continuation$1.resumeWith(SuspendFunctionGun.kt:62)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
    at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(SuspendFunctionGun.kt:138)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:112)
    at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(SuspendFunctionGun.kt:14)
    at io.ktor.util.pipeline.SuspendFunctionGun$continuation$1.resumeWith(SuspendFunctionGun.kt:62)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
    at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(SuspendFunctionGun.kt:138)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:112)
    at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(SuspendFunctionGun.kt:14)
    at io.ktor.util.pipeline.SuspendFunctionGun$continuation$1.resumeWith(SuspendFunctionGun.kt:62)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
    at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(SuspendFunctionGun.kt:138)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:112)
    at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(SuspendFunctionGun.kt:14)
    at io.ktor.util.pipeline.SuspendFunctionGun$continuation$1.resumeWith(SuspendFunctionGun.kt:62)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
    at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:174)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:167)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:470)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:503)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.ktor.server.netty.EventLoopGroupProxy$Companion.create$lambda-1$lambda-0(NettyApplicationEngine.kt:285)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:832)

Expected behavior Credentials passed in KtorRestClient should be used as it somehow seems that they are missing or invalid.

Your context

Kotlin version: 1.7.10 Search client version: search-client:1.99.5 Version of Elasticsearch: "number" : "7.17.1",

Will you be able to help with a pull request?

Optional of course, but do let me know if you plan to do work.

jillesvangurp commented 2 years ago

thanks for reporting, I'll test this and fix it shortly hopefuly,

jillesvangurp commented 2 years ago

Confirmed. The one thing I did not test yet was indeed broken. I fixed and tested this properly now with our elastic cloud server. So it should work now.

The fix involves adding the ktor auth module and installing using basic authentication. Let me know if you have any further issues with this. I will release a new jar shortly.

lengerad commented 2 years ago

Seems to be working! Thanks for a prompt reaction and for fixing 👏