Commit 653c3bf0 authored by Georgios Bitzes's avatar Georgios Bitzes
Browse files

Ignore trailing whitespace when reading password files

Most will be a single line - this makes them easily
copy-pasteable without having to worry about newlines.
parent 4738e257
Pipeline #420168 passed with stages
in 25 minutes and 55 seconds
......@@ -139,6 +139,14 @@ bool readPasswordFile(const std::string &path, std::string &contents) {
retvalue = readFile(in, contents);
fclose(in);
if(retvalue) {
// Right trim any newlines and whitespace. By far the most common case will
// be to have a single line in the password file. Users will expect to be
// able to copy/paste that, let's not complicate matters with newlines.
contents.erase(contents.find_last_not_of(" \t\n\r\f\v") + 1);
}
return retvalue;
}
......
......@@ -56,7 +56,7 @@ TEST(ReadPasswordFile, BasicSanity) {
ASSERT_FALSE(readPasswordFile("/tmp/quarkdb-tests/auth/non-existing", contents));
ASSERT_FALSE(readFile("/tmp/quarkdb-tests/auth/non-existing", contents));
ASSERT_EQ(system("echo 'pickles' > /tmp/quarkdb-tests/auth/f1"), 0);
ASSERT_EQ(system("echo 'pickles\n\n ' > /tmp/quarkdb-tests/auth/f1"), 0);
ASSERT_EQ(system("chmod 777 /tmp/quarkdb-tests/auth/f1"), 0);
ASSERT_FALSE(readPasswordFile("/tmp/quarkdb-tests/auth/f1", contents));
ASSERT_EQ(system("chmod 744 /tmp/quarkdb-tests/auth/f1"), 0);
......@@ -67,6 +67,7 @@ TEST(ReadPasswordFile, BasicSanity) {
ASSERT_FALSE(readPasswordFile("/tmp/quarkdb-tests/auth/f1", contents));
ASSERT_EQ(system("chmod 400 /tmp/quarkdb-tests/auth/f1"), 0);
ASSERT_TRUE(readPasswordFile("/tmp/quarkdb-tests/auth/f1", contents));
ASSERT_EQ(contents, "pickles");
ASSERT_EQ(system("rm -f /tmp/quarkdb-tests/auth/f1"), 0);
}
......
......@@ -62,12 +62,12 @@ TEST(Configuration, T2) {
ASSERT_TRUE(config.getRequirePasswordForLocalhost());
ASSERT_THROW(config.extractPasswordOrDie(), FatalException); // file does not exist
ASSERT_EQ(system("echo 'pickles' > /tmp/quarkdb-tests/password-file"), 0);
ASSERT_EQ(system("echo 'pickles\n \n' > /tmp/quarkdb-tests/password-file"), 0);
ASSERT_EQ(system("chmod 700 /tmp/quarkdb-tests/password-file"), 0);
ASSERT_THROW(config.extractPasswordOrDie(), FatalException); // bad permissions
ASSERT_EQ(system("chmod 400 /tmp/quarkdb-tests/password-file"), 0);
ASSERT_EQ(config.extractPasswordOrDie(), "pickles\n");
ASSERT_EQ(config.extractPasswordOrDie(), "pickles");
ASSERT_EQ(system("chmod 700 /tmp/quarkdb-tests/password-file"), 0);
ASSERT_EQ(system("rm -f /tmp/quarkdb-tests/password-file"), 0);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment