Skip to content

Commit e827e77

Browse files
committed
Proposed changes to the PR
Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
1 parent 0e601b7 commit e827e77

File tree

3 files changed

+38
-81
lines changed

3 files changed

+38
-81
lines changed

src/opts.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,7 @@ _sslCertCallback(SSL* ssl, void* arg)
563563
{
564564
natsSSLCtx *ctx = (natsSSLCtx*)arg;
565565
if (ctx == NULL)
566-
{
567566
return 0;
568-
}
569567

570568
// delete any certificates associated with the SSL object
571569
SSL_certs_clear(ssl);
@@ -598,8 +596,7 @@ natsOptions_LoadCertificatesChainDynamic(natsOptions *opts,
598596
{
599597
natsStatus s = NATS_OK;
600598

601-
if ((certFileName == NULL) || (certFileName[0] == '\0')
602-
|| (keyFileName == NULL) || (keyFileName[0] == '\0'))
599+
if (nats_IsStringEmpty(certFileName) || nats_IsStringEmpty(keyFileName))
603600
{
604601
return nats_setError(NATS_INVALID_ARG, "%s",
605602
"certificate and key file names can't be NULL nor empty");
@@ -613,25 +610,18 @@ natsOptions_LoadCertificatesChainDynamic(natsOptions *opts,
613610
NATS_FREE(opts->sslCtx->certFileName);
614611
opts->sslCtx->certFileName = NATS_STRDUP(certFileName);
615612
if (opts->sslCtx->certFileName == NULL)
616-
{
617613
s = nats_setDefaultError(NATS_NO_MEMORY);
618-
}
619614
}
620-
621615
if (s == NATS_OK)
622616
{
623617
NATS_FREE(opts->sslCtx->keyFileName);
624618
opts->sslCtx->keyFileName = NATS_STRDUP(keyFileName);
625619
if (opts->sslCtx->keyFileName == NULL)
626-
{
627620
s = nats_setDefaultError(NATS_NO_MEMORY);
628-
}
629621
}
630-
631622
if (s == NATS_OK)
632623
{
633624
nats_sslRegisterThreadForCleanup();
634-
635625
SSL_CTX_set_cert_cb(opts->sslCtx->ctx, _sslCertCallback, opts->sslCtx);
636626
}
637627

test/test.c

Lines changed: 35 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -21347,69 +21347,16 @@ void test_SSLVerifyHostname(void)
2134721347
#endif
2134821348
}
2134921349

21350-
static char*
21351-
_getAccountName(natsConnection *nc)
21352-
{
21353-
natsStatus s = NATS_OK;
21354-
natsSubscription *inboxSub = NULL;
21355-
natsMsg *userInfoMsg = NULL;
21356-
natsInbox *inbox = NULL;
21357-
char *accountName = NULL;
21358-
21359-
s = natsConn_newInbox(nc, &inbox);
21360-
21361-
// Subscribe to inbox for response
21362-
IFOK(s, natsConnection_SubscribeSync(&inboxSub, nc, inbox));
21363-
IFOK(s, natsConnection_Flush(nc));
21364-
21365-
// Publish account request with inbox as reply subject
21366-
IFOK(s, natsConnection_PublishRequestString(nc, "$SYS.REQ.USER.INFO", inbox, ""));
21367-
IFOK(s, natsConnection_Flush(nc));
21368-
21369-
// Wait for response
21370-
IFOK(s, natsSubscription_NextMsg(&userInfoMsg, inboxSub, 2000));
21371-
21372-
if (s == NATS_OK && userInfoMsg != NULL)
21373-
{
21374-
// Parse JSON response and extract account information
21375-
nats_JSON *json = NULL;
21376-
nats_JSON *data = NULL;
21377-
const char *account = NULL;
21378-
21379-
s = nats_JSONParse(&json, natsMsg_GetData(userInfoMsg), (int)natsMsg_GetDataLength(userInfoMsg));
21380-
if (s == NATS_OK)
21381-
{
21382-
s = nats_JSONGetObject(json, "data", &data);
21383-
if (s == NATS_OK)
21384-
{
21385-
s = nats_JSONGetStr(data, "account", &account);
21386-
if (s == NATS_OK && account != NULL)
21387-
{
21388-
accountName = NATS_STRDUP(account);
21389-
}
21390-
}
21391-
}
21392-
21393-
// Cleanup
21394-
nats_JSONDestroy(json);
21395-
natsMsg_Destroy(userInfoMsg);
21396-
}
21397-
21398-
natsSubscription_Destroy(inboxSub);
21399-
NATS_FREE(inbox);
21400-
21401-
return accountName;
21402-
}
21403-
2140421350
void test_SSLVerifyDynamic(void)
2140521351
{
2140621352
#if defined(NATS_HAS_TLS)
2140721353
natsStatus s;
2140821354
natsConnection *nc = NULL;
2140921355
natsOptions *opts = NULL;
21356+
natsSubscription *sub = NULL;
21357+
natsMsg *msg = NULL;
2141021358
natsPid serverPid = NATS_INVALID_PID;
2141121359
struct threadArg args;
21412-
char *accountName = NULL;
2141321360

2141421361
s = _createDefaultThreadArgsForCbTests(&args);
2141521362
if (s == NATS_OK)
@@ -21428,6 +21375,18 @@ void test_SSLVerifyDynamic(void)
2142821375
IFOK(s, natsOptions_SetReconnectedCB(opts, _reconnectedCb, &args));
2142921376
IFOK(s, natsConnection_Connect(&nc, opts));
2143021377
testCond(s != NATS_OK);
21378+
nats_clearLastError();
21379+
21380+
test("Check load certs (bad args): ");
21381+
s = natsOptions_LoadCertificatesChainDynamic(opts, "certs/client-cert.pem", "");
21382+
if (s == NATS_INVALID_ARG)
21383+
s = natsOptions_LoadCertificatesChainDynamic(opts, "certs/client-cert.pem", NULL);
21384+
if (s == NATS_INVALID_ARG)
21385+
s = natsOptions_LoadCertificatesChainDynamic(opts, "", "certs/client-key.pem");
21386+
if (s == NATS_INVALID_ARG)
21387+
s = natsOptions_LoadCertificatesChainDynamic(opts, NULL, "certs/client-key.pem");
21388+
testCond(s == NATS_INVALID_ARG);
21389+
nats_clearLastError();
2143121390

2143221391
test("Check that connect succeeds with dynamic cert loading: ");
2143321392
// Create temporary certificate files for dynamic loading
@@ -21443,21 +21402,19 @@ void test_SSLVerifyDynamic(void)
2144321402
if ((s == NATS_OK) && (system("copy certs\\client-key.pem certs\\key-dynamic.pem") != 0))
2144421403
s = NATS_ERR;
2144521404
#endif
21446-
2144721405
IFOK(s, natsOptions_LoadCertificatesChainDynamic(opts,
2144821406
"certs/cert-dynamic.pem",
2144921407
"certs/key-dynamic.pem"));
2145021408
s = natsConnection_Connect(&nc, opts);
21409+
IFOK(s, natsConnection_SubscribeSync(&sub, nc, "*"));
2145121410
IFOK(s, natsConnection_PublishString(nc, "foo", "test"));
2145221411
IFOK(s, natsConnection_Flush(nc));
21453-
testCond(s == NATS_OK);
21454-
21455-
test("Check account name equals \"DEREK\": ");
21456-
accountName = _getAccountName(nc);
21457-
testCond(accountName != NULL && !strcmp(accountName, "DEREK"));
21458-
NATS_FREE(accountName);
21412+
IFOK(s, natsSubscription_NextMsg(&msg, sub, 1000));
21413+
testCond((s == NATS_OK) && (msg != NULL) && (strcmp(natsMsg_GetData(msg), "test") == 0));
21414+
natsMsg_Destroy(msg);
21415+
msg = NULL;
2145921416

21460-
test("Check reconnects with different cert is OK: ");
21417+
test("Change certs: ");
2146121418
_stopServer(serverPid);
2146221419

2146321420
nats_Sleep(100);
@@ -21474,24 +21431,34 @@ void test_SSLVerifyDynamic(void)
2147421431
if ((s == NATS_OK) && (system("copy certs\\server-key.pem certs\\key-dynamic.pem") != 0))
2147521432
s = NATS_ERR;
2147621433
#endif
21434+
testCond(s == NATS_OK);
2147721435

2147821436
serverPid = _startServer("nats://127.0.0.1:4443", "-config tlsverify.conf", true);
2147921437
CHECK_SERVER_STARTED(serverPid);
2148021438

21439+
test("Wait for reconnect: ");
2148121440
natsMutex_Lock(args.m);
2148221441
while ((s != NATS_TIMEOUT) && !(args.reconnected))
2148321442
s = natsCondition_TimedWait(args.c, args.m, 2000);
2148421443
natsMutex_Unlock(args.m);
21444+
testCond(s == NATS_OK);
2148521445

21446+
test("Check not able to publish on foo: ");
2148621447
IFOK(s, natsConnection_PublishString(nc, "foo", "test"));
2148721448
IFOK(s, natsConnection_Flush(nc));
21488-
testCond(s == NATS_OK);
21449+
IFOK(s, natsSubscription_NextMsg(&msg, sub, 250));
21450+
testCond((s == NATS_TIMEOUT) && (msg == NULL));
21451+
nats_clearLastError();
2148921452

21490-
test("Check account name equals \"JOHN\": ");
21491-
accountName = _getAccountName(nc);
21492-
testCond(accountName != NULL && !strcmp(accountName, "JOHN"));
21493-
NATS_FREE(accountName);
21453+
test("Send to right subject: ");
21454+
s = natsConnection_PublishString(nc, "bar", "test2");
21455+
IFOK(s, natsConnection_Flush(nc));
21456+
IFOK(s, natsSubscription_NextMsg(&msg, sub, 1000));
21457+
testCond((s == NATS_OK) && (msg != NULL) && (strcmp(natsMsg_GetData(msg), "test2") == 0));
21458+
natsMsg_Destroy(msg);
21459+
msg = NULL;
2149421460

21461+
natsSubscription_Destroy(sub);
2149521462
natsConnection_Destroy(nc);
2149621463
natsOptions_Destroy(opts);
2149721464

test/tlsverify.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ tls {
2121
accounts {
2222
DEREK: {
2323
users: [
24-
{ user: "derek@nats.io" }
24+
{ user: "derek@nats.io", permissions: { publish: "foo" } }
2525
]
2626
}
2727
JOHN: {
2828
users: [
29-
{ user: "localhost" }
29+
{ user: "localhost", permissions: { publish: "bar" } }
3030
]
3131
}
3232
}

0 commit comments

Comments
 (0)