aratno commented on code in PR #3728:
URL: https://github.com/apache/cassandra/pull/3728#discussion_r1896150290
##########
src/java/org/apache/cassandra/audit/AuditLogManager.java:
##########
@@ -400,4 +419,104 @@ 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)
+ {
+ String argsFmt = "";
+ if (args != null)
+ argsFmt =
Arrays.stream(args).map(Objects::toString).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 onFailure(Subject subject, Method method, Object[] args,
String reason)
+ {
+ if (filter.isFiltered(AuditLogEntryCategory.JMX))
+ return;
+
+ AuditLogEntry entry = new AuditLogEntry.Builder(JMX)
+ .setOperation(String.format("JMX FAILURE: %s due
to %s", JmxFormatter.method(method, args), reason))
+ .setUser(JmxFormatter.user(subject))
+ .build();
+ log(entry);
+ }
+
+
+ private class JmxHandler implements InvocationHandler
+ {
+ private MBeanServer mbs = null;
+
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
+ {
+ // See AuthorizationProxy.invoke
+ if (("setMBeanServer").equals(method.getName()))
+ {
+ if (args[0] == null)
+ throw new IllegalArgumentException("Null MBeanServer");
+
+ if (mbs != null)
+ throw new IllegalArgumentException("MBeanServer already
initialized");
+
+ mbs = (MBeanServer) args[0];
+ return null;
+ }
+
+ AccessControlContext acc = AccessController.getContext();
+ Subject subject = Subject.getSubject(acc);
+
+ try
+ {
+ Object invoke = method.invoke(mbs, args);
+ AuditLogManager.this.onInvocation(subject, method, args);
+ return invoke;
+ }
+ catch (InvocationTargetException e)
+ {
+ Throwable cause = e.getCause();
+ AuditLogManager.instance.onFailure(subject, method, args,
cause.getMessage());
+ throw cause;
+ }
+ }
+ }
+
+ private MBeanServerForwarder createMBeanServerForwarder()
+ {
+ final InvocationHandler handler = new JmxHandler();
+ final Class<?>[] interfaces = { MBeanServerForwarder.class };
Review Comment:
Can do, this was copied from JMXServerUtils
--
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]