Add license object to GET /fleet/device/{token} response (#5820)

This commit is contained in:
gillespi314 2022-05-19 16:28:49 -05:00 committed by GitHub
parent fd3f88527b
commit 8e333509b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 2 deletions

View file

@ -0,0 +1 @@
- Added license object to `GET /fleet/device/{token}` response

View file

@ -24,6 +24,7 @@ type getDeviceHostResponse struct {
Host *HostDetailResponse `json:"host"`
OrgLogoURL string `json:"org_logo_url"`
Err error `json:"error,omitempty"`
License fleet.LicenseInfo `json:"license"`
}
func (r getDeviceHostResponse) error() error { return r.Err }
@ -54,9 +55,15 @@ func getDeviceHostEndpoint(ctx context.Context, request interface{}, svc fleet.S
return getDeviceHostResponse{Err: err}, nil
}
license, err := svc.License(ctx)
if err != nil {
return nil, err
}
return getDeviceHostResponse{
Host: resp,
OrgLogoURL: ac.OrgInfo.OrgLogoURL,
License: *license,
}, nil
}

View file

@ -4594,6 +4594,14 @@ func (s *integrationTestSuite) TestDeviceAuthenticatedEndpoints() {
// get macadmins for invalid token
res = s.DoRawNoAuth("GET", "/api/latest/fleet/device/no_such_token/macadmins", nil, http.StatusUnauthorized)
res.Body.Close()
// response includes license info
getHostResp = getDeviceHostResponse{}
res = s.DoRawNoAuth("GET", "/api/latest/fleet/device/"+token, nil, http.StatusOK)
json.NewDecoder(res.Body).Decode(&getHostResp)
res.Body.Close()
require.NotNil(t, getHostResp.License)
require.Equal(t, getHostResp.License.Tier, "free")
}
func (s *integrationTestSuite) TestModifyUser() {

View file

@ -7,6 +7,7 @@ import (
"strings"
"github.com/fleetdm/fleet/v4/server"
authz_ctx "github.com/fleetdm/fleet/v4/server/contexts/authz"
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
"github.com/fleetdm/fleet/v4/server/contexts/viewer"
"github.com/fleetdm/fleet/v4/server/fleet"
@ -128,8 +129,10 @@ func cleanupURL(url string) string {
}
func (svc *Service) License(ctx context.Context) (*fleet.LicenseInfo, error) {
if err := svc.authz.Authorize(ctx, &fleet.AppConfig{}, fleet.ActionRead); err != nil {
return nil, err
if !svc.authz.IsAuthenticatedWith(ctx, authz_ctx.AuthnDeviceToken) {
if err := svc.authz.Authorize(ctx, &fleet.AppConfig{}, fleet.ActionRead); err != nil {
return nil, err
}
}
return &svc.license, nil