Skip to content

Commit

Permalink
fixup! config overrides for SharedAccessSignature
Browse files Browse the repository at this point in the history
  • Loading branch information
noaccOS committed May 1, 2024
1 parent 64a55cf commit 19da24e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
22 changes: 15 additions & 7 deletions lib/azurex/blob/shared_access_signature.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ defmodule Azurex.Blob.SharedAccessSignature do
https://learn.microsoft.com/en-us/rest/api/storageservices/create-service-sas
"""

alias Azurex.Blob.Config

@doc """
Generates a SAS url on a resource in a given container.
Generates a SAS url on a resource.
## Params
- container: the storage container name
- overrides: use different configuration options for the azure connection. If the parameter is a string, it is treated as the container for backwards compatibility
- resource: the path to the resource (blob, container, directory...)
- opts: an optional keyword list with following options
- resource_type: one of :blob / :blob_version / :blob_snapshot / :container / directory
Expand All @@ -20,16 +22,22 @@ defmodule Azurex.Blob.SharedAccessSignature do
- expiry: a tuple to set how long before the SAS url expires. Defaults to `{:second, 3600}`.
## Examples
- `SharedAccessSignature.sas_url("/")`
- `SharedAccessSignature.sas_url([], "/", permissions: [:read, :write])`
- `SharedAccessSignature.sas_url("my_container", "/", permissions: [:write], expiry: {:day, 2})`
- `SharedAccessSignature.sas_url("my_container", "foo/song.mp3", resource_type: :blob)`
- `SharedAccessSignature.sas_url([storage_account_connection_string: "AccountName=name;AccountKey=key", container: "my_container"], "/")`
- `SharedAccessSignature.sas_url([storage_account_name: "name", storage_account_key: "key"], "bar/image.jpg", resource_type: :blob)`
"""
@spec sas_url(String.t(), String.t(), [{atom(), any()}]) :: String.t()
def sas_url(container, resource, opts \\ []) do
base_url = Azurex.Blob.Config.api_url()
@spec sas_url(Config.config_overrides(), String.t(), [{atom(), any()}]) :: String.t()
def sas_url(overrides \\ [], resource, opts \\ []) do
connection_params = Config.get_connection_params(overrides)
base_url = Config.api_url(connection_params)
resource_type = Keyword.get(opts, :resource_type, :container)
permissions = Keyword.get(opts, :permissions, [:read])
from = Keyword.get(opts, :from, DateTime.utc_now())
expiry = Keyword.get(opts, :expiry, {:second, 3600})
container = Keyword.get(connection_params, :container) || Config.default_container()
resource = Path.join(container, resource)

token =
Expand All @@ -38,8 +46,8 @@ defmodule Azurex.Blob.SharedAccessSignature do
resource,
{from, expiry},
permissions,
Azurex.Blob.Config.storage_account_name(),
Azurex.Blob.Config.storage_account_key()
Config.storage_account_name(connection_params),
Config.storage_account_key(connection_params)
)

"#{Path.join(base_url, resource)}?#{token}"
Expand Down
31 changes: 30 additions & 1 deletion test/azurex/blob/shared_access_signature_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Azurex.Blob.SharedAccessSignatureTest do
use ExUnit.Case
import Azurex.Blob.SharedAccessSignature

setup_all do
setup do
Application.put_env(:azurex, Azurex.Blob.Config,
storage_account_name: "storage_account",
storage_account_key: Base.encode64("secretkey")
Expand Down Expand Up @@ -31,7 +31,36 @@ defmodule Azurex.Blob.SharedAccessSignatureTest do
)
end

test "defaults to parameters from overrides" do
url_with_env = sas_url(container(), blob(), from: now())

connection_params = Keyword.put(delete_env(), :container, container())
url_with_overrides = sas_url(connection_params, blob(), from: now())

assert url_with_env == url_with_overrides
end

test "wihout overrides, operates on default container" do
env =
Application.get_env(:azurex, Azurex.Blob.Config)
|> Keyword.put(:default_container, container())

# Reapply environment but with default container
Application.put_env(:azurex, Azurex.Blob.Config, env)

assert sas_url([], "/", from: now()) == sas_url(container(), "/", from: now())
end

defp container, do: "my_container"
defp blob, do: "/folder/blob.mp4"
defp now, do: ~U[2022-10-10 10:10:00Z]

defp delete_env do
env = Application.get_env(:azurex, Azurex.Blob.Config)
container = env[:default_container]
Application.put_env(:azurex, Azurex.Blob.Config, [])

env
|> Keyword.put(:container, container)
end
end

0 comments on commit 19da24e

Please sign in to comment.