|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "test_helper" |
| 4 | +require "securerandom" |
| 5 | + |
| 6 | +class TestUserDelegationKeyExpiration < TestCase |
| 7 | + attr_reader :client, :key, :content |
| 8 | + |
| 9 | + EXPIRATION = 120 |
| 10 | + |
| 11 | + def setup |
| 12 | + skip if using_shared_key |
| 13 | + @account_name = ENV["AZURE_ACCOUNT_NAME"] |
| 14 | + @container = ENV["AZURE_PRIVATE_CONTAINER"] |
| 15 | + @principal_id = ENV["AZURE_PRINCIPAL_ID"] |
| 16 | + @host = ENV["STORAGE_BLOB_HOST"] |
| 17 | + @client = AzureBlob::Client.new( |
| 18 | + account_name: @account_name, |
| 19 | + container: @container, |
| 20 | + principal_id: @principal_id, |
| 21 | + host: @host, |
| 22 | + ) |
| 23 | + @uid = SecureRandom.uuid |
| 24 | + @key = "test-delegation-expiration-#{@uid}" |
| 25 | + @content = "Test content for delegation key expiration" |
| 26 | + end |
| 27 | + |
| 28 | + def teardown |
| 29 | + client.delete_blob(key) rescue AzureBlob::Http::FileNotFoundError |
| 30 | + end |
| 31 | + |
| 32 | + def test_user_delegation_key_auto_refresh_on_expiration |
| 33 | + original_expiration = AzureBlob::UserDelegationKey.send(:remove_const, :EXPIRATION) |
| 34 | + original_buffer = AzureBlob::UserDelegationKey.send(:remove_const, :EXPIRATION_BUFFER) |
| 35 | + AzureBlob::UserDelegationKey.const_set(:EXPIRATION, 2) |
| 36 | + AzureBlob::UserDelegationKey.const_set(:EXPIRATION_BUFFER, 0) |
| 37 | + |
| 38 | + begin |
| 39 | + client.create_block_blob(key, content) |
| 40 | + |
| 41 | + uri = client.signed_uri( |
| 42 | + key, |
| 43 | + permissions: "r", |
| 44 | + expiry: Time.at(Time.now.to_i + EXPIRATION).utc.iso8601, |
| 45 | + ) |
| 46 | + |
| 47 | + response = AzureBlob::Http.new(uri, { "x-ms-blob-type": "BlockBlob" }).get |
| 48 | + |
| 49 | + assert_equal response, content |
| 50 | + |
| 51 | + sleep 3 |
| 52 | + |
| 53 | + uri = client.signed_uri( |
| 54 | + key, |
| 55 | + permissions: "r", |
| 56 | + expiry: Time.at(Time.now.to_i + EXPIRATION).utc.iso8601, |
| 57 | + ) |
| 58 | + |
| 59 | + response = AzureBlob::Http.new(uri, { "x-ms-blob-type": "BlockBlob" }).get |
| 60 | + |
| 61 | + assert_equal response, content |
| 62 | + ensure |
| 63 | + AzureBlob::UserDelegationKey.send(:remove_const, :EXPIRATION) |
| 64 | + AzureBlob::UserDelegationKey.send(:remove_const, :EXPIRATION_BUFFER) |
| 65 | + AzureBlob::UserDelegationKey.const_set(:EXPIRATION, original_expiration) |
| 66 | + AzureBlob::UserDelegationKey.const_set(:EXPIRATION_BUFFER, original_buffer) |
| 67 | + end |
| 68 | + end |
| 69 | +end |
0 commit comments