Class: Secrets::App::KeyChain

Inherits:
Object
  • Object
show all
Defined in:
lib/secrets/app/keychain.rb

Overview

This class forms and shells several commands that wrap Mac OS-X security command. They provide access to storing generic passwords in the KeyChain Access.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_name, opts = {}) ⇒ KeyChain

Returns a new instance of KeyChain.



35
36
37
38
39
# File 'lib/secrets/app/keychain.rb', line 35

def initialize(key_name, opts = {})
  self.key_name = key_name
  self.opts     = opts
  self.class.validate!
end

Class Attribute Details

.kindObject

Returns the value of attribute kind.



15
16
17
# File 'lib/secrets/app/keychain.rb', line 15

def kind
  @kind
end

.sub_sectionObject

Returns the value of attribute sub_section.



15
16
17
# File 'lib/secrets/app/keychain.rb', line 15

def sub_section
  @sub_section
end

.userObject

Returns the value of attribute user.



15
16
17
# File 'lib/secrets/app/keychain.rb', line 15

def user
  @user
end

Instance Attribute Details

#key_nameObject

Returns the value of attribute key_name.



33
34
35
# File 'lib/secrets/app/keychain.rb', line 33

def key_name
  @key_name
end

#optsObject

Returns the value of attribute opts.



33
34
35
# File 'lib/secrets/app/keychain.rb', line 33

def opts
  @opts
end

#stderr_disabledObject

Returns the value of attribute stderr_disabled.



33
34
35
# File 'lib/secrets/app/keychain.rb', line 33

def stderr_disabled
  @stderr_disabled
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



17
18
19
# File 'lib/secrets/app/keychain.rb', line 17

def configure
  yield self
end

.validate!Object

Raises:

  • (ArgumentError)


21
22
23
24
# File 'lib/secrets/app/keychain.rb', line 21

def validate!
  raise ArgumentError.new(
    'User is not defined. Either set $USER in environment, or directly on the class.') unless self.user
end

Instance Method Details

#add(password) ⇒ Object



41
42
43
# File 'lib/secrets/app/keychain.rb', line 41

def add(password)
  execute command(:add, "-w '#{password}' ")
end

#deleteObject



49
50
51
# File 'lib/secrets/app/keychain.rb', line 49

def delete
  execute command(:delete)
end

#execute(command) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/secrets/app/keychain.rb', line 53

def execute(command)
  command += ' 2>/dev/null' if stderr_disabled
  puts "> #{command.yellow.green}" if opts[:verbose]
  output = `#{command}`
  result = $?
  raise Secrets::Errors::ExternalCommandError.new("Command error: #{result}, command: #{command}") unless result.success?
  output.chomp
rescue Errno::ENOENT => e
  raise Secrets::Errors::ExternalCommandError.new("Command error: #{e.message}, command: #{command}")
end

#findObject



45
46
47
# File 'lib/secrets/app/keychain.rb', line 45

def find
  execute command(:find, ' -g -w ')
end

#stderr_offObject



64
65
66
# File 'lib/secrets/app/keychain.rb', line 64

def stderr_off
  self.stderr_disabled = true
end

#stderr_onObject



68
69
70
# File 'lib/secrets/app/keychain.rb', line 68

def stderr_on
  self.stderr_disabled = false
end