Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fdbbackup/backup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,7 @@ AsyncResult<std::string> getLayerStatus(Reference<ReadYourWritesTransaction> tr,
// that's something that should never happen.
Reference<IBackupContainer> bc = TupleCodec<Reference<IBackupContainer>>::unpack(value);
if (encryptionKeyFileName.present()) {
encryptionSetupResults.push_back(bc->encryptionSetupComplete());
encryptionSetupResults.push_back(bc->encryptionSetupStatus());
encryptionKeyPaths.push_back(encryptionKeyFileName.get());
}
} catch (Error& e) {
Expand Down
1 change: 0 additions & 1 deletion fdbclient/BackupContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ Reference<IBackupContainer> IBackupContainer::openContainer(const std::string& u
throw backup_invalid_url();
}

r->encryptionKeyFileName = encryptionKeyFileName;
r->URL = url;
return r;
} catch (Error& e) {
Expand Down
62 changes: 58 additions & 4 deletions fdbclient/BackupContainerFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1920,11 +1920,12 @@ class BackupContainerFileSystemImpl {
throw;
}
}
// Containers retain this setup future, so recover disk errors before publishing a permanent failure.
co_await delay(1.0);
}
}

static Future<Void> testEncryptionKeyAfterReadFailure(std::string path);

static Future<Void> writeEncryptionMetadataIfNotExists(Reference<BackupContainerFileSystem> bc,
int encryptionBlockSize) {
try {
Expand Down Expand Up @@ -2377,14 +2378,33 @@ std::string BackupContainerFileSystem::encryptionMetadataFileName() {
bool BackupContainerFileSystem::usesEncryption() const {
return encryptionKey.isValid();
}

Future<std::shared_ptr<const StreamCipherKey>> BackupContainerFileSystem::getEncryptionKey() const {
if (encryptionKey.isError() && encryptionKey.getError().isDiskError()) {
lastKeyLoadError = encryptionKey.getError();
encryptionKey = BackupContainerFileSystemImpl::readEncryptionKey(encryptionKeyFileName.get());
}
return encryptionKey;
}

Future<Void> BackupContainerFileSystem::encryptionSetupComplete() const {
return usesEncryption() ? success(encryptionKey) : Future<Void>();
return usesEncryption() ? success(getEncryptionKey()) : Future<Void>();
}

Future<Void> BackupContainerFileSystem::encryptionSetupStatus() const {
auto setup = encryptionSetupComplete();
// Repeatedly waiting through the disk retry delays can exhaust every status transaction's lifetime.
if (lastKeyLoadError.isValid() && !setup.isReady()) {
return lastKeyLoadError;
}
return setup;
}

Future<Reference<IAsyncFile>> BackupContainerFileSystem::encryptedFile(Future<Reference<IAsyncFile>> file,
AsyncFileEncrypted::Mode mode) const {
return map(success(file) && encryptionSetupComplete(),
[file, key = encryptionKey, mode, blockSize = encryptionBlockSize](Void) -> Reference<IAsyncFile> {
auto key = getEncryptionKey();
return map(success(file) && success(key),
[file, key, mode, blockSize = encryptionBlockSize](Void) -> Reference<IAsyncFile> {
return makeReference<AsyncFileEncrypted>(file.get(), mode, blockSize, key.get());
});
}
Expand All @@ -2396,6 +2416,7 @@ Future<Void> BackupContainerFileSystem::writeEntireFileFallback(const std::strin
}

void BackupContainerFileSystem::setEncryptionKey(Optional<std::string> const& encryptionKeyFileName) {
this->encryptionKeyFileName = encryptionKeyFileName;
if (encryptionKeyFileName.present()) {
encryptionKey = BackupContainerFileSystemImpl::readEncryptionKey(encryptionKeyFileName.get());
}
Expand All @@ -2405,6 +2426,34 @@ Future<Void> BackupContainerFileSystem::createTestEncryptionKeyFile(std::string
return BackupContainerFileSystemImpl::createTestEncryptionKeyFile(filename);
}

Future<Void> BackupContainerFileSystemImpl::testEncryptionKeyAfterReadFailure(std::string path) {
const std::string keyFile = path + ".key";
co_await BackupContainerFileSystem::createTestEncryptionKeyFile(keyFile);
auto container = makeReference<BackupContainerLocalDirectory>("file://" + path, keyFile, 4096);
co_await container->encryptionSetupStatus();

container->encryptionKey = io_timeout();
auto status = container->encryptionSetupStatus();
ASSERT(status.isReady());
auto first = container->encryptionKey;
auto second = container->getEncryptionKey();
if (!first.isReady()) {
ASSERT(status.isError());
ASSERT_EQ(status.getError().code(), error_code_io_timeout);
ASSERT(container->encryptionSetupStatus().isError());
ASSERT(!container->encryptionSetupComplete().isReady());
}
co_await (success(first) && success(second));
ASSERT(first.get() == second.get());
ASSERT(container->getEncryptionKey().get() == first.get());
ASSERT(container->encryptionSetupStatus().canGet());

container->encryptionKey = invalid_encryption_key_file();
auto invalid = container->encryptionSetupStatus();
ASSERT(invalid.isError());
ASSERT_EQ(invalid.getError().code(), error_code_invalid_encryption_key_file);
}

namespace backup_test {

int chooseFileSize(std::vector<int>& sizes) {
Expand All @@ -2416,6 +2465,11 @@ int chooseFileSize(std::vector<int>& sizes) {
return deterministicRandom()->randomInt(0, 2e6);
}

TEST_CASE("/backup/containers/encryptionKeyAfterReadFailure") {
co_await BackupContainerFileSystemImpl::testEncryptionKeyAfterReadFailure(
joinPath(params.getDataDir(), deterministicRandom()->randomUniqueID().toString()));
}

TEST_CASE("/backup/containers/localdir/independentEncryptionKeys") {
const std::string base = joinPath(params.getDataDir(), deterministicRandom()->randomUniqueID().toString());
auto open = [&](const std::string& name) {
Expand Down
3 changes: 3 additions & 0 deletions fdbclient/include/fdbclient/BackupContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,10 @@ class IBackupContainer {

static std::string lastOpenError;

// Wait for a usable key, retrying a previous disk failure.
virtual Future<Void> encryptionSetupComplete() const = 0;
// Wait for the first key load. During a retry, report the last completed result.
virtual Future<Void> encryptionSetupStatus() const { return encryptionSetupComplete(); }

virtual int getEncryptionBlockSize() const { return 0; }
virtual void setEncryptionBlockSize(int blockSize) {}
Expand Down
6 changes: 4 additions & 2 deletions fdbclient/include/fdbclient/BackupContainerFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ class BackupContainerFileSystem : public IBackupContainer {

Future<Void> writeEncryptionMetadata(int encryptionBlockSize) override;

// Waits for encryption initialization to complete by reading encryption key file during container opening.
Future<Void> encryptionSetupComplete() const override;
Future<Void> encryptionSetupStatus() const override;

int getEncryptionBlockSize() const override { return encryptionBlockSize; }
void setEncryptionBlockSize(int blockSize) override { encryptionBlockSize = blockSize; }
Expand Down Expand Up @@ -229,7 +229,9 @@ class BackupContainerFileSystem : public IBackupContainer {

friend class BackupContainerFileSystemImpl;

Future<std::shared_ptr<const StreamCipherKey>> encryptionKey;
Future<std::shared_ptr<const StreamCipherKey>> getEncryptionKey() const;
mutable Future<std::shared_ptr<const StreamCipherKey>> encryptionKey;
mutable Error lastKeyLoadError;
};

#endif
Loading