Skip to content

Commit

Permalink
Extend the db-to-pb profile code to include selectors (#3854)
Browse files Browse the repository at this point in the history
* 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

* 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

* `MergeDatabaseGetIntoProfiles` includes selectors

This will be used in selector handlers.

Related: #3723
  • Loading branch information
jhrozek authored and dmjb committed Jul 12, 2024
1 parent 545ec35 commit fd569c7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/db/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (p *Provider) CanImplement(impl ProviderType) bool {
type ProfileRow interface {
GetProfile() Profile
GetEntityProfile() NullEntities
GetSelectors() []ProfileSelector
GetContextualRules() pqtype.NullRawMessage
}

Expand All @@ -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
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions internal/profiles/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -266,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],
Expand Down Expand Up @@ -325,3 +329,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,
})
}
}

0 comments on commit fd569c7

Please sign in to comment.