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

[9.0](backport #3049) [Asset Inventory][Azure] Fix empty entity.name field for some resources #3055

Open
wants to merge 1 commit into
base: 9.0
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions internal/inventory/azurefetcher/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 azurefetcher

func pickName(sortedOptions ...string) string {
for _, option := range sortedOptions {
if option != "" {
return option
}
}
return ""
}
2 changes: 1 addition & 1 deletion internal/inventory/azurefetcher/fetcher_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (f *accountFetcher) fetch(ctx context.Context, resourceName string, functio
assetChan <- inventory.NewAssetEvent(
classification,
item.Id,
item.DisplayName,
pickName(item.DisplayName, item.Name, item.Id),
inventory.WithRawAsset(item),
inventory.WithCloud(inventory.Cloud{
Provider: inventory.AzureCloudProvider,
Expand Down
102 changes: 101 additions & 1 deletion internal/inventory/azurefetcher/fetcher_activedirectory.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (f *activedirectoryFetcher) fetchServicePrincipals(ctx context.Context, ass
assetChan <- inventory.NewAssetEvent(
inventory.AssetClassificationAzureServicePrincipal,
pointers.Deref(item.GetId()),
pointers.Deref(item.GetDisplayName()),
pickName(pointers.Deref(item.GetDisplayName()), pointers.Deref(item.GetId())),
inventory.WithRawAsset(
item.GetBackingStore().Enumerate(),
),
Expand All @@ -75,6 +75,106 @@ func (f *activedirectoryFetcher) fetchServicePrincipals(ctx context.Context, ass
AccountID: tenantId,
ServiceName: "Azure",
}),
<<<<<<< HEAD
=======
inventory.WithTags(item.GetTags()),
)
}
}

func (f *activedirectoryFetcher) fetchDirectoryRoles(ctx context.Context, assetChan chan<- inventory.AssetEvent) {
f.logger.Info("Fetching Directory Roles")
defer f.logger.Info("Fetching Directory Roles - Finished")

items, err := f.provider.ListDirectoryRoles(ctx)
if err != nil {
f.logger.Errorf("Could not fetch Directory Roles: %v", err)
}

for _, item := range items {
assetChan <- inventory.NewAssetEvent(
inventory.AssetClassificationAzureRoleDefinition,
pointers.Deref(item.GetId()),
pickName(pointers.Deref(item.GetDisplayName()), pointers.Deref(item.GetId())),
inventory.WithRawAsset(
item.GetBackingStore().Enumerate(),
),
inventory.WithCloud(inventory.Cloud{
Provider: inventory.AzureCloudProvider,
AccountID: f.tenantID,
ServiceName: "Azure",
}),
inventory.WithUser(inventory.User{
ID: pointers.Deref(item.GetId()),
Name: pointers.Deref(item.GetDisplayName()),
}),
)
}
}

func (f *activedirectoryFetcher) fetchGroups(ctx context.Context, assetChan chan<- inventory.AssetEvent) {
f.logger.Info("Fetching Groups")
defer f.logger.Info("Fetching Groups - Finished")

items, err := f.provider.ListGroups(ctx)
if err != nil {
f.logger.Errorf("Could not fetch Groups: %v", err)
}

for _, item := range items {
// TODO(kuba): How to test this without being able to test Groups?
// var labels map[string]string
// for _, l := range item.GetAssignedLabels() {
// fmt.Println(l)
// }
assetChan <- inventory.NewAssetEvent(
inventory.AssetClassificationAzureEntraGroup,
pointers.Deref(item.GetId()),
pickName(pointers.Deref(item.GetDisplayName()), pointers.Deref(item.GetId())),
inventory.WithRawAsset(
item.GetBackingStore().Enumerate(),
),
inventory.WithCloud(inventory.Cloud{
Provider: inventory.AzureCloudProvider,
AccountID: f.tenantID,
ServiceName: "Azure",
}),
inventory.WithGroup(inventory.Group{
ID: pointers.Deref(item.GetId()),
Name: pointers.Deref(item.GetDisplayName()),
}),
// inventory.WithLabels(labels),
)
}
}

func (f *activedirectoryFetcher) fetchUsers(ctx context.Context, assetChan chan<- inventory.AssetEvent) {
f.logger.Info("Fetching Users")
defer f.logger.Info("Fetching Users - Finished")

items, err := f.provider.ListUsers(ctx)
if err != nil {
f.logger.Errorf("Could not fetch Users: %v", err)
}

for _, item := range items {
assetChan <- inventory.NewAssetEvent(
inventory.AssetClassificationAzureEntraUser,
pointers.Deref(item.GetId()),
pickName(pointers.Deref(item.GetDisplayName()), pointers.Deref(item.GetId())),
inventory.WithRawAsset(
item.GetBackingStore().Enumerate(),
),
inventory.WithCloud(inventory.Cloud{
Provider: inventory.AzureCloudProvider,
AccountID: f.tenantID,
ServiceName: "Azure",
}),
inventory.WithUser(inventory.User{
ID: pointers.Deref(item.GetId()),
Name: pointers.Deref(item.GetDisplayName()),
}),
>>>>>>> cdb8727 ([Asset Inventory][Azure] Fix empty `entity.name` field for some resources (#3049))
)
}
}
6 changes: 5 additions & 1 deletion internal/inventory/azurefetcher/fetcher_resource_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,18 @@ func (f *resourceGraphFetcher) fetch(ctx context.Context, resourceName, resource
}

for _, item := range azureAssets {
<<<<<<< HEAD
name := item.Name
if name == "" {
name = item.DisplayName
}
assetChan <- inventory.NewAssetEvent(
=======
asset := inventory.NewAssetEvent(
>>>>>>> cdb8727 ([Asset Inventory][Azure] Fix empty `entity.name` field for some resources (#3049))
classification,
item.Id,
name,
pickName(item.Name, item.DisplayName, item.Id),
inventory.WithRawAsset(item),
inventory.WithCloud(inventory.Cloud{
Provider: inventory.AzureCloudProvider,
Expand Down
2 changes: 1 addition & 1 deletion internal/inventory/azurefetcher/fetcher_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (f *storageFetcher) fetch(ctx context.Context, storageAccounts []azurelib.A
assetChan <- inventory.NewAssetEvent(
classification,
item.Id,
item.DisplayName,
pickName(item.DisplayName, item.Name, item.Id),
inventory.WithRawAsset(item),
inventory.WithCloud(inventory.Cloud{
Provider: inventory.AzureCloudProvider,
Expand Down
Loading