25 files changed
+481
-12
No files matched your search
@@ -1279,6 +1279,7 @@ pub enum MongoAgentMethod {
|
||||
CountDocuments,
|
||||
ServerVersion,
|
||||
CreateIndex,
|
||||
CreateUser,
|
||||
DropIndexes,
|
||||
DropCollection,
|
||||
DropDatabase,
|
||||
@@ -1290,7 +1291,7 @@ pub enum MongoAgentMethod {
|
||||
}
|
||||
|
||||
impl MongoAgentMethod {
|
||||
pub const ALL: [Self; 18] = [
|
||||
pub const ALL: [Self; 19] = [
|
||||
Self::ListDatabases,
|
||||
Self::ListCollections,
|
||||
Self::FindDocuments,
|
||||
@@ -1301,6 +1302,7 @@ impl MongoAgentMethod {
|
||||
Self::CountDocuments,
|
||||
Self::ServerVersion,
|
||||
Self::CreateIndex,
|
||||
Self::CreateUser,
|
||||
Self::DropIndexes,
|
||||
Self::DropCollection,
|
||||
Self::DropDatabase,
|
||||
@@ -1323,6 +1325,7 @@ impl MongoAgentMethod {
|
||||
Self::CountDocuments => "count_documents",
|
||||
Self::ServerVersion => "server_version",
|
||||
Self::CreateIndex => "create_index",
|
||||
Self::CreateUser => "create_user",
|
||||
Self::DropIndexes => "drop_indexes",
|
||||
Self::DropCollection => "drop_collection",
|
||||
Self::DropDatabase => "drop_database",
|
||||
@@ -2520,6 +2523,13 @@ impl AgentDriverClient {
|
||||
self.call_mongo_method(MongoAgentMethod::CreateIndex, params).await
|
||||
}
|
||||
|
||||
pub async fn mongo_create_user<T: DeserializeOwned + Send + 'static>(
|
||||
&mut self,
|
||||
params: Value,
|
||||
) -> Result<T, String> {
|
||||
self.call_mongo_method(MongoAgentMethod::CreateUser, params).await
|
||||
}
|
||||
|
||||
pub async fn mongo_drop_indexes<T: DeserializeOwned + Send + 'static>(
|
||||
&mut self,
|
||||
params: Value,
|
||||
@@ -4348,6 +4358,7 @@ for line in sys.stdin:
|
||||
assert_eq!(MongoAgentMethod::CountDocuments.as_str(), "count_documents");
|
||||
assert_eq!(MongoAgentMethod::ServerVersion.as_str(), "server_version");
|
||||
assert_eq!(MongoAgentMethod::CreateIndex.as_str(), "create_index");
|
||||
assert_eq!(MongoAgentMethod::CreateUser.as_str(), "create_user");
|
||||
assert_eq!(MongoAgentMethod::DropIndexes.as_str(), "drop_indexes");
|
||||
assert_eq!(MongoAgentMethod::DropCollection.as_str(), "drop_collection");
|
||||
assert_eq!(MongoAgentMethod::DropDatabase.as_str(), "drop_database");
|
||||
|
||||
@@ -1231,6 +1231,48 @@ pub async fn create_index(
|
||||
Ok(name)
|
||||
}
|
||||
|
||||
pub async fn create_user(
|
||||
client: &Client,
|
||||
database: &str,
|
||||
user_json: &str,
|
||||
write_concern_json: Option<&str>,
|
||||
) -> Result<(), String> {
|
||||
let database = validate_mongo_namespace_name(database, "Database")?;
|
||||
let command = create_user_command(user_json, write_concern_json)?;
|
||||
client.database(database).run_command(command).await.map_err(|error| error.kind.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn validate_create_user_request(user_json: &str, write_concern_json: Option<&str>) -> Result<(), String> {
|
||||
create_user_command(user_json, write_concern_json).map(|_| ())
|
||||
}
|
||||
|
||||
fn create_user_command(user_json: &str, write_concern_json: Option<&str>) -> Result<Document, String> {
|
||||
let user_value: serde_json::Value =
|
||||
serde_json::from_str(user_json).map_err(|error| format!("Invalid MongoDB user JSON: {error}"))?;
|
||||
let mut user = json_object_to_document_extended_json(&user_value)
|
||||
.map_err(|error| format!("Invalid MongoDB user document: {error}"))?;
|
||||
let username = user
|
||||
.remove("user")
|
||||
.and_then(|value| value.as_str().map(str::to_string))
|
||||
.filter(|value| !value.trim().is_empty())
|
||||
.ok_or("MongoDB createUser requires a non-empty user name")?;
|
||||
if user.contains_key("createUser") || user.contains_key("writeConcern") {
|
||||
return Err("MongoDB createUser user document contains reserved command fields".to_string());
|
||||
}
|
||||
|
||||
let mut command = doc! { "createUser": username };
|
||||
command.extend(user);
|
||||
if let Some(write_concern_json) = write_concern_json.filter(|value| !value.trim().is_empty()) {
|
||||
let write_concern_value: serde_json::Value = serde_json::from_str(write_concern_json)
|
||||
.map_err(|error| format!("Invalid MongoDB write concern JSON: {error}"))?;
|
||||
let write_concern = json_object_to_document_extended_json(&write_concern_value)
|
||||
.map_err(|error| format!("Invalid MongoDB write concern: {error}"))?;
|
||||
command.insert("writeConcern", write_concern);
|
||||
}
|
||||
Ok(command)
|
||||
}
|
||||
|
||||
/// Validate an index request before it reaches either the native driver or the
|
||||
/// Legacy Agent. In particular, `key` belongs to the createIndexes command
|
||||
/// itself and must not be smuggled through the options document.
|
||||
@@ -3013,6 +3055,35 @@ mod tests {
|
||||
assert_eq!(error, "Collection name is required");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_user_command_preserves_roles_and_write_concern() {
|
||||
let command = create_user_command(
|
||||
r#"{"user":"test-db","pwd":"test-password","roles":[{"role":"readWrite","db":"db1"}]}"#,
|
||||
Some(r#"{"w":"majority","wtimeout":5000}"#),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(command.keys().next().map(String::as_str), Some("createUser"));
|
||||
assert_eq!(command.get_str("createUser").unwrap(), "test-db");
|
||||
assert_eq!(command.get_str("pwd").unwrap(), "test-password");
|
||||
let roles = command.get_array("roles").unwrap();
|
||||
assert_eq!(roles[0].as_document().unwrap(), &doc! { "role": "readWrite", "db": "db1" });
|
||||
assert_eq!(command.get_document("writeConcern").unwrap(), &doc! { "w": "majority", "wtimeout": 5000_i32 });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_user_command_rejects_missing_names_and_reserved_fields() {
|
||||
for user in [
|
||||
r#"{"pwd":"secret","roles":[]}"#,
|
||||
r#"{"user":"","pwd":"secret","roles":[]}"#,
|
||||
r#"{"user":"app","createUser":"other","pwd":"secret","roles":[]}"#,
|
||||
r#"{"user":"app","writeConcern":{"w":1},"pwd":"secret","roles":[]}"#,
|
||||
] {
|
||||
assert!(create_user_command(user, None).is_err(), "{user}");
|
||||
}
|
||||
assert!(create_user_command(r#"{"user":"app","pwd":"secret","roles":[]}"#, Some("true")).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_indexes_command_keeps_raw_options_and_generates_a_name() {
|
||||
let (command, name) = create_indexes_command(
|
||||
|
||||
Reference in new issue
Block a user