Class: Installation::SshKey

Inherits:
Object
  • Object
show all
Includes:
Yast::Logger
Defined in:
src/lib/installation/ssh_key.rb

Overview

Class that allows to memorize a particular SSH keys found in a partition.

Used to implement the SSH keys importing functionality.

Defined Under Namespace

Classes: KeyFile

Constant Summary collapse

PUBLIC_FILE_SUFFIX =
".pub".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ SshKey

Returns a new instance of SshKey.


34
35
36
37
# File 'src/lib/installation/ssh_key.rb', line 34

def initialize(name)
  @name = name
  @files = []
end

Instance Attribute Details

#atimeTime

Returns access time of the most recently accessed file.

Returns:

  • (Time)

    access time of the most recently accessed file


30
31
32
# File 'src/lib/installation/ssh_key.rb', line 30

def atime
  @atime
end

#filesArray<Keyfile>

Returns list of files associated to the key.

Returns:

  • (Array<Keyfile>)

    list of files associated to the key


32
33
34
# File 'src/lib/installation/ssh_key.rb', line 32

def files
  @files
end

#nameString

Returns name for the user to identify the key.

Returns:

  • (String)

    name for the user to identify the key


28
29
30
# File 'src/lib/installation/ssh_key.rb', line 28

def name
  @name
end

Instance Method Details

#add_file(path) ⇒ Object (protected)


73
74
75
76
77
78
79
# File 'src/lib/installation/ssh_key.rb', line 73

def add_file(path)
  content = File.read(path)
  permissions = File.stat(path).mode
  files << KeyFile.new(File.basename(path), content, permissions)
  atime = File.atime(path)
  self.atime = atime unless self.atime && self.atime > atime
end

#read_files(priv_filename) ⇒ Object


39
40
41
42
43
# File 'src/lib/installation/ssh_key.rb', line 39

def read_files(priv_filename)
  add_file(priv_filename) if File.exist?(priv_filename)
  pub_filename = priv_filename + PUBLIC_FILE_SUFFIX
  add_file(pub_filename) if File.exist?(pub_filename)
end

#to_sObject

Override to_s method for logging.


64
65
66
67
# File 'src/lib/installation/ssh_key.rb', line 64

def to_s
  "#{name}:\n" +
    files.collect { |file| "  #{file.filename}" }.join("\n")
end

#write_files(dir) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'src/lib/installation/ssh_key.rb', line 45

def write_files(dir)
  log.info "Write SSH keys to #{dir}:\n#{self}"
  files.each do |file|
    path = File.join(dir, file.filename)
    File.write(path, file.content)
    if file.filename =~ /^ssh_host_.*key$/
      # ssh deamon accepts only private keys with restricted
      # file permissions.
      log.info("Set permissions of #{file.filename} to 0o600")
      File.chmod(0o600, path)
    else
      # Taking already given permissions
      log.info("Set permissions of #{file.filename} to 0o#{file.permissions.to_s(8)}")
      File.chmod(file.permissions, path)
    end
  end
end