JeetKunDoug commented on code in PR #3728:
URL: https://github.com/apache/cassandra/pull/3728#discussion_r1890431190
##########
src/java/org/apache/cassandra/audit/AuditLogManager.java:
##########
@@ -398,4 +418,103 @@ else if (e instanceof
PasswordGuardrail.PasswordGuardrailException)
return PasswordObfuscator.obfuscate(e.getMessage());
}
+
+ private static class JmxFormatter
+ {
+ private static String user(Subject subject)
+ {
+ return String.format("%s", subject == null ? null :
subject.getPrincipals().stream().map(Objects::toString).collect(Collectors.joining(",
")));
+ }
+
+ private static String method(Method method, Object[] args)
+ {
+ Function<Object, String> fmt = obj -> {
+ if (obj == null)
+ return "null";
+ return obj.toString();
+ };
+ String argsFmt = "";
+ if (args != null)
+ argsFmt =
Arrays.stream(args).map(fmt).collect(Collectors.joining(", "));
+ return String.format("%s#%s(%s)",
method.getDeclaringClass().getCanonicalName(), method.getName(), argsFmt);
+ }
Review Comment:
Rather than embedding `fmt` in the `method` method as a function object like
this, I'd move it into its own function and just use a method reference in
stead. It avoids creating the function instance each time you format a method
for logging:
```suggestion
private static String method(Method method, Object[] args)
{
String argsFmt = "";
if (args != null)
argsFmt =
Arrays.stream(args).map(JmxFormatter::nullOrToString).collect(Collectors.joining(",
"));
return String.format("%s#%s(%s)",
method.getDeclaringClass().getCanonicalName(), method.getName(), argsFmt);
}
private static String nullOrToString(Object obj)
{
if (obj == null)
return "null";
return obj.toString();
}
```
##########
src/java/org/apache/cassandra/audit/AuditLogManager.java:
##########
@@ -400,4 +418,103 @@ else if (e instanceof
PasswordGuardrail.PasswordGuardrailException)
return PasswordObfuscator.obfuscate(e.getMessage());
}
+
+ private static class JmxFormatter
+ {
+ private static String user(Subject subject)
+ {
+ return String.format("%s", subject == null ? null :
subject.getPrincipals().stream().map(Objects::toString).collect(Collectors.joining(",
")));
+ }
+
+ private static String method(Method method, Object[] args)
+ {
+ Function<Object, String> fmt = obj -> {
+ if (obj == null)
+ return "null";
+ return obj.toString();
+ };
+ String argsFmt = "";
+ if (args != null)
+ argsFmt =
Arrays.stream(args).map(fmt).collect(Collectors.joining(", "));
+ return String.format("%s#%s(%s)",
method.getDeclaringClass().getCanonicalName(), method.getName(), argsFmt);
+ }
+ }
+
+ @Override
+ public void onInvocation(Subject subject, Method method, Object[] args)
+ {
+ if (filter.isFiltered(AuditLogEntryCategory.JMX))
+ return;
+
+ AuditLogEntry entry = new AuditLogEntry.Builder(JMX)
+ .setOperation(String.format("JMX INVOCATION:
%s", JmxFormatter.method(method, args)))
+ .setUser(JmxFormatter.user(subject))
+ .build();
+ log(entry);
+ }
+
+ @Override
+ public void onRejection(Subject subject, Method method, Object[] args,
String reason)
+ {
+ if (filter.isFiltered(AuditLogEntryCategory.JMX))
+ return;
+
+ AuditLogEntry entry = new AuditLogEntry.Builder(JMX)
+ .setOperation(String.format("JMX REJECTION: %s
due to %s", JmxFormatter.method(method, args), reason))
Review Comment:
Question - is this "rejection" or just more generic "failure"? Rejection
makes it sound like the only reason for a failure is because we have the
AuthorizationProxy _also_ installed and it rejected the request, but I think
there are lots of other reasons why this may fail. If it's more generic, I'd
rename the method and update the string here.
##########
src/java/org/apache/cassandra/audit/AuditLogFilter.java:
##########
@@ -126,6 +126,20 @@ private IncludeExcludeHolder(ImmutableSet<String>
includedSet, ImmutableSet<Stri
}
}
+ /**
+ * Checks whether a give AuditLogEntryCategory is filtered or not.
Review Comment:
NIT (spelling):
```suggestion
* Checks whether a given AuditLogEntryCategory is filtered or not.
```
Actually applies to the pre-existing comment below as well.
##########
test/unit/org/apache/cassandra/audit/InMemoryAuditLogger.java:
##########
@@ -25,7 +25,8 @@
public class InMemoryAuditLogger implements IAuditLogger
{
- final Queue<AuditLogEntry> inMemQueue = new LinkedList<>();
+ @VisibleForTesting
+ public final Queue<AuditLogEntry> inMemQueue = new LinkedList<>();
Review Comment:
Is there a reason you can't use the `internalQueue()` method below to do
this? Although I suppose our code style does say to prefer public final members
to getters, so maybe we should get rid of the method and just inline it
everywhere? Regardless, let's pick one way or the other and be consistent.
##########
src/java/org/apache/cassandra/utils/JmxInvocationListener.java:
##########
@@ -0,0 +1,31 @@
+/*
+ * 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.cassandra.utils;
+
+import java.lang.reflect.Method;
+import javax.security.auth.Subject;
+
+public interface JmxInvocationListener
+{
+ default void onInvocation(Subject subject, Method method, Object[] args)
+ {}
+
+ default void onRejection(Subject subject, Method method, Object[] args,
String reason)
Review Comment:
Same comment about rejection vs. failure from AuditLogManager - I think this
is just `onFailure`?
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]