bkubiak
bkubiak

Reputation: 325

Mongo Query failed with error code 13 and error message 'not authorized'

Error occures when I am trying to make an query to mongo instance on mLab. I am requesting it from spring boot app. Connection to database is stable. Below log from console.

Monitor thread successfully connected to server with description ServerDescription{address=ds131531.mlab.com:31531, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 15]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=null, roundTripTimeNanos=138364906, setName='rs-ds131531', canonicalAddress=ds131531-a.mlab.com:31531, hosts=[ds131531-a.mlab.com:31531], passives=[], arbiters=[], primary='ds131531-a.mlab.com:31531', tagSet=TagSet{[]}, electionId=7fffffff0000000000000001, setVersion=1, lastWriteDate=Wed Jul 18 15:22:37 CEST 2018, lastUpdateTimeNanos=20828794373992}

This is exact error message

Query failed with error code 13 and error message 'not authorized on pizza-store to execute command { find: "product", filter: {}, batchSize: 2147483647 }' on server ds131531.mlab.com:31531; nested exception is com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'not authorized on pizza-store to execute command { find: "product", filter: {}, batchSize: 2147483647 }' on server ds131531.mlab.com:31531

If i make the same query from mongo shell everything is ok. I have added readWrite role and my custom readProduct role role to user who is making request but nothing is helping. dbOwner and read are builtin roles.

Users mongo roles:

"_id" : "pizza-db.kubiakb",
"user" : "kubiakb",
"db" : "pizza-db",
"roles" : [
    {
        "role" : "readProduct",
        "db" : "pizza-db"
    },
    {
        "role" : "read",
        "db" : "pizza-db"
    },
    {
        "role" : "dbOwner",
        "db" : "pizza-db"
    }
]

readProduct role

{
"role" : "readProduct",
"db" : "pizza-db",
"isBuiltin" : false,
"roles" : [ ],
"inheritedRoles" : [ ],
"privileges" : [
    {
        "resource" : {
            "db" : "pizza-db",
            "collection" : "product"
        },
        "actions" : [
            "find"
        ]
    }
],
"inheritedPrivileges" : [
    {
        "resource" : {
            "db" : "pizza-db",
            "collection" : "product"
        },
        "actions" : [
            "find"
        ]
    }
]

}

Edit: Spring application.properties

spring:
  profiles: heroku
  data:
    mongodb:
      uri: mongodb://login:[email protected]:31531/pizza-db

Upvotes: 4

Views: 21726

Answers (2)

Guilherme
Guilherme

Reputation: 897

I created the user with roles on another db. I spent some hours to get that. I know that you fix, but maybe helps others devs.

db.createUser({user: "right_user", pwd: "right_pass",
roles: [{ role: "readWrite", db: "WRONG_DB" }], passwordDigestor: "server", mechanisms: ["SCRAM-SHA-1","SCRAM-SHA-256"]})

Upvotes: 1

bkubiak
bkubiak

Reputation: 325

Problem solved. The issue was i have more profiles on my application.yml file and i don't declare database name in profile heroku so it was reading database name from default profile. Below application.yml file

spring:
    data:
      mongodb:
        database: pizza-store
        uri: mongodb://localhost:27017

price-point-multiplier : 0.1

---
spring:
  profiles: docker
  data:
    mongodb:
      database: pizza-store
      uri: mongodb://mongodb:27017

---
spring:
  profiles: heroku
  data:
    mongodb:
      uri: ${MONGODB_URI}

Upvotes: 3

Related Questions