Skip to content

Commit

Permalink
Merge pull request #7 from lucaong/lazy-signer-instantiation
Browse files Browse the repository at this point in the history
Initialize signer lazily
  • Loading branch information
JoeDupuis authored Dec 10, 2024
2 parents 8be6834 + 4d5b925 commit 7a672f3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [Unreleased]

- Allow lazy loading the signer

## [0.5.4] 2024-11-18

- Allow creating public container
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ client.delete_blob(path)

For the full list of methods: https://www.rubydoc.info/gems/azure-blob/AzureBlob/Client

## options

### Lazy loading

The client is configured to raise an error early for missing credentials, causing it to crash before becoming healthy. This behavior can sometimes be undesirable, such as during assets precompilation.

To enable lazy loading and ignore missing credentials, set the `lazy` option:

`AzureBlob::Client.new(account_name: nil, access_key: nil, container: nil, lazy: true)`

or add `lazy: true` to your `config/storage.yml` for Active Storage.


## Contributing

### Dev environment
Expand Down
36 changes: 23 additions & 13 deletions lib/azure_blob/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,10 @@ def initialize(account_name:, access_key: nil, principal_id: nil, container:, ho
@container = container
@host = host
@cloud_regions = options[:cloud_regions]&.to_sym || :global

no_access_key = access_key.nil? || access_key&.empty?
using_managed_identities = no_access_key && !principal_id.nil? || options[:use_managed_identities]

if !using_managed_identities && no_access_key
raise AzureBlob::Error.new(
"`access_key` cannot be empty. To use managed identities instead, pass a `principal_id` or set `use_managed_identities` to true."
)
end
@signer = using_managed_identities ?
AzureBlob::EntraIdSigner.new(account_name:, host: self.host, principal_id:) :
AzureBlob::SharedKeySigner.new(account_name:, access_key:, host: self.host)
@access_key = access_key
@principal_id = principal_id
@use_managed_identities = options[:use_managed_identities]
signer unless options[:lazy]
end

# Create a blob of type block. Will automatically split the the blob in multiple block and send the blob in pieces (blocks) if the blob is too big.
Expand Down Expand Up @@ -364,6 +356,24 @@ def host
@host ||= "https://#{account_name}.blob.#{CLOUD_REGIONS_SUFFIX[cloud_regions]}"
end

attr_reader :account_name, :signer, :container, :http, :cloud_regions
def signer
@signer ||=
begin
no_access_key = access_key.nil? || access_key&.empty?
using_managed_identities = no_access_key && !principal_id.nil? || use_managed_identities

if !using_managed_identities && no_access_key
raise AzureBlob::Error.new(
"`access_key` cannot be empty. To use managed identities instead, pass a `principal_id` or set `use_managed_identities` to true."
)
end

using_managed_identities ?
AzureBlob::EntraIdSigner.new(account_name:, host:, principal_id:) :
AzureBlob::SharedKeySigner.new(account_name:, access_key:, host:)
end
end

attr_reader :account_name, :container, :http, :cloud_regions, :access_key, :principal_id, :use_managed_identities
end
end
12 changes: 12 additions & 0 deletions test/client/test_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ def test_without_credentials
)
end

def test_lazy_loading_doesnt_raise_before_querying
client = AzureBlob::Client.new(
account_name: @account_name,
container: @container,
lazy: true,
)

assert_raises(AzureBlob::Error) do
client.create_block_blob(key, content)
end
end

def test_single_block_upload
client.create_block_blob(key, content)

Expand Down

0 comments on commit 7a672f3

Please sign in to comment.