Class: Gitolite::SSHKey

Inherits:
Object
  • Object
show all
Defined in:
lib/gitolite/ssh_key.rb

Overview

Models an SSH key within gitolite provides support for multikeys

Types of multi keys:

bob.pub => username: bob
[email protected] => username: bob, location: desktop
[email protected] => username: [email protected]
[email protected]@desktop.pub => username: [email protected], location: desktop

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, blob, email, owner = nil, location = "") ⇒ SSHKey

Returns a new instance of SSHKey.



14
15
16
17
18
19
20
21
# File 'lib/gitolite/ssh_key.rb', line 14

def initialize(type, blob, email, owner = nil, location = "")
  @type = type
  @blob = blob
  @email = email

  @owner = owner || email
  @location = location
end

Instance Attribute Details

#blobObject

Returns the value of attribute blob.



12
13
14
# File 'lib/gitolite/ssh_key.rb', line 12

def blob
  @blob
end

#emailObject

Returns the value of attribute email.



12
13
14
# File 'lib/gitolite/ssh_key.rb', line 12

def email
  @email
end

#locationObject

Returns the value of attribute location.



12
13
14
# File 'lib/gitolite/ssh_key.rb', line 12

def location
  @location
end

#ownerObject

Returns the value of attribute owner.



12
13
14
# File 'lib/gitolite/ssh_key.rb', line 12

def owner
  @owner
end

#typeObject

Returns the value of attribute type.



12
13
14
# File 'lib/gitolite/ssh_key.rb', line 12

def type
  @type
end

Class Method Details

.from_file(key) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gitolite/ssh_key.rb', line 23

def self.from_file(key)
  raise "#{key} does not exist!" unless File.exists?(key)

  #Get our owner and location
  File.basename(key) =~ /^([\w\.-]+(?:@(?:[\w-]+\.)+\D{2,4})?)(?:@(\w+))?.pub$/i
  owner = $1
  location = $2 || ""

  # Use string key constructor
  self.from_string(File.read(key), owner, location)
end

.from_string(key_string, owner, location = "") ⇒ Object

Construct a SSHKey from a string



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gitolite/ssh_key.rb', line 36

def self.from_string(key_string, owner, location = "")
  if owner.nil?
    raise ArgumentError, "owner was nil, you must specify an owner"
  end

  #Get parts of the key
  type, blob, email = key_string.split

  # We need at least a type or blob
  if type.nil? || blob.nil?
    raise ArgumentError, "'#{key_string}' is not a valid SSH key string"
  end

  #If the key didn't have an email, just use the owner
  if email.nil?
    email = owner
  end

  self.new(type, blob, email, owner, location)
end

Instance Method Details

#==(key) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/gitolite/ssh_key.rb', line 75

def ==(key)
  @type == key.type &&
  @blob == key.blob &&
  @email == key.email &&
  @owner == key.owner &&
  @location == key.location
end

#filenameObject



69
70
71
72
73
# File 'lib/gitolite/ssh_key.rb', line 69

def filename
  file = @owner
  file += "@#{@location}" unless @location.empty?
  file += ".pub"
end

#to_file(path) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/gitolite/ssh_key.rb', line 61

def to_file(path)
  key_file = File.join(path, self.filename)
  File.open(key_file, "w") do |f|
    f.write(self.to_s)
  end
  key_file
end

#to_sObject



57
58
59
# File 'lib/gitolite/ssh_key.rb', line 57

def to_s
  [@type, @blob, @email].join(' ')
end