You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "michaeljmarshall (via GitHub)" <gi...@apache.org> on 2023/04/11 01:57:50 UTC

[GitHub] [pulsar] michaeljmarshall commented on a diff in pull request #20030: [fix][metrics] Fix the reason label of authentication metrics

michaeljmarshall commented on code in PR #20030:
URL: https://github.com/apache/pulsar/pull/20030#discussion_r1162217415


##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/authentication/AuthenticationProviderToken.java:
##########
@@ -157,8 +158,7 @@ public String authenticate(AuthenticationDataSource authData) throws Authenticat
             AuthenticationMetrics.authenticateSuccess(getClass().getSimpleName(), getAuthMethodName());
             return role;
         } catch (AuthenticationException exception) {
-            AuthenticationMetrics.authenticateFailure(getClass().getSimpleName(), getAuthMethodName(),
-                    exception.getMessage());
+            AuthenticationMetrics.authenticateFailure(getClass().getSimpleName(), getAuthMethodName(), exception);

Review Comment:
   I think it would be valuable to increment the metric outside of the implementations of the `AuthenticationProvider` so that all custom implementations can integrate with this feature.



##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/authentication/PulsarAuthenticationException.java:
##########
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.authentication;
+
+import javax.naming.AuthenticationException;
+import lombok.Getter;
+
+/**
+ * PulsarAuthenticationException indicates Authentication exception in Pulsar, it contains an error code.
+ */
+public class PulsarAuthenticationException extends AuthenticationException {
+
+    public enum ErrorCode {
+        UNKNOWN,
+        PROVIDER_LIST_AUTH_REQUIRED,
+        BASIC_INVALID_TOKEN,
+        BASIC_INVALID_AUTH_DATA,
+        AUTHZ_NO_CLIENT,
+        AUTHZ_NO_TOKEN,
+        AUTHZ_NO_PUBLIC_KEY,
+        AUTHZ_DOMAIN_MISMATCH,
+        AUTHZ_INVALID_TOKEN,
+        TLS_NO_CERTS,
+        TLS_NO_CN, // cn: common name
+        TOKEN_INVALID_HEADER,
+        TOKEN_NO_AUTH_DATA,
+        TOKEN_EMPTY_TOKEN,
+        TOKEN_INVALID_TOKEN,
+        TOKEN_INVALID_AUDIENCES,
+    }

Review Comment:
   My primary concern with relying on this enum is that it is not easily extended. For example, I just merged the OIDC Provider with the following enum:
   
   https://github.com/apache/pulsar/blob/bafecb2a9c0e73942de6a38df72dd5888d5afd66/pulsar-broker-auth-oidc/src/main/java/org/apache/pulsar/broker/authentication/oidc/AuthenticationExceptionCode.java#L25-L38
   
   From your list and from mine, my primary conclusion is that the types of errors are often provider specific, so a static enum that cannot be extended feels restrictive.
   
   I agree that it is extremely important to limit the cardinality of these metrics, but I also think we want to make it easily extensible by users.
   
   I think an alternative solution could work by making `PulsarAuthenticationException` abstract and letting each provider implement their own exception. Then, the provider itself could have its own enum, and when the framework calls `getErrorCode`, the implementation will convert its own enum into a string.



##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/authentication/metrics/AuthenticationMetrics.java:
##########
@@ -43,11 +46,36 @@ public static void authenticateSuccess(String providerName, String authMethod) {
 
     /**
      * Log authenticate failure event to the authentication metrics.
+     *
+     * This method is deprecated due to the label "reason" is a potential infinite value.
+     * @deprecated See {@link #authenticateFailure(String, String, ErrorCode)} ()}
+     * or {@link #authenticateFailure(String, String, AuthenticationException)}
+     *
      * @param providerName The short class name of the provider
      * @param authMethod Authentication method name.
      * @param reason Failure reason.
      */
+    @Deprecated
     public static void authenticateFailure(String providerName, String authMethod, String reason) {
         authFailuresMetrics.labels(providerName, authMethod, reason).inc();
     }
+
+    public static void authenticateFailure(String providerName, String authMethod,
+                                           AuthenticationException exception) {

Review Comment:
   Because this is a generic metrics class meant to be useful to all authentication provider implementations, I think we should provide an easy way to provide custom "reasons". I respect that it is easy to provide a reason that is high cardinality, but I think we should expect each implementation to handle those details.



-- 
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: commits-unsubscribe@pulsar.apache.org

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