SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
805 stars 171 forks source link

Unable to override root-mongo-resource with environment variable #420

Closed hussam-qasem closed 2 years ago

hussam-qasem commented 2 years ago

I'm attempting to override the default /restheart db by setting a ROOT_MONGO_RESOURCE environment variable.

Expected Behavior

URI / bound to MongoDB resource other than /restheart - specified by the $ROOT_MONGO_RESOURCE env variable.

Current Behavior

URI / bound to MongoDB resource /restheart.

Context

I am trying to override the default values without extending the Docker image. I tried the following env variables:

Steps to Reproduce

Run the Docker image:

docker run --rm -p 8080:8080 --name restheart \
  -e MONGO_URI="mongodb+srv://dbUser:SECRET@cluster0.xyz.mongodb.net/?retryWrites=true&w=majority" \
  -e ROOT_MONGO_RESOURCE="/thinkpink" \
  softinstigate/restheart:6.1.2

Current output

10:36:47.480 [main] INFO  org.restheart.Bootstrapper - Starting RESTHeart instance default
 10:36:47.482 [main] INFO  org.restheart.Bootstrapper - Version 6.1.2
 10:36:47.539 [main] INFO  org.restheart.Bootstrapper - Logging to console with level INFO
 10:36:47.593 [main] INFO  org.restheart.plugins.PluginsFactory - Found plugin jar file:/opt/restheart/plugins/restheart-mongodb.jar
 10:36:47.594 [main] INFO  org.restheart.plugins.PluginsFactory - Found plugin jar file:/opt/restheart/plugins/restheart-polyglot.jar
 10:36:47.594 [main] INFO  org.restheart.plugins.PluginsFactory - Found plugin jar file:/opt/restheart/plugins/restheart-security.jar
 10:36:47.594 [main] INFO  org.restheart.plugins.PluginsFactory - Found plugin jar file:/opt/restheart/plugins/restheart-graphql.jar
 10:36:48.948 [main] WARN  o.r.m.MongoServiceConfiguration - >>> Found environment variable 'MONGO_URI': overriding parameter 'mongo-uri' with value 'mongodb+srv://dbUser:SECRET@cluster0.xyz.mongodb.net/?retryWrites=true&w=majority'
 10:36:49.036 [main] WARN  o.r.polyglot.PolyglotDeployer - Not running on GraalVM, polyglot plugins deployer disabled!
 10:36:49.154 [main] INFO  o.r.mongodb.db.MongoClientSingleton - Connecting to MongoDB...
 10:36:50.312 [main] INFO  o.r.s.a.MongoRealmAuthenticator - No user found. Created default user with _id BsonString{value='admin'}
 10:36:50.356 [main] INFO  o.r.mongodb.db.MongoClientSingleton - MongoDB version 4.4.10
 10:36:50.356 [main] INFO  o.r.mongodb.db.MongoClientSingleton - MongoDB is a replica set.
 10:36:50.521 [main] INFO  org.restheart.mongodb.MongoService - URI / bound to MongoDB resource /restheart

As seen in the last line above, MongoDB resource/db restheart is created, ignoring the environment variables.

Additional question

It would be great if I could also override default passwords in etc/users.yml without extending the image.

ujibang commented 2 years ago

you can override the users without extending the image simply mounting a volume (-v docker option)

docker run --rm -p 8080:8080 --name restheart \
  -e MONGO_URI="mongodb+srv://dbUser:SECRET@cluster0.xyz.mongodb.net/?retryWrites=true&w=majority" \
  -v /local/path/users.yml:/opt/restheart/etc/users.yml \
  softinstigate/restheart:6.1.2
ujibang commented 2 years ago

@mkjsix can you help on the env variable, seems that it does not pick it up

hussam-qasem commented 2 years ago

so the env variable to set is RH_MONGO_URI as follows:

Thank you @ujibang for the prompt response. I don't believe this is the correct variable. RH_MONGO_URI now overrides MONGO_URI, and I'm unable to connect to MongoDB:

 11:21:23.323 [main] WARN  o.r.m.MongoServiceConfiguration - >>> Found environment variable 'RH_MONGO_URI': overriding parameter 'mongo-uri' with value '/thinkpink'
 11:21:23.328 [main] ERROR org.restheart.plugins.PluginsFactory - Error injecting dependency to Initializer mongoInitializer: The connection string is invalid. Connection strings must start with either 'mongodb://' or 'mongodb+srv://
 java.lang.reflect.InvocationTargetException: null
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Caused by: org.restheart.ConfigurationException: wrong parameter mongo-uri
    at org.restheart.mongodb.MongoServiceConfiguration.<init>(MongoServiceConfiguration.java:217)
Caused by: java.lang.IllegalArgumentException: The connection string is invalid. Connection strings must start with either 'mongodb://' or 'mongodb+srv://
    at com.mongodb.ConnectionString.<init>(ConnectionString.java:290)
ujibang commented 2 years ago

yes @hussam-qasem I misunderstood the problem you reported and deleted my answer!

we are looking into it, your need is clearly to override root-mongo-resource with an env variable

mongo-mounts:
  - what: {{{root-mongo-resource}}}
    where: /
hussam-qasem commented 2 years ago

we are looking into it, your need is clearly to override root-mongo-resource with an env variable

Yes, than you! 🙏🏻

ujibang commented 2 years ago

Found the problem:

from https://restheart.org/docs/setup/#environment-variables

It is possible to override any primitive type parameter in restheart.yml with environment variables.

since mongo-mounts is not a primitive type, you cannot override it with an env var.

However we can easily extend RH to cope with it. We will add this to next release...

As a workaround, you can override the /opt/restheart/etc/restheart.yml configuration file with docker volumes.

hussam-qasem commented 2 years ago

Thank you @ujibang.

rdonadono commented 1 year ago

Hi,

to this day it is still not possible to override the root-mongo-resource parameter via environment.

Was it planned or did it remain in the limbo of GH issues? :)

ujibang commented 1 year ago

Hi @rdonadono

we actually implemented the new configuration override right after your issue but it will be available on RH 7.

The good news is that yesterday we finished all planned new features and going to release RH 7 soon, we just need to do some testing and update the documentation 😩.

You can read about it on https://github.com/SoftInstigate/restheart/discussions/425 (search for "New way to override configuration options with environment variables" in the page)

In short, RESTHeart v7 comes with a default configuration (you can avoid to have a configuration file) and can change it via the RHO env variable as follows:

$ RHO='/mongo-uri->"mongodb://127.0.0.1";/mongo-mounts[1]->'{"where: "/api", "what": "mydb"}' java -jar restheart.jar

Of course you can still use a configuration file.

Be aware that RH7 has a new configuration file format: https://github.com/SoftInstigate/restheart/blob/master/core/src/main/resources/restheart-default-config.yml

Finally you can try v7 snapshot release

$ docker run -it --rm -p "8080:8080" --env RHO='/mclient/connection-string->"mongodb://host.docker.internal";/http-listener/host->"0.0.0.0";/mongo/mongo-mounts[1]->{"where": "/api", "what": "restheart"}' softinstigate/restheart-snapshot:latest

Note the RHO env var:

RHO='/mclient/connection-string->"mongodb://host.docker.internal";/http-listener/host->"0.0.0.0";/mongo/mongo-mounts[1]->{"where": "/api", "what": "restheart"}' 

It defines 3 overrides

You can even add an object to the configuration (so not just overriding), e.g.

In the log, you'll see:

09:20:14.233 [main] INFO  o.r.configuration.Configuration - Overriding configuration parameters from RHO environment variable:
09:20:14.234 [main] INFO  o.r.configuration.Configuration -     /mclient/connection-string -> mongodb://host.docker.internal
09:20:14.234 [main] INFO  o.r.configuration.Configuration -     /http-listener/host -> 0.0.0.0
09:20:14.234 [main] INFO  o.r.configuration.Configuration -     /mongo/mongo-mounts[1] -> Document{{where=/api, what=restheart}}
09:20:14.235 [main] INFO  o.r.configuration.Configuration -     /mongo/mongo-mounts[2] -> Document{{where=/cool, what=anotherDb}}
......
09:20:15.835 [main] INFO  org.restheart.mongodb.MongoService - URI /api bound to MongoDB resource restheart
09:20:15.835 [main] INFO  org.restheart.mongodb.MongoService - URI /cool bound to MongoDB resource anotherDb
ujibang commented 1 year ago

RH7 is out. Read about using the new RHO to override any conf parameter. https://restheart.org/docs/configuration

Beware that the configuration file is now optional and that the format changed