List policies for hosts even if they don't have stats (#2865)

This commit is contained in:
Tomas Touceda 2021-11-11 08:40:32 -03:00 committed by GitHub
parent e1f273fdc0
commit 88b32d8c7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 6 deletions

View File

@ -0,0 +1 @@
* List policies as part of hosts right after they are created.

View File

@ -933,12 +933,12 @@ func (d *Datastore) ListPoliciesForHost(ctx context.Context, hid uint) (packs []
END AS response,
q.description,
coalesce(p.resolution, '') as resolution
FROM (
FROM policies p
LEFT JOIN (
SELECT * FROM policy_membership_history WHERE id IN (
SELECT max(id) AS id FROM policy_membership_history WHERE host_id=? GROUP BY host_id, policy_id
)
) as pm
JOIN policies p ON (p.id=pm.policy_id)
) as pm ON (p.id=pm.policy_id)
JOIN queries q ON (p.query_id=q.id)`
var policies []*fleet.HostPolicy

View File

@ -300,9 +300,17 @@ func testPolicyQueriesForHost(t *testing.T, ds *Datastore) {
policies, err = ds.ListPoliciesForHost(context.Background(), host2.ID)
require.NoError(t, err)
require.Len(t, policies, 0)
require.Len(t, policies, 2)
require.NoError(t, ds.RecordPolicyQueryExecutions(context.Background(), host2, map[uint]*bool{gp.ID: nil}, time.Now(), false))
assert.Equal(t, "", policies[0].Response)
require.NoError(t, ds.RecordPolicyQueryExecutions(context.Background(), host2, map[uint]*bool{gp.ID: ptr.Bool(true)}, time.Now(), false))
policies, err = ds.ListPoliciesForHost(context.Background(), host2.ID)
require.NoError(t, err)
require.Len(t, policies, 2)
assert.Equal(t, "pass", policies[0].Response)
// insert a null resolution
res, err := ds.writer.ExecContext(context.Background(), `INSERT INTO policies (query_id) VALUES (?)`, q.ID)
@ -313,7 +321,7 @@ func testPolicyQueriesForHost(t *testing.T, ds *Datastore) {
policies, err = ds.ListPoliciesForHost(context.Background(), host2.ID)
require.NoError(t, err)
require.Len(t, policies, 2)
require.Len(t, policies, 3)
assert.Equal(t, "query1 desc", policies[0].QueryDescription)
assert.Equal(t, "some gp resolution", policies[0].Resolution)