@@ -34,6 +34,8 @@ const (
34
34
35
35
autoRotateCheckInterval = 5 * time .Minute
36
36
legacyRotateReason = "legacy rotation"
37
+ // The keyring is persisted before the root key.
38
+ keyringTimeout = 1 * time .Second
37
39
)
38
40
39
41
// Versions of the AESGCM storage methodology
@@ -208,11 +210,18 @@ func (b *AESGCMBarrier) Initialize(ctx context.Context, key, sealKey []byte, rea
208
210
// persistKeyring is used to write out the keyring using the
209
211
// root key to encrypt it.
210
212
func (b * AESGCMBarrier ) persistKeyring (ctx context.Context , keyring * Keyring ) error {
211
- const (
212
- // The keyring is persisted before the root key.
213
- keyringTimeout = 1 * time .Second
214
- )
213
+ return b .persistKeyringInternal (ctx , keyring , false )
214
+ }
215
+
216
+ // persistKeyringBestEffort is like persistKeyring but 'best effort', ie times out early
217
+ // for non critical keyring writes (encryption/rotation tracking)
218
+ func (b * AESGCMBarrier ) persistKeyringBestEffort (ctx context.Context , keyring * Keyring ) error {
219
+ return b .persistKeyringInternal (ctx , keyring , true )
220
+ }
215
221
222
+ // persistKeyring is used to write out the keyring using the
223
+ // root key to encrypt it.
224
+ func (b * AESGCMBarrier ) persistKeyringInternal (ctx context.Context , keyring * Keyring , bestEffort bool ) error {
216
225
// Create the keyring entry
217
226
keyringBuf , err := keyring .Serialize ()
218
227
defer memzero (keyringBuf )
@@ -238,10 +247,16 @@ func (b *AESGCMBarrier) persistKeyring(ctx context.Context, keyring *Keyring) er
238
247
Value : value ,
239
248
}
240
249
241
- // We reduce the timeout on the initial 'put' but if this succeeds we will
242
- // allow longer later on when we try to persist the root key .
243
- ctxKeyring , cancelKeyring := context .WithTimeout (ctx , keyringTimeout )
244
- defer cancelKeyring ()
250
+ ctxKeyring := ctx
251
+
252
+ if bestEffort {
253
+ // We reduce the timeout on the initial 'put' but if this succeeds we will
254
+ // allow longer later on when we try to persist the root key .
255
+ var cancelKeyring func ()
256
+ ctxKeyring , cancelKeyring = context .WithTimeout (ctx , keyringTimeout )
257
+ defer cancelKeyring ()
258
+ }
259
+
245
260
if err := b .backend .Put (ctxKeyring , pe ); err != nil {
246
261
return fmt .Errorf ("failed to persist keyring: %w" , err )
247
262
}
@@ -1228,7 +1243,7 @@ func (b *AESGCMBarrier) persistEncryptions(ctx context.Context) error {
1228
1243
newEncs := upe + 1
1229
1244
activeKey .Encryptions += uint64 (newEncs )
1230
1245
newKeyring := b .keyring .Clone ()
1231
- err := b .persistKeyring (ctx , newKeyring )
1246
+ err := b .persistKeyringBestEffort (ctx , newKeyring )
1232
1247
if err != nil {
1233
1248
return err
1234
1249
}
0 commit comments