gemstash 2.8.1 → 2.9.0

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 (50) hide show
  1. checksums.yaml +4 -4
  2. data/lib/gemstash/authorization.rb +0 -2
  3. data/lib/gemstash/cache.rb +5 -4
  4. data/lib/gemstash/cli/base.rb +9 -2
  5. data/lib/gemstash/cli/setup.rb +1 -1
  6. data/lib/gemstash/cli/start.rb +4 -0
  7. data/lib/gemstash/configuration.rb +71 -1
  8. data/lib/gemstash/db/upstream.rb +30 -2
  9. data/lib/gemstash/dependencies.rb +28 -5
  10. data/lib/gemstash/env.rb +4 -2
  11. data/lib/gemstash/gem_fetcher.rb +1 -2
  12. data/lib/gemstash/gem_pusher.rb +24 -20
  13. data/lib/gemstash/gem_source/private_source.rb +22 -6
  14. data/lib/gemstash/gem_source/upstream_source.rb +129 -34
  15. data/lib/gemstash/http_client.rb +167 -8
  16. data/lib/gemstash/logging.rb +3 -1
  17. data/lib/gemstash/man/gemstash-authorize.1 +29 -48
  18. data/lib/gemstash/man/gemstash-authorize.1.txt +14 -14
  19. data/lib/gemstash/man/gemstash-configuration.5 +231 -154
  20. data/lib/gemstash/man/gemstash-configuration.5.txt +208 -93
  21. data/lib/gemstash/man/gemstash-customize.7 +150 -168
  22. data/lib/gemstash/man/gemstash-customize.7.txt +80 -86
  23. data/lib/gemstash/man/gemstash-debugging.7 +20 -33
  24. data/lib/gemstash/man/gemstash-debugging.7.txt +5 -7
  25. data/lib/gemstash/man/gemstash-deploy.7 +31 -53
  26. data/lib/gemstash/man/gemstash-deploy.7.txt +17 -18
  27. data/lib/gemstash/man/gemstash-mirror.7 +24 -37
  28. data/lib/gemstash/man/gemstash-mirror.7.txt +14 -9
  29. data/lib/gemstash/man/gemstash-multiple-sources.7 +105 -110
  30. data/lib/gemstash/man/gemstash-multiple-sources.7.txt +62 -41
  31. data/lib/gemstash/man/gemstash-private-gems.7 +87 -136
  32. data/lib/gemstash/man/gemstash-private-gems.7.txt +30 -30
  33. data/lib/gemstash/man/gemstash-readme.7 +105 -127
  34. data/lib/gemstash/man/gemstash-readme.7.txt +43 -49
  35. data/lib/gemstash/man/gemstash-setup.1 +22 -42
  36. data/lib/gemstash/man/gemstash-setup.1.txt +12 -12
  37. data/lib/gemstash/man/gemstash-start.1 +9 -27
  38. data/lib/gemstash/man/gemstash-start.1.txt +5 -5
  39. data/lib/gemstash/man/gemstash-status.1 +9 -27
  40. data/lib/gemstash/man/gemstash-status.1.txt +5 -5
  41. data/lib/gemstash/man/gemstash-stop.1 +9 -27
  42. data/lib/gemstash/man/gemstash-stop.1.txt +5 -5
  43. data/lib/gemstash/man/gemstash-version.1 +8 -28
  44. data/lib/gemstash/man/gemstash-version.1.txt +2 -2
  45. data/lib/gemstash/puma.rb +1 -0
  46. data/lib/gemstash/rack_env_rewriter.rb +3 -0
  47. data/lib/gemstash/upstream.rb +83 -6
  48. data/lib/gemstash/version.rb +1 -1
  49. data/lib/gemstash/web.rb +12 -0
  50. metadata +7 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a82d677f5e34fb5419559efa82be00dac33bc2c88eb42fba56fd7d96affdb861
4
- data.tar.gz: 1312285c0a2031493295d9f1782198acc379639cef84c084cd25355ea3fb8fc4
3
+ metadata.gz: 1090d8b1200f8d8992f8c012cba1958140c2e9fb6574df29a59d75e3fc01f940
4
+ data.tar.gz: a9c8480b06d8468f33d2579b4c79c94bf71aedd119d8b052bd1578962210e0b4
5
5
  SHA512:
6
- metadata.gz: a583c3af4a2e24638530887ec70066b7593272be84e79f1c7b27bd27e9b79d052016704d16e5d82af388a080e7117ed73870080831788773e64bf78abf74fe8b
7
- data.tar.gz: 26b1d25a502ddc1dbd80506230562ea3e761efe622845f1c109609aae0fbfdd782e5364055eb3b45e01ba59d757835033de0d71b8157e84b7ddcc9a9e9fbee55
6
+ metadata.gz: e89dd340e3982f674d5bc9d7cfb3b82b98ce3511b6ae82c0d6384a7123faabee038249481b2e0ccd12ec1001ab44beb563915cd2e17b415ed074d3253cdd7481
7
+ data.tar.gz: 48259089dcdd6ac30c1a6860e22cd759250e166a6b434bad35c5451da7e9a02dc82aa0a1fcb17fe1e3454634743069d059622d54785ea003f0bdd20351fbd4bb
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "set"
4
-
5
3
  module Gemstash
6
4
  # An action was not authorized and should cause the server to send a 401.
7
5
  class NotAuthorizedError < StandardError
@@ -28,8 +28,9 @@ module Gemstash
28
28
  @client.delete("auths/#{auth_key}")
29
29
  end
30
30
 
31
+ # v2: entries written before dependency data was validated are never read
31
32
  def dependencies(scope, gems)
32
- key_prefix = "deps/v1/#{scope}/"
33
+ key_prefix = "deps/v2/#{scope}/"
33
34
  keys = gems.map {|g| "#{key_prefix}#{g}" }
34
35
 
35
36
  @client.get_multi(keys) do |key, value|
@@ -38,11 +39,11 @@ module Gemstash
38
39
  end
39
40
 
40
41
  def set_dependency(scope, gem, value)
41
- @client.set("deps/v1/#{scope}/#{gem}", value, EXPIRY)
42
+ @client.set("deps/v2/#{scope}/#{gem}", value, EXPIRY)
42
43
  end
43
44
 
44
45
  def invalidate_gem(scope, gem)
45
- @client.delete("deps/v1/#{scope}/#{gem}")
46
+ @client.delete("deps/v2/#{scope}/#{gem}")
46
47
  Gemstash::SpecsBuilder.invalidate_stored if scope == "private"
47
48
  end
48
49
  end
@@ -105,7 +106,7 @@ module Gemstash
105
106
  next if v.nil?
106
107
 
107
108
  k = keys[idx]
108
- yield(k, YAML.load(v))
109
+ yield(k, YAML.safe_load(v, permitted_classes: [Symbol]))
109
110
  end
110
111
  end
111
112
 
@@ -31,12 +31,19 @@ module Gemstash
31
31
  end
32
32
 
33
33
  def store_config
34
- config = Gemstash::Configuration.new(file: @cli.options[:config_file])
34
+ config = Gemstash::Configuration.new(file: @cli.options[:config_file], validate: validate_config?)
35
35
  gemstash_env.config = config
36
- rescue Gemstash::Configuration::MissingFileError => e
36
+ rescue Gemstash::Configuration::MissingFileError, Gemstash::Configuration::InvalidAllowedUpstreamError,
37
+ Gemstash::Configuration::InvalidUpstreamLimitError => e
37
38
  raise Gemstash::CLI::Error.new(@cli, e.message)
38
39
  end
39
40
 
41
+ # Only starting the server needs every entry of the configuration to be
42
+ # usable. The other commands must work while a broken file is being fixed.
43
+ def validate_config?
44
+ false
45
+ end
46
+
40
47
  def check_gemstash_version
41
48
  version = Gem::Version.new(Gemstash::Storage.metadata[:gemstash_version])
42
49
  return if Gem::Requirement.new("<= #{Gemstash::VERSION}").satisfied_by?(Gem::Version.new(version))
@@ -84,7 +84,7 @@ module Gemstash
84
84
 
85
85
  def ask_memcached_details
86
86
  say_current_config(:memcached_servers, "Current Memcached servers")
87
- servers = @cli.ask "What is the comma separated Memcached servers? [localhost:11211]"
87
+ servers = @cli.ask "What is the comma-separated Memcached servers? [localhost:11211]"
88
88
  servers = "localhost:11211" if servers.empty?
89
89
  @config[:memcached_servers] = servers
90
90
  end
@@ -20,6 +20,10 @@ module Gemstash
20
20
 
21
21
  private
22
22
 
23
+ def validate_config?
24
+ true
25
+ end
26
+
23
27
  def puma_config
24
28
  File.expand_path("../puma.rb", __dir__)
25
29
  end
@@ -13,9 +13,13 @@ module Gemstash
13
13
  bind: "tcp://0.0.0.0:9292",
14
14
  rubygems_url: "https://rubygems.org",
15
15
  ignore_gemfile_source: false,
16
+ allowed_upstreams: [],
17
+ allowed_redirects: [],
16
18
  protected_fetch: false,
17
19
  fetch_timeout: 20,
18
20
  open_timeout: 2,
21
+ max_upstream_duration: 300,
22
+ max_upstream_bytes: 64 * 1024 * 1024,
19
23
  # Actual default for db_connection_options is dynamic based on the adapter
20
24
  db_connection_options: {},
21
25
  puma_threads: 16,
@@ -26,6 +30,14 @@ module Gemstash
26
30
 
27
31
  DEFAULT_FILE = File.expand_path("~/.gemstash/config.yml").freeze
28
32
 
33
+ # A scheme other than a lowercase http or https. Gemstash::Upstream would
34
+ # read such an entry as a host name and put https:// in front of it.
35
+ UNSUPPORTED_SCHEME = %r{\A(?!https?://)[A-Za-z][A-Za-z0-9+.-]*://}
36
+
37
+ # An http or https scheme without the // after it. Gemstash::Upstream would
38
+ # read "https:user:pass@host" as user "https" and "https:/host" as host "https".
39
+ SCHEME_WITHOUT_SLASHES = %r{\Ahttps?:(?!//)}i
40
+
29
41
  # This error is thrown when a config file is explicitly specified that
30
42
  # doesn't exist.
31
43
  class MissingFileError < StandardError
@@ -34,9 +46,24 @@ module Gemstash
34
46
  end
35
47
  end
36
48
 
37
- def initialize(file: nil, config: nil)
49
+ # This error is thrown when :fetch_timeout, :max_upstream_duration or
50
+ # :max_upstream_bytes has a value that cannot be used.
51
+ class InvalidUpstreamLimitError < ArgumentError; end
52
+
53
+ # This error is thrown when :allowed_upstreams or :allowed_redirects has an
54
+ # entry that cannot be used. It gives the position of the entry and never
55
+ # its value, because the value can contain credentials.
56
+ class InvalidAllowedUpstreamError < StandardError
57
+ def initialize(key, position, problem)
58
+ accepted = key == :allowed_upstreams ? "\"*\" or an http(s) gem source URL" : "an http(s) URL"
59
+ super("Invalid :#{key} entry #{position} (#{problem}). Each entry must be #{accepted}.")
60
+ end
61
+ end
62
+
63
+ def initialize(file: nil, config: nil, validate: true)
38
64
  if config
39
65
  @config = DEFAULTS.merge(config).freeze
66
+ check_config if validate
40
67
  return
41
68
  end
42
69
 
@@ -51,6 +78,8 @@ module Gemstash
51
78
  else
52
79
  @config = DEFAULTS
53
80
  end
81
+
82
+ check_config if validate
54
83
  end
55
84
 
56
85
  def default?(key)
@@ -79,6 +108,20 @@ module Gemstash
79
108
 
80
109
  private
81
110
 
111
+ def check_config
112
+ validate_upstream_limits
113
+ validate_allowed_upstreams
114
+ end
115
+
116
+ def validate_upstream_limits
117
+ %i[fetch_timeout max_upstream_duration].each do |key|
118
+ value = @config[key]
119
+ raise InvalidUpstreamLimitError, "#{key} must be a positive finite number" unless value.is_a?(Numeric) && value.finite? && value.positive?
120
+ end
121
+ bytes = @config[:max_upstream_bytes]
122
+ raise InvalidUpstreamLimitError, "max_upstream_bytes must be nil or a positive integer" unless bytes.nil? || (bytes.is_a?(Integer) && bytes.positive?)
123
+ end
124
+
82
125
  def default_file
83
126
  # Support the config file being specified via environment variable
84
127
  gemstash_config = ENV["GEMSTASH_CONFIG"]
@@ -95,5 +138,32 @@ module Gemstash
95
138
  YAML.load_file(file) || {}
96
139
  end
97
140
  end
141
+
142
+ # Entries are checked when the configuration is loaded, so that a typo
143
+ # stops Gemstash from starting instead of failing requests later on.
144
+ def validate_allowed_upstreams
145
+ %i[allowed_upstreams allowed_redirects].each do |key|
146
+ Array(self[key]).each_with_index do |entry, index|
147
+ problem = allowed_upstream_problem(entry, wildcard: key == :allowed_upstreams)
148
+ raise InvalidAllowedUpstreamError.new(key, index + 1, problem) if problem
149
+ end
150
+ end
151
+ end
152
+
153
+ def allowed_upstream_problem(entry, wildcard:)
154
+ return if entry == "*" && wildcard
155
+ return "wildcard is not supported" if entry == "*"
156
+ return "not a string" unless entry.is_a?(String)
157
+ return "empty" if entry.strip.empty?
158
+ return "scheme is not http:// or https://" if entry.match?(UNSUPPORTED_SCHEME)
159
+ return "scheme is not followed by //" if entry.match?(SCHEME_WITHOUT_SLASHES)
160
+
161
+ begin
162
+ "no host" if Gemstash::Upstream.new(entry).host.to_s.empty?
163
+ rescue URI::InvalidURIError, RuntimeError
164
+ # The parser's message repeats the entry, so it is not passed on.
165
+ "not a valid URL"
166
+ end
167
+ end
98
168
  end
99
169
  end
@@ -5,10 +5,38 @@ module Gemstash
5
5
  # Sequel model for upstreams table.
6
6
  class Upstream < Sequel::Model
7
7
  def self.find_or_insert(upstream)
8
- record = self[uri: upstream.to_s]
8
+ record = find_existing(upstream)
9
9
  return record.id if record
10
10
 
11
- new(uri: upstream.to_s, host_id: upstream.host_id).tap(&:save).id
11
+ new(uri: upstream.cache_uri, host_id: upstream.host_id).tap(&:save).id
12
+ end
13
+
14
+ def self.find_existing(upstream)
15
+ record = self[uri: upstream.cache_uri]
16
+ return record if record
17
+
18
+ # A legacy encoded path may have this ID but represent a different
19
+ # literal path. Do not let it serve that path's modern identity.
20
+ if (record = self[host_id: upstream.host_id]) &&
21
+ (record.uri == upstream.to_s || record.uri == upstream.cache_uri)
22
+ record.update(uri: upstream.cache_uri) if record.uri != upstream.cache_uri
23
+ return record
24
+ end
25
+
26
+ # Older releases decoded the entire URL before deriving the cache ID.
27
+ legacy_uri = CGI.unescape(upstream.to_s)
28
+ begin
29
+ legacy_source = URI(legacy_uri)
30
+ legacy_id = "#{legacy_source.host}_#{Digest::MD5.hexdigest(legacy_uri)}"
31
+ if (record = self[uri: legacy_uri, host_id: legacy_id])
32
+ record.update(uri: Gemstash::Upstream.new(legacy_source).cache_uri)
33
+ return record if legacy_uri == upstream.to_s
34
+ end
35
+ rescue URI::InvalidURIError
36
+ # A decoded URI that could not be parsed never had a legacy cache row.
37
+ end
38
+
39
+ nil
12
40
  end
13
41
  end
14
42
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "json"
3
4
  require "cgi"
4
- require "set"
5
5
 
6
6
  module Gemstash
7
7
  # :nodoc:
@@ -11,7 +11,7 @@ module Gemstash
11
11
  end
12
12
 
13
13
  def self.for_upstream(upstream, http_client)
14
- new(scope: "upstream/#{upstream}", http_client: http_client)
14
+ new(scope: "upstream/#{upstream.is_a?(Gemstash::Upstream) ? upstream.cache_uri : upstream}", http_client: http_client)
15
15
  end
16
16
 
17
17
  def initialize(scope: nil, http_client: nil, db_model: nil)
@@ -29,6 +29,8 @@ module Gemstash
29
29
  include Gemstash::Env::Helper
30
30
  include Gemstash::Logging
31
31
 
32
+ FIELDS = %i[name number platform dependencies].freeze
33
+
32
34
  def initialize(gems, scope, http_client, db_model)
33
35
  @gems = Set.new(gems)
34
36
  @scope = scope
@@ -77,16 +79,37 @@ module Gemstash
77
79
 
78
80
  log.info "Fetching dependencies: #{@gems.to_a.join(", ")}"
79
81
  gems_param = @gems.map {|gem| CGI.escape(gem) }.join(",")
80
- fetched = @http_client.get("api/v1/dependencies?gems=#{gems_param}")
81
- fetched = Marshal.load(fetched).group_by {|r| r[:name] }
82
+ fetched = @http_client.get("api/v1/dependencies.json?gems=#{gems_param}")
83
+ fetched = parse_dependencies(fetched).group_by {|r| r[:name] }
82
84
 
83
85
  fetched.each do |gem, result|
84
- @gems.delete(gem)
86
+ # Only keep what was asked for
87
+ next unless @gems.delete?(gem)
88
+
85
89
  gemstash_env.cache.set_dependency(@scope, gem, result)
86
90
  @dependencies += result
87
91
  end
88
92
  end
89
93
 
94
+ # The result is cached and handed to clients, so only the fields of the
95
+ # dependency API are taken from what the upstream sent.
96
+ def parse_dependencies(body)
97
+ entries = JSON.parse(body, symbolize_names: true)
98
+ valid = entries.is_a?(Array) && entries.all? {|entry| valid_entry?(entry) }
99
+ raise Gemstash::WebError.new("Unexpected dependency data from the upstream", 502) unless valid
100
+
101
+ entries.map {|entry| entry.slice(*FIELDS) }
102
+ rescue JSON::ParserError
103
+ raise Gemstash::WebError.new("Dependency data from the upstream is not valid JSON", 502)
104
+ end
105
+
106
+ def valid_entry?(entry)
107
+ entry.is_a?(Hash) &&
108
+ entry.values_at(:name, :number, :platform).all?(String) &&
109
+ entry[:dependencies].is_a?(Array) &&
110
+ entry[:dependencies].all? {|dependency| dependency.is_a?(Array) && dependency.size == 2 && dependency.all?(String) }
111
+ end
112
+
90
113
  def cache_missing
91
114
  @gems.each do |gem|
92
115
  gemstash_env.cache.set_dependency(@scope, gem, [])
data/lib/gemstash/env.rb CHANGED
@@ -11,7 +11,7 @@ require "pathname"
11
11
  module Gemstash
12
12
  # Storage for application-wide variables and configuration.
13
13
  class Env
14
- # The Gemstash::Env must be set before being retreived via
14
+ # The Gemstash::Env must be set before being retrieved via
15
15
  # Gemstash::Env.current. This error is thrown when that is not honored.
16
16
  class EnvNotSetError < StandardError
17
17
  end
@@ -59,8 +59,10 @@ module Gemstash
59
59
  Thread.current[:gemstash_env] = value
60
60
  end
61
61
 
62
+ # Not validated: "gemstash setup" reads the current file through this
63
+ # before replacing it. "gemstash start" gives the server a checked config.
62
64
  def config
63
- @config ||= Gemstash::Configuration.new
65
+ @config ||= Gemstash::Configuration.new(validate: false)
64
66
  end
65
67
 
66
68
  def config=(value)
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "gemstash"
4
- require "set"
5
4
 
6
5
  module Gemstash
7
6
  # :nodoc:
@@ -41,7 +40,7 @@ module Gemstash
41
40
 
42
41
  def validate_download(content, headers)
43
42
  expected_size = content_length(headers)
44
- raise "Incomplete download, only #{body.length} was downloaded out of #{expected_size}" \
43
+ raise "Incomplete download, only #{content.length} was downloaded out of #{expected_size}" \
45
44
  if content.length < expected_size
46
45
  end
47
46
 
@@ -19,6 +19,7 @@ module Gemstash
19
19
  end
20
20
 
21
21
  def self.serve(app)
22
+ app.auth.check("push")
22
23
  gem = app.request.body.read
23
24
  new(app.auth, gem).serve
24
25
  end
@@ -30,9 +31,19 @@ module Gemstash
30
31
 
31
32
  def serve
32
33
  check_auth
33
- store_gem
34
- store_gemspec
35
- save_to_database
34
+ resource = storage.resource(full_name)
35
+ gemstash_env.db.transaction do
36
+ save_to_database
37
+ raise ExistingVersionError, "Cannot push to an existing version!" if Dir.exist?(resource.folder)
38
+
39
+ begin
40
+ store_gem(resource)
41
+ store_gemspec(resource)
42
+ rescue StandardError
43
+ FileUtils.remove_entry(resource.folder) if Dir.exist?(resource.folder)
44
+ raise
45
+ end
46
+ end
36
47
  invalidate_cache
37
48
  end
38
49
 
@@ -54,34 +65,27 @@ module Gemstash
54
65
  @auth.check("push")
55
66
  end
56
67
 
57
- def store_gem
58
- resource_exist = storage.resource(full_name).exist?
59
- resource_is_indexed = storage.resource(full_name).properties[:indexed] if resource_exist
60
-
61
- raise ExistingVersionError, "Cannot push to an existing version!" if resource_exist && resource_is_indexed
62
-
63
- storage.resource(full_name).save({ gem: @content }, indexed: true)
68
+ def store_gem(resource)
69
+ resource.save({ gem: @content }, indexed: true)
64
70
  end
65
71
 
66
- def store_gemspec
72
+ def store_gemspec(resource)
67
73
  spec = gem.spec
68
74
  spec = Marshal.dump(spec)
69
75
  spec = Zlib::Deflate.deflate(spec)
70
- storage.resource(full_name).save(spec: spec)
76
+ resource.save(spec: spec)
71
77
  end
72
78
 
73
79
  def save_to_database
74
80
  spec = gem.spec
75
81
 
76
- gemstash_env.db.transaction do
77
- gem_id = Gemstash::DB::Rubygem.find_or_insert(spec)
78
- existing = Gemstash::DB::Version.find_by_spec(gem_id, spec)
79
- raise ExistingVersionError, "Cannot push to an existing version!" if existing && existing.indexed
80
- raise YankedVersionError, "Cannot push to a yanked version!" if existing && !existing.indexed
82
+ gem_id = Gemstash::DB::Rubygem.find_or_insert(spec)
83
+ existing = Gemstash::DB::Version.find_by_spec(gem_id, spec)
84
+ raise ExistingVersionError, "Cannot push to an existing version!" if existing && existing.indexed
85
+ raise YankedVersionError, "Cannot push to a yanked version!" if existing && !existing.indexed
81
86
 
82
- version_id = Gemstash::DB::Version.insert_by_spec(gem_id, spec)
83
- Gemstash::DB::Dependency.insert_by_spec(version_id, spec)
84
- end
87
+ version_id = Gemstash::DB::Version.insert_by_spec(gem_id, spec)
88
+ Gemstash::DB::Dependency.insert_by_spec(version_id, spec)
85
89
  end
86
90
 
87
91
  def invalidate_cache
@@ -26,6 +26,14 @@ module Gemstash
26
26
  halt 403, "Not yet supported"
27
27
  end
28
28
 
29
+ def serve_dependencies
30
+ protected_fetch { super }
31
+ end
32
+
33
+ def serve_dependencies_json
34
+ protected_fetch { super }
35
+ end
36
+
29
37
  def serve_add_gem
30
38
  protected(Gemstash::GemPusher)
31
39
  end
@@ -55,8 +63,7 @@ module Gemstash
55
63
  end
56
64
 
57
65
  def serve_marshal(id)
58
- authorization.protect(self) do
59
- auth.check("fetch") if gemstash_env.config[:protected_fetch]
66
+ protected_fetch do
60
67
  gem_full_name = id.delete_suffix(".gemspec.rz")
61
68
  gem = fetch_gem(gem_full_name)
62
69
  halt 404 unless gem.exist?(:spec)
@@ -70,8 +77,7 @@ module Gemstash
70
77
  end
71
78
 
72
79
  def serve_gem(id)
73
- authorization.protect(self) do
74
- auth.check("fetch") if gemstash_env.config[:protected_fetch]
80
+ protected_fetch do
75
81
  gem_full_name = id.delete_suffix(".gem")
76
82
  gem = fetch_gem(gem_full_name)
77
83
  content_type "application/octet-stream"
@@ -100,6 +106,13 @@ module Gemstash
100
106
  authorization.protect(self) { servable.serve(self) }
101
107
  end
102
108
 
109
+ def protected_fetch
110
+ authorization.protect(self) do
111
+ auth.check("fetch") if gemstash_env.config[:protected_fetch]
112
+ yield
113
+ end
114
+ end
115
+
103
116
  def authorization
104
117
  Gemstash::ApiKeyAuthorization
105
118
  end
@@ -113,9 +126,12 @@ module Gemstash
113
126
  end
114
127
 
115
128
  def fetch_gem(gem_full_name)
116
- gem = storage.resource(gem_full_name)
129
+ version = Gemstash::DB::Version.find_by_full_name(gem_full_name)
130
+ halt 404 unless version
131
+
132
+ gem = storage.resource(version.storage_id)
117
133
  halt 404 unless gem.exist?(:gem)
118
- halt 403, "That gem has been yanked" unless gem.properties[:indexed]
134
+ halt 403, "That gem has been yanked" unless version.indexed && gem.properties[:indexed]
119
135
  gem
120
136
  end
121
137
  end