From d95cc33a58d83ab8f580c40cc3b0210f842b7429 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Sun, 21 Feb 2016 22:15:27 +0100 Subject: [PATCH] You can disable the automatic update check by setting CRYFS_NO_UPDATE_CHECK=true in your environment. --- ChangeLog.txt | 1 + src/cryfs/CMakeLists.txt | 1 + src/cryfs/cli/Cli.cpp | 16 ++++---- src/cryfs/cli/Cli.h | 3 +- src/cryfs/cli/Environment.cpp | 19 +++++++++ src/cryfs/cli/Environment.h | 24 +++++++++++ test/cryfs/CMakeLists.txt | 1 + test/cryfs/cli/EnvironmentTest.cpp | 64 ++++++++++++++++++++++++++++++ 8 files changed, 120 insertions(+), 9 deletions(-) create mode 100644 src/cryfs/cli/Environment.cpp create mode 100644 src/cryfs/cli/Environment.h create mode 100644 test/cryfs/cli/EnvironmentTest.cpp diff --git a/ChangeLog.txt b/ChangeLog.txt index d9f19e5f..ce255279 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -3,6 +3,7 @@ Version 0.9.3 (unreleased) * It's easier for tools and scripts to use CryFS: If an environment variable CRYFS_FRONTEND=noninteractive is set, we don't ask for options (but take default values for everything that's not specified on command line). Furthermore, we won't ask for password confirmation when creating a file system but the password only has to be sent once to stdin. +* You can disable the automatic update check by setting CRYFS_NO_UPDATE_CHECK=true in your environment. Version 0.9.2 --------------- diff --git a/src/cryfs/CMakeLists.txt b/src/cryfs/CMakeLists.txt index fdd5c12a..a4adcb44 100644 --- a/src/cryfs/CMakeLists.txt +++ b/src/cryfs/CMakeLists.txt @@ -5,6 +5,7 @@ set(SOURCES cli/VersionChecker.cpp cli/VersionCompare.cpp cli/CallAfterTimeout.cpp + cli/Environment.cpp cli/program_options/utils.cpp cli/program_options/ProgramOptions.cpp cli/program_options/Parser.cpp diff --git a/src/cryfs/cli/Cli.cpp b/src/cryfs/cli/Cli.cpp index 9dd61d5d..6ecf5bc1 100644 --- a/src/cryfs/cli/Cli.cpp +++ b/src/cryfs/cli/Cli.cpp @@ -22,6 +22,7 @@ #include "VersionChecker.h" #include "VersionCompare.h" +#include "Environment.h" //TODO Fails with gpg-homedir in filesystem: gpg --homedir gpg-homedir --gen-key //TODO Many functions accessing the ProgramOptions object. Factor out into class that stores it as a member. @@ -70,15 +71,10 @@ using cpputils::dynamic_pointer_move; //TODO Performance difference when setting compiler parameter -maes for scrypt? namespace cryfs { - const string Cli::CRYFS_FRONTEND_KEY = "CRYFS_FRONTEND"; - const string Cli::CRYFS_FRONTEND_NONINTERACTIVE = "noninteractive"; Cli::Cli(RandomGenerator &keyGenerator, const SCryptSettings &scryptSettings, shared_ptr console, shared_ptr httpClient): _keyGenerator(keyGenerator), _scryptSettings(scryptSettings), _console(console), _httpClient(httpClient), _noninteractive(false) { - char *frontend = std::getenv(CRYFS_FRONTEND_KEY.c_str()); - if (frontend != nullptr && frontend == CRYFS_FRONTEND_NONINTERACTIVE) { - _noninteractive = true; - } + _noninteractive = Environment::isNoninteractive(); } void Cli::_showVersion() { @@ -95,6 +91,13 @@ namespace cryfs { #ifndef NDEBUG cout << "WARNING! This is a debug build. Performance might be slow." << endl; #endif + if (!Environment::noUpdateCheck()) { + _checkForUpdates(); + } + cout << endl; + } + + void Cli::_checkForUpdates() { VersionChecker versionChecker(_httpClient); optional newestVersion = versionChecker.newestVersion(); if (newestVersion == none) { @@ -106,7 +109,6 @@ namespace cryfs { if (securityWarning != none) { cout << *securityWarning << endl; } - cout << endl; } bool Cli::_checkPassword(const string &password) { diff --git a/src/cryfs/cli/Cli.h b/src/cryfs/cli/Cli.h index 5fa00ba9..587781a4 100644 --- a/src/cryfs/cli/Cli.h +++ b/src/cryfs/cli/Cli.h @@ -19,6 +19,7 @@ namespace cryfs { int main(int argc, char *argv[]); private: + void _checkForUpdates(); void _runFilesystem(const program_options::ProgramOptions &options); CryConfigFile _loadOrCreateConfig(const program_options::ProgramOptions &options); boost::optional _loadOrCreateConfigFile(const boost::filesystem::path &configFilePath, const boost::optional &cipher); @@ -40,8 +41,6 @@ namespace cryfs { boost::optional> _createIdleCallback(boost::optional minutes, std::function callback); void _sanityCheckFilesystem(CryDevice *device); - static const std::string CRYFS_FRONTEND_KEY; - static const std::string CRYFS_FRONTEND_NONINTERACTIVE; cpputils::RandomGenerator &_keyGenerator; cpputils::SCryptSettings _scryptSettings; diff --git a/src/cryfs/cli/Environment.cpp b/src/cryfs/cli/Environment.cpp new file mode 100644 index 00000000..19166b28 --- /dev/null +++ b/src/cryfs/cli/Environment.cpp @@ -0,0 +1,19 @@ +#include "Environment.h" +#include + +using std::string; + +namespace cryfs { + const string Environment::FRONTEND_KEY = "CRYFS_FRONTEND"; + const string Environment::FRONTEND_NONINTERACTIVE = "noninteractive"; + const string Environment::NOUPDATECHECK_KEY = "CRYFS_NO_UPDATE_CHECK"; + + bool Environment::isNoninteractive() { + char *frontend = std::getenv(FRONTEND_KEY.c_str()); + return frontend != nullptr && frontend == FRONTEND_NONINTERACTIVE; + } + + bool Environment::noUpdateCheck() { + return nullptr != std::getenv(NOUPDATECHECK_KEY.c_str()); + } +} diff --git a/src/cryfs/cli/Environment.h b/src/cryfs/cli/Environment.h new file mode 100644 index 00000000..3b7cb9e5 --- /dev/null +++ b/src/cryfs/cli/Environment.h @@ -0,0 +1,24 @@ +#pragma once +#ifndef MESSMER_CRYFS_CLI_ENVIRONMENT_H +#define MESSMER_CRYFS_CLI_ENVIRONMENT_H + +#include + +namespace cryfs { + + class Environment { + public: + static bool isNoninteractive(); + static bool noUpdateCheck(); + + private: + Environment() = delete; + + static const std::string FRONTEND_KEY; + static const std::string FRONTEND_NONINTERACTIVE; + static const std::string NOUPDATECHECK_KEY; + }; + +} + +#endif diff --git a/test/cryfs/CMakeLists.txt b/test/cryfs/CMakeLists.txt index 0790a4a7..a775ed05 100644 --- a/test/cryfs/CMakeLists.txt +++ b/test/cryfs/CMakeLists.txt @@ -9,6 +9,7 @@ set(SOURCES cli/program_options/ProgramOptionsTest.cpp cli/program_options/ParserTest.cpp cli/CliTest_ShowingHelp.cpp + cli/EnvironmentTest.cpp cli/VersionCheckerTest.cpp cli/VersionCompareTest.cpp config/crypto/CryConfigEncryptorFactoryTest.cpp diff --git a/test/cryfs/cli/EnvironmentTest.cpp b/test/cryfs/cli/EnvironmentTest.cpp new file mode 100644 index 00000000..8c1a312e --- /dev/null +++ b/test/cryfs/cli/EnvironmentTest.cpp @@ -0,0 +1,64 @@ +#include +#include +#include + +using namespace cryfs; +using std::string; +using boost::optional; +using boost::none; + +class EnvironmentTest : public ::testing::Test { +public: + // WithEnv sets an environment variable while it is in scope. + // Once it leaves scope, the environment is reset. + class WithEnv { + public: + WithEnv(const string &key, const string &value): _key(key) , _oldValue(none) { + char *oldValue = std::getenv(key.c_str()); + if (nullptr != oldValue) { + _oldValue = string(oldValue); + } + ::setenv(key.c_str(), value.c_str(), 1); + } + ~WithEnv() { + if (none == _oldValue) { + ::unsetenv(_key.c_str()); + } else { + ::setenv(_key.c_str(), _oldValue->c_str(), 1); + } + } + + private: + string _key; + optional _oldValue; + }; +}; + +TEST_F(EnvironmentTest, Noninteractive_Unset) { + EXPECT_FALSE(Environment::isNoninteractive()); +} + +TEST_F(EnvironmentTest, Noninteractive_Set) { + WithEnv env("CRYFS_FRONTEND", "noninteractive"); + EXPECT_TRUE(Environment::isNoninteractive()); +} + +TEST_F(EnvironmentTest, Noninteractive_SetToOtherValue) { + WithEnv env("CRYFS_FRONTEND", "someotherfrontend"); + EXPECT_FALSE(Environment::isNoninteractive()); +} + +TEST_F(EnvironmentTest, NoUpdateCheck_Unset) { + EXPECT_FALSE(Environment::noUpdateCheck()); +} + +TEST_F(EnvironmentTest, NoUpdateCheck_Set) { + WithEnv env("CRYFS_NO_UPDATE_CHECK", "true"); + EXPECT_TRUE(Environment::noUpdateCheck()); +} + +TEST_F(EnvironmentTest, NoUpdateCheck_SetToOtherValue) { + WithEnv env("CRYFS_NO_UPDATE_CHECK", "someothervalue"); + // No matter what the value is, setting the environment variable says we don't do update checks. + EXPECT_TRUE(Environment::noUpdateCheck()); +}