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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
/**
* 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.
*/
#include "mongo/platform/basic.h"
#include "mongo/db/logical_session_id_helpers.h"
#include "mongo/db/auth/authorization_session.h"
#include "mongo/db/auth/user.h"
#include "mongo/db/auth/user_name.h"
#include "mongo/db/commands/feature_compatibility_version_documentation.h"
#include "mongo/db/logical_session_cache.h"
#include "mongo/db/operation_context.h"
namespace mongo {
/**
* This is a safe hash that will not collide with a username because all full usernames include an
* '@' character.
*/
const auto kNoAuthDigest = SHA256Block::computeHash(reinterpret_cast<const uint8_t*>(""), 0);
SHA256Block getLogicalSessionUserDigestForLoggedInUser(const OperationContext* opCtx) {
auto client = opCtx->getClient();
ServiceContext* serviceContext = client->getServiceContext();
if (AuthorizationManager::get(serviceContext)->isAuthEnabled()) {
UserName userName;
const auto user = AuthorizationSession::get(client)->getSingleUser();
invariant(user);
uassert(ErrorCodes::BadValue,
"Username too long to use with logical sessions",
user->getName().getDisplayNameLength() < kMaximumUserNameLengthForLogicalSessions);
return user->getDigest();
} else {
return kNoAuthDigest;
}
}
SHA256Block getLogicalSessionUserDigestFor(StringData user, StringData db) {
if (user.empty() && db.empty()) {
return kNoAuthDigest;
}
const UserName un(user, db);
auto fn = un.getDisplayName();
return SHA256Block::computeHash({ConstDataRange(fn.c_str(), fn.size())});
}
boost::optional<LogicalSessionId> getParentSessionId(const LogicalSessionId& sessionId) {
if (sessionId.getTxnUUID()) {
return LogicalSessionId{sessionId.getId(), sessionId.getUid()};
}
return boost::none;
}
LogicalSessionId castToParentSessionId(const LogicalSessionId& sessionId) {
if (auto parentSessionId = getParentSessionId(sessionId)) {
return *parentSessionId;
}
return sessionId;
}
bool isInternalSessionForRetryableWrite(const LogicalSessionId& sessionId) {
return sessionId.getTxnNumber().has_value();
}
bool isInternalSessionForNonRetryableWrite(const LogicalSessionId& sessionId) {
return sessionId.getTxnUUID() && !sessionId.getTxnNumber();
}
LogicalSessionId makeLogicalSessionIdWithTxnNumberAndUUID(const LogicalSessionId& parentLsid,
TxnNumber txnNumber) {
auto lsid = LogicalSessionId(parentLsid.getId(), parentLsid.getUid());
lsid.getInternalSessionFields().setTxnNumber(txnNumber);
lsid.getInternalSessionFields().setTxnUUID(UUID::gen());
return lsid;
}
LogicalSessionId makeLogicalSessionIdWithTxnUUID(const LogicalSessionId& parentLsid) {
auto lsid = LogicalSessionId(parentLsid.getId(), parentLsid.getUid());
lsid.getInternalSessionFields().setTxnUUID(UUID::gen());
return lsid;
}
LogicalSessionId makeLogicalSessionId(const LogicalSessionFromClient& fromClient,
OperationContext* opCtx,
std::initializer_list<Privilege> allowSpoof) {
uassert(ErrorCodes::InvalidOptions,
"Cannot specify txnNumber in lsid without specifying txnUUID",
!fromClient.getTxnNumber() || fromClient.getTxnUUID());
LogicalSessionId lsid;
lsid.setId(fromClient.getId());
lsid.getInternalSessionFields().setTxnNumber(fromClient.getTxnNumber());
lsid.getInternalSessionFields().setTxnUUID(fromClient.getTxnUUID());
if (fromClient.getUid()) {
auto authSession = AuthorizationSession::get(opCtx->getClient());
uassert(ErrorCodes::Unauthorized,
"Unauthorized to set user digest in LogicalSessionId",
std::any_of(allowSpoof.begin(),
allowSpoof.end(),
[&](const auto& priv) {
return authSession->isAuthorizedForPrivilege(priv);
}) ||
authSession->isAuthorizedForPrivilege(Privilege(
ResourcePattern::forClusterResource(), ActionType::impersonate)) ||
getLogicalSessionUserDigestForLoggedInUser(opCtx) == fromClient.getUid());
lsid.setUid(*fromClient.getUid());
} else {
lsid.setUid(getLogicalSessionUserDigestForLoggedInUser(opCtx));
}
return lsid;
}
LogicalSessionId makeLogicalSessionId(OperationContext* opCtx) {
LogicalSessionId id{};
id.setId(UUID::gen());
id.setUid(getLogicalSessionUserDigestForLoggedInUser(opCtx));
return id;
}
LogicalSessionId makeSystemLogicalSessionId() {
LogicalSessionId id{};
id.setId(UUID::gen());
id.setUid((*internalSecurity.getUser())->getDigest());
return id;
}
LogicalSessionRecord makeLogicalSessionRecord(OperationContext* opCtx, Date_t lastUse) {
LogicalSessionId id{};
LogicalSessionRecord lsr{};
auto client = opCtx->getClient();
ServiceContext* serviceContext = client->getServiceContext();
if (AuthorizationManager::get(serviceContext)->isAuthEnabled()) {
auto user = AuthorizationSession::get(client)->getSingleUser();
invariant(user);
id.setUid(user->getDigest());
lsr.setUser(StringData(user->getName().getDisplayName()));
} else {
id.setUid(kNoAuthDigest);
}
id.setId(UUID::gen());
lsr.setId(id);
lsr.setLastUse(lastUse);
return lsr;
}
LogicalSessionRecord makeLogicalSessionRecord(const LogicalSessionId& lsid, Date_t lastUse) {
LogicalSessionId id{};
LogicalSessionRecord lsr{};
id.setId(lsid.getId());
id.setUid(lsid.getUid());
lsr.setId(id);
lsr.setLastUse(lastUse);
return lsr;
}
LogicalSessionRecord makeLogicalSessionRecord(OperationContext* opCtx,
const LogicalSessionId& lsid,
Date_t lastUse) {
auto lsr = makeLogicalSessionRecord(lsid, lastUse);
auto client = opCtx->getClient();
ServiceContext* serviceContext = client->getServiceContext();
if (AuthorizationManager::get(serviceContext)->isAuthEnabled()) {
auto user = AuthorizationSession::get(client)->getSingleUser();
invariant(user);
if (user->getDigest() == lsid.getUid()) {
lsr.setUser(StringData(user->getName().getDisplayName()));
}
}
return lsr;
}
LogicalSessionToClient makeLogicalSessionToClient(const LogicalSessionId& lsid) {
LogicalSessionIdToClient lsitc;
lsitc.setId(lsid.getId());
LogicalSessionToClient id;
id.setId(lsitc);
id.setTimeoutMinutes(localLogicalSessionTimeoutMinutes);
return id;
};
LogicalSessionIdSet makeLogicalSessionIds(const std::vector<LogicalSessionFromClient>& sessions,
OperationContext* opCtx,
std::initializer_list<Privilege> allowSpoof) {
LogicalSessionIdSet lsids;
lsids.reserve(sessions.size());
for (auto&& session : sessions) {
lsids.emplace(makeLogicalSessionId(session, opCtx, allowSpoof));
}
return lsids;
}
namespace logical_session_id_helpers {
void serializeLsidAndTxnNumber(OperationContext* opCtx, BSONObjBuilder* builder) {
OperationSessionInfo sessionInfo;
sessionInfo.setSessionId(opCtx->getLogicalSessionId());
sessionInfo.setTxnNumber(opCtx->getTxnNumber());
sessionInfo.serialize(builder);
}
} // namespace logical_session_id_helpers
} // namespace mongo
|