mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 08:55:24 +00:00
List policies for hosts even if they don't have stats (#2865)
This commit is contained in:
parent
e1f273fdc0
commit
88b32d8c7f
@ -0,0 +1 @@
|
||||
* List policies as part of hosts right after they are created.
|
@ -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
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user