diff options
author | Spencer Jackson <spencer.jackson@mongodb.com> | 2018-08-22 15:25:52 -0400 |
---|---|---|
committer | Spencer Jackson <spencer.jackson@mongodb.com> | 2018-09-17 17:21:40 -0400 |
commit | e78dc4e8cf32da88062090410ab8617f604633c9 (patch) | |
tree | df238a81200a01b354ebad2ad9ecd2dc7c9bedb3 /src/mongo/db/commands.cpp | |
parent | f99914d14b76718f1fef879cfaabe23c0c8f0857 (diff) | |
download | mongo-e78dc4e8cf32da88062090410ab8617f604633c9.tar.gz |
SERVER-36606: Allow commands to expose names of sensitive fields
Diffstat (limited to 'src/mongo/db/commands.cpp')
-rw-r--r-- | src/mongo/db/commands.cpp | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/src/mongo/db/commands.cpp b/src/mongo/db/commands.cpp index 63500fe2af4..a30ad90600c 100644 --- a/src/mongo/db/commands.cpp +++ b/src/mongo/db/commands.cpp @@ -35,6 +35,7 @@ #include <string> #include <vector> +#include "mongo/bson/mutable/algorithm.h" #include "mongo/bson/mutable/document.h" #include "mongo/bson/timestamp.h" #include "mongo/db/audit.h" @@ -142,12 +143,26 @@ void CommandHelpers::auditLogAuthEvent(OperationContext* opCtx, explicit Hook(const CommandInvocation* invocation, const NamespaceString* nss) : _invocation(invocation), _nss(nss) {} - void redactForLogging(mutablebson::Document* cmdObj) const override { + void snipForLogging(mutablebson::Document* cmdObj) const override { if (_invocation) { - _invocation->definition()->redactForLogging(cmdObj); + _invocation->definition()->snipForLogging(cmdObj); } } + StringData sensitiveFieldName() const override { + if (_invocation) { + return _invocation->definition()->sensitiveFieldName(); + } + return StringData{}; + } + + StringData getName() const override { + if (!_invocation) { + return "Error"_sd; + } + return _invocation->definition()->getName(); + } + NamespaceString ns() const override { return *_nss; } @@ -404,7 +419,7 @@ void CommandInvocation::checkAuthorization(OperationContext* opCtx, } catch (const ExceptionFor<ErrorCodes::Unauthorized>&) { namespace mmb = mutablebson; mmb::Document cmdToLog(request.body, mmb::Document::kInPlaceDisabled); - c->redactForLogging(&cmdToLog); + c->snipForLogging(&cmdToLog); auto dbname = request.getDatabase(); uasserted(ErrorCodes::Unauthorized, str::stream() << "not authorized on " << dbname << " to execute command " @@ -476,6 +491,21 @@ private: Command::~Command() = default; +void Command::snipForLogging(mutablebson::Document* cmdObj) const { + StringData sensitiveField = sensitiveFieldName(); + if (!sensitiveField.empty()) { + + for (mutablebson::Element pwdElement = + mutablebson::findFirstChildNamed(cmdObj->root(), sensitiveField); + pwdElement.ok(); + pwdElement = + mutablebson::findElementNamed(pwdElement.rightSibling(), sensitiveField)) { + uassertStatusOK(pwdElement.setValueString("xxx")); + } + } +} + + std::unique_ptr<CommandInvocation> BasicCommand::parse(OperationContext* opCtx, const OpMsgRequest& request) { CommandHelpers::uassertNoDocumentSequences(getName(), request); |