spare_keys 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/spare_keys.rb +113 -109
  3. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee6d263bb19251d17f0bc7798c5654db62373f56
4
- data.tar.gz: e744c90dad10884f4ff680ed7c2e090ff1b21fb4
3
+ metadata.gz: f5afd87fa0f30e31f7b04ca3784fed50c0c6f203
4
+ data.tar.gz: 276c8fc77052d05dc7c1ab39a76fe6677f98579e
5
5
  SHA512:
6
- metadata.gz: ec1a1dbc6e74400ca1340175133be63d53f495abef63256b44118c287568df5a69993780186fdd1536c665bdf3ffdac30211fde17e1dede81aa89102b052a8b0
7
- data.tar.gz: ccfe4b946b6e5409e1a9f91d5967fc5d65f2c69107e590dea2707d9c0883553abf72cd725644ac71c277216b664e6d963053d736184f304f24dd8f6a9a85d928
6
+ metadata.gz: f2c9c8de5ad759c5d9b2274d0fab1b7d2b09233c0c8009cff560d74c6285027b8b8b9889aa17a769e399e0fe52e3e15d4ec884d6bf354465aae0311af6bdb9e5
7
+ data.tar.gz: e3e5b6f81cc03542cea511477ae37dd5f797e665ccbd36c4c0e4e7ed739ec87df32002f6d28f40fa4260f0b08fa31a62af351474debf62988986a42c8c8d3c7c
@@ -1,110 +1,114 @@
1
-
2
-
3
- # Temporarily reconfigures the active keychain
4
- class SpareKeys
5
-
6
- # Temporarily adds the specified keychain to the top of the search list, reverting it after the block is invoked.
7
- #
8
- # If no block is supplied, reverting the state becomes the responsibility of the caller.
9
- # Params:
10
- # +keychain_path+:: path to keychain to switch to
11
- # +clear_list+:: when true, the search list will be initially cleared to prevent fallback to a different keychain
12
- # +type+:: if specified, replaces default/login keychain ("default", "login", nil)
13
- # +domain+:: if specified, performs keychain operations using the specified domain
14
- def self.use_keychain(keychain_path, clear_list = false, type = nil, domain = nil)
15
- domain_flag = "-d #{domain}" if domain
16
-
17
- keychain_path = expand_keychain_path(keychain_path)
18
-
19
- original_list = `security list-keychains #{domain_flag} | xargs`
20
- original_keychain = `security #{type}-keychain #{domain_flag} | xargs` if type
21
-
22
- `security #{type}-keychain #{domain_flag} -s #{keychain_path}` if type
23
-
24
- list_tail = original_list unless clear_list
25
- `security list-keychains #{domain_flag} -s #{keychain_path} #{list_tail}`
26
-
27
- if block_given?
28
- begin
29
- yield if block_given?
30
- ensure
31
- original_keychain = `security #{type}-keychain #{domain_flag} -s #{original_keychain}` if type
32
-
33
- unless clear_list
34
- # Grab the keychain list as it looks right now in case
35
- # another process has changed it
36
- current_list = `security list-keychains #{domain_flag}`
37
- current_list_as_array = current_list.scan(/"[^"]*"/).map { |item| item.gsub(/^"|"$/, "")}
38
- # Remove the supplied keychain
39
- original_list = (current_list_as_array.reject { |item| item == keychain_path }).join(" ")
40
- end
41
-
42
- `security list-keychains #{domain_flag} -s #{original_list}`
43
- end
44
- end
45
- end
46
-
47
- # Creates a secure temporary keychain and adds it to the top of the
48
- # search list, reverting the list and deleting the keychain after the block is invoked.
49
- #
50
- # If no block is supplied, reverting the state becomes the responsibility of the caller.
51
- # Params:
52
- # +clear_list+:: when true, the search list will be initially cleared to prevent fallback to a different keychain
53
- # +type+:: if specified, replaces default/login keychain ("default", "login", nil)
54
- # +domain+:: if specified, performs keychain operations using the specified domain
55
- def self.temp_keychain(clear_list = false, type = nil, domain = nil) # :yields: keychain_path
56
- require 'tempfile'
57
- require 'securerandom'
58
-
59
- password = SecureRandom.hex
60
-
61
- extension = keychain_extension()
62
- temp_keychain = Dir::Tmpname.make_tmpname(['spare-keys-', extension], nil)
63
-
64
- `security create-keychain -p "#{password}" #{temp_keychain}`
65
- `security set-keychain-settings #{temp_keychain}`
66
- `security unlock-keychain -p "#{password}" #{temp_keychain}`
67
-
68
- if block_given?
69
- begin
70
- use_keychain(temp_keychain, clear_list, type) {
71
- yield temp_keychain, password
72
- }
73
- ensure
74
- `security delete-keychain #{temp_keychain}`
75
- end
76
- else
77
- use_keychain(temp_keychain, clear_list, type)
78
- end
79
- end
80
-
81
- private
82
-
83
- def self.keychain_extension()
84
- return is_sierra() ? '.keychain-db' : '.keychain'
85
- end
86
-
87
- def self.is_sierra()
88
-
89
- osVersion = `sysctl -n kern.osrelease`
90
-
91
- majorOsVersion = Integer(osVersion.split('.')[0])
92
-
93
- return majorOsVersion >= 16 # Sierra
94
-
95
- end
96
-
97
- def self.expand_keychain_path(path)
98
-
99
- if (File.basename(path) == path)
100
- default_keychain_path = File.expand_path("~/Library/Keychains")
101
-
102
- path = File.join(default_keychain_path, path)
103
- else
104
- path = File.expand_path(path)
105
- end
106
-
107
- return path
108
- end
109
-
1
+
2
+
3
+ # Temporarily reconfigures the active keychain
4
+ class SpareKeys
5
+
6
+ # Temporarily adds the specified keychain to the top of the search list, reverting it after the block is invoked.
7
+ #
8
+ # If no block is supplied, reverting the state becomes the responsibility of the caller.
9
+ # Params:
10
+ # +keychain_path+:: path to keychain to switch to
11
+ # +clear_list+:: when true, the search list will be initially cleared to prevent fallback to a different keychain
12
+ # +type+:: if specified, replaces default/login keychain ("default", "login", nil)
13
+ # +domain+:: if specified, performs keychain operations using the specified domain
14
+ def self.use_keychain(keychain_path, clear_list = false, type = nil, domain = nil)
15
+ domain_flag = "-d #{domain}" if domain
16
+
17
+ keychain_path = expand_keychain_path(keychain_path)
18
+
19
+ original_list = `security list-keychains #{domain_flag} | xargs`
20
+ original_keychain = `security #{type}-keychain #{domain_flag} | xargs` if type
21
+
22
+ `security #{type}-keychain #{domain_flag} -s #{keychain_path}` if type
23
+
24
+ list_tail = original_list unless clear_list
25
+ `security list-keychains #{domain_flag} -s #{keychain_path} #{list_tail}`
26
+
27
+ if block_given?
28
+ begin
29
+ yield if block_given?
30
+ ensure
31
+ original_keychain = `security #{type}-keychain #{domain_flag} -s #{original_keychain}` if type
32
+
33
+ unless clear_list
34
+ # Grab the keychain list as it looks right now in case
35
+ # another process has changed it
36
+ current_list = `security list-keychains #{domain_flag}`
37
+ current_list_as_array = current_list.scan(/"[^"]*"/).map { |item| item.gsub(/^"|"$/, "")}
38
+ # Remove the supplied keychain
39
+ original_list = (current_list_as_array.reject { |item| item == keychain_path }).join(" ")
40
+ end
41
+
42
+ `security list-keychains #{domain_flag} -s #{original_list}`
43
+ end
44
+ end
45
+ end
46
+
47
+ # Creates a secure temporary keychain and adds it to the top of the
48
+ # search list, reverting the list and deleting the keychain after the block is invoked.
49
+ #
50
+ # If no block is supplied, reverting the state becomes the responsibility of the caller.
51
+ # Params:
52
+ # +clear_list+:: when true, the search list will be initially cleared to prevent fallback to a different keychain
53
+ # +type+:: if specified, replaces default/login keychain ("default", "login", nil)
54
+ # +domain+:: if specified, performs keychain operations using the specified domain
55
+ def self.temp_keychain(clear_list = false, type = nil, domain = nil) # :yields: keychain_path
56
+ require 'tempfile'
57
+ require 'securerandom'
58
+
59
+ password = SecureRandom.hex
60
+ temp_keychain = temporary_keychain_name('spare-keys')
61
+
62
+ `security create-keychain -p "#{password}" #{temp_keychain}`
63
+ `security set-keychain-settings #{temp_keychain}`
64
+ `security unlock-keychain -p "#{password}" #{temp_keychain}`
65
+
66
+ if block_given?
67
+ begin
68
+ use_keychain(temp_keychain, clear_list, type) {
69
+ yield temp_keychain, password
70
+ }
71
+ ensure
72
+ `security delete-keychain #{temp_keychain}`
73
+ end
74
+ else
75
+ use_keychain(temp_keychain, clear_list, type)
76
+ end
77
+ end
78
+
79
+ private
80
+
81
+ def self.keychain_extension()
82
+ return is_sierra() ? '.keychain-db' : '.keychain'
83
+ end
84
+
85
+ def self.is_sierra()
86
+
87
+ osVersion = `sysctl -n kern.osrelease`
88
+
89
+ majorOsVersion = Integer(osVersion.split('.')[0])
90
+
91
+ return majorOsVersion >= 16 # Sierra
92
+
93
+ end
94
+
95
+ def self.expand_keychain_path(path)
96
+
97
+ if (File.basename(path) == path)
98
+ default_keychain_path = File.expand_path("~/Library/Keychains")
99
+
100
+ path = File.join(default_keychain_path, path)
101
+ else
102
+ path = File.expand_path(path)
103
+ end
104
+
105
+ return path
106
+ end
107
+
108
+ def self.temporary_keychain_name(prefix)
109
+ t = Time.now.strftime("%Y%m%d")
110
+ extension = keychain_extension()
111
+ "#{prefix}-#{t}-#{$$}-#{rand(0x100000000).to_s(36)}#{extension}"
112
+ end
113
+
110
114
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spare_keys
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Szalay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-16 00:00:00.000000000 Z
11
+ date: 2019-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -85,9 +85,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
85
  version: '0'
86
86
  requirements: []
87
87
  rubyforge_project:
88
- rubygems_version: 2.6.7
88
+ rubygems_version: 2.5.1
89
89
  signing_key:
90
90
  specification_version: 4
91
91
  summary: Temporarily keychain switcher
92
92
  test_files: []
93
- has_rdoc: