0

I'm studying the Vertx MongoClient API. I previously installed Restheart from Docker and it's own copy of mongodb, so now I have the default configuration for Restheart and the default configuration of Mongo in docker-compose.yml:

        MONGO_INITDB_ROOT_USERNAME: restheart
        MONGO_INITDB_ROOT_PASSWORD: R3ste4rt!

I put the Vertx Mongoclient into a Verticle:

public class MongoClientVerticle extends AbstractVerticle {

    MongoClient mongoClient;
    String db = "monica";
    String collection = "sessions";
    String uri = "mongodb://localhost:27017";
    String username = "admin";
    String password = "password";
    MongoAuth authProvider;

    @Override
    public void start() throws Exception {

        JsonObject config = Vertx.currentContext().config();


        JsonObject mongoconfig = new JsonObject()
                .put("connection_string", uri)
                .put("db_name", db);

        mongoClient = MongoClient.createShared(vertx, mongoconfig);
        JsonObject authProperties = new JsonObject();
        authProvider = MongoAuth.create(mongoClient, authProperties);
//        authProvider.setHashAlgorithm(HashAlgorithm.SHA512);
        JsonObject authInfo = new JsonObject()
                .put("username", username)
                .put("password", password);
        authProvider.authenticate(authInfo, res -> {
            if (res.succeeded()) {
                User user = res.result();
                System.out.println("User " + user.principal() + " is now authenticated");
            } else {
                res.cause().printStackTrace();
            }
        });
    }

and I built a simple query:

public void find(int limit) {
    JsonObject query = new JsonObject();
    FindOptions options = new FindOptions();
    options.setLimit(1000);
    mongoClient.findWithOptions(collection, query, options, res -> {
        if (res.succeeded()) {
            List<JsonObject> result = res.result();
            result.forEach(System.out::println);
        } else {
            res.cause().printStackTrace();
        }
    });
}

but when I access the db I get this error:

MongoQueryException: Query failed with error code 13 and error message 'there are no users authenticated' on server localhost:27017

What am I missing in the authentication process?

I'm using lastest restheart + mongodb and vertx 3.5.3

Ozeta
  • 321
  • 4
  • 18

1 Answers1

0

To be clear, RESTHeart doesn't come with its own copy of Mongodb but connects to any existing instance of Mongodb. The instance you can start via docker compose is for demo purposes only.

This question is much related to Vertx + Mongodb. I'm not an expert of it, but apparently Vert.x Auth Mongo does not use database accounts to authenticate users, it uses a specific collection (by default the "user" collection). You could double check the Vertx docs in case to be sure about this.

However, note that RESTHeart's main purpose is to provide direct HTTP access to Mongodb, without the need to program any specific client or driver. So the side point is that if you are using Vertx then you presumably don't need RESTHeart, and vice versa. Otherwise, you could simply connect to RESTHeart via Vertx's HTTP client, entirely skipping the MongoClient API.

mturatti
  • 651
  • 5
  • 10