Skip to content

Commit

Permalink
Merge pull request #1566 from snyk/fix/azurerm_storage_container_nots…
Browse files Browse the repository at this point in the history
…upportedforaccount

fix: fix #1558 by ignoring FeatureNotSupported
  • Loading branch information
eliecharra authored Jul 19, 2022
2 parents 7e5c31f + 8c8b8ca commit 6a871ac
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
28 changes: 26 additions & 2 deletions enumeration/remote/azurerm/repository/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -111,18 +115,38 @@ 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))
}
}

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
}
35 changes: 35 additions & 0 deletions enumeration/remote/azurerm/repository/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6a871ac

Please sign in to comment.