Files
floci/src/main/java
Sam Fakhreddine 02f9754ac7 fix(ui): reconnect the UI sidecar without restarting Floci (#2718)
* fix(ui): reconnect the UI sidecar without restarting Floci

The floci-ui sidecar's FLOCI_ENDPOINT is baked in at container-create
time, but Floci's container IP changes on every restart. adoptExisting()
took over a surviving sidecar verbatim and never compared its endpoint
against the current one, so the sidecar kept polling the previous
instance's address indefinitely. The dashboard reported "Not connected"
with ECONNREFUSED and the only way out was to recreate the sidecar by
hand -- in practice, by restarting the emulator alongside it.

Detect that drift on adoption and recreate the sidecar instead, so
recovery stays entirely within the sidecar's own lifecycle and never
requires disturbing Floci.

Two supporting changes:

- status() re-arms ensureStarted() when the readiness probe fails.
  started was a one-way latch, so a sidecar that was removed, crashed,
  or turned out to be stale left Floci with no path back. The UI polls
  status continuously, which supplies the retry cadence.

- UiServiceConfig gains endpoint() and insecureSkipTlsVerify(). With TLS
  on, the derived endpoint is https://<floci-container-ip>:<port>, which
  the sidecar's Node/Bun proxy rejects: the self-signed certificate has
  no IP SAN for Floci's own container address, so verification fails with
  ERR_TLS_CERT_ALTNAME_INVALID even once the CA is trusted. Trusting the
  CA alone is therefore not enough. endpoint() lets an operator point the
  sidecar at http://<ip>:<port> -- the same server, via the port's
  HTTP/HTTPS protocol detection, with TLS left enabled -- and
  insecureSkipTlsVerify() covers the case where https must be kept. A
  blank or malformed override is a hard error, not a silent fall back to
  the derived endpoint.

* fix(ui): derive http for the sidecar when the host is a bare IP

With TLS enabled the endpoint handed to the UI sidecar was derived as
https://<floci's own container IP>:4566. That address can never appear in
the self-signed certificate: it is assigned at container-create time and
changes on every recreate, and the certificate is generated by a
ConfigSource that runs before CDI, with no Docker client to ask what the
address will be. Every connection therefore failed altname verification —
not chain trust, so mounting the CA would not have helped either — and the
dashboard sat at "Not connected" with no path back. Nothing in the manager
notices: the sidecar is running, so the re-arm added in 8f4fd900 correctly
declines to replace it, and the readiness probe only checks the sidecar's
own port, never the hop that is broken.

Floci's listener does HTTP/HTTPS protocol detection on the same port, so
http:// reaches it with TLS left enabled. That is one hop on an internal
Docker network, not the emulator, and it is the only scheme that works
unattended on the default configuration.

A named host such as host.docker.internal is a DNS SAN on the certificate
and keeps https. So does an IP literal when insecure-skip-tls-verify is
set, since the operator has said explicitly that they want TLS on this hop
and verification is no longer the obstacle.

isIpLiteral mirrors the iPAddress/dNSName distinction CertificateGenerator
already draws, kept local so this package does not depend on the ACM
service for one predicate.

* fix(ui): bracket an IPv6 host so the sidecar endpoint parses

The endpoint was built by concatenating host and port, which produces
http://fd00::2:4566 when the resolver hands back an IPv6 literal — the
port separator is indistinguishable from the address's own colons and no
client can parse it. DockerHostResolver reaches that case through
InetAddress.getLocalHost().getHostAddress() on an IPv6-only host.

Bracket the authority host. Names and IPv4 literals are unchanged.

* fix(ui): reset the async re-arm latch and stop swallowing pre-start failures

Local code review (review-local.ts) against FlociUiManager.java found two
HIGH-severity bugs before this branch reached a PR:

- ensureStartedAsync()'s kicked latch was only reset after a FAILED start,
  never after a successful one. status()'s self-recovery path sets
  started=false and calls ensureStartedAsync() when the sidecar is gone, but
  after any prior successful async start that CAS always failed silently --
  the documented "recovers without Floci being restarted" behavior could
  never actually fire.

- findByName() and replaceIfEndpointDrifted() ran before ensureStarted()'s
  try/catch. A container-runtime failure there (e.g. an unreachable Docker
  socket) escaped uncaught to a synchronous caller, or vanished into a
  discarded Future on the async path, leaving status() reporting
  {started:false, ready:false, error:null} with no diagnostic.

Reset kicked whenever status() re-arms a lost sidecar, and move the whole
body of ensureStarted() into the try/catch so every failure is routed
through lastError. A misconfigured endpoint override's own validation
message (set by replaceIfEndpointDrifted before it rethrows) is preserved
rather than overwritten by the generic describeStartFailure() message.

* fix(ui): validate endpoint override authority and capture fresh-start errors

- validateEndpointOverride() now rejects http:// URLs with no authority
  (e.g. "http://", "http:///path"), which previously passed the bare
  prefix check and were injected as an unreachable FLOCI_ENDPOINT.
- ensureStarted()'s IllegalStateException handler now fills lastError
  when nothing more specific was already recorded, so a fresh start
  (no existing sidecar to adopt) with a blank/malformed endpoint
  override surfaces the validation message instead of error=null.
- Document the new endpoint and insecure-skip-tls-verify settings in
  application.yml's floci.services.ui block.
2026-08-30 15:05:47 -05:00
..