Skip to content

Backup file format decryption tool #19932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
80 changes: 80 additions & 0 deletions ydb/core/backup/tools/decrypt/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <ydb/core/backup/common/encryption.h>

#include <library/cpp/getopt/opt.h>

#include <util/generic/size_literals.h>
#include <util/stream/file.h>

struct TOptions {
NKikimr::NBackup::TEncryptionKey Key;
TString KeyFile;
TString InputFile;

public:
TOptions(int argc, const char* argv[]) {
try {
ParseOptions(argc, argv);
} catch (const std::exception&) {
Cerr << "Failed to get options: " << CurrentExceptionMessage() << Endl;
exit(1);
}
}

private:
void ParseOptions(int argc, const char** argv) {
NLastGetopt::TOpts opts;
opts.SetTitle("Backup file decryption tool");
opts.SetFreeArgsNum(0);
opts.AddHelpOption('h');
opts.AddVersionOption();

opts.AddLongOption('k', "encryption-key-file", "Encryption key file")
.RequiredArgument("KEY")
.StoreResult(&KeyFile);

opts.AddLongOption('i', "input-file", "Input file")
.RequiredArgument("PATH")
.StoreResult(&InputFile);

NLastGetopt::TOptsParseResult res(&opts, argc, argv);

if (KeyFile) {
TFileInput keyFile(KeyFile);
TString keyData = keyFile.ReadAll();
Key = NKikimr::NBackup::TEncryptionKey(keyData);
} else {
Cerr << "No encryption key file provided" << Endl;
exit(1);
}
}
};


int main(int argc, const char* argv[]) {
TOptions options(argc, argv);
try {
NKikimr::NBackup::TEncryptedFileDeserializer deserializer(options.Key);
std::optional<TFileInput> inputFile;
IInputStream* in = &Cin;
if (options.InputFile) {
inputFile.emplace(options.InputFile);
in = &*inputFile;
}
char buffer[4_MB];
while (size_t bytes = in->Read(buffer, sizeof(buffer))) {
deserializer.AddData(TBuffer(buffer, bytes), false);
if (TMaybe<TBuffer> block = deserializer.GetNextBlock()) {
Cout.Write(block->Data(), block->Size());
}
}
deserializer.AddData(TBuffer(), true);
if (TMaybe<TBuffer> block = deserializer.GetNextBlock()) {
Cout.Write(block->Data(), block->Size());
}
Cerr << "IV: " << deserializer.GetIV().GetHexString() << Endl;
return 0;
} catch (const std::exception& ex) {
Cerr << "Error: " << ex.what() << Endl;
return 1;
}
}
12 changes: 12 additions & 0 deletions ydb/core/backup/tools/decrypt/ya.make
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
PROGRAM()

PEERDIR(
library/cpp/getopt
ydb/core/backup/common
)

SRCS(
main.cpp
)

END()
3 changes: 3 additions & 0 deletions ydb/core/backup/tools/ya.make
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RECURSE(
decrypt
)
1 change: 1 addition & 0 deletions ydb/core/backup/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ RECURSE(
common
controller
impl
tools
)

RECURSE_FOR_TESTS(
Expand Down