diff options
author | Guobiao Mo <guobiaomo@chinamobile.com> | 2019-04-10 13:44:28 -0700 |
---|---|---|
committer | Guobiao Mo <guobiaomo@chinamobile.com> | 2019-04-10 13:44:28 -0700 |
commit | 115e9b9ca1a4fdbdeba84bbb6f83ebf69ea5b08d (patch) | |
tree | c6c835014f40ee0fc406d3c30d3342f841fb930c | |
parent | 35cc15e04411008b2f8094bbd3876e7a2daed587 (diff) |
MongoDB Authentication and SSL
Issue-ID: DCAEGEN2-1411
Change-Id: I881cc0e1dd3d7c012a08307bb289f7f97f9c96d5
Signed-off-by: Guobiao Mo <guobiaomo@chinamobile.com>
4 files changed, 39 insertions, 6 deletions
diff --git a/components/datalake-handler/feeder/src/assembly/scripts/init_db.sql b/components/datalake-handler/feeder/src/assembly/scripts/init_db.sql index 83db9f1f..48cb5f9b 100644 --- a/components/datalake-handler/feeder/src/assembly/scripts/init_db.sql +++ b/components/datalake-handler/feeder/src/assembly/scripts/init_db.sql @@ -22,6 +22,8 @@ CREATE TABLE `db` ( `name` varchar(255) NOT NULL,
`host` varchar(255) DEFAULT NULL,
`port` int(11) DEFAULT NULL,
+ `database` varchar(255) DEFAULT NULL,
+ `encrypt` bit(1) DEFAULT NULL,
`login` varchar(255) DEFAULT NULL,
`pass` varchar(255) DEFAULT NULL,
`property1` varchar(255) DEFAULT NULL,
@@ -41,9 +43,9 @@ CREATE TABLE `map_db_topic` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-insert into db (name,host,login,pass,property1) values ('Couchbase','dl_couchbase','dl','dl1234','dl');
+insert into db (name,host,login,pass,database) values ('Couchbase','dl_couchbase','dl','dl1234','datalake');
insert into db (name,host) values ('Elasticsearch','dl_es');
-insert into db (name,host,port,property1) values ('MongoDB','dl_mongodb',27017,'datalake');
+insert into db (name,host,port,database) values ('MongoDB','dl_mongodb',27017,'datalake');
insert into db (name,host) values ('Druid','dl_druid');
diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/domain/Db.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/domain/Db.java index 306af490..3fe14e33 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/domain/Db.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/domain/Db.java @@ -51,6 +51,9 @@ public class Db { private String login; private String pass; + private String database; + private Boolean encrypt; + private String property1; private String property2; private String property3; diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/CouchbaseService.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/CouchbaseService.java index f74829e1..a63a927c 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/CouchbaseService.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/CouchbaseService.java @@ -68,7 +68,7 @@ public class CouchbaseService { Db couchbase = dbService.getCouchbase(); Cluster cluster = CouchbaseCluster.create(couchbase.getHost()); cluster.authenticate(couchbase.getLogin(), couchbase.getPass()); - bucket = cluster.openBucket(couchbase.getProperty1()); + bucket = cluster.openBucket(couchbase.getDatabase()); log.info("Connect to Couchbase " + couchbase.getHost()); diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/MongodbService.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/MongodbService.java index 2b889215..9008fc9a 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/MongodbService.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/MongodbService.java @@ -28,6 +28,7 @@ import java.util.Map; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; +import org.apache.commons.lang3.StringUtils; import org.bson.Document; import org.json.JSONObject; @@ -41,6 +42,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.mongodb.MongoClient; +import com.mongodb.MongoClientOptions; +import com.mongodb.MongoClientOptions.Builder; +import com.mongodb.MongoCredential; +import com.mongodb.ServerAddress; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; @@ -66,8 +71,31 @@ public class MongodbService { private void init() { Db mongodb = dbService.getMongoDB(); - mongoClient = new MongoClient(mongodb.getHost(), mongodb.getPort()); - database = mongoClient.getDatabase(mongodb.getProperty1()); + String host = mongodb.getHost(); + + Integer port = mongodb.getPort(); + if (port == null || port == 0) { + port = 27017; //MongoDB default + } + + String databaseName = mongodb.getDatabase(); + String userName = mongodb.getLogin(); + String password = mongodb.getPass(); + + MongoCredential credential = null; + if (StringUtils.isNoneBlank(userName) && StringUtils.isNoneBlank(password)) { + credential = MongoCredential.createCredential(userName, databaseName, password.toCharArray()); + } + + Builder builder = MongoClientOptions.builder(); + builder.serverSelectionTimeout(30000);//server selection timeout, in milliseconds + + //http://mongodb.github.io/mongo-java-driver/3.0/driver/reference/connecting/ssl/ + builder.sslEnabled(Boolean.TRUE.equals(mongodb.getEncrypt()));// getEncrypt() can be null + MongoClientOptions options = builder.build(); + + mongoClient = new MongoClient(new ServerAddress(host, port), credential, options); + database = mongoClient.getDatabase(mongodb.getDatabase()); } @PreDestroy @@ -88,7 +116,7 @@ public class MongodbService { documents.add(doc); } - String collectionName = topic.getName().replaceAll("[^a-zA-Z0-9]","");//remove - _ . + String collectionName = topic.getName().replaceAll("[^a-zA-Z0-9]", "");//remove - _ . MongoCollection<Document> collection = mongoCollectionMap.computeIfAbsent(collectionName, k -> database.getCollection(k)); collection.insertMany(documents); |