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

Refresh azcontainerregistry module #22161

Merged
merged 5 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions sdk/containers/azcontainerregistry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

### Features Added
* Add `ConfigMediaType` and `MediaType` properties to `ManifestAttributes`
* Enabled spans for distributed tracing

### Other Changes
* Refine some logics and comments
* Updated to latest version of azcore

## 0.2.0 (2023-06-06)

Expand Down
44 changes: 30 additions & 14 deletions sdk/containers/azcontainerregistry/authentication_client.go

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

26 changes: 16 additions & 10 deletions sdk/containers/azcontainerregistry/authentication_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (
func Test_authenticationClient_ExchangeAADAccessTokenForACRRefreshToken(t *testing.T) {
startRecording(t)
endpoint, cred, options := getEndpointCredAndClientOptions(t)
client := newAuthenticationClient(endpoint, &authenticationClientOptions{ClientOptions: options})
client, err := newAuthenticationClient(endpoint, &authenticationClientOptions{ClientOptions: options})
require.NoError(t, err)
ctx := context.Background()
if reflect.ValueOf(options.Cloud).IsZero() {
options.Cloud = cloud.AzurePublic
Expand All @@ -44,9 +45,10 @@ func Test_authenticationClient_ExchangeAADAccessTokenForACRRefreshToken(t *testi
func Test_authenticationClient_ExchangeAADAccessTokenForACRRefreshToken_fail(t *testing.T) {
startRecording(t)
endpoint, _, options := getEndpointCredAndClientOptions(t)
client := newAuthenticationClient(endpoint, &authenticationClientOptions{ClientOptions: options})
client, err := newAuthenticationClient(endpoint, &authenticationClientOptions{ClientOptions: options})
require.NoError(t, err)
ctx := context.Background()
_, err := client.ExchangeAADAccessTokenForACRRefreshToken(ctx, postContentSchemaGrantTypeAccessToken, strings.TrimPrefix(endpoint, "https://"), &authenticationClientExchangeAADAccessTokenForACRRefreshTokenOptions{
_, err = client.ExchangeAADAccessTokenForACRRefreshToken(ctx, postContentSchemaGrantTypeAccessToken, strings.TrimPrefix(endpoint, "https://"), &authenticationClientExchangeAADAccessTokenForACRRefreshTokenOptions{
Tenant: to.Ptr("wrong tenant"),
RefreshToken: to.Ptr("wrong token"),
AccessToken: to.Ptr("wrong token"),
Expand All @@ -58,16 +60,18 @@ func Test_authenticationClient_ExchangeAADAccessTokenForACRRefreshToken_error(t
srv, closeServer := mock.NewServer()
defer closeServer()
srv.AppendResponse(mock.WithStatusCode(http.StatusOK), mock.WithBody([]byte("wrong response")))
client := newAuthenticationClient(srv.URL(), &authenticationClientOptions{ClientOptions: azcore.ClientOptions{Transport: srv}})
client, err := newAuthenticationClient(srv.URL(), &authenticationClientOptions{ClientOptions: azcore.ClientOptions{Transport: srv}})
require.NoError(t, err)
ctx := context.Background()
_, err := client.ExchangeAADAccessTokenForACRRefreshToken(ctx, "grantType", "service", nil)
_, err = client.ExchangeAADAccessTokenForACRRefreshToken(ctx, "grantType", "service", nil)
require.Error(t, err)
}

func Test_authenticationClient_ExchangeACRRefreshTokenForACRAccessToken(t *testing.T) {
startRecording(t)
endpoint, cred, options := getEndpointCredAndClientOptions(t)
client := newAuthenticationClient(endpoint, &authenticationClientOptions{ClientOptions: options})
client, err := newAuthenticationClient(endpoint, &authenticationClientOptions{ClientOptions: options})
require.NoError(t, err)
ctx := context.Background()
if reflect.ValueOf(options.Cloud).IsZero() {
options.Cloud = cloud.AzurePublic
Expand All @@ -91,18 +95,20 @@ func Test_authenticationClient_ExchangeACRRefreshTokenForACRAccessToken(t *testi
func Test_authenticationClient_ExchangeACRRefreshTokenForACRAccessToken_fail(t *testing.T) {
startRecording(t)
endpoint, _, options := getEndpointCredAndClientOptions(t)
client := newAuthenticationClient(endpoint, &authenticationClientOptions{ClientOptions: options})
client, err := newAuthenticationClient(endpoint, &authenticationClientOptions{ClientOptions: options})
require.NoError(t, err)
ctx := context.Background()
_, err := client.ExchangeACRRefreshTokenForACRAccessToken(ctx, strings.TrimPrefix(endpoint, "https://"), "registry:catalog:*", "wrong token", &authenticationClientExchangeACRRefreshTokenForACRAccessTokenOptions{GrantType: to.Ptr(tokenGrantTypeRefreshToken)})
_, err = client.ExchangeACRRefreshTokenForACRAccessToken(ctx, strings.TrimPrefix(endpoint, "https://"), "registry:catalog:*", "wrong token", &authenticationClientExchangeACRRefreshTokenForACRAccessTokenOptions{GrantType: to.Ptr(tokenGrantTypeRefreshToken)})
require.Error(t, err)
}

func Test_authenticationClient_ExchangeACRRefreshTokenForACRAccessToken_error(t *testing.T) {
srv, closeServer := mock.NewServer()
defer closeServer()
srv.AppendResponse(mock.WithStatusCode(http.StatusOK), mock.WithBody([]byte("wrong response")))
client := newAuthenticationClient(srv.URL(), &authenticationClientOptions{ClientOptions: azcore.ClientOptions{Transport: srv}})
client, err := newAuthenticationClient(srv.URL(), &authenticationClientOptions{ClientOptions: azcore.ClientOptions{Transport: srv}})
require.NoError(t, err)
ctx := context.Background()
_, err := client.ExchangeACRRefreshTokenForACRAccessToken(ctx, "service", "scope", "refresh token", nil)
_, err = client.ExchangeACRRefreshTokenForACRAccessToken(ctx, "service", "scope", "refresh token", nil)
require.Error(t, err)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@ type authenticationClientOptions struct {
// newAuthenticationClient creates a new instance of AuthenticationClient with the specified values.
// - endpoint - Registry login URL
// - options - Client options, pass nil to accept the default values.
func newAuthenticationClient(endpoint string, options *authenticationClientOptions) *authenticationClient {
func newAuthenticationClient(endpoint string, options *authenticationClientOptions) (*authenticationClient, error) {
if options == nil {
options = &authenticationClientOptions{}
}

pipeline := runtime.NewPipeline(moduleName, moduleVersion, runtime.PipelineOptions{}, &options.ClientOptions)
azcoreClient, err := azcore.NewClient(moduleName, moduleVersion, runtime.PipelineOptions{}, &options.ClientOptions)
tadelesh marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

client := &authenticationClient{
internal: azcoreClient,
endpoint: endpoint,
pl: pipeline,
}
return client
return client, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

func Test_newAuthenticationClient(t *testing.T) {
client := newAuthenticationClient("test", nil)
client, err := newAuthenticationClient("test", nil)
require.NoError(t, err)
require.NotNil(t, client)
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ func Test_authenticationPolicy_getAccessToken_live(t *testing.T) {
if reflect.ValueOf(options.Cloud).IsZero() {
options.Cloud = cloud.AzurePublic
}
authClient := newAuthenticationClient(endpoint, &authenticationClientOptions{options})
authClient, err := newAuthenticationClient(endpoint, &authenticationClientOptions{options})
require.NoError(t, err)
p := &authenticationPolicy{
temporal.NewResource(acquireRefreshToken),
atomic.Value{},
Expand All @@ -154,7 +155,8 @@ func Test_authenticationPolicy_getAccessToken_error(t *testing.T) {
srv.AppendResponse(mock.WithStatusCode(http.StatusOK), mock.WithBody([]byte("{\"refresh_token\": \".eyJqdGkiOiIwMDAwMDAwMC0wMDAwLTAwMDAtMDAwMC0wMDAwMDAwMDAwMDAiLCJzdWIiOiIwMDAwMDAwMC0wMDAwLTAwMDAtMDAwMC0wMDAwMDAwMDAwMDAiLCJuYmYiOjQ2NzA0MTEyMTIsImV4cCI6NDY3MDQyMjkxMiwiaWF0Ijo0NjcwNDExMjEyLCJpc3MiOiJBenVyZSBDb250YWluZXIgUmVnaXN0cnkiLCJhdWQiOiJhemFjcmxpdmV0ZXN0LmF6dXJlY3IuaW8iLCJ2ZXJzaW9uIjoiMS4wIiwicmlkIjoiMDAwMCIsImdyYW50X3R5cGUiOiJyZWZyZXNoX3Rva2VuIiwiYXBwaWQiOiIwMDAwMDAwMC0wMDAwLTAwMDAtMDAwMC0wMDAwMDAwMDAwMDAiLCJwZXJtaXNzaW9ucyI6eyJBY3Rpb25zIjpbInJlYWQiLCJ3cml0ZSIsImRlbGV0ZSIsImRlbGV0ZWQvcmVhZCIsImRlbGV0ZWQvcmVzdG9yZS9hY3Rpb24iXSwiTm90QWN0aW9ucyI6bnVsbH0sInJvbGVzIjpbXX0.\"}")))
srv.AppendResponse(mock.WithStatusCode(http.StatusOK), mock.WithBody([]byte("wrong response")))
srv.AppendResponse(mock.WithStatusCode(http.StatusOK), mock.WithBody([]byte("wrong response")))
authClient := newAuthenticationClient(srv.URL(), &authenticationClientOptions{ClientOptions: azcore.ClientOptions{Transport: srv}})
authClient, err := newAuthenticationClient(srv.URL(), &authenticationClientOptions{ClientOptions: azcore.ClientOptions{Transport: srv}})
require.NoError(t, err)

p := &authenticationPolicy{
temporal.NewResource(acquireRefreshToken),
Expand All @@ -179,7 +181,8 @@ func Test_authenticationPolicy_getAccessToken_error(t *testing.T) {
func Test_authenticationPolicy_getAccessToken_live_anonymous(t *testing.T) {
startRecording(t)
endpoint, _, options := getEndpointCredAndClientOptions(t)
authClient := newAuthenticationClient(endpoint, &authenticationClientOptions{options})
authClient, err := newAuthenticationClient(endpoint, &authenticationClientOptions{options})
require.NoError(t, err)
p := &authenticationPolicy{
refreshTokenCache: temporal.NewResource(acquireRefreshToken),
authClient: authClient,
Expand Down Expand Up @@ -237,7 +240,8 @@ func Test_authenticationPolicy(t *testing.T) {
srv.AppendResponse(mock.WithStatusCode(http.StatusOK), mock.WithBody([]byte("{\"access_token\": \"test\"}")))
srv.AppendResponse(mock.WithStatusCode(http.StatusOK))

authClient := newAuthenticationClient(srv.URL(), &authenticationClientOptions{ClientOptions: azcore.ClientOptions{Transport: srv}})
authClient, err := newAuthenticationClient(srv.URL(), &authenticationClientOptions{ClientOptions: azcore.ClientOptions{Transport: srv}})
require.NoError(t, err)
authPolicy := &authenticationPolicy{
temporal.NewResource(acquireRefreshToken),
atomic.Value{},
Expand Down
10 changes: 7 additions & 3 deletions sdk/containers/azcontainerregistry/autorest.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ clear-output-folder: false
export-clients: true
openapi-type: "data-plane"
output-folder: ../azcontainerregistry
use: "@autorest/go@4.0.0-preview.45"
use: "@autorest/go@4.0.0-preview.60"
honor-body-placement: true
remove-unreferenced-types: true
module-name: sdk/containers/azcontainerregistry
module: github.com/Azure/azure-sdk-for-go/$(module-name)
inject-spans: true
```

## Customizations
Expand Down Expand Up @@ -328,10 +331,11 @@ directive:
transform: return $.replaceAll(/AuthenticationClient/g, "authenticationClient").replace(/AcrRefreshToken\n/, "acrRefreshToken\n").replace(/AcrAccessToken\n/, "acrAccessToken\n");
- from:
- models.go
- options.go
where: $
transform: return $.replaceAll(/AuthenticationClient/g, "authenticationClient").replace(/AcrRefreshToken struct/, "acrRefreshToken struct").replace(/AcrAccessToken struct/, "acrAccessToken struct");
- from:
- models.go
- options.go
where: $
transform: return $.replace(/TokenGrantType/, "tokenGrantType");
- from:
Expand Down Expand Up @@ -444,7 +448,7 @@ directive:
});
- from:
- blob_client.go
- models.go
- options.go
where: $
transform: return $.replaceAll(/BlobClientUploadChunkOptions/g, "blobClientUploadChunkOptions").replace(/BlobClient\.UploadChunk/, "BlobClient.uploadChunk");
```
Expand Down
Loading