From 8c8b8cad912bae036dfc8617625b3d92e13e384d Mon Sep 17 00:00:00 2001 From: Martin Guibert Date: Tue, 19 Jul 2022 16:41:56 +0200 Subject: [PATCH] fix: fix #1558 by ignoring FeatureNotSupported --- .../remote/azurerm/repository/storage.go | 28 +++++++++++++-- .../remote/azurerm/repository/storage_test.go | 35 +++++++++++++++++++ .../azurerm_storage_container/terraform.tf | 9 +++++ 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/enumeration/remote/azurerm/repository/storage.go b/enumeration/remote/azurerm/repository/storage.go index 94cc3e87d..595db2cc4 100644 --- a/enumeration/remote/azurerm/repository/storage.go +++ b/enumeration/remote/azurerm/repository/storage.go @@ -3,6 +3,10 @@ package repository import ( "context" "fmt" + "strings" + + "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" + "github.com/sirupsen/logrus" "github.com/snyk/driftctl/enumeration/remote/azurerm/common" "github.com/snyk/driftctl/enumeration/remote/cache" @@ -111,7 +115,9 @@ func (s *storageRepository) ListAllStorageContainer(account *armstorage.StorageA for pager.NextPage(context.Background()) { resp := pager.PageResponse() if err := pager.Err(); err != nil { - return nil, err + if !shouldIgnoreStorageContainerError(err) { + return nil, err + } } for _, item := range resp.BlobContainersListResult.ListContainerItems.Value { results = append(results, fmt.Sprintf("%s%s", *account.Properties.PrimaryEndpoints.Blob, *item.Name)) @@ -119,10 +125,28 @@ func (s *storageRepository) ListAllStorageContainer(account *armstorage.StorageA } if err := pager.Err(); err != nil { - return nil, err + if !shouldIgnoreStorageContainerError(err) { + return nil, err + } } s.cache.Put(cacheKey, results) return results, nil } + +func shouldIgnoreStorageContainerError(err error) bool { + azureErr, ok := err.(azblob.ResponseError) + if !ok { + return false + } + unwrapped := azureErr.Unwrap().Error() + if strings.Contains(unwrapped, "FeatureNotSupportedForAccount") { + logrus.WithFields(logrus.Fields{ + "repository": "StorageRepository", + "error": err, + }).Debug("Ignoring ListStorageContainer error ...") + return true + } + return false +} diff --git a/enumeration/remote/azurerm/repository/storage_test.go b/enumeration/remote/azurerm/repository/storage_test.go index 2deea26e1..c8e4461ea 100644 --- a/enumeration/remote/azurerm/repository/storage_test.go +++ b/enumeration/remote/azurerm/repository/storage_test.go @@ -4,6 +4,7 @@ import ( "reflect" "testing" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage" "github.com/pkg/errors" @@ -371,3 +372,37 @@ func Test_ListAllStorageContainer_Error(t *testing.T) { assert.Nil(t, got) assert.Equal(t, expectedErr, err) } + +func Test_ListAllStorageContainer_IgnoredError(t *testing.T) { + + account := armstorage.StorageAccount{ + TrackedResource: armstorage.TrackedResource{ + Resource: armstorage.Resource{ + ID: to.StringPtr("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foobar/providers/Microsoft.Storage/storageAccounts/testeliedriftctl"), + Name: to.StringPtr("testeliedriftctl"), + }, + }, + } + + fakeClient := &mockBlobContainerClient{} + mockPager := &mockBlobContainerListPager{} + mockPager.On("NextPage", mock.Anything).Return(false).Times(1) + mockPager.On("Err").Return(runtime.NewResponseError( + errors.New("{\"error\":{\"code\":\"FeatureNotSupportedForAccount\",\"message\":\"Blob is not supported for the account.\"}}"), + nil), + ).Times(1) + + fakeClient.On("List", "foobar", "testeliedriftctl", (*armstorage.BlobContainersListOptions)(nil)).Return(mockPager) + + s := &storageRepository{ + blobContainerClient: fakeClient, + cache: cache.New(0), + } + got, err := s.ListAllStorageContainer(&account) + + fakeClient.AssertExpectations(t) + mockPager.AssertExpectations(t) + + assert.Empty(t, got) + assert.Equal(t, nil, err) +} diff --git a/pkg/resource/azurerm/testdata/acc/azurerm_storage_container/terraform.tf b/pkg/resource/azurerm/testdata/acc/azurerm_storage_container/terraform.tf index 14d355c9c..6857ef4ec 100644 --- a/pkg/resource/azurerm/testdata/acc/azurerm_storage_container/terraform.tf +++ b/pkg/resource/azurerm/testdata/acc/azurerm_storage_container/terraform.tf @@ -27,6 +27,15 @@ resource "azurerm_storage_account" "example" { } } +resource "azurerm_storage_account" "noblob" { + name = "testaccdriftctlnoblob" + resource_group_name = data.azurerm_resource_group.qa1.name + location = data.azurerm_resource_group.qa1.location + account_tier = "Premium" + account_replication_type = "LRS" + account_kind = "FileStorage" +} + resource "azurerm_storage_container" "private" { name = "private" storage_account_name = azurerm_storage_account.example.name