Package org.apache.hadoop.security
Class UserGroupInformation
java.lang.Object
org.apache.hadoop.security.UserGroupInformation
User and group information for Hadoop.
This class wraps around a JAAS Subject and provides methods to determine the
user's username and groups. It supports both the Windows, Unix and Kerberos
login modules.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumexisting types of authentications' methodsstatic classorg.apache.hadoop.security.UserGroupInformation.HadoopLoginModuleA login module that looks at the Kerberos, Unix, or Windows principal and adds the corresponding UserName. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCredentials(Credentials credentials) Add the given Credentials to this user.booleanaddToken(Text alias, Token<? extends TokenIdentifier> token) Add a named token to this UGIbooleanaddToken(Token<? extends TokenIdentifier> token) Add a token to this UGIbooleanaddTokenIdentifier(TokenIdentifier tokenId) Add a TokenIdentifier to this UGI.voidRe-login a user from keytab if TGT is expired or is close to expiry.static UserGroupInformationcreateProxyUser(String user, UserGroupInformation realUser) Create a proxy user using username of the effective user and the ugi of the real user.static UserGroupInformationcreateProxyUserForTesting(String user, UserGroupInformation realUser, String[] userGroups) Create a proxy user UGI for testing HDFS and MapReducestatic UserGroupInformationcreateRemoteUser(String user) Create a user from a login name.static UserGroupInformationcreateRemoteUser(String user, SaslRpcServer.AuthMethod authMethod) Create a user from a login name.static UserGroupInformationcreateUserForTesting(String user, String[] userGroups) Create a UGI for testing HDFS and MapReduce<T> TdoAs(PrivilegedAction<T> action) Run the given action as the user.<T> TdoAs(PrivilegedExceptionAction<T> action) Run the given action as the user, potentially throwing an exception.booleanCompare the subjects to see if they are equal to each other.voidForce re-Login a user in from a keytab file irrespective of the last login time.voidForce re-Login a user in from the ticket cache irrespective of the last login time.Get the authentication method from the subjectstatic UserGroupInformationgetBestUGI(String ticketCachePath, String user) Find the most appropriate UserGroupInformation to useObtain the tokens in credentials form associated with this user.static UserGroupInformationReturn the current user, including any doAs in the current stack.String[]Get the group names for this user.Deprecated.Get the groups names for the user as a Set.static UserGroupInformationGet the currently logged in user.Get the authentication method from the real user's subject.Returns the authentication method of a ugi.get RealUser (vs.static UserGroupInformationIf this is a proxy user, get the real user.Get the user's login name.protected SubjectGet the underlying subject from this ugi.Get the set of TokenIdentifiers belonging to this UGICollection<Token<? extends TokenIdentifier>>Obtain the collection of tokens associated with this user.static UserGroupInformationgetUGIFromSubject(Subject subject) Create a UserGroupInformation from a Subject with Kerberos principal.static UserGroupInformationgetUGIFromTicketCache(String ticketCache, String user) Create a UserGroupInformation from a Kerberos ticket cache.Get the user's full principal name.inthashCode()Return the hash of the subject.booleanchecks if logged in using kerberosbooleanIs this user logged in from a keytab file managed by the UGI?static booleanstatic booleanDid the login happen via keytab.booleanThis method checks for a successful Kerberos login and returns true by default if it is not using Kerberos.static booleanDid the login happen via ticket cache.static booleanDetermine if UserGroupInformation is using Kerberos to determine user identities or is relying on simple authenticationstatic voidLog all (current, real, login) UGI and token info into UGI debug log.static voidlogAllUserInfo(org.slf4j.Logger log, UserGroupInformation ugi) Log all (current, real, login) UGI and token info into specified log.static voidloginUserFromKeytab(String user, String path) Log a user in from a keytab file.static UserGroupInformationloginUserFromKeytabAndReturnUGI(String user, String path) Log a user in from a keytab file.static voidloginUserFromSubject(Subject subject) Log in a user using the given subjectvoidLog the current user out who previously logged in using keytab.static voidlogUserInfo(org.slf4j.Logger log, String caption, UserGroupInformation ugi) Log current UGI and token information into specified log.static voidA test method to print out the current user's UGI.static voidReattach the class's metrics to a new metric system.voidRe-Login a user in from a keytab file.voidRe-Login a user in from the ticket cache.voidremoveToken(Text alias) Remove a named token from this UGI.static voidreset()voidsetAuthenticationMethod(SaslRpcServer.AuthMethod authMethod) Sets the authentication method in the subjectvoidSets the authentication method in the subjectstatic voidSet the static configuration for UGI.static voidsetLoginUser(UserGroupInformation ugi) static voidsetShouldRenewImmediatelyForTests(boolean immediate) For the purposes of unit tests, we want to test login from keytab and don't want to wait until the renew window (controlled by TICKET_RENEW_WINDOW).booleanshouldRelogin()toString()Return the username.static StringtrimLoginMethod(String userName) remove the login method that is followed by a space from the username e.g.
-
Field Details
-
HADOOP_TOKEN_FILE_LOCATION
Environment variable pointing to the token cache file- See Also:
-
HADOOP_TOKEN
Environment variable pointing to the base64 tokens.- See Also:
-
-
Method Details
-
setShouldRenewImmediatelyForTests
@VisibleForTesting public static void setShouldRenewImmediatelyForTests(boolean immediate) For the purposes of unit tests, we want to test login from keytab and don't want to wait until the renew window (controlled by TICKET_RENEW_WINDOW).- Parameters:
immediate- true if we should login without waiting for ticket window
-
reattachMetrics
public static void reattachMetrics()Reattach the class's metrics to a new metric system. -
isInitialized
public static boolean isInitialized() -
setConfiguration
Set the static configuration for UGI. In particular, set the security authentication mechanism and the group look up service.- Parameters:
conf- the configuration to use
-
reset
@Private @VisibleForTesting public static void reset() -
isSecurityEnabled
public static boolean isSecurityEnabled()Determine if UserGroupInformation is using Kerberos to determine user identities or is relying on simple authentication- Returns:
- true if UGI is working in a secure environment
-
isLoginSuccess
public boolean isLoginSuccess()This method checks for a successful Kerberos login and returns true by default if it is not using Kerberos.- Returns:
- true on successful login
-
hasKerberosCredentials
public boolean hasKerberosCredentials()checks if logged in using kerberos- Returns:
- true if the subject logged via keytab or has a Kerberos TGT
-
getCurrentUser
Return the current user, including any doAs in the current stack.- Returns:
- the current user
- Throws:
IOException- if login fails
-
getBestUGI
public static UserGroupInformation getBestUGI(String ticketCachePath, String user) throws IOException Find the most appropriate UserGroupInformation to use- Parameters:
ticketCachePath- The Kerberos ticket cache path, or NULL if none is specfieduser- The user name, or NULL if none is specified.- Returns:
- The most appropriate UserGroupInformation
- Throws:
IOException- raised on errors performing I/O.
-
getUGIFromTicketCache
@Public @Evolving public static UserGroupInformation getUGIFromTicketCache(String ticketCache, String user) throws IOException Create a UserGroupInformation from a Kerberos ticket cache.- Parameters:
user- The principal name to load from the ticket cacheticketCache- the path to the ticket cache file- Returns:
- UserGroupInformation.
- Throws:
IOException- if the kerberos login fails
-
getUGIFromSubject
Create a UserGroupInformation from a Subject with Kerberos principal.- Parameters:
subject- The KerberosPrincipal to use in UGI. The creator of subject is responsible for renewing credentials.- Returns:
- UserGroupInformation
- Throws:
IOException- raised on errors performing I/O.KerberosAuthException- if the kerberos login fails
-
getLoginUser
Get the currently logged in user. If no explicit login has occurred, the user will automatically be logged in with either kerberos credentials if available, or as the local OS user, based on security settings.- Returns:
- the logged in user
- Throws:
IOException- if login fails
-
trimLoginMethod
remove the login method that is followed by a space from the username e.g. "jack (auth:SIMPLE)" -> "jack"- Parameters:
userName- userName.- Returns:
- userName without login method
-
loginUserFromSubject
Log in a user using the given subject- Parameters:
subject- the subject to use when logging in a user, or null to create a new subject. If subject is not null, the creator of subject is responsible for renewing credentials.- Throws:
IOException- if login fails
-
setLoginUser
-
isFromKeytab
public boolean isFromKeytab()Is this user logged in from a keytab file managed by the UGI?- Returns:
- true if the credentials are from a keytab file.
-
shouldRelogin
@Private @Unstable public boolean shouldRelogin() -
loginUserFromKeytab
@Public @Evolving public static void loginUserFromKeytab(String user, String path) throws IOException Log a user in from a keytab file. Loads a user identity from a keytab file and logs them in. They become the currently logged-in user.- Parameters:
user- the principal name to load from the keytabpath- the path to the keytab file- Throws:
IOException- raised on errors performing I/O.KerberosAuthException- if it's a kerberos login exception.
-
logoutUserFromKeytab
Log the current user out who previously logged in using keytab. This method assumes that the user logged in by callingloginUserFromKeytab(String, String).- Throws:
IOException- raised on errors performing I/O.KerberosAuthException- if a failure occurred in logout, or if the user did not log in by invoking loginUserFromKeyTab() before.
-
checkTGTAndReloginFromKeytab
Re-login a user from keytab if TGT is expired or is close to expiry.- Throws:
IOException- raised on errors performing I/O.KerberosAuthException- if it's a kerberos login exception.
-
reloginFromKeytab
Re-Login a user in from a keytab file. Loads a user identity from a keytab file and logs them in. They become the currently logged-in user. This method assumes thatloginUserFromKeytab(String, String)had happened already. The Subject field of this UserGroupInformation object is updated to have the new credentials.- Throws:
IOException- raised on errors performing I/O.KerberosAuthException- on a failure
-
forceReloginFromKeytab
Force re-Login a user in from a keytab file irrespective of the last login time. Loads a user identity from a keytab file and logs them in. They become the currently logged-in user. This method assumes thatloginUserFromKeytab(String, String)had happened already. The Subject field of this UserGroupInformation object is updated to have the new credentials.- Throws:
IOException- raised on errors performing I/O.KerberosAuthException- on a failure
-
forceReloginFromTicketCache
Force re-Login a user in from the ticket cache irrespective of the last login time. This method assumes that login had happened already. The Subject field of this UserGroupInformation object is updated to have the new credentials.- Throws:
IOException- raised on errors performing I/O.KerberosAuthException- on a failure
-
reloginFromTicketCache
Re-Login a user in from the ticket cache. This method assumes that login had happened already. The Subject field of this UserGroupInformation object is updated to have the new credentials.- Throws:
IOException- raised on errors performing I/O.KerberosAuthException- on a failure
-
loginUserFromKeytabAndReturnUGI
public static UserGroupInformation loginUserFromKeytabAndReturnUGI(String user, String path) throws IOException Log a user in from a keytab file. Loads a user identity from a keytab file and login them in. This new user does not affect the currently logged-in user.- Parameters:
user- the principal name to load from the keytabpath- the path to the keytab file- Returns:
- UserGroupInformation.
- Throws:
IOException- if the keytab file can't be read
-
isLoginKeytabBased
Did the login happen via keytab.- Returns:
- true or false
- Throws:
IOException- raised on errors performing I/O.
-
isLoginTicketBased
Did the login happen via ticket cache.- Returns:
- true or false
- Throws:
IOException- raised on errors performing I/O.
-
createRemoteUser
Create a user from a login name. It is intended to be used for remote users in RPC, since it won't have any credentials.- Parameters:
user- the full user principal name, must not be empty or null- Returns:
- the UserGroupInformation for the remote user.
-
createRemoteUser
@Public @Evolving public static UserGroupInformation createRemoteUser(String user, SaslRpcServer.AuthMethod authMethod) Create a user from a login name. It is intended to be used for remote users in RPC, since it won't have any credentials.- Parameters:
user- the full user principal name, must not be empty or nullauthMethod- authMethod.- Returns:
- the UserGroupInformation for the remote user.
-
createProxyUser
@Public @Evolving public static UserGroupInformation createProxyUser(String user, UserGroupInformation realUser) Create a proxy user using username of the effective user and the ugi of the real user.- Parameters:
user- user.realUser- realUser.- Returns:
- proxyUser ugi
-
getRealUser
get RealUser (vs. EffectiveUser)- Returns:
- realUser running over proxy user
-
getRealUserOrSelf
If this is a proxy user, get the real user. Otherwise, return this user.- Parameters:
user- the user to check- Returns:
- the real user or self
-
createUserForTesting
@Public @Evolving public static UserGroupInformation createUserForTesting(String user, String[] userGroups) Create a UGI for testing HDFS and MapReduce- Parameters:
user- the full user principal nameuserGroups- the names of the groups that the user belongs to- Returns:
- a fake user for running unit tests
-
createProxyUserForTesting
public static UserGroupInformation createProxyUserForTesting(String user, UserGroupInformation realUser, String[] userGroups) Create a proxy user UGI for testing HDFS and MapReduce- Parameters:
user- the full user principal name for effective userrealUser- UGI of the real useruserGroups- the names of the groups that the user belongs to- Returns:
- a fake user for running unit tests
-
getShortUserName
Get the user's login name.- Returns:
- the user's name up to the first '/' or '@'.
-
getPrimaryGroupName
- Throws:
IOException
-
getUserName
Get the user's full principal name.- Returns:
- the user's full principal name.
-
addTokenIdentifier
Add a TokenIdentifier to this UGI. The TokenIdentifier has typically been authenticated by the RPC layer as belonging to the user represented by this UGI.- Parameters:
tokenId- tokenIdentifier to be added- Returns:
- true on successful add of new tokenIdentifier
-
getTokenIdentifiers
Get the set of TokenIdentifiers belonging to this UGI- Returns:
- the set of TokenIdentifiers belonging to this UGI
-
addToken
Add a token to this UGI- Parameters:
token- Token to be added- Returns:
- true on successful add of new token
-
addToken
Add a named token to this UGI- Parameters:
alias- Name of the tokentoken- Token to be added- Returns:
- true on successful add of new token
-
removeToken
Remove a named token from this UGI.- Parameters:
alias- Name of the token
-
getTokens
Obtain the collection of tokens associated with this user.- Returns:
- an unmodifiable collection of tokens associated with user
-
getCredentials
Obtain the tokens in credentials form associated with this user.- Returns:
- Credentials of tokens associated with this user
-
addCredentials
Add the given Credentials to this user.- Parameters:
credentials- of tokens and secrets
-
getGroupNames
Get the group names for this user.getGroupsSet()is less expensive alternative when checking for a contained element.- Returns:
- the list of users with the primary group first. If the command fails, it returns an empty list.
-
getGroups
Deprecated.UsegetGroupsSet()instead.Get the group names for this user.getGroupsSet()is less expensive alternative when checking for a contained element.- Returns:
- the list of users with the primary group first. If the command fails, it returns an empty list.
-
getGroupsSet
Get the groups names for the user as a Set.- Returns:
- the set of users with the primary group first. If the command fails, it returns an empty set.
-
toString
Return the username. -
setAuthenticationMethod
Sets the authentication method in the subject- Parameters:
authMethod- authMethod.
-
setAuthenticationMethod
Sets the authentication method in the subject- Parameters:
authMethod- authMethod.
-
getAuthenticationMethod
Get the authentication method from the subject- Returns:
- AuthenticationMethod in the subject, null if not present.
-
getRealAuthenticationMethod
Get the authentication method from the real user's subject. If there is no real user, return the given user's authentication method.- Returns:
- AuthenticationMethod in the subject, null if not present.
-
getRealAuthenticationMethod
public static UserGroupInformation.AuthenticationMethod getRealAuthenticationMethod(UserGroupInformation ugi) Returns the authentication method of a ugi. If the authentication method is PROXY, returns the authentication method of the real user.- Parameters:
ugi- ugi.- Returns:
- AuthenticationMethod
-
equals
Compare the subjects to see if they are equal to each other. -
hashCode
public int hashCode()Return the hash of the subject. -
getSubject
Get the underlying subject from this ugi.- Returns:
- the subject that represents this user.
-
doAs
Run the given action as the user.- Type Parameters:
T- the return type of the run method- Parameters:
action- the method to execute- Returns:
- the value from the run method
-
doAs
@Public @Evolving public <T> T doAs(PrivilegedExceptionAction<T> action) throws IOException, InterruptedException Run the given action as the user, potentially throwing an exception.- Type Parameters:
T- the return type of the run method- Parameters:
action- the method to execute- Returns:
- the value from the run method
- Throws:
IOException- if the action throws an IOExceptionError- if the action throws an ErrorRuntimeException- if the action throws a RuntimeExceptionInterruptedException- if the action throws an InterruptedExceptionUndeclaredThrowableException- if the action throws something else
-
logUserInfo
@LimitedPrivate({"HDFS","KMS"}) @Unstable public static void logUserInfo(org.slf4j.Logger log, String caption, UserGroupInformation ugi) Log current UGI and token information into specified log.- Parameters:
ugi- - UGIlog- log.caption- caption.
-
logAllUserInfo
@LimitedPrivate({"HDFS","KMS"}) @Unstable public static void logAllUserInfo(org.slf4j.Logger log, UserGroupInformation ugi) throws IOException Log all (current, real, login) UGI and token info into specified log.- Parameters:
ugi- - UGIlog- - log.- Throws:
IOException- raised on errors performing I/O.
-
logAllUserInfo
Log all (current, real, login) UGI and token info into UGI debug log.- Parameters:
ugi- - UGI- Throws:
IOException- raised on errors performing I/O.
-
main
A test method to print out the current user's UGI.- Parameters:
args- if there are two arguments, read the user from the keytab and print it out.- Throws:
Exception- Exception.
-
getGroupsSet()instead.