Skip to content

Commit

Permalink
bgpd: fix show rpki json void output
Browse files Browse the repository at this point in the history
"show rpki XX json" should not return a void output because json.loads()
considers it to be an incorrect JSON.

> >>> json.loads("")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/usr/lib/python3.9/json/__init__.py", line 346, in loads
>     return _default_decoder.decode(s)
>   File "/usr/lib/python3.9/json/decoder.py", line 337, in decode
>     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
>   File "/usr/lib/python3.9/json/decoder.py", line 355, in raw_decode
>     raise JSONDecodeError("Expecting value", s, err.value) from None
> json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
> >>> json.loads("{}")
> {}

Return "{}" instead in such a case.

Link: FRRouting#15034
Fixes: dff41cc ("bgpd: Add JSON output for `show rpki prefix` and other show commands")
Signed-off-by: Louis Scalbert <[email protected]>
  • Loading branch information
louis-6wind committed Dec 21, 2023
1 parent bbda45a commit f53e5d5
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions bgpd/bgp_rpki.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,9 @@ static void print_prefix_table_by_asn(struct vty *vty, as_t as,
arg.asnotation = bgp_get_asnotation(bgp_lookup_by_vrf_id(VRF_DEFAULT));

if (!group) {
if (!json)
if (json)
vty_json(vty, json);
else
vty_out(vty, "Cannot find a connected group.\n");
return;
}
Expand Down Expand Up @@ -786,7 +788,9 @@ static void print_prefix_table(struct vty *vty, json_object *json)
arg.asnotation = bgp_get_asnotation(bgp_lookup_by_vrf_id(VRF_DEFAULT));

if (!group) {
if (!json)
if (json)
vty_json(vty, json);
else
vty_out(vty, "Cannot find a connected group.\n");
return;
}
Expand Down Expand Up @@ -1326,15 +1330,17 @@ DEFPY (show_rpki_prefix_table,
{
struct json_object *json = NULL;

if (uj)
json = json_object_new_object();

if (!is_synchronized()) {
if (!uj)
if (uj)
vty_json(vty, json);
else
vty_out(vty, "No connection to RPKI cache server.\n");
return CMD_WARNING;
}

if (uj)
json = json_object_new_object();

print_prefix_table(vty, json);
return CMD_SUCCESS;
}
Expand All @@ -1350,15 +1356,17 @@ DEFPY (show_rpki_as_number,
{
struct json_object *json = NULL;

if (uj)
json = json_object_new_object();

if (!is_synchronized()) {
if (!uj)
if (uj)
vty_json(vty, json);
else
vty_out(vty, "No Connection to RPKI cache server.\n");
return CMD_WARNING;
}

if (uj)
json = json_object_new_object();

print_prefix_table_by_asn(vty, by_asn, json);
return CMD_SUCCESS;
}
Expand All @@ -1378,8 +1386,13 @@ DEFPY (show_rpki_prefix,
json_object *json_records = NULL;
enum asnotation_mode asnotation;

if (uj)
json = json_object_new_object();

if (!is_synchronized()) {
if (!uj)
if (uj)
vty_json(vty, json);
else
vty_out(vty, "No Connection to RPKI cache server.\n");
return CMD_WARNING;
}
Expand All @@ -1392,7 +1405,9 @@ DEFPY (show_rpki_prefix,
memcpy(addr_str, prefix_str, addr_len);

if (lrtr_ip_str_to_addr(addr_str, &addr) != 0) {
if (!json)
if (json)
vty_json(vty, json);
else
vty_out(vty, "Invalid IP prefix\n");
return CMD_WARNING;
}
Expand All @@ -1404,13 +1419,13 @@ DEFPY (show_rpki_prefix,
if (pfx_table_validate_r(rtr_config->pfx_table, &matches, &match_count,
asn, &addr, prefix->prefixlen,
&result) != PFX_SUCCESS) {
if (!json)
if (json)
vty_json(vty, json);
else
vty_out(vty, "Prefix lookup failed\n");
return CMD_WARNING;
}

if (uj)
json = json_object_new_object();

if (!json) {
vty_out(vty, "%-40s %s %s\n", "Prefix", "Prefix Length",
Expand Down

0 comments on commit f53e5d5

Please sign in to comment.