Skip to content
Merged
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
12 changes: 8 additions & 4 deletions internal/inventory/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,15 @@ type Host struct {
Type string `json:"type,omitempty"`
IP string `json:"ip,omitempty"`
MacAddress []string `json:"mac,omitempty"`
Entity *Entity `json:"entity,omitempty"`
}

type User struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
Roles []string `json:"roles,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
Roles []string `json:"roles,omitempty"`
Entity *Entity `json:"entity,omitempty"`
}

type Orchestrator struct {
Expand Down Expand Up @@ -332,6 +334,7 @@ func WithGroup(group Group) AssetEnricher {
func WithHost(host Host) AssetEnricher {
return func(a *AssetEvent) {
a.Host = &host
a.Host.Entity = &a.Entity
}
}

Expand All @@ -347,6 +350,7 @@ func WithTags(tags []string) AssetEnricher {
func WithUser(user User) AssetEnricher {
return func(a *AssetEvent) {
a.User = &user
a.User.Entity = &a.Entity
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (f *resourceGraphFetcher) fetch(ctx context.Context, resourceName, serviceN
Name: vmProperties.Extended.InstanceView.ComputerName,
Type: vmProperties.HardwareProfile.VmSize,
}
asset.Host.Entity = &asset.Entity
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not necessary related to your changes, but I wonder why we don't use the inventory.WithHost utility here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like a bad practice in this single place.
Do we have a way to enforce those utilities?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not with linting or something like that, maybe unit tests can help enforce it, but I'd prefer the tests to be focus on a single class and not on the utilities.

asset.Cloud.MachineType = vmProperties.HardwareProfile.VmSize
}
asset.Cloud.InstanceID = item.Id
Expand Down
8 changes: 8 additions & 0 deletions internal/inventory/gcpfetcher/fetcher_assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ func TestAccountFetcher_EnrichAsset(t *testing.T) {
expected.Event = actual.Event
expected.Entity = actual.Entity

// Set Entity field on Host and User if they exist (WithHost and WithUser set this)
if expected.Host != nil {
expected.Host.Entity = &expected.Entity
}
if expected.User != nil {
expected.User.Entity = &expected.Entity
}

// Cloud is the only field where we have both common and enriched fields
if expected.Cloud == nil {
// Use the actual cloud fields when there are no cloud enrichments
Expand Down
27 changes: 15 additions & 12 deletions internal/inventory/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,22 @@ import (
func TestAssetInventory_Run(t *testing.T) {
var emptyRef *any
now := func() time.Time { return time.Date(2024, 1, 1, 1, 1, 1, 0, time.Local) }
entity := Entity{
Id: "arn:aws:ec2:us-east::ec2/234567890",
Name: "test-server",
Source: pointers.Ref(AwsCloudProvider),
AssetClassification: AssetClassification{
Category: CategoryInfrastructure,
Type: "Virtual Machine",
},
Raw: emptyRef,
}
expected := []beat.Event{
{
Meta: mapstr.M{libevents.FieldMetaIndex: "logs-cloud_asset_inventory.asset_inventory-infrastructure_virtual_machine-default"},
Timestamp: now(),
Fields: mapstr.M{
"entity": Entity{
Id: "arn:aws:ec2:us-east::ec2/234567890",
Name: "test-server",
Source: pointers.Ref(AwsCloudProvider),
AssetClassification: AssetClassification{
Category: CategoryInfrastructure,
Type: "Virtual Machine",
},
Raw: emptyRef,
},
"entity": entity,
"event": Event{
Kind: "asset",
},
Expand All @@ -64,13 +65,15 @@ func TestAssetInventory_Run(t *testing.T) {
Architecture: string(types.ArchitectureValuesX8664),
Type: "instance-type",
ID: "i-a2",
Entity: &entity,
},
"network": &Network{
Name: "vpc-id",
},
"user": &User{
ID: "a123123",
Name: "name",
ID: "a123123",
Name: "name",
Entity: &entity,
},
"related.entity": []string{"arn:aws:ec2:us-east::ec2/234567890"},
"tags": []string{"foo", "bar"},
Expand Down