From cceef296f8729bb04a1e16245c07d84b375f7c5b Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Thu, 4 Jul 2024 14:32:39 +0200 Subject: [PATCH 1/3] Extend the ProfileRow interface for selectors The ProfileRow interface is used to abstract DB rows list with or without labels that are subsequently passed to `MergeDatabaseListIntoProfiles`. Let's extend the interface with a method to get selectors so we can use them in `MergeDatabaseListIntoProfiles`. Fixes: #3723 --- internal/db/domain.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/internal/db/domain.go b/internal/db/domain.go index 74159151de..75ab139d6c 100644 --- a/internal/db/domain.go +++ b/internal/db/domain.go @@ -32,6 +32,7 @@ func (p *Provider) CanImplement(impl ProviderType) bool { type ProfileRow interface { GetProfile() Profile GetEntityProfile() NullEntities + GetSelectors() []ProfileSelector GetContextualRules() pqtype.NullRawMessage } @@ -50,6 +51,11 @@ func (r ListProfilesByProjectIDAndLabelRow) GetContextualRules() pqtype.NullRawM return r.ProfilesWithEntityProfile.ContextualRules } +// GetSelectors returns the selectors +func (r ListProfilesByProjectIDAndLabelRow) GetSelectors() []ProfileSelector { + return r.ProfilesWithSelectors +} + // GetProfile returns the profile func (r ListProfilesByProjectIDRow) GetProfile() Profile { return r.Profile @@ -65,6 +71,11 @@ func (r ListProfilesByProjectIDRow) GetContextualRules() pqtype.NullRawMessage { return r.ProfilesWithEntityProfile.ContextualRules } +// GetSelectors returns the selectors +func (r ListProfilesByProjectIDRow) GetSelectors() []ProfileSelector { + return r.ProfilesWithSelectors +} + // LabelsFromFilter parses the filter string and populates the IncludeLabels and ExcludeLabels fields func (lp *ListProfilesByProjectIDAndLabelParams) LabelsFromFilter(filter string) { // If s does not contain sep and sep is not empty, Split returns a From 134d719919adfbfaa8bde09dd67f2d551fcad085 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Thu, 4 Jul 2024 14:35:56 +0200 Subject: [PATCH 2/3] Add selectors to profile in `MergeDatabaseListIntoProfiles` `MergeDatabaseListIntoProfiles` creates a protobuf representation of a profile from a database list. Extend the function with the profile selectors from the database list. Related: #3723 --- internal/profiles/util.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/internal/profiles/util.go b/internal/profiles/util.go index 766b42913b..5e85ff96d4 100644 --- a/internal/profiles/util.go +++ b/internal/profiles/util.go @@ -212,6 +212,8 @@ func MergeDatabaseListIntoProfiles[T db.ProfileRow](ppl []T) map[string]*pb.Prof } else { profiles[p.GetProfile().Name].Alert = proto.String(string(db.ActionTypeOn)) } + + selectorsToProfile(profiles[p.GetProfile().Name], p.GetSelectors()) } if pm := rowInfoToProfileMap( profiles[p.GetProfile().Name], p.GetEntityProfile(), @@ -325,3 +327,18 @@ func rowInfoToProfileMap( return profile } + +func selectorsToProfile( + profile *pb.Profile, + selectors []db.ProfileSelector, +) { + profile.Selection = make([]*pb.Profile_Selector, 0, len(selectors)) + for _, s := range selectors { + profile.Selection = append(profile.Selection, &pb.Profile_Selector{ + Id: s.ID.String(), + Entity: string(s.Entity.Entities), + Selector: s.Selector, + Comment: s.Comment, + }) + } +} From b43d38bc1f97430ed641bd94c8bdebff0c678e16 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Tue, 9 Jul 2024 10:18:57 +0200 Subject: [PATCH 3/3] `MergeDatabaseGetIntoProfiles` includes selectors This will be used in selector handlers. Related: #3723 --- internal/profiles/util.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/profiles/util.go b/internal/profiles/util.go index 5e85ff96d4..6eb336d26c 100644 --- a/internal/profiles/util.go +++ b/internal/profiles/util.go @@ -268,6 +268,8 @@ func MergeDatabaseGetIntoProfiles(ppl []db.GetProfileByProjectAndIDRow) map[stri } else { profiles[p.Profile.Name].Alert = proto.String(string(db.ActionTypeOn)) } + + selectorsToProfile(profiles[p.Profile.Name], p.ProfilesWithSelectors) } if pm := rowInfoToProfileMap( profiles[p.Profile.Name],