You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "showuon (via GitHub)" <gi...@apache.org> on 2024/01/29 12:34:19 UTC

[PR] MINOR: add client info in authentication error log [kafka]

showuon opened a new pull request, #15280:
URL: https://github.com/apache/kafka/pull/15280

   When client authenticate failed, the server will log with: the client IP address only. The the IP address sometimes cannot represent a specific user, especially if there is proxy between client and server. Ex:
   
   ```
   INFO [SocketServer listenerType=ZK_BROKER, nodeId=0] Failed authentication with /127.0.0.1 (channelId=127.0.0.1:9093-127.0.0.1:53223-5) (Authentication failed: Invalid username or password) (org.apache.kafka.common.network.Selector)
   ```
   
   If there are many failed authentication log appeared in the server, it'd be better to identify who is triggering it. Adding the client info to the log is a good start. This PR adds the user info in the log if any. After this PR, the log will be like this:
   ```
   INFO [SocketServer listenerType=ZK_BROKER, nodeId=0] Failed authentication with /127.0.0.1 (channelId=127.0.0.1:9093-127.0.0.1:54700-0) (Authentication failed: Invalid username or password for user {abc}) (org.apache.kafka.common.network.Selector)
   ```
   
   No tests added because only log info added.
   
   ### Committer Checklist (excluded from commit message)
   - [ ] Verify design and implementation 
   - [ ] Verify test coverage and CI build status
   - [ ] Verify documentation (including upgrade notes)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: add client info in authentication error log [kafka]

Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #15280:
URL: https://github.com/apache/kafka/pull/15280#discussion_r1469531952


##########
clients/src/main/java/org/apache/kafka/common/security/plain/internals/PlainSaslServer.java:
##########
@@ -93,20 +93,20 @@ public byte[] evaluateResponse(byte[] responseBytes) throws SaslAuthenticationEx
             throw new SaslAuthenticationException("Authentication failed: username not specified");
         }
         if (password.isEmpty()) {
-            throw new SaslAuthenticationException("Authentication failed: password not specified");
+            throw new SaslAuthenticationException(String.format("Authentication failed: password not specified for user {%s}", username));

Review Comment:
   After L93, we confirmed we have `username`. Adding it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: add client info in authentication error log [kafka]

Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #15280:
URL: https://github.com/apache/kafka/pull/15280#discussion_r1469530712


##########
clients/src/main/java/org/apache/kafka/common/security/oauthbearer/internals/OAuthBearerSaslServer.java:
##########
@@ -159,10 +159,15 @@ private byte[] process(String tokenValue, String authorizationId, SaslExtensions
         }
         OAuthBearerToken token = callback.token();
         if (token == null) {
-            errorMessage = jsonErrorResponse(callback.errorStatus(), callback.errorScope(),
+            String returnedMessage = jsonErrorResponse(callback.errorStatus(), callback.errorScope(),
                     callback.errorOpenIDConfiguration());
+            if (!authorizationId.isEmpty()) {
+                errorMessage = String.format("The authorizationId {%s}: %s", authorizationId, returnedMessage);
+            } else {
+                errorMessage = returnedMessage;
+            }

Review Comment:
   The `returnedMessage` is the standard json data for response. The `errorMessage` is the message to throw in exception, so adding authorizationId is provided.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-16214: add client info in authentication error log [kafka]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #15280: KAFKA-16214: add client info in authentication error log
URL: https://github.com/apache/kafka/pull/15280


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-16214: add client info in authentication error log [kafka]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #15280:
URL: https://github.com/apache/kafka/pull/15280#issuecomment-2570032656

   This PR has been closed since it has not had any activity in 120 days. If you feel like this
   was a mistake, or you would like to continue working on it, please feel free to re-open the 
   PR and ask for a review.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: add client info in authentication error log [kafka]

Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #15280:
URL: https://github.com/apache/kafka/pull/15280#discussion_r1469531952


##########
clients/src/main/java/org/apache/kafka/common/security/plain/internals/PlainSaslServer.java:
##########
@@ -93,20 +93,20 @@ public byte[] evaluateResponse(byte[] responseBytes) throws SaslAuthenticationEx
             throw new SaslAuthenticationException("Authentication failed: username not specified");
         }
         if (password.isEmpty()) {
-            throw new SaslAuthenticationException("Authentication failed: password not specified");
+            throw new SaslAuthenticationException(String.format("Authentication failed: password not specified for user {%s}", username));

Review Comment:
   After L93, we confirmed we have `username`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-16214: add client info in authentication error log [kafka]

Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on PR #15280:
URL: https://github.com/apache/kafka/pull/15280#issuecomment-1921205131

   @ijuma @mimaison , please take a look when available. Thanks.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-16214: add client info in authentication error log [kafka]

Posted by "jeffkbkim (via GitHub)" <gi...@apache.org>.
jeffkbkim commented on code in PR #15280:
URL: https://github.com/apache/kafka/pull/15280#discussion_r1498526867


##########
clients/src/main/java/org/apache/kafka/common/utils/Utils.java:
##########
@@ -1675,6 +1677,20 @@ public static <V> Map<String, V> entriesWithPrefix(Map<String, V> map, String pr
         return result;
     }
 
+    /**
+     * Sanitize the string by limiting the max length and trim the spaces (i.e. \s\r\n\t)
+     * @param str the original string
+     * @param len the max length of the string
+     * @return sanitized string
+     */
+    public static String sanitizeString(String str, int len) {

Review Comment:
   i'm wondering whether we can have
   ```
       public static String sanitizeString(String str) {
           return sanitizeString(str, USERNAME_MAX_LEN);
       }
   ```
   and use this instead of passing USERNAME_MAX_LEN across all invocations



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: add client info in authentication error log [kafka]

Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #15280:
URL: https://github.com/apache/kafka/pull/15280#discussion_r1469537246


##########
clients/src/main/java/org/apache/kafka/common/security/scram/internals/ScramSaslServer.java:
##########
@@ -127,10 +127,10 @@ public byte[] evaluateResponse(byte[] response) throws SaslException, SaslAuthen
                         }
                         this.scramCredential = credentialCallback.scramCredential();
                         if (scramCredential == null)
-                            throw new SaslException("Authentication failed: Invalid user credentials");
+                            throw new SaslException(String.format("Authentication failed: Invalid user {%s} credentials", username));
                         String authorizationIdFromClient = clientFirstMessage.authorizationId();
                         if (!authorizationIdFromClient.isEmpty() && !authorizationIdFromClient.equals(username))
-                            throw new SaslAuthenticationException("Authentication failed: Client requested an authorization id that is different from username");
+                            throw new SaslAuthenticationException(String.format("Authentication failed: Client requested an authorization id {%s} that is different from username {%s}", authorizationIdFromClient, username));

Review Comment:
   The username is got in L111. Adding it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-16214: add client info in authentication error log [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #15280:
URL: https://github.com/apache/kafka/pull/15280#discussion_r1511627095


##########
clients/src/main/java/org/apache/kafka/common/security/scram/internals/ScramSaslServer.java:
##########
@@ -143,7 +146,7 @@ public byte[] evaluateResponse(byte[] response) throws SaslException, SaslAuthen
                     } catch (SaslException | AuthenticationException e) {
                         throw e;
                     } catch (Throwable e) {
-                        throw new SaslException("Authentication failed: Credentials could not be obtained", e);
+                        throw new SaslException("Authentication failed: Credentials could not be obtained" + (username == null ? "" : " for user " + Utils.sanitizeString(username, USERNAME_MAX_LEN)), e);

Review Comment:
   why we handle null `username` here? the previous invocation of `Utils.sanitizeString` does not.



##########
clients/src/main/java/org/apache/kafka/common/security/scram/internals/ScramSaslServer.java:
##########
@@ -116,7 +118,7 @@ public byte[] evaluateResponse(byte[] response) throws SaslException, SaslAuthen
                             credentialCallback = tokenCallback;
                             callbackHandler.handle(new Callback[]{nameCallback, tokenCallback});
                             if (tokenCallback.tokenOwner() == null)
-                                throw new SaslException("Token Authentication failed: Invalid tokenId : " + username);
+                                throw new SaslException("Token Authentication failed: Invalid tokenId : " + Utils.sanitizeString(username, USERNAME_MAX_LEN));

Review Comment:
   How about "Token Authentication failed: Invalid tokenId from username "



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-16214: add client info in authentication error log [kafka]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #15280:
URL: https://github.com/apache/kafka/pull/15280#issuecomment-2144213214

   This PR is being marked as stale since it has not had any activity in 90 days. If you would like to keep this PR alive, please ask a committer for review. If the PR has  merge conflicts, please update it with the latest from trunk (or appropriate release branch) <p> If this PR is no longer valid or desired, please feel free to close it. If no activity occurs in the next 30 days, it will be automatically closed.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: add client info in authentication error log [kafka]

Posted by "showuon (via GitHub)" <gi...@apache.org>.
showuon commented on code in PR #15280:
URL: https://github.com/apache/kafka/pull/15280#discussion_r1469542360


##########
clients/src/main/java/org/apache/kafka/common/security/scram/internals/ScramSaslServer.java:
##########
@@ -143,7 +143,7 @@ public byte[] evaluateResponse(byte[] response) throws SaslException, SaslAuthen
                     } catch (SaslException | AuthenticationException e) {
                         throw e;
                     } catch (Throwable e) {
-                        throw new SaslException("Authentication failed: Credentials could not be obtained", e);
+                        throw new SaslException("Authentication failed: Credentials could not be obtained" + (username == null ? "" : " for user " + username), e);

Review Comment:
   From the point the below, we're unsure if `username` is already set or not, so only print it when username has value.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org