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

[Java][FlightSQL] In authentication, how does flight server get the IP/Hostname of ADBC client #38911

Closed
xinyiZzz opened this issue Nov 28, 2023 · 4 comments
Labels
Component: FlightRPC Type: usage Issue is a user question

Comments

@xinyiZzz
Copy link

Describe the usage question you have. Please include as many useful details as possible.

Now I implement CallHeaderAuthenticator in Java, and Override AuthResult authenticate(CallHeaders incomingHeaders).

xxx implements CallHeaderAuthenticator {
@Override
    public AuthResult authenticate(CallHeaders incomingHeaders) {
    }
}

Then, BasicCallHeaderAuthenticator can decode username and password from incomingHeaders. I implements BasicCallHeaderAuthenticator.CredentialValidator and Override AuthResult validate(String username, String password) to complete authentication of username and password.

xxx implements BasicCallHeaderAuthenticator.CredentialValidator {
@Override
    public AuthResult validate(String username, String password) {
    }
}

But in my database(Apache Doris), the client's IP will also participate in authentication, so is there a way to get the ADBC client's IP in Flight Server?

Thanks for your help.

Component(s)

FlightRPC

@xinyiZzz xinyiZzz added the Type: usage Issue is a user question label Nov 28, 2023
@xinyiZzz
Copy link
Author

xinyiZzz commented Dec 5, 2023

I hope for help, thanks~

@aiguofer
Copy link
Contributor

We currently accomplish this with a gRPC ServerInterceptor to add it to the RequestContext (in Kotlin):

class RequestContextKeys {
    companion object {
        const val AUTHORITY = "authority"
        const val SCHEME = "scheme"
        const val PORT = "port"
        const val INCOMING_URL = "incomingUrl"
        const val REQUEST_ID = "request_id"
    }
}

/*
 * Simple interceptor to add more metadata from the call to the RequestContext.
 */
class RequestContextInterceptor : ServerInterceptor {
    override fun <ReqT : Any, RespT : Any> interceptCall(
        call: ServerCall<ReqT, RespT>,
        headers: Metadata,
        next: ServerCallHandler<ReqT, RespT>
    ): ServerCall.Listener<ReqT> {
        RequestContextAdapter.REQUEST_CONTEXT_KEY.get()?.also { requestContext ->
            val authority =
                call.authority ?: throw InternalServiceException("Could not find authority")
            val scheme = if (authority.startsWith("localhost")) "http" else "https"
            val port =
                when {
                    authority.contains(":") -> authority.substringAfter(":")
                    scheme == "https" -> "443"
                    else -> throw InternalServiceException("Could not infer port number")
                }
            val host = authority.substringBefore(":")

            requestContext.put(RequestContextKeys.AUTHORITY, authority)
            requestContext.put(RequestContextKeys.SCHEME, scheme)
            requestContext.put(RequestContextKeys.PORT, port)
            requestContext.put(RequestContextKeys.INCOMING_URL, "$scheme://$host:$port")
        }
        return next.startCall(call, headers)
    }
}

We can then access it anywhere using:

val requestContext = RequestContextAdapter.REQUEST_CONTEXT_KEY.get()
requestContext.get(RequestContextKeys.INCOMING_URL)

@aiguofer
Copy link
Contributor

Oh sorry, I just realized you're talking about the IP/Host of the client, not the one they requested. However, you could follow a similar pattern to add w/e info you want from the call. You can get the caller IP: https://stackoverflow.com/questions/36124618/how-to-get-client-ip-from-request-metadata-with-grpc-java

@xinyiZzz
Copy link
Author

Oh sorry, I just realized you're talking about the IP/Host of the client, not the one they requested. However, you could follow a similar pattern to add w/e info you want from the call. You can get the caller IP: https://stackoverflow.com/questions/36124618/how-to-get-client-ip-from-request-metadata-with-grpc-java

Thanks! @aiguofer, I'll try it.

@xinyiZzz xinyiZzz closed this as completed Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: FlightRPC Type: usage Issue is a user question
Projects
None yet
Development

No branches or pull requests

2 participants