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
13 before_save :set_file_names
15 # Query only undeleted collections by default.
16 default_scope where("expires_at IS NULL or expires_at > CURRENT_TIMESTAMP")
18 api_accessible :user, extend: :common do |t|
22 t.add :portable_data_hash
23 t.add :signed_manifest_text, as: :manifest_text
24 t.add :replication_desired
27 def self.attributes_required_columns
29 # If we don't list manifest_text explicitly, the
30 # params[:select] code gets confused by the way we
31 # expose signed_manifest_text as manifest_text in the
32 # API response, and never let clients select the
33 # manifest_text column.
34 'manifest_text' => ['manifest_text'],
36 # This is a shim until the database column gets
37 # renamed to replication_desired in #3410.
38 'replication_desired' => ['redundancy'],
43 return false if self.manifest_text.nil?
45 return true if current_user.andand.is_admin
47 # Provided the manifest_text hasn't changed materially since an
48 # earlier validation, it's safe to pass this validation on
49 # subsequent passes without checking any signatures. This is
50 # important because the signatures have probably been stripped off
51 # by the time we get to a second validation pass!
52 return true if @signatures_checked and @signatures_checked == compute_pdh
54 if self.manifest_text_changed?
55 # Check permissions on the collection manifest.
56 # If any signature cannot be verified, raise PermissionDeniedError
57 # which will return 403 Permission denied to the client.
58 api_token = current_api_client_authorization.andand.api_token
60 key: Rails.configuration.blob_signing_key,
62 ttl: Rails.configuration.blob_signing_ttl,
64 self.manifest_text.lines.each do |entry|
65 entry.split[1..-1].each do |tok|
66 if /^[[:digit:]]+:[[:digit:]]+:/.match tok
67 # This is a filename token, not a blob locator. Note that we
68 # keep checking tokens after this, even though manifest
69 # format dictates that all subsequent tokens will also be
70 # filenames. Safety first!
71 elsif Blob.verify_signature tok, signing_opts
73 elsif Keep::Locator.parse(tok).andand.signature
74 # Signature provided, but verify_signature did not like it.
75 logger.warn "Invalid signature on locator #{tok}"
76 raise ArvadosModel::PermissionDeniedError
77 elsif Rails.configuration.permit_create_collection_with_unsigned_manifest
78 # No signature provided, but we are running in insecure mode.
79 logger.debug "Missing signature on locator #{tok} ignored"
80 elsif Blob.new(tok).empty?
81 # No signature provided -- but no data to protect, either.
83 logger.warn "Missing signature on locator #{tok}"
84 raise ArvadosModel::PermissionDeniedError
89 @signatures_checked = compute_pdh
92 def strip_manifest_text
93 if self.manifest_text_changed?
94 # Remove any permission signatures from the manifest.
95 self.class.munge_manifest_locators!(self[:manifest_text]) do |loc|
96 loc.without_signature.to_s
102 def set_portable_data_hash
103 if (portable_data_hash.nil? or
104 portable_data_hash == "" or
105 (manifest_text_changed? and !portable_data_hash_changed?))
106 @need_pdh_validation = false
107 self.portable_data_hash = compute_pdh
108 elsif portable_data_hash_changed?
109 @need_pdh_validation = true
111 loc = Keep::Locator.parse!(self.portable_data_hash)
114 self.portable_data_hash = loc.to_s
116 self.portable_data_hash = "#{loc.hash}+#{portable_manifest_text.bytesize}"
118 rescue ArgumentError => e
119 errors.add(:portable_data_hash, "#{e}")
126 def ensure_hash_matches_manifest_text
127 return true unless manifest_text_changed? or portable_data_hash_changed?
128 # No need verify it if :set_portable_data_hash just computed it!
129 return true if not @need_pdh_validation
130 expect_pdh = compute_pdh
131 if expect_pdh != portable_data_hash
132 errors.add(:portable_data_hash,
133 "does not match computed hash #{expect_pdh}")
139 if self.manifest_text_changed?
140 self.file_names = manifest_files
147 if self.manifest_text
148 self.manifest_text.scan(/ \d+:\d+:(\S+)/) do |name|
149 names << name.first.gsub('\040',' ') + "\n"
150 break if names.length > 2**12
154 if self.manifest_text and names.length < 2**12
155 self.manifest_text.scan(/^\.\/(\S+)/m) do |stream_name|
156 names << stream_name.first.gsub('\040',' ') + "\n"
157 break if names.length > 2**12
165 if manifest_text.encoding.name == 'UTF-8' and manifest_text.valid_encoding?
169 # If Ruby thinks the encoding is something else, like 7-bit
170 # ASCII, but its stored bytes are equal to the (valid) UTF-8
171 # encoding of the same string, we declare it to be a UTF-8
174 utf8.force_encoding Encoding::UTF_8
175 if utf8.valid_encoding? and utf8 == manifest_text.encode(Encoding::UTF_8)
181 errors.add :manifest_text, "must use UTF-8 encoding"
186 def replication_desired
187 # Shim until database columns get fixed up in #3410.
191 def redundancy_status
192 if redundancy_confirmed_as.nil?
194 elsif redundancy_confirmed_as < redundancy
197 if redundancy_confirmed_at.nil?
199 elsif Time.now - redundancy_confirmed_at < 7.days
207 def signed_manifest_text
208 if has_attribute? :manifest_text
209 token = current_api_client_authorization.andand.api_token
210 @signed_manifest_text = self.class.sign_manifest manifest_text, token
214 def self.sign_manifest manifest, token
216 key: Rails.configuration.blob_signing_key,
218 ttl: Rails.configuration.blob_signing_ttl,
221 munge_manifest_locators!(m) do |loc|
222 Blob.sign_locator(loc.to_s, signing_opts)
227 def self.munge_manifest_locators! manifest
228 # Given a manifest text and a block, yield each locator,
229 # and replace it with whatever the block returns.
230 manifest.andand.gsub!(/ [[:xdigit:]]{32}(\+[[:digit:]]+)?(\+\S+)/) do |word|
231 if loc = Keep::Locator.parse(word.strip)
239 def self.normalize_uuid uuid
242 uuid.split('+').each do |token|
243 if token.match /^[0-9a-f]{32,}$/
244 raise "uuid #{uuid} has multiple hash parts" if hash_part
246 elsif token.match /^\d+$/
247 raise "uuid #{uuid} has multiple size parts" if size_part
251 raise "uuid #{uuid} has no hash part" if !hash_part
252 [hash_part, size_part].compact.join '+'
255 # Return array of Collection objects
256 def self.find_all_for_docker_image(search_term, search_tag=nil, readers=nil)
257 readers ||= [Thread.current[:user]]
259 readable_by(*readers).
260 readable_by(*readers, table_name: "collections").
261 joins("JOIN collections ON links.head_uuid = collections.uuid").
262 order("links.created_at DESC")
264 # If the search term is a Collection locator that contains one file
265 # that looks like a Docker image, return it.
266 if loc = Keep::Locator.parse(search_term)
268 coll_match = readable_by(*readers).where(portable_data_hash: loc.to_s).limit(1).first
270 # Check if the Collection contains exactly one file whose name
271 # looks like a saved Docker image.
272 manifest = Keep::Manifest.new(coll_match.manifest_text)
273 if manifest.exact_file_count?(1) and
274 (manifest.files[0][1] =~ /^[0-9A-Fa-f]{64}\.tar$/)
280 if search_tag.nil? and (n = search_term.index(":"))
281 search_tag = search_term[n+1..-1]
282 search_term = search_term[0..n-1]
285 # Find Collections with matching Docker image repository+tag pairs.
286 matches = base_search.
287 where(link_class: "docker_image_repo+tag",
288 name: "#{search_term}:#{search_tag || 'latest'}")
290 # If that didn't work, find Collections with matching Docker image hashes.
292 matches = base_search.
293 where("link_class = ? and links.name LIKE ?",
294 "docker_image_hash", "#{search_term}%")
297 # Generate an order key for each result. We want to order the results
298 # so that anything with an image timestamp is considered more recent than
299 # anything without; then we use the link's created_at as a tiebreaker.
301 matches.all.map do |link|
302 uuid_timestamps[link.head_uuid] = [(-link.properties["image_timestamp"].to_datetime.to_i rescue 0),
303 -link.created_at.to_i]
305 Collection.where('uuid in (?)', uuid_timestamps.keys).sort_by { |c| uuid_timestamps[c.uuid] }
308 def self.for_latest_docker_image(search_term, search_tag=nil, readers=nil)
309 find_all_for_docker_image(search_term, search_tag, readers).first
312 def self.searchable_columns operator
313 super - ["manifest_text"]
317 def portable_manifest_text
318 portable_manifest = self[:manifest_text].dup
319 self.class.munge_manifest_locators!(portable_manifest) do |loc|
320 loc.hash + '+' + loc.size.to_s
326 portable_manifest = portable_manifest_text
327 (Digest::MD5.hexdigest(portable_manifest) +
329 portable_manifest.bytesize.to_s)