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

Invalid client connection when SASL token is rejected by broker #1423

Closed
d1egoaz opened this issue Jul 8, 2019 · 4 comments
Closed

Invalid client connection when SASL token is rejected by broker #1423

d1egoaz opened this issue Jul 8, 2019 · 4 comments

Comments

@d1egoaz
Copy link
Contributor

d1egoaz commented Jul 8, 2019

Versions

Please specify real version numbers or git SHAs, not just "Latest" since that changes fairly regularly.
Sarama Version:
digest = "1:2ec153af6a806c3d63d4299f2549bcb29d75d9703097341be309a46db3481488"
version = "v1.22.1"

Kafka Version: 2.0.1
Go Version: 1.12

Configuration

What configuration values are you using for Sarama and Kafka?
SASL authentication

Problem Description

When creating a client connection to kafka using SASL and the token provider provides an invalid token, sarama is not returning an error, it returns a client with no brokers, and the connection is still open, so if we try to talk to kafka, i.e, Topics() you're getting an empty list, where this should raise an error as the client connection is invalid

when a token provider returns a valid token, it can get the list of brokers and connect to them succesfully:

{"level":"info","msg":"Initializing new client","test":"kafkaClient"}
{"level":"info","msg":"client/metadata fetching metadata for all topics from broker localhost:9094\n","test":"kafkaClient"}
{"level":"info","msg":"Successful SASL handshake. Available mechanisms: [OAUTHBEARER]","test":"kafkaClient"}
{"level":"info","msg":"Connected to broker at localhost:9094 (unregistered)\n","test":"kafkaClient"}
{"level":"info","msg":"client/brokers registered new broker #1 at localhost:9094","test":"kafkaClient"}
{"level":"info","msg":"Successfully initialized new client","test":"kafkaClient"}
{"level":"info","msg":"client.Closed() false"}
{"level":"info","msg":"brokers count: 1"}
{"level":"info","msg":"topic list [__consumer_offsets __identities]","test":"kafkaClient"}

when token provider returns an invalid token:

{"level":"info","msg":"Initializing new client","test":"kafkaClient"}
{"level":"info","msg":"client/metadata fetching metadata for all topics from broker localhost:9094\n","test":"kafkaClient"}
{"level":"info","msg":"Successful SASL handshake. Available mechanisms: [OAUTHBEARER]","test":"kafkaClient"}
{"level":"info","msg":"Received SASL auth response: {\"status\":\"invalid_request\", \"scope\":\"Could not verify token\", \"openid-configuration\":\"https://accounts.google.com/.well-known/openid-configuration\"}","test":"kafkaClient"}
{"level":"info","msg":"Connected to broker at localhost:9094 (unregistered)\n","test":"kafkaClient"}
{"level":"info","msg":"Successfully initialized new client","test":"kafkaClient"}
{"level":"info","msg":"client.Closed() false"}
{"level":"info","msg":"brokers count 0"}
{"level":"info","msg":"topic list []","test":"kafkaClient"}

as we can see, broker is rejecting the client authentication, and this seems it's an unrecoverable error for the client.

It seems this method reads the invalid request response, but it doesn't check the content to see if the broker rejected the user credentials/token:
https://github.com/Shopify/sarama/blob/c82066c1586636238edf9256e1ab6e410e080e49/broker.go#L1263-L1265

Not sure if the solution would be to add some checks to this method to see if the broker accepted the user's token, and somehow make the NewClient function return an error. and close the connection.

Also, not sure if the current workflow is expected, and the client needs to check something before start talking to kafka,

@mk6i
Copy link
Contributor

mk6i commented Jul 15, 2019

@d1egoaz Thanks for raising this issue. I believe it's due to the Sarama client not properly closing the token exchange as described by https://tools.ietf.org/html/rfc7628#section-4.3. I will work on fixing this.

@mk6i
Copy link
Contributor

mk6i commented Jul 17, 2019

@d1egoaz feel free to close the issue if the fix merged to master solves your problem.

@d1egoaz
Copy link
Contributor Author

d1egoaz commented Jul 17, 2019

that's great @mkaminski1988
Please let me find some time this week to test the fix

Thanks you very much for this!

@d1egoaz
Copy link
Contributor Author

d1egoaz commented Jul 17, 2019

Just tested the new version and it works great!
kafka_client_test.go:194: couldn't connect to kafka, kafka server: SASL Authentication failed.

Thanks again!

@d1egoaz d1egoaz closed this as completed Jul 17, 2019
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

2 participants