Skip to content

Commit 5308523

Browse files
committed
Error on incorect client arguments
1 parent 3f6e64a commit 5308523

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ AzureBlob supports managed identities on :
3434

3535
AKS support will likely require more work. Contributions are welcome.
3636

37-
To authenticate through managed identities instead of a shared key, omit `storage_access_key` from your `storage.yml` file.
38-
39-
It is recommended to add the identity's `principal_id` to the config.
37+
To authenticate through managed identities instead of a shared key, omit `storage_access_key` from your `storage.yml` file and pass in the identity `principal_id`.
4038

4139
ActiveStorage config example:
4240

lib/azure_blob/client.rb

+12-4
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@ module AzureBlob
1414
# AzureBlob Client class. You interact with the Azure Blob api
1515
# through an instance of this class.
1616
class Client
17-
def initialize(account_name:, access_key:, container:, **options)
17+
def initialize(account_name:, access_key: nil, principal_id: nil, container:, **options)
1818
@account_name = account_name
1919
@container = container
2020
@cloud_regions = options[:cloud_regions]&.to_sym || :global
2121

22-
@signer = !access_key.nil? && !access_key.empty? ?
23-
AzureBlob::SharedKeySigner.new(account_name:, access_key:) :
24-
AzureBlob::EntraIdSigner.new(account_name:, host:, **options.slice(:principal_id))
22+
no_access_key = access_key.nil? || access_key&.empty?
23+
using_managed_identities = no_access_key && !principal_id.nil? || options[:use_managed_identities]
24+
25+
if !using_managed_identities && no_access_key
26+
raise AzureBlob::Error.new(
27+
"`access_key` cannot be empty. To use managed identities instead, pass a `principal_id` or set `use_managed_identities` to true."
28+
)
29+
end
30+
@signer = using_managed_identities ?
31+
AzureBlob::EntraIdSigner.new(account_name:, host:, principal_id: ) :
32+
AzureBlob::SharedKeySigner.new(account_name:, access_key:)
2533
end
2634

2735
# 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.

test/client/test_client.rb

+32
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,38 @@ def test_rails_is_not_loaded
3232
assert_raises(NoMethodError) { 10.minutes }
3333
end
3434

35+
def test_without_credentials
36+
assert_raises(AzureBlob::Error) do
37+
AzureBlob::Client.new(
38+
account_name: @account_name,
39+
container: @container,
40+
)
41+
end
42+
43+
assert_raises(AzureBlob::Error) do
44+
AzureBlob::Client.new(
45+
access_key: "",
46+
account_name: @account_name,
47+
container: @container,
48+
)
49+
end
50+
51+
AzureBlob::Client.new(
52+
access_key: "",
53+
use_managed_identities: true,
54+
account_name: @account_name,
55+
container: @container,
56+
)
57+
58+
59+
AzureBlob::Client.new(
60+
access_key: "",
61+
principal_id: "123",
62+
account_name: @account_name,
63+
container: @container,
64+
)
65+
end
66+
3567
def test_single_block_upload
3668
client.create_block_blob(key, content)
3769

0 commit comments

Comments
 (0)