-
Notifications
You must be signed in to change notification settings - Fork 395
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
[ISSUE-1206] improvement: Use the real user for audit information #1258
Changes from all commits
89dd66a
0969cb4
f9d9134
5a19916
b20c7fd
391042a
ab524ab
aca126b
854669b
2167435
320ca46
12f5c93
989d850
50127a1
fc8d3fd
0b1b3a9
9896175
ba2384c
9911e19
4a4bafd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2023 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
|
||
package com.datastrato.gravitino.utils; | ||
|
||
import com.datastrato.gravitino.UserPrincipal; | ||
import com.datastrato.gravitino.auth.AuthConstants; | ||
import com.google.common.base.Throwables; | ||
import java.security.Principal; | ||
import java.security.PrivilegedActionException; | ||
import java.security.PrivilegedExceptionAction; | ||
import javax.security.auth.Subject; | ||
|
||
@SuppressWarnings("removal") | ||
public class PrincipalUtils { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I refer to the doAs and getCurrentUser from Hadoop UserInformation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This api will be removed in the future. But JDK doesn't provide alternative solution. Hadoop will encounter the similar situation. I add the anotation to avoid the failure of compile. |
||
private PrincipalUtils() {} | ||
|
||
public static <T> T doAs(Principal principal, PrivilegedExceptionAction<T> action) | ||
throws Exception { | ||
try { | ||
Subject subject = new Subject(); | ||
subject.getPrincipals().add(principal); | ||
return Subject.doAs(subject, action); | ||
} catch (PrivilegedActionException pae) { | ||
Throwable cause = pae.getCause(); | ||
Throwables.propagateIfPossible(cause, Exception.class); | ||
throw new RuntimeException("doAs method occurs an unexpected exception", pae); | ||
} | ||
} | ||
|
||
public static Principal getCurrentPrincipal() { | ||
java.security.AccessControlContext context = java.security.AccessController.getContext(); | ||
Subject subject = Subject.getSubject(context); | ||
if (subject == null || subject.getPrincipals(UserPrincipal.class).isEmpty()) { | ||
return new UserPrincipal(AuthConstants.ANONYMOUS_USER); | ||
} | ||
return subject.getPrincipals(UserPrincipal.class).iterator().next(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2023 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
|
||
package com.datastrato.gravitino.utils; | ||
|
||
import com.datastrato.gravitino.UserPrincipal; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class TestPrincipalUtils { | ||
|
||
@Test | ||
public void testNormal() throws Exception { | ||
UserPrincipal principal = new UserPrincipal("testNormal"); | ||
PrincipalUtils.doAs( | ||
principal, | ||
() -> { | ||
Assertions.assertEquals("testNormal", PrincipalUtils.getCurrentPrincipal().getName()); | ||
return null; | ||
}); | ||
} | ||
|
||
@Test | ||
public void testThread() throws Exception { | ||
UserPrincipal principal = new UserPrincipal("testThread"); | ||
PrincipalUtils.doAs( | ||
principal, | ||
() -> { | ||
Thread thread = | ||
new Thread( | ||
() -> | ||
Assertions.assertEquals( | ||
"testThread", PrincipalUtils.getCurrentPrincipal().getName())); | ||
thread.start(); | ||
thread.join(); | ||
return null; | ||
}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of using
AuthConstants.class.getName()
as an attribute name?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refer to the style of Pulsar. I have added the comment.