Class: Xcode::Keychain

Inherits:
Object
  • Object
show all
Includes:
TerminalOutput
Defined in:
lib/xcode/keychain.rb

Constant Summary collapse

TEMP_PASSWORD =
"build_keychain_password"

Constants included from TerminalOutput

TerminalOutput::LEVELS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TerminalOutput

#color_output=, #color_output?, #format_lhs, included, #log_level, log_level=, #print, #print_input, #print_output, #print_system, #print_task, #puts, terminal_supports_colors?

Constructor Details

#initialize(path) {|_self| ... } ⇒ Keychain

Open the keychain with the specified name. It is assumed that keychains reside in the ~/Library/Keychains directory

Parameters:

  • the (String)

    name of the keychain

Yields:

  • (_self)

Yield Parameters:



54
55
56
57
58
59
# File 'lib/xcode/keychain.rb', line 54

def initialize(path)
  @path = File.expand_path path
  @name = File.basename path
  
  yield(self) if block_given?
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



44
45
46
# File 'lib/xcode/keychain.rb', line 44

def name
  @name
end

#pathObject

Returns the value of attribute path.



44
45
46
# File 'lib/xcode/keychain.rb', line 44

def path
  @path
end

Class Method Details

.create(path, password) {|kc| ... } ⇒ Xcode::Keychain

Create a new keychain with the given name and password

Parameters:

  • the (String)

    name for the new keychain

  • the (String)

    password for the new keychain

Yields:

  • (kc)

Returns:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/xcode/keychain.rb', line 145

def self.create(path, password)
  cmd = Xcode::Shell::Command.new "security"
  cmd << "create-keychain"
  cmd << "-p #{password}"
  cmd << "\"#{path}\""
  cmd.execute
  
  cmd = Xcode::Shell::Command.new "security"
  cmd << "set-keychain-settings"
  cmd << "-u"
  cmd << "\"#{path}\""
  cmd.execute
  
  kc = Xcode::Keychain.new(path)
  yield(kc) if block_given?
  kc
end

.login {|kc| ... } ⇒ Xcode::Keychain

Opens the default login.keychain for current user

Yields:

  • (kc)

Returns:



203
204
205
206
207
# File 'lib/xcode/keychain.rb', line 203

def self.
  kc = Xcode::Keychain.new("~/Library/Keychains/login.keychain")
  yield(kc) if block_given?
  kc
end

.tempObject

Creates a keychain with the given name that lasts for the duration of the provided block.

The keychain is deleted even if the block throws an exception.

If no block is provided, the temporary keychain is returned and it is deleted on system exit



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/xcode/keychain.rb', line 180

def self.temp
  kc = Xcode::Keychain.create("/tmp/xcoder#{Time.now.to_i}", TEMP_PASSWORD)
  kc.unlock(TEMP_PASSWORD)
  
  if !block_given?
    at_exit do
      kc.delete
    end
    kc
  else
    begin
      yield(kc)
    ensure
      kc.delete
    end
  end
end

Instance Method Details

#deleteObject

Remove the keychain from the filesystem

FIXME: dangerous



168
169
170
171
172
# File 'lib/xcode/keychain.rb', line 168

def delete
  cmd = Xcode::Shell::Command.new "security"
  cmd << "delete-keychain \"#{@path}\""
  cmd.execute
end

#identitiesArray<String>

Returns a list of identities in the keychain.

Returns:



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/xcode/keychain.rb', line 102

def identities
  names = []
  cmd = Xcode::Shell::Command.new "security"
  cmd << "find-certificate"
  cmd << "-a"
  cmd << "\"#{@path}\""
  cmd.show_output = false
  cmd.execute.join("").scan /\s+"labl"<blob>="([^"]+)"/ do |m|
    names << m[0]
  end
  names
end

#import(cert, password) ⇒ Object

Import the .p12 certificate file into the keychain using the provided password

Parameters:

  • the (String)

    path to the .p12 certificate file

  • the (String)

    password to open the certificate file



88
89
90
91
92
93
94
95
# File 'lib/xcode/keychain.rb', line 88

def import(cert, password)
  cmd = Xcode::Shell::Command.new "security"
  cmd << "import '#{cert}'"
  cmd << "-k \"#{@path}\""
  cmd << "-P #{password}"
  cmd << "-T /usr/bin/codesign"
  cmd.execute
end

#in_search_path(&block) ⇒ Object

Installs this keychain in the head of teh search path and restores the original on completion of the block

Parameters:

  • the

    block to be invoked with the modified search path



71
72
73
74
75
76
77
78
79
80
# File 'lib/xcode/keychain.rb', line 71

def in_search_path(&block)
  keychains = Keychains.search_path
  begin 
    Keychains.search_path = [self] + keychains
    yield
  ensure
    Keychains.search_path = keychains
    # print_task 'keychain', "Restored search path"
  end
end

#lockObject

Secure the keychain



118
119
120
121
122
123
# File 'lib/xcode/keychain.rb', line 118

def lock
  cmd = Xcode::Shell::Command.new "security"
  cmd << "lock-keychain"
  cmd << "\"#{@path}\""
  cmd.execute
end

#to_sObject



61
62
63
# File 'lib/xcode/keychain.rb', line 61

def to_s
  "Keychain(#{@name})"
end

#unlock(password) ⇒ Object

Unlock the keychain using the provided password

Parameters:

  • the (String)

    password to open the keychain



130
131
132
133
134
135
136
# File 'lib/xcode/keychain.rb', line 130

def unlock(password)
  cmd = Xcode::Shell::Command.new "security"
  cmd << "unlock-keychain"
  cmd << "-p #{password}"
  cmd << "\"#{@path}\""
  cmd.execute
end