Skip to content
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

Enable anonymous access for ACR #20919

Merged
merged 8 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ internal ArtifactTagProperties() { }
public partial class ContainerRegistryClient
{
protected ContainerRegistryClient() { }
public ContainerRegistryClient(System.Uri registryUri) { }
public ContainerRegistryClient(System.Uri registryUri, Azure.Containers.ContainerRegistry.ContainerRegistryClientOptions options) { }
public ContainerRegistryClient(System.Uri registryUri, Azure.Core.TokenCredential credential) { }
public ContainerRegistryClient(System.Uri registryUri, Azure.Core.TokenCredential credential, Azure.Containers.ContainerRegistry.ContainerRegistryClientOptions options) { }
public virtual string LoginServer { get { throw null; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ private async Task<string> GetAnonymousAcrAccessTokenAsync(string service, strin
Response<AcrAccessToken> acrAccessToken = null;
if (async)
{
acrAccessToken = await _authenticationClient.ExchangeAcrRefreshTokenForAcrAccessTokenAsync(service, scope, acrRefreshToken: string.Empty, TokenGrantType.Password, cancellationToken ).ConfigureAwait(false);
acrAccessToken = await _authenticationClient.ExchangeAcrRefreshTokenForAcrAccessTokenAsync(service, scope, string.Empty, TokenGrantType.Password, cancellationToken ).ConfigureAwait(false);
}
else
{
acrAccessToken = _authenticationClient.ExchangeAcrRefreshTokenForAcrAccessToken(service, scope, acrRefreshToken: string.Empty, TokenGrantType.Password, cancellationToken);
acrAccessToken = _authenticationClient.ExchangeAcrRefreshTokenForAcrAccessToken(service, scope, string.Empty, TokenGrantType.Password, cancellationToken);
}

return acrAccessToken.Value.AccessToken;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using Azure.Core.TestFramework;
using NUnit.Framework;
Expand All @@ -11,26 +10,10 @@ namespace Azure.Containers.ContainerRegistry.Tests
{
public class ContainerRegistryClientLiveTests : ContainerRegistryRecordedTestBase
{
public ContainerRegistryClientLiveTests(bool isAsync) : base(isAsync, RecordedTestMode.Live)
public ContainerRegistryClientLiveTests(bool isAsync) : base(isAsync)
{
}

private ContainerRegistryClient CreateClient(bool anonymousAccess = false)
{
return anonymousAccess ?

InstrumentClient(new ContainerRegistryClient(
new Uri(TestEnvironment.Endpoint),
InstrumentClientOptions(new ContainerRegistryClientOptions())
)) :

InstrumentClient(new ContainerRegistryClient(
new Uri(TestEnvironment.Endpoint),
TestEnvironment.Credential,
InstrumentClientOptions(new ContainerRegistryClientOptions())
));
}

[RecordedTest]
[TestCase(true)]
[TestCase(false)]
Expand Down Expand Up @@ -118,6 +101,7 @@ public async Task CanStartPagingMidCollection(bool anonymous)
public async Task CanDeleteRepostitory()
{
// Arrange
string registry = TestEnvironment.Registry;
string repository = $"library/hello-world";
List<string> tags = new List<string>()
{
Expand All @@ -133,7 +117,7 @@ public async Task CanDeleteRepostitory()
{
if (Mode != RecordedTestMode.Playback)
{
await ImportImage(repository, tags);
await ImportImage(registry, repository, tags);
}

// Act
Expand Down Expand Up @@ -162,9 +146,19 @@ public async Task CanDeleteRepostitory()
// Clean up - put the repository with tags back.
if (Mode != RecordedTestMode.Playback)
{
await ImportImage(repository, tags);
await ImportImage(registry, repository, tags);
}
}
}

[RecordedTest, NonParallelizable]
public void CanDeleteRepostitory_Anonymous()
{
// Arrange
string repository = $"library/hello-world";
var client = CreateClient(anonymousAccess: true);

Assert.ThrowsAsync<RequestFailedException>(() => client.DeleteRepositoryAsync(repository));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,28 @@ public ContainerRegistryRecordedTestBase(bool isAsync, RecordedTestMode mode) :
Sanitizer = new ContainerRegistryRecordedTestSanitizer();
}

public async Task ImportImage(string repository, string tag)
public ContainerRegistryClient CreateClient(bool anonymousAccess = false)
{
await ImportImage(repository, new List<string>() { tag });
return anonymousAccess ?

InstrumentClient(new ContainerRegistryClient(
new Uri(TestEnvironment.AnonymousAccessEndpoint),
InstrumentClientOptions(new ContainerRegistryClientOptions())
)) :

InstrumentClient(new ContainerRegistryClient(
new Uri(TestEnvironment.Endpoint),
TestEnvironment.Credential,
InstrumentClientOptions(new ContainerRegistryClientOptions())
));
}

public async Task ImportImage(string registry, string repository, string tag)
{
await ImportImage(registry, repository, new List<string>() { tag });
}

public async Task ImportImage(string repository, List<string> tags)
public async Task ImportImage(string registry, string repository, List<string> tags)
{
var credential = new AzureCredentials(
new ServicePrincipalLoginInformation
Expand All @@ -55,7 +71,7 @@ public async Task ImportImage(string repository, List<string> tags)

await managementClient.Registries.ImportImageAsync(
resourceGroupName: TestEnvironment.ResourceGroup,
registryName: TestEnvironment.Registry,
registryName: registry,
parameters:
new ImportImageParameters
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Azure.Containers.ContainerRegistry.Tests
public class ContainerRegistryTestEnvironment : TestEnvironment
{
public string Endpoint => GetRecordedVariable("CONTAINERREGISTRY_ENDPOINT");
public string UserName => GetRecordedVariable("CONTAINERREGISTRY_USERNAME", options => options.IsSecret());
public string Password => GetRecordedVariable("CONTAINERREGISTRY_PASSWORD", options => options.IsSecret());
public string Registry => GetRecordedVariable("CONTAINERREGISTRY_REGISTRY_NAME");
public string AnonymousAccessEndpoint => GetRecordedVariable("CONTAINERREGISTRY_ANONREGISTRY_ENDPOINT");
public string AnonymousAccessRegistry => GetRecordedVariable("CONTAINERREGISTRY_ANONREGISTRY_NAME");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,6 @@ public ContainerRepositoryLiveTests(bool isAsync) : base(isAsync)
{
}

#region Setup methods

private ContainerRegistryClient CreateClient()
{
return InstrumentClient(new ContainerRegistryClient(
new Uri(TestEnvironment.Endpoint),
TestEnvironment.Credential,
InstrumentClientOptions(new ContainerRegistryClientOptions())
));
}

#endregion

#region Repository Tests
[RecordedTest]
public async Task CanGetRepositoryProperties()
{
Expand Down Expand Up @@ -83,6 +69,25 @@ public async Task CanSetRepositoryProperties()
await repository.SetPropertiesAsync(originalContentProperties);
}

[RecordedTest, NonParallelizable]
public void CanSetRepositoryProperties_Anonymous()
{
// Arrange
var client = CreateClient(anonymousAccess: true);
var repository = client.GetRepository(_repositoryName);

// Act
Assert.ThrowsAsync<RequestFailedException>(() =>
repository.SetPropertiesAsync(
new ContentProperties()
{
CanList = false,
CanRead = false,
CanWrite = false,
CanDelete = false,
}));
}

[RecordedTest, NonParallelizable]
public async Task CanDeleteRepository()
{
Expand All @@ -103,7 +108,7 @@ public async Task CanDeleteRepository()
{
if (Mode != RecordedTestMode.Playback)
{
await ImportImage(_repositoryName, tags);
await ImportImage(TestEnvironment.Registry, _repositoryName, tags);
}

// Act
Expand All @@ -124,16 +129,18 @@ public async Task CanDeleteRepository()
// Clean up - put the repository with tags back.
if (Mode != RecordedTestMode.Playback)
{
await ImportImage(_repositoryName, tags);
await ImportImage(TestEnvironment.Registry, _repositoryName, tags);
}
}
}

[RecordedTest]
public async Task CanGetManifests()
[TestCase(true)]
[TestCase(false)]
public async Task CanGetManifests(bool anonymous)
{
// Arrange
var client = CreateClient();
var client = CreateClient(anonymous);
var repository = client.GetRepository(_repositoryName);

// Act
Expand All @@ -155,10 +162,12 @@ public async Task CanGetManifests()
}

[RecordedTest]
public async Task CanGetManifestsWithCustomPageSize()
[TestCase(true)]
[TestCase(false)]
public async Task CanGetManifestsWithCustomPageSize(bool anonymous)
{
// Arrange
var client = CreateClient();
var client = CreateClient(anonymous);
var repository = client.GetRepository(_repositoryName);
int pageSize = 2;
int minExpectedPages = 2;
Expand All @@ -179,10 +188,12 @@ public async Task CanGetManifestsWithCustomPageSize()
}

[RecordedTest]
public async Task CanGetArtifactsStartingMidCollection()
[TestCase(true)]
[TestCase(false)]
public async Task CanGetArtifactsStartingMidCollection(bool anonymous)
{
// Arrange
var client = CreateClient();
var client = CreateClient(anonymous);
var repository = client.GetRepository(_repositoryName);
int pageSize = 1;
int minExpectedPages = 2;
Expand Down Expand Up @@ -244,7 +255,7 @@ public async Task CanGetManifestsOrdered()
{
if (Mode != RecordedTestMode.Playback)
{
await ImportImage(repositoryName, tag);
await ImportImage(TestEnvironment.Registry, repositoryName, tag);
}

// Act
Expand All @@ -270,8 +281,5 @@ public async Task CanGetManifestsOrdered()
await artifact.DeleteAsync();
}
}

#endregion

}
}
Loading