Skip to content

Add macos custom profiles support via fleetctl apply - #9824

Merged
mna merged 24 commits into
mainfrom
mna-9587-fleetctl-apply-profiles
Feb 15, 2023
Merged

mna merged 24 commits into
mainfrom
mna-9587-fleetctl-apply-profiles

Conversation

@mna

@mna mna commented Feb 14, 2023

Copy link
Copy Markdown
Contributor

#9587

Checklist for submitter

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

  • Changes file added for user-visible changes in changes/ or orbit/changes/.
    See Changes files for more information.
  • Documented any API changes (docs/Using-Fleet/REST-API.md or docs/Contributing/API-for-contributors.md)
  • Input data is properly validated, SELECT * is avoided, SQL injection is prevented (using placeholders for values in statements)
  • Added/updated tests
  • Manual QA for all new/changed functionality (fleetctl apply tested manually)

@mna
mna temporarily deployed to Docker Hub February 14, 2023 21:47 — with GitHub Actions Inactive
@mna
mna temporarily deployed to Docker Hub February 14, 2023 21:49 — with GitHub Actions Inactive
@mna
mna temporarily deployed to Docker Hub February 14, 2023 21:58 — with GitHub Actions Inactive
@mna
mna temporarily deployed to Docker Hub February 14, 2023 22:00 — with GitHub Actions Inactive

##### Default response

`200` // TODO: should that be 204 no content?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

^ Any thoughts here? Is there something useful that this could return, or should it return 204? Haven't looked much into it, I'm not sure how easy it is to change the status code of a success response.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

besides the IDs of the profiles (which doesn't seem useful) I can't think of anything else to return 🤔

as for setting the status I have seen (but never used) this before:

if e, ok := response.(statuser); ok {
w.WriteHeader(e.Status())
if e.Status() == http.StatusNoContent {
return nil
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I'll use that, I think StatusNoContent would be a bit nicer.

}

func (svc *Service) BatchSetMDMAppleProfiles(ctx context.Context, tmID *uint, tmName *string, profiles [][]byte, dryRun bool) error {
if !svc.config.MDMApple.Enable {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For now I required that this was set. As commented below, I suspect this will become just a check that APNs is configured when we remove the feature flag.

@mna
mna marked this pull request as ready for review February 14, 2023 22:07
@mna
mna requested review from a team, lukeheath and rynsatterlee as code owners February 14, 2023 22:07
@codecov-commenter

codecov-commenter commented Feb 14, 2023 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.19718% with 120 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.66%. Comparing base (8b99dc9) to head (0356870).
⚠️ Report is 16414 commits behind head on main.

Files with missing lines Patch % Lines
server/service/client.go 44.44% 52 Missing and 3 partials ⚠️
server/fleet/app.go 0.00% 21 Missing ⚠️
server/datastore/mysql/apple_mdm.go 87.39% 10 Missing and 5 partials ⚠️
server/service/apple_mdm.go 82.85% 9 Missing and 3 partials ⚠️
server/service/client_teams.go 0.00% 8 Missing ⚠️
server/fleet/teams.go 0.00% 6 Missing ⚠️
server/service/client_appconfig.go 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9824      +/-   ##
==========================================
+ Coverage   60.58%   60.66%   +0.07%     
==========================================
  Files         492      492              
  Lines       49293    49732     +439     
==========================================
+ Hits        29865    30169     +304     
- Misses      16641    16763     +122     
- Partials     2787     2800      +13     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

roperzh
roperzh previously approved these changes Feb 15, 2023

@roperzh roperzh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, I like how easy to follow BatchSetMDMAppleProfiles despite its inherent complexity.

Comment thread cmd/fleetctl/get_test.go
`
expectedJson := `{"kind":"team","apiVersion":"v1","spec":{"team":{"id":42,"created_at":"1999-03-10T02:45:06.371Z","name":"team1","description":"team1 description","webhook_settings":{"failing_policies_webhook":{"enable_failing_policies_webhook":false,"destination_url":"","policy_ids":null,"host_batch_size":0}},"integrations":{"jira":null,"zendesk":null},"features":{"enable_host_users":true,"enable_software_inventory":true},"mdm":{"macos_updates":{"minimum_version":"","deadline":""}},"user_count":99,"host_count":42}}}
{"kind":"team","apiVersion":"v1","spec":{"team":{"id":43,"created_at":"1999-03-10T02:45:06.371Z","name":"team2","description":"team2 description","agent_options":{"config":{"foo":"bar"},"overrides":{"platforms":{"darwin":{"foo":"override"}}}},"webhook_settings":{"failing_policies_webhook":{"enable_failing_policies_webhook":false,"destination_url":"","policy_ids":null,"host_batch_size":0}},"integrations":{"jira":null,"zendesk":null},"features":{"enable_host_users":false,"enable_software_inventory":false,"additional_queries":{"foo":"bar"}},"mdm":{"macos_updates":{"minimum_version":"12.3.1","deadline":"2021-12-14"}},"user_count":87,"host_count":43}}}
expectedJson := `{"kind":"team","apiVersion":"v1","spec":{"team":{"id":42,"created_at":"1999-03-10T02:45:06.371Z","name":"team1","description":"team1 description","webhook_settings":{"failing_policies_webhook":{"enable_failing_policies_webhook":false,"destination_url":"","policy_ids":null,"host_batch_size":0}},"integrations":{"jira":null,"zendesk":null},"features":{"enable_host_users":true,"enable_software_inventory":true},"mdm":{"macos_updates":{"minimum_version":"","deadline":""}},"macos_settings":{"custom_settings":null},"user_count":99,"host_count":42}}}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

rant: curious if this test (and others in this file) is as annoying to you as it is to me to modify. Obviously not for now! but I have seen we use golden files for other tests, I wonder if we're at the same point here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah eventually we should make them all golden files, nicer to maintain!


##### Default response

`200` // TODO: should that be 204 no content?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

besides the IDs of the profiles (which doesn't seem useful) I can't think of anything else to return 🤔

as for setting the status I have seen (but never used) this before:

if e, ok := response.(statuser); ok {
w.WriteHeader(e.Status())
if e.Status() == http.StatusNoContent {
return nil
}
}

svc.authz.SkipAuthorization(ctx) // so that the error message is not replaced by "forbidden"
return ctxerr.Wrap(ctx, fleet.NewInvalidArgumentError("team_name", "cannot specify both team_id and team_name"))
}
if tmID != nil || tmName != nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

my understanding is that technically we would do something on the lines of if tmID != nil || tmName != nil { return missingLicenseError } and handle all the stuff to load the team in /ee.

the underlying reason for the premium code to be isolated in /ee is just so the license can be enforced from a legal standpoint.

given the time constraint and how difficult it is to deal with separating stuff that way I'd vote to keep this as-is and revisit later. (we're doing this check in some other places either way)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

the underlying reason for the premium code to be isolated in /ee is just so the license can be enforced from a legal standpoint.

Oh I didn't know that! I did something similar to the validations of premium stuff in the modify appconfig endpoint.

I'd vote to keep this as-is and revisit later.

Yeah if that's acceptable, I'd do that too. Not quite sure how it'd go to separate just the team-loading part in ee/, it'd probably not feel super natural.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thinking out loud: one possibility is to split this method into two service methods, one that handles loading the team and another for the proper profile stuff that receives a team as a paramete

addressing this post-mvp sounds good to me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's a good idea, I'll give it a shot see if it's not too big a change, otherwise I'll leave it like that for now.

Comment thread server/service/client.go
Comment on lines +339 to +341
if err := c.ApplyTeamProfiles(tmName, profs, opts); err != nil {
return fmt.Errorf("applying custom settings for team %q: %w", tmName, err)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder: if this fails, should we clean the list of profiles from the team config JSON? (same for appConfig for the no-team case)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(in the database I mean: since we have already applied the team specs)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's a possibility, those are two distinct, non-atomic operations so they may fail separately, but I wouldn't go out of my way to try and handle this edge case as there's no way to "win", i.e. the next call to clean the list of profiles may fail, and then what? There are many such non-atomic operations throughout the codebase - activities are created outside of the associated action and may fail separately, applying team specs apply each team separately, it may apply one and fail on the second, we don't "undo" the first applied team even though the fleetctl apply operation did fail, etc.

I'd suggest that we leave it like that, maybe to revisit later after the MDM initial release but I'd say this is a larger discussion than this specific case.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thanks! that makes sense to me.

…more validations checked on apply teams dry run
@mna
mna temporarily deployed to Docker Hub February 15, 2023 14:04 — with GitHub Actions Inactive
@mna
mna temporarily deployed to Docker Hub February 15, 2023 14:19 — with GitHub Actions Inactive
@mna
mna temporarily deployed to Docker Hub February 15, 2023 14:46 — with GitHub Actions Inactive
roperzh
roperzh previously approved these changes Feb 15, 2023
@mna
mna temporarily deployed to Docker Hub February 15, 2023 15:21 — with GitHub Actions Inactive
@mna
mna merged commit 33f3316 into main Feb 15, 2023
@mna
mna deleted the mna-9587-fleetctl-apply-profiles branch February 15, 2023 18:01

This branch was previously deployed

1 inactive deployment
Docker Hub — 0356870f Deployed Feb 15, 2023 by mna
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants