20259: Add documentation for banner and tooltip features
[arvados.git] / apps / workbench / app / models / arvados_api_client.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'httpclient'
6 require 'thread'
7
8 class ArvadosApiClient
9   class ApiError < StandardError
10     attr_reader :api_response, :api_response_s, :api_status, :request_url
11
12     def initialize(request_url, errmsg)
13       @request_url = request_url
14       @api_response ||= {}
15       errors = @api_response[:errors]
16       if not errors.is_a?(Array)
17         @api_response[:errors] = [errors || errmsg]
18       end
19       super(errmsg)
20     end
21   end
22
23   class NoApiResponseException < ApiError
24     def initialize(request_url, exception)
25       @api_response_s = exception.to_s
26       super(request_url,
27             "#{exception.class.to_s} error connecting to API server")
28     end
29   end
30
31   class InvalidApiResponseException < ApiError
32     def initialize(request_url, api_response)
33       @api_status = api_response.status_code
34       @api_response_s = api_response.content
35       super(request_url, "Unparseable response from API server")
36     end
37   end
38
39   class ApiErrorResponseException < ApiError
40     def initialize(request_url, api_response)
41       @api_status = api_response.status_code
42       @api_response_s = api_response.content
43       @api_response = Oj.strict_load(@api_response_s, :symbol_keys => true)
44       errors = @api_response[:errors]
45       if errors.respond_to?(:join)
46         errors = errors.join("\n\n")
47       else
48         errors = errors.to_s
49       end
50       super(request_url, "#{errors} [API: #{@api_status}]")
51     end
52   end
53
54   class AccessForbiddenException < ApiErrorResponseException; end
55   class NotFoundException < ApiErrorResponseException; end
56   class NotLoggedInException < ApiErrorResponseException; end
57
58   ERROR_CODE_CLASSES = {
59     401 => NotLoggedInException,
60     403 => AccessForbiddenException,
61     404 => NotFoundException,
62   }
63
64   @@profiling_enabled = Rails.configuration.Workbench.ProfilingEnabled
65   @@discovery = nil
66
67   # An API client object suitable for handling API requests on behalf
68   # of the current thread.
69   def self.new_or_current
70     # If this thread doesn't have an API client yet, *or* this model
71     # has been reloaded since the existing client was created, create
72     # a new client. Otherwise, keep using the latest client created in
73     # the current thread.
74     unless Thread.current[:arvados_api_client].andand.class == self
75       Thread.current[:arvados_api_client] = new
76     end
77     Thread.current[:arvados_api_client]
78   end
79
80   def initialize *args
81     @api_client = nil
82     @client_mtx = Mutex.new
83   end
84
85   def api(resources_kind, action, data=nil, tokens={}, include_anon_token=true)
86
87     profile_checkpoint
88
89     if not @api_client
90       @client_mtx.synchronize do
91         @api_client = HTTPClient.new
92         @api_client.ssl_config.timeout = Rails.configuration.Workbench.APIClientConnectTimeout
93         @api_client.connect_timeout = Rails.configuration.Workbench.APIClientConnectTimeout
94         @api_client.receive_timeout = Rails.configuration.Workbench.APIClientReceiveTimeout
95         if Rails.configuration.TLS.Insecure
96           @api_client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
97         else
98           # Use system CA certificates
99           ["/etc/ssl/certs/ca-certificates.crt",
100            "/etc/pki/tls/certs/ca-bundle.crt"]
101             .select { |ca_path| File.readable?(ca_path) }
102             .each { |ca_path| @api_client.ssl_config.add_trust_ca(ca_path) }
103         end
104         if Rails.configuration.Workbench.APIResponseCompression
105           @api_client.transparent_gzip_decompression = true
106         end
107       end
108     end
109
110     resources_kind = class_kind(resources_kind).pluralize if resources_kind.is_a? Class
111     url = "#{self.arvados_v1_base}/#{resources_kind}#{action}"
112
113     # Clean up /arvados/v1/../../discovery/v1 to /discovery/v1
114     url.sub! '/arvados/v1/../../', '/'
115
116     anon_tokens = [Rails.configuration.Users.AnonymousUserToken].select { |x| !x.empty? && include_anon_token }
117
118     query = {
119       'reader_tokens' => ((tokens[:reader_tokens] ||
120                            Thread.current[:reader_tokens] ||
121                            []) +
122                           anon_tokens).to_json,
123     }
124     if !data.nil?
125       data.each do |k,v|
126         if v.is_a? String or v.nil?
127           query[k] = v
128         elsif v == true
129           query[k] = 1
130         elsif v == false
131           query[k] = 0
132         else
133           query[k] = Oj.dump(v, mode: :compat)
134         end
135       end
136     else
137       query["_method"] = "GET"
138     end
139
140     if @@profiling_enabled
141       query["_profile"] = "true"
142     end
143
144     headers = {
145       "Accept" => "application/json",
146       "Authorization" => "OAuth2 " +
147                          (tokens[:arvados_api_token] ||
148                           Thread.current[:arvados_api_token] ||
149                           ''),
150       "X-Request-Id" => Thread.current[:request_id] || '',
151     }
152
153     profile_checkpoint { "Prepare request #{query["_method"] or "POST"} #{url} #{query[:uuid]} #{query.inspect[0,256]}" }
154     msg = @client_mtx.synchronize do
155       begin
156         @api_client.post(url, query, headers)
157       rescue => exception
158         raise NoApiResponseException.new(url, exception)
159       end
160     end
161     profile_checkpoint 'API transaction'
162     if @@profiling_enabled
163       if msg.headers['X-Runtime']
164         Rails.logger.info "API server: #{msg.headers['X-Runtime']} runtime reported"
165       end
166       Rails.logger.info "Content-Encoding #{msg.headers['Content-Encoding'].inspect}, Content-Length #{msg.headers['Content-Length'].inspect}, actual content size #{msg.content.size}"
167     end
168
169     begin
170       resp = Oj.strict_load(msg.content, :symbol_keys => true)
171     rescue Oj::ParseError
172       resp = nil
173     end
174
175     if not resp.is_a? Hash
176       raise InvalidApiResponseException.new(url, msg)
177     elsif msg.status_code != 200
178       error_class = ERROR_CODE_CLASSES.fetch(msg.status_code,
179                                              ApiErrorResponseException)
180       raise error_class.new(url, msg)
181     end
182
183     if resp[:_profile]
184       Rails.logger.info "API client: " \
185       "#{resp.delete(:_profile)[:request_time]} request_time"
186     end
187     profile_checkpoint 'Parse response'
188     resp
189   end
190
191   def self.patch_paging_vars(ary, items_available, offset, limit, links=nil)
192     if items_available
193       (class << ary; self; end).class_eval { attr_accessor :items_available }
194       ary.items_available = items_available
195     end
196     if offset
197       (class << ary; self; end).class_eval { attr_accessor :offset }
198       ary.offset = offset
199     end
200     if limit
201       (class << ary; self; end).class_eval { attr_accessor :limit }
202       ary.limit = limit
203     end
204     if links
205       (class << ary; self; end).class_eval { attr_accessor :links }
206       ary.links = links
207     end
208     ary
209   end
210
211   def unpack_api_response(j, kind=nil)
212     if j.is_a? Hash and j[:items].is_a? Array and j[:kind].match(/(_list|List)$/)
213       ary = j[:items].collect { |x| unpack_api_response x, x[:kind] }
214       links = ArvadosResourceList.new Link
215       links.results = (j[:links] || []).collect do |x|
216         unpack_api_response x, x[:kind]
217       end
218       self.class.patch_paging_vars(ary, j[:items_available], j[:offset], j[:limit], links)
219     elsif j.is_a? Hash and (kind || j[:kind])
220       oclass = self.kind_class(kind || j[:kind])
221       if oclass
222         j.keys.each do |k|
223           childkind = j["#{k.to_s}_kind".to_sym]
224           if childkind
225             j[k] = self.unpack_api_response(j[k], childkind)
226           end
227         end
228         oclass.new.private_reload(j)
229       else
230         j
231       end
232     else
233       j
234     end
235   end
236
237   def arvados_login_url(params={})
238     if Rails.configuration.testing_override_login_url
239       uri = URI(Rails.configuration.testing_override_login_url)
240       uri.path = "/login"
241       uri.query = URI.encode_www_form(params)
242       return uri.to_s
243     end
244
245     case
246     when Rails.configuration.Login.PAM.Enable,
247          Rails.configuration.Login.LDAP.Enable,
248          Rails.configuration.Login.Test.Enable
249
250       uri = URI.parse(Rails.configuration.Services.Workbench1.ExternalURL.to_s)
251       uri.path = "/users/welcome"
252       uri.query = URI.encode_www_form(params)
253     else
254       uri = URI.parse(Rails.configuration.Services.Controller.ExternalURL.to_s)
255       uri.path = "/login"
256       uri.query = URI.encode_www_form(params)
257     end
258     uri.to_s
259   end
260
261   def arvados_logout_url(params={})
262     uri = URI.parse(Rails.configuration.Services.Controller.ExternalURL.to_s)
263     if Rails.configuration.testing_override_login_url
264       uri = URI(Rails.configuration.testing_override_login_url)
265     end
266     uri.path = "/logout"
267     uri.query = URI.encode_www_form(params)
268     uri.to_s
269   end
270
271   def arvados_v1_base
272     # workaround Ruby 2.3 bug, can't duplicate URI objects
273     # https://github.com/httprb/http/issues/388
274     u = URI.parse(Rails.configuration.Services.Controller.ExternalURL.to_s)
275     u.path = "/arvados/v1"
276     u.to_s
277   end
278
279   def discovery
280     @@discovery ||= api '../../discovery/v1/apis/arvados/v1/rest', ''
281   end
282
283   def kind_class(kind)
284     kind.match(/^arvados\#(.+?)(_list|List)?$/)[1].pluralize.classify.constantize rescue nil
285   end
286
287   def class_kind(resource_class)
288     resource_class.to_s.underscore
289   end
290
291   def self.class_kind(resource_class)
292     resource_class.to_s.underscore
293   end
294
295   protected
296   def profile_checkpoint label=nil
297     return if !@@profiling_enabled
298     label = yield if block_given?
299     t = Time.now
300     if label and @profile_t0
301       Rails.logger.info "API client: #{t - @profile_t0} #{label}"
302     end
303     @profile_t0 = t
304   end
305 end