summaryrefslogtreecommitdiff
path: root/src/mongo/db/auth/authorization_session.h
diff options
context:
space:
mode:
authorADAM David Alan Martin <adam.martin@10gen.com>2018-05-11 12:59:17 -0400
committerADAM David Alan Martin <adam.martin@10gen.com>2018-05-11 12:59:17 -0400
commitecf8ad987548705e773d23d5ddc3973cbc1ea7e5 (patch)
tree62164575b6303aebecbe41670c74b713cfa314c7 /src/mongo/db/auth/authorization_session.h
parent56cbcf28c6bd4d9f9e739fb834d83b61c99516f1 (diff)
downloadmongo-ecf8ad987548705e773d23d5ddc3973cbc1ea7e5.tar.gz
SERVER-33008 Slice Authorization framework
The Authorization framework was intertwined with many subsystems and needed to be properly abstracted in order to facilitate cutting down on certain unnecessary dependencies in some libraries. This also facilitates creating a reduced authorization framework for use in embedded builds.
Diffstat (limited to 'src/mongo/db/auth/authorization_session.h')
-rw-r--r--src/mongo/db/auth/authorization_session.h198
1 files changed, 103 insertions, 95 deletions
diff --git a/src/mongo/db/auth/authorization_session.h b/src/mongo/db/auth/authorization_session.h
index e260d859651..9b561c467d4 100644
--- a/src/mongo/db/auth/authorization_session.h
+++ b/src/mongo/db/auth/authorization_session.h
@@ -1,5 +1,5 @@
/**
-* Copyright (C) 2012 10gen Inc.
+* Copyright (C) 2018 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
@@ -32,7 +32,6 @@
#include <string>
#include <vector>
-#include "mongo/base/disallow_copying.h"
#include "mongo/base/status.h"
#include "mongo/db/auth/action_set.h"
#include "mongo/db/auth/action_type.h"
@@ -48,7 +47,7 @@ namespace mongo {
namespace auth {
struct CreateOrUpdateRoleArgs;
-}
+} // namespace auth
class Client;
/**
@@ -66,9 +65,15 @@ class Client;
* the lifetime of the operation.
*/
class AuthorizationSession {
- MONGO_DISALLOW_COPYING(AuthorizationSession);
+ AuthorizationSession(const AuthorizationSession&) = delete;
+ AuthorizationSession& operator=(const AuthorizationSession&) = delete;
public:
+ static MONGO_DECLARE_SHIM(
+ (AuthorizationManager * authzManager)->std::unique_ptr<AuthorizationSession>) create;
+
+ AuthorizationSession() = default;
+
/**
* Provides a way to swap out impersonate data for the duration of the ScopedImpersonate's
* lifetime.
@@ -77,8 +82,14 @@ public:
public:
ScopedImpersonate(AuthorizationSession* authSession,
std::vector<UserName>* users,
- std::vector<RoleName>* roles);
- ~ScopedImpersonate();
+ std::vector<RoleName>* roles)
+ : _authSession(*authSession), _users(*users), _roles(*roles) {
+ swap();
+ }
+
+ ~ScopedImpersonate() {
+ this->swap();
+ }
private:
void swap();
@@ -118,245 +129,242 @@ public:
static void set(Client* client, std::unique_ptr<AuthorizationSession> session);
// Takes ownership of the externalState.
- explicit AuthorizationSession(std::unique_ptr<AuthzSessionExternalState> externalState);
- ~AuthorizationSession();
+ virtual ~AuthorizationSession() = 0;
- AuthorizationManager& getAuthorizationManager();
+ virtual AuthorizationManager& getAuthorizationManager() = 0;
// Should be called at the beginning of every new request. This performs the checks
// necessary to determine if localhost connections should be given full access.
// TODO: try to eliminate the need for this call.
- void startRequest(OperationContext* opCtx);
+ virtual void startRequest(OperationContext* opCtx) = 0;
/**
* Adds the User identified by "UserName" to the authorization session, acquiring privileges
* for it in the process.
*/
- Status addAndAuthorizeUser(OperationContext* opCtx, const UserName& userName);
+ virtual Status addAndAuthorizeUser(OperationContext* opCtx, const UserName& userName) = 0;
// Returns the authenticated user with the given name. Returns NULL
// if no such user is found.
// The user remains in the _authenticatedUsers set for this AuthorizationSession,
// and ownership of the user stays with the AuthorizationManager
- User* lookupUser(const UserName& name);
+ virtual User* lookupUser(const UserName& name) = 0;
// Returns the single user on this auth session. If no user is authenticated, or if
// multiple users are authenticated, this method will throw an exception.
- User* getSingleUser();
+ virtual User* getSingleUser() = 0;
// Is authenticated as at least one user.
- bool isAuthenticated();
+ virtual bool isAuthenticated() = 0;
// Gets an iterator over the names of all authenticated users stored in this manager.
- UserNameIterator getAuthenticatedUserNames();
+ virtual UserNameIterator getAuthenticatedUserNames() = 0;
// Gets an iterator over the roles of all authenticated users stored in this manager.
- RoleNameIterator getAuthenticatedRoleNames();
+ virtual RoleNameIterator getAuthenticatedRoleNames() = 0;
// Returns a std::string representing all logged-in users on the current session.
// WARNING: this std::string will contain NUL bytes so don't call c_str()!
- std::string getAuthenticatedUserNamesToken();
+ virtual std::string getAuthenticatedUserNamesToken() = 0;
// Removes any authenticated principals whose authorization credentials came from the given
// database, and revokes any privileges that were granted via that principal.
- void logoutDatabase(const std::string& dbname);
+ virtual void logoutDatabase(const std::string& dbname) = 0;
// Adds the internalSecurity user to the set of authenticated users.
// Used to grant internal threads full access.
- void grantInternalAuthorization();
+ virtual void grantInternalAuthorization() = 0;
// Generates a vector of default privileges that are granted to any user,
// regardless of which roles that user does or does not possess.
// If localhost exception is active, the permissions include the ability to create
// the first user and the ability to run the commands needed to bootstrap the system
// into a state where the first user can be created.
- PrivilegeVector getDefaultPrivileges();
+ virtual PrivilegeVector getDefaultPrivileges() = 0;
// Checks if this connection has the privileges necessary to perform a find operation
// on the supplied namespace identifier.
- Status checkAuthForFind(const NamespaceString& ns, bool hasTerm);
+ virtual Status checkAuthForFind(const NamespaceString& ns, bool hasTerm) = 0;
// Checks if this connection has the privileges necessary to perform a getMore operation on
// the identified cursor, supposing that cursor is associated with the supplied namespace
// identifier.
- Status checkAuthForGetMore(const NamespaceString& ns, long long cursorID, bool hasTerm);
+ virtual Status checkAuthForGetMore(const NamespaceString& ns,
+ long long cursorID,
+ bool hasTerm) = 0;
// Checks if this connection has the privileges necessary to perform the given update on the
// given namespace.
- Status checkAuthForUpdate(OperationContext* opCtx,
- const NamespaceString& ns,
- const BSONObj& query,
- const BSONObj& update,
- bool upsert);
+ virtual Status checkAuthForUpdate(OperationContext* opCtx,
+ const NamespaceString& ns,
+ const BSONObj& query,
+ const BSONObj& update,
+ bool upsert) = 0;
// Checks if this connection has the privileges necessary to insert the given document
// to the given namespace. Correctly interprets inserts to system.indexes and performs
// the proper auth checks for index building.
- Status checkAuthForInsert(OperationContext* opCtx,
- const NamespaceString& ns,
- const BSONObj& document);
+ virtual Status checkAuthForInsert(OperationContext* opCtx,
+ const NamespaceString& ns,
+ const BSONObj& document) = 0;
// Checks if this connection has the privileges necessary to perform a delete on the given
// namespace.
- Status checkAuthForDelete(OperationContext* opCtx,
- const NamespaceString& ns,
- const BSONObj& query);
+ virtual Status checkAuthForDelete(OperationContext* opCtx,
+ const NamespaceString& ns,
+ const BSONObj& query) = 0;
// Checks if this connection has the privileges necessary to perform a killCursor on
// the identified cursor, supposing that cursor is associated with the supplied namespace
// identifier.
- Status checkAuthForKillCursors(const NamespaceString& cursorNss, UserNameIterator cursorOwner);
+ virtual Status checkAuthForKillCursors(const NamespaceString& cursorNss,
+ UserNameIterator cursorOwner) = 0;
// Checks if this connection has the privileges necessary to run the aggregation pipeline
// specified in 'cmdObj' on the namespace 'ns' either directly on mongoD or via mongoS.
- Status checkAuthForAggregate(const NamespaceString& ns, const BSONObj& cmdObj, bool isMongos);
+ virtual Status checkAuthForAggregate(const NamespaceString& ns,
+ const BSONObj& cmdObj,
+ bool isMongos) = 0;
// Checks if this connection has the privileges necessary to create 'ns' with the options
// supplied in 'cmdObj' either directly on mongoD or via mongoS.
- Status checkAuthForCreate(const NamespaceString& ns, const BSONObj& cmdObj, bool isMongos);
+ virtual Status checkAuthForCreate(const NamespaceString& ns,
+ const BSONObj& cmdObj,
+ bool isMongos) = 0;
// Checks if this connection has the privileges necessary to modify 'ns' with the options
// supplied in 'cmdObj' either directly on mongoD or via mongoS.
- Status checkAuthForCollMod(const NamespaceString& ns, const BSONObj& cmdObj, bool isMongos);
+ virtual Status checkAuthForCollMod(const NamespaceString& ns,
+ const BSONObj& cmdObj,
+ bool isMongos) = 0;
// Checks if this connection has the privileges necessary to grant the given privilege
// to a role.
- Status checkAuthorizedToGrantPrivilege(const Privilege& privilege);
+ virtual Status checkAuthorizedToGrantPrivilege(const Privilege& privilege) = 0;
// Checks if this connection has the privileges necessary to revoke the given privilege
// from a role.
- Status checkAuthorizedToRevokePrivilege(const Privilege& privilege);
+ virtual Status checkAuthorizedToRevokePrivilege(const Privilege& privilege) = 0;
// Checks if this connection is using the localhost bypass
- bool isUsingLocalhostBypass();
+ virtual bool isUsingLocalhostBypass() = 0;
// Checks if this connection has the privileges necessary to parse a namespace from a
// given BSONElement.
- bool isAuthorizedToParseNamespaceElement(const BSONElement& elem);
+ virtual bool isAuthorizedToParseNamespaceElement(const BSONElement& elem) = 0;
// Checks if this connection has the privileges necessary to create a new role
- bool isAuthorizedToCreateRole(const auth::CreateOrUpdateRoleArgs& args);
+ virtual bool isAuthorizedToCreateRole(const auth::CreateOrUpdateRoleArgs& args) = 0;
// Utility function for isAuthorizedForActionsOnResource(
// ResourcePattern::forDatabaseName(role.getDB()), ActionType::grantAnyRole)
- bool isAuthorizedToGrantRole(const RoleName& role);
+ virtual bool isAuthorizedToGrantRole(const RoleName& role) = 0;
// Utility function for isAuthorizedForActionsOnResource(
// ResourcePattern::forDatabaseName(role.getDB()), ActionType::grantAnyRole)
- bool isAuthorizedToRevokeRole(const RoleName& role);
+ virtual bool isAuthorizedToRevokeRole(const RoleName& role) = 0;
// Utility function for isAuthorizedToChangeOwnPasswordAsUser and
// isAuthorizedToChangeOwnCustomDataAsUser
- bool isAuthorizedToChangeAsUser(const UserName& userName, ActionType actionType);
+ virtual bool isAuthorizedToChangeAsUser(const UserName& userName, ActionType actionType) = 0;
// Returns true if the current session is authenticated as the given user and that user
// is allowed to change his/her own password
- bool isAuthorizedToChangeOwnPasswordAsUser(const UserName& userName);
+ virtual bool isAuthorizedToChangeOwnPasswordAsUser(const UserName& userName) = 0;
// Returns true if the current session is authorized to list the collections in the given
// database.
- bool isAuthorizedToListCollections(StringData dbname);
+ virtual bool isAuthorizedToListCollections(StringData dbname) = 0;
// Returns true if the current session is authenticated as the given user and that user
// is allowed to change his/her own customData.
- bool isAuthorizedToChangeOwnCustomDataAsUser(const UserName& userName);
+ virtual bool isAuthorizedToChangeOwnCustomDataAsUser(const UserName& userName) = 0;
// Returns true if any of the authenticated users on this session have the given role.
// NOTE: this does not refresh any of the users even if they are marked as invalid.
- bool isAuthenticatedAsUserWithRole(const RoleName& roleName);
+ virtual bool isAuthenticatedAsUserWithRole(const RoleName& roleName) = 0;
// Returns true if this session is authorized for the given Privilege.
//
// Contains all the authorization logic including handling things like the localhost
// exception.
- bool isAuthorizedForPrivilege(const Privilege& privilege);
+ virtual bool isAuthorizedForPrivilege(const Privilege& privilege) = 0;
// Like isAuthorizedForPrivilege, above, except returns true if the session is authorized
// for all of the listed privileges.
- bool isAuthorizedForPrivileges(const std::vector<Privilege>& privileges);
+ virtual bool isAuthorizedForPrivileges(const std::vector<Privilege>& privileges) = 0;
// Utility function for isAuthorizedForPrivilege(Privilege(resource, action)).
- bool isAuthorizedForActionsOnResource(const ResourcePattern& resource, ActionType action);
+ virtual bool isAuthorizedForActionsOnResource(const ResourcePattern& resource,
+ ActionType action) = 0;
// Utility function for isAuthorizedForPrivilege(Privilege(resource, actions)).
- bool isAuthorizedForActionsOnResource(const ResourcePattern& resource,
- const ActionSet& actions);
+ virtual bool isAuthorizedForActionsOnResource(const ResourcePattern& resource,
+ const ActionSet& actions) = 0;
// Utility function for
// isAuthorizedForActionsOnResource(ResourcePattern::forExactNamespace(ns), action).
- bool isAuthorizedForActionsOnNamespace(const NamespaceString& ns, ActionType action);
+ virtual bool isAuthorizedForActionsOnNamespace(const NamespaceString& ns,
+ ActionType action) = 0;
// Utility function for
// isAuthorizedForActionsOnResource(ResourcePattern::forExactNamespace(ns), actions).
- bool isAuthorizedForActionsOnNamespace(const NamespaceString& ns, const ActionSet& actions);
+ virtual bool isAuthorizedForActionsOnNamespace(const NamespaceString& ns,
+ const ActionSet& actions) = 0;
// Replaces the data for users that a system user is impersonating with new data.
// The auditing system adds these users and their roles to each audit record in the log.
- void setImpersonatedUserData(std::vector<UserName> usernames, std::vector<RoleName> roles);
+ virtual void setImpersonatedUserData(std::vector<UserName> usernames,
+ std::vector<RoleName> roles) = 0;
// Gets an iterator over the names of all users that the system user is impersonating.
- UserNameIterator getImpersonatedUserNames();
+ virtual UserNameIterator getImpersonatedUserNames() = 0;
// Gets an iterator over the roles of all users that the system user is impersonating.
- RoleNameIterator getImpersonatedRoleNames();
+ virtual RoleNameIterator getImpersonatedRoleNames() = 0;
// Clears the data for impersonated users.
- void clearImpersonatedUserData();
+ virtual void clearImpersonatedUserData() = 0;
// Returns true if the session and 'opClient's AuthorizationSession share an
// authenticated user. If either object has impersonated users,
// those users will be considered as 'authenticated' for the purpose of this check.
//
// The existence of 'opClient' must be guaranteed through locks taken by the caller.
- bool isCoauthorizedWithClient(Client* opClient);
+ virtual bool isCoauthorizedWithClient(Client* opClient) = 0;
// Returns true if the session and 'userNameIter' share an authenticated user, or if both have
// no authenticated users. Impersonated users are not considered as 'authenticated' for the
// purpose of this check. This always returns true if auth is not enabled.
- bool isCoauthorizedWith(UserNameIterator userNameIter);
+ virtual bool isCoauthorizedWith(UserNameIterator userNameIter) = 0;
// Tells whether impersonation is active or not. This state is set when
// setImpersonatedUserData is called and cleared when clearImpersonatedUserData is
// called.
- bool isImpersonating() const;
-
-protected:
- // Builds a vector of all roles held by users who are authenticated on this connection. The
- // vector is stored in _authenticatedRoleNames. This function is called when users are
- // logged in or logged out, as well as when the user cache is determined to be out of date.
- void _buildAuthenticatedRolesVector();
-
- // All Users who have been authenticated on this connection.
- UserSet _authenticatedUsers;
+ virtual bool isImpersonating() const = 0;
- // The roles of the authenticated users. This vector is generated when the authenticated
- // users set is changed.
- std::vector<RoleName> _authenticatedRoleNames;
+ // Returns a status encoding whether the current session in the specified `opCtx` has privilege
+ // to access a cursor in the specified `cursorSessionId` parameter. Returns `Status::OK()`,
+ // when the session is accessible. Returns a `mongo::Status` with information regarding the
+ // nature of session inaccessibility when the session is not accessible.
+ virtual Status checkCursorSessionPrivilege(
+ OperationContext* const opCtx, boost::optional<LogicalSessionId> cursorSessionId) = 0;
-private:
- // If any users authenticated on this session are marked as invalid this updates them with
- // up-to-date information. May require a read lock on the "admin" db to read the user data.
- void _refreshUserInfoAsNeeded(OperationContext* opCtx);
-
-
- // Checks if this connection is authorized for the given Privilege, ignoring whether or not
- // we should even be doing authorization checks in general. Note: this may acquire a read
- // lock on the admin database (to update out-of-date user privilege information).
- bool _isAuthorizedForPrivilege(const Privilege& privilege);
-
- std::unique_ptr<AuthzSessionExternalState> _externalState;
-
- // A vector of impersonated UserNames and a vector of those users' RoleNames.
- // These are used in the auditing system. They are not used for authz checks.
- std::vector<UserName> _impersonatedUserNames;
- std::vector<RoleName> _impersonatedRoleNames;
- bool _impersonationFlag;
+protected:
+ virtual std::tuple<std::vector<UserName>*, std::vector<RoleName>*> _getImpersonations() = 0;
};
// Returns a status encoding whether the current session in the specified `opCtx` has privilege to
// access a cursor in the specified `cursorSessionId` parameter. Returns `Status::OK()`, when the
// session is accessible. Returns a `mongo::Status` with information regarding the nature of
// session inaccessibility when the session is not accessible.
-Status checkCursorSessionPrivilege(OperationContext* const opCtx,
- const boost::optional<LogicalSessionId> cursorSessionId);
+inline Status checkCursorSessionPrivilege(OperationContext* const opCtx,
+ const boost::optional<LogicalSessionId> cursorSessionId) {
+ if (!AuthorizationSession::exists(opCtx->getClient())) {
+ return Status::OK();
+ }
+ auto* const authSession = AuthorizationSession::get(opCtx->getClient());
+ return authSession->checkCursorSessionPrivilege(opCtx, cursorSessionId);
+}
+
} // namespace mongo