Class AuthTokenManager


  • @Service
    @Singleton
    public class AuthTokenManager
    extends Object
    Coordinates generation and consumption of very-limited-use authentication tokens.

    Some DAS commands submit admin commands to be run elsewhere - either in another process on the same host or, via ssh, to another host. Given that it is already executing, the DAS command in progress has already been authenticated (if required). Therefore we want the soon-to-be submitted commands to also be authenticated, but we do not want to send the username and/or password information that was used to authenticate the currently-running DAS command to the other process for it to use.

    Instead, the currently-running DAS command can use this service to obtain a one-time authentication token. The DAS command then includes the token, rather than username/password credentials, in the submitted command.

    This service records which tokens have been given out but not yet used up. When an admin request arrives with a token, the AdminAdapter consults this service to see if the token is valid and, if so, the AdminAdapter allows the request to run.

    We allow each token to be used twice, once for retrieving the command metadata and then the second time to execute the command. (Also see the note below.)

    Tokens have a limited life as measured in time also. If a token is created but not fully consumed before it expires, then this manager considers the token invalid and removes it from the collection of known valid tokens. NOTE Commands that trigger other commands on multiple hosts - such as start-cluster - will need to reuse the authentication token more than twice. For such purposes the code using the token can append a "+" to the token. When such a token is used, this manager does NOT decrement the remaining number of uses. Rather, it only refreshes the token's expiration time.

    Author:
    Tim Quinn
    • Constructor Detail

      • AuthTokenManager

        public AuthTokenManager()
    • Method Detail

      • createToken

        public String createToken​(Subject subject,
                                  long lifetime)
        Creates a new limited use authentication token with the specified lifetime (in ms).
        Parameters:
        subject - the Subject to associate with this token when it is consumed
        lifetime - how long each use of the token extends its lifetime
        Returns:
        auth token
      • createToken

        public String createToken()
        Creates a new limited use authentication token with the default lifetime.
        Returns:
        auth token
      • createToken

        public String createToken​(Subject subject)
        Creates a new limited use authentication token with the given Subject and the default lifetime.
        Parameters:
        subject - the Subject to associated with this token when it is consumed
        Returns:
      • createToken

        public String createToken​(long lifetime)
        Creates a new limited use authentication token with the specified lifetime but no Subject.
        Parameters:
        lifetime - how long each use of the token extends its lifetime
        Returns:
      • findToken

        public Subject findToken​(String token)
        Locates the Subject for the specified token (if any) without consuming the token.

        Use this method only from authentication logic that needs to find the token. Later command processing will consume the token if it is present. This avoids having to force the special admin LoginModule to run even if username/password authentication works.

        Parameters:
        token - the token to find
        Returns:
        Subject for the token; null if the token does not exist;
      • consumeToken

        public Subject consumeToken​(String token)
        Records the use of an authentication token by an admin request.

        Just to make it easier for callers, the token value can have any number of trailing reuse markers. This simplifies the code in RemoteAdminCommand which actually sends two requests for each command: one to retrieve metadata and one to execute the command. It might be that the command itself might be reusing the token, in which case it will already have appened a reuse marker to it. Then the code which sends the metadata request can freely append the marker again without having to check if it is already present.

        Parameters:
        token - the token consumed, with 0 or more cppies of the reuse marker appended
        Returns:
        the Subject stored with the token when it was created; null if none was provided
      • markTokenForReuse

        public static String markTokenForReuse​(String token)