summaryrefslogtreecommitdiff
path: root/src/mongo/db/commands
diff options
context:
space:
mode:
authorLynne Wang <lynne.wang@mongodb.com>2022-08-10 12:46:20 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-08-10 13:20:08 +0000
commit85ff13118e4f882b7c254fd1d3c2f4abd4c5296b (patch)
tree6c67145c7f707cdf63ccd7e75a35aedf66151265 /src/mongo/db/commands
parent86c0cc1046f22753ae9c5c82d62329f491d70e71 (diff)
downloadmongo-85ff13118e4f882b7c254fd1d3c2f4abd4c5296b.tar.gz
SERVER-67997 Validation of analyze parameters
Diffstat (limited to 'src/mongo/db/commands')
-rw-r--r--src/mongo/db/commands/analyze_cmd.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/mongo/db/commands/analyze_cmd.cpp b/src/mongo/db/commands/analyze_cmd.cpp
index 15868758600..89e80c8d775 100644
--- a/src/mongo/db/commands/analyze_cmd.cpp
+++ b/src/mongo/db/commands/analyze_cmd.cpp
@@ -31,6 +31,8 @@
#include "mongo/db/auth/authorization_session.h"
#include "mongo/db/commands.h"
+#include "mongo/db/db_raii.h"
+#include "mongo/db/namespace_string.h"
#include "mongo/db/query/analyze_command_gen.h"
#include "mongo/db/query/query_feature_flags_gen.h"
@@ -66,6 +68,58 @@ public:
}
void typedRun(OperationContext* opCtx) {
+ const auto& cmd = request();
+ const NamespaceString& nss = ns();
+
+ // Validate collection
+ // Namespace exists
+ AutoGetCollectionForReadMaybeLockFree autoColl(opCtx, nss);
+ const auto& collection = autoColl.getCollection();
+ uassert(6799700, str::stream() << "Couldn't find collection " << nss.ns(), collection);
+
+ // Namespace cannot be capped collection
+ const bool isCapped = collection->isCapped();
+ uassert(6799701, "Analyze command is not supported on capped collections", !isCapped);
+
+ // Namespace is normal or clustered collection
+ const bool isNormalColl = nss.isNormalCollection();
+ const bool isClusteredColl = collection->isClustered();
+ uassert(6799702,
+ str::stream() << nss.toString() << " is not a normal or clustered collection",
+ isNormalColl || isClusteredColl);
+
+ // Validate key
+ auto key = cmd.getKey();
+ if (key) {
+ const FieldRef keyFieldRef(*key);
+
+ // Empty path
+ uassert(6799703, "Key path is empty", !keyFieldRef.empty());
+
+ for (size_t i = 0; i < keyFieldRef.numParts(); ++i) {
+ FieldPath::uassertValidFieldName(keyFieldRef.getPart(i));
+ }
+
+ // Numerics
+ const auto numericPathComponents = keyFieldRef.getNumericPathComponents(0);
+ uassert(6799704,
+ str::stream() << "Key path contains numeric component "
+ << keyFieldRef.getPart(*(numericPathComponents.begin())),
+ numericPathComponents.empty());
+ }
+
+ // Sample rate and sample size can't both be present
+ auto sampleRate = cmd.getSampleRate();
+ auto sampleSize = cmd.getSampleSize();
+ uassert(6799705,
+ "Only one of sample rate and sample size may be present",
+ !sampleRate || !sampleSize);
+
+ if (sampleSize || sampleRate) {
+ uassert(
+ 6799706, "It is illegal to pass sampleRate or sampleSize without a key", key);
+ }
+
uassert(6660400,
"Analyze command requires common query framework feature flag to be enabled",
serverGlobalParams.featureCompatibility.isVersionInitialized() &&