Skip to content

Conversation

@jajeffries
Copy link
Contributor

This pull request introduces robust support for automatic reconnection and JWT token refresh for the Fleet MQTT connection. The changes ensure that the agent can recover from authentication or connectivity failures by refreshing its token and re-establishing the connection without manual intervention. The implementation tracks consecutive failures in heartbeat, capabilities, and group membership publishing, triggering a reconnection sequence when thresholds are reached. Additionally, the AuthTokenManager now caches credentials and token state to enable seamless refreshes.

Automatic reconnection and JWT refresh:

  • Added a reconnectChan to fleetConfigManager and MQTTConnection for signaling reconnection requests, and implemented logic to trigger reconnection after repeated failures in heartbeat, capabilities, or group membership publishing. [1] [2] [3] [4] [5]
  • Implemented a goroutine in fleetConfigManager.Start that listens for reconnect requests and calls a new refreshAndReconnect method to refresh the JWT token and re-establish the MQTT connection.

Auth token management improvements:

  • Enhanced AuthTokenManager to cache token credentials and expiration time, enabling token refresh via a new RefreshToken method and expiration checks via IsTokenExpired. [1] [2] [3]

Connection lifecycle enhancements:

  • Added a Reconnect method to MQTTConnection to cleanly disconnect, reset failure counters, and reconnect with new credentials.
  • Updated heartbeat logic to accept a failure callback and reset failure counters on successful publish, improving reliability and observability. [1] [2] [3] [4]

Test updates:

  • Updated unit tests to accommodate the new reconnectChan argument and heartbeat changes, ensuring coverage of the new reconnection logic. [1] [2] [3] [4] [5]

@github-actions
Copy link

Go test coverage

STATUS ELAPSED PACKAGE COVER PASS FAIL SKIP
🟢 PASS 1.02s github.com/netboxlabs/orb-agent/agent 8.2% 1 0 0
🟢 PASS 1.10s github.com/netboxlabs/orb-agent/agent/backend 33.3% 30 0 0
🟢 PASS 4.03s github.com/netboxlabs/orb-agent/agent/backend/devicediscovery 67.1% 2 0 0
🟢 PASS 0.20s github.com/netboxlabs/orb-agent/agent/backend/mocks 0.0% 0 0 0
🟢 PASS 4.02s github.com/netboxlabs/orb-agent/agent/backend/networkdiscovery 59.0% 2 0 0
🟢 PASS 4.03s github.com/netboxlabs/orb-agent/agent/backend/opentelemetryinfinity 46.3% 2 0 0
🟢 PASS 4.03s github.com/netboxlabs/orb-agent/agent/backend/pktvisor 67.0% 2 0 0
🟢 PASS 4.03s github.com/netboxlabs/orb-agent/agent/backend/snmpdiscovery 58.5% 2 0 0
🟢 PASS 5.03s github.com/netboxlabs/orb-agent/agent/backend/worker 68.1% 3 0 0
🟢 PASS 1.02s github.com/netboxlabs/orb-agent/agent/config 100.0% 6 0 0
🟢 PASS 31.20s github.com/netboxlabs/orb-agent/agent/configmgr 47.8% 20 0 0
🟢 PASS 5.54s github.com/netboxlabs/orb-agent/agent/configmgr/fleet 64.3% 129 0 0
🟢 PASS 1.02s github.com/netboxlabs/orb-agent/agent/otlpbridge 30.7% 5 0 0
🟢 PASS 1.01s github.com/netboxlabs/orb-agent/agent/policies 100.0% 15 0 0
🟢 PASS 1.03s github.com/netboxlabs/orb-agent/agent/policymgr 71.6% 11 0 0
🟢 PASS 23.69s github.com/netboxlabs/orb-agent/agent/secretsmgr 48.9% 54 0 0
🟢 PASS 1.02s github.com/netboxlabs/orb-agent/agent/telemetry 81.7% 19 0 0
🟢 PASS 1.01s github.com/netboxlabs/orb-agent/agent/version 100.0% 1 0 0

Total coverage: 55.5%

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