checkStorage tests the storage by writing random bytes to a random key, and then loading those bytes and comparing the loaded value. If this fails, the provided cfg.Storage mechanism should not be used.
(ctx context.Context)
| 1234 | // comparing the loaded value. If this fails, the provided |
| 1235 | // cfg.Storage mechanism should not be used. |
| 1236 | func (cfg *Config) checkStorage(ctx context.Context) error { |
| 1237 | if cfg.DisableStorageCheck { |
| 1238 | return nil |
| 1239 | } |
| 1240 | key := fmt.Sprintf("rw_test_%d", weakrand.Int()) |
| 1241 | contents := make([]byte, 1024*10) // size sufficient for one or two ACME resources |
| 1242 | // This is how ChaCha8.Read works, without handling the case where the slice length is not a multiple of 8. |
| 1243 | // This also avoids the use of a mutex and an import. |
| 1244 | for i := 0; i < len(contents); i += 8 { |
| 1245 | v := weakrand.Uint64() |
| 1246 | contents[i] = byte(v) |
| 1247 | contents[i+1] = byte(v >> 8) |
| 1248 | contents[i+2] = byte(v >> 16) |
| 1249 | contents[i+3] = byte(v >> 24) |
| 1250 | contents[i+4] = byte(v >> 32) |
| 1251 | contents[i+5] = byte(v >> 40) |
| 1252 | contents[i+6] = byte(v >> 48) |
| 1253 | contents[i+7] = byte(v >> 56) |
| 1254 | } |
| 1255 | err := cfg.Storage.Store(ctx, key, contents) |
| 1256 | if err != nil { |
| 1257 | return err |
| 1258 | } |
| 1259 | defer func() { |
| 1260 | deleteErr := cfg.Storage.Delete(ctx, key) |
| 1261 | if deleteErr != nil { |
| 1262 | cfg.Logger.Error("deleting test key from storage", |
| 1263 | zap.String("key", key), zap.Error(err)) |
| 1264 | } |
| 1265 | // if there was no other error, make sure |
| 1266 | // to return any error returned from Delete |
| 1267 | if err == nil { |
| 1268 | err = deleteErr |
| 1269 | } |
| 1270 | }() |
| 1271 | loaded, err := cfg.Storage.Load(ctx, key) |
| 1272 | if err != nil { |
| 1273 | return err |
| 1274 | } |
| 1275 | if !bytes.Equal(contents, loaded) { |
| 1276 | return fmt.Errorf("load yielded different value than was stored; expected %d bytes, got %d bytes of differing elements", len(contents), len(loaded)) |
| 1277 | } |
| 1278 | return nil |
| 1279 | } |
| 1280 | |
| 1281 | // storageHasCertResources returns true if the storage |
| 1282 | // associated with cfg's certificate cache has all the |