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

added authentication for mongo #6

Open
wants to merge 1 commit into
base: master
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
38 changes: 37 additions & 1 deletion mongo_fdw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)
{
Expand Down
13 changes: 11 additions & 2 deletions mongo_fdw.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 */
Expand All @@ -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 }
};


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

} MongoFdwOptions;

Expand Down