Skip to content

Commit 107b11c

Browse files
committed
Check if the blob is public internally
1 parent 696b13e commit 107b11c

File tree

6 files changed

+50
-23
lines changed

6 files changed

+50
-23
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ Configuration = new ConfigurationBuilder()
2828
.AddBlobJson(new BlobJsonConfigurationOption
2929
{
3030
BlobUri = "{the_blob_uri}",
31-
IsPublic = false,
3231
ReloadOnChange = true,
3332
LogReloadException = e => logger.LogError(e, e.Message),
3433
ActionOnReload = () => logger.LogInformation("Reloaded.")

samples/SampleWebApp/Program.cs

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public static IHostBuilder CreateHostBuilder(string[] args)
2929
configuration.AddBlobJson(new BlobJsonConfigurationOption
3030
{
3131
BlobUri = new Uri(blobConfig["BlobUrl"]),
32-
IsPublic = true,
3332
ReloadOnChange = true,
3433
LogReloadException = ex => s_logger.LogError(ex, ex.Message),
3534
ActionOnReload = () => s_logger.LogInformation("Reloaded.")

samples/SampleWebApp/SampleWebApp.csproj

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
<ItemGroup>
1212
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.2" />
1313
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
14-
<PackageReference Include="AzureBlobConfigurationExtension" Version="0.0.2" />
1514
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.2" />
1615
</ItemGroup>
1716

17+
<ItemGroup>
18+
<ProjectReference Include="..\..\src\AzureBlobConfigurationExtension.csproj" />
19+
</ItemGroup>
20+
1821
</Project>

src/BlobAccessor.cs

+45-18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Threading;
44
using System.Threading.Tasks;
55
using Microsoft.Azure.Services.AppAuthentication;
6+
using Microsoft.WindowsAzure.Storage;
67
using Microsoft.WindowsAzure.Storage.Auth;
78
using Microsoft.WindowsAzure.Storage.Blob;
89

@@ -12,29 +13,26 @@ internal class BlobAccessor
1213
{
1314
protected CloudBlockBlob _blob;
1415

15-
public BlobAccessor(Uri blobUri, string account, string sasToken, bool isPublic)
16+
private BlobAccessor(CloudBlockBlob blob)
1617
{
17-
if (isPublic)
18+
_blob = blob;
19+
}
20+
21+
public static BlobAccessor Create(Uri blobUri, string account, string sasToken)
22+
{
23+
if (!string.IsNullOrEmpty(sasToken))
1824
{
19-
_blob = new CloudBlockBlob(blobUri);
25+
var storageCredentials = new StorageCredentials(account, sasToken);
26+
var blob = new CloudBlockBlob(blobUri, storageCredentials);
27+
return new BlobAccessor(blob);
2028
}
21-
else if (string.IsNullOrEmpty(sasToken))
22-
{
23-
var azureServiceTokenProvider = new AzureServiceTokenProvider();
24-
var tokenAndFrequency = TokenRenewerAsync(azureServiceTokenProvider, CancellationToken.None).GetAwaiter().GetResult();
25-
var tokenCredential = new TokenCredential(tokenAndFrequency.Token,
26-
TokenRenewerAsync,
27-
azureServiceTokenProvider,
28-
tokenAndFrequency.Frequency.Value);
2929

30-
var storageCredentials = new StorageCredentials(tokenCredential);
31-
_blob = new CloudBlockBlob(blobUri, storageCredentials);
32-
}
33-
else
30+
if (IsBlobPublic(blobUri).Result)
3431
{
35-
var storageCredentials = new StorageCredentials(account, sasToken);
36-
_blob = new CloudBlockBlob(blobUri, storageCredentials);
32+
return new BlobAccessor(new CloudBlockBlob(blobUri));
3733
}
34+
35+
return CreateBlobAccessorWithAAD(blobUri);
3836
}
3937

4038
public async Task<(string, bool)> RetrieveIfUpdated(MemoryStream ms, string eTag)
@@ -61,10 +59,39 @@ private static async Task<NewTokenAndFrequency> TokenRenewerAsync(object state,
6159
var next = (authResult.ExpiresOn - DateTimeOffset.UtcNow) - TimeSpan.FromMinutes(5);
6260
if (next.Ticks < 0)
6361
{
64-
next = default(TimeSpan);
62+
next = default;
6563
}
6664

6765
return new NewTokenAndFrequency(authResult.AccessToken, next);
6866
}
67+
68+
private static async Task<bool> IsBlobPublic(Uri blobUri)
69+
{
70+
try
71+
{
72+
// check if the blob can be accessed directly.
73+
await new CloudBlockBlob(blobUri).FetchAttributesAsync();
74+
}
75+
catch (StorageException)
76+
{
77+
return false;
78+
}
79+
80+
return true;
81+
}
82+
83+
private static BlobAccessor CreateBlobAccessorWithAAD(Uri blobUri)
84+
{
85+
var azureServiceTokenProvider = new AzureServiceTokenProvider();
86+
var tokenAndFrequency = TokenRenewerAsync(azureServiceTokenProvider, CancellationToken.None).GetAwaiter().GetResult();
87+
var tokenCredential = new TokenCredential(tokenAndFrequency.Token,
88+
TokenRenewerAsync,
89+
azureServiceTokenProvider,
90+
tokenAndFrequency.Frequency.Value);
91+
92+
var storageCredentials = new StorageCredentials(tokenCredential);
93+
var blob = new CloudBlockBlob(blobUri, storageCredentials);
94+
return new BlobAccessor(blob);
95+
}
6996
}
7097
}

src/BlobJsonConfigurationOption.cs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ public class BlobJsonConfigurationOption
77
{
88
public Uri BlobUri { get; set; }
99
public string SASToken { get; set; }
10-
public bool IsPublic { get; set; }
1110
public bool ReloadOnChange { get; set; } = false;
1211
public TimeSpan PollingInterval { get; set; } = TimeSpan.FromSeconds(5);
1312
public Action<Exception> LogReloadException { get; set; }

src/BlobJsonConfigurationSource.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public BlobJsonConfigurationSource(BlobJsonConfigurationOption option)
1414

1515
var account = BlobJsonConfigurationOption.GetAccount(option.BlobUri);
1616

17-
BlobAccessor = new BlobAccessor(option.BlobUri, account, option.SASToken, option.IsPublic);
17+
BlobAccessor = BlobAccessor.Create(option.BlobUri, account, option.SASToken);
1818
}
1919

2020
public override IConfigurationProvider Build(IConfigurationBuilder builder)

0 commit comments

Comments
 (0)