Skip to content

ak-sysd: make managed-domain selection and lifecycle deterministic - #1359

Open
PeshekDotDev wants to merge 2 commits into
mainfrom
fix/sysd-active-domain-token
Open

ak-sysd: make managed-domain selection and lifecycle deterministic#1359
PeshekDotDev wants to merge 2 commits into
mainfrom
fix/sysd-active-domain-token

Conversation

@PeshekDotDev

@PeshekDotDev PeshekDotDev commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Details

What does this PR change?

Three related changes to how sysd handles domains, all in cfg/domain.rs:

  1. active() skips domains with no token. It previously returned the first enabled domain regardless of whether it could authenticate.
  2. active() prefers an MDM-managed domain when one is usable, falling back to the first usable domain otherwise.
  3. load_managed() removes the managed domain when its config is gone, which its doc comment ("Loads (or re-enrolls, or removes)…") already promised but never did.

active() also now logs which domain it selected.

Why is this change needed?

load_managed adds an ak-mdm-managed domain alongside any user-enrolled ones, so a machine routinely has two. active() took the first enabled entry in read_dir order. On a real macOS host ak-mdm-managed.json sorts before mac.json, and its enrollment had produced no token — so every Platform SSO registration authenticated as Bearer+agent and came back:

403 {"detail":"Authentication credentials were not provided."}

while a perfectly good domain sat second in the list. Nothing logged which domain had been chosen, so this presented as "PSSO registration is broken" and took a long time to attribute.

Ordering is also mutable at runtime, not just arbitrary: save_domain does retain(…) then push(…), so re-running domains join moves that domain to the end of the list — re-enrolling a domain makes it less likely to be selected until the next restart.

Preferring the managed domain gives a rule that can be stated in a sentence: MDM policy beats a hand-run domains join. The managed enrollment carries the connector and device group the organisation assigned; a manual join may point elsewhere.

The teardown matters because load_all reloads every *.json whether or not the config that created it still exists. A machine that was managed once keeps a phantom managed domain forever — never refreshed, never revocable by removing the profile, and still competing to be active. Observed on a live host: ak-mdm-managed.json persisted for hours after its managed preferences stopped resolving.

How was this tested?

cargo check -p ak-sysd. Verified on macOS 26.6 with both a token-less ak-mdm-managed and a working mac domain: before, RegisterDevice returned 403 "credentials were not provided"; after, sysd selects mac and the request authenticates.

Known limitation

This makes selection deterministic; it does not resolve the underlying split. The storage layer holds N domains and exposes DomainList/DomainEnroll/DomainUnenroll, but nine call sites (ping, auth/apple, auth/token, auth/interactive, session, directory) each take active() and ignore the rest — only device check-in and healthcheck_all fan out. If a machine is deliberately enrolled with two authentik instances, those paths will silently use one of them. Making multi-domain genuinely work means binding a domain to each call site, which is a larger design decision for whoever owns the domain model.

Linked issues

One of several macOS regressions found alongside the Rust migration (#1262), with #1358 and #1360.

Checklist

  • The project has been linted, built, and tested (make all)
  • The documentation has been updated and formatted (make docs)

@codecov

codecov Bot commented Aug 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.77778% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 42.86%. Comparing base (bb7fd33) to head (31c686d).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
ak-sysd/src/cfg/domain.rs 77.77% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1359      +/-   ##
==========================================
+ Coverage   42.84%   42.86%   +0.02%     
==========================================
  Files         161      161              
  Lines       14706    14719      +13     
  Branches     9721     9734      +13     
==========================================
+ Hits         6301     6310       +9     
- Misses       8270     8274       +4     
  Partials      135      135              
Flag Coverage Δ
rs-e2e 36.31% <77.77%> (+0.03%) ⬆️
rs-macos-26-ak-agent ?
rs-macos-26-ak-agent-desktop 0.00% <ø> (ø)
rs-macos-26-ak-api-cli-gen 66.71% <ø> (ø)
rs-macos-26-ak-browser-support 24.19% <ø> (ø)
rs-macos-26-ak-cli 11.92% <ø> (ø)
rs-macos-26-ak-platform 0.29% <ø> (ø)
rs-macos-26-ak-platform-facts 83.96% <ø> (ø)
rs-macos-26-ak-sysd 34.92% <50.00%> (+0.01%) ⬆️
rs-ubuntu-24.04-ak-agent 33.81% <ø> (ø)
rs-ubuntu-24.04-ak-agent-desktop 0.00% <ø> (ø)
rs-ubuntu-24.04-ak-api-cli-gen 66.71% <ø> (ø)
rs-ubuntu-24.04-ak-browser-support 24.19% <ø> (ø)
rs-ubuntu-24.04-ak-cli 11.92% <ø> (ø)
rs-ubuntu-24.04-ak-nss 77.37% <ø> (ø)
rs-ubuntu-24.04-ak-pam 0.00% <ø> (ø)
rs-ubuntu-24.04-ak-platform 0.29% <ø> (ø)
rs-ubuntu-24.04-ak-platform-facts 83.14% <ø> (ø)
rs-ubuntu-24.04-ak-sysd 35.02% <50.00%> (+0.01%) ⬆️
rs-ubuntu-24.04-arm-ak-agent 33.81% <ø> (+2.10%) ⬆️
rs-ubuntu-24.04-arm-ak-agent-desktop 0.00% <ø> (ø)
rs-ubuntu-24.04-arm-ak-api-cli-gen 66.71% <ø> (ø)
rs-ubuntu-24.04-arm-ak-browser-support 24.19% <ø> (ø)
rs-ubuntu-24.04-arm-ak-cli 11.92% <ø> (ø)
rs-ubuntu-24.04-arm-ak-nss 77.37% <ø> (ø)
rs-ubuntu-24.04-arm-ak-pam 0.00% <ø> (ø)
rs-ubuntu-24.04-arm-ak-platform 0.29% <ø> (ø)
rs-ubuntu-24.04-arm-ak-platform-facts 83.14% <ø> (ø)
rs-ubuntu-24.04-arm-ak-sysd 35.02% <50.00%> (+0.01%) ⬆️
rs-windows-2025-ak-agent 33.81% <ø> (ø)
rs-windows-2025-ak-agent-desktop 0.00% <ø> (ø)
rs-windows-2025-ak-api-cli-gen 66.71% <ø> (ø)
rs-windows-2025-ak-browser-support 24.19% <ø> (ø)
rs-windows-2025-ak-cli 11.92% <ø> (ø)
rs-windows-2025-ak-platform 0.30% <ø> (ø)
rs-windows-2025-ak-platform-facts 79.37% <ø> (-0.24%) ⬇️
rs-windows-2025-ak-sysd 33.20% <50.00%> (+0.02%) ⬆️
swift 3.41% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@authentik-automation

Copy link
Copy Markdown
Contributor

🚀 Deployed on https://pr-1359--authentik-pkg.netlify.app

@authentik-automation
authentik-automation Bot temporarily deployed to pull request August 11, 2026 22:40 Inactive
@PeshekDotDev
PeshekDotDev force-pushed the fix/sysd-active-domain-token branch from 2cec743 to b9e7241 Compare August 11, 2026 23:51
@PeshekDotDev PeshekDotDev changed the title ak-sysd: skip token-less domains when selecting the active one ak-sysd: make managed-domain selection and lifecycle deterministic Aug 11, 2026
@authentik-automation
authentik-automation Bot temporarily deployed to pull request August 12, 2026 00:33 Inactive
@PeshekDotDev PeshekDotDev self-assigned this Aug 12, 2026
@PeshekDotDev
PeshekDotDev force-pushed the fix/sysd-active-domain-token branch from b9e7241 to 31c686d Compare August 12, 2026 06:07
@authentik-automation
authentik-automation Bot temporarily deployed to pull request August 12, 2026 06:43 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant