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
|
/**
* 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 <functional>
#include <memory>
#include "mongo/base/status_with.h"
#include "mongo/db/key_generator.h"
#include "mongo/db/keys_collection_cache.h"
#include "mongo/db/keys_collection_document.h"
#include "mongo/db/keys_collection_manager_gen.h"
#include "mongo/platform/atomic_word.h"
#include "mongo/platform/mutex.h"
#include "mongo/stdx/thread.h"
#include "mongo/util/concurrency/notification.h"
#include "mongo/util/duration.h"
namespace mongo {
class OperationContext;
class LogicalTime;
class ServiceContext;
class KeysCollectionClient;
namespace keys_collection_manager_util {
/**
* Returns the amount of time to wait until the monitoring thread should attempt to refresh again.
*/
Milliseconds howMuchSleepNeedFor(const LogicalTime& currentTime,
const LogicalTime& latestExpiredAt,
const Milliseconds& interval);
} // namespace keys_collection_manager_util
/**
* The KeysCollectionManager queries the config servers for keys that can be used for
* HMAC computation. It maintains an internal background thread that is used to periodically
* refresh the local key cache against the keys collection stored on the config servers.
*/
class KeysCollectionManager {
public:
static const std::string kKeyManagerPurposeString;
KeysCollectionManager(std::string purpose,
std::unique_ptr<KeysCollectionClient> client,
Seconds keyValidForInterval);
/**
* Return a key that is valid for the given time and also matches the keyId. Note that this call
* can block if it will need to do a refresh.
*
* Throws ErrorCode::ExceededTimeLimit if it times out.
*/
StatusWith<KeysCollectionDocument> getKeyForValidation(OperationContext* opCtx,
long long keyId,
const LogicalTime& forThisTime);
/**
* Returns a key that is valid for the given time. Note that unlike getKeyForValidation, this
* will never do a refresh.
*
* Throws ErrorCode::ExceededTimeLimit if it times out.
*/
StatusWith<KeysCollectionDocument> getKeyForSigning(OperationContext* opCtx,
const LogicalTime& forThisTime);
/**
* Request this manager to perform a refresh.
*/
void refreshNow(OperationContext* opCtx);
/**
* Starts a background thread that will constantly update the internal cache of keys.
*
* Cannot call this after stopMonitoring was called at least once.
*/
void startMonitoring(ServiceContext* service);
/**
* Stops the background thread updating the cache.
*/
void stopMonitoring();
/**
* Enable writing new keys to the config server primary. Should only be called if current node
* is the config primary.
*/
void enableKeyGenerator(OperationContext* opCtx, bool doEnable);
/**
* Returns true if the refresher has ever successfully returned keys from the config server.
*/
bool hasSeenKeys();
/**
* Clears the in memory cache of the keys.
*/
void clearCache();
private:
/**
* This is responsible for periodically performing refresh in the background.
*/
class PeriodicRunner {
public:
using RefreshFunc = std::function<StatusWith<KeysCollectionDocument>(OperationContext*)>;
/**
* Preemptively inform the monitoring thread it needs to perform a refresh. Returns an
* object
* that gets notified after the current round of refresh is over. Note that being notified
* can
* mean either of these things:
*
* 1. An error occurred and refresh was not performed.
* 2. No error occurred but no new key was found.
* 3. No error occurred and new keys were found.
*/
void refreshNow(OperationContext* opCtx);
/**
* Sets the refresh function to use.
* Should only be used to bootstrap this refresher with initial strategy.
*/
void setFunc(RefreshFunc newRefreshStrategy);
/**
* Switches the current strategy with a new one. This also waits to make sure that the old
* strategy is not being used and will no longer be used after this call.
*/
void switchFunc(OperationContext* opCtx, RefreshFunc newRefreshStrategy);
/**
* Starts the refresh thread.
*/
void start(ServiceContext* service,
const std::string& threadName,
Milliseconds refreshInterval);
/**
* Stops the refresh thread.
*/
void stop();
/**
* Returns true if keys have ever successfully been returned from the config server.
*/
bool hasSeenKeys() const noexcept;
private:
void _doPeriodicRefresh(ServiceContext* service,
std::string threadName,
Milliseconds refreshInterval);
AtomicWord<bool> _hasSeenKeys{false};
// protects all the member variables below.
Mutex _mutex = MONGO_MAKE_LATCH("PeriodicRunner::_mutex");
std::shared_ptr<Notification<void>> _refreshRequest;
stdx::condition_variable _refreshNeededCV;
stdx::thread _backgroundThread;
std::shared_ptr<RefreshFunc> _doRefresh;
bool _inShutdown = false;
};
/**
* Return a key that is valid for the given time and also matches the keyId.
*/
StatusWith<KeysCollectionDocument> _getKeyWithKeyIdCheck(long long keyId,
const LogicalTime& forThisTime);
/**
* Return a key that is valid for the given time.
*/
StatusWith<KeysCollectionDocument> _getKey(const LogicalTime& forThisTime);
std::unique_ptr<KeysCollectionClient> _client;
const std::string _purpose;
const Seconds _keyValidForInterval;
// No mutex needed since the members below have their own mutexes.
KeysCollectionCache _keysCache;
PeriodicRunner _refresher;
};
} // namespace mongo
|