X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/b28565c8aa08cbf70762fa69e49c5067fcb57e96..ee1b698d98149561c58c7d8c7206d8d4f4dc0bb5:/services/api/app/controllers/arvados/v1/collections_controller.rb diff --git a/services/api/app/controllers/arvados/v1/collections_controller.rb b/services/api/app/controllers/arvados/v1/collections_controller.rb index 6c9d41e3f1..b65fa5b962 100644 --- a/services/api/app/controllers/arvados/v1/collections_controller.rb +++ b/services/api/app/controllers/arvados/v1/collections_controller.rb @@ -13,7 +13,6 @@ class Arvados::V1::CollectionsController < ApplicationController # Check permissions on the collection manifest. # If any signature cannot be verified, return 403 Permission denied. - perms_ok = true api_token = current_api_client_authorization.andand.api_token signing_opts = { key: Rails.configuration.blob_signing_key, @@ -22,35 +21,33 @@ class Arvados::V1::CollectionsController < ApplicationController } resource_attrs[:manifest_text].lines.each do |entry| entry.split[1..-1].each do |tok| - # TODO(twp): in Phase 4, fail the request if the locator - # lacks a permission signature. (see #2755) - loc = Locator.parse(tok) - if loc and loc.signature - if !api_token - logger.warn "No API token present; cannot verify signature on #{loc}" - perms_ok = false - elsif !Blob.verify_signature tok, signing_opts - logger.warn "Invalid signature on locator #{loc}" - perms_ok = false - end + if /^[[:digit:]]+:[[:digit:]]+:/.match tok + # 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 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 end - unless perms_ok - raise ArvadosModel::PermissionDeniedError - end # Remove any permission signatures from the manifest. - resource_attrs[:manifest_text] - .gsub!(/ [[:xdigit:]]{32}(\+[[:digit:]]+)?(\+\S+)/) { |word| - word.strip! - loc = Locator.parse(word) - if loc - " " + loc.without_signature.to_s - else - " " + word - end - } + munge_manifest_locators(resource_attrs[:manifest_text]) do |loc| + loc.without_signature.to_s + end # Save the collection with the stripped manifest. act_as_system_user do @@ -59,11 +56,11 @@ class Arvados::V1::CollectionsController < ApplicationController @object.save! rescue ActiveRecord::RecordNotUnique logger.debug resource_attrs.inspect - if resource_attrs[:manifest_text] and resource_attrs[:uuid] + if @object.manifest_text and @object.uuid @existing_object = model_class. where('uuid=? and manifest_text=?', - resource_attrs[:uuid], - resource_attrs[:manifest_text]). + @object.uuid, + @object.manifest_text). first @object = @existing_object || @object end @@ -87,24 +84,13 @@ class Arvados::V1::CollectionsController < ApplicationController end def show - if current_api_client_authorization - signing_opts = { - key: Rails.configuration.blob_signing_key, - api_token: current_api_client_authorization.api_token, - ttl: Rails.configuration.blob_signing_ttl, - } - @object[:manifest_text] - .gsub!(/ [[:xdigit:]]{32}(\+[[:digit:]]+)?(\+\S+)/) { |word| - word.strip! - loc = Locator.parse(word) - if loc - " " + Blob.sign_locator(word, signing_opts) - else - " " + word - end - } - end - render json: @object.as_api_response(:with_data) + sign_manifests(@object[:manifest_text]) + super + end + + def index + sign_manifests(*@objects.map { |c| c[:manifest_text] }) + super end def collection_uuid(uuid) @@ -145,7 +131,7 @@ class Arvados::V1::CollectionsController < ApplicationController logger.debug "visiting #{uuid}" - if m + if m # uuid is a collection Collection.readable_by(current_user).where(uuid: uuid).each do |c| visited[uuid] = c.as_api_response @@ -162,7 +148,7 @@ class Arvados::V1::CollectionsController < ApplicationController Job.readable_by(current_user).where(log: uuid).each do |job| generate_provenance_edges(visited, job.uuid) end - + else # uuid is something else rsc = ArvadosModel::resource_class_for_uuid uuid @@ -204,7 +190,7 @@ class Arvados::V1::CollectionsController < ApplicationController logger.debug "visiting #{uuid}" - if m + if m # uuid is a collection Collection.readable_by(current_user).where(uuid: uuid).each do |c| visited[uuid] = c.as_api_response @@ -222,7 +208,7 @@ class Arvados::V1::CollectionsController < ApplicationController Job.readable_by(current_user).where(["jobs.script_parameters like ?", "%#{uuid}%"]).each do |job| generate_used_by_edges(visited, job.uuid) end - + else # uuid is something else rsc = ArvadosModel::resource_class_for_uuid uuid @@ -254,7 +240,27 @@ class Arvados::V1::CollectionsController < ApplicationController render json: visited end + def self.munge_manifest_locators(manifest) + # Given a manifest text and a block, yield each locator, + # and replace it with whatever the block returns. + manifest.andand.gsub!(/ [[:xdigit:]]{32}(\+[[:digit:]]+)?(\+\S+)/) do |word| + if loc = Locator.parse(word.strip) + " " + yield(loc) + else + " " + word + end + end + end + protected + + def find_objects_for_index + # Omit manifest_text from index results unless expressly selected. + @select ||= model_class.api_accessible_attributes(:user). + map { |attr_spec| attr_spec.first.to_s } - ["manifest_text"] + super + end + def find_object_by_uuid super if !@object and !params[:uuid].match(/^[0-9a-f]+\+\d+$/) @@ -273,4 +279,23 @@ class Arvados::V1::CollectionsController < ApplicationController end end end + + def munge_manifest_locators(manifest, &block) + self.class.munge_manifest_locators(manifest, &block) + end + + def sign_manifests(*manifests) + if current_api_client_authorization + signing_opts = { + key: Rails.configuration.blob_signing_key, + api_token: current_api_client_authorization.api_token, + ttl: Rails.configuration.blob_signing_ttl, + } + manifests.each do |text| + munge_manifest_locators(text) do |loc| + Blob.sign_locator(loc.to_s, signing_opts) + end + end + end + end end