Skip to content

Commit

Permalink
Add a GET /node/primary (#5793)
Browse files Browse the repository at this point in the history
  • Loading branch information
achamayou authored Oct 27, 2023
1 parent 115d1af commit af21407
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- In governance contexts, JS runtimes now only use runtime limits from the [public:ccf.gov.js_runtime_options map](https://microsoft.github.io/CCF/main/audit/builtin_maps.html#js-runtime-options) if they are strictly higher than the defaults (#5730).
- Fixed an issue where a JS runtime limit could be hit out of user code execution, leading to an incorrectly constructed JS runtime or a crash (#5730).
- Added a GET /node/primary endpoint, returning 200 when primary and 404 when not, for load balancers to use (#5789).

## [5.0.0-dev4]

Expand Down
15 changes: 14 additions & 1 deletion doc/schemas/node_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@
"info": {
"description": "This API provides public, uncredentialed access to service and node state.",
"title": "CCF Public Node API",
"version": "4.6.0"
"version": "4.7.0"
},
"openapi": "3.0.0",
"paths": {
Expand Down Expand Up @@ -1365,6 +1365,19 @@
}
},
"/node/primary": {
"get": {
"responses": {
"200": {
"description": "Default response description"
},
"default": {
"$ref": "#/components/responses/default"
}
},
"x-ccf-forwarding": {
"$ref": "#/components/x-ccf-forwarding/never"
}
},
"head": {
"responses": {
"200": {
Expand Down
26 changes: 23 additions & 3 deletions src/node/rpc/node_frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ namespace ccf
openapi_info.description =
"This API provides public, uncredentialed access to service and node "
"state.";
openapi_info.document_version = "4.6.0";
openapi_info.document_version = "4.7.0";
}

void init_handlers() override
Expand Down Expand Up @@ -1232,7 +1232,7 @@ namespace ccf
.set_auto_schema<void, GetNode::Out>()
.install();

auto is_primary = [this](auto& args) {
auto head_primary = [this](auto& args) {
if (this->node_operation.can_replicate())
{
args.rpc_ctx->set_response_status(HTTP_STATUS_OK);
Expand Down Expand Up @@ -1276,7 +1276,27 @@ namespace ccf
}
};
make_read_only_endpoint(
"/primary", HTTP_HEAD, is_primary, no_auth_required)
"/primary", HTTP_HEAD, head_primary, no_auth_required)
.set_forwarding_required(endpoints::ForwardingRequired::Never)
.install();

auto get_primary = [this](auto& args) {
if (this->node_operation.can_replicate())
{
args.rpc_ctx->set_response_status(HTTP_STATUS_OK);
return;
}
else
{
args.rpc_ctx->set_error(
HTTP_STATUS_NOT_FOUND,
ccf::errors::ResourceNotFound,
"Node is not primary");
return;
}
};
make_read_only_endpoint(
"/primary", HTTP_GET, get_primary, no_auth_required)
.set_forwarding_required(endpoints::ForwardingRequired::Never)
.install();

Expand Down
6 changes: 6 additions & 0 deletions tests/e2e_common_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def test_primary(network, args):
with primary.client() as c:
r = c.head("/node/primary")
assert r.status_code == http.HTTPStatus.OK.value
r = c.get("/node/primary")
assert r.status_code == http.HTTPStatus.OK.value

backup = network.find_any_backup()
for interface_name in backup.host.rpc_interfaces.keys():
Expand All @@ -31,6 +33,10 @@ def test_primary(network, args):
LOG.info(
f'Successfully redirected to {r.headers["location"]} on primary {primary.local_node_id}'
)
r = c.get("/node/primary", allow_redirects=False)
assert r.status_code == http.HTTPStatus.NOT_FOUND.value, r
assert r.body.json()["error"]["code"] == "ResourceNotFound"
assert r.body.json()["error"]["message"] == "Node is not primary"
return network


Expand Down

0 comments on commit af21407

Please sign in to comment.