X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/091af67c8481567a9e6724df0a77b3fae13ee1ad..cea92754dfacf2b409d1f5b45dd0775fc44c842d:/services/api/test/functional/arvados/v1/collections_controller_test.rb diff --git a/services/api/test/functional/arvados/v1/collections_controller_test.rb b/services/api/test/functional/arvados/v1/collections_controller_test.rb index e5235c485d..ba6929be64 100644 --- a/services/api/test/functional/arvados/v1/collections_controller_test.rb +++ b/services/api/test/functional/arvados/v1/collections_controller_test.rb @@ -2,6 +2,25 @@ require 'test_helper' class Arvados::V1::CollectionsControllerTest < ActionController::TestCase + # StoppedClock.now always returns the same timestamp. + # Set the Blob permission signing clock to ensure that + # all permission hints use consistent timestamps for testing. + + class StoppedClock + @@cached_timestamp = Time.now + def self.now + return @@cached_timestamp + end + end + + def setup + Blob.set_clock(StoppedClock) + end + + def teardown + Blob.set_clock(Time) + end + test "should get index" do authorize_with :active get :index @@ -20,6 +39,17 @@ class Arvados::V1::CollectionsControllerTest < ActionController::TestCase end end + test "items.count == items_available" do + authorize_with :active + get :index, limit: 100000 + assert_response :success + resp = JSON.parse(@response.body) + assert_equal resp['items_available'], assigns(:objects).length + assert_equal resp['items_available'], resp['items'].count + unique_uuids = resp['items'].collect { |i| i['uuid'] }.compact.uniq + assert_equal unique_uuids.count, resp['items'].count + end + test "get index with limit=2 offset=99999" do # Assume there are not that many test fixtures. authorize_with :active @@ -166,9 +196,23 @@ EOS assert_response 422 end + test "collection UUID is normalized when created" do + authorize_with :active + post :create, { + collection: { + manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n", + uuid: "d30fe8ae534397864cb96c544f4cf102+47+Khint+Xhint+Zhint" + } + } + assert_response :success + assert_not_nil assigns(:object) + resp = JSON.parse(@response.body) + assert_equal "d30fe8ae534397864cb96c544f4cf102+47", resp['uuid'] + end + test "get full provenance for baz file" do authorize_with :active - get :provenance, uuid: 'ea10d51bcf88862dbcc36eb292017dfd+45' + get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45' assert_response :success resp = JSON.parse(@response.body) assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz @@ -181,14 +225,14 @@ EOS test "get no provenance for foo file" do # spectator user cannot even see baz collection authorize_with :spectator - get :provenance, uuid: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45' + get :provenance, id: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45' assert_response 404 end test "get partial provenance for baz file" do # spectator user can see bar->baz job, but not foo->bar job authorize_with :spectator - get :provenance, uuid: 'ea10d51bcf88862dbcc36eb292017dfd+45' + get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45' assert_response :success resp = JSON.parse(@response.body) assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz @@ -205,8 +249,224 @@ EOS } assert_response :success found = assigns(:objects).collect(&:uuid) - assert_equal 1, assigns(:objects).count + assert_equal 1, found.count assert_equal true, !!found.index('1f4b0bc7583c2a7f9102c395f4ffc5e3+45') end + test "create collection with signed manifest" do + authorize_with :active + locators = %w( + d41d8cd98f00b204e9800998ecf8427e+0 + acbd18db4cc2f85cedef654fccc4a4d8+3 + ea10d51bcf88862dbcc36eb292017dfd+45) + + unsigned_manifest = locators.map { |loc| + ". " + loc + " 0:0:foo.txt\n" + }.join() + manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) + + '+' + + unsigned_manifest.length.to_s + + # build a manifest with both signed and unsigned locators. + # TODO(twp): in phase 4, all locators will need to be signed, so + # this test should break and will need to be rewritten. Issue #2755. + signing_opts = { + key: Rails.configuration.blob_signing_key, + api_token: api_token(:active), + } + signed_manifest = + ". " + locators[0] + " 0:0:foo.txt\n" + + ". " + Blob.sign_locator(locators[1], signing_opts) + " 0:0:foo.txt\n" + + ". " + Blob.sign_locator(locators[2], signing_opts) + " 0:0:foo.txt\n" + + post :create, { + collection: { + manifest_text: signed_manifest, + uuid: manifest_uuid, + } + } + assert_response :success + assert_not_nil assigns(:object) + resp = JSON.parse(@response.body) + assert_equal manifest_uuid, resp['uuid'] + assert_equal 48, resp['data_size'] + # All of the locators in the output must be signed. + resp['manifest_text'].lines.each do |entry| + m = /([[:xdigit:]]{32}\+\S+)/.match(entry) + if m + assert Blob.verify_signature m[0], signing_opts + end + end + end + + test "create collection with signed manifest and explicit TTL" do + authorize_with :active + locators = %w( + d41d8cd98f00b204e9800998ecf8427e+0 + acbd18db4cc2f85cedef654fccc4a4d8+3 + ea10d51bcf88862dbcc36eb292017dfd+45) + + unsigned_manifest = locators.map { |loc| + ". " + loc + " 0:0:foo.txt\n" + }.join() + manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) + + '+' + + unsigned_manifest.length.to_s + + # build a manifest with both signed and unsigned locators. + # TODO(twp): in phase 4, all locators will need to be signed, so + # this test should break and will need to be rewritten. Issue #2755. + signing_opts = { + key: Rails.configuration.blob_signing_key, + api_token: api_token(:active), + ttl: 3600 # 1 hour + } + signed_manifest = + ". " + locators[0] + " 0:0:foo.txt\n" + + ". " + Blob.sign_locator(locators[1], signing_opts) + " 0:0:foo.txt\n" + + ". " + Blob.sign_locator(locators[2], signing_opts) + " 0:0:foo.txt\n" + + post :create, { + collection: { + manifest_text: signed_manifest, + uuid: manifest_uuid, + } + } + assert_response :success + assert_not_nil assigns(:object) + resp = JSON.parse(@response.body) + assert_equal manifest_uuid, resp['uuid'] + assert_equal 48, resp['data_size'] + # All of the locators in the output must be signed. + resp['manifest_text'].lines.each do |entry| + m = /([[:xdigit:]]{32}\+\S+)/.match(entry) + if m + assert Blob.verify_signature m[0], signing_opts + end + end + end + + test "create fails with invalid signature" do + authorize_with :active + signing_opts = { + key: Rails.configuration.blob_signing_key, + api_token: api_token(:active), + } + + # Generate a locator with a bad signature. + unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0" + bad_locator = unsigned_locator + "+Affffffff@ffffffff" + assert !Blob.verify_signature(bad_locator, signing_opts) + + # Creating a collection with this locator should + # produce 403 Permission denied. + unsigned_manifest = ". #{unsigned_locator} 0:0:foo.txt\n" + manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) + + '+' + + unsigned_manifest.length.to_s + + bad_manifest = ". #{bad_locator} 0:0:foo.txt\n" + post :create, { + collection: { + manifest_text: bad_manifest, + uuid: manifest_uuid + } + } + + assert_response 403 + end + + test "create fails with uuid of signed manifest" do + authorize_with :active + signing_opts = { + key: Rails.configuration.blob_signing_key, + api_token: api_token(:active), + } + + unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0" + signed_locator = Blob.sign_locator(unsigned_locator, signing_opts) + signed_manifest = ". #{signed_locator} 0:0:foo.txt\n" + manifest_uuid = Digest::MD5.hexdigest(signed_manifest) + + '+' + + signed_manifest.length.to_s + + post :create, { + collection: { + manifest_text: signed_manifest, + uuid: manifest_uuid + } + } + + assert_response 422 + end + + test "multiple locators per line" do + authorize_with :active + locators = %w( + d41d8cd98f00b204e9800998ecf8427e+0 + acbd18db4cc2f85cedef654fccc4a4d8+3 + ea10d51bcf88862dbcc36eb292017dfd+45) + + manifest_text = [".", *locators, "0:0:foo.txt\n"].join(" ") + manifest_uuid = Digest::MD5.hexdigest(manifest_text) + + '+' + + manifest_text.length.to_s + + post :create, { + collection: { + manifest_text: manifest_text, + uuid: manifest_uuid, + } + } + assert_response :success + assert_not_nil assigns(:object) + resp = JSON.parse(@response.body) + assert_equal manifest_uuid, resp['uuid'] + assert_equal 48, resp['data_size'] + assert_equal resp['manifest_text'], manifest_text + end + + test "multiple signed locators per line" do + authorize_with :active + locators = %w( + d41d8cd98f00b204e9800998ecf8427e+0 + acbd18db4cc2f85cedef654fccc4a4d8+3 + ea10d51bcf88862dbcc36eb292017dfd+45) + + signing_opts = { + key: Rails.configuration.blob_signing_key, + api_token: api_token(:active), + } + + unsigned_manifest = [".", *locators, "0:0:foo.txt\n"].join(" ") + manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) + + '+' + + unsigned_manifest.length.to_s + + signed_locators = locators.map { |loc| Blob.sign_locator loc, signing_opts } + signed_manifest = [".", *signed_locators, "0:0:foo.txt\n"].join(" ") + + post :create, { + collection: { + manifest_text: signed_manifest, + uuid: manifest_uuid, + } + } + assert_response :success + assert_not_nil assigns(:object) + resp = JSON.parse(@response.body) + assert_equal manifest_uuid, resp['uuid'] + assert_equal 48, resp['data_size'] + # All of the locators in the output must be signed. + # Each line is of the form "path locator locator ... 0:0:file.txt" + # entry.split[1..-2] will yield just the tokens in the middle of the line + returned_locator_count = 0 + resp['manifest_text'].lines.each do |entry| + entry.split[1..-2].each do |tok| + returned_locator_count += 1 + assert Blob.verify_signature tok, signing_opts + end + end + assert_equal locators.count, returned_locator_count + end end