Merge pull request #1566 from snyk/fix/azurerm_storage_container_notsupportedforaccount

fix: fix #1558 by ignoring FeatureNotSupported
main
Elie 2022-07-19 16:49:16 +02:00 committed by GitHub
commit 6a871acd30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 2 deletions

View File

@ -3,6 +3,10 @@ package repository
import ( import (
"context" "context"
"fmt" "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/azurerm/common"
"github.com/snyk/driftctl/enumeration/remote/cache" "github.com/snyk/driftctl/enumeration/remote/cache"
@ -111,7 +115,9 @@ func (s *storageRepository) ListAllStorageContainer(account *armstorage.StorageA
for pager.NextPage(context.Background()) { for pager.NextPage(context.Background()) {
resp := pager.PageResponse() resp := pager.PageResponse()
if err := pager.Err(); err != nil { if err := pager.Err(); err != nil {
return nil, err if !shouldIgnoreStorageContainerError(err) {
return nil, err
}
} }
for _, item := range resp.BlobContainersListResult.ListContainerItems.Value { for _, item := range resp.BlobContainersListResult.ListContainerItems.Value {
results = append(results, fmt.Sprintf("%s%s", *account.Properties.PrimaryEndpoints.Blob, *item.Name)) 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 { if err := pager.Err(); err != nil {
return nil, err if !shouldIgnoreStorageContainerError(err) {
return nil, err
}
} }
s.cache.Put(cacheKey, results) s.cache.Put(cacheKey, results)
return results, nil 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
}

View File

@ -4,6 +4,7 @@ import (
"reflect" "reflect"
"testing" "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/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage" "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -371,3 +372,37 @@ func Test_ListAllStorageContainer_Error(t *testing.T) {
assert.Nil(t, got) assert.Nil(t, got)
assert.Equal(t, expectedErr, err) 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)
}

View File

@ -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" { resource "azurerm_storage_container" "private" {
name = "private" name = "private"
storage_account_name = azurerm_storage_account.example.name storage_account_name = azurerm_storage_account.example.name