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? (`reg query HKU\\S-1-5-19 2>&1` =~ /ERROR/).nil?
end 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 # This takes any path and converts it from a Windows path to a
# Cygwin or msys style path. # Cygwin or msys style path.
# #

View File

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

View File

@ -15,6 +15,7 @@ describe VagrantPlugins::HyperV::Provider do
machine.stub(id: "foo") machine.stub(id: "foo")
platform.stub(windows?: true) platform.stub(windows?: true)
platform.stub(windows_admin?: true) platform.stub(windows_admin?: true)
platform.stub(windows_hyperv_admin?: true)
powershell.stub(available?: true) powershell.stub(available?: true)
end end
@ -26,11 +27,18 @@ describe VagrantPlugins::HyperV::Provider do
expect(subject).to_not be_usable expect(subject).to_not be_usable
end 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_admin?: false)
platform.stub(windows_hyperv_admin?: false)
expect(subject).to_not be_usable expect(subject).to_not be_usable
end 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 it "returns false if powershell is not available" do
powershell.stub(available?: false) powershell.stub(available?: false)
expect(subject).to_not be_usable expect(subject).to_not be_usable
@ -43,8 +51,17 @@ describe VagrantPlugins::HyperV::Provider do
to raise_error(VagrantPlugins::HyperV::Errors::WindowsRequired) to raise_error(VagrantPlugins::HyperV::Errors::WindowsRequired)
end 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_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) }. expect { subject.usable?(true) }.
to raise_error(VagrantPlugins::HyperV::Errors::AdminRequired) to raise_error(VagrantPlugins::HyperV::Errors::AdminRequired)