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

Extend the db-to-pb profile code to include selectors #3854

Merged
merged 3 commits into from
Jul 11, 2024
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
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,
})
}
}