MongoDB: PLAIN method not suported - mongodb

I'm getting this message when I'm trying to deploy my war artifact. My app is using hibernate ogm and it's trying to build persistence context when it's deployed. The message I'm getting is:
org.hibernate.service.spi.ServiceException: OGM000071: Unable to start datatore provider Caused by: org.hibernate.service.spi.ServiceException: OGM000071: Unable to start datatore provider Caused by: org.hibernate.HibernateException: OGM001214: Unable to connect to MongoDB instance: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=mongo:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=PLAIN, userName='living', source='lvdb', password=, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 2: 'Unsupported mechanism PLAIN' on server mongo:27017. The full response is { \"supportedMechanisms\" : [\"MONGODB-CR\", \"MONGODB-X509\", \"SCRAM-SHA-1\"], \"ok\" : 0.0, \"errmsg\" : \"Unsupported mechanism PLAIN\", \"code\" : 2, \"codeName\" : \"BadValue\" }}}] Caused by: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=mongo:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=PLAIN, userName='living', source='lvdb', password=, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 2: 'Unsupported mechanism PLAIN' on server mongo:27017. The full response is { \"supportedMechanisms\" : [\"MONGODB-CR\", \"MONGODB-X509\", \"SCRAM-SHA-1\"], \"ok\" : 0.0, \"errmsg\" : \"Unsupported mechanism PLAIN\", \"code\" : 2, \"codeName\" : \"BadValue\" }}}]"}}
What do I need to do in order to use the other mechanisms?

You'd have to specificy the property hibernate.ogm.mongodb.authentication_mechanism (see here).
Which is the authentication mechanism you'd want to use? We may not support it yet, but if you let us know about your preference, we can try and look into making it work quickly.

For MongoDB, default supported mechanism is 'SCRAM-SHA-1'. So, while connecting, you need to specify this mechanism. I saw the same error when connecting to MongoDB through spring-data. While I didn't make any special configuration for mongo, first and obvious option for me to try was PLAIN mechanism for authentication. Finally, resolved it using SCRAM-SHA-1.
For Spring-data, code looked something like this:
MongoCredential mongoCredential = MongoCredential.createScramSha1Credential(mongoUser, mongoDB, mongoPass.toCharArray());
Hope this helps.

Related

Connecting to MongoDB from Camel-K

How do I connect to MongoDB with Camel-K (version 1.6.0) ?
I've tried setting the host name directly on the MongoDB Camel component URI:
...
.to("mongodb:mongoBean?database=[redacted]&collection=[redacted]&hosts=[redacted]&username=[redacted]&password=[redacted]&operation=getDbStats")
but it insists on calling 127.0.0.1:27017:
com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]
at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:177)
Probably I should be setting a class reference in a properties file, like so:
camel.beans.mongoBean = #class:...
but then I'm not sure what class that should be? - and what other property names to set?
The full Camel-K integration Java file I'm deploying to the Kubernetes cluster (OpenShift CodeReady containers, with the command kamel run MongoDBTest.java --dev), looks like this:
// camel-k: language=java property=file:integration.properties
import org.apache.camel.builder.RouteBuilder;
public class MongoDBTest extends RouteBuilder {
#Override
public void configure() throws Exception {
from("timer:java?period=1000")
.routeId("java")
.to("mongodb:mongoBean?database=[redacted]&collection=[redacted]&operation=getDbStats&hosts=[redacted]&username=[redacted]&password=[redacted]")
.to("log:info");
}
}
and in the integration.properties file I have:
camel.beans.mongoBean = #class:com.mongodb.MongoClient
should there be some other class reference?
if not setting the host name on the MongoDB Camel component URI, what properties should come in there under camel.beans.mongoBean?
The solution was to set the quarkus.mongodb... Camel Quarkus MongoDB properties, as pointed out here: https://github.com/apache/camel-k/issues/2684#issuecomment-939798018

MongoDB config for Spring Boot - 'url' with and without authentication credentials

I want to make the application.yml work with and without username/password when running tests. My server needs the credentials but when I am on localhost I want it to work without an authentication and with the default settings. The spring profile ("integrationtest") should stay the same.
I have the following Spring Boot MongoDB config:
mongodb:
uri: mongodb://${mongodb_username:testuser}:${mongodb_password:testpassword}#${mongodb_ip:localhost}:${mongodb_port:27017}/testDB
If I remove the username and password I get this Error:
org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='testuser', source='testDB', password=<hidden>, mechanismProperties=<hidden>}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='testuser', source='testDB', password=<hidden>, mechanismProperties=<hidden>}
Caused by: com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server localhost:27017. The full response is {"ok": 0.0, "errmsg": "Authentication failed.", "code": 18, "codeName": "AuthenticationFailed"}
How can I make it work with the same profile? Maybe there is a way with nested variables? Thank you.
A simple solution is to put the complete uri in a single variable, localhost works with the default config and the server specifies the uri at server start. The config looks like:
uri: ${mongodb_uri:mongodb://localhost:27017/testDB}
At server start override it with:
-Dmongodb_uri=mongodb://testUser:testPassword#example.com:27017/testDB

Unable to connect to CosmosDb Mongo Api from SpringBoot Application

Trying to connect to cosmosdb Mongo API using Primary Connection String and UserName(Database Name)
Caused by: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1#3c291aad. Client view of cluster state is {type=REPLICA_SET, servers=[{address=**********.documents.azure.com:10255, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {java.net.SocketException: Connection reset}}]
at com.mongodb.internal.connection.BaseCluster.createTimeoutException(BaseCluster.java:401) ~[mongodb-driver-core-3.8.2.jar:na]
at com.mongodb.internal.connection.BaseCluster.selectServer(BaseCluster.java:120) ~[mongodb-driver-core-3.8.2.jar:na]
at com.mongodb.internal.connection.MultiServerCluster.selectServer(MultiServerCluster.java:54) ~[mongodb-driver-core-3.8.2.jar:na]
at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:138) ~[mongodb-driver-3.8.2.jar:na]
at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:94) ~[mongodb-driver-3.8.2.jar:na]
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:249) ~[mongodb-driver-3.8.2.jar:na]
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:190) ~[mongodb-driver-3.8.2.jar:na]
at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:960) ~[mongodb-driver-3.8.2.jar:na]
at com.mongodb.client.internal.MongoCollectionImpl.executeDelete(MongoCollectionImpl.java:940) ~[mongodb-driver-3.8.2.jar:na]
at com.mongodb.client.internal.MongoCollectionImpl.deleteMany(MongoCollectionImpl.java:551) ~[mongodb-driver-3.8.2.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate$9.doInCollection(MongoTemplate.java:1729) ~[spring-data-mongodb-2.1.9.RELEASE.jar:2.1.9.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate$9.doInCollection(MongoTemplate.java:1689) ~[spring-data-mongodb-2.1.9.RELEASE.jar:2.1.9.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:545) ~[spring-data-mongodb-2.1.9.RELEASE.jar:2.1.9.RELEASE]
... 30 common frames omitted
Please follow the example github project in the link below and you will be able to connect.
https://github.com/Azure-Samples/azure-cosmos-db-mongodb-spring
Try with MongoDB driver 3.7.1 or earlier.
It appears that’s there’s an issue with MongoDB driver for Java 3.8.0 and above and the way Cosmos DB takes the MongoDB protocol.

Dataproc trying to connect to Postgres through JDBC, missing permissions

Thanks in advance...
I want to connect/write using JDBC APIs to a Postgres SQL instance running using Cloud SQL programmatically. I have used following jars:
postgresql
postgres-socket-factory
postgres-socket-factory-1.0.11-jar-with-dependencies.jar
I am running Dataproc which will try to connect using step #1, but I get following exception:
2019-04-01 11:05:03.998 IST
Something unusual has occurred to cause the driver to fail. Please report this exception.
at org.postgresql.Driver.connect(Driver.java:277)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at rdsConnector$.getConnection(rdsConnector.scala:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.spark.deploy.yarn.ApplicationMaster$$anon$4.run(ApplicationMaster.scala:721)
Caused by: java.lang.RuntimeException: Unable to retrieve information about Cloud SQL instance [projectID:us-east1:dB]
at com.google.cloud.sql.core.SslSocketFactory.obtainInstanceMetadata(SslSocketFactory.java:459)
at com.google.cloud.sql.core.SslSocketFactory.fetchInstanceSslInfo(SslSocketFactory.java:333)
at com.google.cloud.sql.core.SslSocketFactory.getInstanceSslInfo(SslSocketFactory.java:313)
at com.google.cloud.sql.core.SslSocketFactory.createAndConfigureSocket(SslSocketFactory.java:194)
at com.google.cloud.sql.core.SslSocketFactory.create(SslSocketFactory.java:160)
at com.google.cloud.sql.postgres.SocketFactory.createSocket(SocketFactory.java:96)
at org.postgresql.core.PGStream.<init>(PGStream.java:62)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
at org.postgresql.Driver.makeConnection(Driver.java:454)
at org.postgresql.Driver.connect(Driver.java:256)
... 11 more
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
2019-04-01 11:05:03.000 IST
User class threw exception: org.postgresql.util.PSQLException: Something unusual has occurred to cause the driver to fail. Please report this exception.
I understand this as a permission issue, but since I am using Dataproc to connect to Postgres what permission is missing? if I was running from a local laptop machine then I have to set GOOGLE_APPLICATION_CREDENTIALS to a json file. But what is the process in case of Dataproc?
The JDBC SocketFactory uses the Application Default Credentials strategy for accessing account credentials.
For Cloud Dataproc, a default service account is provided for you at [project-number]-compute#developer.gserviceaccount.com. You grant this account the "Cloud SQL Client" IAM role, the JDBC SocketFactory will use it to authenticate, and thus you will be able to connect to your application.
Just leave further notes for future visitors:
If the error you're getting is like:
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Insufficient Permission",
"reason" : "insufficientPermissions"
} ],
"message" : "Request had insufficient authentication scopes.",
"status" : "PERMISSION_DENIED"
}
then it could be caused by issues with the scopes (Cloud API access scopes) of the VM instances. One way to fix this is adding the scope of sql-admin when creating the Dataproc cluster. For example:
gcloud dataproc clusters create <your-cluster-name> \
--region=<your-region> \
--zone=<your-zone> \
--scopes=https://www.googleapis.com/auth/sqlservice.admin \
...
Another way is edited that in the VM instances.
See the gcloud documentation for details

PostgreSQL on cloudfoundry throws PSQLException: FATAL: terminating connection due to administrator command

I have a grails application successfully running in cloudfoundry with H2 as database. I now want to switch to postgresql. Everything works as expected when I run the application locally against postgreSQL.
On the cloudfoundry instance I get an exception during one rather long running analysis, which I perform as a background task (using grails executor plugin). Within this asynchronous task
def future = executorService.submit({
return analysisService.analyzeProject(model, project)
})
I get the following exception from the database:
2012-11-26 10:27:38,319 [pool-2-thread-1] ERROR interceptor.TransactionInterceptor - Application exception overridden by rollback exception
org.springframework.dao.DataAccessResourceFailureException: Hibernate operation: could not execute query; SQL [select this_.id as id8_0_, this_.version as version8_0_, this_.language as language8_0_, this_.url as url8_0_ from sonar_adapter_configuration this_]; FATAL: terminating connection due to administrator command; nested exception is org.postgresql.util.PSQLException: FATAL: terminating connection due to administrator command
at myapp.adapters.sonar.SonarAdapterService.loadSonarConfig(SonarAdapterService.groovy:184)
at myapp.adapters.sonar.SonarAdapterService.determineArtefactSizes(SonarAdapterService.groovy:145)
at myapp.project.AnalysisService.analyzeProject(AnalysisService.groovy:46)
at myapp.project.ProjectController$_analyzeProject_closure2.doCall(ProjectController.groovy:69)
at grails.plugin.executor.PersistenceContextRunnableWrapper$_run_closure1.doCall(PersistenceContextRunnableWrapper.groovy:34)
at grails.plugin.executor.PersistenceContextWrapper.wrap(PersistenceContextWrapper.groovy:35)
at grails.plugin.executor.PersistenceContextRunnableWrapper.run(PersistenceContextRunnableWrapper.groovy:34)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.postgresql.util.PSQLException: FATAL: terminating connection due to administrator command
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:273)
... 13 more
Any ideas what might cause this?
Chances are you are hitting the configured timeout for long running connections (which can't be changed if I'm not mistaken, as it is here to protect the server from ill formed queries that would consume all cpu/io)
What you may want to try is to slice your long running query into smaller queries and sum up the results (if it's a select)