StartRotator starts a background process that rotates keys in the database. It ensures there's at least one valid key per feature prior to returning. Canceling the provided context will stop the background process.
(ctx context.Context, logger slog.Logger, db database.Store, opts ...RotatorOption)
| 55 | // It ensures there's at least one valid key per feature prior to returning. |
| 56 | // Canceling the provided context will stop the background process. |
| 57 | func StartRotator(ctx context.Context, logger slog.Logger, db database.Store, opts ...RotatorOption) { |
| 58 | //nolint:gocritic // KeyRotator can only rotate crypto keys. |
| 59 | ctx = dbauthz.AsKeyRotator(ctx) |
| 60 | kr := &rotator{ |
| 61 | db: db, |
| 62 | logger: logger.Named("keyrotator"), |
| 63 | clock: quartz.NewReal(), |
| 64 | keyDuration: DefaultKeyDuration, |
| 65 | features: database.AllCryptoKeyFeatureValues(), |
| 66 | } |
| 67 | |
| 68 | for _, opt := range opts { |
| 69 | opt(kr) |
| 70 | } |
| 71 | |
| 72 | err := kr.rotateKeys(ctx) |
| 73 | if err != nil { |
| 74 | kr.logger.Critical(ctx, "failed to rotate keys", slog.Error(err)) |
| 75 | } |
| 76 | |
| 77 | go kr.start(ctx) |
| 78 | } |
| 79 | |
| 80 | // start begins the process of rotating keys. |
| 81 | // Canceling the context will stop the rotation process. |