+ def self.attributes_required_columns
+ super.merge(
+ # If we don't list manifest_text explicitly, the
+ # params[:select] code gets confused by the way we
+ # expose signed_manifest_text as manifest_text in the
+ # API response, and never let clients select the
+ # manifest_text column.
+ #
+ # We need trash_at and is_trashed to determine the
+ # correct timestamp in signed_manifest_text.
+ 'manifest_text' => ['manifest_text', 'trash_at', 'is_trashed'],
+ 'unsigned_manifest_text' => ['manifest_text'],
+ )
+ end
+
+ def self.ignored_select_attributes
+ super + ["updated_at", "file_names"]
+ end
+
+ FILE_TOKEN = /^[[:digit:]]+:[[:digit:]]+:/
+ def check_signatures
+ return false if self.manifest_text.nil?
+
+ return true if current_user.andand.is_admin
+
+ # Provided the manifest_text hasn't changed materially since an
+ # earlier validation, it's safe to pass this validation on
+ # subsequent passes without checking any signatures. This is
+ # important because the signatures have probably been stripped off
+ # by the time we get to a second validation pass!
+ if @signatures_checked && @signatures_checked == computed_pdh
+ return true
+ end
+
+ if self.manifest_text_changed?
+ # Check permissions on the collection manifest.
+ # If any signature cannot be verified, raise PermissionDeniedError
+ # which will return 403 Permission denied to the client.
+ api_token = current_api_client_authorization.andand.api_token
+ signing_opts = {
+ api_token: api_token,
+ now: @validation_timestamp.to_i,
+ }
+ self.manifest_text.each_line do |entry|
+ entry.split.each do |tok|
+ if tok == '.' or tok.starts_with? './'
+ # Stream name token.
+ elsif tok =~ FILE_TOKEN
+ # This is a filename token, not a blob locator. Note that we
+ # keep checking tokens after this, even though manifest
+ # format dictates that all subsequent tokens will also be
+ # filenames. Safety first!
+ elsif Blob.verify_signature tok, signing_opts
+ # OK.
+ elsif Keep::Locator.parse(tok).andand.signature
+ # Signature provided, but verify_signature did not like it.
+ logger.warn "Invalid signature on locator #{tok}"
+ raise ArvadosModel::PermissionDeniedError
+ elsif Rails.configuration.permit_create_collection_with_unsigned_manifest
+ # No signature provided, but we are running in insecure mode.
+ logger.debug "Missing signature on locator #{tok} ignored"
+ elsif Blob.new(tok).empty?
+ # No signature provided -- but no data to protect, either.
+ else
+ logger.warn "Missing signature on locator #{tok}"
+ raise ArvadosModel::PermissionDeniedError
+ end
+ end