3036: Assign collection uuid without complaint if not provided by client.
[arvados.git] / services / api / app / controllers / arvados / v1 / collections_controller.rb
index 4d0d0045084df882da519bd383d0f902909a91ae..a0c64aa6e6adc4ad30dc4094681a45d3ce597ecb 100644 (file)
@@ -13,30 +13,48 @@ 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.permission_key, api_token: api_token }
+    signing_opts = {
+      key: Rails.configuration.blob_signing_key,
+      api_token: api_token,
+      ttl: Rails.configuration.blob_signing_ttl,
+    }
     resource_attrs[:manifest_text].lines.each do |entry|
-      # TODO(twp): fail the request if this match fails.
-      # Add in Phase 4 (see #2755)
-      m = /([[:xdigit:]]{32}(\+[[:digit:]]+)?)(\+A\S*)?/.match(entry)
-      if m and m[3]
-        if !api_token
-          logger.warn "No API token present; cannot verify signature #{m[0]}"
-          perms_ok = false
-        elsif !Blob.verify_signature m[0], signing_opts
-          logger.warn "Invalid signature on locator #{m[0]}"
-          perms_ok = false
+      entry.split[1..-1].each do |tok|
+        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!(/^(\S+\s+)([[:xdigit:]]{32}(\+[[:digit:]]+)?)(\+A\S*)/, '\1\2')
+      .gsub!(/ [[:xdigit:]]{32}(\+[[:digit:]]+)?(\+\S+)/) { |word|
+      word.strip!
+      loc = Locator.parse(word)
+      if loc
+        " " + loc.without_signature.to_s
+      else
+        " " + word
+      end
+    }
 
     # Save the collection with the stripped manifest.
     act_as_system_user do
@@ -45,16 +63,15 @@ 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
       end
-
       if @object
         link_attrs = {
           owner_uuid: owner_uuid,
@@ -76,12 +93,19 @@ class Arvados::V1::CollectionsController < ApplicationController
   def show
     if current_api_client_authorization
       signing_opts = {
-        key: Rails.configuration.permission_key,
+        key: Rails.configuration.blob_signing_key,
         api_token: current_api_client_authorization.api_token,
+        ttl: Rails.configuration.blob_signing_ttl,
       }
       @object[:manifest_text]
-        .gsub!(/^(\S+\s+)([[:xdigit:]]{32}(\+[[:digit:]]+)?)/) { |m|
-        $1 + Blob.sign_locator($2, signing_opts)
+        .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)
@@ -253,5 +277,4 @@ class Arvados::V1::CollectionsController < ApplicationController
       end
     end
   end
-
 end