Merge branch '8784-dir-listings'
[arvados.git] / services / api / app / controllers / arvados / v1 / keep_disks_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class Arvados::V1::KeepDisksController < ApplicationController
6   skip_before_filter :require_auth_scope, only: :ping
7   skip_before_filter :render_404_if_no_object, only: :ping
8
9   def self._ping_requires_parameters
10     {
11       uuid: {required: false},
12       ping_secret: {required: true},
13       node_uuid: {required: false},
14       filesystem_uuid: {required: false},
15       service_host: {required: false},
16       service_port: {required: true},
17       service_ssl_flag: {required: true}
18     }
19   end
20
21   def ping
22     params[:service_host] ||= request.env['REMOTE_ADDR']
23     if !params[:uuid] && current_user.andand.is_admin
24       # Create a new KeepDisk and ping it.
25       @object = KeepDisk.new(filesystem_uuid: params[:filesystem_uuid])
26       @object.save!
27
28       # In the first ping from this new filesystem_uuid, we can't
29       # expect the keep node to know the ping_secret so we made sure
30       # we got an admin token. Here we add ping_secret to params so
31       # the ping call below is properly authenticated.
32       params[:ping_secret] = @object.ping_secret
33     end
34     act_as_system_user do
35       if !@object.andand.ping(params)
36         return render_not_found "object not found"
37       end
38       # Render the :superuser view (i.e., include the ping_secret) even
39       # if !current_user.is_admin. This is safe because @object.ping's
40       # success implies the ping_secret was already known by the client.
41       send_json @object.as_api_response(:superuser)
42     end
43   end
44
45   def find_objects_for_index
46     # all users can list all keep disks
47     @objects = model_class.where('1=1')
48     super
49   end
50 end