Skip to content

feat(opcua): x-plc-status says why the link to the server is down - #779

Closed
mfaferek93 wants to merge 4 commits into
mainfrom
fix/opcua-status-says-why-it-is-down
Closed

mfaferek93 wants to merge 4 commits into
mainfrom
fix/opcua-status-says-why-it-is-down

Conversation

@mfaferek93

@mfaferek93 mfaferek93 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

What

GET /components/{id}/x-plc-status of the OPC UA plugin gains last_error: the OPC UA status name of the last failure of the link.

  • a connect the server or the client refused: BadCertificateUntrusted (the server's certificate is not in the trust list), the status of the server's ERR message such as BadSecurityChecksFailed (the server does not trust the client's certificate), BadUserAccessDenied (a refused user), BadIdentityTokenInvalid (no token policy of the server takes the configured user, e.g. anonymous on a server that wants a user), BadTimeout / BadConnectionClosed (nothing answers)
  • BadConfigurationError when the client's own certificate, key or security settings could not be applied
  • a session that dropped, or a value read the server refused

It is cleared by a connect that succeeds. It has its own lock, so the route never waits behind a connect in progress.

Two fixes on the way:

Why

A client of the gateway saw connected: false and nothing else. A missing trust entry on either side, a wrong user and a server that is off all looked the same.

open62541 1.3 ends a refused channel with BadConnectionClosed (ua_client.c receiveResponse) and a missing token policy with BadInternalError, and only logs why. connectStatus holds the same codes. So the client puts two taps on its own config: one in front of the certificate check of the server's certificate, and one in front of the logger that matches the exact format strings of processERRResponse and of the token policy selection. Both forward to the original plugin.

Tests

  • test_opcua_client: a refused port names a Bad... status that is not a certificate one, a contradictory security config gives BadConfigurationError, a new client has none.
  • test_opcua_secured (real open62541 server, Basic256Sha256 SignAndEncrypt):
    • wrong password: connected == false, last_error == BadUserAccessDenied
    • server certificate not in the gateway's trust list: connected == false, last_error == BadCertificateUntrusted, no value served. Without the taps this reads BadConnectionClosed.
    • a second server that does not trust the gateway's certificate: last_error == BadSecurityChecksFailed
    • anonymous gateway, server without an anonymous policy: connected == false, last_error == BadIdentityTokenInvalid. Without the session check this reads connected: true with every read failing.
  • Local run in the jazzy image: ctest 28/28 with ROS2_MEDKIT_OPCUA_SECURE_REQUIRE=1, clang-format clean. Debug build of main plus the session check: SIGBUS on the third connect of a refused session; with fix(opcua): keep a secured client's config alive as long as the client #778: none.

open62541 1.3 security policy contexts keep a pointer to the logger of the
config they were built in. That config was a local of the connect call, so
the log line on closing a secured channel read a dead stack frame, and a
dropped link crashed the gateway (SIGSEGV in UA_ChannelModule_Delete_Context).
x-plc-status gains last_error, the OPC UA status name of the last failure:
a refused server certificate, an ERR from a server that refuses the client's
certificate, a refused user, a timeout. open62541 1.3 ends a refused channel
with BadConnectionClosed and only logs why, so the reason is taken from the
client's certificate check and from the server's ERR message.
Copilot AI balanced review requested due to automatic review settings November 9, 2026 02:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved issues leave /x-plc-status blocked during stalled reconnects and allow connection, security-configuration, and single-value read failures to be omitted or misreported.

5 open issues
What changed in this PR

Adds last_error to the OPC UA plugin’s x-plc-status endpoint so gateway clients can distinguish connection, security, and read failures.

Changes:

  • Tracks failure status names and clears the diagnostic after a successful connection.
  • Captures certificate-verification and server ERR reasons that would otherwise appear as generic connection failures.
  • Adds failure-reporting tests and documents the field in the package README.
File Description
src/​ros2_medkit_plugins/​ros2_medkit_opcua/​test/​test_opcua_client.cpp Checks initial, failed-connect, and configuration-error diagnostics.
src/​ros2_medkit_plugins/​ros2_medkit_opcua/​test/​integration/​test_opcua_secured.test.py Adds status assertions for password and certificate refusals.
src/​ros2_medkit_plugins/​ros2_medkit_opcua/​src/​opcua_plugin.cpp Exposes last_error in the status response.
src/​ros2_medkit_plugins/​ros2_medkit_opcua/​src/​opcua_client.cpp Implements failure tracking and certificate/logger interception.
src/​ros2_medkit_plugins/​ros2_medkit_opcua/​README.md Documents the response field and its lifecycle.
src/​ros2_medkit_plugins/​ros2_medkit_opcua/​include/​ros2_medkit_opcua/​opcua_client.hpp Declares and documents the diagnostic accessor.

Review effort: Balanced

Comment on lines +297 to +301
for (UA_StatusCode code : {UA_STATUSCODE_BADSECURITYCHECKSFAILED, UA_STATUSCODE_BADCERTIFICATEUNTRUSTED,
UA_STATUSCODE_BADCERTIFICATEINVALID, UA_STATUSCODE_BADCERTIFICATETIMEINVALID,
UA_STATUSCODE_BADCERTIFICATEURIINVALID, UA_STATUSCODE_BADCERTIFICATEREVOKED,
UA_STATUSCODE_BADCERTIFICATEUSENOTALLOWED, UA_STATUSCODE_BADSECURITYPOLICYREJECTED,
UA_STATUSCODE_BADTCPENDPOINTURLINVALID, UA_STATUSCODE_BADTCPMESSAGETOOLARGE}) {
Comment on lines +672 to +674
if (!apply_security_config(impl_->client, config)) {
impl_->connected = false;
impl_->set_last_error(UA_StatusCode_name(UA_STATUSCODE_BADCONFIGURATIONERROR));
r.good = true;
} catch (const opcua::BadStatus & e) {
r.good = false;
impl_->set_last_error(UA_StatusCode_name(e.code()));
Comment on lines +1880 to +1882
if (connect_status != UA_STATUSCODE_GOOD) {
impl_->set_last_error(UA_StatusCode_name(connect_status));
}
j["write_capable"] = MEDKIT_OPCUA_READ_ONLY == 0;
j["poll_count"] = snap.poll_count;
j["error_count"] = snap.error_count;
j["last_error"] = client_->last_error();
…s-client' into fix/opcua-status-says-why-it-is-down
…n policy says so

A channel left open by a refused ActivateSession read as connected through
open62541pp's isConnected(), so x-plc-status said connected and every read
failed. Only an activated session counts now. A server that offers no token
policy for the configured user is reported as BadIdentityTokenInvalid instead
of open62541's BadInternalError. Built on the secured config lifetime fix.
@mfaferek93

Copy link
Copy Markdown
Collaborator Author

Superseded by #783, which carries this change as its own commit.

@mfaferek93 mfaferek93 closed this Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants