Skip to content

Commit

Permalink
Added db authentication and minor modifications to getAllProperties q…
Browse files Browse the repository at this point in the history
…uery
  • Loading branch information
rraks committed May 5, 2020
1 parent f4a9c73 commit ddc7f4b
Show file tree
Hide file tree
Showing 9 changed files with 1,220 additions and 397 deletions.
2 changes: 2 additions & 0 deletions config/example_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"vocserver.http.instances": 2,
"vocserver.database.queue": "vocserver.database.queue",
"vocserver.database.url": "mongodb://localhost:27017",
"vocserver.database.username": "abc",
"vocserver.database.password": "123",
"vocserver.database.name": "voc",
"vocserver.database.poolname": "mongo_pool",
"vocserver.auth.instances": 2,
Expand Down
10 changes: 8 additions & 2 deletions config/mongo_setup.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#!/bin/sh

set -e;
uname=`grep -oP '(?<="vocserver.database.username": ")[^"]*' /docker-entrypoint-initdb.d/vocserver.json`
passwd=`grep -oP '(?<="vocserver.database.password": ")[^"]*' /docker-entrypoint-initdb.d/vocserver.json`

mongo <<EOF
use voc
db.createUser({user: "$uname", pwd: "$passwd", roles: [ { role: "readWrite", db: "voc" }]})
db.createCollection("master")
db.createCollection("properties")
db.originTable.createIndex("@id")
db.properties.createIndex({"@id": 1})
db.createCollection("classes")
db.originTable.createIndex("@id")
db.classes.createIndex({"@id": 1})
quit()
EOF
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ services:
volumes:
- mongo-data:/var/lib/mongodb/
- ./config/mongo_setup.sh:/docker-entrypoint-initdb.d/init.sh
- ./config/vocserver.json:/docker-entrypoint-initdb.d/vocserver.json
networks:
- voc-net
expose:
Expand All @@ -47,10 +48,12 @@ services:
volumes:
- mongo-data:/var/lib/mongodb/
- ./config/mongo_setup.sh:/docker-entrypoint-initdb.d/init.sh
- ./config/vocserver_local.json:/docker-entrypoint-initdb.d/vocserver.json
networks:
- voc-net
ports:
- "27017:27017"
command: mongod --auth
logging:
driver: "json-file"
options:
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/iudx/vocserver/database/DBService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ public interface DBService {

/**
* getMasterContext - Gets json-ld iudx master context
* @param name Property/Class Name
* @return {@link DBServiceImpl}
*/
@Fluent
DBService getMasterContext(Handler<AsyncResult<JsonArray>> resultHandler);

/**
* insertMasterContext - insert json-ld iudx master context
* @param contex JsonObject Master Context
* @return {@link DBServiceImpl}
*/
@Fluent
DBService insertMasterContext(JsonObject context, Handler<AsyncResult<Boolean>> resultHandler);

/**
* getAllProperties - Gets all vocabulary properties
* @param name Property Name
Expand Down Expand Up @@ -72,7 +79,7 @@ public interface DBService {
* @return {@link DBServiceImpl}
*/
@Fluent
DBService insertProperty(String name, JsonObject prop, Handler<AsyncResult<JsonObject>> resultHandler);
DBService insertProperty(String name, JsonObject prop, Handler<AsyncResult<Boolean>> resultHandler);

/**
* insertClass - Insert a class
Expand All @@ -81,23 +88,23 @@ public interface DBService {
* @return {@link DBServiceImpl}
*/
@Fluent
DBService insertClass(String name, JsonObject cls, Handler<AsyncResult<JsonObject>> resultHandler);
DBService insertClass(String name, JsonObject cls, Handler<AsyncResult<Boolean>> resultHandler);

/**
* deleteProperty - Delete a property
* @param name name of the property
* @return {@link DBServiceImpl}
*/
@Fluent
DBService deleteProperty(String name, Handler<AsyncResult<JsonObject>> resultHandler);
DBService deleteProperty(String name, Handler<AsyncResult<Boolean>> resultHandler);

/**
* deleteClass - Delete a class
* @param name name of the class
* @return {@link DBServiceImpl}
*/
@Fluent
DBService deleteClass(String name, Handler<AsyncResult<JsonObject>> resultHandler);
DBService deleteClass(String name, Handler<AsyncResult<Boolean>> resultHandler);

@GenIgnore
static DBService create(MongoClient dbClient, Handler<AsyncResult<DBService>> readyHandler) {
Expand Down
35 changes: 30 additions & 5 deletions src/main/java/iudx/vocserver/database/DBServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class DBServiceImpl implements DBService {
+ " { \"$project\": {\"_id\": 0, \"rdfs:label\": \"$@graph.rdfs:label\", \"rdfs:comment\": \"$@graph.rdfs:comment\" } } ])";

// Find all properties
// TODO: Temporary fix for searching all kinds of properties
private static final String QUERY_FIND_ALL_PROPERTIES =
"[ {\"$unwind\": \"$@graph\"},"
+ " { \"$match\": {\"@graph.@type\": { \"$in\": [\"rdf:Property\"] }}},"
+ " { \"$project\": {\"_id\": 0, \"rdfs:label\": \"$@graph.rdfs:label\", \"rdfs:comment\": \"$@graph.rdfs:comment\" } } ])";

// Find a class
Expand Down Expand Up @@ -169,12 +169,37 @@ public DBService getClass(String name, Handler<AsyncResult<JsonObject>> resultHa
return this;
}

/**
* @{@inheritDoc}
*/
@Override
public DBService insertMasterContext(JsonObject context,
Handler<AsyncResult<Boolean>> resultHandler) {
dbClient.dropCollection("master",
res -> {
if (!res.succeeded()) {
LOGGER.error("Failed inserting master schema");
resultHandler.handle(Future.failedFuture(res.cause()));
}
});
dbClient.insert("master", context,
res -> {
if (res.succeeded()) {
resultHandler.handle(Future.succeededFuture());
} else {
LOGGER.error("Failed inserting master schema");
resultHandler.handle(Future.failedFuture(res.cause()));
}
});
return this;
}

/**
* @{@inheritDoc}
*/
@Override
public DBService insertProperty(String name, JsonObject prop,
Handler<AsyncResult<JsonObject>> resultHandler) {
Handler<AsyncResult<Boolean>> resultHandler) {
dbClient.updateCollectionWithOptions("properties",
new JsonObject(QUERY_MATCH_ID.replace("$1", "iudx:"+name)),
new JsonObject().put("$set", prop),
Expand All @@ -196,7 +221,7 @@ public DBService insertProperty(String name, JsonObject prop,
*/
@Override
public DBService insertClass(String name, JsonObject cls,
Handler<AsyncResult<JsonObject>> resultHandler) {
Handler<AsyncResult<Boolean>> resultHandler) {
dbClient.updateCollectionWithOptions("classes",
new JsonObject(QUERY_MATCH_ID.replace("$1", "iudx:"+name)),
new JsonObject().put("$set", cls),
Expand All @@ -219,7 +244,7 @@ public DBService insertClass(String name, JsonObject cls,
*/
@Override
public DBService deleteClass(String name,
Handler<AsyncResult<JsonObject>> resultHandler) {
Handler<AsyncResult<Boolean>> resultHandler) {
dbClient.findOneAndDelete("classes",
new JsonObject(QUERY_MATCH_ID.replace("$1", "iudx:"+name)),
res -> {
Expand All @@ -238,7 +263,7 @@ public DBService deleteClass(String name,
*/
@Override
public DBService deleteProperty(String name,
Handler<AsyncResult<JsonObject>> resultHandler) {
Handler<AsyncResult<Boolean>> resultHandler) {
dbClient.findOneAndDelete("property",
new JsonObject(QUERY_MATCH_ID.replace("$1", "iudx:"+name)),
res -> {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/iudx/vocserver/database/DBVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class DBVerticle extends AbstractVerticle {
public static final String CONFIG_DB_NAME = "vocserver.database.name";
public static final String CONFIG_DB_QUEUE = "vocserver.database.queue";
public static final String CONFIG_DB_POOLNAME = "vocserver.database.poolname";
public static final String CONFIG_DB_UNAME = "vocserver.database.username";
public static final String CONFIG_DB_PASSWORD = "vocserver.database.password";
private static final Logger LOGGER = LoggerFactory.getLogger(DBVerticle.class);

@Override
Expand All @@ -24,6 +26,8 @@ public void start(Promise<Void> promise) throws Exception {
/* Load default mongo client config if none specified*/
JsonObject mongoconfig = new JsonObject()
.put("connection_string", config().getString(CONFIG_DB_URL))
.put("username", config().getString(CONFIG_DB_UNAME))
.put("password", config().getString(CONFIG_DB_PASSWORD))
.put("db_name", config().getString(CONFIG_DB_NAME));
MongoClient dbClient = MongoClient.createShared(vertx,
mongoconfig,
Expand Down
Loading

0 comments on commit ddc7f4b

Please sign in to comment.