in nodemanager/utils/System.cpp [536:596]
int System::AddSshKey(
const std::string& userName,
const std::string& key,
const std::string& fileName,
const std::string& filePermission,
std::string& filePath)
{
std::string output;
int ret = System::GetHomeDir(userName, output);
if (0 != ret) { return ret; }
std::string homeDir = output;
std::string sshFolder = String::Join("", homeDir, "/.ssh/");
Logger::Debug("User {0}'s ssh folder {1}", userName, sshFolder);
ret = System::ExecuteCommandOut(
output,
"[ -d ", homeDir, " ] || (mkdir -p ", homeDir, " && chown ", userName, " ", homeDir, ")",
" && [ -d ", sshFolder, " ] || mkdir ", sshFolder,
" && chown ", userName, " ", sshFolder, " && chmod 700 ", sshFolder);
if (ret != 0)
{
Logger::Info("Cannot create folder {0}, error code {1}", sshFolder, ret);
return ret;
}
filePath = String::Join("", sshFolder, fileName);
if (!key.empty())
{
std::ifstream test(filePath);
// won't overwrite existing user's private key
if (!test.good())
{
std::ofstream keyFile(filePath, std::ios::trunc);
keyFile << key;
keyFile.close();
ret = System::ExecuteCommandOut(output, "chown", userName, filePath, "&& chmod", filePermission, filePath);
if (0 != ret)
{
Logger::Error("Error when change the file {0}'s permission to {1}, ret {2}", filePath, filePermission, ret);
}
}
else
{
Logger::Info("File {0} exist, skip overwriting", filePath);
ret = -2;
}
test.close();
return ret;
}
return -1;
}