20470: select_for_klass checks for bogus prefixed fields
[arvados.git] / services / api / app / controllers / arvados / v1 / containers_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class Arvados::V1::ContainersController < ApplicationController
6   accept_attribute_as_json :environment, Hash
7   accept_attribute_as_json :mounts, Hash
8   accept_attribute_as_json :runtime_constraints, Hash
9   accept_attribute_as_json :runtime_status, Hash
10   accept_attribute_as_json :command, Array
11   accept_attribute_as_json :scheduling_parameters, Hash
12
13   skip_before_action :find_object_by_uuid, only: [:current]
14   skip_before_action :render_404_if_no_object, only: [:current]
15
16   def auth
17     if @object.locked_by_uuid != Thread.current[:api_client_authorization].uuid
18       raise ArvadosModel::PermissionDeniedError.new("Not locked by your token")
19     end
20     if @object.runtime_token.nil?
21       @object = @object.auth
22     else
23       @object = ApiClientAuthorization.validate(token: @object.runtime_token)
24       if @object.nil?
25         raise ArvadosModel::PermissionDeniedError.new("Invalid runtime_token")
26       end
27     end
28     show
29   end
30
31   def update
32     if (resource_attrs.keys - [:cost, :gateway_address, :output_properties, :progress, :runtime_status]).empty?
33       # If no attributes are being updated besides these, there are no
34       # cascading changes to other rows/tables, so we should just use
35       # row locking.
36       @object.reload(lock: true)
37       super
38     else
39       # Lock containers table to avoid deadlock in cascading priority
40       # update (see #20240)
41       Container.transaction do
42         ActiveRecord::Base.connection.execute "LOCK TABLE containers IN EXCLUSIVE MODE"
43         super
44       end
45     end
46   end
47
48   def find_objects_for_index
49     super
50     if action_name == 'lock' || action_name == 'unlock'
51       # Avoid loading more fields than we need
52       @objects = @objects.select(:id, :uuid, :state, :priority, :auth_uuid, :locked_by_uuid, :lock_count)
53       # This gets called from within find_object_by_uuid.
54       # find_object_by_uuid stores the original value of @select in
55       # @preserve_select, edits the value of @select, calls
56       # find_objects_for_index, then restores @select from the value
57       # of @preserve_select.  So if we want our updated value of
58       # @select here to stick, we have to set @preserve_select.
59       @select = @preserve_select = %w(uuid state priority auth_uuid locked_by_uuid)
60     elsif action_name == 'update_priority'
61       # We're going to reload(lock: true) in the handler, which will
62       # select all attributes, but will fail if we don't select :id
63       # now.
64       @objects = @objects.select(:id, :uuid)
65     end
66   end
67
68   def lock
69     @object.lock
70     show
71   end
72
73   def unlock
74     @object.unlock
75     show
76   end
77
78   def update_priority
79     # Lock containers table to avoid deadlock in cascading priority update (see #20240)
80     Container.transaction do
81       ActiveRecord::Base.connection.execute "LOCK TABLE containers IN EXCLUSIVE MODE"
82       @object.reload(lock: true)
83       @object.update_priority!
84       show
85     end
86   end
87
88   def current
89     if Thread.current[:api_client_authorization].nil?
90       send_error("Not logged in", status: 401)
91     else
92       @object = Container.for_current_token
93       if @object.nil?
94         send_error("Token is not associated with a container.", status: 404)
95       else
96         show
97       end
98     end
99   end
100
101   def secret_mounts
102     c = Container.for_current_token
103     if @object && c && @object.uuid == c.uuid
104       send_json({"secret_mounts" => @object.secret_mounts})
105     else
106       send_error("Token is not associated with this container.", status: 403)
107     end
108   end
109 end