Skip to content

Conversation

@lstocchi
Copy link
Contributor

@lstocchi lstocchi commented Dec 1, 2025

Following the work done in #26277 (which allowed init and remove operations for the "Hyper-V Administrators" group), this PR extends that logic to all actions targeting Hyper-v machines.

it resolves #27614

Checklist

Ensure you have completed the following checklist for your pull request to be reviewed:

  • Certify you wrote the patch or otherwise have the right to pass it on as an open-source patch by signing all
    commits. (git commit -s). (If needed, use git commit -s --amend). The author email must match
    the sign-off email address. See CONTRIBUTING.md
    for more information.
  • Referenced issues using Fixes: #00000 in commit message (if applicable)
  • Tests have been added/updated (or no tests are needed)
  • Documentation has been updated (or no documentation changes are needed)
  • All commits pass make validatepr (format/lint checks)
  • Release note entered in the section below (or None if no user-facing changes)

Does this PR introduce a user-facing change?

Permission validation for Hyper-V has been extended to cover all actions. Users in the "Hyper-V Administrators" group can now execute all actions without needing mandatory full local Administrator privileges. Users without the right privileges will now see an error message when attempting to execute these commands.

@mheon
Copy link
Member

mheon commented Dec 1, 2025

@l0rd PTAL

assert.Equal(t, []define.VMType{define.AppleHvVirt, define.LibKrun}, SupportedProviders())
case "windows":
assert.Equal(t, []define.VMType{define.WSLVirt, define.HyperVVirt}, SupportedProviders())
assert.Equal(t, []define.VMType{define.HyperVVirt, define.WSLVirt}, SupportedProviders())
Copy link
Member

Choose a reason for hiding this comment

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

Why the ordering swap?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Bc SupportedProviders() returns []define.VMType{define.HyperVVirt, define.WSLVirt} and assert.Equal performs a strict comparison so the items order matters. It failed the way it was written.

Copy link
Member

Choose a reason for hiding this comment

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

And CI isn't catching it because pkg\machine\provider is skipped. @lstocchi would you mind removing that package from the skipped ones?

Copy link
Member

Choose a reason for hiding this comment

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

Can't you use assert.ElementsMatch or similar? I dislike having tests be this fragile.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mheon @l0rd done both changes

// Variables to hold permission check functions for testing purposes.
var (
hasAdminRightsFunc = windows.HasAdminRights
hasHyperVAdminRightsFunc = hyperv.HasHyperVAdminRights
Copy link
Member

Choose a reason for hiding this comment

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

These don't seem to add much as written. It seems like a single functioning combining both would be more useful, and line 86 could just be windows.HasAdminRights()

Copy link
Member

Choose a reason for hiding this comment

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

@lstocchi you could add a new hyperv.CanList() function similar to the hyperv.canCreate() where you hide the logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done. I named it HasHyperVPermissions as it's more generic and not just related to list, let me know

new(wsl.WSLStubber),
new(hyperv.HyperVStubber),
}
if hasAdminRightsFunc() || hasHyperVAdminRightsFunc() {
Copy link
Member

Choose a reason for hiding this comment

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

I am struggling with the logic here if I am a user. If I don't have permissions, should it not exist? Earlier, when we returned an error, that certainly made sense to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They have two different purposes imo. Above (GetByVMType()) returns an error because user is explicitly executing a command and if they do not have rights to do so we stop them and return an error.
GetAll is more a discovery thing. It is used by the list command. If someone wants to list machines, it's annoying to show an error message, maybe they're just interested to WSL. So if you don't have permissions to work with Hyper-V it skips it. Hyper-v console works the same - if you don't have the rights you see nothing.

Copy link
Member

Choose a reason for hiding this comment

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

This is about podman machine ls, and the problem has changed in 6.0. If the user doesn't have privileges we still want to list WSL machines.

But we should indeed fail when the user specifies --provider hyperv. And if the default provider is hyperv too.

Update GetAll() and GetByVMType() to add a check to prevent non hyper-v admin users to
interact with hyperv machines.
Users can work with hyperv machines only with elevated rights or if
members of the hyperv administrators group

Signed-off-by: lstocchi <lstocchi@redhat.com>
fixes broken windows tests and enables them to be run on
windows CI

Signed-off-by: lstocchi <lstocchi@redhat.com>
Signed-off-by: lstocchi <lstocchi@redhat.com>
Copy link
Member

@l0rd l0rd left a comment

Choose a reason for hiding this comment

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

I did some tests and all work as expected with the expection of some corner cases.

Inspecting or starting a WSL machine, if the default provider is hyperv, fails:

> $env:CONTAINERS_MACHINE_PROVIDER="hyperv"
> podman.exe machine inspect mywslmachine`
Error: Hyper-V machines require Hyper-V admin rights...
> podman.exe machine start mywslmachine`
Error: Hyper-V machines require Hyper-V admin rights...

And this is awkward because podman machine ls works and shows the WSL machines. But that may not be related to this PR.

Also commands like machine cp, machine ssh etc..., against an hyperv machine, should work too, even if the user doesn't have Hyper-V admin privileges.

That said these scenarios aren't blockers from my point of view.

@baude
Copy link
Member

baude commented Dec 6, 2025

@lstocchi will you be addressing the problems @l0rd mentions here or @l0rd do we need to plan for this?

else LGTM

@l0rd
Copy link
Member

l0rd commented Dec 8, 2025

From my point of view this PR is now mergeable as is. It's up to @lstocchi, if he has bandwidth, to fix those corner cases. Otherwise I will open an issue and we can prioritize them later. Let's wait for him to come back (today is national holiday but tomorrow he should be back).

@lstocchi
Copy link
Contributor Author

lstocchi commented Dec 9, 2025

I did some tests and all work as expected with the expection of some corner cases.

Inspecting or starting a WSL machine, if the default provider is hyperv, fails:

> $env:CONTAINERS_MACHINE_PROVIDER="hyperv"
> podman.exe machine inspect mywslmachine`
Error: Hyper-V machines require Hyper-V admin rights...
> podman.exe machine start mywslmachine`
Error: Hyper-V machines require Hyper-V admin rights...

And this is awkward because podman machine ls works and shows the WSL machines. But that may not be related to this PR.

I think this is a change that would affect all OSes with more than 1 provider, not just Windows. I'd rather have a separate PR to test it also on macOS.

Also commands like machine cp, machine ssh etc..., against an hyperv machine, should work too, even if the user doesn't have Hyper-V admin privileges.

That said these scenarios aren't blockers from my point of view.

I'm going to add the change here as it seems related to this PR

@l0rd
Copy link
Member

l0rd commented Dec 9, 2025

After discussion we decided that it was ok to merge the PR as is. I have created a separate issue #27723

/approve /lgtm

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Dec 9, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: l0rd, lstocchi

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 9, 2025
@l0rd
Copy link
Member

l0rd commented Dec 9, 2025

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Dec 9, 2025
@openshift-merge-bot openshift-merge-bot bot merged commit f66f7c8 into containers:main Dec 9, 2025
86 checks passed
@lstocchi lstocchi deleted the i27614 branch December 9, 2025 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. machine release-note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prevent non hyper-v admin users to execute machine commands

4 participants