Class: Nucleon::Util::SSH::Keypair

Inherits:
Object
  • Object
show all
Defined in:
lib/core/util/ssh.rb

Overview


Keypair interface

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_data, is_new, original_key, passphrase = nil) ⇒ Keypair




87
88
89
90
91
92
93
94
# File 'lib/core/util/ssh.rb', line 87

def initialize(key_data, is_new, original_key, passphrase = nil)
  @type          = key_data.type
  @private_key   = key_data.private_key
  @encrypted_key = is_new ? key_data.encrypted_private_key : original_key
  @public_key    = key_data.public_key
  @ssh_key       = key_data.ssh_public_key
  @passphrase    = passphrase
end

Instance Attribute Details

#encrypted_keyObject (readonly)

Returns the value of attribute encrypted_key.



83
84
85
# File 'lib/core/util/ssh.rb', line 83

def encrypted_key
  @encrypted_key
end

#passphraseObject (readonly)

Returns the value of attribute passphrase.



83
84
85
# File 'lib/core/util/ssh.rb', line 83

def passphrase
  @passphrase
end

#private_keyObject (readonly)

Returns the value of attribute private_key.



83
84
85
# File 'lib/core/util/ssh.rb', line 83

def private_key
  @private_key
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



83
84
85
# File 'lib/core/util/ssh.rb', line 83

def public_key
  @public_key
end

#ssh_keyObject (readonly)

Returns the value of attribute ssh_key.



83
84
85
# File 'lib/core/util/ssh.rb', line 83

def ssh_key
  @ssh_key
end

#typeObject (readonly)

Returns the value of attribute type.



83
84
85
# File 'lib/core/util/ssh.rb', line 83

def type
  @type
end

Class Method Details

.render(type, key_base = 'id') ⇒ Object



136
137
138
# File 'lib/core/util/ssh.rb', line 136

def self.render(type, key_base = 'id')
  "#{key_base}_#{type.downcase}"
end

Instance Method Details

#private_key_file(key_path = nil, key_base = 'id') ⇒ Object




98
99
100
101
102
103
# File 'lib/core/util/ssh.rb', line 98

def private_key_file(key_path = nil, key_base = 'id')
  key_path = SSH.key_path if key_path.nil?
  key_name = render(key_base)

  File.join(key_path, "#{key_name}")
end

#public_key_file(key_path = nil, key_base = 'id') ⇒ Object



105
106
107
# File 'lib/core/util/ssh.rb', line 105

def public_key_file(key_path = nil, key_base = 'id')
  private_key_file(key_path, key_base) + '.pub'
end

#render(key_base = 'id') ⇒ Object




132
133
134
# File 'lib/core/util/ssh.rb', line 132

def render(key_base = 'id')
  self.class.render(type, key_base)
end

#store(key_path = nil, key_base = 'id', secure = true) ⇒ Object




111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/core/util/ssh.rb', line 111

def store(key_path = nil, key_base = 'id', secure = true)
  private_key_file = private_key_file(key_path, key_base)
  public_key_file  = public_key_file(key_path, key_base)

  if secure
    private_success = Disk.write(private_key_file, encrypted_key)
  else
    private_success = Disk.write(private_key_file, private_key)
  end
  FileUtils.chmod(0600, private_key_file) if private_success

  public_success = Disk.write(public_key_file, ssh_key)

  if private_success && public_success
    return { :private_key => private_key_file, :public_key => public_key_file }
  end
  false
end