Class: Knife::Server::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/knife/server/credentials.rb

Overview

Creates credentials for a Chef server.

Instance Method Summary collapse

Constructor Details

#initialize(ssh, validation_key_path, options = {}) ⇒ Credentials

Returns a new instance of Credentials.



27
28
29
30
31
32
# File 'lib/knife/server/credentials.rb', line 27

def initialize(ssh, validation_key_path, options = {})
  @ssh = ssh
  @validation_key_path = validation_key_path
  @omnibus = options[:omnibus]
  @io = options.delete(:io) || $stdout
end

Instance Method Details

#create_root_clientObject



52
53
54
# File 'lib/knife/server/credentials.rb', line 52

def create_root_client
  @ssh.exec!(omnibus? ? client_omnibus_cmd : client_chef10_cmd)
end

#install_client_key(user, client_key_path, suffix = Time.now.to_i) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/knife/server/credentials.rb', line 56

def install_client_key(user, client_key_path, suffix = Time.now.to_i)
  if omnibus? && File.exist?(client_key_path)
    use_current_client_key(user, client_key_path)
  else
    create_new_client_key(user, client_key_path, suffix)
  end

  @ssh.exec!("rm -f /tmp/chef-client-#{user}.pem")
end

#install_validation_key(suffix = Time.now.to_i) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/knife/server/credentials.rb', line 34

def install_validation_key(suffix = Time.now.to_i)
  dest = @validation_key_path
  backup = backup_file_path(@validation_key_path, suffix)

  if File.exist?(dest)
    info "Creating backup of #{dest} locally at #{backup}"
    FileUtils.cp(dest, backup)
  end

  chef10_key = "/etc/chef/validation.pem"
  omnibus_key = "/etc/chef-server/chef-validator.pem"

  info "Installing validation private key locally at #{dest}"
  File.open(dest, "wb") do |f|
    f.write(@ssh.exec!("cat #{omnibus? ? omnibus_key : chef10_key}"))
  end
end