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

Project filtering for reporting server ListProfiles endpoint. #464

Merged
merged 6 commits into from
Jun 5, 2019
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
6 changes: 6 additions & 0 deletions components/compliance-service/api/reporting/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ func (srv *Server) ListProfiles(ctx context.Context, in *reporting.Query) (*repo
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

formattedFilters := formatFilters(in.Filters)
formattedFilters, err = filterByProjects(ctx, formattedFilters)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

profiles, counts, err := srv.es.GetAllProfilesFromNodes(from, perPage, formattedFilters, SORT_FIELDS[sort], asc)
if err != nil {
return nil, utils.FormatErrorMsg(err, "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,12 @@ func TestListNodesFiltering(t *testing.T) {

for _, test := range cases {
t.Run(test.description, func(t *testing.T) {
suite.InsertInspecReports(test.reports)
_, err := suite.InsertInspecReports(test.reports)
require.NoError(t, err)
defer suite.DeleteAllDocuments()

response, err := server.ListNodes(ctx, &test.query)
assert.NoError(t, err)
require.NoError(t, err)
require.NotNil(t, response)

actualIds := make([]string, len(response.Nodes))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package integration_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

iam_v2 "github.com/chef/automate/api/interservice/authz/v2"
authzConstants "github.com/chef/automate/components/authz-service/constants/v2"
"github.com/chef/automate/components/compliance-service/api/reporting"
reportingServer "github.com/chef/automate/components/compliance-service/api/reporting/server"
"github.com/chef/automate/components/compliance-service/ingest/events/compliance"
"github.com/chef/automate/components/compliance-service/inspec"
"github.com/chef/automate/components/compliance-service/reporting/relaxting"
)

func TestListProfiles(t *testing.T) {
es2Backend := relaxting.ES2Backend{ESUrl: elasticsearchUrl}
server := reportingServer.New(&es2Backend)

reportFileName := "../ingest/examples/compliance-success-tiny-report.json"
everythingCtx := contextWithProjects([]string{authzConstants.AllProjectsExternalID})

n := 5

reportIds := make([]string, n)

for i := 0; i < n; i++ {
err := suite.ingestReport(reportFileName, func(r *compliance.Report) {
id := newUUID()

r.Profiles[0].Controls = r.Profiles[0].Controls[:1]
r.Profiles[0].Controls[0].Id = id
r.Profiles = r.Profiles[:1]
r.Profiles[0].Sha256 = id
r.ReportUuid = id
r.Roles = []string{id}

reportIds[i] = id
})

require.NoError(t, err)
}

defer suite.DeleteAllDocuments()

waitFor(func() bool {
response, _ := server.ListReports(everythingCtx, &reporting.Query{})

return response != nil && len(response.Reports) == n
})

reportsProjects := map[string][]string{
"project1": reportIds[1:3],
"project2": reportIds[2:5],
"project3": reportIds[3:],
}

projectRules := map[string]*iam_v2.ProjectRules{}
for k, v := range reportsProjects {
projectRules[k] = &iam_v2.ProjectRules{
Rules: []*iam_v2.ProjectRule{
{
Conditions: []*iam_v2.Condition{
{
Type: iam_v2.ProjectRuleConditionTypes_ROLES,
Values: v,
},
},
},
},
}
}

// Send a project rules update event
esJobID, err := suite.ingesticESClient.UpdateReportProjectsTags(everythingCtx, projectRules)
require.NoError(t, err)

suite.WaitForESJobToComplete(esJobID)

suite.RefreshComplianceReportIndex()

profileIds := make([]string, len(reportIds))
for i := range reportIds {
profileId := reportIds[i]
profileIds[i] = profileId

profile := inspec.Profile{
Name: "a",
Title: "b",
Version: "1",
Sha256: profileId,
}

err := es2Backend.StoreProfile(profile)
require.NoError(t, err)
}

successCases := []struct {
description string
allowedProjects []string
expectedIds []string
}{
{
description: "Projects: user has access to all projects",
allowedProjects: []string{authzConstants.AllProjectsExternalID},
expectedIds: profileIds,
},
{
description: "Projects: user has access to one project with reports",
allowedProjects: []string{"project1"},
expectedIds: profileIds[1:3],
},
{
description: "Projects: user has access to some projects with reports",
allowedProjects: []string{"project1", "project2"},
expectedIds: profileIds[1:5],
},
{
description: "Projects: user has access to projects without reports",
allowedProjects: []string{"project4", "project5"},
expectedIds: []string{},
},
{
description: "Projects: user has access to one project with reports and unassigned reports",
allowedProjects: []string{"project1", authzConstants.UnassignedProjectID},
expectedIds: profileIds[:3],
},
{
description: "Projects: user has access to some projects with reports and unassigned reports",
allowedProjects: []string{"project1", "project2", authzConstants.UnassignedProjectID},
expectedIds: profileIds[:5],
},
{
description: "Projects: user has access to projects without reports and unassigned reports",
allowedProjects: []string{"project4", "project5", authzConstants.UnassignedProjectID},
expectedIds: profileIds[:1],
},
{
description: "Projects: user has access to unassigned reports",
allowedProjects: []string{authzConstants.UnassignedProjectID},
expectedIds: profileIds[:1],
},
}

for _, test := range successCases {
t.Run(test.description, func(t *testing.T) {
ctx := contextWithProjects(test.allowedProjects)

query := reporting.Query{Filters: []*reporting.ListFilter{{Type: "end_time", Values: []string{"2018-10-25T23:59:59Z"}}}}

response, err := server.ListProfiles(ctx, &query)

assert.NoError(t, err)
require.NotNil(t, response)

assert.Equal(t, len(test.expectedIds), int(response.Counts.Total))

actualIds := make([]string, len(response.Profiles))
for i, profile := range response.Profiles {
actualIds[i] = profile.Id
}

assert.ElementsMatch(t, test.expectedIds, actualIds)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,12 @@ func TestReportingListSuggestionsFiltering(t *testing.T) {

for _, test := range cases {
t.Run(test.description, func(t *testing.T) {
suite.InsertInspecSummaries(test.summaries)
_, err := suite.InsertInspecSummaries(test.summaries)
require.NoError(t, err)
defer suite.DeleteAllDocuments()

response, err := server.ListSuggestions(ctx, &test.request)
assert.NoError(t, err)
require.NoError(t, err)
require.NotNil(t, response)

actualTerms := make([]string, len(response.Suggestions))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,7 @@ func TestProjectUpdate(t *testing.T) {
_, err = suite.InsertInspecSummaries([]*relaxting.ESInSpecSummary{test.summary})
require.NoError(t, err)

defer suite.DeleteAllDocuments()
// Send a project rules update event
esJobID, err := suite.ingesticESClient.UpdateReportProjectsTags(ctx, test.projects)
require.NoError(t, err)
Expand Down Expand Up @@ -1329,8 +1330,6 @@ func TestProjectUpdate(t *testing.T) {
updatedSummary := summaries[0]

assert.ElementsMatch(t, test.projectIDs, updatedSummary.Projects)

suite.DeleteAllDocuments()
})
}
}
Expand Down
35 changes: 2 additions & 33 deletions components/compliance-service/integration_test/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,50 +363,19 @@ func (s *Suite) DeleteAllDocuments() {
// ES Query to match all documents
q := elastic.RawStringQuery("{\"match_all\":{}}")

// Make sure we clean them all!
indices, _ := s.elasticClient.IndexNames()

_, err := s.elasticClient.DeleteByQuery().
Index(indices...).
Type(s.types()...).
_, err := s.elasticClient.DeleteByQuery("_all").
Query(q).
IgnoreUnavailable(true).
Refresh("true").
WaitForCompletion(true).
Do(context.Background())

if err != nil {
fmt.Printf("Could not 'clean' ES documents from indices: '%v'\nError: %s", indices, err)
fmt.Printf("Could not 'clean' ES documents.\nError: %s", err)
os.Exit(3)
}
}

// types returns the list of ES types registered in the Ingest service code base
func (s *Suite) types() []string {
types := make([]string, len(mappings.AllMappings))

for i, esMap := range mappings.AllMappings {
types[i] = esMap.Type
}

return types
}

// Indices returns the list of ES indices registered in the Ingest service code base
func (s *Suite) Indices() []string {
indices := make([]string, len(mappings.AllMappings))

for i, esMap := range mappings.AllMappings {
if esMap.Timeseries {
indices[i] = esMap.IndexTimeseriesFmt(time.Now())
} else {
indices[i] = esMap.Index
}
}

return indices
}

type NotifierMock struct {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (depth *ProfileDepth) getProfileMinsFromNodesResults(
filters map[string][]string,
searchResult *elastic.SearchResult,
statusFilters []string) (map[string]reporting.ProfileMin, *reportingapi.ProfileCounts, error) {
myName := "ProfileDepth::getProfileMinsFromNodes"
myName := "ProfileDepth::getProfileMinsFromNodesResults"

profileMins := make(map[string]reporting.ProfileMin)
statusMap := make(map[string]int, 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ func (depth *ReportDepth) getProfileMinsFromNodesResults(
var profileStatus string
if profileStatusHash["skipped"] > 0 && profileStatusHash["loaded"] == 0 && profileStatusHash[""] == 0 {
profileStatus = "skipped"
logrus.Debugf("getProfileMinsFromNodes profile_name=%q, status=%q", profileName, profileStatus)
logrus.Debugf("getProfileMinsFromNodesResults profile_name=%q, status=%q", profileName, profileStatus)
} else {
sumFailures, _ := bucket.Aggregations.Sum("failures")
sumPassed, _ := bucket.Aggregations.Sum("passed")
sumSkipped, _ := bucket.Aggregations.Sum("skipped")
profileStatus = computeStatus(int32(*sumFailures.Value), int32(*sumPassed.Value), int32(*sumSkipped.Value))
logrus.Debugf("getProfileMinsFromNodes profile_name=%s, status=%s (sumFailures=%d, sumPassed=%d, sumSkipped=%d)", profileName, profileStatus, int32(*sumFailures.Value), int32(*sumPassed.Value), int32(*sumSkipped.Value))
logrus.Debugf("getProfileMinsFromNodesResults profile_name=%s, status=%s (sumFailures=%d, sumPassed=%d, sumSkipped=%d)", profileName, profileStatus, int32(*sumFailures.Value), int32(*sumPassed.Value), int32(*sumSkipped.Value))
}

if len(statusFilters) > 0 && !stringutils.SliceContains(statusFilters, profileStatus) {
Expand Down