Skip to content

Commit e8e9766

Browse files
Nickcandyisra-fel
andauthored
Upgrade list tag api to track 2 (#20528)
* upgrade list tag api to track 2 * revise code * update common version * add required assembly * Update src/ContainerRegistry/ContainerRegistry/Models/ContainerRegistryDataPlaneClient.cs Co-authored-by: Yeming Liu <[email protected]> * change name for track2client * add changelog * revise changelog Co-authored-by: Yeming Liu <[email protected]>
1 parent 6a5f4c3 commit e8e9766

File tree

8 files changed

+215
-20
lines changed

8 files changed

+215
-20
lines changed

src/ContainerRegistry/ContainerRegistry/Az.ContainerRegistry.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ RequiredModules = @(@{ModuleName = 'Az.Accounts'; ModuleVersion = '2.8.0'; })
5757

5858
# Assemblies that must be loaded prior to importing this module
5959
RequiredAssemblies = 'Microsoft.Azure.PowerShell.ContainerRegistry.Sdk.dll',
60-
'Microsoft.Azure.ContainerRegistry.dll'
60+
'Microsoft.Azure.ContainerRegistry.dll',
61+
"Azure.Containers.ContainerRegistry.dll"
6162

6263
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
6364
# ScriptsToProcess = @()

src/ContainerRegistry/ContainerRegistry/ChangeLog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21-
21+
* Fixed bug in `Get-AzContainerRegistryTag` to show correct tags [#20528]
2222
## Version 3.0.0
2323
* Updated parameter types from bool to bool? for `Update-AzContainerRegistryRepository` [#17857]
2424
- `ReadEnabled`

src/ContainerRegistry/ContainerRegistry/ContainerRegistry.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14+
<PackageReference Include="Azure.Containers.ContainerRegistry" Version="1.0.0" />
1415
<PackageReference Include="Microsoft.Azure.ContainerRegistry" Version="1.0.0-preview.1" />
1516
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.6.0" />
1617
</ItemGroup>

src/ContainerRegistry/ContainerRegistry/Models/ContainerRegistryDataPlaneClient.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
using System;
2323
using System.Collections.Generic;
2424
using Microsoft.Azure.Commands.Common.Exceptions;
25+
using Track2 = Azure.Containers.ContainerRegistry;
26+
using Azure.Containers.ContainerRegistry;
27+
using Microsoft.Azure.Commands.Common.Strategies;
28+
using Microsoft.Azure.Commands.ContainerRegistry.Track2Models;
29+
using Azure;
2530

2631
namespace Microsoft.Azure.Commands.ContainerRegistry
2732
{
@@ -33,6 +38,8 @@ public class ContainerRegistryDataPlaneClient
3338
private const string _refreshTokenKey = "AcrRefreshToken";
3439

3540
private AzureContainerRegistryClient _client;
41+
private Track2.ContainerRegistryClient _track2Client;
42+
private Track2TokenCredential _credential;
3643
private string _accessToken = default(string);
3744
private string _endPoint;
3845
private readonly string _suffix;
@@ -100,6 +107,12 @@ public void SetEndPoint(string RegistryName)
100107
{
101108
_endPoint = RegistryName.ToLower() + '.' + _suffix;
102109
_client.LoginUri = _https + _endPoint;
110+
_credential = new Track2TokenCredential(new DataServiceCredential(AzureSession.Instance.AuthenticationFactory,
111+
_context, AzureEnvironment.ExtendedEndpoint.ContainerRegistryEndpointResourceId));
112+
_track2Client = new Track2.ContainerRegistryClient(new Uri(_https + _endPoint), _credential, new Track2.ContainerRegistryClientOptions()
113+
{
114+
Audience = _context.Environment.ExtendedProperties[AzureEnvironment.ExtendedEndpoint.ContainerRegistryEndpointResourceId]
115+
});
103116
}
104117

105118
public string GetEndPoint()
@@ -196,7 +209,11 @@ public PSTagAttribute GetTag(string repository, string tag)
196209

197210
public PSTagList ListTag(string repository)
198211
{
199-
return new ContainerRegistryTagListOperation(this, repository).ProcessRequest();
212+
ContainerRepository image = _track2Client.GetRepository(repository);
213+
214+
Pageable<ArtifactManifestProperties> properties = image.GetAllManifestProperties();
215+
216+
return new PSTagList(properties);
200217
}
201218

202219
public bool RemoveTag(string repository, string tag)

src/ContainerRegistry/ContainerRegistry/Models/PSTagList.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Azure.Containers.ContainerRegistry;
16+
using Azure;
1517
using Microsoft.Azure.ContainerRegistry.Models;
1618
using System.Collections.Generic;
1719
using System.Linq;
20+
using Microsoft.Azure.Commands.Common.Compute.Version2016_04_preview.Models;
21+
using System.Collections;
1822

1923
namespace Microsoft.Azure.Commands.ContainerRegistry.Models
2024
{
@@ -24,6 +28,20 @@ public PSTagList()
2428
{
2529
}
2630

31+
public PSTagList(Pageable<ArtifactManifestProperties> properties)
32+
{
33+
this.Tags = new List<PSTagAttributeBase>();
34+
foreach (ArtifactManifestProperties property in properties)
35+
{
36+
foreach (string tag in property.Tags)
37+
{
38+
PSTagAttributeBase newTag = new PSTagAttributeBase(tag, property.Digest, property.CreatedOn.ToString(), property.LastUpdatedOn.ToString(),null ,new ChangeableAttributes(property.CanDelete, property.CanWrite, property.CanList, property.CanRead));
39+
Tags.Add(newTag);
40+
}
41+
this.ImageName = property.RepositoryName;
42+
this.Registry = property.RegistryLoginServer;
43+
}
44+
}
2745
public PSTagList(string registry = default(string), string imageName = default(string), IList<PSTagAttributeBase> tags = default(IList<PSTagAttributeBase>))
2846
{
2947
Registry = registry;
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.Common.Authentication;
16+
using System;
17+
using System.Linq;
18+
using System.Threading.Tasks;
19+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
20+
using Microsoft.Rest;
21+
22+
namespace Microsoft.Azure.Commands.ContainerRegistry.Track2Models
23+
{
24+
internal class DataServiceCredential
25+
{
26+
private readonly IAuthenticationFactory _authenticationFactory;
27+
private readonly IAzureContext _context;
28+
private readonly string _endpointName;
29+
30+
public DataServiceCredential(IAuthenticationFactory authFactory, IAzureContext context, string resourceIdEndpoint)
31+
{
32+
if (authFactory == null)
33+
throw new ArgumentNullException("authFactory");
34+
if (context == null)
35+
throw new ArgumentNullException("context");
36+
_authenticationFactory = authFactory;
37+
_context = context;
38+
_endpointName = resourceIdEndpoint;
39+
this.TenantId = GetTenantId(context);
40+
}
41+
42+
public string TenantId { get; private set; }
43+
44+
/// <summary>
45+
/// Authentication callback method required by KeyVaultClient
46+
/// </summary>
47+
/// <param name="authority"></param>
48+
/// <param name="resource"></param>
49+
/// <param name="scope"></param>
50+
/// <returns></returns>
51+
public Task<string> OnAuthentication(string authority, string resource, string scope)
52+
{
53+
// TODO: Add trace to log tokenType, resource, authority, scope etc
54+
string tokenStr = string.Empty;
55+
56+
// overriding the cached resourceId value to resource returned from the server
57+
if (!string.IsNullOrEmpty(resource))
58+
{
59+
_context.Environment.SetEndpoint(_endpointName, resource);
60+
}
61+
62+
var bundle = GetTokenInternal(this.TenantId, this._authenticationFactory, this._context, this._endpointName);
63+
bundle.Item1.AuthorizeRequest((tokenType, tokenValue) =>
64+
{
65+
tokenStr = tokenValue;
66+
});
67+
return Task.FromResult<string>(tokenStr);
68+
}
69+
70+
public string GetToken()
71+
{
72+
return GetAccessToken().AccessToken;
73+
}
74+
75+
public IAccessToken GetAccessToken()
76+
{
77+
return GetTokenInternal(TenantId, _authenticationFactory, _context, _endpointName).Item1;
78+
}
79+
80+
private static string GetTenantId(IAzureContext context)
81+
{
82+
if (context.Account == null)
83+
{
84+
throw new ArgumentException("No account found in the context. Please login using Connect-AzAccount.");
85+
}
86+
87+
var tenantId = string.Empty;
88+
if (context.Tenant != null && context.Tenant.GetId() != Guid.Empty)
89+
{
90+
tenantId = context.Tenant.Id.ToString();
91+
}
92+
else if (string.IsNullOrWhiteSpace(tenantId) && context.Subscription != null && context.Account != null)
93+
{
94+
tenantId = context.Subscription.GetPropertyAsArray(AzureSubscription.Property.Tenants)
95+
.Intersect(context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants))
96+
.FirstOrDefault();
97+
}
98+
99+
return tenantId;
100+
}
101+
102+
private static Tuple<IAccessToken, string> GetTokenInternal(string tenantId, IAuthenticationFactory authFactory, IAzureContext context, string resourceIdEndpoint)
103+
{
104+
if (string.IsNullOrWhiteSpace(tenantId))
105+
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.");
106+
107+
try
108+
{
109+
var tokenCache = AzureSession.Instance.TokenCache;
110+
if (context.TokenCache != null && context.TokenCache.CacheData != null && context.TokenCache.CacheData.Length > 0)
111+
{
112+
tokenCache = context.TokenCache;
113+
}
114+
115+
var accesstoken = authFactory.Authenticate(context.Account, context.Environment, tenantId, null, ShowDialog.Never,
116+
null, tokenCache, resourceIdEndpoint);
117+
118+
if (context.TokenCache != null && context.TokenCache.CacheData != null && context.TokenCache.CacheData.Length > 0)
119+
{
120+
context.TokenCache = tokenCache;
121+
}
122+
123+
return Tuple.Create(accesstoken, context.Environment.GetEndpoint(resourceIdEndpoint));
124+
}
125+
catch (Exception ex)
126+
{
127+
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);
128+
}
129+
}
130+
}
131+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Azure.Core;
5+
6+
namespace Microsoft.Azure.Commands.ContainerRegistry.Track2Models
7+
{
8+
internal class Track2TokenCredential : TokenCredential
9+
{
10+
private readonly DataServiceCredential dataServiceCredential;
11+
12+
public Track2TokenCredential(DataServiceCredential dataServiceCredential)
13+
{
14+
this.dataServiceCredential = dataServiceCredential;
15+
}
16+
17+
public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken)
18+
{
19+
return new AccessToken(dataServiceCredential.GetToken(), DateTimeOffset.UtcNow);
20+
}
21+
22+
public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken)
23+
{
24+
return new ValueTask<AccessToken>(this.GetToken(requestContext, cancellationToken));
25+
}
26+
}
27+
}

tools/Common.Netcore.Dependencies.targets

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
<ItemGroup>
44
<PackageReference Include="Microsoft.Rest.ClientRuntime" Version="2.3.24"/>
55
<PackageReference Include="Microsoft.Rest.ClientRuntime.Azure" Version="3.3.19"/>
6-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Aks" Version="1.3.67-preview"/>
7-
<PackageReference Include="Microsoft.Azure.PowerShell.Authentication.Abstractions" Version="1.3.67-preview"/>
8-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Authorization" Version="1.3.67-preview"/>
9-
<PackageReference Include="Microsoft.Azure.PowerShell.Common" Version="1.3.67-preview"/>
10-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Compute" Version="1.3.67-preview"/>
11-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Graph.Rbac" Version="1.3.67-preview"/>
12-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.KeyVault" Version="1.3.67-preview"/>
13-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Monitor" Version="1.3.67-preview"/>
14-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Network" Version="1.3.67-preview"/>
15-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.PolicyInsights" Version="1.3.67-preview"/>
16-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.ResourceManager" Version="1.3.67-preview"/>
17-
<PackageReference Include="Microsoft.Azure.PowerShell.Storage" Version="1.3.67-preview"/>
18-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Storage.Management" Version="1.3.67-preview"/>
19-
<PackageReference Include="Microsoft.Azure.PowerShell.Strategies" Version="1.3.67-preview"/>
20-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Websites" Version="1.3.67-preview"/>
21-
<PackageReference Include="Microsoft.Azure.PowerShell.Common.Share" Version="1.3.67-preview"/>
6+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Aks" Version="1.3.68-preview"/>
7+
<PackageReference Include="Microsoft.Azure.PowerShell.Authentication.Abstractions" Version="1.3.68-preview"/>
8+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Authorization" Version="1.3.68-preview"/>
9+
<PackageReference Include="Microsoft.Azure.PowerShell.Common" Version="1.3.68-preview"/>
10+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Compute" Version="1.3.68-preview"/>
11+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Graph.Rbac" Version="1.3.68-preview"/>
12+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.KeyVault" Version="1.3.68-preview"/>
13+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Monitor" Version="1.3.68-preview"/>
14+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Network" Version="1.3.68-preview"/>
15+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.PolicyInsights" Version="1.3.68-preview"/>
16+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.ResourceManager" Version="1.3.68-preview"/>
17+
<PackageReference Include="Microsoft.Azure.PowerShell.Storage" Version="1.3.68-preview"/>
18+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Storage.Management" Version="1.3.68-preview"/>
19+
<PackageReference Include="Microsoft.Azure.PowerShell.Strategies" Version="1.3.68-preview"/>
20+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Websites" Version="1.3.68-preview"/>
21+
<PackageReference Include="Microsoft.Azure.PowerShell.Common.Share" Version="1.3.68-preview"/>
2222
</ItemGroup>
2323
<ItemGroup>
2424
<PackageReference Include="Azure.Core" Version="1.25.0"/>
@@ -36,7 +36,7 @@
3636
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0" PrivateAssets="All" />
3737
</ItemGroup>
3838
<PropertyGroup>
39-
<StorageToolsPath>$(NugetPackageRoot)\microsoft.azure.powershell.storage\1.3.67-preview\tools\</StorageToolsPath>
39+
<StorageToolsPath>$(NugetPackageRoot)\microsoft.azure.powershell.storage\1.3.68-preview\tools\</StorageToolsPath>
4040
</PropertyGroup>
4141
<ItemGroup Condition="'$(OmitJsonPackage)' != 'true'">
4242
<PackageReference Include="Newtonsoft.Json" Version="10.0.3"/>

0 commit comments

Comments
 (0)