2021-07-21 17:03:10 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
)
|
|
|
|
|
|
|
|
type translatorRequest struct {
|
|
|
|
List []fleet.TranslatePayload `json:"list"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type translatorResponse struct {
|
|
|
|
List []fleet.TranslatePayload `json:"list"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r translatorResponse) error() error { return r.Err }
|
|
|
|
|
2022-12-27 14:26:59 +00:00
|
|
|
func translatorEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (errorer, error) {
|
2021-07-21 17:03:10 +00:00
|
|
|
req := request.(*translatorRequest)
|
|
|
|
resp, err := svc.Translate(ctx, req.List)
|
|
|
|
if err != nil {
|
|
|
|
return translatorResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return translatorResponse{List: resp}, nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
type translateFunc func(ctx context.Context, ds fleet.Datastore, identifier string) (uint, error)
|
2021-07-21 17:03:10 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
func translateEmailToUserID(ctx context.Context, ds fleet.Datastore, identifier string) (uint, error) {
|
|
|
|
user, err := ds.UserByEmail(ctx, identifier)
|
2021-07-21 17:03:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return user.ID, nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
func translateLabelToID(ctx context.Context, ds fleet.Datastore, identifier string) (uint, error) {
|
|
|
|
labelIDs, err := ds.LabelIDsByName(ctx, []string{identifier})
|
2021-07-21 17:03:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return labelIDs[0], nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
func translateTeamToID(ctx context.Context, ds fleet.Datastore, identifier string) (uint, error) {
|
|
|
|
team, err := ds.TeamByName(ctx, identifier)
|
2021-07-21 17:03:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return team.ID, nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
func translateHostToID(ctx context.Context, ds fleet.Datastore, identifier string) (uint, error) {
|
|
|
|
host, err := ds.HostByIdentifier(ctx, identifier)
|
2021-07-21 17:03:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return host.ID, nil
|
|
|
|
}
|
|
|
|
|
2022-03-08 16:27:38 +00:00
|
|
|
func (svc *Service) Translate(ctx context.Context, payloads []fleet.TranslatePayload) ([]fleet.TranslatePayload, error) {
|
2021-07-21 17:03:10 +00:00
|
|
|
var finalPayload []fleet.TranslatePayload
|
|
|
|
|
|
|
|
for _, payload := range payloads {
|
|
|
|
var translateFunc translateFunc
|
|
|
|
|
|
|
|
switch payload.Type {
|
|
|
|
case fleet.TranslatorTypeUserEmail:
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.User{}, fleet.ActionRead); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
translateFunc = translateEmailToUserID
|
|
|
|
case fleet.TranslatorTypeLabel:
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Label{}, fleet.ActionRead); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
translateFunc = translateLabelToID
|
|
|
|
case fleet.TranslatorTypeTeam:
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Team{}, fleet.ActionRead); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
translateFunc = translateTeamToID
|
|
|
|
case fleet.TranslatorTypeHost:
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Host{}, fleet.ActionRead); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
translateFunc = translateHostToID
|
|
|
|
default:
|
|
|
|
return nil, fleet.NewErrorf(fleet.ErrNoUnknownTranslate, "Type %s is unknown.", payload.Type)
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
id, err := translateFunc(ctx, svc.ds, payload.Payload.Identifier)
|
2021-07-21 17:03:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
payload.Payload.ID = id
|
|
|
|
finalPayload = append(finalPayload, fleet.TranslatePayload{
|
|
|
|
Type: payload.Type,
|
|
|
|
Payload: payload.Payload,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return finalPayload, nil
|
|
|
|
}
|