Skip to content

Commit 87e699d

Browse files
committed
broker: use cert not zert_t in overlay
Problem: the broker overlay class uses the CZMQ zcert class but we are migrating away from CZMQ. Switch to the cert class. Update the overlay unit test too.
1 parent 19d9fb5 commit 87e699d

File tree

2 files changed

+48
-27
lines changed

2 files changed

+48
-27
lines changed

src/broker/overlay.c

+41-22
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
#include "config.h"
1313
#endif
1414
#include <stdarg.h>
15-
#include <czmq.h>
15+
#include <sys/types.h>
16+
#include <sys/stat.h>
17+
#include <fcntl.h>
1618
#include <zmq.h>
19+
#include <unistd.h>
20+
#include <assert.h>
1721
#include <flux/core.h>
1822
#include <inttypes.h>
1923
#include <jansson.h>
@@ -23,6 +27,7 @@
2327
#include "src/common/libzmqutil/sockopt.h"
2428
#include "src/common/libzmqutil/reactor.h"
2529
#include "src/common/libzmqutil/zap.h"
30+
#include "src/common/libzmqutil/cert.h"
2631
#include "src/common/libzmqutil/monitor.h"
2732
#include "src/common/libczmqcontainers/czmq_containers.h"
2833
#include "src/common/libutil/log.h"
@@ -32,7 +37,8 @@
3237
#include "src/common/libutil/monotime.h"
3338
#include "src/common/libutil/errprintf.h"
3439
#include "src/common/librouter/rpc_track.h"
35-
#include "src/common/libccan/ccan/ptrint/ptrint.h"
40+
#include "ccan/ptrint/ptrint.h"
41+
#include "ccan/str/str.h"
3642

3743
#include "overlay.h"
3844
#include "attr.h"
@@ -140,7 +146,7 @@ struct overlay_monitor {
140146

141147
struct overlay {
142148
void *zctx;
143-
zcert_t *cert;
149+
struct cert *cert;
144150
struct zmqutil_zap *zap;
145151
int enable_ipv6;
146152

@@ -1303,8 +1309,8 @@ int overlay_connect (struct overlay *ov)
13031309
return -1;
13041310
}
13051311
#endif
1306-
zcert_apply (ov->cert, ov->parent.zsock);
1307-
1312+
if (cert_apply (ov->cert, ov->parent.zsock) < 0)
1313+
return -1;
13081314
if (zmq_connect (ov->parent.zsock, ov->parent.uri) < 0)
13091315
return -1;
13101316
if (!(ov->parent.w = zmqutil_watcher_create (ov->reactor,
@@ -1373,8 +1379,10 @@ int overlay_bind (struct overlay *ov, const char *uri)
13731379
}
13741380
}
13751381
#endif
1376-
zcert_apply (ov->cert, ov->bind_zsock);
1377-
1382+
if (cert_apply (ov->cert, ov->bind_zsock) < 0) {
1383+
log_err ("error setting curve socket options");
1384+
return -1;
1385+
}
13781386
if (zmq_bind (ov->bind_zsock, uri) < 0) {
13791387
log_err ("error binding to %s", uri);
13801388
return -1;
@@ -1713,35 +1721,46 @@ static void overlay_disconnect_subtree_cb (flux_t *h,
17131721
int overlay_cert_load (struct overlay *ov, const char *path)
17141722
{
17151723
struct stat sb;
1716-
zcert_t *cert;
1724+
int fd;
1725+
FILE *f = NULL;
1726+
struct cert *cert;
17171727

1718-
if (stat (path, &sb) < 0) {
1719-
log_err ("%s", path);
1720-
return -1;
1728+
if ((fd = open (path, O_RDONLY)) < 0
1729+
|| fstat (fd, &sb) < 0) {
1730+
goto error;
17211731
}
17221732
if ((sb.st_mode & S_IROTH) | (sb.st_mode & S_IRGRP)) {
17231733
log_msg ("%s: readable by group/other", path);
17241734
errno = EPERM;
1725-
return -1;
1735+
goto error_quiet;
17261736
}
1727-
if (!(cert = zcert_load (path))) {
1728-
log_msg ("%s: invalid CURVE certificate", path);
1729-
errno = EINVAL;
1730-
return -1;
1731-
}
1732-
zcert_destroy (&ov->cert);
1737+
if (!(f = fdopen (fd, "r")))
1738+
goto error;
1739+
fd = -1; // now owned by 'f'
1740+
if (!(cert = cert_read (f)))
1741+
goto error;
1742+
cert_destroy (ov->cert); // replace ov->cert (if any) with this
17331743
ov->cert = cert;
1744+
(void)fclose (f);
17341745
return 0;
1746+
error:
1747+
log_err ("%s", path);
1748+
error_quiet:
1749+
if (fd >= 0)
1750+
(void)close (fd);
1751+
if (f)
1752+
(void)fclose (f);
1753+
return -1;
17351754
}
17361755

17371756
const char *overlay_cert_pubkey (struct overlay *ov)
17381757
{
1739-
return zcert_public_txt (ov->cert);
1758+
return cert_public_txt (ov->cert);
17401759
}
17411760

17421761
const char *overlay_cert_name (struct overlay *ov)
17431762
{
1744-
return zcert_meta (ov->cert, "name");
1763+
return cert_meta_get (ov->cert, "name");
17451764
}
17461765

17471766
int overlay_authorize (struct overlay *ov,
@@ -2042,7 +2061,7 @@ void overlay_destroy (struct overlay *ov)
20422061

20432062
flux_msglist_destroy (ov->health_requests);
20442063

2045-
zcert_destroy (&ov->cert);
2064+
cert_destroy (ov->cert);
20462065
zmqutil_zap_destroy (ov->zap);
20472066

20482067
flux_future_destroy (ov->f_sync);
@@ -2164,7 +2183,7 @@ struct overlay *overlay_create (flux_t *h,
21642183
goto error;
21652184
if (flux_msg_handler_addvec (h, htab, ov, &ov->handlers) < 0)
21662185
goto error;
2167-
if (!(ov->cert = zcert_new ()))
2186+
if (!(ov->cert = cert_create ()))
21682187
goto nomem;
21692188
if (!(ov->health_requests = flux_msglist_create ()))
21702189
goto error;

src/broker/test/overlay.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
#include <errno.h>
1616
#include <string.h>
1717
#include <flux/core.h>
18-
#include <czmq.h>
18+
#include <zmq.h>
1919

2020
#include "src/common/libtap/tap.h"
2121
#include "src/common/libzmqutil/msg_zsock.h"
2222
#include "src/common/libzmqutil/sockopt.h"
23+
#include "src/common/libzmqutil/cert.h"
2324
#include "src/common/libczmqcontainers/czmq_containers.h"
2425
#include "src/common/libtestutil/util.h"
2526
#include "src/common/libutil/stdlog.h"
2627
#include "src/common/libutil/unlink_recursive.h"
28+
#include "ccan/str/str.h"
2729

2830
#include "src/broker/overlay.h"
2931
#include "src/broker/attr.h"
@@ -280,7 +282,7 @@ void trio (flux_t *h)
280282
const char *topic;
281283
void *zsock_none;
282284
void *zsock_curve;
283-
zcert_t *cert;
285+
struct cert *cert;
284286
const char *sender;
285287

286288
ctx[0] = ctx_create (h, "trio", size, 0, "kary:2", recv_cb);
@@ -478,10 +480,10 @@ void trio (flux_t *h)
478480
|| zsetsockopt_str (zsock_curve, ZMQ_CURVE_SERVERKEY, server_pubkey) < 0
479481
|| zsetsockopt_str (zsock_curve, ZMQ_IDENTITY, "2") < 0)
480482
BAIL_OUT ("zmq_socket failed");
481-
if (!(cert = zcert_new ()))
483+
if (!(cert = cert_create ()))
482484
BAIL_OUT ("zcert_new failed");
483-
zcert_apply (cert, zsock_curve);
484-
zcert_destroy (&cert);
485+
cert_apply (cert, zsock_curve);
486+
cert_destroy (cert);
485487
ok (zmq_connect (zsock_curve, parent_uri) == 0,
486488
"curve-2: zmq_connect %s works", parent_uri);
487489
ok (zmqutil_msg_send (zsock_curve, msg) == 0,

0 commit comments

Comments
 (0)