14260: secret_mounts works with runtime_token (really this time)
[arvados.git] / services / api / app / controllers / arvados / v1 / collections_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require "arvados/keep"
6 require "trashable"
7
8 class Arvados::V1::CollectionsController < ApplicationController
9   include DbCurrentTime
10   include TrashableController
11
12   def self._index_requires_parameters
13     (super rescue {}).
14       merge({
15         include_trash: {
16           type: 'boolean', required: false, description: "Include collections whose is_trashed attribute is true."
17         },
18       })
19   end
20
21   def create
22     if resource_attrs[:uuid] and (loc = Keep::Locator.parse(resource_attrs[:uuid]))
23       resource_attrs[:portable_data_hash] = loc.to_s
24       resource_attrs.delete :uuid
25     end
26     super
27   end
28
29   def find_objects_for_index
30     if params[:include_trash] || ['destroy', 'trash', 'untrash'].include?(action_name)
31       @objects = Collection.readable_by(*@read_users, {include_trash: true})
32     end
33     super
34   end
35
36   def find_object_by_uuid
37     if loc = Keep::Locator.parse(params[:id])
38       loc.strip_hints!
39
40       # It matters which Collection object we pick because we use it to get signed_manifest_text,
41       # the value of which is affected by the value of trash_at.
42       #
43       # From postgres doc: "By default, null values sort as if larger than any non-null
44       # value; that is, NULLS FIRST is the default for DESC order, and
45       # NULLS LAST otherwise."
46       #
47       # "trash_at desc" sorts null first, then latest to earliest, so
48       # it will select the Collection object with the longest
49       # available lifetime.
50
51       if c = Collection.readable_by(*@read_users).where({ portable_data_hash: loc.to_s }).order("trash_at desc").limit(1).first
52         @object = {
53           uuid: c.portable_data_hash,
54           portable_data_hash: c.portable_data_hash,
55           manifest_text: c.signed_manifest_text,
56         }
57       end
58       true
59     else
60       super
61     end
62   end
63
64   def show
65     if @object.is_a? Collection
66       # Omit unsigned_manifest_text
67       @select ||= model_class.selectable_attributes - ["unsigned_manifest_text"]
68       super
69     else
70       send_json @object
71     end
72   end
73
74
75   def find_collections(visited, sp, &b)
76     case sp
77     when ArvadosModel
78       sp.class.columns.each do |c|
79         find_collections(visited, sp[c.name.to_sym], &b) if c.name != "log"
80       end
81     when Hash
82       sp.each do |k, v|
83         find_collections(visited, v, &b)
84       end
85     when Array
86       sp.each do |v|
87         find_collections(visited, v, &b)
88       end
89     when String
90       if m = /[a-f0-9]{32}\+\d+/.match(sp)
91         yield m[0], nil
92       elsif m = Collection.uuid_regex.match(sp)
93         yield nil, m[0]
94       end
95     end
96   end
97
98   def search_edges(visited, uuid, direction)
99     if uuid.nil? or uuid.empty? or visited[uuid]
100       return
101     end
102
103     if loc = Keep::Locator.parse(uuid)
104       loc.strip_hints!
105       return if visited[loc.to_s]
106     end
107
108     logger.debug "visiting #{uuid}"
109
110     if loc
111       # uuid is a portable_data_hash
112       collections = Collection.readable_by(*@read_users).where(portable_data_hash: loc.to_s)
113       c = collections.limit(2).all
114       if c.size == 1
115         visited[loc.to_s] = c[0]
116       elsif c.size > 1
117         name = collections.limit(1).where("name <> ''").first
118         if name
119           visited[loc.to_s] = {
120             portable_data_hash: c[0].portable_data_hash,
121             name: "#{name.name} + #{collections.count-1} more"
122           }
123         else
124           visited[loc.to_s] = {
125             portable_data_hash: c[0].portable_data_hash,
126             name: loc.to_s
127           }
128         end
129       end
130
131       if direction == :search_up
132         # Search upstream for jobs where this locator is the output of some job
133         Job.readable_by(*@read_users).where(output: loc.to_s).each do |job|
134           search_edges(visited, job.uuid, :search_up)
135         end
136
137         Job.readable_by(*@read_users).where(log: loc.to_s).each do |job|
138           search_edges(visited, job.uuid, :search_up)
139         end
140       elsif direction == :search_down
141         if loc.to_s == "d41d8cd98f00b204e9800998ecf8427e+0"
142           # Special case, don't follow the empty collection.
143           return
144         end
145
146         # Search downstream for jobs where this locator is in script_parameters
147         Job.readable_by(*@read_users).where(["jobs.script_parameters like ?", "%#{loc.to_s}%"]).each do |job|
148           search_edges(visited, job.uuid, :search_down)
149         end
150
151         Job.readable_by(*@read_users).where(["jobs.docker_image_locator = ?", "#{loc.to_s}"]).each do |job|
152           search_edges(visited, job.uuid, :search_down)
153         end
154       end
155     else
156       # uuid is a regular Arvados UUID
157       rsc = ArvadosModel::resource_class_for_uuid uuid
158       if rsc == Job
159         Job.readable_by(*@read_users).where(uuid: uuid).each do |job|
160           visited[uuid] = job.as_api_response
161           if direction == :search_up
162             # Follow upstream collections referenced in the script parameters
163             find_collections(visited, job) do |hash, col_uuid|
164               search_edges(visited, hash, :search_up) if hash
165               search_edges(visited, col_uuid, :search_up) if col_uuid
166             end
167           elsif direction == :search_down
168             # Follow downstream job output
169             search_edges(visited, job.output, direction)
170           end
171         end
172       elsif rsc == Collection
173         if c = Collection.readable_by(*@read_users).where(uuid: uuid).limit(1).first
174           search_edges(visited, c.portable_data_hash, direction)
175           visited[c.portable_data_hash] = c.as_api_response
176         end
177       elsif rsc != nil
178         rsc.where(uuid: uuid).each do |r|
179           visited[uuid] = r.as_api_response
180         end
181       end
182     end
183
184     if direction == :search_up
185       # Search for provenance links pointing to the current uuid
186       Link.readable_by(*@read_users).
187         where(head_uuid: uuid, link_class: "provenance").
188         each do |link|
189         visited[link.uuid] = link.as_api_response
190         search_edges(visited, link.tail_uuid, direction)
191       end
192     elsif direction == :search_down
193       # Search for provenance links emanating from the current uuid
194       Link.readable_by(current_user).
195         where(tail_uuid: uuid, link_class: "provenance").
196         each do |link|
197         visited[link.uuid] = link.as_api_response
198         search_edges(visited, link.head_uuid, direction)
199       end
200     end
201   end
202
203   def provenance
204     visited = {}
205     search_edges(visited, @object[:portable_data_hash], :search_up)
206     search_edges(visited, @object[:uuid], :search_up)
207     send_json visited
208   end
209
210   def used_by
211     visited = {}
212     search_edges(visited, @object[:uuid], :search_down)
213     search_edges(visited, @object[:portable_data_hash], :search_down)
214     send_json visited
215   end
216
217   protected
218
219   def load_limit_offset_order_params *args
220     super
221     if action_name == 'index'
222       # Omit manifest_text and unsigned_manifest_text from index results unless expressly selected.
223       @select ||= model_class.selectable_attributes - ["manifest_text", "unsigned_manifest_text"]
224     end
225   end
226 end