Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to /fleet/queries/run endpoint #14909

Merged
merged 4 commits into from
Nov 6, 2023
Merged

Conversation

getvictor
Copy link
Member

@getvictor getvictor commented Nov 2, 2023

Fixes to /fleet/queries/run endpoint:

  • now returns 403 for an unauthorized user
  • now returns 400 when query_ids or host_ids are not specified

#11446 and #11901

Checklist for submitter

If some of the following don't apply, delete the relevant line.

API clarifications are in a separate PR #14956

  • Changes file added for user-visible changes in changes/ or orbit/changes/.
    See Changes files for more information.
  • Added/updated tests
  • Manual QA for all new/changed functionality

@getvictor getvictor temporarily deployed to Docker Hub November 2, 2023 21:44 — with GitHub Actions Inactive
@getvictor getvictor temporarily deployed to Docker Hub November 2, 2023 22:30 — with GitHub Actions Inactive
@getvictor getvictor marked this pull request as ready for review November 2, 2023 22:30
@getvictor getvictor requested a review from a team as a code owner November 2, 2023 22:30
}
if allResultsForbidden {
return nil, authz.ForbiddenWithInternal("All Live Query results were forbidden.", authz.UserFromContext(ctx), nil, nil)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's check with the product team what the expected behavior is when sending multiple query_ids and the user is not authorized to run some of them:

  • Should the request fail and no queries be executed?
  • Should the request not fail and only run the queries that the user is authorized to run?

API: https://fleetdm.com/docs/rest-api/rest-api#parameters97

@noahtalerman @rachaelshaw

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marko-lisica did we define a similar behavior for running MDM commands that we can borrow here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current behavior for mix of authorized/unauthorized live queries is that user will get back an array of results. Good results will be valid, and unauthorized results will have "error":"forbidden"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Could we take the chance to document this behavior in the rest-api.md?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Can be done later on another PR.)

Copy link
Member

@rachaelshaw rachaelshaw Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After chatting with Victor about it earlier, this behavior of mixed results makes sense to me, but I definitely agree we should document the behavior. @getvictor if you don't mind adding that to this PR, that'd be awesome. Or I'd be happy to take a stab at it after this is merged, just let me know

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noahtalerman In the CLI we have error message for this use case - figma link. Regarding API, seems there's 403: forbidden error, but not sure when do we return this one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rachaelshaw I added PR #14956 for rest-api.md updates.

Copy link
Member

@lucasmrod lucasmrod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but left a question regarding expected behavior. (Please let me know if this was defined somewhere and I missed it.)

getvictor and others added 3 commits November 6, 2023 08:08
- now returns 403 for an unauthorized user
- now returns 400 when query_ids or host_ids are not specified
@getvictor getvictor force-pushed the 11446-queries-run-when-forbidden branch from 28e69d0 to cca3505 Compare November 6, 2023 14:08
@getvictor getvictor marked this pull request as draft November 6, 2023 14:08
Copy link

codecov bot commented Nov 6, 2023

Codecov Report

Attention: 6 lines in your changes are missing coverage. Please review.

Comparison is base (5391b68) 58.85% compared to head (f4c7df1) 58.87%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #14909      +/-   ##
==========================================
+ Coverage   58.85%   58.87%   +0.02%     
==========================================
  Files         953      953              
  Lines       80241    80274      +33     
  Branches     2222     2222              
==========================================
+ Hits        47223    47265      +42     
+ Misses      29341    29336       -5     
+ Partials     3677     3673       -4     
Flag Coverage Δ
backend 59.52% <83.78%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
server/service/live_queries.go 76.92% <100.00%> (+3.47%) ⬆️
server/utils.go 41.93% <100.00%> (+7.78%) ⬆️
server/authz/errors.go 21.87% <0.00%> (-4.06%) ⬇️

... and 3 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@getvictor getvictor marked this pull request as ready for review November 6, 2023 16:28
@@ -48,21 +50,45 @@ func runLiveQueryEndpoint(ctx context.Context, request interface{}, svc fleet.Se
logging.WithExtras(ctx, "live_query_rest_period_err", err)
}

// Only allow a host to be specified once in HostIDs
req.HostIDs = server.RemoveDuplicatesFromSlice(req.HostIDs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity: Is there a bug or just a sanity check to not cause unnecessary load?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small bug. User can specify the same host twice, but service will only return 1 result. So, TargetedHostCount(2) will never match RespondedHostCount(1).

@getvictor getvictor merged commit f38524a into main Nov 6, 2023
17 of 18 checks passed
@getvictor getvictor deleted the 11446-queries-run-when-forbidden branch November 6, 2023 17:03
getvictor added a commit that referenced this pull request Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants