Our company recently changed from Basic auth to LDAP authentication and regular apps are now using authSource=$external&authMechanism=PLAIN on the URL.
This works fine on a normal app but I can't figure out how to set these using Spring Data. Surely there is a way to do this, right?
As you can see below you can't set something like authSource/authMechanism so you have to change to use URI. Also note when using URI you cannot use host/port and username/password. Those all need to go in the URI.
Something like
export spring_data_mongodb_database=db
export spring_data_mongodb_uri = mongodb://username:password#host:port?authSource=$external&authMechanism=PLAIN
Here all the properties that you can currently configure for mongodb in spring boot.
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
spring.data.mongodb.authentication-database= # Authentication database name.
spring.data.mongodb.database=test # Database name.
spring.data.mongodb.field-naming-strategy= # Fully qualified name of the FieldNamingStrategy to use.
spring.data.mongodb.grid-fs-database= # GridFS database name.
spring.data.mongodb.host=localhost # Mongo server host.
spring.data.mongodb.password= # Login password of the mongo server.
spring.data.mongodb.port=27017 # Mongo server port.
spring.data.mongodb.repositories.enabled=true # Enable Mongo repositories.
spring.data.mongodb.uri=mongodb://localhost/test # Mongo database URI. When set, host and port are ignored.
spring.data.mongodb.username= # Login user of the mongo server.
If you are using Spring-Boot mongodb api with LDAP then the uri has to be the following in application.properties files in the resources folder:
spring.data.mongodb.uri="mongodb://username:password#host:port/?authSource=$external&authMechanism=PLAIN"
spring.data.mongodb.database="databasThatYouWantToConnectTo"
No need for other parameters like spring.data.mongodb.username, spring.data.mongodb.password, etc., because everhything is mentioned in the spring.data.mongodb.uri
the substring authSource=$external&authMechanism=PLAIN in spring.data.mongodb.uri says Mongo that the authentication and authorization is by LDAP.
You can check the image below which is taken from the Connection String URI format document form MongoDB
If you are using the Mongo Java Driver provided by Mongo Db then you have to do the connection like this:
import com.mongodb.MonogoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoDatabase;
public class MongoConnect{
public static void main(String args[]){
MongoClientURI connString = new MongoClientURI("mongodb://username:password#host:port/?authSource=$external&authMechanism=PLAIN");
MongoClient mongoClient = new MongoClient(connString);
MongoDatabase database = mongoClient.getDatabase("databasThatYouWantToConnectTo");
System.out.println(database.getName()+" "+ mongoClient.getCredentialList());
}
}
Related
I have drupal8 application where I use various dbs. When i try to connect with mongo db by giving username and password, connection is not happening. I could connect with mongo db by mentioning server address alone.
note: mongodb is configured with username and password in my server
I tried below methods.
$this->connect_string = 'mongodb://user:password#serveraddress'; (not connecting)
$this->connect_string = 'mongodb://serveraddress'; (connecting)
#$this->connect_string = 'mongodb://user:password#serveraddress:27017/?authSource=dbname';(not connecting)
Php v-7.4, Drupal 8
developed custom module to connect with mongo db, to display news feed (news feed related information stored in mongo db).
how to pass user name & password to connect with mongo db?
Could not find any error logs in mongodb server.
I'm trying to follow the Flask-User documentation with MongoDB.
However, when I try to connect to my MongoDB Atlas, it's always writing to a "test" database, instead of the one I specify.
I have identified that it is because of the Flask-MongoEngine that is being used, as it mentions that
By default, Flask-MongoEngine assumes that the mongod instance is
running on localhost on port 27017, and you wish to connect to the
database named test.
However, even after inputting my URI details, I still keep writing to the test database.
Uri style connections are also supported, just supply the uri as the
hostin the ‘MONGODB_SETTINGS’ dictionary with app.config. Note that
database name from uri has priority over name.
app.config['MONGODB_SETTINGS'] = { 'db': 'project1', 'host': 'mongodb://localhost/database_name' }
My code
My configuration settings (albeit in another syntax) are as below
app.config['MONGODB_DB'] = '<redcated-database-name>'
app.config['MONGODB_HOST'] = 'mongodb+srv://<redcated-user>:<redcated-pw>#gapcluster-np4bu.mongodb.net/test?retryWrites=true&w=majority'
app.config['MONGODB_PORT'] = 27017
app.config["MONGODB_CONNECTION"] = False
Has anyone successfully used Flask-User with MongoDB Atlas before?
Problem Summary
I'm trying to setup a python eve REST API server with mongodb at localhost 5000 (http://127.0.0.1:5000/). In the beginning, my mongodb was hosted at localhost 27017, and the API connection worked perfectly. But I want to change the database to a cloud server, so I dumped my db to MongoDB Atlas and tried to revise the MONGO_HOST and add the user credentials in the settings.py file. But it didn't work. Here's the code I've tried to revise.
Before change
settings.py
MONGO_HOST = 'localhost'
MONGO_PORT = 27017
MONGO_DBNAME = 'mydb'
...
SERVER_NAME = '127.0.0.1:5000'
URL_PREFIX = 'api'
After change
settings.py
MONGO_HOST = '<clustername>-mz123.gcp.mongodb.net'
MONGO_USERNAME = 'user'
MONGO_PASSWORD = 'user'
MONGO_DBNAME = 'mydb'
...
SERVER_NAME = '127.0.0.1:5000'
URL_PREFIX = 'api'
I've also tried to follow the instruction from official to access the mongoDB Atlas with pymongo and it worked successfully as below. So I think the problem may exactly be how to set the eve settings.py file for connecting to MongoDB Atlas in the correct way.
from pymongo import MongoClient
client = MongoClient("mongodb+srv://user:password#<clustername>-mz123.gcp.mongodb.net/test?retryWrites=true&w=majority")
db = client['db']
col = db['collection']
My questions and thoughts
Is there any setting parameter I just missed? I'm not sure if the name of MONGO_HOST is wrong, but I just got it from the instruction on Atlas website. Or do I need to change the setting to the form like MONGO_URI instead? But I've tried and failed, too. LOL
Does the eve REST API serve only for localhost DB (27017) when serving at localhost 5000?
Any suggestions? Thanks!
In your settings.py, define your MONGO_URI with the param you've used in your MongoClient example. See MONGO_URI here in the documentation (https://docs.python-eve.org/en/stable/config.html#global-configuration)
https://spring.io/guides/gs/accessing-data-mongodb/ - The example does not show connecting to Mongo DB usage.
default spring data mongo db is connect your localhost if you not declare any connfiguration properties in applicaiton.properties file
for example
launch mongodb server with mongod command and after launch your application and make any CRUD operation see log in your command prompt
if your application need special server uri or any configuration properties
add application.properties file this properties and configure
for example
spring.data.mongodb.uri=mongodb://user:secret#mongo1.example.com:12345,mongo2.example.com:23456/test
this configure your uri with colleciton names
and also declare host and port with this
spring.data.mongodb.host=mongoserver
spring.data.mongodb.port=27017
and also more properties knowledges(for example: username and password settings) here: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
connecting features here :
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-connecting-to-mongodb
The documentation for Mongo states that when authentication is enabled, and for users added to the admin database, these users should be able to access the other databases in Mongo, with the rights granted at the admin database level.
"The admin database is unique. Users with normal access to the admin database have read and write access to all databases. Users with read only access to the admin database have read only access to all databases." http://docs.mongodb.org/manual/administration/security/
But in testing with the C# library version 1.7.0.4714, this is not the case.
Only accounts created in a specific database have access to that database.
I have tested with credentials on the connection string
and by setting credentials explicitly at the database level in C#
server.GetDatabase(...
new MongoClient(a connectionString ...
Does anyone know if this expected behavior? or can suggest a resolution.
The answer was already posted here on stackoverflow :)
Mongodb C# driver - can't use admin authentication to access other databases
the username used should have (admin) after it to use that account.
This is not a mongodb problem. I am sure that you could authenticate from mongodb shell like this:
use admin
db.auth(user, pass)
This is a mongodb c# driver trick. A long time ago i spent some time to read c# driver code in order to understand this.
So connection string should be like this:
mongodb://admin(admin):1#localhost:27020/myDb
The trick in (admin) in order to tell the driver that you are going to authenticate via admin user.
There is another way to connect with authentication against the admin database.
The downside is that you have to setup the whole connection object instead of packing all info solely on a connection string.
Instead of instantiating the MongoClient with a connection string like
var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
you can create a MongoClientSettings object, set the credentials (along with any other connection settings) and instantiate the client passing that object
string authenticationDB = "admin"
string authenticationUsername = "user"
string authenticationPassword = "pass"
MongoClientSettings settings = new MongoClientSettings();
settings.Credentials = new[] { MongoCredential.CreateMongoCRCredential(authenticationDB, authenticationUsername, authenticationPassword) };
settings.Servers = new[] { new MongoServerAddress("host_1"), new MongoServerAddress("host_2"), new MongoServerAddress("host_3") };
settings.ConnectionMode = ConnectionMode.ReplicaSet;
var client = new MongoClient(settings);
var db = client.GetServer().GetDatabase(database);
http://docs.mongodb.org/ecosystem/tutorial/authenticate-with-csharp-driver/