fix: bound live credential recovery to the pending rotation window
4 files changed,
+37
−5
internal/equipo/runner_mutations.go+1 −1internal/equipo/runner_rotation.go+3 −3internal/equipotest/final_rotation_test.go+32 −0internal/equipotest/runner_api_test.go+1 −1
diff --git a/internal/equipo/runner_mutations.go b/internal/equipo/runner_mutations.go| index ce10e74a..e46944df 100644 |
| --- a/internal/equipo/runner_mutations.go |
| +++ b/internal/equipo/runner_mutations.go |
| @@ -282,7 +282,7 @@ func (e *Execution) mutateAt(ctx context.Context, tx *gorm.DB, a runnerAssignmen |
| return nil, err |
| } |
| if value.Selection.Isolation.RunnerTainted || value.Selection.Isolation.Status == "unconfined" { |
| - return e.rotateContained(tx, a, q, now) |
| + return e.rotateContained(tx, a, q, now, true) |
| } |
| return CredentialRotation{}, nil |
| case "stop": |
diff --git a/internal/equipo/runner_rotation.go b/internal/equipo/runner_rotation.go| index 2d505689..942e1ed8 100644 |
| --- a/internal/equipo/runner_rotation.go |
| +++ b/internal/equipo/runner_rotation.go |
| @@ -13,7 +13,7 @@ import ( |
| "gorm.io/gorm/clause" |
| ) |
| -func (e *Execution) rotateContained(tx *gorm.DB, a runnerAssignment, q RunnerRequest, now time.Time) (CredentialRotation, error) { |
| +func (e *Execution) rotateContained(tx *gorm.DB, a runnerAssignment, q RunnerRequest, now time.Time, allowLive bool) (CredentialRotation, error) { |
| result := CredentialRotation{CleanupTokens: map[string]string{}} |
| var runner runnerRecord |
| if err := tx.First(&runner, "id=?", a.RunnerID).Error; err != nil { |
| @@ -23,7 +23,7 @@ func (e *Execution) rotateContained(tx *gorm.DB, a runnerAssignment, q RunnerReq |
| if err := tx.Where("runner_id=? AND revoked_at IS NULL AND expires_at>?", a.RunnerID, now).Find(&credentials).Error; err != nil { |
| return result, storeError(err) |
| } |
| - live := runner.RevokedAt == nil && len(credentials) > 0 |
| + live := allowLive && runner.RevokedAt == nil && len(credentials) > 0 |
| if _, err := currentRunnerMember(tx, runner.Person); err != nil { |
| if !errors.Is(err, ErrForbidden) && !errors.Is(err, ErrNotFound) { |
| return result, err |
| @@ -216,7 +216,7 @@ func (e *Execution) recoverRotation(ctx context.Context, token string, q RunnerR |
| if hex.EncodeToString(sum[:]) != original.PayloadHash { |
| return ErrConflict |
| } |
| - result, err = e.rotateContained(tx, a, q, now) |
| + result, err = e.rotateContained(tx, a, q, now, rotation.State == "pending" && !now.Before(recovery.At) && now.Before(recovery.At.Add(5*time.Minute))) |
| return err |
| }) |
| return result, found, err |
diff --git a/internal/equipotest/final_rotation_test.go b/internal/equipotest/final_rotation_test.go| new file mode 100644 |
| index 00000000..a3cbb88a |
| --- /dev/null |
| +++ b/internal/equipotest/final_rotation_test.go |
| @@ -0,0 +1,32 @@ |
| +package equipotest |
| + |
| +import ( |
| + "amadan.net/oficina/equipo/internal/equipo" |
| + "context" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestRotationRecoveryWindow(t *testing.T) { |
| + for _, age := range []time.Duration{4 * time.Minute, 5 * time.Minute} { |
| + t.Run(age.String(), func(t *testing.T) { |
| + f := runnerFixtureNew(t, true) |
| + f.x.SetRunnerValidators(equipo.RunnerValidators{Containment: func(context.Context, equipo.Assignment, equipo.RunnerRequest) error { return nil }}) |
| + q := f.request() |
| + q.RequestID = "rotation" |
| + q.Proof = []byte(`{}`) |
| + _, err := f.x.Mutate(connectionContext, f.principal, "contained", q) |
| + executionMust(t, err) |
| + f.clock.Advance(age) |
| + value, err := f.x.Cleanup(connectionContext, f.claim.CleanupToken, "contained", q) |
| + executionMust(t, err) |
| + rotation := value.(equipo.CredentialRotation) |
| + if (rotation.Bearer.Token != "") != (age < 5*time.Minute) { |
| + t.Fatal("rotation recovered live authority outside window", age) |
| + } |
| + if len(rotation.CleanupTokens) == 0 { |
| + t.Fatal("expired rotation lost cleanup") |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/equipotest/runner_api_test.go b/internal/equipotest/runner_api_test.go| index dfe988f9..a6599962 100644 |
| --- a/internal/equipotest/runner_api_test.go |
| +++ b/internal/equipotest/runner_api_test.go |
| @@ -572,7 +572,7 @@ func TestUnconfinedContainmentRotatesTokensConcurrent(t *testing.T) { |
| if scalar(t, f.d, "SELECT count(*) FROM runner_credentials") != before { |
| t.Fatal("same attempt rotated twice") |
| } |
| - if scalar(t, f.d, "SELECT count(*) FROM time_reservations WHERE state='open'") != 2 { |
| + if scalar(t, f.d, "SELECT count(*) FROM time_reservations WHERE state='settled' AND elapsed_unknown=1 AND settled_seconds=reserved_seconds") != 2 { |
| t.Fatal("containment refunded time") |
| } |
| }) |