Skip to content

Commit 9b25def

Browse files
authored
Revert "Revert "Xdb 402 decode password (#201)" (#202)" (#204)
This reverts commit 32e1b46.
1 parent 32e1b46 commit 9b25def

3 files changed

Lines changed: 100 additions & 3 deletions

File tree

fdbcli/fdbcli.actor.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ enum {
104104
OPT_DEBUG_TLS,
105105
OPT_API_VERSION,
106106
OPT_MEMORY,
107-
OPT_USE_FUTURE_PROTOCOL_VERSION
107+
OPT_USE_FUTURE_PROTOCOL_VERSION,
108+
OPT_ENCRYPT
108109
};
109110

110111
CSimpleOpt::SOption g_rgOptions[] = { { OPT_CONNFILE, "-C", SO_REQ_SEP },
@@ -130,6 +131,7 @@ CSimpleOpt::SOption g_rgOptions[] = { { OPT_CONNFILE, "-C", SO_REQ_SEP },
130131
{ OPT_API_VERSION, "--api-version", SO_REQ_SEP },
131132
{ OPT_MEMORY, "--memory", SO_REQ_SEP },
132133
{ OPT_USE_FUTURE_PROTOCOL_VERSION, "--use-future-protocol-version", SO_NONE },
134+
{ OPT_ENCRYPT, "--encrypt", SO_REQ_SEP },
133135
TLS_OPTION_FLAGS,
134136
SO_END_OF_OPTIONS };
135137

@@ -503,6 +505,11 @@ static void printProgramUsage(const char* name) {
503505
" --use-future-protocol-version\n"
504506
" Use the simulated future protocol version to connect to the cluster.\n"
505507
" This option can be used testing purposes only!\n"
508+
" --encrypt PASSWORD\n"
509+
" Encrypts the specified password and prints the encrypted password\n"
510+
" with the `encrypted:' prefix. The encrypted password can be used\n"
511+
" with --tls-password option. This option causes fdbcli to encrypt\n"
512+
" the password and exit.\n"
506513
" -v, --version Print FoundationDB CLI version information and exit.\n"
507514
" -h, --help Display this help and exit.\n");
508515
}
@@ -899,7 +906,6 @@ void LogCommand(std::string line, UID randomID, std::string errMsg) {
899906
printf("%s\n", errMsg.c_str());
900907
TraceEvent(SevInfo, "CLICommandLog", randomID).detail("Command", line).detail("Error", errMsg);
901908
}
902-
903909
struct CLIOptions {
904910
std::string program_name;
905911
int exit_code = -1;
@@ -923,6 +929,7 @@ struct CLIOptions {
923929
std::string tlsCAPath;
924930
std::string tlsPassword;
925931
uint64_t memLimit = 8uLL << 30;
932+
Optional<std::string> encrypt;
926933

927934
std::vector<std::pair<std::string, std::string>> knobs;
928935

@@ -1062,6 +1069,9 @@ struct CLIOptions {
10621069
knobs.emplace_back(knobName.get(), args.OptionArg());
10631070
break;
10641071
}
1072+
case OPT_ENCRYPT:
1073+
encrypt = args.OptionArg();
1074+
break;
10651075
case OPT_DEBUG_TLS:
10661076
debugTLS = true;
10671077
break;
@@ -2385,6 +2395,16 @@ int main(int argc, char** argv) {
23852395
if (opt.exit_code != -1)
23862396
return opt.exit_code;
23872397

2398+
if (opt.encrypt.present()) {
2399+
std::string encrypted;
2400+
if (!TLSConfig::encodePassword(opt.encrypt.get(), encrypted)) {
2401+
fprintf(stderr, "ERROR: Failed to encrypt password\n");
2402+
return 1;
2403+
}
2404+
printf("%s\n", encrypted.c_str());
2405+
return 0;
2406+
}
2407+
23882408
if (opt.trace) {
23892409
if (opt.traceDir.empty())
23902410
setNetworkOption(FDBNetworkOptions::TRACE_ENABLE);

flow/TLSConfig.actor.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,3 +832,78 @@ bool TLSPolicy::verify_peer(bool preverified, X509_STORE_CTX* store_ctx) {
832832
}
833833
return rc;
834834
}
835+
struct CryptoLibHandle {
836+
void* lib = nullptr;
837+
void* func = nullptr;
838+
839+
CryptoLibHandle(std::string_view funcName) {
840+
const char* libName = "libnscipher-crypto.so";
841+
lib = loadLibrary(libName);
842+
if (!lib) {
843+
TraceEvent(SevError, "ExternalLibLoadError").detail("Library", libName);
844+
return;
845+
}
846+
func = loadFunction(lib, funcName.data());
847+
if (!func) {
848+
TraceEvent(SevError, "ExternalLibFunctionLoadError")
849+
.detail("Function", funcName)
850+
.detail("Library", libName);
851+
fprintf(stderr, "ERROR: Failed to load '%s' function\n", funcName.data());
852+
closeLibrary(lib);
853+
lib = nullptr;
854+
}
855+
}
856+
explicit operator bool() const { return func != nullptr; }
857+
~CryptoLibHandle() {
858+
if (lib)
859+
closeLibrary(lib);
860+
}
861+
};
862+
863+
static bool processWithCrypto(std::string_view funcName, const std::string& input, std::string& output) {
864+
constexpr int bufLen = 1024; // Assume max size of encrypted and decrypted password is 1024
865+
CryptoLibHandle cryptoHandle(funcName);
866+
867+
if (!cryptoHandle) {
868+
return false;
869+
}
870+
871+
int outputLen = bufLen;
872+
873+
char buf[bufLen]{};
874+
875+
auto func = reinterpret_cast<int (*)(const char*, char*, int*)>(cryptoHandle.func);
876+
if (int rc = func(input.c_str(), buf, &outputLen); rc != 0) {
877+
fprintf(stderr, "ERROR: Failed to exec function (rc=%d)\n", rc);
878+
TraceEvent(SevError, "ErrorExecFunction").detail("ReturnCode", rc);
879+
return false;
880+
}
881+
output.assign(buf, outputLen);
882+
return true;
883+
}
884+
885+
constexpr std::string_view encryptedPrefix = "encrypted:";
886+
887+
bool TLSConfig::encodePassword(const std::string& plainPassword, std::string& encoded) {
888+
if (processWithCrypto("crypt", plainPassword, encoded)) {
889+
encoded.insert(0, encryptedPrefix);
890+
return true;
891+
}
892+
return false;
893+
}
894+
895+
void TLSConfig::setPassword(const std::string& password) {
896+
if (password.size() > encryptedPrefix.size() && password.starts_with(encryptedPrefix)) {
897+
898+
std::string decoded;
899+
900+
if (processWithCrypto("decrypt", password.substr(encryptedPrefix.size()), decoded)) {
901+
tlsPassword = std::move(decoded);
902+
} else {
903+
tlsPassword.clear();
904+
TraceEvent(SevError, "FailedToDecryptPassword");
905+
}
906+
} else {
907+
tlsPassword = password;
908+
}
909+
}

flow/include/flow/TLSConfig.actor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ class TLSConfig {
173173
tlsCAPath = "";
174174
}
175175

176-
void setPassword(const std::string& password) { tlsPassword = password; }
176+
void setPassword(const std::string& password);
177+
178+
static bool encodePassword(const std::string& plainPassword, std::string& encoded);
177179

178180
void clearVerifyPeers() { tlsVerifyPeers.clear(); }
179181

0 commit comments

Comments
 (0)