Merge remote-tracking branch 'origin/master' into 3605-improved-dashboard
[arvados.git] / services / api / app / controllers / arvados / v1 / collections_controller.rb
1 require "arvados/keep"
2
3 class Arvados::V1::CollectionsController < ApplicationController
4   def create
5     if resource_attrs[:uuid] and (loc = Keep::Locator.parse(resource_attrs[:uuid]))
6       resource_attrs[:portable_data_hash] = loc.to_s
7       resource_attrs.delete :uuid
8     end
9     super
10   end
11
12   def find_object_by_uuid
13     if loc = Keep::Locator.parse(params[:id])
14       loc.strip_hints!
15       if c = Collection.readable_by(*@read_users).where({ portable_data_hash: loc.to_s }).limit(1).first
16         @object = {
17           uuid: c.portable_data_hash,
18           portable_data_hash: c.portable_data_hash,
19           manifest_text: c.manifest_text,
20         }
21       end
22     else
23       super
24     end
25     true
26   end
27
28   def show
29     sign_manifests(@object[:manifest_text])
30     if @object.is_a? Collection
31       render json: @object.as_api_response
32     else
33       render json: @object
34     end
35   end
36
37   def index
38     sign_manifests(*@objects.map { |c| c[:manifest_text] })
39     super
40   end
41
42   def script_param_edges(visited, sp)
43     case sp
44     when Hash
45       sp.each do |k, v|
46         script_param_edges(visited, v)
47       end
48     when Array
49       sp.each do |v|
50         script_param_edges(visited, v)
51       end
52     when String
53       return if sp.empty?
54       if loc = Keep::Locator.parse(sp)
55         search_edges(visited, loc.to_s, :search_up)
56       end
57     end
58   end
59
60   def search_edges(visited, uuid, direction)
61     if uuid.nil? or uuid.empty? or visited[uuid]
62       return
63     end
64
65     if loc = Keep::Locator.parse(uuid)
66       loc.strip_hints!
67       return if visited[loc.to_s]
68     end
69
70     logger.debug "visiting #{uuid}"
71
72     if loc
73       # uuid is a portable_data_hash
74       if c = Collection.readable_by(*@read_users).where(portable_data_hash: loc.to_s).limit(1).first
75         visited[loc.to_s] = {
76           portable_data_hash: c.portable_data_hash,
77         }
78       end
79
80       if direction == :search_up
81         # Search upstream for jobs where this locator is the output of some job
82         Job.readable_by(*@read_users).where(output: loc.to_s).each do |job|
83           search_edges(visited, job.uuid, :search_up)
84         end
85
86         Job.readable_by(*@read_users).where(log: loc.to_s).each do |job|
87           search_edges(visited, job.uuid, :search_up)
88         end
89       elsif direction == :search_down
90         if loc.to_s == "d41d8cd98f00b204e9800998ecf8427e+0"
91           # Special case, don't follow the empty collection.
92           return
93         end
94
95         # Search downstream for jobs where this locator is in script_parameters
96         Job.readable_by(*@read_users).where(["jobs.script_parameters like ?", "%#{loc.to_s}%"]).each do |job|
97           search_edges(visited, job.uuid, :search_down)
98         end
99       end
100     else
101       # uuid is a regular Arvados UUID
102       rsc = ArvadosModel::resource_class_for_uuid uuid
103       if rsc == Job
104         Job.readable_by(*@read_users).where(uuid: uuid).each do |job|
105           visited[uuid] = job.as_api_response
106           if direction == :search_up
107             # Follow upstream collections referenced in the script parameters
108             script_param_edges(visited, job.script_parameters)
109           elsif direction == :search_down
110             # Follow downstream job output
111             search_edges(visited, job.output, direction)
112           end
113         end
114       elsif rsc == Collection
115         if c = Collection.readable_by(*@read_users).where(uuid: uuid).limit(1).first
116           search_edges(visited, c.portable_data_hash, direction)
117           visited[c.portable_data_hash] = c.as_api_response
118         end
119       elsif rsc != nil
120         rsc.where(uuid: uuid).each do |r|
121           visited[uuid] = r.as_api_response
122         end
123       end
124     end
125
126     if direction == :search_up
127       # Search for provenance links pointing to the current uuid
128       Link.readable_by(*@read_users).
129         where(head_uuid: uuid, link_class: "provenance").
130         each do |link|
131         visited[link.uuid] = link.as_api_response
132         search_edges(visited, link.tail_uuid, direction)
133       end
134     elsif direction == :search_down
135       # Search for provenance links emanating from the current uuid
136       Link.readable_by(current_user).
137         where(tail_uuid: uuid, link_class: "provenance").
138         each do |link|
139         visited[link.uuid] = link.as_api_response
140         search_edges(visited, link.head_uuid, direction)
141       end
142     end
143   end
144
145   def provenance
146     visited = {}
147     search_edges(visited, @object[:uuid] || @object[:portable_data_hash], :search_up)
148     render json: visited
149   end
150
151   def used_by
152     visited = {}
153     search_edges(visited, @object[:uuid] || @object[:portable_data_hash], :search_down)
154     render json: visited
155   end
156
157   protected
158
159   def apply_filters
160     if action_name == 'index'
161       # Omit manifest_text from index results unless expressly selected.
162       @select ||= model_class.api_accessible_attributes(:user).
163         map { |attr_spec| attr_spec.first.to_s } - ["manifest_text"]
164     end
165     super
166   end
167
168   def sign_manifests(*manifests)
169     if current_api_client_authorization
170       signing_opts = {
171         key: Rails.configuration.blob_signing_key,
172         api_token: current_api_client_authorization.api_token,
173         ttl: Rails.configuration.blob_signing_ttl,
174       }
175       manifests.each do |text|
176         Collection.munge_manifest_locators(text) do |loc|
177           Blob.sign_locator(loc.to_s, signing_opts)
178         end
179       end
180     end
181   end
182 end