Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows support hacks #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/chef/knife/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# Copyright (c) Matthew Macdonald-Wallace / Green and Secure IT Limited 2012
#

require 'windows_redefine_file_symlink'

# Monkey patch ::Chef::Knife to give us back the config file, cause there's
# no public method to actually use the lookup logic
class Chef
Expand All @@ -20,7 +22,7 @@ def get_config_file
GreenAndSecure.locate_config_file config
end

File.dirname(config[:config_file])
config[:config_file]
end
end
end
Expand All @@ -34,7 +36,12 @@ def current_chef_version
end

def chef_config_base
@@knife.get_config_file
config_file = @@knife.get_config_file
if config_file
File.dirname(config_file)
else
Dir.home + "/.chef"
end
end

# Copied from chef/knife.rb
Expand Down Expand Up @@ -222,7 +229,7 @@ def run
knife_config.config[:client_key] = "#{GreenAndSecure::chef_config_base}/#{@client_name}-#{@config_name}.pem"
knife_config.run

puts "#{GreenAndSecure::chef_config_base}/knife-#{@config_name}.rb has been sucessfully created"
puts "#{GreenAndSecure::chef_config_base}/knife-#{@config_name}.rb has been successfully created"
GreenAndSecure::BlockList.new.run
use = GreenAndSecure::BlockUse.new
use.name_args = [@config_name]
Expand Down
56 changes: 56 additions & 0 deletions lib/windows_redefine_file_symlink.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# File class override of symlink behaviors
# Created by Gabe

require 'open3'

class << File
alias_method :old_symlink, :symlink
alias_method :old_symlink?, :symlink?
alias_method :old_readlink, :readlink

def symlink(old_name, new_name)
#if on windows, call mklink, else self.symlink
if RUBY_PLATFORM =~ /mswin32|cygwin|mingw|bccwin/
#windows mklink syntax is reverse of unix ln -s
#windows mklink is built into cmd.exe
#windows cmd.exe will choke on forward slash before filename.
old_name.gsub!(/\//, '\\') #replace forward slash with a backslash. Bug case: escaped forward slashes.
#vulnerable to command injection, but okay because this is a hack to make a cli tool work.
stdin, stdout, stderr, wait_thr = Open3.popen3("cmd.exe /c mklink \"#{new_name}\" \"#{old_name}\"")
#puts stdout.gets
puts stderr.gets
wait_thr.value.exitstatus
else
self.old_symlink(old_name, new_name)
end
end

def symlink?(file_name)
if RUBY_PLATFORM =~ /mswin32|cygwin|mingw|bccwin/
#vulnerable to command injection because calling with cmd.exe with /c?
stdin, stdout, stderr, wait_thr = Open3.popen3("cmd.exe /c dir \"#{file_name}\" | find \"SYMLINK\"")
wait_thr.value.exitstatus
else
self.old_symlink?(file_name)
end
end

def readlink(file_name)
if RUBY_PLATFORM =~ /mswin32|cygwin|mingw|bccwin/
#vulnerable to command injection because calling with cmd.exe with /c?
file_name = file_name.sub(/(.*)\//,'\1\\') #replace final forward slash with backslash to workaround cmd.exe
stdin, stdout, stderr, wait_thr = Open3.popen3("cmd.exe /c dir \"#{file_name}\" | find \"SYMLINK\"")
wait_thr.value.exitstatus
line = stdout.gets
if line
target = line.match('.+\[(.+)\]')[1]
else
target = nil
end
target
else
self.old_readlink?(file_name)
end
end
end