-
Notifications
You must be signed in to change notification settings - Fork 999
Fix SCEP autorenew failing for offline hosts #44250
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
435e4da
Fix SCEP autorenew failing for offline hosts
dantecatalfamo 9fac641
Use constant time value for SCEP test instead of hardcoded int
dantecatalfamo ad034f3
Add changes/
dantecatalfamo 33f124b
Add changes/
dantecatalfamo 8d2db50
Only retry certificates that are failed/succeeded
dantecatalfamo 97043a6
Fixed typo
dantecatalfamo d1e616c
Update release notes
dantecatalfamo fee9b0b
Add new challenges tests, end-to-end apple certificate renewal test
dantecatalfamo a428a76
Linting in test
dantecatalfamo 5964ee6
Remove unused import
dantecatalfamo 93baf61
Park iOS managed-cert profiles at verifying until cert metadata sync
dantecatalfamo f596537
Add backoff to failed certificate enrollments causing loops
dantecatalfamo 396f27c
Merge branch 'main' into 44111-scep-autorenew-fail
dantecatalfamo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| - Fixed SCEP renewals not retrying after initial failure | ||
| - Extend SCEP enrollment/renewal challenge credential TTL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package mysql | ||
|
|
||
| import ( | ||
| "database/sql" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/fleetdm/fleet/v4/server/fleet" | ||
| "github.com/jmoiron/sqlx" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestChallenges(t *testing.T) { | ||
| ds := CreateMySQLDS(t) | ||
|
|
||
| cases := []struct { | ||
| name string | ||
| fn func(t *testing.T, ds *Datastore) | ||
| }{ | ||
| {"NewAndConsume", testChallengeNewAndConsume}, | ||
| {"ConsumeMissing", testChallengeConsumeMissing}, | ||
| {"ConsumeWithinTTL", testChallengeConsumeWithinTTL}, | ||
| {"ConsumeExpired", testChallengeConsumeExpired}, | ||
| {"CleanupRespectsTTL", testChallengeCleanupRespectsTTL}, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| t.Run(c.name, func(t *testing.T) { | ||
| defer TruncateTables(t, ds) | ||
| c.fn(t, ds) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func testChallengeNewAndConsume(t *testing.T, ds *Datastore) { | ||
| ctx := t.Context() | ||
|
|
||
| challenge, err := ds.NewChallenge(ctx) | ||
| require.NoError(t, err) | ||
| require.NotEmpty(t, challenge) | ||
|
|
||
| err = ds.ConsumeChallenge(ctx, challenge) | ||
| require.NoError(t, err) | ||
|
|
||
| // Second consume must fail — challenge is one-time. | ||
| err = ds.ConsumeChallenge(ctx, challenge) | ||
| require.Error(t, err) | ||
| require.ErrorIs(t, err, sql.ErrNoRows) | ||
| } | ||
|
|
||
| func testChallengeConsumeMissing(t *testing.T, ds *Datastore) { | ||
| ctx := t.Context() | ||
|
|
||
| err := ds.ConsumeChallenge(ctx, "") | ||
| require.Error(t, err) | ||
| require.ErrorIs(t, err, sql.ErrNoRows) | ||
|
|
||
| err = ds.ConsumeChallenge(ctx, "never-issued") | ||
| require.Error(t, err) | ||
| require.ErrorIs(t, err, sql.ErrNoRows) | ||
| } | ||
|
|
||
| // testChallengeConsumeWithinTTL backdates a challenge to just within OneTimeChallengeTTL and | ||
| // confirms it's still consumable. Regression coverage for issue #44111: devices may take | ||
| // hours/days to process the InstallProfile push before sending the SCEP request. | ||
| func testChallengeConsumeWithinTTL(t *testing.T, ds *Datastore) { | ||
| ctx := t.Context() | ||
|
|
||
| challenge, err := ds.NewChallenge(ctx) | ||
| require.NoError(t, err) | ||
|
|
||
| // Backdate to 1 minute inside the TTL. | ||
| backdated := time.Now().Add(-fleet.OneTimeChallengeTTL).Add(1 * time.Minute) | ||
| ExecAdhocSQL(t, ds, func(q sqlx.ExtContext) error { | ||
| _, err := q.ExecContext(ctx, `UPDATE challenges SET created_at = ? WHERE challenge = ?`, backdated, challenge) | ||
| return err | ||
| }) | ||
|
|
||
| err = ds.ConsumeChallenge(ctx, challenge) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| // testChallengeConsumeExpired backdates a challenge past OneTimeChallengeTTL and confirms it's | ||
| // rejected as expired (returns sql.ErrNoRows wrapped with "challenge expired"). | ||
| func testChallengeConsumeExpired(t *testing.T, ds *Datastore) { | ||
| ctx := t.Context() | ||
|
|
||
| challenge, err := ds.NewChallenge(ctx) | ||
| require.NoError(t, err) | ||
|
|
||
| // Backdate past the TTL. | ||
| expired := time.Now().Add(-fleet.OneTimeChallengeTTL).Add(-1 * time.Minute) | ||
| ExecAdhocSQL(t, ds, func(q sqlx.ExtContext) error { | ||
| _, err := q.ExecContext(ctx, `UPDATE challenges SET created_at = ? WHERE challenge = ?`, expired, challenge) | ||
| return err | ||
| }) | ||
|
|
||
| err = ds.ConsumeChallenge(ctx, challenge) | ||
| require.Error(t, err) | ||
| require.ErrorIs(t, err, sql.ErrNoRows) | ||
| } | ||
|
|
||
| // testChallengeCleanupRespectsTTL verifies CleanupExpiredChallenges deletes only challenges | ||
| // older than OneTimeChallengeTTL and leaves still-valid challenges in place. | ||
| func testChallengeCleanupRespectsTTL(t *testing.T, ds *Datastore) { | ||
| ctx := t.Context() | ||
|
|
||
| freshChallenge, err := ds.NewChallenge(ctx) | ||
| require.NoError(t, err) | ||
| expiredChallenge, err := ds.NewChallenge(ctx) | ||
| require.NoError(t, err) | ||
|
|
||
| expired := time.Now().Add(-fleet.OneTimeChallengeTTL).Add(-1 * time.Minute) | ||
| ExecAdhocSQL(t, ds, func(q sqlx.ExtContext) error { | ||
| _, err := q.ExecContext(ctx, `UPDATE challenges SET created_at = ? WHERE challenge = ?`, expired, expiredChallenge) | ||
| return err | ||
| }) | ||
|
|
||
| deleted, err := ds.CleanupExpiredChallenges(ctx) | ||
| require.NoError(t, err) | ||
| require.EqualValues(t, 1, deleted) | ||
|
|
||
| // Fresh challenge survives and is still consumable. | ||
| err = ds.ConsumeChallenge(ctx, freshChallenge) | ||
| require.NoError(t, err) | ||
|
|
||
| // Expired challenge is gone. | ||
| err = ds.ConsumeChallenge(ctx, expiredChallenge) | ||
| require.Error(t, err) | ||
| require.ErrorIs(t, err, sql.ErrNoRows) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this cause an infinite loop where the renewal cron keeps renewing forever?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If they keep failing infinitely on renewal, possibly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current behaviour is that if there's a failure it silently forgets to renew forever
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the query is:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point, I'll see if I can narrow the renew loop to not try on intermediate statuses
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the renewal check to look for verified/failed instead of NOT NULL in the status field. It shouldn't re-trigger for in-flight SCEP profiles anymore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there might still be a bit of a race here on iOS/iPadOS where, since we don't have osquery the profiles go straight to verified on the ack but it could be up to an hour before they check in again and the renewal cron might run in that interval and re-renew the cert?
There is some code that at least for NDES and maybe others triggers a repush of the cert profile when the challenge is expired. Do we know if that's triggering here?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have two possible solutions for that problem. I'm not super familiar with the iOS MDM cycle, so take these suggestions with a grain of salt. I could either add a column to keep track of when the last renewal was on the instantly 'verified' profile, or I could detect if the profile contains a SCEP profile and only set it it to 'verifying' until the certificate list is renewed. Do either of those make more sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got claude to implement both solutions and the second seemed far less invasive. Let me know what you think