From 2c3e52a3148d31b118f3d076e346374ccae9ee93 Mon Sep 17 00:00:00 2001 From: Shahram Kalantari Date: Thu, 3 Oct 2024 12:21:40 +1000 Subject: [PATCH] chore: remove unnecessary functions Signed-off-by: Shahram Kalantari --- .../oras/authprovider/azure/azureidentity.go | 15 ----- .../authprovider/azure/azureidentity_test.go | 66 ++++--------------- .../azure/azureworkloadidentity.go | 17 ----- .../azure/azureworkloadidentity_test.go | 57 +--------------- 4 files changed, 12 insertions(+), 143 deletions(-) diff --git a/pkg/common/oras/authprovider/azure/azureidentity.go b/pkg/common/oras/authprovider/azure/azureidentity.go index 5cfc21b3c0..380f692ad2 100644 --- a/pkg/common/oras/authprovider/azure/azureidentity.go +++ b/pkg/common/oras/authprovider/azure/azureidentity.go @@ -42,21 +42,6 @@ type MIAuthProvider struct { getManagedIdentityToken func(ctx context.Context, clientID string) (azcore.AccessToken, error) } -// NewAzureWIAuthProvider is defined to enable mocking of some of the function in unit tests -func NewAzureMIAuthProvider() *MIAuthProvider { - return &MIAuthProvider{ - authClientFactory: func(serverURL string, options *azcontainerregistry.AuthenticationClientOptions) (AuthClient, error) { - client, err := azcontainerregistry.NewAuthenticationClient(serverURL, options) - if err != nil { - return nil, err - } - return &AuthenticationClientWrapper{client: client}, nil - }, - getRegistryHost: provider.GetRegistryHostName, - getManagedIdentityToken: getManagedIdentityToken, - } -} - type azureManagedIdentityAuthProviderConf struct { Name string `json:"name"` ClientID string `json:"clientID"` diff --git a/pkg/common/oras/authprovider/azure/azureidentity_test.go b/pkg/common/oras/authprovider/azure/azureidentity_test.go index 680b6b4895..af33afe44f 100644 --- a/pkg/common/oras/authprovider/azure/azureidentity_test.go +++ b/pkg/common/oras/authprovider/azure/azureidentity_test.go @@ -103,61 +103,6 @@ func TestAzureMSIValidation_EnvironmentVariables_ExpectedResults(t *testing.T) { } } -func TestNewAzureMIAuthProvider_AuthenticationClientError(t *testing.T) { - // Create a new mock client factory - mockFactory := new(MockAuthClientFactory) - - // Setup mock to return an error - mockFactory.On("NewAuthenticationClient", mock.Anything, mock.Anything). - Return(nil, errors.New("failed to create authentication client")) - - // Create a new WIAuthProvider instance - provider := NewAzureMIAuthProvider() - provider.authClientFactory = mockFactory.NewAuthenticationClient - - // Call authClientFactory to test error handling - _, err := provider.authClientFactory("https://myregistry.azurecr.io", nil) - - // Assert that an error is returned - assert.Error(t, err) - assert.Equal(t, "failed to create authentication client", err.Error()) - - // Verify that the mock was called - mockFactory.AssertCalled(t, "NewAuthenticationClient", "https://myregistry.azurecr.io", mock.Anything) -} - -func TestNewAzureMIAuthProvider_Success(t *testing.T) { - // Create a new mock client factory - mockFactory := new(MockAuthClientFactory) - - // Create a mock auth client to return from the factory - mockAuthClient := new(MockAuthClient) - - // Setup mock to return a successful auth client - mockFactory.On("NewAuthenticationClient", mock.Anything, mock.Anything). - Return(mockAuthClient, nil) - - // Create a new WIAuthProvider instance - provider := NewAzureMIAuthProvider() - - // Replace authClientFactory with the mock factory - provider.authClientFactory = mockFactory.NewAuthenticationClient - - // Call authClientFactory to test successful return - client, err := provider.authClientFactory("https://myregistry.azurecr.io", nil) - - // Assert that the client is returned without an error - assert.NoError(t, err) - assert.NotNil(t, client) - - // Assert that the returned client is of the expected type - _, ok := client.(*MockAuthClient) - assert.True(t, ok, "expected client to be of type *MockAuthClient") - - // Verify that the mock was called - mockFactory.AssertCalled(t, "NewAuthenticationClient", "https://myregistry.azurecr.io", mock.Anything) -} - func TestMIProvide_Success(t *testing.T) { const registryHost = "myregistry.azurecr.io" mockClient := new(MockAuthClient) @@ -243,3 +188,14 @@ func TestMIProvide_RefreshAAD(t *testing.T) { assert.NoError(t, err) mockGetManagedIdentityToken.AssertCalled(t, "GetManagedIdentityToken", mock.Anything, "mockClientID") // Assert that getManagedIdentityToken was called } + +func TestMIProvide_Failure_InvalidHostName(t *testing.T) { + provider := &MIAuthProvider{ + getRegistryHost: func(_ string) (string, error) { + return "", errors.New("invalid hostname") + }, + } + + _, err := provider.Provide(context.Background(), "artifact") + assert.Error(t, err) +} diff --git a/pkg/common/oras/authprovider/azure/azureworkloadidentity.go b/pkg/common/oras/authprovider/azure/azureworkloadidentity.go index 77744622b9..a5ed6d9f2c 100644 --- a/pkg/common/oras/authprovider/azure/azureworkloadidentity.go +++ b/pkg/common/oras/authprovider/azure/azureworkloadidentity.go @@ -25,7 +25,6 @@ import ( re "github.com/ratify-project/ratify/errors" "github.com/ratify-project/ratify/internal/logger" provider "github.com/ratify-project/ratify/pkg/common/oras/authprovider" - "github.com/ratify-project/ratify/pkg/metrics" "github.com/ratify-project/ratify/pkg/utils/azureauth" "github.com/AzureAD/microsoft-authentication-library-for-go/apps/confidential" @@ -54,22 +53,6 @@ type AuthClient interface { ExchangeAADAccessTokenForACRRefreshToken(ctx context.Context, grantType, service string, options *azcontainerregistry.AuthenticationClientExchangeAADAccessTokenForACRRefreshTokenOptions) (azcontainerregistry.AuthenticationClientExchangeAADAccessTokenForACRRefreshTokenResponse, error) } -// NewAzureWIAuthProvider is defined to enable mocking of some of the function in unit tests -func NewAzureWIAuthProvider() *WIAuthProvider { - return &WIAuthProvider{ - authClientFactory: func(serverURL string, options *azcontainerregistry.AuthenticationClientOptions) (AuthClient, error) { - client, err := azcontainerregistry.NewAuthenticationClient(serverURL, options) - if err != nil { - return nil, err - } - return &AuthenticationClientWrapper{client: client}, nil - }, - getRegistryHost: provider.GetRegistryHostName, - getAADAccessToken: azureauth.GetAADAccessToken, - reportMetrics: metrics.ReportACRExchangeDuration, - } -} - type azureWIAuthProviderConf struct { Name string `json:"name"` ClientID string `json:"clientID,omitempty"` diff --git a/pkg/common/oras/authprovider/azure/azureworkloadidentity_test.go b/pkg/common/oras/authprovider/azure/azureworkloadidentity_test.go index 1f6d3743b1..bbfd29eb93 100644 --- a/pkg/common/oras/authprovider/azure/azureworkloadidentity_test.go +++ b/pkg/common/oras/authprovider/azure/azureworkloadidentity_test.go @@ -165,61 +165,6 @@ func TestAzureWIValidation_EnvironmentVariables_ExpectedResults(t *testing.T) { } } -func TestNewAzureWIAuthProvider_AuthenticationClientError(t *testing.T) { - // Create a new mock client factory - mockFactory := new(MockAuthClientFactory) - - // Setup mock to return an error - mockFactory.On("NewAuthenticationClient", mock.Anything, mock.Anything). - Return(nil, errors.New("failed to create authentication client")) - - // Create a new WIAuthProvider instance - provider := NewAzureWIAuthProvider() - provider.authClientFactory = mockFactory.NewAuthenticationClient - - // Call authClientFactory to test error handling - _, err := provider.authClientFactory("https://myregistry.azurecr.io", nil) - - // Assert that an error is returned - assert.Error(t, err) - assert.Equal(t, "failed to create authentication client", err.Error()) - - // Verify that the mock was called - mockFactory.AssertCalled(t, "NewAuthenticationClient", "https://myregistry.azurecr.io", mock.Anything) -} - -func TestNewAzureWIAuthProvider_Success(t *testing.T) { - // Create a new mock client factory - mockFactory := new(MockAuthClientFactory) - - // Create a mock auth client to return from the factory - mockAuthClient := new(MockAuthClient) - - // Setup mock to return a successful auth client - mockFactory.On("NewAuthenticationClient", mock.Anything, mock.Anything). - Return(mockAuthClient, nil) - - // Create a new WIAuthProvider instance - provider := NewAzureWIAuthProvider() - - // Replace authClientFactory with the mock factory - provider.authClientFactory = mockFactory.NewAuthenticationClient - - // Call authClientFactory to test successful return - client, err := provider.authClientFactory("https://myregistry.azurecr.io", nil) - - // Assert that the client is returned without an error - assert.NoError(t, err) - assert.NotNil(t, client) - - // Assert that the returned client is of the expected type - _, ok := client.(*MockAuthClient) - assert.True(t, ok, "expected client to be of type *MockAuthClient") - - // Verify that the mock was called - mockFactory.AssertCalled(t, "NewAuthenticationClient", "https://myregistry.azurecr.io", mock.Anything) -} - func TestWIProvide_Success(t *testing.T) { mockClient := new(MockAuthClient) expectedRefreshToken := "mocked_refresh_token" @@ -300,7 +245,7 @@ func TestWIProvide_RefreshAAD(t *testing.T) { mockAzureAuth.AssertCalled(t, "GetAADAccessToken", mock.Anything, mock.Anything, mock.Anything, mock.Anything) } -func TestProvide_Failure_InvalidHostName(t *testing.T) { +func TestWIProvide_Failure_InvalidHostName(t *testing.T) { provider := &WIAuthProvider{ getRegistryHost: func(_ string) (string, error) { return "", errors.New("invalid hostname")