Skip to content

Commit f614beb

Browse files
authored
Merge pull request #4342 from garlick/cert_name
improve logging of overlay peer authentication
2 parents f1b97f0 + 75cc96b commit f614beb

File tree

6 files changed

+56
-19
lines changed

6 files changed

+56
-19
lines changed

doc/man1/flux-keygen.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ flux-keygen(1)
66
SYNOPSIS
77
========
88

9-
**flux** **keygen** *PATH*
9+
**flux** **keygen** [*--name=NAME*] *PATH*
1010

1111

1212
DESCRIPTION
@@ -28,6 +28,17 @@ In that case, each broker self-generates a unique certificate and the
2828
public keys are exchanged with PMI.
2929

3030

31+
OPTIONS
32+
=======
33+
34+
``flux-keygen`` accepts the following options:
35+
36+
**-n, --name=NAME**
37+
Set the certificate metadata ``name`` field. The value is logged when
38+
:man1:`flux-broker` authenticates a peer that presents this certificate.
39+
A cluster name might be appropriate here. Default: the local hostname.
40+
41+
3142
RESOURCES
3243
=========
3344

src/cmd/flux-keygen.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,16 @@
1313
#endif
1414
#include <unistd.h>
1515
#include <flux/core.h>
16+
#include <flux/optparse.h>
1617
#include <czmq.h>
1718

1819
#include "src/common/libutil/log.h"
1920

20-
21-
void usage (void)
22-
{
23-
fprintf (stderr,
24-
"Usage: flux-keygen PATH\n"
25-
);
26-
exit (1);
27-
}
21+
static struct optparse_option opts[] = {
22+
{ .name = "name", .key = 'n', .has_arg = 1, .arginfo = "NAME",
23+
.usage = "Set certificate name (default: hostname)", },
24+
OPTPARSE_TABLE_END,
25+
};
2826

2927
static char * ctime_iso8601_now (char *buf, size_t sz)
3028
{
@@ -42,26 +40,37 @@ static char * ctime_iso8601_now (char *buf, size_t sz)
4240

4341
int main (int argc, char *argv[])
4442
{
43+
const char *usage_msg = "[OPTIONS] [PATH]";
44+
optparse_t *p;
45+
int optindex;
4546
zcert_t *cert;
4647
char buf[64];
4748
char *path = NULL;
4849

4950
log_init ("flux-keygen");
50-
51-
if (argc == 1)
51+
if (!(p = optparse_create ("flux-keygen"))
52+
|| optparse_add_option_table (p, opts) != OPTPARSE_SUCCESS
53+
|| optparse_set (p, OPTPARSE_USAGE, usage_msg) != OPTPARSE_SUCCESS)
54+
log_err_exit ("error setting up otpion parsing");
55+
if ((optindex = optparse_parse_args (p, argc, argv)) < 0)
56+
exit (1);
57+
if (optindex < argc)
58+
path = argv[optindex++];
59+
if (optindex < argc) {
60+
optparse_print_usage (p);
61+
exit (1);
62+
}
63+
if (!path)
5264
log_msg ("WARNING: add PATH argument to save generated certificate");
53-
else if (argc == 2 && *argv[1] != '-')
54-
path = argv[1];
55-
else
56-
usage ();
5765

5866
if (!(cert = zcert_new ()))
5967
log_msg_exit ("zcert_new: %s", zmq_strerror (errno));
6068

6169
if (gethostname (buf, sizeof (buf)) < 0)
6270
log_err_exit ("gethostname");
6371
zcert_set_meta (cert, "hostname", "%s", buf);
64-
zcert_set_meta (cert, "name", "%s", buf); // used in overlay logging
72+
// name is used in overlay logging
73+
zcert_set_meta (cert, "name", "%s", optparse_get_str (p, "name", buf));
6574
zcert_set_meta (cert, "time", "%s", ctime_iso8601_now (buf, sizeof (buf)));
6675
zcert_set_meta (cert, "userid", "%d", getuid ());
6776

@@ -70,6 +79,7 @@ int main (int argc, char *argv[])
7079

7180
zcert_destroy (&cert);
7281

82+
optparse_destroy (p);
7383
log_fini ();
7484

7585
return 0;

src/common/libzmqutil/zap.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,15 @@ static void zap_cb (flux_reactor_t *r,
123123
status_text = "OK";
124124
user_id = pubkey;
125125
name = zcert_meta (cert, "name");
126-
log_level = LOG_INFO;
126+
log_level = LOG_DEBUG;
127127
}
128128
if (!name)
129129
name = "unknown";
130-
logger (zap, log_level, "overlay auth %s %s", name, status_text);
130+
logger (zap,
131+
log_level,
132+
"overlay auth cert-name=%s %s",
133+
name,
134+
status_text);
131135

132136
if (!(rep = zmsg_new ()))
133137
goto done;

t/sharness.d/flux-sharness.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ make_bootstrap_config() {
9999

100100
mkdir $workdir/conf.d
101101
mkdir $workdir/state
102-
flux keygen $workdir/cert
102+
flux keygen --name testcert $workdir/cert
103103
cat >$workdir/conf.d/bootstrap.toml <<-EOT
104104
[bootstrap]
105105
curve_cert = "$workdir/cert"

t/t0001-basic.t

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ test_expect_success 'flux-keygen works' '
3737
flux keygen cert &&
3838
test -f cert
3939
'
40+
test_expect_success 'flux-keygen --name=test works' '
41+
flux keygen --name=testcert cert2 &&
42+
test -f cert2 &&
43+
grep testcert cert2
44+
'
45+
test_expect_success 'flux-keygen fails with extra positional argument' '
46+
test_must_fail flux keygen cert xyz
47+
'
4048
test_expect_success 'flux-keygen generated cert with u=rw access' '
4149
echo '-rw-------' >cert-access.exp &&
4250
stat --format=%A cert >cert-access.out &&

t/t3300-system-basic.t

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ test_expect_success 'startctl status works' '
3131
test_expect_success HAVE_JQ 'broker overlay shows 2 connected children' '
3232
test $(overlay_connected_children) -eq 2
3333
'
34+
test_expect_success 'testcert was used to authenticate' '
35+
flux dmesg |grep "overlay auth" >auth.log &&
36+
grep testcert auth.log
37+
'
3438

3539
test_expect_success 'overlay status is full' '
3640
test "$(flux overlay status --timeout=0 --summary)" = "full"

0 commit comments

Comments
 (0)