Skip to content

Commit

Permalink
Move error checking back up to service method so error can be logged
Browse files Browse the repository at this point in the history
  • Loading branch information
dantecatalfamo committed Jan 7, 2025
1 parent a3d6bae commit 52b66ab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
17 changes: 17 additions & 0 deletions ee/server/service/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,23 @@ func (svc *Service) ModifyTeamAgentOptions(ctx context.Context, teamID uint, tea

if teamOptions != nil {
if err := fleet.ValidateJSONAgentOptions(ctx, svc.ds, teamOptions, true); err != nil {
if field := fleet.GetJSONUnknownField(err); field != nil {
correctKeyPath, keyErr := fleet.FindAgentOptionsKeyPath(*field)
if keyErr != nil {
level.Error(svc.logger).Log("err", err, "msg", "error parsing generated agent options structs")
}
var keyPathJoined string
switch pathLen := len(correctKeyPath); {
case pathLen > 1:
keyPathJoined = fmt.Sprintf("%q", strings.Join(correctKeyPath[:len(correctKeyPath)-1], "."))
case pathLen == 1:
keyPathJoined = "top level"
}
if keyPathJoined != "" {
err = fmt.Errorf("%q should be part of the %s object", *field, keyPathJoined)
}
}

err = fleet.NewUserMessageError(err, http.StatusBadRequest)
if applyOptions.Force && !applyOptions.DryRun {
level.Info(svc.logger).Log("err", err, "msg", "force-apply team agent options with validation errors")
Expand Down
23 changes: 1 addition & 22 deletions server/fleet/agent_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,6 @@ func (o *AgentOptions) ForPlatform(platform string) json.RawMessage {
// The validation always uses the most recent Osquery version that is available
// at the time of the Fleet release.
func ValidateJSONAgentOptions(ctx context.Context, ds Datastore, rawJSON json.RawMessage, isPremium bool) error {
err := validateJSONAgentOptionsInternal(ctx, ds, rawJSON, isPremium)
if field := GetJSONUnknownField(err); field != nil {
correctKeyPath, keyErr := findAgentOptionsKeyPath(*field)
if keyErr != nil {
return fmt.Errorf("agent options struct parsing: %w", err)
}
var keyPathJoined string
switch pathLen := len(correctKeyPath); {
case pathLen > 1:
keyPathJoined = fmt.Sprintf("%q", strings.Join(correctKeyPath[:len(correctKeyPath)-1], "."))
case pathLen == 1:
keyPathJoined = "top level"
}
if keyPathJoined != "" {
err = fmt.Errorf("%q should be part of the %s object", *field, keyPathJoined)
}
}
return err
}

func validateJSONAgentOptionsInternal(ctx context.Context, ds Datastore, rawJSON json.RawMessage, isPremium bool) error {
var opts AgentOptions
if err := JSONStrictDecode(bytes.NewReader(rawJSON), &opts); err != nil {
return err
Expand Down Expand Up @@ -347,7 +326,7 @@ func validateJSONAgentOptionsSet(rawJSON json.RawMessage) error {
return nil
}

func findAgentOptionsKeyPath(key string) ([]string, error) {
func FindAgentOptionsKeyPath(key string) ([]string, error) {
if key == "script_execution_timeout" {
return []string{"script_execution_timeout"}, nil
}
Expand Down

0 comments on commit 52b66ab

Please sign in to comment.