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

Implement Azure fetchers and asset provider #1310

Merged
merged 20 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
cloud.google.com/go/asset v1.14.1
cloud.google.com/go/iam v1.1.2
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.1
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph v0.8.1
github.com/aquasecurity/go-dep-parser v0.0.0-20230605080024-b71d9356a6c6
github.com/aquasecurity/trivy v0.42.1
github.com/aquasecurity/trivy-db v0.0.0-20230515061101-378ab9ed302c
Expand Down Expand Up @@ -94,7 +95,7 @@ require (
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1 // indirect
github.com/AdamKorcz/go-118-fuzz-build v0.0.0-20221215162035-5330a85ea652 // indirect
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.7.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.8.0-beta.3
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
Expand Down Expand Up @@ -454,7 +455,7 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
google.golang.org/protobuf v1.30.0
gopkg.in/cheggaaa/pb.v1 v1.0.28 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,14 @@ github.com/Azure/azure-sdk-for-go v16.2.1+incompatible/go.mod h1:9XXNKU+eRnpl9mo
github.com/Azure/azure-sdk-for-go v56.3.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible h1:fcYLmCpyNYRnvJbPerq7U0hS+6+I79yEDJBqVNcqUzU=
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.7.1 h1:/iHxaJhsFr0+xVFfbMr5vxz848jyiWuIEDhYq3y5odY=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.7.1/go.mod h1:bjGvMhVMb+EEm3VRNQawDMUyMMjo+S5ewNjflkep/0Q=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.8.0-beta.3 h1:XV/HZqgyUQQAc1/UwYXK/p9PyPuDrprwSXcKARy183U=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.8.0-beta.3/go.mod h1:bjGvMhVMb+EEm3VRNQawDMUyMMjo+S5ewNjflkep/0Q=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.1 h1:LNHhpdK7hzUcx/k1LIcuh5k7k1LGIWLQfCjaneSj7Fc=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.1/go.mod h1:uE9zaUfEQT/nbQjVi2IblCG9iaLtZsuYZ8ne+PuQ02M=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 h1:sXr+ck84g/ZlZUOZiNELInmMgOsuGwdjjVkEIde0OtY=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0/go.mod h1:okt5dMMTOFjX/aovMlrjvvXoPMBVSPzk9185BT0+eZM=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph v0.8.1 h1:nGiU2ovpbtkcC3x+g/wNHV4S9TOIYe2/yOVAj3wiGHI=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph v0.8.1/go.mod h1:T3ZgvD1aRKu12mEA0fU3PPvI7V0Nh0wzIdK0QMBhf0Y=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
github.com/Azure/go-ansiterm v0.0.0-20210608223527-2377c96fe795/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
Expand Down
8 changes: 8 additions & 0 deletions resources/fetching/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ const (
KeyManagement = "key-management"
ProjectManagement = "project-management"
DataProcessing = "data-processing"

// Azure resource types
AzureVMType = "azure-vm"
AzureStorageAccountType = "azure-storage-account"
AzureNetworkInterface = "azure-network-interface"
AzureApplicationService = "azure-application-service"
AzureLoggingAndMonitoring = "azure-logging-and-monitoring"
AzureDatabaseService = "azure-database-service"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
AzureNetworkInterface = "azure-network-interface"
AzureApplicationService = "azure-application-service"
AzureLoggingAndMonitoring = "azure-logging-and-monitoring"
AzureDatabaseService = "azure-database-service"

Let's add these one by one as we implement the rules. We can merge this PR without them.

)

// Fetcher represents a data fetcher.
Expand Down
139 changes: 139 additions & 0 deletions resources/fetching/fetchers/azure/assets_fetcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// 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 AzureAsset struct {
Type string
SubType string
Asset AzureAssetInfo `json:"asset,omitempty"`
}

// TODO: Fill this struct with the required fields
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this done?

Suggested change
// TODO: Fill this struct with the required fields

type AzureAssetInfo struct {
Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Location string `json:"location,omitempty"`
Properties map[string]any `json:"properties,omitempty"`
ResourceGroup string `json:"resource_group,omitempty"`
SubscriptionId string `json:"subscription_id,omitempty"`
TenantId string `json:"tenant_id,omitempty"`
}

// TODO: Implement other types
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// TODO: Implement other types

Can we track this in a new issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is basically the other piece of work regarding the comment above:
Let's add these one by one as we implement the rules. We can merge this PR without them

var AzureAssetTypes = map[string]string{
"microsoft.compute/virtualmachines": fetching.AzureVMType,
"microsoft.storage/storageaccounts": 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// TODO: Maybe we should use a query per type instead of listing all assets in a single query

Is there any benefit to that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that it might help us If we will need to collect multiple resource types to evaluate a single rule.

assets, err := f.provider.ListAllAssetTypesByName(maps.Keys(AzureAssetTypes))
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,
// TODO: Safe guard this conversion
Resource: getAssetFromData(asset.(map[string]any)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't ListAllAssetTypesByName return the correct type / handle the casting instead of the caller?

}:
}
}

return nil
}

func getAssetFromData(data map[string]any) *AzureAsset {
assetType := getString(data, "type")
properties, _ := data["properties"].(map[string]any)

return &AzureAsset{
Type: AzureAssetTypes[assetType],
SubType: getAzureSubType(assetType),
Asset: AzureAssetInfo{
Id: getString(data, "id"),
Name: getString(data, "name"),
Location: getString(data, "location"),
Properties: properties,
ResourceGroup: getString(data, "resourceGroup"),
SubscriptionId: getString(data, "subscriptionId"),
TenantId: getString(data, "tenantId"),
},
}
}

func getString(data map[string]any, key string) string {
value, _ := data[key].(string)
return value
}

// TODO: Handle this function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's resolve the TODOs, is this relevant?

func (f *AzureAssetsFetcher) Stop() {
// f.provider.Close()
}

func (r *AzureAsset) GetData() any {
return r.Asset
}

func (r *AzureAsset) 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 *AzureAsset) GetElasticCommonData() (map[string]any, error) { return nil, nil }

// TODO: Implement this function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the significance of this? Is it a blocker?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not a blocker because we did not decide on the subTypes yet.
As of current we only utilize the resource type and do not look at subTypes in the policies.
Future implementation of additional rules might change that, this is why it is left blank as of current.

func getAzureSubType(assetType string) string {
return ""
}
63 changes: 63 additions & 0 deletions resources/fetching/fetchers/azure/assets_fetcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_getString(t *testing.T) {
tests := []struct {
name string
data map[string]any
key string
want string
}{
{
name: "nil map",
data: nil,
key: "key",
want: "",
},
{
name: "key does not exist",
data: map[string]any{"key": "value"},
key: "other-key",
want: "",
},
{
name: "wrong type",
data: map[string]any{"key": 1},
key: "key",
want: "",
},
{
name: "correct value",
data: map[string]any{"key": "value", "other-key": 1},
key: "key",
want: "value",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, getString(tt.data, tt.key), "getString(%v, %s) = %s", tt.data, tt.key, tt.want)
})
}
}
37 changes: 37 additions & 0 deletions resources/fetching/preset/azure_factory.go
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
}
Loading