Skip to content

Commit

Permalink
Add ability to cancel PKI tidy operations, pause between tidying certs (
Browse files Browse the repository at this point in the history
#16958)

* Allow tidy operations to be cancelled

When tidy operations take a long time to execute (and especially when
executing them automatically), having the ability to cancel them becomes
useful to reduce strain on Vault clusters (and let them be rescheduled
at a later time).

To this end, we add the /tidy-cancel write endpoint.

Signed-off-by: Alexander Scheel <[email protected]>

* Add missing auto-tidy synopsis / description

Signed-off-by: Alexander Scheel <[email protected]>

* Add a pause duration between tidying certificates

By setting pause_duration, operators can have a little control over the
resource utilization of a tidy operation. While the list of certificates
remain in memory throughout the entire operation, a pause is added
between processing certificates and the revocation lock is released.
This allows other operations to occur during this gap and potentially
allows the tidy operation to consume less resources per unit of time
(due to the sleep -- though obviously consumes the same resources over
the time of the operation).

Signed-off-by: Alexander Scheel <[email protected]>

* Add tests for cancellation, pause

Signed-off-by: Alexander Scheel <[email protected]>

* Add API docs on pause_duration, /tidy-cancel

Signed-off-by: Alexander Scheel <[email protected]>

* Add changelog entry

Signed-off-by: Alexander Scheel <[email protected]>

* Add lock releasing around tidy pause

Signed-off-by: Alexander Scheel <[email protected]>

* Reset cancel guard, return errors

Signed-off-by: Alexander Scheel <[email protected]>

Signed-off-by: Alexander Scheel <[email protected]>
  • Loading branch information
cipherboy authored Aug 31, 2022
1 parent 9d97dec commit 76d89fd
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 38 deletions.
14 changes: 10 additions & 4 deletions builtin/logical/pki/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func Backend(conf *logical.BackendConfig) *backend {
pathRevoke(&b),
pathRevokeWithKey(&b),
pathTidy(&b),
pathTidyCancel(&b),
pathTidyStatus(&b),
pathConfigAutoTidy(&b),

Expand Down Expand Up @@ -184,6 +185,7 @@ func Backend(conf *logical.BackendConfig) *backend {
}

b.tidyCASGuard = new(uint32)
b.tidyCancelCAS = new(uint32)
b.tidyStatus = &tidyStatus{state: tidyStatusInactive}
b.storage = conf.StorageView
b.backendUUID = conf.BackendUUID
Expand All @@ -205,6 +207,7 @@ type backend struct {
storage logical.Storage
revokeStorageLock sync.RWMutex
tidyCASGuard *uint32
tidyCancelCAS *uint32

tidyStatusLock sync.RWMutex
tidyStatus *tidyStatus
Expand All @@ -223,10 +226,12 @@ type (
)

const (
tidyStatusInactive tidyStatusState = iota
tidyStatusStarted
tidyStatusFinished
tidyStatusError
tidyStatusInactive tidyStatusState = iota
tidyStatusStarted = iota
tidyStatusFinished = iota
tidyStatusError = iota
tidyStatusCancelling = iota
tidyStatusCancelled = iota
)

type tidyStatus struct {
Expand All @@ -235,6 +240,7 @@ type tidyStatus struct {
tidyCertStore bool
tidyRevokedCerts bool
tidyRevokedAssocs bool
pauseDuration string

// Status
state tidyStatusState
Expand Down
1 change: 1 addition & 0 deletions builtin/logical/pki/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3877,6 +3877,7 @@ func TestBackend_RevokePlusTidy_Intermediate(t *testing.T) {
"tidy_cert_store": true,
"tidy_revoked_certs": true,
"tidy_revoked_cert_issuer_associations": false,
"pause_duration": "0s",
"state": "Finished",
"error": nil,
"time_started": nil,
Expand Down
12 changes: 12 additions & 0 deletions builtin/logical/pki/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,5 +463,17 @@ Defaults to 72 hours.`,
Default: int(defaultTidyConfig.SafetyBuffer / time.Second), // TypeDurationSecond currently requires defaults to be int
}

fields["pause_duration"] = &framework.FieldSchema{
Type: framework.TypeString,
Description: `The amount of time to wait between processing
certificates. This allows operators to change the execution profile
of tidy to take consume less resources by slowing down how long it
takes to run. Note that the entire list of certificates will be
stored in memory during the entire tidy operation, but resources to
read/process/update existing entries will be spread out over a
greater period of time. By default this is zero seconds.`,
Default: "0s",
}

return fields
}
Loading

0 comments on commit 76d89fd

Please sign in to comment.