Class: Leeloo::GpgPrivateLocalFileSystemKeystore

Inherits:
PrivateLocalFileSystemKeystore show all
Defined in:
lib/leeloo/keystore.rb

Instance Attribute Summary

Attributes inherited from PrivateLocalFileSystemKeystore

#path

Attributes inherited from Keystore

#name

Instance Method Summary collapse

Methods inherited from PrivateLocalFileSystemKeystore

#==, #find_secrets, #secrets

Methods inherited from Keystore

#==, #footprint, #secrets, #sync

Constructor Details

#initialize(name, path) ⇒ GpgPrivateLocalFileSystemKeystore

Returns a new instance of GpgPrivateLocalFileSystemKeystore.



126
127
128
129
130
# File 'lib/leeloo/keystore.rb', line 126

def initialize name, path
  super name, path
  FileUtils.mkdir_p "#{@path}/keys"
  populate_recipients
end

Instance Method Details

#add_key(email) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/leeloo/keystore.rb', line 149

def add_key email
  paths = []
  GPGME::Key.find(:public, email).each do |key| 
    key.export(:output => File.open("#{path}/keys/#{key.uids.first.email}", "w+"))
    paths << "#{path}/keys/#{key.uids.first.email}"
  end
  return paths
end

#footprint_of(name) ⇒ Object



175
176
177
178
179
# File 'lib/leeloo/keystore.rb', line 175

def footprint_of name
  footprint = super name
  footprint["sign"] = Base64.strict_encode64 GPGME::Crypto.new.sign(footprint["footprint"]).to_s
  footprint
end

#initObject



138
139
140
141
# File 'lib/leeloo/keystore.rb', line 138

def init
  super
  File.write("#{@path}/keys/do_not_remove_me", "do not remove me")
end

#keysObject



143
144
145
146
147
# File 'lib/leeloo/keystore.rb', line 143

def keys
  available = GPGME::Key.find(:public, nil, ).map { |key| key.email }
  actual = Dir.glob("#{@path}/keys/**").map { |path| path.split('/').last }
  available.map { |email| actual.include?(email) ? "#{email}::true" : "#{email}::false" }
end

#populate_recipientsObject



132
133
134
135
136
# File 'lib/leeloo/keystore.rb', line 132

def populate_recipients
  @recipients = []
  Dir.glob("#{path}/keys/*") { |key| @recipients << File.basename(key) }
  @recipients.each { |key| GPGME::Key.import(File.open("#{path}/keys/#{key}")) }
end

#remove_key(email) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/leeloo/keystore.rb', line 158

def remove_key email
  if File.exist?("#{path}/keys/#{email}")
    File.delete("#{path}/keys/#{email}")
    return "#{path}/keys/#{email}"
  end
  return nil
end

#secret_from_footprint(footprint) ⇒ Object



181
182
183
184
185
186
187
188
# File 'lib/leeloo/keystore.rb', line 181

def secret_from_footprint footprint
  data = GPGME::Crypto.new.verify(Base64.strict_decode64 footprint["sign"]) { |signature| signature.valid? }
  if data.read == footprint["footprint"]
    super footprint
  else
    raise "signature is not valid"
  end
end

#secret_from_name(name) ⇒ Object



171
172
173
# File 'lib/leeloo/keystore.rb', line 171

def secret_from_name name
  secret_of "#{path}/secrets/#{name}.gpg"
end

#secret_of(path) ⇒ Object



166
167
168
169
# File 'lib/leeloo/keystore.rb', line 166

def secret_of path
  name = path.gsub("#{@path}/secrets/", "").gsub(".gpg", "")
  GpgLocalFileSystemSecret.new path, name, @recipients
end