bundler 4.0.24 → 4.1.0.beta1

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 (190) hide show
  1. checksums.yaml +4 -4
  2. data/{lib/bundler/vendor/connection_pool/LICENSE → MIT.txt} +8 -6
  3. data/bundler.gemspec +23 -6
  4. data/lib/bundler/build_metadata.rb +2 -2
  5. data/lib/bundler/checksum.rb +40 -25
  6. data/lib/bundler/ci_detector.rb +1 -1
  7. data/lib/bundler/cli/add.rb +3 -4
  8. data/lib/bundler/cli/cache.rb +4 -0
  9. data/lib/bundler/cli/clean.rb +5 -0
  10. data/lib/bundler/cli/common.rb +37 -1
  11. data/lib/bundler/cli/config.rb +4 -2
  12. data/lib/bundler/cli/exec.rb +1 -1
  13. data/lib/bundler/cli/gem.rb +0 -17
  14. data/lib/bundler/cli/install.rb +4 -5
  15. data/lib/bundler/cli/lock.rb +5 -21
  16. data/lib/bundler/cli/outdated.rb +8 -3
  17. data/lib/bundler/cli/platform.rb +1 -15
  18. data/lib/bundler/cli/pristine.rb +2 -1
  19. data/lib/bundler/cli/update.rb +21 -12
  20. data/lib/bundler/cli.rb +19 -28
  21. data/lib/bundler/definition.rb +62 -27
  22. data/lib/bundler/dependency.rb +4 -0
  23. data/lib/bundler/dsl.rb +79 -17
  24. data/lib/bundler/endpoint_specification.rb +51 -14
  25. data/lib/bundler/env.rb +7 -24
  26. data/lib/bundler/errors.rb +34 -5
  27. data/lib/bundler/fetcher/compact_index.rb +14 -5
  28. data/lib/bundler/fetcher/connection_pools.rb +150 -0
  29. data/lib/bundler/fetcher/downloader.rb +33 -24
  30. data/lib/bundler/fetcher.rb +9 -51
  31. data/lib/bundler/injector.rb +3 -3
  32. data/lib/bundler/inline.rb +44 -4
  33. data/lib/bundler/installer/parallel_installer.rb +25 -30
  34. data/lib/bundler/installer.rb +20 -10
  35. data/lib/bundler/lazy_specification.rb +44 -17
  36. data/lib/bundler/lockfile_generator.rb +24 -2
  37. data/lib/bundler/lockfile_parser.rb +43 -5
  38. data/lib/bundler/man/bundle-add.1 +7 -4
  39. data/lib/bundler/man/bundle-add.1.ronn +8 -4
  40. data/lib/bundler/man/bundle-binstubs.1 +1 -1
  41. data/lib/bundler/man/bundle-cache.1 +2 -2
  42. data/lib/bundler/man/bundle-cache.1.ronn +2 -2
  43. data/lib/bundler/man/bundle-check.1 +1 -1
  44. data/lib/bundler/man/bundle-clean.1 +1 -1
  45. data/lib/bundler/man/bundle-config.1 +38 -6
  46. data/lib/bundler/man/bundle-config.1.ronn +143 -10
  47. data/lib/bundler/man/bundle-console.1 +1 -1
  48. data/lib/bundler/man/bundle-doctor.1 +1 -1
  49. data/lib/bundler/man/bundle-env.1 +1 -1
  50. data/lib/bundler/man/bundle-exec.1 +1 -1
  51. data/lib/bundler/man/bundle-fund.1 +1 -1
  52. data/lib/bundler/man/bundle-gem.1 +1 -1
  53. data/lib/bundler/man/bundle-help.1 +1 -1
  54. data/lib/bundler/man/bundle-info.1 +1 -1
  55. data/lib/bundler/man/bundle-init.1 +1 -1
  56. data/lib/bundler/man/bundle-install.1 +2 -2
  57. data/lib/bundler/man/bundle-install.1.ronn +4 -4
  58. data/lib/bundler/man/bundle-issue.1 +1 -1
  59. data/lib/bundler/man/bundle-licenses.1 +1 -1
  60. data/lib/bundler/man/bundle-list.1 +1 -1
  61. data/lib/bundler/man/bundle-lock.1 +4 -4
  62. data/lib/bundler/man/bundle-lock.1.ronn +6 -6
  63. data/lib/bundler/man/bundle-open.1 +1 -1
  64. data/lib/bundler/man/bundle-outdated.1 +2 -2
  65. data/lib/bundler/man/bundle-outdated.1.ronn +1 -1
  66. data/lib/bundler/man/bundle-platform.1 +1 -1
  67. data/lib/bundler/man/bundle-plugin.1 +1 -1
  68. data/lib/bundler/man/bundle-pristine.1 +1 -1
  69. data/lib/bundler/man/bundle-remove.1 +1 -1
  70. data/lib/bundler/man/bundle-show.1 +1 -1
  71. data/lib/bundler/man/bundle-update.1 +5 -5
  72. data/lib/bundler/man/bundle-update.1.ronn +5 -7
  73. data/lib/bundler/man/bundle-version.1 +1 -1
  74. data/lib/bundler/man/bundle.1 +1 -1
  75. data/lib/bundler/man/gemfile.5 +45 -3
  76. data/lib/bundler/man/gemfile.5.ronn +53 -6
  77. data/lib/bundler/match_metadata.rb +20 -0
  78. data/lib/bundler/match_platform.rb +25 -2
  79. data/lib/bundler/match_remote_metadata.rb +21 -6
  80. data/lib/bundler/materialization.rb +2 -1
  81. data/lib/bundler/override.rb +69 -0
  82. data/lib/bundler/plugin/dsl.rb +3 -5
  83. data/lib/bundler/plugin/events.rb +55 -13
  84. data/lib/bundler/plugin/index.rb +87 -11
  85. data/lib/bundler/plugin/installer/path.rb +1 -1
  86. data/lib/bundler/plugin/installer.rb +13 -5
  87. data/lib/bundler/plugin.rb +58 -30
  88. data/lib/bundler/remote_specification.rb +6 -3
  89. data/lib/bundler/resolver/base.rb +2 -1
  90. data/lib/bundler/resolver/incompatibility.rb +1 -1
  91. data/lib/bundler/resolver.rb +73 -25
  92. data/lib/bundler/ruby_dsl.rb +1 -2
  93. data/lib/bundler/ruby_version.rb +0 -2
  94. data/lib/bundler/rubygems_ext.rb +75 -16
  95. data/lib/bundler/rubygems_gem_installer.rb +41 -2
  96. data/lib/bundler/rubygems_integration.rb +7 -2
  97. data/lib/bundler/runtime.rb +69 -2
  98. data/lib/bundler/self_manager.rb +9 -15
  99. data/lib/bundler/settings.rb +373 -30
  100. data/lib/bundler/shared_helpers.rb +2 -1
  101. data/lib/bundler/source/git/git_proxy.rb +2 -10
  102. data/lib/bundler/source/git.rb +15 -6
  103. data/lib/bundler/source/path/installer.rb +1 -1
  104. data/lib/bundler/source/rubygems/remote.rb +10 -5
  105. data/lib/bundler/source/rubygems.rb +49 -13
  106. data/lib/bundler/spec_set.rb +16 -4
  107. data/lib/bundler/stub_specification.rb +2 -1
  108. data/lib/bundler/templates/newgem/Gemfile.tt +4 -4
  109. data/lib/bundler/templates/newgem/ext/newgem/Cargo.toml.tt +1 -1
  110. data/lib/bundler/templates/newgem/newgem.gemspec.tt +3 -3
  111. data/lib/bundler/templates/newgem/spec/newgem_spec.rb.tt +1 -1
  112. data/lib/bundler/templates/newgem/test/minitest/test_newgem.rb.tt +1 -1
  113. data/lib/bundler/ui/shell.rb +0 -4
  114. data/lib/bundler/ui/silent.rb +0 -4
  115. data/lib/bundler/vendor/fileutils/lib/fileutils.rb +29 -54
  116. data/lib/bundler/vendored_pub_grub.rb +5 -2
  117. data/lib/bundler/vendored_securerandom.rb +11 -8
  118. data/lib/bundler/vendored_uri.rb +14 -14
  119. data/lib/bundler/version.rb +1 -1
  120. data/lib/bundler/worker.rb +3 -2
  121. data/lib/bundler.rb +4 -2
  122. data/lib/rubygems/compact_index_client/cache.rb +122 -0
  123. data/lib/rubygems/compact_index_client/cache_file.rb +147 -0
  124. data/lib/rubygems/compact_index_client/http_fetcher.rb +88 -0
  125. data/lib/rubygems/compact_index_client/parser.rb +90 -0
  126. data/lib/rubygems/compact_index_client/updater.rb +113 -0
  127. data/lib/rubygems/compact_index_client.rb +111 -0
  128. data/lib/rubygems/cooldown_settings.rb +57 -0
  129. data/lib/rubygems/credential_store/native/linux.rb +97 -0
  130. data/lib/rubygems/credential_store/native/macos.rb +88 -0
  131. data/lib/rubygems/credential_store/native/windows.rb +130 -0
  132. data/lib/rubygems/credential_store.rb +332 -0
  133. data/lib/{bundler → rubygems}/vendor/pub_grub/lib/pub_grub/assignment.rb +1 -1
  134. data/lib/{bundler → rubygems}/vendor/pub_grub/lib/pub_grub/basic_package_source.rb +7 -7
  135. data/lib/{bundler → rubygems}/vendor/pub_grub/lib/pub_grub/failure_writer.rb +1 -1
  136. data/lib/{bundler → rubygems}/vendor/pub_grub/lib/pub_grub/incompatibility.rb +4 -4
  137. data/lib/{bundler → rubygems}/vendor/pub_grub/lib/pub_grub/package.rb +1 -1
  138. data/lib/{bundler → rubygems}/vendor/pub_grub/lib/pub_grub/partial_solution.rb +1 -1
  139. data/lib/{bundler → rubygems}/vendor/pub_grub/lib/pub_grub/rubygems.rb +3 -3
  140. data/lib/{bundler → rubygems}/vendor/pub_grub/lib/pub_grub/solve_failure.rb +1 -1
  141. data/lib/{bundler → rubygems}/vendor/pub_grub/lib/pub_grub/static_package_source.rb +3 -3
  142. data/lib/{bundler → rubygems}/vendor/pub_grub/lib/pub_grub/strategy.rb +2 -2
  143. data/lib/{bundler → rubygems}/vendor/pub_grub/lib/pub_grub/term.rb +1 -1
  144. data/lib/{bundler → rubygems}/vendor/pub_grub/lib/pub_grub/version.rb +1 -1
  145. data/lib/{bundler → rubygems}/vendor/pub_grub/lib/pub_grub/version_constraint.rb +3 -3
  146. data/lib/{bundler → rubygems}/vendor/pub_grub/lib/pub_grub/version_range.rb +1 -1
  147. data/lib/{bundler → rubygems}/vendor/pub_grub/lib/pub_grub/version_solver.rb +2 -2
  148. data/lib/{bundler → rubygems}/vendor/pub_grub/lib/pub_grub/version_union.rb +1 -1
  149. data/lib/rubygems/vendor/pub_grub/lib/pub_grub.rb +57 -0
  150. data/lib/{bundler → rubygems}/vendor/securerandom/lib/securerandom.rb +10 -6
  151. data/lib/{bundler → rubygems}/vendor/uri/lib/uri/common.rb +120 -120
  152. data/lib/{bundler → rubygems}/vendor/uri/lib/uri/file.rb +13 -13
  153. data/lib/{bundler → rubygems}/vendor/uri/lib/uri/ftp.rb +24 -24
  154. data/lib/{bundler → rubygems}/vendor/uri/lib/uri/generic.rb +135 -147
  155. data/lib/{bundler → rubygems}/vendor/uri/lib/uri/http.rb +18 -18
  156. data/lib/{bundler → rubygems}/vendor/uri/lib/uri/https.rb +4 -4
  157. data/lib/{bundler → rubygems}/vendor/uri/lib/uri/ldap.rb +14 -14
  158. data/lib/{bundler → rubygems}/vendor/uri/lib/uri/ldaps.rb +4 -4
  159. data/lib/{bundler → rubygems}/vendor/uri/lib/uri/mailto.rb +21 -21
  160. data/lib/{bundler → rubygems}/vendor/uri/lib/uri/rfc2396_parser.rb +39 -39
  161. data/lib/{bundler → rubygems}/vendor/uri/lib/uri/rfc3986_parser.rb +14 -14
  162. data/lib/{bundler → rubygems}/vendor/uri/lib/uri/version.rb +2 -2
  163. data/lib/{bundler → rubygems}/vendor/uri/lib/uri/ws.rb +12 -12
  164. data/lib/{bundler → rubygems}/vendor/uri/lib/uri/wss.rb +4 -4
  165. data/lib/{bundler → rubygems}/vendor/uri/lib/uri.rb +35 -31
  166. data/lib/rubygems/yaml_serializer.rb +880 -0
  167. metadata +77 -68
  168. data/CHANGELOG.md +0 -5830
  169. data/LICENSE.md +0 -22
  170. data/README.md +0 -58
  171. data/lib/bundler/compact_index_client/cache.rb +0 -107
  172. data/lib/bundler/compact_index_client/cache_file.rb +0 -148
  173. data/lib/bundler/compact_index_client/parser.rb +0 -87
  174. data/lib/bundler/compact_index_client/updater.rb +0 -105
  175. data/lib/bundler/compact_index_client.rb +0 -92
  176. data/lib/bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb +0 -237
  177. data/lib/bundler/vendor/connection_pool/lib/connection_pool/version.rb +0 -3
  178. data/lib/bundler/vendor/connection_pool/lib/connection_pool/wrapper.rb +0 -56
  179. data/lib/bundler/vendor/connection_pool/lib/connection_pool.rb +0 -233
  180. data/lib/bundler/vendor/net-http-persistent/README.rdoc +0 -82
  181. data/lib/bundler/vendor/net-http-persistent/lib/net/http/persistent/connection.rb +0 -41
  182. data/lib/bundler/vendor/net-http-persistent/lib/net/http/persistent/pool.rb +0 -65
  183. data/lib/bundler/vendor/net-http-persistent/lib/net/http/persistent/timed_stack_multi.rb +0 -89
  184. data/lib/bundler/vendor/net-http-persistent/lib/net/http/persistent.rb +0 -1159
  185. data/lib/bundler/vendor/pub_grub/lib/pub_grub.rb +0 -31
  186. data/lib/bundler/vendored_persistent.rb +0 -11
  187. data/lib/bundler/yaml_serializer.rb +0 -98
  188. /data/lib/{bundler → rubygems}/vendor/pub_grub/LICENSE.txt +0 -0
  189. /data/lib/{bundler → rubygems}/vendor/securerandom/COPYING +0 -0
  190. /data/lib/{bundler → rubygems}/vendor/uri/COPYING +0 -0
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Skip reloading when an identical copy (e.g. the one shipped inside the Bundler
4
+ # gem) was already required from a different path, to avoid redefinition warnings.
5
+ return if defined?(Gem::CooldownSettings)
6
+
7
+ ##
8
+ # The rules RubyGems and Bundler share for reading a configured cooldown
9
+ # period. Bundler ships a copy of this file, so it deliberately has no
10
+ # requires: none of the rest of RubyGems is guaranteed to be around it.
11
+
12
+ module Gem::CooldownSettings
13
+ ##
14
+ # +value+ read as a number of days, or nil when it is absent or cannot be
15
+ # read as a non-negative integer. A gemrc entry and a Bundler config entry
16
+ # are both arbitrary YAML, so either can be any type at all.
17
+
18
+ def self.days(value)
19
+ return if value.nil?
20
+
21
+ # Base 10 explicitly: a leading zero is how a user writes a small number
22
+ # of days, not a request for octal.
23
+ days = Integer(value.to_s, 10, exception: false)
24
+
25
+ days if days && !days.negative?
26
+ end
27
+
28
+ ##
29
+ # True when +value+ is configured but cannot be read as a number of days.
30
+
31
+ def self.invalid?(value)
32
+ !value.nil? && days(value).nil?
33
+ end
34
+
35
+ ##
36
+ # The cooldown that applies when RubyGems and Bundler are configured
37
+ # separately: the longest of +values+, so a cooldown configured for only
38
+ # one of the two tools protects both. Returns nil when none of them is
39
+ # usable.
40
+ #
41
+ # A configured 0 takes part like any other value rather than switching the
42
+ # cooldown off, which leaves <tt>--cooldown 0</tt> as the way to bypass a
43
+ # cooldown that the other tool configures.
44
+
45
+ def self.combine(*values)
46
+ values.filter_map {|value| days(value) }.max
47
+ end
48
+
49
+ # +source+ names where the value came from, since RubyGems and Bundler now
50
+ # each read the other's setting and the same complaint from either of them
51
+ # is otherwise impossible to trace back to a file.
52
+
53
+ def self.invalid_message(value, source) # :nodoc:
54
+ "Invalid cooldown value #{value.inspect} in #{source}, so it is ignored. " \
55
+ "Expected a non-negative integer number of days."
56
+ end
57
+ end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+
5
+ class Gem::CredentialStore; end unless defined?(Gem::CredentialStore)
6
+
7
+ ##
8
+ # Stores credentials in the Secret Service API (GNOME Keyring, KWallet,
9
+ # ...) via the +secret-tool+ command line tool from libsecret.
10
+
11
+ class Gem::CredentialStore::LinuxBackend
12
+ # secret-tool prints an item's attributes to stderr, one per line.
13
+ ACCOUNT_ATTRIBUTE = /^attribute\.account = (.*)$/
14
+ def self.available?
15
+ return @available if defined?(@available)
16
+
17
+ @available = ENV["PATH"].to_s.split(File::PATH_SEPARATOR).any? do |dir|
18
+ File.executable?(File.join(dir, "secret-tool"))
19
+ end
20
+ end
21
+
22
+ ##
23
+ # Clears the memoized #available? result. Intended for tests only.
24
+
25
+ def self.reset!
26
+ remove_instance_variable(:@available) if defined?(@available)
27
+ end
28
+
29
+ def self.get(service, account)
30
+ out, err, status = Open3.capture3(
31
+ "secret-tool", "lookup", "service", service, "account", account
32
+ )
33
+ # secret-tool exits 1 with nothing on stderr when the entry is simply
34
+ # absent. Anything else is a real failure.
35
+ unless status.success?
36
+ return nil if status.exitstatus == 1 && err.to_s.strip.empty?
37
+
38
+ raise "secret-tool exited with #{status.exitstatus}: #{err.strip}"
39
+ end
40
+
41
+ secret = out.chomp
42
+ secret.empty? ? nil : secret
43
+ end
44
+
45
+ def self.set(service, account, secret)
46
+ _out, status = Open3.capture2(
47
+ "secret-tool", "store", "--label=RubyGems", "service", service, "account", account,
48
+ stdin_data: secret
49
+ )
50
+ status.success?
51
+ end
52
+
53
+ # secret-tool writes attributes to stderr and secrets to stdout, so accounts
54
+ # are read from stderr. Discarding stdout also keeps a secret containing a
55
+ # newline from being mistaken for an attribute line.
56
+ def self.list(service)
57
+ _out, err, status = Open3.capture3(
58
+ "secret-tool", "search", "--all", "service", service
59
+ )
60
+ return [] unless status.success?
61
+
62
+ err.scan(ACCOUNT_ATTRIBUTE).flatten.uniq
63
+ end
64
+
65
+ def self.delete(service, account)
66
+ _out, err, status = Open3.capture3(
67
+ "secret-tool", "clear", "service", service, "account", account
68
+ )
69
+ return cleared?(service, account) if status.success?
70
+
71
+ # secret-tool clear exits 1 with no stderr when nothing matched.
72
+ status.exitstatus == 1 && err.to_s.strip.empty?
73
+ end
74
+
75
+ def self.delete_all(service)
76
+ _out, err, status = Open3.capture3(
77
+ "secret-tool", "clear", "service", service
78
+ )
79
+ return cleared?(service) if status.success?
80
+
81
+ status.exitstatus == 1 && err.to_s.strip.empty?
82
+ end
83
+
84
+ # libsecret clears only unlocked items and reports no error for the ones it
85
+ # skipped, so a locked keyring answers a clear with success while keeping
86
+ # every secret. search exits zero either way, so its output is the answer.
87
+ def self.cleared?(service, account = nil)
88
+ _out, err, status = Open3.capture3(
89
+ "secret-tool", "search", "--all", "service", service
90
+ )
91
+ return false unless status.success?
92
+
93
+ remaining = err.scan(ACCOUNT_ATTRIBUTE).flatten
94
+ account ? !remaining.include?(account) : remaining.empty?
95
+ end
96
+ private_class_method :cleared?
97
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+
5
+ class Gem::CredentialStore; end unless defined?(Gem::CredentialStore)
6
+
7
+ ##
8
+ # Stores credentials in the macOS Keychain via the +security+ command line
9
+ # tool. +security+ has no way to read a password from stdin as raw bytes
10
+ # for +add-generic-password+, so #set uses +security -i+ (batch/interactive
11
+ # mode, one tokenized command per stdin line) to keep the secret off argv
12
+ # and out of +ps+ output.
13
+ #
14
+ # A newline would start a second command in the +security -i+ batch, and
15
+ # +security find-generic-password -w+ prints any non-printable byte back as a
16
+ # hex string rather than the original value, so a non-ASCII secret would
17
+ # round-trip corrupted. Those are the limits Gem::CredentialStore#set enforces
18
+ # for every backend, so the caller falls back to file storage rather than
19
+ # storing something that cannot be read back.
20
+
21
+ class Gem::CredentialStore::MacOSBackend
22
+ NOT_FOUND_STATUS = 44
23
+
24
+ def self.get(service, account)
25
+ out, err, status = Open3.capture3(
26
+ "security", "find-generic-password", "-a", account, "-s", service, "-w"
27
+ )
28
+ # A locked keychain and an absent entry both yield no secret, but only the
29
+ # second one is ordinary.
30
+ unless status.success?
31
+ return nil if status.exitstatus == NOT_FOUND_STATUS
32
+
33
+ raise "security exited with #{status.exitstatus}: #{err.strip}"
34
+ end
35
+
36
+ secret = out.chomp
37
+ secret.empty? ? nil : secret
38
+ end
39
+
40
+ def self.set(service, account, secret)
41
+ command = "add-generic-password -U -a #{quote(account)} -s #{quote(service)} -w #{quote(secret)}\n"
42
+ _out, err, status = Open3.capture3("security", "-i", stdin_data: command)
43
+ return true if status.success?
44
+
45
+ # Raise rather than return false so the reason reaches the user. The
46
+ # wrapper turns it back into false after reporting it.
47
+ raise "security exited with #{status.exitstatus}: #{err.strip}"
48
+ end
49
+
50
+ # security has no "list by service" subcommand, so this reads the dump, which
51
+ # never reports the secrets.
52
+ def self.list(service)
53
+ out, status = Open3.capture2("security", "dump-keychain", err: File::NULL)
54
+ return [] unless status.success?
55
+
56
+ out.split(/^keychain: /).filter_map do |entry|
57
+ next unless entry[/^\s*"svce"<blob>="(.*)"$/, 1] == service
58
+
59
+ entry[/^\s*"acct"<blob>="(.*)"$/, 1]
60
+ end.uniq
61
+ end
62
+
63
+ def self.delete(service, account)
64
+ _out, status = Open3.capture2(
65
+ "security", "delete-generic-password", "-a", account, "-s", service,
66
+ err: File::NULL
67
+ )
68
+ status.success? || status.exitstatus == NOT_FOUND_STATUS
69
+ end
70
+
71
+ # security deletes one entry per call, so keep going until it reports there
72
+ # is nothing left (exit 44). Other services are untouched.
73
+ def self.delete_all(service)
74
+ loop do
75
+ _out, status = Open3.capture2(
76
+ "security", "delete-generic-password", "-s", service,
77
+ err: File::NULL
78
+ )
79
+ return true if status.exitstatus == NOT_FOUND_STATUS
80
+ return false unless status.success?
81
+ end
82
+ end
83
+
84
+ def self.quote(value)
85
+ %("#{value.gsub("\\", "\\\\\\\\").gsub('"', '\\"')}")
86
+ end
87
+ private_class_method :quote
88
+ end
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+
5
+ class Gem::CredentialStore; end unless defined?(Gem::CredentialStore)
6
+
7
+ ##
8
+ # Stores credentials in the Windows Credential Manager via the
9
+ # +Windows.Security.Credentials.PasswordVault+ WinRT API, driven from
10
+ # PowerShell. Account/service/secret values are passed as environment
11
+ # variables rather than interpolated into the script text, so no quoting
12
+ # scheme is needed and values cannot break out of the script.
13
+ #
14
+ # Windows PowerShell is used rather than PowerShell 7 (+pwsh+) because the
15
+ # WinRT projection used here is not reliably available under pwsh. It is
16
+ # spawned as +powershell+ rather than +powershell.exe+, the way this codebase
17
+ # spawns +git+, so PATHEXT resolves it. That also lets the tests put a shim
18
+ # ahead of it on Windows, where a file with a shebang is not executable.
19
+
20
+ class Gem::CredentialStore::WindowsBackend
21
+ LOAD_VAULT_TYPE = <<~POWERSHELL
22
+ $ErrorActionPreference = 'Stop'
23
+ [void][Windows.Security.Credentials.PasswordVault,Windows.Security.Credentials,ContentType=WindowsRuntime]
24
+ POWERSHELL
25
+ private_constant :LOAD_VAULT_TYPE
26
+
27
+ def self.get(service, account)
28
+ script = <<~POWERSHELL
29
+ #{LOAD_VAULT_TYPE}
30
+ $vault = New-Object Windows.Security.Credentials.PasswordVault
31
+ $credential = $vault.Retrieve($env:RUBYGEMS_CRED_SERVICE, $env:RUBYGEMS_CRED_ACCOUNT)
32
+ # Emitting into the pipeline would send the string through PowerShell's
33
+ # formatter, which wraps at the host width and would corrupt a secret.
34
+ [Console]::Out.Write($credential.Password)
35
+ POWERSHELL
36
+
37
+ out, err, status = run(script, service, account)
38
+ # An absent credential is ordinary. Any other failure is not, so raise and
39
+ # let the wrapper report why rather than authenticating without one.
40
+ unless status.success?
41
+ return nil if missing_credential?(err)
42
+
43
+ raise "powershell exited with #{status.exitstatus}: #{err.strip}"
44
+ end
45
+
46
+ secret = out.chomp
47
+ secret.empty? ? nil : secret
48
+ end
49
+
50
+ def self.set(service, account, secret)
51
+ script = <<~POWERSHELL
52
+ #{LOAD_VAULT_TYPE}
53
+ $vault = New-Object Windows.Security.Credentials.PasswordVault
54
+ try {
55
+ $existing = $vault.Retrieve($env:RUBYGEMS_CRED_SERVICE, $env:RUBYGEMS_CRED_ACCOUNT)
56
+ $vault.Remove($existing)
57
+ } catch {}
58
+ $credential = New-Object Windows.Security.Credentials.PasswordCredential($env:RUBYGEMS_CRED_SERVICE, $env:RUBYGEMS_CRED_ACCOUNT, $env:RUBYGEMS_CRED_SECRET)
59
+ $vault.Add($credential)
60
+ POWERSHELL
61
+
62
+ _out, err, status = run(script, service, account, secret)
63
+ return true if status.success?
64
+
65
+ # Raise so the reason reaches the user; see MacOSBackend.set.
66
+ raise "powershell exited with #{status.exitstatus}: #{err.strip}"
67
+ end
68
+
69
+ def self.delete(service, account)
70
+ script = <<~POWERSHELL
71
+ #{LOAD_VAULT_TYPE}
72
+ $vault = New-Object Windows.Security.Credentials.PasswordVault
73
+ $credential = $vault.Retrieve($env:RUBYGEMS_CRED_SERVICE, $env:RUBYGEMS_CRED_ACCOUNT)
74
+ $vault.Remove($credential)
75
+ POWERSHELL
76
+
77
+ _out, err, status = run(script, service, account)
78
+ status.success? || missing_credential?(err)
79
+ end
80
+
81
+ # FindAllByResource raises when the resource has no entries, which is an
82
+ # empty list rather than an error.
83
+ def self.list(service)
84
+ script = <<~POWERSHELL
85
+ #{LOAD_VAULT_TYPE}
86
+ $vault = New-Object Windows.Security.Credentials.PasswordVault
87
+ try {
88
+ $vault.FindAllByResource($env:RUBYGEMS_CRED_SERVICE) | ForEach-Object {
89
+ [Console]::Out.WriteLine($_.UserName)
90
+ }
91
+ } catch {
92
+ if (-not ($_.Exception.Message -match 'not found|0x80070490')) { throw }
93
+ }
94
+ POWERSHELL
95
+
96
+ out, _err, status = run(script, service, nil)
97
+ return [] unless status.success?
98
+
99
+ out.split("\n").map(&:chomp).reject(&:empty?).uniq
100
+ end
101
+
102
+ def self.delete_all(service)
103
+ script = <<~POWERSHELL
104
+ #{LOAD_VAULT_TYPE}
105
+ $vault = New-Object Windows.Security.Credentials.PasswordVault
106
+ try {
107
+ $vault.FindAllByResource($env:RUBYGEMS_CRED_SERVICE) | ForEach-Object { $vault.Remove($_) }
108
+ } catch {
109
+ if (-not ($_.Exception.Message -match 'not found|0x80070490')) { throw }
110
+ }
111
+ POWERSHELL
112
+
113
+ _out, err, status = run(script, service, nil)
114
+ status.success? || missing_credential?(err)
115
+ end
116
+
117
+ def self.run(script, service, account, secret = nil)
118
+ env = { "RUBYGEMS_CRED_SERVICE" => service, "RUBYGEMS_CRED_ACCOUNT" => account }
119
+ env["RUBYGEMS_CRED_SECRET"] = secret if secret
120
+
121
+ Open3.capture3(env, "powershell", "-NoProfile", "-NonInteractive", "-Command", "-", stdin_data: script)
122
+ end
123
+ private_class_method :run
124
+
125
+ def self.missing_credential?(message)
126
+ text = message.to_s.downcase
127
+ text.include?("element not found") || text.include?("0x80070490") || text.include?("could not be found")
128
+ end
129
+ private_class_method :missing_credential?
130
+ end
@@ -0,0 +1,332 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Skip reloading when an identical copy (e.g. the one shipped inside the Bundler
4
+ # gem) was already required from a different path, to avoid redefinition warnings.
5
+ return if defined?(Gem::CredentialStore::SERVICE_NAME)
6
+
7
+ ##
8
+ # Gem::CredentialStore is opt-in storage for authentication secrets (API
9
+ # keys, host credentials) in the operating system's native secret store
10
+ # instead of a plain text file:
11
+ #
12
+ # * macOS: Keychain, via the +security+ command line tool.
13
+ # * Linux: the Secret Service API (GNOME Keyring, KWallet, ...), via
14
+ # +secret-tool+.
15
+ # * Windows: Credential Manager, via the +Windows.Security.Credentials.PasswordVault+
16
+ # API from PowerShell.
17
+ #
18
+ # A third party can add another backend (1Password, pass, HashiCorp Vault,
19
+ # ...) by shipping a gem that provides
20
+ # <tt>rubygems/credential_store/backends/<name></tt> and calls
21
+ # .register_backend from it. Users then select it by name instead of +true+
22
+ # (see .resolve_backend).
23
+ #
24
+ # Every public method traps all errors and returns +nil+/+false+ instead of
25
+ # raising, so that callers can transparently fall back to their existing
26
+ # file-based storage when the native store is unavailable or fails (a
27
+ # locked keychain over SSH, a headless Linux session without a keyring
28
+ # daemon, ...).
29
+
30
+ class Gem::CredentialStore
31
+ SERVICE_NAME = "rubygems"
32
+
33
+ ##
34
+ # Returns the store to use for +spec+, or +nil+ when the credential store
35
+ # is off. +spec+ is either +true+ (use this platform's native backend) or
36
+ # the name of a registered backend such as "1password". +service+ names
37
+ # the account namespace within the backend, so RubyGems and Bundler keep
38
+ # separate credentials in one native store. The store is memoized per
39
+ # +spec+ and +service+ for the life of the process, so the read cache and
40
+ # any expensive backend startup are shared across callers. A test may
41
+ # install a stand-in via #instance= that is returned here for any enabled
42
+ # +spec+, or inject a shared backend via #backend=.
43
+
44
+ def self.for(spec, service: SERVICE_NAME)
45
+ return nil unless spec
46
+ return @override if defined?(@override) && @override
47
+
48
+ backend = defined?(@override_backend) && @override_backend ? @override_backend : backend_for(spec)
49
+ (@instances ||= {})[[spec, service]] ||= new(backend: backend, service: service)
50
+ end
51
+
52
+ ##
53
+ # The default-backed store for this platform, i.e. <tt>for(true)</tt>.
54
+ # Kept for callers and tests that only care about the native backend.
55
+
56
+ def self.instance
57
+ self.for(true)
58
+ end
59
+
60
+ ##
61
+ # Installs a stand-in store that .for returns for any enabled setting.
62
+ # Intended for tests that inject a fake backend.
63
+
64
+ def self.instance=(store)
65
+ @override = store
66
+ end
67
+
68
+ ##
69
+ # Installs a shared backend that .for wraps for every spec and service.
70
+ # Intended for tests that need RubyGems and Bundler credentials to land in
71
+ # one backend under their own service names.
72
+
73
+ def self.backend=(backend)
74
+ @override_backend = backend
75
+ end
76
+
77
+ ##
78
+ # Clears the memoized stores, the injected overrides, and the warned
79
+ # messages. Intended for tests only.
80
+
81
+ def self.reset!
82
+ @override = nil
83
+ @override_backend = nil
84
+ @instances = nil
85
+ @warned = nil
86
+ @warn_handler = nil
87
+ end
88
+
89
+ ##
90
+ # Warns once per distinct message. A single flag for every message would
91
+ # let an early warning about, say, a misspelled backend name suppress the
92
+ # later warning that a secret was written in plain text.
93
+
94
+ def self.warn_once(message)
95
+ @warned ||= {}
96
+ return if @warned.key?(message)
97
+
98
+ @warned[message] = true
99
+
100
+ if defined?(@warn_handler) && @warn_handler
101
+ @warn_handler.call(message)
102
+ else
103
+ Gem.ui.alert_warning message
104
+ end
105
+ end
106
+
107
+ ##
108
+ # Sends warnings to +handler+ (anything responding to #call) instead of
109
+ # Gem.ui. Bundler sets this because it replaces Gem.ui with a subclass of
110
+ # Gem::SilentUI, which discards alert_warning entirely, so a credential
111
+ # store failure would otherwise be silent for the whole bundle command.
112
+
113
+ def self.warn_handler=(handler)
114
+ @warn_handler = handler
115
+ end
116
+
117
+ ##
118
+ # Registers +backend+ under +name+ so it can be selected with
119
+ # <tt>credential_store = <name></tt>. A third-party backend gem calls this
120
+ # from the file RubyGems loads for that name (see .resolve_backend).
121
+
122
+ def self.register_backend(name, backend)
123
+ (@backends ||= {})[name.to_s] = backend
124
+ end
125
+
126
+ BACKEND_NAME = /\A[a-z0-9_-]+\z/
127
+
128
+ ##
129
+ # Resolves a registered backend by +name+, requiring
130
+ # <tt>rubygems/credential_store/backends/<name></tt> on first use so a
131
+ # backend shipped as its own gem loads only when actually selected.
132
+ # Returns +nil+ (warning once) when the name is malformed or no gem
133
+ # provides it, which makes callers fall back to file storage. The fixed
134
+ # require prefix and the restricted name charset keep the setting a piece
135
+ # of data, never a path or a command.
136
+
137
+ def self.resolve_backend(name)
138
+ # The setting can carry bytes Regexp#match? would reject. A name that gets
139
+ # past the match is ASCII only, so the require path it builds stays sound.
140
+ name = name.to_s.b
141
+ unless BACKEND_NAME.match?(name)
142
+ warn_once "Ignoring invalid credential store backend name #{name.inspect}."
143
+ return nil
144
+ end
145
+
146
+ return @backends[name] if @backends&.key?(name)
147
+
148
+ begin
149
+ require "rubygems/credential_store/backends/#{name}"
150
+ rescue LoadError
151
+ warn_once "Credential store backend #{name.inspect} is not installed. " \
152
+ "Install a gem that provides rubygems/credential_store/backends/#{name}, " \
153
+ "or unset the credential_store setting. Falling back to file storage."
154
+ return nil
155
+ end
156
+
157
+ @backends && @backends[name]
158
+ end
159
+
160
+ def self.backend_for(spec)
161
+ spec == true ? default_backend : resolve_backend(spec)
162
+ end
163
+ private_class_method :backend_for
164
+
165
+ def self.default_backend
166
+ if Gem.win_platform?
167
+ require_relative "credential_store/native/windows"
168
+ WindowsBackend
169
+ elsif RUBY_PLATFORM.include?("darwin")
170
+ require_relative "credential_store/native/macos"
171
+ MacOSBackend
172
+ elsif RUBY_PLATFORM.include?("linux")
173
+ require_relative "credential_store/native/linux"
174
+ LinuxBackend if LinuxBackend.available?
175
+ end
176
+ end
177
+
178
+ ##
179
+ # +backend+ is only used by tests to inject a fake backend regardless of
180
+ # the platform the test suite happens to run on. +service+ is the account
181
+ # namespace this store reads and writes under.
182
+
183
+ def initialize(backend: self.class.default_backend, service: SERVICE_NAME)
184
+ @backend = backend
185
+ @service = service
186
+ @cache = {}
187
+ end
188
+
189
+ ##
190
+ # True if a native credential backend is usable on this platform.
191
+
192
+ def available?
193
+ !@backend.nil?
194
+ end
195
+
196
+ ##
197
+ # Returns the secret stored for +account+, or +nil+ if there is none or
198
+ # the backend is unavailable/fails.
199
+
200
+ def get(account)
201
+ return nil unless @backend
202
+ return @cache[account] if @cache.key?(account)
203
+
204
+ @cache[account] = @backend.get(@service, account)
205
+ rescue StandardError => e
206
+ warn_failure(:read, e)
207
+ # Retrying means another subprocess and, on some platforms, another
208
+ # authorization prompt. #read_failed? keeps this apart from an absent one.
209
+ (@failed ||= {})[account] = true
210
+ @cache[account] = nil
211
+ end
212
+
213
+ ##
214
+ # True when #get returned +nil+ for +account+ because the backend could not
215
+ # answer, rather than because nothing is stored under it. Callers that would
216
+ # otherwise fall back to a different credential need the difference: a
217
+ # missing entry means "use something else", an unreadable one does not.
218
+
219
+ def read_failed?(account)
220
+ return false unless defined?(@failed) && @failed
221
+
222
+ @failed.key?(account)
223
+ end
224
+
225
+ ##
226
+ # Stores +secret+ for +account+. Returns +true+ on success.
227
+
228
+ def set(account, secret)
229
+ return false unless @backend
230
+
231
+ validate_credential(account, secret)
232
+
233
+ if @backend.set(@service, account, secret)
234
+ @cache[account] = secret
235
+ @failed&.delete(account)
236
+ invalidate_list
237
+ true
238
+ else
239
+ false
240
+ end
241
+ rescue StandardError => e
242
+ warn_failure(:write, e)
243
+ false
244
+ end
245
+
246
+ ##
247
+ # Removes the secret stored for +account+. Returns +true+ if the entry is
248
+ # gone, whether or not it existed beforehand.
249
+
250
+ def delete(account)
251
+ return false unless @backend
252
+
253
+ result = @backend.delete(@service, account)
254
+ @cache.delete(account)
255
+ @failed&.delete(account)
256
+ invalidate_list
257
+ result
258
+ rescue StandardError => e
259
+ warn_failure(:remove, e)
260
+ false
261
+ end
262
+
263
+ ##
264
+ # The accounts this store holds, or +nil+ when the backend cannot
265
+ # enumerate them. Listing is optional in the backend protocol: the native
266
+ # backends implement it, but a third-party backend that only resolves
267
+ # credentials on demand has nothing to enumerate. Callers must treat +nil+
268
+ # as "unknown", not as "empty". Secrets are never returned.
269
+
270
+ def list
271
+ return nil unless @backend.respond_to?(:list)
272
+ return @list if defined?(@list)
273
+
274
+ @list = @backend.list(@service)
275
+ rescue StandardError => e
276
+ warn_failure(:list, e)
277
+ # Remembered for the same reason #get remembers a failed read.
278
+ @list = nil
279
+ end
280
+
281
+ ##
282
+ # Removes every entry this store owns (all accounts under its service).
283
+ # Returns +true+ when the store is now clear. Used by +gem signout+ to end
284
+ # every session at once, mirroring deletion of the whole credentials file.
285
+
286
+ def delete_all
287
+ return false unless @backend
288
+
289
+ result = @backend.delete_all(@service)
290
+ @cache.clear
291
+ @failed = nil
292
+ invalidate_list
293
+ result
294
+ rescue StandardError => e
295
+ warn_failure(:remove, e)
296
+ false
297
+ end
298
+
299
+ private
300
+
301
+ # The listing is memoized, so a write has to drop it.
302
+ def invalidate_list
303
+ remove_instance_variable(:@list) if defined?(@list)
304
+ end
305
+
306
+ # The macOS keychain hands non-printable bytes back as hex through the only
307
+ # read-back its CLI offers. Applied to every backend so the same value is
308
+ # stored, or refused for the same reason, everywhere.
309
+ PRINTABLE_ASCII = /\A[\x20-\x7e]*\z/
310
+
311
+ # What happens after a failure depends on the operation, so the warning has
312
+ # to say the right thing for each.
313
+ OUTCOMES = {
314
+ read: "any copy left in the config file will be used instead",
315
+ write: "falling back to file storage",
316
+ remove: "the credential is still in the store",
317
+ list: "stored credentials will not be listed",
318
+ }.freeze
319
+
320
+ # A newline in an account would start a second command in the macOS batch
321
+ # input. #set turns the raise back into a warning and a false.
322
+ def validate_credential(account, secret)
323
+ raise ArgumentError, "credential secret must be printable ASCII" unless secret.to_s.b.match?(PRINTABLE_ASCII)
324
+ raise ArgumentError, "credential account must not contain a newline" if account.to_s.include?("\n")
325
+ raise ArgumentError, "credential service must not contain a newline" if @service.to_s.include?("\n")
326
+ end
327
+
328
+ def warn_failure(operation, error)
329
+ self.class.warn_once "Credential store #{operation} failed for #{@service}" \
330
+ " (#{error.class}: #{error.message}); #{OUTCOMES[operation]}."
331
+ end
332
+ end