Class: GitWit::AuthorizedKeys::File

Inherits:
AuthorizedKeys::File
  • Object
show all
Defined in:
lib/git_wit/authorized_keys/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#original_locationObject

Returns the value of attribute original_location.



4
5
6
# File 'lib/git_wit/authorized_keys/file.rb', line 4

def original_location
  @original_location
end

Instance Method Details

#clear(&block) ⇒ Object



56
57
58
# File 'lib/git_wit/authorized_keys/file.rb', line 56

def clear(&block)
  modify "w", &block
end

#keysObject



6
7
8
9
10
11
12
13
14
# File 'lib/git_wit/authorized_keys/file.rb', line 6

def keys
  list = []
  modify "r" do |file|
    file.each do |line|
      list << Key.new(line.chomp)
    end
  end
  list
end

#modify(mode, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/git_wit/authorized_keys/file.rb', line 38

def modify(mode, &block)
  return super if owned? || self.original_location
  contents = %x(sudo -u "##{owner}" cat "#{location}") unless mode.include? "w"
  original_owner = owner
  self.original_location = location
  tmp = Tempfile.new "git_wit_authorized_keys"
  self.location = tmp.path
  tmp.write contents unless mode.include? "w"
  tmp.close
  super
  self.location = original_location
  if mode != "r"
    %x(cat "#{tmp.path}" | sudo -u "##{owner}" tee "#{location}" >/dev/null)
  end
  tmp.unlink
  self.original_location = nil
end

#owned?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/git_wit/authorized_keys/file.rb', line 26

def owned?
  owner == Process.uid
end

#owner(file = nil) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/git_wit/authorized_keys/file.rb', line 30

def owner(file = nil)
  file ||= location
  ::File.stat(file).uid
rescue Errno::EACCES, Errno::ENOENT
  parent = ::File.dirname file
  owner parent unless file == parent
end

#remove(key) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/git_wit/authorized_keys/file.rb', line 16

def remove(key)
  key = Key.new(key) if key.is_a?(String)
  cached_keys = keys
  modify 'w' do |file|
    cached_keys.each do |k|
      file.puts k unless key == k
    end
  end
end