Skip to content

Commit

Permalink
Connect to Azurite via Connection String
Browse files Browse the repository at this point in the history
  • Loading branch information
sebagomez committed Apr 1, 2024
1 parent 7f6d6c1 commit 7332ba1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docker-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ cd "${0%/*}" || exit 1

echo App will run on http://localhost:8080

docker run --rm -p 8080:80 --name azurestorageexplorer azurestorageexplorer:local
docker run --rm -p 8080:8080 --name azurestorageexplorer azurestorageexplorer:local
4 changes: 2 additions & 2 deletions src/web/Pages/BaseComponent.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public partial class BaseComponent
NavigationManager? NavManager { get; set; }

[Inject]
ProtectedSessionStorage? SessionStorage {get; set;}
ProtectedSessionStorage? SessionStorage { get; set; }

[Parameter]
public string? Selected { get; set; }
Expand All @@ -33,7 +33,7 @@ public void Increment(Counter counter)
{
counter.Inc();
}

public virtual Task SelectionDeletedAsync()
{
StateHasChanged();
Expand Down
37 changes: 31 additions & 6 deletions src/web/Pages/Login.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,35 @@ private void NavigateOnEnter(KeyboardEventArgs args)
}
}

protected override async Task OnInitializedAsync()
{
string? connString = Environment.GetEnvironmentVariable(Util.AZURE_STORAGE_CONNECTIONSTRING);
if (!string.IsNullOrEmpty(connString))
{
Credentials cred = new Credentials { ConnectionString = connString };
if (await cred.IsAuthenticated(SessionStorage!))
NavManager!.NavigateTo("home");
}
else
{
string? account = Environment.GetEnvironmentVariable(Util.AZURE_STORAGE_ACCOUNT);
if (!string.IsNullOrEmpty(account))
{
string? key = Environment.GetEnvironmentVariable(Util.AZURE_STORAGE_KEY);
if (!string.IsNullOrEmpty(key))
{
string? endpoint = Environment.GetEnvironmentVariable(Util.AZURE_STORAGE_ENDPOINT);
if (!string.IsNullOrEmpty(endpoint))
{
Credentials cred = new Credentials { Account = account, Key = key, Endpoint = endpoint };
if (await cred.IsAuthenticated(SessionStorage!))
NavManager!.NavigateTo("home");
}
}
}
}
}

private async Task SignIn()
{
ShowError = false;
Expand All @@ -49,12 +78,8 @@ private async Task SignIn()
ConnectionString = ConnectionString
};

StorageFactory factory = Util.GetStorageFactory(cred);
var containers = await factory.Containers.ListContainersAsync(); //This authentication method is dangerous with SaS and ConnectionStrings

await cred.SaveAsync(SessionStorage!);

NavManager!.NavigateTo("home");
if (await cred.IsAuthenticated(SessionStorage!))
NavManager!.NavigateTo("home");
}
catch (Exception ex)
{
Expand Down
13 changes: 12 additions & 1 deletion src/web/Utils/Credentials.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Runtime.Serialization;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using StorageLibrary;

namespace web.Utils
{
Expand Down Expand Up @@ -44,7 +45,7 @@ public async Task SaveAsync(ProtectedBrowserStorage sessionStorage)

private string GetAccountName()
{
var connStringArray = ConnectionString!.Split(';');
var connStringArray = ConnectionString!.Split(';', StringSplitOptions.RemoveEmptyEntries);
var dictionary = new Dictionary<string, string>();
foreach (var item in connStringArray)
{
Expand Down Expand Up @@ -89,5 +90,15 @@ public static async Task ClearAsync(ProtectedBrowserStorage sessionStorage)
await sessionStorage.DeleteAsync(ENDPOINT);
await sessionStorage.DeleteAsync(CONNECTION_STRING);
}

public async Task<bool> IsAuthenticated(ProtectedBrowserStorage sessionStorage)
{
StorageFactory factory = Util.GetStorageFactory(this);
var containers = await factory.Containers.ListContainersAsync(); //This authentication method is dangerous with SaS and ConnectionStrings

await SaveAsync(sessionStorage!);

return true;
}
}
}
4 changes: 4 additions & 0 deletions src/web/Utils/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ public class Util
{
const long MAX_MEGS = 10;
public const long MAX_UPLOAD_SIZE = 1024 * 1024 * MAX_MEGS;
public const string AZURE_STORAGE_CONNECTIONSTRING = "AZURE_STORAGE_CONNECTIONSTRING";
public const string AZURE_STORAGE_ACCOUNT = "AZURE_STORAGE_ACCOUNT";
public const string AZURE_STORAGE_KEY = "AZURE_STORAGE_KEY";
public const string AZURE_STORAGE_ENDPOINT = "AZURE_STORAGE_ENDPOINT";

public static StorageFactory GetStorageFactory(Credentials cred)
{
Expand Down

0 comments on commit 7332ba1

Please sign in to comment.