diff --git a/mongo_fdw.c b/mongo_fdw.c index fdb844b..f4e9b00 100644 --- a/mongo_fdw.c +++ b/mongo_fdw.c @@ -287,6 +287,9 @@ MongoBeginForeignScan(ForeignScanState *scanState, int executorFlags) MongoFdwOptions *mongoFdwOptions = NULL; MongoFdwExecState *executionState = NULL; + char *databaseName= NULL; + char *username= NULL; + char *password= NULL; /* if Explain with no Analyze, do nothing */ if (executorFlags & EXEC_FLAG_EXPLAIN_ONLY) { @@ -313,7 +316,31 @@ MongoBeginForeignScan(ForeignScanState *scanState, int executorFlags) ereport(ERROR, (errmsg("could not connect to %s:%d", addressName, portNumber), errhint("Mongo driver connection error: %d", errorCode))); + return; + } + + /*authentication*/ + databaseName = mongoFdwOptions->databaseName; + username = mongoFdwOptions->username; + password = mongoFdwOptions->password; + if (username && password) + { + connectStatus = mongo_cmd_authenticate(mongoConnection, databaseName, username, password); + if (connectStatus != MONGO_OK) + { + + errorCode = (int32) mongoConnection->err; + + mongo_destroy(mongoConnection); + mongo_dispose(mongoConnection); + + ereport(ERROR, (errmsg("could not authenticate to %s:%s:%s", databaseName, username, password), + errhint("Mongo driver connection error: %d", errorCode))); + return; + + } } + /*end authentication*/ /* deserialize query document; and create column info hash */ foreignScan = (ForeignScan *) scanState->ss.ps.plan; @@ -632,6 +659,8 @@ MongoGetOptions(Oid foreignTableId) int32 portNumber = 0; char *databaseName = NULL; char *collectionName = NULL; + char *username= NULL; + char *password= NULL; addressName = MongoGetOptionValue(foreignTableId, OPTION_NAME_ADDRESS); if (addressName == NULL) @@ -660,12 +689,16 @@ MongoGetOptions(Oid foreignTableId) { collectionName = get_rel_name(foreignTableId); } - + username = MongoGetOptionValue(foreignTableId, OPTION_NAME_USERNAME); + password = MongoGetOptionValue(foreignTableId, OPTION_NAME_PASSWORD); + mongoFdwOptions = (MongoFdwOptions *) palloc0(sizeof(MongoFdwOptions)); mongoFdwOptions->addressName = addressName; mongoFdwOptions->portNumber = portNumber; mongoFdwOptions->databaseName = databaseName; mongoFdwOptions->collectionName = collectionName; + mongoFdwOptions->username = username; + mongoFdwOptions->password = password; return mongoFdwOptions; } @@ -681,15 +714,18 @@ MongoGetOptionValue(Oid foreignTableId, const char *optionName) { ForeignTable *foreignTable = NULL; ForeignServer *foreignServer = NULL; + UserMapping *mapping= NULL; List *optionList = NIL; ListCell *optionCell = NULL; char *optionValue = NULL; foreignTable = GetForeignTable(foreignTableId); foreignServer = GetForeignServer(foreignTable->serverid); + mapping = GetUserMapping(GetUserId(), foreignTable->serverid); optionList = list_concat(optionList, foreignTable->options); optionList = list_concat(optionList, foreignServer->options); + optionList = list_concat(optionList, mapping->options); foreach(optionCell, optionList) { diff --git a/mongo_fdw.h b/mongo_fdw.h index ea0e3ef..de1c006 100644 --- a/mongo_fdw.h +++ b/mongo_fdw.h @@ -25,12 +25,15 @@ #include "nodes/relation.h" #include "utils/timestamp.h" +#include "catalog/pg_user_mapping.h" /* Defines for valid option names */ #define OPTION_NAME_ADDRESS "address" #define OPTION_NAME_PORT "port" #define OPTION_NAME_DATABASE "database" #define OPTION_NAME_COLLECTION "collection" +#define OPTION_NAME_USERNAME "username" +#define OPTION_NAME_PASSWORD "password" /* Default values for option parameters */ #define DEFAULT_IP_ADDRESS "127.0.0.1" @@ -60,7 +63,7 @@ typedef struct MongoValidOption /* Array of options that are valid for mongo_fdw */ -static const uint32 ValidOptionCount = 4; +static const uint32 ValidOptionCount = 6; static const MongoValidOption ValidOptionArray[] = { /* foreign server options */ @@ -69,7 +72,11 @@ static const MongoValidOption ValidOptionArray[] = /* foreign table options */ { OPTION_NAME_DATABASE, ForeignTableRelationId }, - { OPTION_NAME_COLLECTION, ForeignTableRelationId } + { OPTION_NAME_COLLECTION, ForeignTableRelationId }, + + /* User mapping options */ + { OPTION_NAME_USERNAME, UserMappingRelationId }, + { OPTION_NAME_PASSWORD, UserMappingRelationId } }; @@ -84,6 +91,8 @@ typedef struct MongoFdwOptions int32 portNumber; char *databaseName; char *collectionName; + char *username; + char *password; } MongoFdwOptions;