Allow to refetch hosts if you are an observer/maintainer for the team for that host (#1649)

This commit is contained in:
Tomas Touceda 2021-08-11 19:01:37 -03:00 committed by GitHub
parent d88fac6b76
commit e8def4af00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 4 deletions

View file

@ -0,0 +1 @@
* Allow observers and maintainers to refetch a host in a team they belong to.

View file

@ -183,10 +183,6 @@ func (svc Service) AddHostsToTeamByFilter(ctx context.Context, teamID *uint, opt
}
func (svc *Service) RefetchHost(ctx context.Context, id uint) error {
if err := svc.authz.Authorize(ctx, &fleet.Host{}, fleet.ActionRead); err != nil {
return err
}
host, err := svc.ds.Host(id)
if err != nil {
return errors.Wrap(err, "find host for refetch")

View file

@ -110,6 +110,41 @@ func TestRefetchHost(t *testing.T) {
}
require.NoError(t, svc.RefetchHost(test.UserContext(test.UserAdmin), host.ID))
require.NoError(t, svc.RefetchHost(test.UserContext(test.UserObserver), host.ID))
require.NoError(t, svc.RefetchHost(test.UserContext(test.UserMaintainer), host.ID))
}
func TestRefetchHostUserInTeams(t *testing.T) {
ds := new(mock.Store)
svc := newTestService(ds, nil, nil)
host := &fleet.Host{ID: 3, TeamID: ptr.Uint(4)}
ds.HostFunc = func(hid uint) (*fleet.Host, error) {
return host, nil
}
ds.SaveHostFunc = func(host *fleet.Host) error {
assert.True(t, host.RefetchRequested)
return nil
}
maintainer := &fleet.User{
Teams: []fleet.UserTeam{
{
Team: fleet.Team{ID: 4},
Role: fleet.RoleMaintainer,
},
}}
require.NoError(t, svc.RefetchHost(test.UserContext(maintainer), host.ID))
observer := &fleet.User{
Teams: []fleet.UserTeam{
{
Team: fleet.Team{ID: 4},
Role: fleet.RoleObserver,
},
}}
require.NoError(t, svc.RefetchHost(test.UserContext(observer), host.ID))
}
func TestAddHostsToTeamByFilter(t *testing.T) {