summaryrefslogtreecommitdiff
path: root/src/mongo/db/auth/auth_name.cpp
blob: 6c76351bbcd1b3a3bd3b2bcd450fad142a332972 (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
/**
 *    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/db/auth/auth_name.h"

#include "mongo/db/auth/role_name.h"
#include "mongo/db/auth/user_name.h"

namespace mongo {
namespace {
constexpr auto kTenantFieldName = "tenant"_sd;
}  // namespace

template <typename T>
StatusWith<T> AuthName<T>::parse(StringData str, const boost::optional<TenantId>& tenant) {
    auto split = str.find('.');

    if (split == std::string::npos) {
        return Status(ErrorCodes::BadValue,
                      str::stream() << T::kName << " must contain a '.' separated database."
                                    << T::kFieldName << " pair");
    }

    return T(str.substr(split + 1), str.substr(0, split), tenant);
}

template <typename T>
T AuthName<T>::parseFromVariant(const stdx::variant<std::string, BSONObj>& name,
                                const boost::optional<TenantId>& tenant) {
    if (stdx::holds_alternative<std::string>(name)) {
        return uassertStatusOK(parse(stdx::get<std::string>(name)));
    }

    return parseFromBSONObj(stdx::get<BSONObj>(name), tenant);
}

template <typename T>
T AuthName<T>::parseFromBSONObj(const BSONObj& obj, const boost::optional<TenantId>& activeTenant) {
    std::bitset<3> usedFields;
    constexpr size_t kNameFieldBit = 0;
    constexpr size_t kDbFieldBit = 1;
    constexpr size_t kTenantFieldBit = 2;
    StringData name, db;
    boost::optional<TenantId> tenant = activeTenant;

    const auto validateField = [&](const BSONElement& elem, const size_t bit, BSONType expType) {
        const auto fieldName = elem.fieldNameStringData();
        uassert(ErrorCodes::BadValue,
                str::stream() << T::kName << " must contain a " << typeName(expType)
                              << " field named: " << fieldName << ". But, has type "
                              << typeName(elem.type()),
                elem.type() == expType);
        uassert(ErrorCodes::BadValue,
                str::stream() << T::kName << " has more than one field named: " << fieldName,
                !usedFields[bit]);
        usedFields.set(bit);
    };

    for (const auto& element : obj) {
        const auto fieldName = element.fieldNameStringData();

        if (fieldName == T::kFieldName) {
            validateField(element, kNameFieldBit, String);
            name = element.valueStringData();

        } else if (fieldName == "db"_sd) {
            validateField(element, kDbFieldBit, String);
            db = element.valueStringData();

        } else if (fieldName == kTenantFieldName) {
            validateField(element, kTenantFieldBit, jstOID);
            tenant = TenantId::parseFromBSON(element);
            if (activeTenant) {
                uassert(ErrorCodes::BadValue,
                        str::stream()
                            << T::kName
                            << " contains a TenantId which does not match the active tenant",
                        tenant == activeTenant);
            }

        } else if constexpr (std::is_same_v<UserName, T>) {
            // Only UserName is strict, RoleName is non-strict.
            uasserted(ErrorCodes::BadValue,
                      str::stream()
                          << T::kName << " contains an unknown field named: '" << fieldName);
        }
    }

    uassert(ErrorCodes::BadValue,
            str::stream() << T::kName << " must contain a field named: " << T::kFieldName,
            usedFields[kNameFieldBit]);

    uassert(ErrorCodes::BadValue,
            str::stream() << T::kName << " must contain a field named: db",
            usedFields[kDbFieldBit]);

    return T(name, db, tenant);
}

template <typename T>
T AuthName<T>::parseFromBSON(const BSONElement& elem,
                             const boost::optional<TenantId>& activeTenant) {
    if (elem.type() == String) {
        return uassertStatusOK(parse(elem.valueStringData(), activeTenant));
    } else if (elem.type() == Object) {
        const auto obj = elem.embeddedObject();
        return parseFromBSONObj(obj, activeTenant);
    } else {
        uasserted(ErrorCodes::BadValue,
                  str::stream() << T::kName << " must be either a string or an object");
    }
}

template <typename T>
void AuthName<T>::serializeToBSON(StringData fieldName, BSONObjBuilder* bob) const {
    BSONObjBuilder builder(bob->subobjStart(fieldName));
    appendToBSON(&builder);
}

template <typename T>
void AuthName<T>::serializeToBSON(BSONArrayBuilder* bob) const {
    BSONObjBuilder builder(bob->subobjStart());
    appendToBSON(&builder);
}

template <typename T>
void AuthName<T>::appendToBSON(BSONObjBuilder* bob, bool encodeTenant) const {
    *bob << T::kFieldName << getName() << "db"_sd << getDB();
    if (encodeTenant && _tenant) {
        *bob << kTenantFieldName << _tenant.value();
    }
}

template <typename T>
BSONObj AuthName<T>::toBSON(bool encodeTenant) const {
    BSONObjBuilder bob;
    appendToBSON(&bob, encodeTenant);
    return bob.obj();
}

template <typename T>
std::size_t AuthName<T>::getBSONObjSize() const {
    return 4UL +                            // BSONObj size
        1UL + T::kFieldName.size() + 1UL +  // FieldName elem type, FieldName, terminating NULL.
        4UL + getName().size() + 1UL +      // Length of name data, name data, terminating NULL.
        1UL + ("db"_sd).size() + 1UL +      // DB field elem type, "db", terminating NULL.
        4UL + getDB().size() + 1UL +        // DB value length, DB value, terminating NULL.
        1UL;                                // EOD marker.
}

// Materialize the types we care about.
template class AuthName<RoleName>;
template class AuthName<UserName>;

}  // namespace mongo