Merge branch '11016-doc-signing-ttl'
[arvados.git] / services / api / app / models / keep_service.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class KeepService < ArvadosModel
6   include HasUuid
7   include KindAndEtag
8   include CommonApiTemplate
9   extend DbCurrentTime
10
11   SERVER_START_TIME = db_current_time
12
13   api_accessible :user, extend: :common do |t|
14     t.add  :service_host
15     t.add  :service_port
16     t.add  :service_ssl_flag
17     t.add  :service_type
18     t.add  :read_only
19   end
20   api_accessible :superuser, :extend => :user do |t|
21   end
22
23   # return the set of keep services from the database (if this is an
24   # older installation or test system where entries have been added
25   # manually) or, preferably, the cluster config file.
26   def self.all *args
27     if super.count == 0
28       from_config
29     else
30       super
31     end
32   end
33
34   def self.where *args
35     all.where *args
36   end
37
38   protected
39
40   def permission_to_create
41     current_user.andand.is_admin
42   end
43
44   def permission_to_update
45     current_user.andand.is_admin
46   end
47
48   def self.from_config
49     config_time = connection.quote(SERVER_START_TIME)
50     owner = connection.quote(system_user_uuid)
51     values = []
52     id = 1
53     Rails.configuration.Services.Keepstore.InternalURLs.each do |url, info|
54       values << "(#{id}, " + quoted_column_values_from_url(url: url.to_s, rendezvous: info.Rendezvous).join(", ") + ", 'disk', 'f'::bool, #{config_time}, #{config_time}, #{owner}, #{owner}, null)"
55       id += 1
56     end
57     url = Rails.configuration.Services.Keepproxy.ExternalURL.to_s
58     if !url.blank?
59       values << "(#{id}, " + quoted_column_values_from_url(url: url, rendezvous: "").join(", ") + ", 'proxy', 'f'::bool, #{config_time}, #{config_time}, #{owner}, #{owner}, null)"
60       id += 1
61     end
62     if values.length == 0
63       # return empty set as AR relation
64       return unscoped.where('1=0')
65     else
66       sql = "(values #{values.join(", ")}) as keep_services (id, uuid, service_host, service_port, service_ssl_flag, service_type, read_only, created_at, modified_at, owner_uuid, modified_by_user_uuid, modified_by_client_uuid)"
67       return unscoped.from(sql)
68     end
69   end
70
71   private
72
73   def self.quoted_column_values_from_url(url:, rendezvous:)
74     rvz = rendezvous
75     rvz = url if rvz.blank?
76     if /^[a-zA-Z0-9]{15}$/ !~ rvz
77       # If rvz is an URL (either the real service URL, or an alternate
78       # one specified in config in order to preserve rendezvous order
79       # when changing hosts/ports), hash it to get 15 alphanums.
80       rvz = Digest::MD5.hexdigest(rvz)[0..15]
81     end
82     uuid = Rails.configuration.ClusterID + "-bi6l4-" + rvz
83     uri = URI::parse(url)
84     [uuid, uri.host, uri.port].map { |x| connection.quote(x) } + [(uri.scheme == 'https' ? "'t'::bool" : "'f'::bool")]
85   end
86
87 end