summaryrefslogtreecommitdiff
path: root/src/mongo/db/auth/authorization_manager_impl.h
blob: 17502fd00676e4db374d5b4163bc75aee347e4fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#pragma once

#include "mongo/db/auth/authorization_manager.h"
#include "mongo/db/auth/privilege.h"
#include "mongo/platform/atomic_word.h"
#include "mongo/platform/mutex.h"
#include "mongo/stdx/condition_variable.h"
#include "mongo/stdx/unordered_map.h"
#include "mongo/util/concurrency/thread_pool.h"

namespace mongo {

/**
 * Contains server/cluster-wide information about Authorization.
 */
class AuthorizationManagerImpl final : public AuthorizationManager {
public:
    struct InstallMockForTestingOrAuthImpl {
        explicit InstallMockForTestingOrAuthImpl() = default;
    };

    AuthorizationManagerImpl(ServiceContext* service,
                             std::unique_ptr<AuthzManagerExternalState> externalState);
    ~AuthorizationManagerImpl();


    std::unique_ptr<AuthorizationSession> makeAuthorizationSession() override;

    void setShouldValidateAuthSchemaOnStartup(bool validate) override;

    bool shouldValidateAuthSchemaOnStartup() override;

    void setAuthEnabled(bool enabled) override;

    bool isAuthEnabled() const override;

    Status getAuthorizationVersion(OperationContext* opCtx, int* version) override;

    OID getCacheGeneration() override;

    bool hasAnyPrivilegeDocuments(OperationContext* opCtx) override;

    Status getUserDescription(OperationContext* opCtx,
                              const UserName& userName,
                              BSONObj* result) override;

    Status rolesExist(OperationContext* opCtx, const std::vector<RoleName>& roleNames) override;

    StatusWith<ResolvedRoleData> resolveRoles(OperationContext* opCtx,
                                              const std::vector<RoleName>& roleNames,
                                              ResolveRoleOption option) override;

    Status getRolesDescription(OperationContext* opCtx,
                               const std::vector<RoleName>& roleName,
                               PrivilegeFormat privilegeFormat,
                               AuthenticationRestrictionsFormat,
                               std::vector<BSONObj>* result) override;

    Status getRolesAsUserFragment(OperationContext* opCtx,
                                  const std::vector<RoleName>& roleName,
                                  AuthenticationRestrictionsFormat,
                                  BSONObj* result) override;

    Status getRoleDescriptionsForDB(OperationContext* opCtx,
                                    const DatabaseName& dbname,
                                    PrivilegeFormat privilegeFormat,
                                    AuthenticationRestrictionsFormat,
                                    bool showBuiltinRoles,
                                    std::vector<BSONObj>* result) override;

    StatusWith<UserHandle> acquireUser(OperationContext* opCtx,
                                       const UserRequest& userRequest) override;
    StatusWith<UserHandle> reacquireUser(OperationContext* opCtx, const UserHandle& user) override;

    /**
     * Invalidate a user, and repin it if necessary.
     */
    void invalidateUserByName(OperationContext* opCtx, const UserName& user) override;

    void invalidateUsersFromDB(OperationContext* opCtx, const DatabaseName& dbname) override;

    void invalidateUsersByTenant(OperationContext* opCtx,
                                 const boost::optional<TenantId>& tenant) override;

    /**
     * Verify role information for users in the $external database and insert updated information
     * into the cache if necessary. Currently, this is only used to refresh LDAP users.
     */
    Status refreshExternalUsers(OperationContext* opCtx) override;

    Status initialize(OperationContext* opCtx) override;

    /**
     * Invalidate the user cache, and repin all pinned users.
     */
    void invalidateUserCache(OperationContext* opCtx) override;

    void updatePinnedUsersList(std::vector<UserName> names) override;

    void logOp(OperationContext* opCtx,
               StringData opstr,
               const NamespaceString& nss,
               const BSONObj& obj,
               const BSONObj* patt) override;

    std::vector<CachedUserInfo> getUserCacheInfo() const override;

private:
    void _updateCacheGeneration();

    void _pinnedUsersThreadRoutine() noexcept;

    std::unique_ptr<AuthzManagerExternalState> _externalState;

    // True if AuthSchema startup checks should be applied in this AuthorizationManager. Changes to
    // its value are not synchronized, so it should only be set once, at initalization time.
    bool _startupAuthSchemaValidation{true};

    // True if access control enforcement is enabled in this AuthorizationManager. Changes to its
    // value are not synchronized, so it should only be set once, at initalization time.
    bool _authEnabled{false};

    // A cache of whether there are any users set up for the cluster.
    AtomicWord<bool> _privilegeDocsExist{false};

    // Serves as a source for the return value of getCacheGeneration(). Refer to this method for
    // more details.
    Mutex _cacheGenerationMutex =
        MONGO_MAKE_LATCH("AuthorizationManagerImpl::_cacheGenerationMutex");
    OID _cacheGeneration{OID::gen()};

    /**
     * Cache which contains at most a single entry (which has key 0), whose value is the version of
     * the auth schema.
     */
    class AuthSchemaVersionCache : public ReadThroughCache<int, int> {
    public:
        AuthSchemaVersionCache(ServiceContext* service,
                               ThreadPoolInterface& threadPool,
                               AuthzManagerExternalState* externalState);

    private:
        // Even though the dist cache permits for lookup to return boost::none for non-existent
        // values, the contract of the authorization manager is that it should throw an exception if
        // the value can not be loaded, so if it returns, the value will always be set.
        LookupResult _lookup(OperationContext* opCtx,
                             int unusedKey,
                             const ValueHandle& unusedCachedValue);

        Mutex _mutex =
            MONGO_MAKE_LATCH("AuthorizationManagerImpl::AuthSchemaVersionDistCache::_mutex");

        AuthzManagerExternalState* const _externalState;
    } _authSchemaVersionCache;

    /**
     * Cache of the users known to the authentication subsystem.
     */
    class UserCacheImpl : public UserCache {
    public:
        UserCacheImpl(ServiceContext* service,
                      ThreadPoolInterface& threadPool,
                      int cacheSize,
                      AuthSchemaVersionCache* authSchemaVersionCache,
                      AuthzManagerExternalState* externalState);

    private:
        // Even though the dist cache permits for lookup to return boost::none for non-existent
        // values, the contract of the authorization manager is that it should throw an exception if
        // the value can not be loaded, so if it returns, the value will always be set.
        LookupResult _lookup(OperationContext* opCtx,
                             const UserRequest& user,
                             const UserHandle& unusedCachedUser);

        Mutex _mutex = MONGO_MAKE_LATCH("AuthorizationManagerImpl::UserDistCacheImpl::_mutex");

        AuthSchemaVersionCache* const _authSchemaVersionCache;

        AuthzManagerExternalState* const _externalState;
    } _userCache;

    // Thread pool on which to perform the blocking activities that load the user credentials from
    // storage
    ThreadPool _threadPool;

    Mutex _pinnedUsersMutex = MONGO_MAKE_LATCH("AuthorizationManagerImpl::_pinnedUsersMutex");
    stdx::condition_variable _pinnedUsersCond;
    std::once_flag _pinnedThreadTrackerStarted;
    boost::optional<std::vector<UserName>> _usersToPin;
};

extern int authorizationManagerCacheSize;

}  // namespace mongo