Skip to content

Restart the KVM agent with sudo when the host user is not root - #14741

Open
bhouse-nexthop wants to merge 1 commit into
apache:4.22from
bhouse-nexthop:fix-kvm-agent-restart-sudo
Open

bhouse-nexthop wants to merge 1 commit into
apache:4.22from
bhouse-nexthop:fix-kvm-agent-restart-sudo

Conversation

@bhouse-nexthop

Copy link
Copy Markdown
Collaborator

Description

This PR runs the KVM agent restart in cancelHostMaintenance with sudo when the host user is not root.

With kvm.ssh.to.agent=true, cancelling maintenance on a KVM host whose agent is not Up restarts the agent over SSH as the host's stored user. The command had no sudo:

  • A host can be added with a non-root user. LibvirtServerDiscoverer already runs sudo cloudstack-setup-agent for one.
  • For such a host the restart fails: Could not restart agent on Host ... Failed to restart cloudstack-agent.service: Interactive authentication required.

Fix: new getAgentRestartCommand(username):

Host user Command
root service cloudstack-agent restart (unchanged)
other sudo service cloudstack-agent restart

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)
  • Build/CI
  • Test (unit or integration test code)

Feature/Enhancement Scale or Bug Severity

Bug Severity

  • BLOCKER
  • Critical
  • Major
  • Minor
  • Trivial

How Has This Been Tested?

  • Seen on 4.22.1: Ubuntu 24.04 KVM hosts added with user cloudstack, which has NOPASSWD sudo for /usr/sbin/service.
  • ResourceManagerImplTest passes (74 tests). The mocked host user is user, so the existing restart tests now expect sudo. New testGetAgentRestartCommand covers root and non-root.
  • Checkstyle is clean for server.
  • Not yet run on the cluster.

How did you try to break this feature and the system with this change?

  • Root hosts get the same command as before.
  • A non-root host user needs passwordless sudo for service, which the discoverer already needs for cloudstack-setup-agent.

cancelHostMaintenance on a KVM host whose agent is not Up restarts the
agent over SSH (kvm.ssh.to.agent=true). It ran "service cloudstack-agent
restart" as the host's stored user, without sudo. A host can be added
with a non-root user, and LibvirtServerDiscoverer already runs
cloudstack-setup-agent with sudo for such a user. The restart then
fails with "Interactive authentication required".

Prefix the restart with sudo when the user is not root.

Signed-off-by: Brad House <bhouse@nexthop.ai>
@codecov

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 18.32%. Comparing base (b56bbc2) to head (ccea714).

Additional details and impacted files
@@            Coverage Diff            @@
##               4.22   #14741   +/-   ##
=========================================
  Coverage     18.32%   18.32%           
- Complexity    16644    16648    +4     
=========================================
  Files          5939     5939           
  Lines        537787   537792    +5     
  Branches      65932    65933    +1     
=========================================
+ Hits          98544    98558   +14     
+ Misses       428033   428024    -9     
  Partials      11210    11210           
Flag Coverage Δ
uitests 4.08% <ø> (ø)
unittests 19.41% <100.00%> (+<0.01%) ⬆️

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.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
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.

1 participant