-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Azure fetchers and asset provider (#1310)
* Implement Azure fetchers and asset provider * Fixing subscription to env * up * CR comments --------- Co-authored-by: Orestis Floros <[email protected]>
- Loading branch information
1 parent
a961057
commit 83fa2d8
Showing
11 changed files
with
865 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package fetchers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/elastic/elastic-agent-libs/logp" | ||
"golang.org/x/exp/maps" | ||
|
||
"github.com/elastic/cloudbeat/resources/fetching" | ||
"github.com/elastic/cloudbeat/resources/providers/azurelib/inventory" | ||
) | ||
|
||
type AzureAssetsFetcher struct { | ||
log *logp.Logger | ||
resourceCh chan fetching.ResourceInfo | ||
provider inventory.ServiceAPI | ||
} | ||
|
||
type AzureResource struct { | ||
Type string | ||
SubType string | ||
Asset inventory.AzureAsset `json:"asset,omitempty"` | ||
} | ||
|
||
var AzureResourceTypes = map[string]string{ | ||
inventory.VirtualMachineAssetType: fetching.AzureVMType, | ||
inventory.StorageAccountAssetType: fetching.AzureStorageAccountType, | ||
} | ||
|
||
func NewAzureAssetsFetcher(log *logp.Logger, ch chan fetching.ResourceInfo, provider inventory.ServiceAPI) *AzureAssetsFetcher { | ||
return &AzureAssetsFetcher{ | ||
log: log, | ||
resourceCh: ch, | ||
provider: provider, | ||
} | ||
} | ||
|
||
func (f *AzureAssetsFetcher) Fetch(ctx context.Context, cMetadata fetching.CycleMetadata) error { | ||
f.log.Info("Starting AzureAssetsFetcher.Fetch") | ||
// TODO: Maybe we should use a query per type instead of listing all assets in a single query | ||
// This might be relevant if we'd like to fetch assets in parallel in order to evaluate a rule that uses multiple resources | ||
assets, err := f.provider.ListAllAssetTypesByName(maps.Keys(AzureResourceTypes)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, asset := range assets { | ||
select { | ||
case <-ctx.Done(): | ||
f.log.Infof("AzureAssetsFetcher.Fetch context err: %s", ctx.Err().Error()) | ||
return nil | ||
case f.resourceCh <- fetching.ResourceInfo{ | ||
CycleMetadata: cMetadata, | ||
Resource: &AzureResource{ | ||
Type: AzureResourceTypes[asset.Type], | ||
SubType: getAzureSubType(asset.Type), | ||
Asset: asset, | ||
}, | ||
}: | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getAzureSubType(assetType string) string { | ||
return "" | ||
} | ||
|
||
func (f *AzureAssetsFetcher) Stop() {} | ||
|
||
func (r *AzureResource) GetData() any { | ||
return r.Asset | ||
} | ||
|
||
func (r *AzureResource) GetMetadata() (fetching.ResourceMetadata, error) { | ||
return fetching.ResourceMetadata{ | ||
ID: r.Asset.Id, | ||
Type: r.Type, | ||
SubType: r.SubType, | ||
Name: r.Asset.Name, | ||
Region: r.Asset.Location, | ||
}, nil | ||
} | ||
|
||
func (r *AzureResource) GetElasticCommonData() (map[string]any, error) { return nil, nil } |
121 changes: 121 additions & 0 deletions
121
resources/fetching/fetchers/azure/assets_fetcher_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package fetchers | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/samber/lo" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/elastic/cloudbeat/resources/fetching" | ||
"github.com/elastic/cloudbeat/resources/providers/azurelib/inventory" | ||
"github.com/elastic/cloudbeat/resources/utils/testhelper" | ||
) | ||
|
||
type AzureAssetsFetcherTestSuite struct { | ||
suite.Suite | ||
|
||
resourceCh chan fetching.ResourceInfo | ||
} | ||
|
||
func TestAzureAssetsFetcherTestSuite(t *testing.T) { | ||
s := new(AzureAssetsFetcherTestSuite) | ||
|
||
suite.Run(t, s) | ||
} | ||
|
||
func (s *AzureAssetsFetcherTestSuite) SetupTest() { | ||
s.resourceCh = make(chan fetching.ResourceInfo, 50) | ||
} | ||
|
||
func (s *AzureAssetsFetcherTestSuite) TearDownTest() { | ||
close(s.resourceCh) | ||
} | ||
|
||
func (s *AzureAssetsFetcherTestSuite) TestFetcher_Fetch() { | ||
ctx := context.Background() | ||
mockInventoryService := &inventory.MockServiceAPI{} | ||
fetcher := AzureAssetsFetcher{ | ||
log: testhelper.NewLogger(s.T()), | ||
resourceCh: s.resourceCh, | ||
provider: mockInventoryService, | ||
} | ||
|
||
mockAssets := []inventory.AzureAsset{ | ||
{ | ||
Id: "id1", | ||
Name: "name1", | ||
Location: "location1", | ||
Properties: map[string]interface{}{"key1": "value1"}, | ||
ResourceGroup: "rg1", | ||
SubscriptionId: "subId1", | ||
TenantId: "tenantId1", | ||
Type: inventory.VirtualMachineAssetType, | ||
}, | ||
{ | ||
Id: "id2", | ||
Name: "name2", | ||
Location: "location2", | ||
Properties: map[string]interface{}{"key2": "value2"}, | ||
ResourceGroup: "rg2", | ||
SubscriptionId: "subId2", | ||
TenantId: "tenantId2", | ||
Type: inventory.StorageAccountAssetType, | ||
}, | ||
} | ||
|
||
mockInventoryService.On("ListAllAssetTypesByName", mock.MatchedBy(func(assets []string) bool { | ||
return true | ||
})).Return( | ||
mockAssets, nil, | ||
) | ||
|
||
err := fetcher.Fetch(ctx, fetching.CycleMetadata{}) | ||
s.NoError(err) | ||
results := testhelper.CollectResources(s.resourceCh) | ||
|
||
s.Equal(len(AzureResourceTypes), len(results)) | ||
|
||
lo.ForEach(results, func(r fetching.ResourceInfo, index int) { | ||
data := r.GetData() | ||
s.NotNil(data) | ||
resource := data.(inventory.AzureAsset) | ||
s.NotEmpty(resource) | ||
s.Equal(mockAssets[index].Id, resource.Id) | ||
s.Equal(mockAssets[index].Name, resource.Name) | ||
s.Equal(mockAssets[index].Location, resource.Location) | ||
s.Equal(mockAssets[index].Properties, resource.Properties) | ||
s.Equal(mockAssets[index].ResourceGroup, resource.ResourceGroup) | ||
s.Equal(mockAssets[index].SubscriptionId, resource.SubscriptionId) | ||
s.Equal(mockAssets[index].TenantId, resource.TenantId) | ||
s.Equal(mockAssets[index].Type, resource.Type) | ||
meta, err := r.GetMetadata() | ||
s.NoError(err) | ||
s.NotNil(meta) | ||
s.NoError(err) | ||
s.NotEmpty(meta) | ||
s.Equal(mockAssets[index].Id, meta.ID) | ||
s.Equal(AzureResourceTypes[mockAssets[index].Type], meta.Type) | ||
s.Equal("", meta.SubType) | ||
s.Equal(mockAssets[index].Name, meta.Name) | ||
s.Equal(mockAssets[index].Location, meta.Region) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package preset | ||
|
||
import ( | ||
"github.com/elastic/elastic-agent-libs/logp" | ||
|
||
"github.com/elastic/cloudbeat/resources/fetching" | ||
fetchers "github.com/elastic/cloudbeat/resources/fetching/fetchers/azure" | ||
"github.com/elastic/cloudbeat/resources/fetching/registry" | ||
"github.com/elastic/cloudbeat/resources/providers/azurelib/inventory" | ||
) | ||
|
||
func NewCisAzureFactory(log *logp.Logger, ch chan fetching.ResourceInfo, inventory inventory.ServiceAPI) (registry.FetchersMap, error) { | ||
log.Infof("Initializing Azure fetchers") | ||
m := make(registry.FetchersMap) | ||
|
||
assetsFetcher := fetchers.NewAzureAssetsFetcher(log, ch, inventory) | ||
m["azure_cloud_assets_fetcher"] = registry.RegisteredFetcher{Fetcher: assetsFetcher} | ||
|
||
return m, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package inventory | ||
|
||
const ( | ||
VirtualMachineAssetType = "microsoft.compute/virtualmachines" | ||
StorageAccountAssetType = "microsoft.storage/storageaccounts" | ||
) |
Oops, something went wrong.