1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
6 require 'record_filters'
9 class ArvadosModel < ActiveRecord::Base
10 self.abstract_class = true
12 include CurrentApiClient # current_user, current_api_client, etc.
16 after_initialize :log_start_state
17 before_save :ensure_permission_to_save
18 before_save :ensure_owner_uuid_is_permitted
19 before_save :ensure_ownership_path_leads_to_user
20 before_destroy :ensure_owner_uuid_is_permitted
21 before_destroy :ensure_permission_to_destroy
22 before_create :update_modified_by_fields
23 before_update :maybe_update_modified_by_fields
24 after_create :log_create
25 after_update :log_update
26 after_destroy :log_destroy
27 after_find :convert_serialized_symbols_to_strings
28 before_validation :normalize_collection_uuids
29 before_validation :set_default_owner
30 validate :ensure_valid_uuids
32 # Note: This only returns permission links. It does not account for
33 # permissions obtained via user.is_admin or
34 # user.uuid==object.owner_uuid.
35 has_many(:permissions,
36 ->{where(link_class: 'permission')},
37 foreign_key: :head_uuid,
41 class PermissionDeniedError < StandardError
47 class AlreadyLockedError < StandardError
53 class LockFailedError < StandardError
59 class InvalidStateTransitionError < StandardError
65 class UnauthorizedError < StandardError
71 class UnresolvableContainerError < StandardError
77 def self.kind_class(kind)
78 kind.match(/^arvados\#(.+)$/)[1].classify.safe_constantize rescue nil
82 "#{current_api_base}/#{self.class.to_s.pluralize.underscore}/#{self.uuid}"
85 def self.permit_attribute_params raw_params
86 # strong_parameters does not provide security: permissions are
87 # implemented with before_save hooks.
89 # The following permit! is necessary even with
90 # "ActionController::Parameters.permit_all_parameters = true",
91 # because permit_all does not permit nested attributes.
93 serialized_attributes.each do |colname, coder|
94 param = raw_params[colname.to_sym]
97 elsif !param.is_a?(coder.object_class)
98 raise ArgumentError.new("#{colname} parameter must be #{coder.object_class}, not #{param.class}")
99 elsif has_nonstring_keys?(param)
100 raise ArgumentError.new("#{colname} parameter cannot have non-string hash keys")
104 ActionController::Parameters.new(raw_params).permit!
107 def initialize raw_params={}, *args
108 super(self.class.permit_attribute_params(raw_params), *args)
111 # Reload "old attributes" for logging, too.
117 def self.create raw_params={}, *args
118 super(permit_attribute_params(raw_params), *args)
121 def update_attributes raw_params={}, *args
122 super(self.class.permit_attribute_params(raw_params), *args)
125 def self.selectable_attributes(template=:user)
126 # Return an array of attribute name strings that can be selected
127 # in the given template.
128 api_accessible_attributes(template).map { |attr_spec| attr_spec.first.to_s }
131 def self.searchable_columns operator
132 textonly_operator = !operator.match(/[<=>]/)
133 self.columns.select do |col|
137 when :datetime, :integer, :boolean
145 def self.attribute_column attr
146 self.columns.select { |col| col.name == attr.to_s }.first
149 def self.attributes_required_columns
150 # This method returns a hash. Each key is the name of an API attribute,
151 # and it's mapped to a list of database columns that must be fetched
152 # to generate that attribute.
153 # This implementation generates a simple map of attributes to
154 # matching column names. Subclasses can override this method
155 # to specify that method-backed API attributes need to fetch
156 # specific columns from the database.
157 all_columns = columns.map(&:name)
158 api_column_map = Hash.new { |hash, key| hash[key] = [] }
159 methods.grep(/^api_accessible_\w+$/).each do |method_name|
160 next if method_name == :api_accessible_attributes
161 send(method_name).each_pair do |api_attr_name, col_name|
162 col_name = col_name.to_s
163 if all_columns.include?(col_name)
164 api_column_map[api_attr_name.to_s] |= [col_name]
171 def self.ignored_select_attributes
172 ["href", "kind", "etag"]
175 def self.columns_for_attributes(select_attributes)
176 if select_attributes.empty?
177 raise ArgumentError.new("Attribute selection list cannot be empty")
179 api_column_map = attributes_required_columns
181 select_attributes.each do |s|
182 next if ignored_select_attributes.include? s
183 if not s.is_a? String or not api_column_map.include? s
187 if not invalid_attrs.empty?
188 raise ArgumentError.new("Invalid attribute(s): #{invalid_attrs.inspect}")
190 # Given an array of attribute names to select, return an array of column
191 # names that must be fetched from the database to satisfy the request.
192 select_attributes.flat_map { |attr| api_column_map[attr] }.uniq
195 def self.default_orders
196 ["#{table_name}.modified_at desc", "#{table_name}.uuid"]
199 def self.unique_columns
203 def self.limit_index_columns_read
204 # This method returns a list of column names.
205 # If an index request reads that column from the database,
206 # APIs that return lists will only fetch objects until reaching
207 # max_index_database_read bytes of data from those columns.
211 # If current user can manage the object, return an array of uuids of
212 # users and groups that have permission to write the object. The
213 # first two elements are always [self.owner_uuid, current user's
216 # If current user can write but not manage the object, return
217 # [self.owner_uuid, current user's uuid].
219 # If current user cannot write this object, just return
222 return [owner_uuid] if not current_user
223 unless (owner_uuid == current_user.uuid or
224 current_user.is_admin or
225 (current_user.groups_i_can(:manage) & [uuid, owner_uuid]).any?)
226 if ((current_user.groups_i_can(:write) + [current_user.uuid]) &
227 [uuid, owner_uuid]).any?
228 return [owner_uuid, current_user.uuid]
233 [owner_uuid, current_user.uuid] + permissions.collect do |p|
234 if ['can_write', 'can_manage'].index p.name
240 # Return a query with read permissions restricted to the union of of the
241 # permissions of the members of users_list, i.e. if something is readable by
242 # any user in users_list, it will be readable in the query returned by this
244 def self.readable_by(*users_list)
245 # Get rid of troublesome nils
248 # Load optional keyword arguments, if they exist.
249 if users_list.last.is_a? Hash
250 kwargs = users_list.pop
255 # Collect the UUIDs of the authorized users.
256 sql_table = kwargs.fetch(:table_name, table_name)
257 include_trashed = kwargs.fetch(:include_trashed, 0)
260 user_uuids = users_list.map { |u| u.uuid }
262 User.install_view('permission')
264 # Check if any of the users are admin.
265 if users_list.select { |u| u.is_admin }.any?
266 # For admins, only filter on "trashed"
267 # sql_conds += ["#{sql_table}.uuid in (SELECT target_uuid
268 # FROM permission_view
269 # WHERE trashed in (:include_trashed)
270 # GROUP BY user_uuid, target_uuid)"]
272 # if self.column_names.include? 'owner_uuid'
273 # sql_conds[0] += "AND #{sql_table}.owner_uuid in (SELECT target_uuid
274 # FROM permission_view
275 # WHERE trashed in (:include_trashed)
276 # GROUP BY user_uuid, target_uuid)"
280 # Match any object (evidently a group or user) whose UUID is
281 # listed explicitly in user_uuids.
282 sql_conds += ["#{sql_table}.uuid in (:user_uuids)"]
284 # Match any object whose owner is listed explicitly in
286 sql_conds += ["#{sql_table}.owner_uuid IN (:user_uuids)"]
288 # At least read permission from user_uuid to target_uuid of object
289 sql_conds += ["#{sql_table}.uuid in (SELECT target_uuid
291 WHERE user_uuid in (:user_uuids) and perm_level >= 1 and trashed = (:include_trashed)
292 GROUP BY user_uuid, target_uuid)"]
294 if self.column_names.include? 'owner_uuid'
295 # At least read permission from user_uuid to target_uuid that owns object
296 sql_conds += ["#{sql_table}.owner_uuid in (SELECT target_uuid
298 WHERE user_uuid in (:user_uuids) and
299 target_owner_uuid IS NOT NULL and
300 perm_level >= 1 and trashed = (:include_trashed)
301 GROUP BY user_uuid, target_uuid)"]
304 if sql_table == "links"
305 # Match any permission link that gives one of the authorized
306 # users some permission _or_ gives anyone else permission to
307 # view one of the authorized users.
308 sql_conds += ["(#{sql_table}.link_class in (:permission_link_classes) AND "+
309 "(#{sql_table}.head_uuid IN (:user_uuids) OR #{sql_table}.tail_uuid IN (:user_uuids)))"]
313 where(sql_conds.join(' OR '),
314 user_uuids: user_uuids,
315 permission_link_classes: ['permission', 'resources'],
316 include_trashed: include_trashed)
319 def save_with_unique_name!
324 conn = ActiveRecord::Base.connection
325 conn.exec_query 'SAVEPOINT save_with_unique_name'
328 rescue ActiveRecord::RecordNotUnique => rn
329 raise if max_retries == 0
332 conn.exec_query 'ROLLBACK TO SAVEPOINT save_with_unique_name'
334 # Dig into the error to determine if it is specifically calling out a
335 # (owner_uuid, name) uniqueness violation. In this specific case, and
336 # the client requested a unique name with ensure_unique_name==true,
337 # update the name field and try to save again. Loop as necessary to
338 # discover a unique name. It is necessary to handle name choosing at
339 # this level (as opposed to the client) to ensure that record creation
340 # never fails due to a race condition.
341 err = rn.original_exception
342 raise unless err.is_a?(PG::UniqueViolation)
344 # Unfortunately ActiveRecord doesn't abstract out any of the
345 # necessary information to figure out if this the error is actually
346 # the specific case where we want to apply the ensure_unique_name
347 # behavior, so the following code is specialized to Postgres.
348 detail = err.result.error_field(PG::Result::PG_DIAG_MESSAGE_DETAIL)
349 raise unless /^Key \(owner_uuid, name\)=\([a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{15}, .*?\) already exists\./.match detail
351 new_name = "#{name_was} (#{db_current_time.utc.iso8601(3)})"
353 # If the database is fast enough to do two attempts in the
354 # same millisecond, we need to wait to ensure we try a
355 # different timestamp on each attempt.
357 new_name = "#{name_was} (#{db_current_time.utc.iso8601(3)})"
360 self[:name] = new_name
361 self[:uuid] = nil if uuid_was.nil? && !uuid.nil?
362 conn.exec_query 'SAVEPOINT save_with_unique_name'
365 conn.exec_query 'RELEASE SAVEPOINT save_with_unique_name'
370 def logged_attributes
371 attributes.except(*Rails.configuration.unlogged_attributes)
374 def self.full_text_searchable_columns
375 self.columns.select do |col|
376 col.type == :string or col.type == :text
380 def self.full_text_tsvector
381 parts = full_text_searchable_columns.collect do |column|
382 "coalesce(#{column},'')"
384 "to_tsvector('english', #{parts.join(" || ' ' || ")})"
387 def self.apply_filters query, filters
388 ft = record_filters filters, self
389 if not ft[:cond_out].any?
392 query.where('(' + ft[:cond_out].join(') AND (') + ')',
398 def self.deep_sort_hash(x)
400 x.sort.collect do |k, v|
401 [k, deep_sort_hash(v)]
404 x.collect { |v| deep_sort_hash(v) }
410 def ensure_ownership_path_leads_to_user
411 if new_record? or owner_uuid_changed?
412 uuid_in_path = {owner_uuid => true, uuid => true}
414 while (owner_class = ArvadosModel::resource_class_for_uuid(x)) != User
417 # Test for cycles with the new version, not the DB contents
419 elsif !owner_class.respond_to? :find_by_uuid
420 raise ActiveRecord::RecordNotFound.new
422 x = owner_class.find_by_uuid(x).owner_uuid
424 rescue ActiveRecord::RecordNotFound => e
425 errors.add :owner_uuid, "is not owned by any user: #{e}"
430 errors.add :owner_uuid, "would create an ownership cycle"
432 errors.add :owner_uuid, "has an ownership cycle"
436 uuid_in_path[x] = true
442 def set_default_owner
443 if new_record? and current_user and respond_to? :owner_uuid=
444 self.owner_uuid ||= current_user.uuid
448 def ensure_owner_uuid_is_permitted
449 raise PermissionDeniedError if !current_user
451 if self.owner_uuid.nil?
452 errors.add :owner_uuid, "cannot be nil"
453 raise PermissionDeniedError
456 rsc_class = ArvadosModel::resource_class_for_uuid owner_uuid
457 unless rsc_class == User or rsc_class == Group
458 errors.add :owner_uuid, "must be set to User or Group"
459 raise PermissionDeniedError
462 if new_record? || owner_uuid_changed?
463 # Permission on owner_uuid_was is needed to move an existing
464 # object away from its previous owner (which implies permission
465 # to modify this object itself, so we don't need to check that
466 # separately). Permission on the new owner_uuid is also needed.
467 [['old', owner_uuid_was],
469 ].each do |which, check_uuid|
471 # old_owner_uuid is nil? New record, no need to check.
472 elsif !current_user.can?(write: check_uuid)
473 logger.warn "User #{current_user.uuid} tried to set ownership of #{self.class.to_s} #{self.uuid} but does not have permission to write #{which} owner_uuid #{check_uuid}"
474 errors.add :owner_uuid, "cannot be set or changed without write permission on #{which} owner"
475 raise PermissionDeniedError
479 # If the object already existed and we're not changing
480 # owner_uuid, we only need write permission on the object
482 if !current_user.can?(write: self.uuid)
483 logger.warn "User #{current_user.uuid} tried to modify #{self.class.to_s} #{self.uuid} without write permission"
484 errors.add :uuid, "is not writable"
485 raise PermissionDeniedError
492 def ensure_permission_to_save
493 unless (new_record? ? permission_to_create : permission_to_update)
494 raise PermissionDeniedError
498 def permission_to_create
499 current_user.andand.is_active
502 def permission_to_update
504 logger.warn "Anonymous user tried to update #{self.class.to_s} #{self.uuid_was}"
507 if !current_user.is_active
508 logger.warn "Inactive user #{current_user.uuid} tried to update #{self.class.to_s} #{self.uuid_was}"
511 return true if current_user.is_admin
512 if self.uuid_changed?
513 logger.warn "User #{current_user.uuid} tried to change uuid of #{self.class.to_s} #{self.uuid_was} to #{self.uuid}"
519 def ensure_permission_to_destroy
520 raise PermissionDeniedError unless permission_to_destroy
523 def permission_to_destroy
527 def maybe_update_modified_by_fields
528 update_modified_by_fields if self.changed? or self.new_record?
532 def update_modified_by_fields
533 current_time = db_current_time
534 self.created_at = created_at_was || current_time
535 self.updated_at = current_time
536 self.owner_uuid ||= current_default_owner if self.respond_to? :owner_uuid=
537 self.modified_at = current_time
538 self.modified_by_user_uuid = current_user ? current_user.uuid : nil
539 self.modified_by_client_uuid = current_api_client ? current_api_client.uuid : nil
543 def self.has_nonstring_keys? x
546 return true if !(k.is_a?(String) || k.is_a?(Symbol)) || has_nonstring_keys?(v)
550 return true if has_nonstring_keys?(v)
556 def self.has_symbols? x
559 return true if has_symbols?(k) or has_symbols?(v)
563 return true if has_symbols?(k)
568 return true if x.start_with?(':') && !x.start_with?('::')
573 def self.recursive_stringify x
575 Hash[x.collect do |k,v|
576 [recursive_stringify(k), recursive_stringify(v)]
580 recursive_stringify k
584 elsif x.is_a? String and x.start_with?(':') and !x.start_with?('::')
591 def self.where_serialized(colname, value)
593 # rails4 stores as null, rails3 stored as serialized [] or {}
594 sql = "#{colname.to_s} is null or #{colname.to_s} IN (?)"
597 sql = "#{colname.to_s} IN (?)"
598 sorted = deep_sort_hash(value)
600 where(sql, [sorted.to_yaml, SafeJSON.dump(sorted)])
604 Hash => HashSerializer,
605 Array => ArraySerializer,
608 def self.serialize(colname, type)
609 coder = Serializer[type]
610 @serialized_attributes ||= {}
611 @serialized_attributes[colname.to_s] = coder
612 super(colname, coder)
615 def self.serialized_attributes
616 @serialized_attributes ||= {}
619 def serialized_attributes
620 self.class.serialized_attributes
623 def convert_serialized_symbols_to_strings
624 # ensure_serialized_attribute_type should prevent symbols from
625 # getting into the database in the first place. If someone managed
626 # to get them into the database (perhaps using an older version)
627 # we'll convert symbols to strings when loading from the
628 # database. (Otherwise, loading and saving an object with existing
629 # symbols in a serialized field will crash.)
630 self.class.serialized_attributes.each do |colname, attr|
631 if self.class.has_symbols? attributes[colname]
632 attributes[colname] = self.class.recursive_stringify attributes[colname]
634 self.class.recursive_stringify(attributes[colname]))
639 def foreign_key_attributes
640 attributes.keys.select { |a| a.match(/_uuid$/) }
643 def skip_uuid_read_permission_check
644 %w(modified_by_client_uuid)
647 def skip_uuid_existence_check
651 def normalize_collection_uuids
652 foreign_key_attributes.each do |attr|
653 attr_value = send attr
654 if attr_value.is_a? String and
655 attr_value.match(/^[0-9a-f]{32,}(\+[@\w]+)*$/)
657 send "#{attr}=", Collection.normalize_uuid(attr_value)
659 # TODO: abort instead of silently accepting unnormalizable value?
665 @@prefixes_hash = nil
666 def self.uuid_prefixes
667 unless @@prefixes_hash
669 Rails.application.eager_load!
670 ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |k|
671 if k.respond_to?(:uuid_prefix)
672 @@prefixes_hash[k.uuid_prefix] = k
679 def self.uuid_like_pattern
680 "#{Rails.configuration.uuid_prefix}-#{uuid_prefix}-_______________"
684 %r/[a-z0-9]{5}-#{uuid_prefix}-[a-z0-9]{15}/
687 def ensure_valid_uuids
688 specials = [system_user_uuid]
690 foreign_key_attributes.each do |attr|
691 if new_record? or send (attr + "_changed?")
692 next if skip_uuid_existence_check.include? attr
693 attr_value = send attr
694 next if specials.include? attr_value
696 if (r = ArvadosModel::resource_class_for_uuid attr_value)
697 unless skip_uuid_read_permission_check.include? attr
698 r = r.readable_by(current_user)
700 if r.where(uuid: attr_value).count == 0
701 errors.add(attr, "'#{attr_value}' not found")
718 def self.readable_by (*u)
723 [{:uuid => u[:uuid]}]
727 def self.resource_class_for_uuid(uuid)
728 if uuid.is_a? ArvadosModel
731 unless uuid.is_a? String
735 uuid.match HasUuid::UUID_REGEX do |re|
736 return uuid_prefixes[re[1]] if uuid_prefixes[re[1]]
739 if uuid.match(/.+@.+/)
746 # ArvadosModel.find_by_uuid needs extra magic to allow it to return
747 # an object in any class.
748 def self.find_by_uuid uuid
749 if self == ArvadosModel
750 # If called directly as ArvadosModel.find_by_uuid rather than via subclass,
751 # delegate to the appropriate subclass based on the given uuid.
752 self.resource_class_for_uuid(uuid).unscoped.find_by_uuid(uuid)
759 @old_attributes = Marshal.load(Marshal.dump(attributes))
760 @old_logged_attributes = Marshal.load(Marshal.dump(logged_attributes))
763 def log_change(event_type)
764 log = Log.new(event_type: event_type).fill_object(self)
771 log_change('create') do |log|
772 log.fill_properties('old', nil, nil)
778 log_change('update') do |log|
779 log.fill_properties('old', etag(@old_attributes), @old_logged_attributes)
785 log_change('delete') do |log|
786 log.fill_properties('old', etag(@old_attributes), @old_logged_attributes)