[CSHARP-2190] Permissions issue when using BulkWrite Created: 14/Feb/18  Updated: 05/Apr/19  Resolved: 14/Feb/18

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 2.5
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Denys Kholod Assignee: Unassigned
Resolution: Done Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Hello.
I have a mongodb replicaset cluster with 3 members.
Using shell I can test bulkwrite operation, which works fine

use admin
db.auth("SportsEtl", "<some password>") // returns 1 (OK)
 
use SomeTestDb
db.Markets.bulkWrite([
   { replaceOne :
      {
         "filter" : {_id:"4"},
         "replacement" : { _id:"4", item: "text", defaultQty: 100, status: "A", points: 100 },
         "upsert" : true
      }
   }
],
{ ordered : true }
 )
 
Returns  "acknowledged" : true, an rest of payload.

Now I'm trying to do the same with C# driver

This is my connection string
mongodb://SportsEtl:<some password>@server1:27018,server2:27018,server3:27018/admin?AUTHMechanism=SCRAM-SHA-1

Initialization

var mongoConfig = MongoClientSettings.FromUrl(new MongoUrl(mongoUrl));
var mongoClient = new MongoClient(mongoConfig);
var db = mongoClient.GetDatabase("SomeTestDb");

Code that works fine

_appMetadataCollection = db.GetCollection<AppMetadata>("AppMetadata");
public void UpdateAppMetadata(AppMetadata appMetadata)
{
            _appMetadataCollection.DeleteMany(FilterDefinition<AppMetadata>.Empty);
            _appMetadataCollection.InsertOne(appMetadata);
}

Code that fails

static readonly BulkWriteOptions _bulkWriteOptions = new BulkWriteOptions { IsOrdered = true, BypassDocumentValidation = true };
_marketsCollection = db.GetCollection<Dto.Market>("Markets");
 
public void UpdateMarkets(List<Dto.Market> marketsBatch)
{
var models = marketsBatch
                    .Select(m => new ReplaceOneModel<Dto.Market>(new BsonDocument("_id", m.Id), m) {IsUpsert = true})
                    .ToList();
 
                _marketsCollection.BulkWrite(models, _bulkWriteOptions);
}

Exception message:

MongoDB.Driver.MongoCommandException: Command update failed: not authorized on SomeTestDb to execute command { update: \"Markets\", ordered: true, bypassDocumentValidation: true, $db: \"SomeTestDb\" }.
   at MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.ProcessReply(ConnectionId connectionId, ReplyMessage`1 reply)
   at MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.Execute(IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocol[TResult](IWireProtocol`1 protocol, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.Server.ServerChannel.Command[TResult](DatabaseNamespace databaseNamespace, BsonDocument command, IElementNameValidator commandValidator, Func`1 responseHandling, Boolean slaveOk, IBsonSerializer`1 resultSerializer, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.BulkUnmixedWriteOperationBase.ExecuteProtocol(IChannelHandle channel, BsonDocument command, Func`1 responseHandling, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.BulkUnmixedWriteOperationBase.ExecuteBatch(IChannelHandle channel, BatchableSource`1 requestSource, Int32 originalIndex, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.BulkUnmixedWriteOperationBase.ExecuteBatches(IChannelHandle channel, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.BulkUnmixedWriteOperationBase.Execute(IChannelHandle channel, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation.ExecuteBatch(IChannelHandle channel, Run run, Boolean isLast, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation.Execute(IWriteBinding binding, CancellationToken cancellationToken)
   at MongoDB.Driver.OperationExecutor.ExecuteWriteOperation[TResult](IWriteBinding binding, IWriteOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.ExecuteWriteOperation[TResult](IWriteOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.BulkWrite(IEnumerable`1 requests, BulkWriteOptions options, CancellationToken cancellationToken)
<my project stack goes here>
;

Please suggest what's going - why auth works fine for insert, delete and update but fails for bulk. How can I fix that.

Thanks.



 Comments   
Comment by Jeffrey Yemin [ 14/Feb/18 ]

No problem... Good luck!

Comment by Denys Kholod [ 14/Feb/18 ]

@jeff.yemin thanks a lot, you saved my day!!!
With bypassDocumentValidation=false all works fine.

Comment by Jeffrey Yemin [ 14/Feb/18 ]

I suspect the issue is the use of bypassDocumentValidation in the second example. That flag requires an additional privilege for the user, which is documented here.

To confirm that this is the root cause, try setting bypassDocumentValidation to false in the second example.

If you're not using schema validation, then this flag is irrelevant and should not be set in your application.

Generated at Wed Feb 07 21:41:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.