Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Asset Inventory][Azure] Add storage asset fetchers #2938

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/inventory/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ var (
AssetClassificationAzureStorageBlobService = AssetClassification{CategoryStorageBucket, "Azure Storage Blob Service"}
AssetClassificationAzureStorageQueue = AssetClassification{CategoryMessagingService, "Azure Storage Queue"}
AssetClassificationAzureStorageQueueService = AssetClassification{CategoryMessagingService, "Azure Storage Queue Service"}
AssetClassificationAzureStorageTableService = AssetClassification{CategoryMessagingService, "Azure Storage Table Service"}
AssetClassificationAzureSubscription = AssetClassification{CategoryAccessManagement, "Azure Subscription"}
AssetClassificationAzureTenant = AssetClassification{CategoryAccessManagement, "Azure Tenant"}
AssetClassificationAzureVirtualMachine = AssetClassification{CategoryHost, "Azure Virtual Machine"}
Expand Down
2 changes: 2 additions & 0 deletions internal/inventory/azurefetcher/fetcher_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type (
ListStorageAccountBlobServices(ctx context.Context, storageAccounts []azurelib.AzureAsset) ([]azurelib.AzureAsset, error)
ListStorageAccountQueues(ctx context.Context, storageAccounts []azurelib.AzureAsset) ([]azurelib.AzureAsset, error)
ListStorageAccountQueueServices(ctx context.Context, storageAccounts []azurelib.AzureAsset) ([]azurelib.AzureAsset, error)
ListStorageAccountTableServices(ctx context.Context, storageAccounts []azurelib.AzureAsset) ([]azurelib.AzureAsset, error)
ListStorageAccounts(ctx context.Context, storageAccountsSubscriptionsIds []string) ([]azurelib.AzureAsset, error)
}
)
Expand All @@ -59,6 +60,7 @@ func (f *storageFetcher) Fetch(ctx context.Context, assetChan chan<- inventory.A
{"Storage Blob Services", f.provider.ListStorageAccountBlobServices, inventory.AssetClassificationAzureStorageBlobService},
{"Storage Queue Services", f.provider.ListStorageAccountQueueServices, inventory.AssetClassificationAzureStorageQueueService},
{"Storage Queues", f.provider.ListStorageAccountQueues, inventory.AssetClassificationAzureStorageQueue},
{"Storage Table Services", f.provider.ListStorageAccountTableServices, inventory.AssetClassificationAzureStorageTableService},
}

storageAccounts, err := f.listStorageAccounts(ctx)
Expand Down
21 changes: 21 additions & 0 deletions internal/inventory/azurefetcher/fetcher_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func TestStorageFetcher_Fetch(t *testing.T) {
Name: "queue",
DisplayName: "queue",
}
azureTableService := azurelib_inventory.AzureAsset{
Id: "queue",
Name: "queue",
DisplayName: "queue",
}

expected := []inventory.AssetEvent{
inventory.NewAssetEvent(
Expand Down Expand Up @@ -83,6 +88,16 @@ func TestStorageFetcher_Fetch(t *testing.T) {
ServiceName: "Azure",
}),
),
inventory.NewAssetEvent(
inventory.AssetClassificationAzureStorageTableService,
azureQueue.Id,
azureQueue.Name,
inventory.WithRawAsset(azureTableService),
inventory.WithCloud(inventory.Cloud{
Provider: inventory.AzureCloudProvider,
ServiceName: "Azure",
}),
),
}

// setup
Expand Down Expand Up @@ -119,6 +134,12 @@ func TestStorageFetcher_Fetch(t *testing.T) {
[]azurelib_inventory.AzureAsset{azureQueue}, nil,
)

provider.EXPECT().ListStorageAccountTableServices(
mock.Anything, mock.Anything,
).Return(
[]azurelib_inventory.AzureAsset{azureTableService}, nil,
)

fetcher := newStorageFetcher(logger, provider)
// test & compare
testutil.CollectResourcesAndMatch(t, fetcher, expected)
Expand Down
55 changes: 55 additions & 0 deletions internal/inventory/azurefetcher/mock_storage_provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type storageAccountAzureClientWrapper struct {
AssetBlobServices func(ctx context.Context, subID string, clientOptions *arm.ClientOptions, resourceGroup, storageAccountName string, options *armstorage.BlobServicesClientListOptions) ([]armstorage.BlobServicesClientListResponse, error)
AssetQueues func(ctx context.Context, subID string, clientOptions *arm.ClientOptions, resourceGroup, storageAccountName string, options *armstorage.QueueClientListOptions) ([]armstorage.QueueClientListResponse, error)
AssetQueueServices func(ctx context.Context, subID string, clientOptions *arm.ClientOptions, resourceGroup, storageAccountName string, options *armstorage.QueueServicesClientListOptions) (armstorage.QueueServicesClientListResponse, error)
AssetTableServices func(ctx context.Context, subID string, clientOptions *arm.ClientOptions, resourceGroup, storageAccountName string, options *armstorage.TableServicesClientListOptions) (armstorage.TableServicesClientListResponse, error)
AssetAccountStorage func(ctx context.Context, subID string, clientOptions *arm.ClientOptions) ([]armstorage.AccountsClientListResponse, error)
}

Expand All @@ -50,6 +51,7 @@ type StorageAccountProviderAPI interface {
ListStorageAccountQueueServices(ctx context.Context, storageAccounts []AzureAsset) ([]AzureAsset, error)
ListStorageAccountsBlobDiagnosticSettings(ctx context.Context, storageAccounts []AzureAsset) ([]AzureAsset, error)
ListStorageAccountsTableDiagnosticSettings(ctx context.Context, storageAccounts []AzureAsset) ([]AzureAsset, error)
ListStorageAccountTableServices(ctx context.Context, storageAccounts []AzureAsset) ([]AzureAsset, error)
ListStorageAccountsQueueDiagnosticSettings(ctx context.Context, storageAccounts []AzureAsset) ([]AzureAsset, error)
ListStorageAccounts(ctx context.Context, storageAccountsSubscriptionsIds []string) ([]AzureAsset, error)
}
Expand Down Expand Up @@ -88,6 +90,13 @@ func NewStorageAccountProvider(log *logp.Logger, diagnosticSettingsClient *armmo
}
return cl.List(ctx, resourceGroupName, storageAccountName, options)
},
AssetTableServices: func(ctx context.Context, subID string, clientOptions *arm.ClientOptions, resourceGroupName, storageAccountName string, options *armstorage.TableServicesClientListOptions) (armstorage.TableServicesClientListResponse, error) {
cl, err := armstorage.NewTableServicesClient(subID, credentials, clientOptions)
if err != nil {
return armstorage.TableServicesClientListResponse{}, err
}
return cl.List(ctx, resourceGroupName, storageAccountName, options)
},
AssetAccountStorage: func(ctx context.Context, subID string, clientOptions *arm.ClientOptions) ([]armstorage.AccountsClientListResponse, error) {
accountsClient, err := armstorage.NewAccountsClient(subID, credentials, clientOptions)
if err != nil {
Expand Down Expand Up @@ -231,6 +240,38 @@ func (p *storageAccountProvider) ListStorageAccountQueueServices(ctx context.Con
return assets, nil
}

func (p *storageAccountProvider) ListStorageAccountTableServices(ctx context.Context, storageAccounts []AzureAsset) ([]AzureAsset, error) {
var assets []AzureAsset
for _, sa := range storageAccounts {
response, err := p.client.AssetTableServices(ctx, sa.SubscriptionId, nil, sa.ResourceGroup, sa.Name, nil)
if err != nil {
return nil, fmt.Errorf("error while fetching azure table services for storage accounts %s: %w", sa.Id, err)
}

for _, item := range response.Value {
properties, err := maps.AsMapStringAny(item.TableServiceProperties)
if err != nil {
p.log.Errorf("error while transforming azure table services for storage accounts %s: %v", sa.Id, err)
}

assets = append(assets, AzureAsset{
Id: pointers.Deref(item.ID),
Name: pointers.Deref(item.Name),
Type: strings.ToLower(pointers.Deref(item.Type)),
ResourceGroup: sa.ResourceGroup,
SubscriptionId: sa.SubscriptionId,
TenantId: sa.TenantId,
Properties: properties,
Extension: map[string]any{
ExtensionStorageAccountID: sa.Id,
ExtensionStorageAccountName: sa.Name,
},
})
}
}
return assets, nil
}

func transformBlobServices(servicesPages []armstorage.BlobServicesClientListResponse, storageAccount AzureAsset) ([]AzureAsset, error) {
var errs error
return lo.FlatMap(servicesPages, func(response armstorage.BlobServicesClientListResponse, _ int) []AzureAsset {
Expand Down
55 changes: 55 additions & 0 deletions internal/resources/providers/azurelib/mock_provider_api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,8 @@
category="Storage Bucket",
type_="Azure Storage Blob Service",
),
"[Asset Inventory][Azure][Azure Table Service] assets found": AssetInventoryCase(
category="Messaging Service",
type_="Azure Storage Table Service",
),
}
2 changes: 1 addition & 1 deletion tests/product/tests/test_aws_asset_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_aws_asset_inventory(
assert isinstance(entities, list) and len(entities) > 0, "Expected the list to be non-empty"
for entity in entities:
assert entity.cloud, "Expected .cloud section"
assert entity.cloud.Provider == "aws", f'Expected "aws" provider, got {entity.cloud.Provider}'
assert entity.cloud.provider == "aws", f'Expected "aws" provider, got {entity.cloud.provider}'
assert len(entity.entity.id) > 0, "Expected .entity.id list to contain an ID"
assert len(entity.entity.id[0]) > 0, "Expected the ID to be non-empty"
assert entity.Attributes, "Expected the resource under .Attributes"
Expand Down
2 changes: 1 addition & 1 deletion tests/product/tests/test_azure_asset_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_azure_asset_inventory(
assert isinstance(entities, list) and len(entities) > 0, "Expected the list to be non-empty"
for entity in entities:
assert entity.cloud, "Expected .cloud section"
assert entity.cloud.Provider == "azure", f'Expected "aws" provider, got {entity.cloud.Provider}'
assert entity.cloud.provider == "azure", f'Expected "aws" provider, got {entity.cloud.provider}'
assert len(entity.entity.id) > 0, "Expected .entity.id list to contain an ID"
assert len(entity.entity.id[0]) > 0, "Expected the ID to be non-empty"
assert entity.Attributes, "Expected the resource under .Attributes"
Expand Down
2 changes: 1 addition & 1 deletion tests/product/tests/test_gcp_asset_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_gcp_asset_inventory(
assert isinstance(entities, list) and len(entities) > 0, "Expected the list to be non-empty"
for entity in entities:
assert entity.cloud, "Expected .cloud section"
assert entity.cloud.Provider == "gcp", f'Expected "gcp" provider, got {entity.cloud.Provider}'
assert entity.cloud.provider == "gcp", f'Expected "gcp" provider, got {entity.cloud.provider}'
assert len(entity.entity.id) > 0, "Expected .entity.id list to contain an ID"
assert len(entity.entity.id[0]) > 0, "Expected the ID to be non-empty"
assert entity.Attributes, "Expected the resource under .Attributes"
Expand Down
8 changes: 6 additions & 2 deletions tests/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ dependencies = [
packages = [
{include="fleet_api"}
]
name = "tests"
version = "0.1.0"
description = "Test project for cloudbeat agent"
authors = ["Cloudbeat Team"]

[tool.poetry.requires-plugins]
poetry-plugin-export = ">=1.8"
#[tool.poetry.requires-plugins]
#poetry-plugin-export = ">=1.8"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
Loading