Permalink
Browse files

Fix auth bug where auth only was done when a command was send instead…

… when another message was send.
  • Loading branch information...
1 parent 0a64703 commit 8f24af552018e344389c16dcc7bc5c9a1715b54d @lanwin lanwin committed Jun 3, 2010
@@ -44,14 +44,14 @@ public void TestReconnectOnce()
try
{
var qmsg = GenerateQueryMessage();
- conn.SendTwoWayMessage(qmsg);
+ conn.SendTwoWayMessage(qmsg,string.Empty);
}
catch(IOException)
{
//Should be able to resend.
Assert.IsTrue(conn.IsConnected);
var qmsg = GenerateQueryMessage();
- var rmsg = conn.SendTwoWayMessage(qmsg);
+ var rmsg = conn.SendTwoWayMessage(qmsg, string.Empty);
Assert.IsNotNull(rmsg);
}
}
@@ -64,7 +64,7 @@ public void TestSendQueryMessage()
conn.Open();
var qmsg = GenerateQueryMessage();
- conn.SendTwoWayMessage(qmsg);
+ conn.SendTwoWayMessage(qmsg, string.Empty);
conn.Close();
}
@@ -81,6 +81,19 @@ public void TestLoginBadPassword()
}
}
+ [Test]
+ [ExpectedException(typeof(MongoException))]
+ public void TryUnautenticatedInsertWithoutSendingACommand()
+ {
+ using(var mongo = ConnectAndAuthenticatedMongo("noexisting", "noexisting"))
+ {
+ mongo.Connect();
+
+ var collection = mongo[TestDatabaseName]["testCollection"];
+ collection.Insert(new Document().Add("value", 84), false);
+ }
+ }
+
private Mongo ConnectAndAuthenticatedMongo(string username,string password)
{
var builder = new MongoConnectionStringBuilder(_connectionString)
@@ -61,13 +61,14 @@ public Connection(IConnectionFactory factory)
get { return _connection.EndPoint; }
}
- /// <summary>
+ /// <summary>
/// Sends the two way message.
/// </summary>
/// <param name="message">The MSG.</param>
+ /// <param name="database">The database.</param>
/// <returns></returns>
- public ReplyMessage<Document> SendTwoWayMessage(IRequestMessage message){
- return SendTwoWayMessage<Document>(message,new BsonReaderSettings());
+ public ReplyMessage<Document> SendTwoWayMessage(IRequestMessage message, string database){
+ return SendTwoWayMessage<Document>(message,new BsonReaderSettings(), database);
}
/// <summary>
@@ -76,42 +77,77 @@ public Connection(IConnectionFactory factory)
/// <typeparam name="T"></typeparam>
/// <param name="message">The message.</param>
/// <param name="readerSettings">The reader settings.</param>
+ /// <param name="database">The database.</param>
/// <returns></returns>
/// <exception cref="IOException">A reconnect will be issued but it is up to the caller to handle the error.</exception>
- public ReplyMessage<T> SendTwoWayMessage<T>(IRequestMessage message, BsonReaderSettings readerSettings) where T:class {
+ public ReplyMessage<T> SendTwoWayMessage<T>(IRequestMessage message, BsonReaderSettings readerSettings, string database) where T:class {
+ AuthenticateIfRequired(database);
+
+ return SendTwoWayMessageCore<T>(message, readerSettings);
+ }
+
+ /// <summary>
+ /// Sends the two way message core.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="message">The message.</param>
+ /// <param name="readerSettings">The reader settings.</param>
+ /// <returns></returns>
+ internal ReplyMessage<T> SendTwoWayMessageCore<T>(IRequestMessage message, BsonReaderSettings readerSettings) where T : class
+ {
if(!IsConnected)
- throw new MongoConnectionException ("Operation cannot be performed on a closed connection.", this);
-
- try {
+ throw new MongoConnectionException("Operation cannot be performed on a closed connection.", this);
+
+ try
+ {
var reply = new ReplyMessage<T>(readerSettings);
- lock (_connection) {
- message.Write (_connection.GetStream ());
- reply.Read (_connection.GetStream ());
+ lock(_connection)
+ {
+ message.Write(_connection.GetStream());
+ reply.Read(_connection.GetStream());
}
return reply;
- } catch (IOException) {
- ReplaceInvalidConnection ();
+ }
+ catch(IOException)
+ {
+ ReplaceInvalidConnection();
throw;
}
-
+
}
/// <summary>
/// Used for sending a message that gets no reply such as insert or update.
/// </summary>
/// <param name="message">The message.</param>
+ /// <param name="database">The database.</param>
/// <exception cref="IOException">A reconnect will be issued but it is up to the caller to handle the error.</exception>
- public void SendMessage (IRequestMessage message){
+ public void SendMessage(IRequestMessage message, string database){
+ AuthenticateIfRequired(database);
+
+ SendMessageCore(message);
+ }
+
+ /// <summary>
+ /// Sends the message core.
+ /// </summary>
+ /// <param name="message">The message.</param>
+ internal void SendMessageCore(IRequestMessage message)
+ {
if(!IsConnected)
throw new MongoConnectionException("Operation cannot be performed on a closed connection.", this);
-
- try {
- lock (_connection) {
- message.Write (_connection.GetStream ());
+
+ try
+ {
+ lock(_connection)
+ {
+ message.Write(_connection.GetStream());
}
- } catch (IOException) {
+ }
+ catch(IOException)
+ {
//Sending doesn't seem to always trigger the detection of a closed socket.
- ReplaceInvalidConnection ();
+ ReplaceInvalidConnection();
throw;
}
}
@@ -134,7 +170,7 @@ public bool IsConnected
/// A <see cref="System.String"/>
/// </param>
public void SendMsgMessage (String message){
- SendMessage(new MsgMessage{Message = message});
+ SendMessageCore(new MsgMessage{Message = message});
}
/// <summary>
@@ -250,10 +286,10 @@ private T SendCommandCore<T>(ISerializationFactory factory, string database, Typ
try
{
- var reply = SendTwoWayMessage<T>(query, readerSettings);
+ var reply = SendTwoWayMessageCore<T>(query, readerSettings);
if(reply.CursorId > 0)
- SendMessage(new KillCursorsMessage(reply.CursorId));
+ SendMessage(new KillCursorsMessage(reply.CursorId),database);
return reply.Documents.FirstOrDefault();
}
@@ -298,7 +334,10 @@ private void AuthenticateIfRequired(string databaseName)
};
try
{
- SendCommandCore<Document>(serializationFactory, databaseName, typeof(Document), auth);
+ var result = SendCommandCore<Document>(serializationFactory, databaseName, typeof(Document), auth);
+
+ if(!Convert.ToBoolean(result["ok"]))
+ throw new MongoException("Authentication faild for " + builder.Username);
}
catch(MongoCommandException exception)
{
View
@@ -14,7 +14,8 @@ namespace MongoDB
/// <typeparam name="T"></typeparam>
public class Cursor<T> : ICursor<T> where T : class
{
- private readonly Connection _connection;
+ private readonly Connection _connection;
+ private readonly string _databaseName;
private readonly Document _specOpts = new Document();
private object _spec;
private object _fields;
@@ -30,13 +31,15 @@ public class Cursor<T> : ICursor<T> where T : class
/// </summary>
/// <param name="serializationFactory">The serialization factory.</param>
/// <param name="connection">The conn.</param>
- /// <param name="fullCollectionName">Full name of the collection.</param>
- public Cursor(ISerializationFactory serializationFactory, Connection connection, string fullCollectionName)
+ /// <param name="databaseName">Name of the database.</param>
+ /// <param name="collectionName">Name of the collection.</param>
+ public Cursor(ISerializationFactory serializationFactory, Connection connection, string databaseName, string collectionName)
{
IsModifiable = true;
//Todo: should be internal
- _connection = connection;
- FullCollectionName = fullCollectionName;
+ _connection = connection;
+ _databaseName = databaseName;
+ FullCollectionName = databaseName + "." + collectionName;
_serializationFactory = serializationFactory;
}
@@ -45,13 +48,14 @@ public Cursor(ISerializationFactory serializationFactory, Connection connection,
/// </summary>
/// <param name="serializationFactory">The serialization factory.</param>
/// <param name="connection">The conn.</param>
- /// <param name="fullCollectionName">Full name of the collection.</param>
+ /// <param name="databaseName">Name of the database.</param>
+ /// <param name="collectionName">Name of the collection.</param>
/// <param name="spec">The spec.</param>
/// <param name="limit">The limit.</param>
/// <param name="skip">The skip.</param>
/// <param name="fields">The fields.</param>
- public Cursor(ISerializationFactory serializationFactory, Connection connection, string fullCollectionName, object spec, int limit, int skip, object fields)
- : this(serializationFactory, connection, fullCollectionName)
+ public Cursor(ISerializationFactory serializationFactory, Connection connection, string databaseName, string collectionName, object spec, int limit, int skip, object fields)
+ : this(serializationFactory, connection, databaseName, collectionName)
{
//Todo: should be internal
if (spec == null)
@@ -300,7 +304,7 @@ private void KillCursor(long cursorId)
var killCursorsMessage = new KillCursorsMessage(cursorId);
try {
- _connection.SendMessage(killCursorsMessage);
+ _connection.SendMessage(killCursorsMessage,_databaseName);
Id = 0;
} catch (IOException exception) {
throw new MongoConnectionException("Could not read data, communication failure", _connection, exception);
@@ -341,7 +345,8 @@ private void KillCursor(long cursorId)
try
{
- var reply = _connection.SendTwoWayMessage<TReply>(message, readerSettings);
+
+ var reply = _connection.SendTwoWayMessage<TReply>(message, readerSettings, _databaseName);
Id = reply.CursorId;
@@ -158,7 +158,7 @@ public T FindOne(string javascriptWhere)
public ICursor<T> Find(object spec, int limit, int skip, object fields){
if (spec == null)
spec = new Document();
- return new Cursor<T>(_configuration.SerializationFactory, _connection, FullName, spec, limit, skip, fields);
+ return new Cursor<T>(_configuration.SerializationFactory, _connection, DatabaseName, Name, spec, limit, skip, fields);
}
/// <summary>
@@ -325,7 +325,7 @@ public T FindOne(string javascriptWhere)
insertMessage.Documents = insertDocument.ToArray();
try {
- _connection.SendMessage(insertMessage);
+ _connection.SendMessage(insertMessage,DatabaseName);
} catch (IOException exception) {
throw new MongoConnectionException("Could not insert document, communication failure", _connection, exception);
}
@@ -376,7 +376,7 @@ public void Delete(object selector, bool safemode)
{
FullCollectionName = FullName,
Selector = selector
- });
+ },DatabaseName);
} catch (IOException exception) {
throw new MongoConnectionException("Could not delete document, communication failure", _connection, exception);
}
@@ -398,7 +398,7 @@ public void Delete(object selector, bool safemode)
{
FullCollectionName = FullName,
Selector = selector
- });
+ }, DatabaseName);
}
catch(IOException exception)
{
@@ -478,7 +478,7 @@ public void Update(object document, bool safemode)
Selector = selector,
Document = document,
Flags = (int)flags
- });
+ }, DatabaseName);
} catch (IOException exception) {
throw new MongoConnectionException("Could not update document, communication failure", _connection, exception);
}

0 comments on commit 8f24af5

Please sign in to comment.