1 class Arvados::V1::CollectionsController < ApplicationController
3 # Collections are owned by system_user. Creating a collection has
4 # two effects: The collection is added if it doesn't already
5 # exist, and a "permission" Link is added (if one doesn't already
6 # exist) giving the current user (or specified owner_uuid)
7 # permission to read it.
8 owner_uuid = resource_attrs.delete(:owner_uuid) || current_user.uuid
9 unless current_user.can? write: owner_uuid
10 logger.warn "User #{current_user.andand.uuid} tried to set collection owner_uuid to #{owner_uuid}"
11 raise ArvadosModel::PermissionDeniedError
14 # Check permissions on the collection manifest.
15 # If any signature cannot be verified, return 403 Permission denied.
16 api_token = current_api_client_authorization.andand.api_token
18 key: Rails.configuration.blob_signing_key,
20 ttl: Rails.configuration.blob_signing_ttl,
22 resource_attrs[:manifest_text].lines.each do |entry|
23 entry.split[1..-1].each do |tok|
24 if /^[[:digit:]]+:[[:digit:]]+:/.match tok
25 # This is a filename token, not a blob locator. Note that we
26 # keep checking tokens after this, even though manifest
27 # format dictates that all subsequent tokens will also be
28 # filenames. Safety first!
29 elsif Blob.verify_signature tok, signing_opts
31 elsif Locator.parse(tok).andand.signature
32 # Signature provided, but verify_signature did not like it.
33 logger.warn "Invalid signature on locator #{tok}"
34 raise ArvadosModel::PermissionDeniedError
35 elsif Rails.configuration.permit_create_collection_with_unsigned_manifest
36 # No signature provided, but we are running in insecure mode.
37 logger.debug "Missing signature on locator #{tok} ignored"
38 elsif Blob.new(tok).empty?
39 # No signature provided -- but no data to protect, either.
41 logger.warn "Missing signature on locator #{tok}"
42 raise ArvadosModel::PermissionDeniedError
47 # Remove any permission signatures from the manifest.
48 resource_attrs[:manifest_text]
49 .gsub!(/ [[:xdigit:]]{32}(\+[[:digit:]]+)?(\+\S+)/) { |word|
51 loc = Locator.parse(word)
53 " " + loc.without_signature.to_s
59 # Save the collection with the stripped manifest.
61 @object = model_class.new resource_attrs.reject { |k,v| k == :owner_uuid }
64 rescue ActiveRecord::RecordNotUnique
65 logger.debug resource_attrs.inspect
66 if resource_attrs[:manifest_text] and resource_attrs[:uuid]
67 @existing_object = model_class.
68 where('uuid=? and manifest_text=?',
69 resource_attrs[:uuid],
70 resource_attrs[:manifest_text]).
72 @object = @existing_object || @object
77 owner_uuid: owner_uuid,
78 link_class: 'permission',
80 head_uuid: @object.uuid,
83 ActiveRecord::Base.transaction do
84 if Link.where(link_attrs).empty?
85 Link.create! link_attrs
94 if current_api_client_authorization
96 key: Rails.configuration.blob_signing_key,
97 api_token: current_api_client_authorization.api_token,
98 ttl: Rails.configuration.blob_signing_ttl,
100 @object[:manifest_text]
101 .gsub!(/ [[:xdigit:]]{32}(\+[[:digit:]]+)?(\+\S+)/) { |word|
103 loc = Locator.parse(word)
105 " " + Blob.sign_locator(word, signing_opts)
111 render json: @object.as_api_response(:with_data)
114 def collection_uuid(uuid)
115 m = /([a-f0-9]{32}(\+[0-9]+)?)(\+.*)?/.match(uuid)
123 def script_param_edges(visited, sp)
127 script_param_edges(visited, v)
131 script_param_edges(visited, v)
135 m = collection_uuid(sp)
137 generate_provenance_edges(visited, m)
142 def generate_provenance_edges(visited, uuid)
143 m = collection_uuid(uuid)
146 if not uuid or uuid.empty? or visited[uuid]
150 logger.debug "visiting #{uuid}"
153 # uuid is a collection
154 Collection.readable_by(current_user).where(uuid: uuid).each do |c|
155 visited[uuid] = c.as_api_response
156 visited[uuid][:files] = []
158 visited[uuid][:files] << f
162 Job.readable_by(current_user).where(output: uuid).each do |job|
163 generate_provenance_edges(visited, job.uuid)
166 Job.readable_by(current_user).where(log: uuid).each do |job|
167 generate_provenance_edges(visited, job.uuid)
171 # uuid is something else
172 rsc = ArvadosModel::resource_class_for_uuid uuid
174 Job.readable_by(current_user).where(uuid: uuid).each do |job|
175 visited[uuid] = job.as_api_response
176 script_param_edges(visited, job.script_parameters)
179 rsc.where(uuid: uuid).each do |r|
180 visited[uuid] = r.as_api_response
185 Link.readable_by(current_user).
186 where(head_uuid: uuid, link_class: "provenance").
188 visited[link.uuid] = link.as_api_response
189 generate_provenance_edges(visited, link.tail_uuid)
192 #puts "finished #{uuid}"
197 generate_provenance_edges(visited, @object[:uuid])
201 def generate_used_by_edges(visited, uuid)
202 m = collection_uuid(uuid)
205 if not uuid or uuid.empty? or visited[uuid]
209 logger.debug "visiting #{uuid}"
212 # uuid is a collection
213 Collection.readable_by(current_user).where(uuid: uuid).each do |c|
214 visited[uuid] = c.as_api_response
215 visited[uuid][:files] = []
217 visited[uuid][:files] << f
221 if uuid == "d41d8cd98f00b204e9800998ecf8427e+0"
222 # special case for empty collection
226 Job.readable_by(current_user).where(["jobs.script_parameters like ?", "%#{uuid}%"]).each do |job|
227 generate_used_by_edges(visited, job.uuid)
231 # uuid is something else
232 rsc = ArvadosModel::resource_class_for_uuid uuid
234 Job.readable_by(current_user).where(uuid: uuid).each do |job|
235 visited[uuid] = job.as_api_response
236 generate_used_by_edges(visited, job.output)
239 rsc.where(uuid: uuid).each do |r|
240 visited[uuid] = r.as_api_response
245 Link.readable_by(current_user).
246 where(tail_uuid: uuid, link_class: "provenance").
248 visited[link.uuid] = link.as_api_response
249 generate_used_by_edges(visited, link.head_uuid)
252 #puts "finished #{uuid}"
257 generate_used_by_edges(visited, @object[:uuid])
262 def find_object_by_uuid
264 if !@object and !params[:uuid].match(/^[0-9a-f]+\+\d+$/)
265 # Normalize the given uuid and search again.
266 hash_part = params[:uuid].match(/^([0-9a-f]*)/)[1]
267 collection = Collection.where('uuid like ?', hash_part + '+%').first
269 # We know the collection exists, and what its real uuid is in
270 # the database. Now, throw out @objects and repeat the usual
271 # lookup procedure. (Returning the collection at this point
272 # would bypass permission checks.)
274 @where = { uuid: collection.uuid }
275 find_objects_for_index
276 @object = @objects.first