HotFix - ambiguous policy search name (#15312)

This commit is contained in:
Tim Lee 2023-11-27 12:21:39 -07:00 committed by GitHub
parent 9c7b740b03
commit b2568ad475
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 4 deletions

View File

@ -21,7 +21,7 @@ const policyCols = `
p.author_id, p.platforms, p.created_at, p.updated_at, p.critical
`
var policySearchColumns = []string{"name"}
var policySearchColumns = []string{"p.name"}
func (ds *Datastore) NewGlobalPolicy(ctx context.Context, authorID *uint, args fleet.PolicyPayload) (*fleet.Policy, error) {
if args.QueryID != nil {
@ -380,9 +380,9 @@ func (ds *Datastore) CountPolicies(ctx context.Context, teamID *uint, matchQuery
)
if teamID == nil {
query = `SELECT count(*) FROM policies WHERE team_id IS NULL`
query = `SELECT count(*) FROM policies p WHERE team_id IS NULL`
} else {
query = `SELECT count(*) FROM policies WHERE team_id = ?`
query = `SELECT count(*) FROM policies p WHERE team_id = ?`
args = append(args, *teamID)
}

View File

@ -54,6 +54,7 @@ func TestPolicies(t *testing.T) {
{"TestListTeamPoliciesCanPaginate", testListTeamPoliciesCanPaginate},
{"TestCountPolicies", testCountPolicies},
{"TestUpdatePolicyHostCounts", testUpdatePolicyHostCounts},
{"TestPoliciesListOptions", testPoliciesListOptions},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
@ -203,6 +204,58 @@ func testPoliciesNewGlobalPolicyProprietary(t *testing.T, ds *Datastore) {
assert.Equal(t, user1.ID, *p3.AuthorID)
}
func testPoliciesListOptions(t *testing.T, ds *Datastore) {
user1 := test.NewUser(t, ds, "Alice", "alice@example.com", true)
ctx := context.Background()
_, err := ds.NewGlobalPolicy(ctx, &user1.ID, fleet.PolicyPayload{
Name: "apple",
Query: "select 1;",
Description: "query1 desc",
Resolution: "query1 resolution",
})
require.NoError(t, err)
_, err = ds.NewGlobalPolicy(ctx, &user1.ID, fleet.PolicyPayload{
Name: "banana",
Query: "select 1;",
Description: "query2 desc",
Resolution: "query2 resolution",
})
require.NoError(t, err)
_, err = ds.NewGlobalPolicy(ctx, &user1.ID, fleet.PolicyPayload{
Name: "cherry",
Query: "select 1;",
Description: "query3 desc",
Resolution: "query3 resolution",
})
require.NoError(t, err)
_, err = ds.NewGlobalPolicy(ctx, &user1.ID, fleet.PolicyPayload{
Name: "apple pie",
Query: "select 1;",
Description: "query4 desc",
Resolution: "query4 resolution",
})
require.NoError(t, err)
_, err = ds.NewGlobalPolicy(ctx, &user1.ID, fleet.PolicyPayload{
Name: "rotten apple",
Query: "select 1;",
Description: "query5 desc",
Resolution: "query5 resolution",
})
require.NoError(t, err)
policies, err := ds.ListGlobalPolicies(ctx, fleet.ListOptions{MatchQuery: "apple", OrderKey: "name", OrderDirection: fleet.OrderAscending})
require.NoError(t, err)
require.Len(t, policies, 3)
assert.Equal(t, "apple", policies[0].Name)
assert.Equal(t, "apple pie", policies[1].Name)
assert.Equal(t, "rotten apple", policies[2].Name)
}
func testPoliciesMembershipView(deferred bool, t *testing.T, ds *Datastore) {
ctx := context.Background()

View File

@ -27,5 +27,22 @@ func TestIntegrationSync(t *testing.T) {
}
require.NoError(t, err)
require.Contains(t, filesInVulnPath, io.MacOfficeRelNotesFileName(time.Now()))
// Checking for the presence of the file for today or yesterday
// in case the NVD repo is having delays publishing the data
todayFilename := io.MacOfficeRelNotesFileName(time.Now())
yesterdayFilename := io.MacOfficeRelNotesFileName(time.Now().AddDate(0, 0, -1))
require.Condition(t, func() bool {
return contains(filesInVulnPath, todayFilename) || contains(filesInVulnPath, yesterdayFilename)
}, "Expected to find %s or %s in %s", todayFilename, yesterdayFilename, vulnPath)
}
func contains(slice []string, str string) bool {
for _, v := range slice {
if v == str {
return true
}
}
return false
}