Stand down when the portal revokes this instance
The connector treats close code 4410 from the hub as final: it stops dialling, keeps cloud.json so a new claim code replaces it, and reports revoked in /cloud/status, which Settings → Remote access shows as Removed at the portal. Enrolling again, or a restart, brings the link back. An older hub still answers 403 and the link keeps retrying as before.
This commit is contained in:
@@ -35,6 +35,10 @@ from fluksio.cloud import config as cloud_config
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
PROTOCOL = 1
|
||||
#: The hub closes a credential it will never accept again with this code (see
|
||||
#: the hub's ``tunnel/protocol.py``). The link then stands down until enrolled
|
||||
#: again or restarted, rather than dialling a deleted instance forever.
|
||||
REVOKED_CLOSE = 4410
|
||||
HEARTBEAT_S = 20.0
|
||||
STATUS_S = 60.0
|
||||
BASE_BACKOFF_S = 1.0
|
||||
@@ -89,18 +93,27 @@ async def watch_enrolment(app: FastAPI) -> None:
|
||||
while True:
|
||||
await asyncio.sleep(ENROL_POLL_S)
|
||||
task = getattr(app.state, "cloud_task", None)
|
||||
if task is not None and task.done():
|
||||
# It returns of its own accord when the config goes away, which is
|
||||
# what Disconnect, here or on the portal, does.
|
||||
app.state.cloud_task = None
|
||||
app.state.cloud_connector = None
|
||||
task = None
|
||||
# `load()`, not `exists()`: a file that cannot be read is not an
|
||||
# enrolment, and the connector would give up on it the instant it
|
||||
# started — which, started from here, is a restart every few seconds.
|
||||
# A config that is fine but unreachable keeps its task, and the
|
||||
# retrying belongs to the connector rather than to this.
|
||||
config = cloud_config.load()
|
||||
if task is not None and task.done():
|
||||
connector = getattr(app.state, "cloud_connector", None)
|
||||
if (
|
||||
connector is not None
|
||||
and connector.revoked
|
||||
and _identity(config) == getattr(app.state, "cloud_identity", None)
|
||||
):
|
||||
# Stood down on the portal's word, and kept for its status.
|
||||
# Only a new enrolment brings the link back.
|
||||
continue
|
||||
# It returns of its own accord when the config goes away, which is
|
||||
# what Disconnect, here or on the portal, does.
|
||||
app.state.cloud_task = None
|
||||
app.state.cloud_connector = None
|
||||
task = None
|
||||
if task is not None and _identity(config) != getattr(
|
||||
app.state, "cloud_identity", None
|
||||
):
|
||||
@@ -125,6 +138,11 @@ class CloudConnector:
|
||||
self._connected = False
|
||||
self._connected_since: float | None = None
|
||||
self._last_error: str | None = None
|
||||
self._revoked = False
|
||||
|
||||
@property
|
||||
def revoked(self) -> bool:
|
||||
return self._revoked
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# Status, for the local settings screen
|
||||
@@ -143,6 +161,7 @@ class CloudConnector:
|
||||
"instance_id": config.instance_id if config else None,
|
||||
"last_error": self._last_error,
|
||||
"connected_since": self._connected_since,
|
||||
"revoked": self._revoked,
|
||||
}
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
@@ -174,6 +193,12 @@ class CloudConnector:
|
||||
raise
|
||||
except Exception as exc:
|
||||
self._last_error = str(exc)
|
||||
if _is_revoked(exc):
|
||||
# Dialling again would be refused the same way forever.
|
||||
# The config stays, so enrolling again replaces it.
|
||||
self._revoked = True
|
||||
logger.warning("The portal revoked this instance; standing down")
|
||||
return
|
||||
logger.warning("Portal link down: %s", exc)
|
||||
finally:
|
||||
self._connected = False
|
||||
@@ -606,6 +631,17 @@ class CloudConnector:
|
||||
task.cancel()
|
||||
|
||||
|
||||
def _is_revoked(exc: Exception) -> bool:
|
||||
"""Whether the hub closed the link as revoked, at the handshake or later."""
|
||||
from websockets.exceptions import ConnectionClosed
|
||||
|
||||
return (
|
||||
isinstance(exc, ConnectionClosed)
|
||||
and exc.rcvd is not None
|
||||
and exc.rcvd.code == REVOKED_CLOSE
|
||||
)
|
||||
|
||||
|
||||
def _dump(payload: Any) -> str:
|
||||
import json
|
||||
|
||||
|
||||
Reference in New Issue
Block a user