3 class Collection < ArvadosModel
6 include CommonApiTemplate
8 before_validation :check_encoding
9 before_validation :check_signatures
10 before_validation :strip_manifest_text
11 before_validation :set_portable_data_hash
12 validate :ensure_hash_matches_manifest_text
14 # Query only undeleted collections by default.
15 default_scope where("expires_at IS NULL or expires_at > CURRENT_TIMESTAMP")
17 api_accessible :user, extend: :common do |t|
21 t.add :portable_data_hash
22 t.add :signed_manifest_text, as: :manifest_text
25 def self.attributes_required_columns
26 # If we don't list this explicitly, the params[:select] code gets
27 # confused by the way we expose signed_manifest_text as
28 # manifest_text in the API response, and never let clients select
29 # the manifest_text column.
30 super.merge('manifest_text' => ['manifest_text'])
34 return false if self.manifest_text.nil?
36 return true if current_user.andand.is_admin
38 # Provided the manifest_text hasn't changed materially since an
39 # earlier validation, it's safe to pass this validation on
40 # subsequent passes without checking any signatures. This is
41 # important because the signatures have probably been stripped off
42 # by the time we get to a second validation pass!
43 return true if @signatures_checked and @signatures_checked == compute_pdh
45 if self.manifest_text_changed?
46 # Check permissions on the collection manifest.
47 # If any signature cannot be verified, raise PermissionDeniedError
48 # which will return 403 Permission denied to the client.
49 api_token = current_api_client_authorization.andand.api_token
51 key: Rails.configuration.blob_signing_key,
53 ttl: Rails.configuration.blob_signing_ttl,
55 self.manifest_text.lines.each do |entry|
56 entry.split[1..-1].each do |tok|
57 if /^[[:digit:]]+:[[:digit:]]+:/.match tok
58 # This is a filename token, not a blob locator. Note that we
59 # keep checking tokens after this, even though manifest
60 # format dictates that all subsequent tokens will also be
61 # filenames. Safety first!
62 elsif Blob.verify_signature tok, signing_opts
64 elsif Keep::Locator.parse(tok).andand.signature
65 # Signature provided, but verify_signature did not like it.
66 logger.warn "Invalid signature on locator #{tok}"
67 raise ArvadosModel::PermissionDeniedError
68 elsif Rails.configuration.permit_create_collection_with_unsigned_manifest
69 # No signature provided, but we are running in insecure mode.
70 logger.debug "Missing signature on locator #{tok} ignored"
71 elsif Blob.new(tok).empty?
72 # No signature provided -- but no data to protect, either.
74 logger.warn "Missing signature on locator #{tok}"
75 raise ArvadosModel::PermissionDeniedError
80 @signatures_checked = compute_pdh
83 def strip_manifest_text
84 if self.manifest_text_changed?
85 # Remove any permission signatures from the manifest.
86 self.class.munge_manifest_locators!(self[:manifest_text]) do |loc|
87 loc.without_signature.to_s
93 def set_portable_data_hash
94 if (portable_data_hash.nil? or
95 portable_data_hash == "" or
96 (manifest_text_changed? and !portable_data_hash_changed?))
97 @need_pdh_validation = false
98 self.portable_data_hash = compute_pdh
99 elsif portable_data_hash_changed?
100 @need_pdh_validation = true
102 loc = Keep::Locator.parse!(self.portable_data_hash)
105 self.portable_data_hash = loc.to_s
107 self.portable_data_hash = "#{loc.hash}+#{portable_manifest_text.bytesize}"
109 rescue ArgumentError => e
110 errors.add(:portable_data_hash, "#{e}")
117 def ensure_hash_matches_manifest_text
118 return true unless manifest_text_changed? or portable_data_hash_changed?
119 # No need verify it if :set_portable_data_hash just computed it!
120 return true if not @need_pdh_validation
121 expect_pdh = compute_pdh
122 if expect_pdh != portable_data_hash
123 errors.add(:portable_data_hash,
124 "does not match computed hash #{expect_pdh}")
130 if manifest_text.encoding.name == 'UTF-8' and manifest_text.valid_encoding?
134 # If Ruby thinks the encoding is something else, like 7-bit
135 # ASCII, but its stored bytes are equal to the (valid) UTF-8
136 # encoding of the same string, we declare it to be a UTF-8
139 utf8.force_encoding Encoding::UTF_8
140 if utf8.valid_encoding? and utf8 == manifest_text.encode(Encoding::UTF_8)
146 errors.add :manifest_text, "must use UTF-8 encoding"
151 def redundancy_status
152 if redundancy_confirmed_as.nil?
154 elsif redundancy_confirmed_as < redundancy
157 if redundancy_confirmed_at.nil?
159 elsif Time.now - redundancy_confirmed_at < 7.days
167 def signed_manifest_text
168 if has_attribute? :manifest_text
169 token = current_api_client_authorization.andand.api_token
170 @signed_manifest_text = self.class.sign_manifest manifest_text, token
174 def self.sign_manifest manifest, token
176 key: Rails.configuration.blob_signing_key,
178 ttl: Rails.configuration.blob_signing_ttl,
181 munge_manifest_locators!(m) do |loc|
182 Blob.sign_locator(loc.to_s, signing_opts)
187 def self.munge_manifest_locators! manifest
188 # Given a manifest text and a block, yield each locator,
189 # and replace it with whatever the block returns.
190 manifest.andand.gsub!(/ [[:xdigit:]]{32}(\+[[:digit:]]+)?(\+\S+)/) do |word|
191 if loc = Keep::Locator.parse(word.strip)
199 def self.normalize_uuid uuid
202 uuid.split('+').each do |token|
203 if token.match /^[0-9a-f]{32,}$/
204 raise "uuid #{uuid} has multiple hash parts" if hash_part
206 elsif token.match /^\d+$/
207 raise "uuid #{uuid} has multiple size parts" if size_part
211 raise "uuid #{uuid} has no hash part" if !hash_part
212 [hash_part, size_part].compact.join '+'
215 # Return array of Collection objects
216 def self.find_all_for_docker_image(search_term, search_tag=nil, readers=nil)
217 readers ||= [Thread.current[:user]]
219 readable_by(*readers).
220 readable_by(*readers, table_name: "collections").
221 joins("JOIN collections ON links.head_uuid = collections.uuid").
222 order("links.created_at DESC")
224 # If the search term is a Collection locator that contains one file
225 # that looks like a Docker image, return it.
226 if loc = Keep::Locator.parse(search_term)
228 coll_match = readable_by(*readers).where(portable_data_hash: loc.to_s).limit(1).first
230 # Check if the Collection contains exactly one file whose name
231 # looks like a saved Docker image.
232 manifest = Keep::Manifest.new(coll_match.manifest_text)
233 if manifest.exact_file_count?(1) and
234 (manifest.files[0][1] =~ /^[0-9A-Fa-f]{64}\.tar$/)
240 if search_tag.nil? and (n = search_term.index(":"))
241 search_tag = search_term[n+1..-1]
242 search_term = search_term[0..n-1]
245 # Find Collections with matching Docker image repository+tag pairs.
246 matches = base_search.
247 where(link_class: "docker_image_repo+tag",
248 name: "#{search_term}:#{search_tag || 'latest'}")
250 # If that didn't work, find Collections with matching Docker image hashes.
252 matches = base_search.
253 where("link_class = ? and links.name LIKE ?",
254 "docker_image_hash", "#{search_term}%")
257 # Generate an order key for each result. We want to order the results
258 # so that anything with an image timestamp is considered more recent than
259 # anything without; then we use the link's created_at as a tiebreaker.
261 matches.all.map do |link|
262 uuid_timestamps[link.head_uuid] = [(-link.properties["image_timestamp"].to_datetime.to_i rescue 0),
263 -link.created_at.to_i]
265 Collection.where('uuid in (?)', uuid_timestamps.keys).sort_by { |c| uuid_timestamps[c.uuid] }
268 def self.for_latest_docker_image(search_term, search_tag=nil, readers=nil)
269 find_all_for_docker_image(search_term, search_tag, readers).first
273 def portable_manifest_text
274 portable_manifest = self[:manifest_text].dup
275 self.class.munge_manifest_locators!(portable_manifest) do |loc|
276 loc.hash + '+' + loc.size.to_s
282 portable_manifest = portable_manifest_text
283 (Digest::MD5.hexdigest(portable_manifest) +
285 portable_manifest.bytesize.to_s)