Skip to content

Commit 6a871ac

Browse files
authored
Merge pull request #1566 from snyk/fix/azurerm_storage_container_notsupportedforaccount
fix: fix #1558 by ignoring FeatureNotSupported
2 parents 7e5c31f + 8c8b8ca commit 6a871ac

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

enumeration/remote/azurerm/repository/storage.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package repository
33
import (
44
"context"
55
"fmt"
6+
"strings"
7+
8+
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
9+
"github.com/sirupsen/logrus"
610
"github.com/snyk/driftctl/enumeration/remote/azurerm/common"
711
"github.com/snyk/driftctl/enumeration/remote/cache"
812

@@ -111,18 +115,38 @@ func (s *storageRepository) ListAllStorageContainer(account *armstorage.StorageA
111115
for pager.NextPage(context.Background()) {
112116
resp := pager.PageResponse()
113117
if err := pager.Err(); err != nil {
114-
return nil, err
118+
if !shouldIgnoreStorageContainerError(err) {
119+
return nil, err
120+
}
115121
}
116122
for _, item := range resp.BlobContainersListResult.ListContainerItems.Value {
117123
results = append(results, fmt.Sprintf("%s%s", *account.Properties.PrimaryEndpoints.Blob, *item.Name))
118124
}
119125
}
120126

121127
if err := pager.Err(); err != nil {
122-
return nil, err
128+
if !shouldIgnoreStorageContainerError(err) {
129+
return nil, err
130+
}
123131
}
124132

125133
s.cache.Put(cacheKey, results)
126134

127135
return results, nil
128136
}
137+
138+
func shouldIgnoreStorageContainerError(err error) bool {
139+
azureErr, ok := err.(azblob.ResponseError)
140+
if !ok {
141+
return false
142+
}
143+
unwrapped := azureErr.Unwrap().Error()
144+
if strings.Contains(unwrapped, "FeatureNotSupportedForAccount") {
145+
logrus.WithFields(logrus.Fields{
146+
"repository": "StorageRepository",
147+
"error": err,
148+
}).Debug("Ignoring ListStorageContainer error ...")
149+
return true
150+
}
151+
return false
152+
}

enumeration/remote/azurerm/repository/storage_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"reflect"
55
"testing"
66

7+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
78
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
89
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage"
910
"github.com/pkg/errors"
@@ -371,3 +372,37 @@ func Test_ListAllStorageContainer_Error(t *testing.T) {
371372
assert.Nil(t, got)
372373
assert.Equal(t, expectedErr, err)
373374
}
375+
376+
func Test_ListAllStorageContainer_IgnoredError(t *testing.T) {
377+
378+
account := armstorage.StorageAccount{
379+
TrackedResource: armstorage.TrackedResource{
380+
Resource: armstorage.Resource{
381+
ID: to.StringPtr("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foobar/providers/Microsoft.Storage/storageAccounts/testeliedriftctl"),
382+
Name: to.StringPtr("testeliedriftctl"),
383+
},
384+
},
385+
}
386+
387+
fakeClient := &mockBlobContainerClient{}
388+
mockPager := &mockBlobContainerListPager{}
389+
mockPager.On("NextPage", mock.Anything).Return(false).Times(1)
390+
mockPager.On("Err").Return(runtime.NewResponseError(
391+
errors.New("{\"error\":{\"code\":\"FeatureNotSupportedForAccount\",\"message\":\"Blob is not supported for the account.\"}}"),
392+
nil),
393+
).Times(1)
394+
395+
fakeClient.On("List", "foobar", "testeliedriftctl", (*armstorage.BlobContainersListOptions)(nil)).Return(mockPager)
396+
397+
s := &storageRepository{
398+
blobContainerClient: fakeClient,
399+
cache: cache.New(0),
400+
}
401+
got, err := s.ListAllStorageContainer(&account)
402+
403+
fakeClient.AssertExpectations(t)
404+
mockPager.AssertExpectations(t)
405+
406+
assert.Empty(t, got)
407+
assert.Equal(t, nil, err)
408+
}

pkg/resource/azurerm/testdata/acc/azurerm_storage_container/terraform.tf

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ resource "azurerm_storage_account" "example" {
2727
}
2828
}
2929

30+
resource "azurerm_storage_account" "noblob" {
31+
name = "testaccdriftctlnoblob"
32+
resource_group_name = data.azurerm_resource_group.qa1.name
33+
location = data.azurerm_resource_group.qa1.location
34+
account_tier = "Premium"
35+
account_replication_type = "LRS"
36+
account_kind = "FileStorage"
37+
}
38+
3039
resource "azurerm_storage_container" "private" {
3140
name = "private"
3241
storage_account_name = azurerm_storage_account.example.name

0 commit comments

Comments
 (0)