-
Notifications
You must be signed in to change notification settings - Fork 0
Demo: resource-first configuration and Blob Storage via the TokenCredential adapter #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| --- | ||
|
|
||
| Bring the Demo to the resource-first configuration: Azure SQL over Google federation on Cloud Run as the main path, a managed identity variant for Azure hosts, and an optional Blob Storage test file downloaded through `WorkloadIdentityTokenCredential`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
Csag.WorkloadIdentity.Demo/Extensions/BlobStorageExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| namespace Csag.WorkloadIdentity.Demo.Extensions | ||
| { | ||
| using System; | ||
| using Azure.Storage.Blobs; | ||
| using Csag.WorkloadIdentity.Abstractions; | ||
| using Csag.WorkloadIdentity.Demo.Services; | ||
| using Csag.WorkloadIdentity.Options; | ||
|
|
||
| public static class BlobStorageExtensions | ||
| { | ||
| public static IServiceCollection AddBlobStorageClient(this IServiceCollection services, IConfiguration configuration) | ||
| { | ||
| // Blob Storage is optional in the Demo. The client is only registered when the library has a BlobStorage | ||
| // resource to obtain tokens for and a test file is named, so that a run configured for Azure SQL alone | ||
| // still starts and /test reports the blob part as skipped. | ||
| var resourceSection = configuration.GetSection($"{WorkloadIdentityOptions.ConfigurationSectionName}:{nameof(WorkloadIdentityOptions.BlobStorage)}"); | ||
| var endpoint = configuration["BlobStorageTestFile:Endpoint"]; | ||
| var filePath = configuration["BlobStorageTestFile:FilePath"]; | ||
| if (!resourceSection.Exists() || string.IsNullOrWhiteSpace(endpoint) || string.IsNullOrWhiteSpace(filePath)) | ||
| { | ||
| return services; | ||
| } | ||
|
|
||
| // The library's TokenCredential adapter serves the client from the held Blob Storage token and reports | ||
| // that token's real expiry, so the Azure SDK asks for a new one only when the provider refreshes it. | ||
| services.AddSingleton(serviceProvider => new BlobServiceClient( | ||
| new Uri(endpoint), | ||
| new WorkloadIdentityTokenCredential(serviceProvider.GetRequiredService<IBlobStorageTokenProvider>()))); | ||
| services.AddSingleton<IBlobStorageService>(serviceProvider => new BlobStorageService(serviceProvider.GetRequiredService<BlobServiceClient>(), filePath)); | ||
|
|
||
| return services; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| namespace Csag.WorkloadIdentity.Demo.Services | ||
| { | ||
| using System; | ||
| using Azure.Storage.Blobs; | ||
|
|
||
| public class BlobStorageService : IBlobStorageService | ||
| { | ||
| private readonly BlobClient blobClient; | ||
|
|
||
| public BlobStorageService(BlobServiceClient blobServiceClient, string filePath) | ||
| { | ||
| // The test file is addressed as "<container>/<blob>"; the blob name may itself contain slashes. | ||
| var separator = filePath.IndexOf('/'); | ||
| if (separator <= 0 || separator == filePath.Length - 1) | ||
| { | ||
| throw new ArgumentException("The test file path must have the form '<container>/<blob>'.", nameof(filePath)); | ||
| } | ||
|
|
||
| this.blobClient = blobServiceClient.GetBlobContainerClient(filePath[..separator]).GetBlobClient(filePath[(separator + 1)..]); | ||
| } | ||
|
|
||
| public async Task<string> DownloadTestFileAsync(CancellationToken cancellationToken) | ||
| { | ||
| var download = await this.blobClient.DownloadContentAsync(cancellationToken); | ||
| return download.Value.Content.ToString(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| namespace Csag.WorkloadIdentity.Demo.Services | ||
| { | ||
| public interface IBlobStorageService | ||
| { | ||
| Task<string> DownloadTestFileAsync(CancellationToken cancellationToken); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.