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

[FIXED] Reconnecting to implicit URL would not use known user info #603

Merged
merged 1 commit into from
Oct 10, 2022
Merged
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
17 changes: 17 additions & 0 deletions src/conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,23 @@ _connectProto(natsConnection *nc, char **proto)
pwd = opts->password;
token = opts->token;
nkey = opts->nkey;

// Options take precedence for an implicit URL. If above is still
// empty, we will check if we have saved a user from an explicit
// URL in the server pool.
if (nats_IsStringEmpty(user)
&& nats_IsStringEmpty(token)
&& (nc->srvPool->user != NULL))
{
user = nc->srvPool->user;
pwd = nc->srvPool->pwd;
// Again, if there is no password, assume username is token.
if (pwd == NULL)
{
token = user;
user = NULL;
}
}
}

if (opts->userJWTHandler != NULL)
Expand Down
15 changes: 14 additions & 1 deletion src/srvpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ natsSrvPool_Destroy(natsSrvPool *pool)
NATS_FREE(pool->srvrs);
pool->srvrs = NULL;
pool->size = 0;
NATS_FREE(pool->user);
NATS_FREE(pool->pwd);
NATS_FREE(pool);
}

Expand All @@ -145,7 +147,18 @@ _addURLToPool(natsSrvPool *pool, char *sURL, bool implicit, const char *tlsName)

s = _createSrv(&srv, sURL, implicit, tlsName);
if (s != NATS_OK)
return s;
return NATS_UPDATE_ERR_STACK(s);

// For and explicit URL, we will save the user info if one is provided
// and if not already done.
if (!implicit && (pool->user == NULL) && (srv->url->username != NULL))
{
DUP_STRING(s, pool->user, srv->url->username);
if ((s == NATS_OK) && !nats_IsStringEmpty(srv->url->password))
DUP_STRING(s, pool->pwd, srv->url->password);
if (s != NATS_OK)
return NATS_UPDATE_ERR_STACK(s);
}

// In the map, we need to add an URL that is just host:port
snprintf(bareURL, sizeof(bareURL), "%s:%d", srv->url->host, srv->url->port);
Expand Down
2 changes: 2 additions & 0 deletions src/srvpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ typedef struct __natsSrvPool
int size;
int cap;
bool randomize;
char *user;
char *pwd;

} natsSrvPool;

Expand Down
1 change: 1 addition & 0 deletions test/list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ ServerPoolUpdatedOnClusterUpdate
ReconnectJitter
CustomReconnectDelay
LameDuckMode
ReconnectImplicitUserInfo
JetStreamUnmarshalAccInfo
JetStreamUnmarshalStreamState
JetStreamUnmarshalStreamCfg
Expand Down
100 changes: 98 additions & 2 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -14681,7 +14681,10 @@ test_AuthServers(void)
testCond((s == NATS_OK)
&& (nc != NULL)
&& (natsConnection_GetConnectedUrl(nc, buffer, sizeof(buffer)) == NATS_OK)
&& (strcmp(buffer, authServers[1]) == 0));
// Even though we are using the url nats://127.0.0.1:1222, the library
// will use the user/pwd info found in the second url. So we should have
// connected OK to the first (no randomization option set at beginning of test).
&& (strcmp(buffer, authServers[0]) == 0));

natsOptions_Destroy(opts);
natsConnection_Destroy(nc);
Expand Down Expand Up @@ -21156,7 +21159,7 @@ test_SSLReconnectWithAuthError(void)
IFOK(s, natsOptions_SetMaxReconnect(opts, 1000));
IFOK(s, natsOptions_SetReconnectWait(opts, 100));
IFOK(s, natsOptions_SetClosedCB(opts, _closedCb, (void*) &args));
IFOK(s, natsOptions_SetURL(opts, "tls://user:pwd@127.0.0.1:4443"));
IFOK(s, natsOptions_SetServers(opts, (const char*[2]){"tls://user:pwd@127.0.0.1:4443", "tls://bad:pwd@127.0.0.1:4444"}, 2));
if (opts == NULL)
FAIL("Unable to create reconnect options!");

Expand Down Expand Up @@ -21306,6 +21309,98 @@ _createConfFile(char *buf, int bufLen, const char *content)
fclose(f);
}

static void
test_ReconnectImplicitUserInfo(void)
{
natsStatus s;
natsPid pid1 = NATS_INVALID_PID;
natsPid pid2 = NATS_INVALID_PID;
natsOptions *o1 = NULL;
natsConnection *nc1 = NULL;
natsSubscription *sub = NULL;
natsConnection *nc2 = NULL;
natsMsg *msg = NULL;
char conf[256];
char cmdLine[1024];
struct threadArg args;

_createConfFile(conf, sizeof(conf),
"accounts { "\
" A { "\
" users: [{user: a, password: pwd}] "\
" }\n"\
" B { "\
" users: [{user: b, password: pwd}] "\
" }\n"\
"}\n"\
"no_auth_user: b\n");
test("Start server1: ");
snprintf(cmdLine, sizeof(cmdLine), "-cluster_name \"local\" -cluster nats://127.0.0.1:6222 -c %s", conf);
pid1 = _startServer("nats://127.0.0.1:4222", cmdLine, true);
CHECK_SERVER_STARTED(pid1);
testCond(true);

test("Connect1: ");
s = _createDefaultThreadArgsForCbTests(&args);
IFOK(s, natsOptions_Create(&o1));
IFOK(s, natsOptions_SetDiscoveredServersCB(o1, _discoveredServersCb, (void*) &args));
IFOK(s, natsOptions_SetURL(o1, "nats://a:pwd@127.0.0.1:4222"));
IFOK(s, natsOptions_SetReconnectWait(o1, 100));
IFOK(s, natsOptions_SetReconnectedCB(o1, _reconnectedCb, (void*) &args));
IFOK(s, natsConnection_Connect(&nc1, o1));
testCond(s == NATS_OK);

test("Start server2: ");
snprintf(cmdLine, sizeof(cmdLine), "-p 4223 -cluster_name \"local\" -cluster nats://127.0.0.1:6223 -routes nats://127.0.0.1:6222 -c %s", conf);
pid2 = _startServer("nats://127.0.0.1:4223", cmdLine, true);
CHECK_SERVER_STARTED(pid2);
testCond(true);

test("Check s2 discovered: ");
natsMutex_Lock(args.m);
while ((s != NATS_TIMEOUT) && (args.sum == 0))
s = natsCondition_TimedWait(args.c, args.m, 2000);
natsMutex_Unlock(args.m);
testCond(s == NATS_OK);

test("Stop srv1: ");
_stopServer(pid1);
natsMutex_Lock(args.m);
while ((s != NATS_TIMEOUT) && !args.reconnected)
s = natsCondition_TimedWait(args.c, args.m, 2000);
natsMutex_Unlock(args.m);
testCond(s == NATS_OK);

test("Create sub: ");
s = natsConnection_SubscribeSync(&sub, nc1, "foo");
testCond(s == NATS_OK);

test("Flush: ");
s = natsConnection_Flush(nc1);
testCond(s == NATS_OK);

test("Connect 2: ");
s = natsConnection_ConnectTo(&nc2, "nats://a:pwd@127.0.0.1:4223");
testCond(s == NATS_OK);

test("Publish: ");
s = natsConnection_PublishString(nc2, "foo", "msg");
testCond(s == NATS_OK);

test("Check received: ");
s = natsSubscription_NextMsg(&msg, sub, 1000);
testCond(s == NATS_OK);
natsMsg_Destroy(msg);

natsSubscription_Destroy(sub);
natsConnection_Destroy(nc1);
natsOptions_Destroy(o1);
natsConnection_Destroy(nc2);
_stopServer(pid2);
_destroyDefaultThreadArgs(&args);
remove(conf);
}

static void
test_JetStreamUnmarshalAccountInfo(void)
{
Expand Down Expand Up @@ -33528,6 +33623,7 @@ static testInfo allTests[] =
{"ReconnectJitter", test_ReconnectJitter},
{"CustomReconnectDelay", test_CustomReconnectDelay},
{"LameDuckMode", test_LameDuckMode},
{"ReconnectImplicitUserInfo", test_ReconnectImplicitUserInfo},

{"JetStreamUnmarshalAccInfo", test_JetStreamUnmarshalAccountInfo},
{"JetStreamUnmarshalStreamState", test_JetStreamUnmarshalStreamState},
Expand Down