Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add authentication support through user mappings. #24

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions mongo_fdw.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "postgres.h"
#include "mongo_fdw.h"

#include "miscadmin.h"
#include "access/reloptions.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
Expand Down Expand Up @@ -391,6 +392,7 @@ MongoBeginForeignScan(ForeignScanState *scanState, int executorFlags)
mongo *mongoConnection = NULL;
mongo_cursor *mongoCursor = NULL;
int32 connectStatus = MONGO_ERROR;
int32 authStatus = MONGO_ERROR;
Oid foreignTableId = InvalidOid;
List *columnList = NIL;
HTAB *columnMappingHash = NULL;
Expand Down Expand Up @@ -433,6 +435,26 @@ MongoBeginForeignScan(ForeignScanState *scanState, int executorFlags)
errhint("Mongo driver connection error: %d", errorCode)));
}

if (mongoFdwOptions->username)
{
authStatus = mongo_cmd_authenticate(mongoConnection, mongoFdwOptions->databaseName, mongoFdwOptions->username, mongoFdwOptions->password);
}
else
{
authStatus = MONGO_OK;
}

if (authStatus != MONGO_OK)
{
errorCode = (int32) mongoConnection->err;

mongo_destroy(mongoConnection);
mongo_dispose(mongoConnection);

ereport(ERROR, (errmsg("could not authenticate mongo to %s:%d", addressName, portNumber),
errhint("Mongo driver connection error: %d", errorCode)));
}

/* deserialize query document; and create column info hash */
foreignScan = (ForeignScan *) scanState->ss.ps.plan;
foreignPrivateList = foreignScan->fdw_private;
Expand Down Expand Up @@ -653,8 +675,20 @@ ForeignTableDocumentCount(Oid foreignTableId)
status = mongo_connect(mongoConnection, options->addressName, options->portNumber);
if (status == MONGO_OK)
{
documentCount = mongo_count(mongoConnection, options->databaseName,
options->collectionName, emptyQuery);
if (options->username)
{
status = mongo_cmd_authenticate(mongoConnection, options->databaseName, options->username, options->password);
}

if (status == MONGO_OK)
{
documentCount = mongo_count(mongoConnection, options->databaseName,
options->collectionName, emptyQuery);
}
else
{
documentCount = -1.0;
}
}
else
{
Expand Down Expand Up @@ -682,6 +716,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)
Expand Down Expand Up @@ -711,11 +747,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;
}
Expand All @@ -731,16 +772,23 @@ MongoGetOptionValue(Oid foreignTableId, const char *optionName)
{
ForeignTable *foreignTable = NULL;
ForeignServer *foreignServer = NULL;
UserMapping *userMapping = NULL;
List *optionList = NIL;
ListCell *optionCell = NULL;
char *optionValue = NULL;

foreignTable = GetForeignTable(foreignTableId);
foreignServer = GetForeignServer(foreignTable->serverid);
userMapping = GetUserMapping(GetUserId(), foreignServer->serverid);

optionList = list_concat(optionList, foreignTable->options);
optionList = list_concat(optionList, foreignServer->options);

if (userMapping)
{
optionList = list_concat(optionList, userMapping->options);
}

foreach(optionCell, optionList)
{
DefElem *optionDef = (DefElem *) lfirst(optionCell);
Expand Down
13 changes: 11 additions & 2 deletions mongo_fdw.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "fmgr.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_foreign_table.h"
#include "catalog/pg_user_mapping.h"
#include "utils/datetime.h"
#include "nodes/pg_list.h"
#include "nodes/relation.h"
Expand All @@ -29,6 +30,8 @@
#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"
Expand Down Expand Up @@ -58,7 +61,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 */
Expand All @@ -67,7 +70,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 }
};


Expand All @@ -82,6 +89,8 @@ typedef struct MongoFdwOptions
int32 portNumber;
char *databaseName;
char *collectionName;
char *username;
char *password;

} MongoFdwOptions;

Expand Down