Merge pull request #6662 from lukebakken/fixes/lrb/gh-4503-hyper-v-admin-privs

Fix for 4503
This commit is contained in:
Mitchell Hashimoto 2015-12-14 15:56:55 -08:00
commit 06a1461081
3 changed files with 41 additions and 4 deletions

View File

@ -61,6 +61,25 @@ module Vagrant
(`reg query HKU\\S-1-5-19 2>&1` =~ /ERROR/).nil?
end
# Checks if the user running Vagrant on Windows is a member of the
# "Hyper-V Administrators" group.
#
# From: https://support.microsoft.com/en-us/kb/243330
# SID: S-1-5-32-578
# Name: BUILTIN\Hyper-V Administrators
#
# @return [Boolean]
def windows_hyperv_admin?
begin
username = ENV["USERNAME"]
process = Subprocess.execute("net", "localgroup", "Hyper-V Administrators")
output = process.stdout.chomp
return output.include?(username)
rescue Errors::CommandUnavailableWindows
return false
end
end
# This takes any path and converts it from a Windows path to a
# Cygwin or msys style path.
#

View File

@ -16,7 +16,8 @@ module VagrantPlugins
raise Errors::WindowsRequired
end
if !Vagrant::Util::Platform.windows_admin?
if !Vagrant::Util::Platform.windows_admin? and
!Vagrant::Util::Platform.windows_hyperv_admin?
raise Errors::AdminRequired
end

View File

@ -15,6 +15,7 @@ describe VagrantPlugins::HyperV::Provider do
machine.stub(id: "foo")
platform.stub(windows?: true)
platform.stub(windows_admin?: true)
platform.stub(windows_hyperv_admin?: true)
powershell.stub(available?: true)
end
@ -26,11 +27,18 @@ describe VagrantPlugins::HyperV::Provider do
expect(subject).to_not be_usable
end
it "returns false if not an admin" do
it "returns false if neither an admin nor a hyper-v admin" do
platform.stub(windows_admin?: false)
platform.stub(windows_hyperv_admin?: false)
expect(subject).to_not be_usable
end
it "returns true if not an admin but is a hyper-v admin" do
platform.stub(windows_admin?: false)
platform.stub(windows_hyperv_admin?: true)
expect(subject).to be_usable
end
it "returns false if powershell is not available" do
powershell.stub(available?: false)
expect(subject).to_not be_usable
@ -43,8 +51,17 @@ describe VagrantPlugins::HyperV::Provider do
to raise_error(VagrantPlugins::HyperV::Errors::WindowsRequired)
end
it "raises an exception if not an admin" do
it "raises an exception if neither an admin nor a hyper-v admin" do
platform.stub(windows_admin?: false)
platform.stub(windows_hyperv_admin?: false)
expect { subject.usable?(true) }.
to raise_error(VagrantPlugins::HyperV::Errors::AdminRequired)
end
it "raises an exception if neither an admin nor a hyper-v admin" do
platform.stub(windows_admin?: false)
platform.stub(windows_hyperv_admin?: false)
expect { subject.usable?(true) }.
to raise_error(VagrantPlugins::HyperV::Errors::AdminRequired)