This repository was archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Network troubleshooter - support for managed identity connections and key vault references #38
Open
gajibillik
wants to merge
29
commits into
Azure:main
Choose a base branch
from
gajibillik:sidkri/527_Networkvalidator_V3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
6d962f9
Code changes for Blob, Queue, Servicebus and EventHub of New SDK vers…
gajibillik 424453a
Modified the code as per the comments
gajibillik fd98f98
changed the code as per the comments
gajibillik 2c880c7
Changes done as per the sid's comments in PR
gajibillik 973dad8
Added comments for queue, eventhub and servicebus
gajibillik 858dd6a
Code has been changed as per the comments in PR
gajibillik 0561022
Code modified as per the comments
gajibillik 6df0d71
Changed the code as per the comments
gajibillik 46ab0ff
Changed the code as per the comments
gajibillik 062c3a3
Removed ommented code
gajibillik db7a575
jwt Token related code changes
gajibillik 2ec976c
Updated error messages and refactored ManagedIdentityException to sup…
7a375a3
Additional changes for error messages
e397ada
Additional error msg and logic changes
d01dd31
Code changes as per the latest comments
gajibillik 3566ed8
Code changes of latest comments on fobidden case
gajibillik 1dc1279
Improved error and mitigation messages and fixed a few relevant bugs …
25cb261
Updated Authentication failed error messages with docs links
4ee3cf1
Added error handling logic for key vault reference resolution failure…
aa895f7
Returning a StatusSummary for UnknownError status to keep client side…
9d880e7
Added support for entity not found error handling. Quotes around val…
ef2aa54
Code changes as per the comments
gajibillik c8fca3b
Code changes as per the comments
gajibillik e997f66
Code changes as per the latest comments.
gajibillik 474051a
PR feedback on messaging.
54fccf6
Modified the code as per the comments by yifguoMSFT
gajibillik 5a35676
Merge branch 'sidkri/527_Networkvalidator_V3' of https://github.com/g…
gajibillik 1254010
Merge branch 'feature/network-troubleshooter-managed-identity-support…
sidkri 8854e89
Removed duplicate package reference
sidkri 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
Large diffs are not rendered by default.
Oops, something went wrong.
133 changes: 133 additions & 0 deletions
133
DiagnosticsExtension/Models/ConnectionStringValidator/BlobStorageValidator.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,133 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="StorageValidator.cs" company="Microsoft Corporation"> | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. See LICENSE in the project root for license information. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| using DiagnosticsExtension.Controllers; | ||
| using DiagnosticsExtension.Models.ConnectionStringValidator.Exceptions; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using Azure.Storage.Blobs; | ||
| using Azure.Storage.Queues; | ||
| using Azure.Storage.Blobs.Models; | ||
| using Microsoft.WindowsAzure.Storage; | ||
| using Azure.Core; | ||
| using Azure.Identity; | ||
|
|
||
| namespace DiagnosticsExtension.Models.ConnectionStringValidator | ||
| { | ||
| public class BlobStorageValidator : IConnectionStringValidator | ||
| { | ||
| public string ProviderName => "Microsoft.WindowsAzure.Storage"; | ||
| public ConnectionStringType Type => ConnectionStringType.BlobStorageAccount; | ||
| public async Task<ConnectionStringValidationResult> ValidateViaAppsettingAsync(string appSettingName, string entityName) | ||
| { | ||
| ConnectionStringValidationResult response = new ConnectionStringValidationResult(Type); | ||
| bool isManagedIdentityConnection = false; | ||
| try | ||
| { | ||
| var envDict = Environment.GetEnvironmentVariables(); | ||
| string appSettingClientIdValue, appSettingClientCredValue = null; | ||
| BlobServiceClient client = null; | ||
| if (envDict.Contains(appSettingName)) | ||
| { | ||
| // Connection String | ||
| try | ||
| { | ||
| string connectionString = Environment.GetEnvironmentVariable(appSettingName); | ||
| client = new BlobServiceClient(connectionString); | ||
| } | ||
| catch (ArgumentNullException e) | ||
| { | ||
| throw new EmptyConnectionStringException(e.Message, e); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| throw new MalformedConnectionStringException(e.Message, e); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // Managed Identity | ||
| isManagedIdentityConnection = true; | ||
| string serviceUriString = ManagedIdentityConnectionResponseUtility.ResolveManagedIdentityCommonProperty(appSettingName, ConnectionStringValidationResult.ManagedIdentityCommonProperty.blobServiceUri); | ||
| if (string.IsNullOrEmpty(serviceUriString)) | ||
| { | ||
| serviceUriString = ManagedIdentityConnectionResponseUtility.ResolveManagedIdentityCommonProperty(appSettingName, ConnectionStringValidationResult.ManagedIdentityCommonProperty.serviceUri); | ||
| } | ||
| if (!string.IsNullOrEmpty(serviceUriString)) | ||
| { | ||
| string clientIdAppSettingKey = Environment.GetEnvironmentVariables().Keys.Cast<string>().Where(k => k.StartsWith(appSettingName) && k.ToLower().EndsWith("clientid")).FirstOrDefault(); | ||
| appSettingClientIdValue = ManagedIdentityConnectionResponseUtility.ResolveManagedIdentityCommonProperty(appSettingName, ConnectionStringValidationResult.ManagedIdentityCommonProperty.clientId); | ||
| appSettingClientCredValue = ManagedIdentityConnectionResponseUtility.ResolveManagedIdentityCommonProperty(appSettingName, ConnectionStringValidationResult.ManagedIdentityCommonProperty.credential); | ||
| if (appSettingClientCredValue != null && appSettingClientCredValue != Constants.ValidCredentialValue) | ||
| { | ||
| throw new ManagedIdentityException(String.Format(Constants.ManagedIdentityCredentialInvalidSummary, appSettingName)); | ||
| } | ||
| Uri serviceUri = new Uri(serviceUriString); | ||
| // If the user has configured __credential with "managedidentity" and set an app setting for __clientId (even if its empty) we assume their intent is to use a user assigned managed identity | ||
| if (appSettingClientCredValue != null && clientIdAppSettingKey != null) | ||
| { | ||
| if (string.IsNullOrEmpty(appSettingClientIdValue)) | ||
| { | ||
| throw new ManagedIdentityException(String.Format(Constants.ManagedIdentityClientIdEmptySummary, clientIdAppSettingKey), | ||
| String.Format(Constants.ManagedIdentityClientIdEmptyDetails, appSettingName)); | ||
| } | ||
| response.IdentityType = Constants.User; | ||
| client = new BlobServiceClient(serviceUri, ManagedIdentityCredentialTokenValidator.GetValidatedCredential(appSettingClientIdValue,appSettingName)); | ||
| } | ||
| else | ||
| { | ||
| // Creating client using System assigned managed identity | ||
| response.IdentityType = Constants.System; | ||
| client = new BlobServiceClient(serviceUri, new Azure.Identity.ManagedIdentityCredential()); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| string serviceuriAppSettingName = Environment.GetEnvironmentVariables().Keys.Cast<string>().Where(k => k.StartsWith(appSettingName) && k.ToLower().EndsWith("serviceuri")).FirstOrDefault(); | ||
| if (serviceuriAppSettingName == null) | ||
| { | ||
| throw new ManagedIdentityException(Constants.BlobServiceUriMissingSummary); | ||
| } | ||
| throw new ManagedIdentityException(String.Format(Constants.BlobServiceUriEmptySummary, serviceuriAppSettingName)); | ||
|
|
||
| } | ||
| } | ||
| var resultSegment = | ||
| client.GetBlobContainers(BlobContainerTraits.Metadata, null, default) | ||
| .AsPages(default, 10); | ||
| //need to read at least one result item to confirm authorization check for connection | ||
| resultSegment.Single(); | ||
|
|
||
| response.Status = ConnectionStringValidationResult.ResultStatus.Success; | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| if (isManagedIdentityConnection) | ||
| { | ||
| ManagedIdentityConnectionResponseUtility.EvaluateResponseStatus(e, Type, ref response, appSettingName); | ||
| } | ||
| else | ||
| { | ||
| ConnectionStringResponseUtility.EvaluateResponseStatus(e, Type, ref response, appSettingName); | ||
| } | ||
| } | ||
|
|
||
| return response; | ||
| } | ||
| public async Task<ConnectionStringValidationResult> ValidateAsync(string connStr, string clientId = null) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| public async Task<bool> IsValidAsync(string connStr) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| } | ||
| } |
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.