|
| 1 | +/* |
| 2 | + Copyright NetFoundry Inc. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package policy |
| 18 | + |
| 19 | +import ( |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/michaelquigley/pfxlog" |
| 23 | + "github.com/openziti/ziti/v2/common/runner" |
| 24 | + "github.com/openziti/ziti/v2/controller/change" |
| 25 | + "github.com/openziti/ziti/v2/controller/env" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + RevocationEnforcerRun = "revocation.enforcer.run" |
| 30 | + RevocationEnforcerDelete = "revocation.enforcer.delete" |
| 31 | + RevocationEnforcerSource = "revocation.enforcer" |
| 32 | +) |
| 33 | + |
| 34 | +// RevocationEnforcer periodically purges expired revocation records from the |
| 35 | +// controller database and router data models. |
| 36 | +type RevocationEnforcer struct { |
| 37 | + appEnv *env.AppEnv |
| 38 | + *runner.BaseOperation |
| 39 | +} |
| 40 | + |
| 41 | +// NewRevocationEnforcer creates a RevocationEnforcer that runs at the given frequency. |
| 42 | +func NewRevocationEnforcer(appEnv *env.AppEnv, frequency time.Duration) *RevocationEnforcer { |
| 43 | + return &RevocationEnforcer{ |
| 44 | + appEnv: appEnv, |
| 45 | + BaseOperation: runner.NewBaseOperation("RevocationEnforcer", frequency), |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// Run deletes all expired revocation entries. |
| 50 | +func (e *RevocationEnforcer) Run() error { |
| 51 | + startTime := time.Now() |
| 52 | + |
| 53 | + defer func() { |
| 54 | + e.appEnv.GetMetricsRegistry().Timer(RevocationEnforcerRun).UpdateSince(startTime) |
| 55 | + }() |
| 56 | + |
| 57 | + ctx := change.New().SetSourceType(RevocationEnforcerSource).SetChangeAuthorType(change.AuthorTypeController) |
| 58 | + |
| 59 | + total, err := e.appEnv.GetManagers().Revocation.DeleteExpired(ctx) |
| 60 | + if err != nil { |
| 61 | + pfxlog.Logger().WithError(err).Error("failed to delete expired revocations") |
| 62 | + return nil |
| 63 | + } |
| 64 | + |
| 65 | + if total > 0 { |
| 66 | + pfxlog.Logger().Debugf("removed %d expired revocations", total) |
| 67 | + e.appEnv.GetMetricsRegistry().Meter(RevocationEnforcerDelete).Mark(int64(total)) |
| 68 | + } |
| 69 | + |
| 70 | + return nil |
| 71 | +} |
0 commit comments