diff options
author | Moustafa Maher <m.maher@10gen.com> | 2021-03-24 17:31:56 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-03-25 04:52:57 +0000 |
commit | c4530deb51e0f97cc48c2547f915bb4b39e49253 (patch) | |
tree | ccc8fc22015c41b6d494cbea2ed958712151bde0 /src/mongo/db/auth | |
parent | a49827ee30cf37844f177b587071b6b51db87c90 (diff) | |
download | mongo-c4530deb51e0f97cc48c2547f915bb4b39e49253.tar.gz |
SERVER-54927 move away from using Bson_serialization_type any
Diffstat (limited to 'src/mongo/db/auth')
-rw-r--r-- | src/mongo/db/auth/user_name.cpp | 92 | ||||
-rw-r--r-- | src/mongo/db/auth/user_name.h | 16 |
2 files changed, 89 insertions, 19 deletions
diff --git a/src/mongo/db/auth/user_name.cpp b/src/mongo/db/auth/user_name.cpp index d9d218855c8..65aa84cba1e 100644 --- a/src/mongo/db/auth/user_name.cpp +++ b/src/mongo/db/auth/user_name.cpp @@ -49,6 +49,9 @@ UserName::UserName(StringData user, StringData dbname) { _splitPoint = user.size(); } +/** + * Don't change the logic of this function as it will break stable API version 1. + */ StatusWith<UserName> UserName::parse(StringData userNameStr) { size_t splitPoint = userNameStr.find('.'); @@ -63,29 +66,76 @@ StatusWith<UserName> UserName::parse(StringData userNameStr) { return UserName(userNamePortion, userDBPortion); } -UserName UserName::parseFromBSON(const BSONElement& elem) { - if (elem.type() == String) { - return uassertStatusOK(UserName::parse(elem.valueStringData())); - } else if (elem.type() == Object) { - const auto obj = elem.embeddedObject(); - std::array<BSONElement, 2> fields; - obj.getFields( - {AuthorizationManager::USER_NAME_FIELD_NAME, AuthorizationManager::USER_DB_FIELD_NAME}, - &fields); +/** + * Don't change the logic of this function as it will break stable API version 1. + */ +UserName UserName::parseFromVariant(const stdx::variant<std::string, BSONObj>& helloUserName) { + if (stdx::holds_alternative<std::string>(helloUserName)) { + return uassertStatusOK(parse(stdx::get<std::string>(helloUserName))); + } + + return parseFromBSONObj(stdx::get<BSONObj>(helloUserName)); +} + +/** + * Don't change the logic of this function as it will break stable API version 1. + */ +UserName UserName::parseFromBSONObj(const BSONObj& obj) { + std::bitset<2> usedFields; + const auto kUserNameFieldName = AuthorizationManager::USER_NAME_FIELD_NAME; + const auto kUserDbFieldName = AuthorizationManager::USER_DB_FIELD_NAME; + const size_t kUserNameFieldBit = 0; + const size_t kUserDbFieldBit = 1; + StringData userName, userDb; + + for (const auto& element : obj) { + const auto fieldName = element.fieldNameStringData(); - const auto& nameField = fields[0]; uassert(ErrorCodes::BadValue, - str::stream() << "username must contain a string field named: " - << AuthorizationManager::USER_NAME_FIELD_NAME, - nameField.type() == String); + str::stream() << "username contains an unknown field named: '" << fieldName, + fieldName == kUserNameFieldName || fieldName == kUserDbFieldName); - const auto& dbField = fields[1]; uassert(ErrorCodes::BadValue, - str::stream() << "username must contain a string field named: " - << AuthorizationManager::USER_DB_FIELD_NAME, - dbField.type() == String); + str::stream() << "username must contain a string field named: " << fieldName, + element.type() == String); + + if (fieldName == kUserNameFieldName) { + uassert(ErrorCodes::BadValue, + str::stream() << "username has more than one field named: " + << kUserNameFieldName, + !usedFields[kUserNameFieldBit]); + + usedFields.set(kUserNameFieldBit); + userName = element.valueStringData(); + } else if (fieldName == kUserDbFieldName) { + uassert(ErrorCodes::BadValue, + str::stream() << "username has more than one field named: " << kUserDbFieldName, + !usedFields[kUserDbFieldBit]); + + usedFields.set(kUserDbFieldBit); + userDb = element.valueStringData(); + } + } + + if (!usedFields[kUserNameFieldBit]) { + uasserted(ErrorCodes::BadValue, + str::stream() << "username must contain a field named: " << kUserNameFieldName); + } + + if (!usedFields[kUserDbFieldBit]) { + uasserted(ErrorCodes::BadValue, + str::stream() << "username must contain a field named: " << kUserDbFieldName); + } + + return UserName(userName, userDb); +} - return UserName(nameField.valueStringData(), dbField.valueStringData()); +UserName UserName::parseFromBSON(const BSONElement& elem) { + if (elem.type() == String) { + return uassertStatusOK(UserName::parse(elem.valueStringData())); + } else if (elem.type() == Object) { + const auto obj = elem.embeddedObject(); + return parseFromBSONObj(obj); } else { uasserted(ErrorCodes::BadValue, "username must be either a string or an object"); } @@ -101,11 +151,17 @@ void UserName::serializeToBSON(BSONArrayBuilder* bab) const { appendToBSON(&sub); } +/** + * Don't change the logic of this function as it will break stable API version 1. + */ void UserName::appendToBSON(BSONObjBuilder* bob) const { *bob << AuthorizationManager::USER_NAME_FIELD_NAME << getUser() << AuthorizationManager::USER_DB_FIELD_NAME << getDB(); } +/** + * Don't change the logic of this function as it will break stable API version 1. + */ BSONObj UserName::toBSON() const { BSONObjBuilder ret; appendToBSON(&ret); diff --git a/src/mongo/db/auth/user_name.h b/src/mongo/db/auth/user_name.h index 81afb1895a3..4654ce4dabb 100644 --- a/src/mongo/db/auth/user_name.h +++ b/src/mongo/db/auth/user_name.h @@ -39,6 +39,7 @@ #include "mongo/base/string_data.h" #include "mongo/bson/bsonelement.h" #include "mongo/bson/bsonobjbuilder.h" +#include "mongo/stdx/variant.h" namespace mongo { @@ -54,16 +55,29 @@ public: /** * Parses a string of the form "db.username" into a UserName object. + * Don't change the logic of this function as it will break stable API version 1. */ static StatusWith<UserName> parse(StringData userNameStr); - /* + /** * These methods support parsing usernames from IDL + * Don't change the logic of this function as it will break stable API version 1. */ + static UserName parseFromVariant( + const stdx::variant<std::string, mongo::BSONObj>& helloUserName); + static UserName parseFromBSONObj(const BSONObj& obj); static UserName parseFromBSON(const BSONElement& elem); void serializeToBSON(StringData fieldName, BSONObjBuilder* bob) const; void serializeToBSON(BSONArrayBuilder* bob) const; + + /** + * Don't change the logic of this function as it will break stable API version 1. + */ void appendToBSON(BSONObjBuilder* bob) const; + + /** + * Don't change the logic of this function as it will break stable API version 1. + */ BSONObj toBSON() const; /** |