Skip to content

improve error handling during re-registration#673

Open
AdamVB wants to merge 4 commits into
mainfrom
fix/protect-mondoo-yml-during-reregister
Open

improve error handling during re-registration#673
AdamVB wants to merge 4 commits into
mainfrom
fix/protect-mondoo-yml-during-reregister

Conversation

@AdamVB

@AdamVB AdamVB commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Backs up mondoo.yml to mondoo.yml.bak before deletion during re-registration, and restores it automatically if cnspec login fails
  • Adds $LASTEXITCODE checks on both cnspec logout and cnspec login commands — previously only the output string was checked for "ERROR", which misses crashes, network timeouts, and
    non-zero exits without that keyword
  • Adds a file existence check after login to catch cases where login reports success but produces no config file

Problem

Since commit b177c0c ("add reregister feature", Dec 2023), running the installer with a -RegistrationToken on a system that already has mondoo.yml triggers a logout → delete → login
cycle. If cnspec login fails for any reason (network issue, invalid token, API outage, DNS failure), the config file is already gone with no way to recover. This leaves the agent
unregistered and unable to communicate with the platform until someone manually re-registers.

The risk was increased by commit c930408 (Jan 2024), which removed the error check on the logout command — meaning a failed logout followed by a failed login still deleted the config.

This also runs on every installer invocation with a token, even when the installed version is already current, so scheduled updater tasks can trigger this path repeatedly.

Test plan

  • Run installer with valid token on a system with existing mondoo.yml — should logout, re-register, and clean up .bak ✅
  • Run installer with invalid token on a system with existing mondoo.yml — should fail login, restore .bak to mondoo.yml, and throw ✅
  • Run installer with valid token on a system with no existing mondoo.yml — should register normally with no backup/restore logic ✅
  • Simulate network failure during login (e.g. block api.mondoo.com) — should restore .bak and throw
  • Verify mondoo.yml.bak is not left behind after successful re-registration ✅

Also:

Add random delay for windows update scheduled task

@mondoo-code-review mondoo-code-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-registration now backs up and restores the config file on failure, preventing agents from being left unconfigured.

Comment thread install.ps1
Comment thread install.ps1
Comment thread install.ps1
mondoo-code-review[bot]
mondoo-code-review Bot previously approved these changes Mar 24, 2026

@mondoo-code-review mondoo-code-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Error handling improvements for config backup/removal during re-registration look correct.

Comment thread install.ps1
mondoo-code-review[bot]
mondoo-code-review Bot previously approved these changes Mar 24, 2026

@mondoo-code-review mondoo-code-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-registration now backs up and restores mondoo.yml on login failure, preventing agents from being left unconfigured.

@mondoo-code-review mondoo-code-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Adds optional random delay (splay) to the scheduled task trigger, low risk change.

Comment thread install.ps1 Outdated

@mondoo-code-review mondoo-code-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Adds input validation for the Splay parameter when configuring scheduled task random delay.

@mondoo-code-review mondoo-code-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-registration now backs up and restores mondoo.yml on login failure, and adds splay/random-delay support for scheduled tasks.

Comment thread install.ps1
if (Test-Path -Path "$configPath.bak") {
Remove-Item "$configPath.bak" -Force
}
if ($output) {

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.

should we throw $output if $loginFailed = true so we maintain the previous behaviour?

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