Class: Chef::Knife::WindowsCertInstall

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/windows_cert_install.rb

Instance Method Summary collapse

Instance Method Details

#get_cert_passphraseObject



32
33
34
35
36
# File 'lib/chef/knife/windows_cert_install.rb', line 32

def get_cert_passphrase
  print "Enter given certificate's passphrase (empty for no passphrase):"
  passphrase = STDIN.gets
  passphrase.strip
end

#runObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chef/knife/windows_cert_install.rb', line 38

def run
  STDOUT.sync = STDERR.sync = true

  if Chef::Platform.windows?
    if @name_args.empty?
      ui.error "Please specify the certificate path. e.g-  'knife windows cert install <path>"
      exit 1
    end
    file_path = @name_args.first
    config[:cert_passphrase] = get_cert_passphrase unless config[:cert_passphrase]

    begin
      ui.info "Adding certificate to the Windows Certificate Store..."
      result = `powershell.exe -Command " '#{config[:cert_passphrase]}' | certutil -importPFX '#{file_path}' AT_KEYEXCHANGE"`
      if $?.exitstatus == 0
        ui.info "Certificate added to Certificate Store"
      else
        ui.info "Error adding the certificate. Use -VV option for details"
      end
      Chef::Log.debug "#{result}"
    rescue => e
      puts "ERROR: + #{e}"
    end
  else
    ui.error "Certificate can be installed on Windows system only"
    exit 1
  end
end