-
Notifications
You must be signed in to change notification settings - Fork 2.8k
azrepos: fall back to OAuth when org DisablePatCreationPolicyViolation policy is set #2346
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
p00j4
wants to merge
1
commit into
git-ecosystem:main
Choose a base branch
from
p00j4:fix/azrepos-pat-policy-violation-oauth-fallback
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
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 |
|---|---|---|
|
|
@@ -20,6 +20,8 @@ public class AzureReposHostProvider : DisposableObject, IHostProvider, IConfigur | |
| private readonly IMicrosoftAuthentication _msAuth; | ||
| private readonly IAzureDevOpsAuthorityCache _authorityCache; | ||
| private readonly IAzureReposBindingManager _bindingManager; | ||
| // Set to true if PAT creation was blocked by org policy this invocation, forcing OAuth fallback. | ||
| private bool _forcedOAuth; | ||
|
|
||
| public AzureReposHostProvider(ICommandContext context) | ||
| : this(context, new AzureDevOpsRestApi(context), new MicrosoftAuthentication(context), | ||
|
|
@@ -118,20 +120,28 @@ public async Task<GetCredentialResult> GetCredentialAsync(InputArguments input) | |
|
|
||
| // No existing credential was found, create a new one | ||
| _context.Trace.WriteLine("Creating new credential..."); | ||
| credential = await GeneratePersonalAccessTokenAsync(input); | ||
| _context.Trace.WriteLine("Credential created."); | ||
| try | ||
| { | ||
| credential = await GeneratePersonalAccessTokenAsync(input); | ||
| _context.Trace.WriteLine("Credential created."); | ||
| return new GetCredentialResult(credential); | ||
| } | ||
| catch (Exception ex) when (IsPatPolicyViolation(ex)) | ||
| { | ||
| HandlePatPolicyViolation(); | ||
| // Fall through to OAuth path below | ||
| } | ||
| } | ||
| else | ||
| { | ||
| _context.Trace.WriteLine("Existing credential found."); | ||
| return new GetCredentialResult(credential); | ||
| } | ||
|
|
||
| return new GetCredentialResult(credential); | ||
| } | ||
| else | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when removing this |
||
|
|
||
| // Include the username request here so that we may use it as an override | ||
| // for user account lookups when getting Azure Access Tokens. | ||
| { | ||
| // Include the username request here so that we may use it as an override | ||
| // for user account lookups when getting Azure Access Tokens. | ||
| var azureResult = await GetAzureAccessTokenAsync(input); | ||
| var azureCredential = new GitCredential(azureResult.AccountUpn, azureResult.AccessToken); | ||
| return new GetCredentialResult(azureCredential); | ||
|
|
@@ -485,8 +495,26 @@ private static string GetAccountNameForCredentialQuery(InputArguments input) | |
| /// Check if Azure DevOps Personal Access Tokens should be used or not. | ||
| /// </summary> | ||
| /// <returns>True if Personal Access Tokens should be used, false otherwise.</returns> | ||
| private static bool IsPatPolicyViolation(Exception ex) => | ||
| ex.Message.Contains("DisablePatCreationPolicyViolation", StringComparison.OrdinalIgnoreCase); | ||
|
|
||
| private void HandlePatPolicyViolation() | ||
| { | ||
| _context.Trace.WriteLine("PAT creation blocked by org policy, falling back to OAuth for this request."); | ||
| _context.Streams.Error.WriteLine( | ||
| "warning: PAT creation is disabled by your Azure DevOps organization policy."); | ||
| _context.Streams.Error.WriteLine( | ||
| "hint: To permanently use OAuth, run:"); | ||
| _context.Streams.Error.WriteLine( | ||
| $"hint: git config --global credential.{AzureDevOpsConstants.GitConfiguration.Credential.CredentialType} {AzureDevOpsConstants.OAuthCredentialType}"); | ||
|
|
||
| _forcedOAuth = true; | ||
| } | ||
|
|
||
| private bool UsePersonalAccessTokens() | ||
| { | ||
| if (_forcedOAuth) return false; | ||
|
|
||
| // Default to using PATs except on DevBox where we prefer OAuth tokens | ||
| bool defaultValue = !PlatformUtils.IsDevBox(); | ||
|
|
||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly this is an incomplete implementation. Because Git calls GCM multiple times, as a new process invocation each time, we 'forget' the value of
_forcedOAuthhere when it comes to thestorecall that Git does following a successfulget+ remote op.Without first implementing the functionality of #2057, we'd be better just printing the error and resolution message (changing to OAuth mode).