-
Notifications
You must be signed in to change notification settings - Fork 4k
upgrade list tag api to track 2 #20528
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e2bea48
upgrade list tag api to track 2
Nickcandy ab866a4
revise code
Nickcandy ef7db89
update common version
Nickcandy bf40ed7
add required assembly
Nickcandy a1a021d
Update src/ContainerRegistry/ContainerRegistry/Models/ContainerRegist…
Nickcandy d56cd91
change name for track2client
Nickcandy 5b97ec6
add changelog
Nickcandy 82bbdf9
Merge branch 'release-2023-01-10' into nanxiang/ContainerRegistry
Nickcandy 2ca7254
Merge branch 'release-2023-01-10' into nanxiang/ContainerRegistry
Nickcandy bbf970c
Merge branch 'release-2023-01-10' into nanxiang/ContainerRegistry
Nickcandy 54e01b3
revise changelog
Nickcandy 446afc5
Merge branch 'release-2023-01-10' into nanxiang/ContainerRegistry
Nickcandy 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
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
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
131 changes: 131 additions & 0 deletions
131
src/ContainerRegistry/ContainerRegistry/Models/Track2Models/DataServiceCredential.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,131 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// | ||
// Copyright Microsoft Corporation | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ---------------------------------------------------------------------------------- | ||
|
||
using Microsoft.Azure.Commands.Common.Authentication; | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Commands.Common.Authentication.Abstractions; | ||
using Microsoft.Rest; | ||
|
||
namespace Microsoft.Azure.Commands.ContainerRegistry.Track2Models | ||
{ | ||
internal class DataServiceCredential | ||
{ | ||
private readonly IAuthenticationFactory _authenticationFactory; | ||
private readonly IAzureContext _context; | ||
private readonly string _endpointName; | ||
|
||
public DataServiceCredential(IAuthenticationFactory authFactory, IAzureContext context, string resourceIdEndpoint) | ||
{ | ||
if (authFactory == null) | ||
throw new ArgumentNullException("authFactory"); | ||
if (context == null) | ||
throw new ArgumentNullException("context"); | ||
_authenticationFactory = authFactory; | ||
_context = context; | ||
_endpointName = resourceIdEndpoint; | ||
this.TenantId = GetTenantId(context); | ||
} | ||
|
||
public string TenantId { get; private set; } | ||
|
||
/// <summary> | ||
/// Authentication callback method required by KeyVaultClient | ||
/// </summary> | ||
/// <param name="authority"></param> | ||
/// <param name="resource"></param> | ||
/// <param name="scope"></param> | ||
/// <returns></returns> | ||
public Task<string> OnAuthentication(string authority, string resource, string scope) | ||
{ | ||
// TODO: Add trace to log tokenType, resource, authority, scope etc | ||
string tokenStr = string.Empty; | ||
|
||
// overriding the cached resourceId value to resource returned from the server | ||
if (!string.IsNullOrEmpty(resource)) | ||
{ | ||
_context.Environment.SetEndpoint(_endpointName, resource); | ||
} | ||
|
||
var bundle = GetTokenInternal(this.TenantId, this._authenticationFactory, this._context, this._endpointName); | ||
bundle.Item1.AuthorizeRequest((tokenType, tokenValue) => | ||
{ | ||
tokenStr = tokenValue; | ||
}); | ||
return Task.FromResult<string>(tokenStr); | ||
} | ||
|
||
public string GetToken() | ||
{ | ||
return GetAccessToken().AccessToken; | ||
} | ||
|
||
public IAccessToken GetAccessToken() | ||
{ | ||
return GetTokenInternal(TenantId, _authenticationFactory, _context, _endpointName).Item1; | ||
} | ||
|
||
private static string GetTenantId(IAzureContext context) | ||
{ | ||
if (context.Account == null) | ||
{ | ||
throw new ArgumentException("No account found in the context. Please login using Connect-AzAccount."); | ||
} | ||
|
||
var tenantId = string.Empty; | ||
if (context.Tenant != null && context.Tenant.GetId() != Guid.Empty) | ||
{ | ||
tenantId = context.Tenant.Id.ToString(); | ||
} | ||
else if (string.IsNullOrWhiteSpace(tenantId) && context.Subscription != null && context.Account != null) | ||
{ | ||
tenantId = context.Subscription.GetPropertyAsArray(AzureSubscription.Property.Tenants) | ||
.Intersect(context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants)) | ||
.FirstOrDefault(); | ||
} | ||
|
||
return tenantId; | ||
} | ||
|
||
private static Tuple<IAccessToken, string> GetTokenInternal(string tenantId, IAuthenticationFactory authFactory, IAzureContext context, string resourceIdEndpoint) | ||
{ | ||
if (string.IsNullOrWhiteSpace(tenantId)) | ||
throw new ArgumentException("No tenant found in the context. Please ensure that the credentials you provided are authorized to access an Azure subscription, then run Connect-AzAccount to login."); | ||
|
||
try | ||
{ | ||
var tokenCache = AzureSession.Instance.TokenCache; | ||
if (context.TokenCache != null && context.TokenCache.CacheData != null && context.TokenCache.CacheData.Length > 0) | ||
{ | ||
tokenCache = context.TokenCache; | ||
} | ||
|
||
var accesstoken = authFactory.Authenticate(context.Account, context.Environment, tenantId, null, ShowDialog.Never, | ||
null, tokenCache, resourceIdEndpoint); | ||
|
||
if (context.TokenCache != null && context.TokenCache.CacheData != null && context.TokenCache.CacheData.Length > 0) | ||
{ | ||
context.TokenCache = tokenCache; | ||
} | ||
|
||
return Tuple.Create(accesstoken, context.Environment.GetEndpoint(resourceIdEndpoint)); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new ArgumentException("Your Azure credentials have not been set up or have expired, please run Connect-AzAccount to set up your Azure credentials.", ex); | ||
} | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/ContainerRegistry/ContainerRegistry/Models/Track2Models/Track2TokenCredential.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,27 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Core; | ||
|
||
namespace Microsoft.Azure.Commands.ContainerRegistry.Track2Models | ||
{ | ||
internal class Track2TokenCredential : TokenCredential | ||
{ | ||
private readonly DataServiceCredential dataServiceCredential; | ||
|
||
public Track2TokenCredential(DataServiceCredential dataServiceCredential) | ||
{ | ||
this.dataServiceCredential = dataServiceCredential; | ||
} | ||
|
||
public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken) | ||
{ | ||
return new AccessToken(dataServiceCredential.GetToken(), DateTimeOffset.UtcNow); | ||
} | ||
|
||
public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) | ||
{ | ||
return new ValueTask<AccessToken>(this.GetToken(requestContext, cancellationToken)); | ||
} | ||
} | ||
} |
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
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.