diff options
author | marcinjb <marcinjb@chromium.org> | 2016-01-15 14:18:36 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-01-15 22:19:39 +0000 |
commit | 446acf6543358a806704d1cacc69255e8600079d (patch) | |
tree | 01db166faa1fc22512e1e08a239f4b8b36cb195b /blimp | |
parent | 0918e57467b6f6834ee8199349b64b0a2409b894 (diff) | |
download | chromium_src-446acf6543358a806704d1cacc69255e8600079d.zip chromium_src-446acf6543358a806704d1cacc69255e8600079d.tar.gz chromium_src-446acf6543358a806704d1cacc69255e8600079d.tar.bz2 |
Add BlimpEngineConfig to verify command line switches and store config
Initially this has the shared secret between the client and the engine.
BUG=575421
Review URL: https://codereview.chromium.org/1489413005
Cr-Commit-Position: refs/heads/master@{#369861}
Diffstat (limited to 'blimp')
-rw-r--r-- | blimp/docs/container.md | 12 | ||||
-rw-r--r-- | blimp/docs/running.md | 4 | ||||
-rw-r--r-- | blimp/engine/Dockerfile | 11 | ||||
-rw-r--r-- | blimp/engine/browser/BUILD.gn | 5 | ||||
-rw-r--r-- | blimp/engine/browser/blimp_browser_main_parts.cc | 12 | ||||
-rw-r--r-- | blimp/engine/browser/blimp_browser_main_parts.h | 2 | ||||
-rw-r--r-- | blimp/engine/browser/blimp_engine_config.cc | 59 | ||||
-rw-r--r-- | blimp/engine/browser/blimp_engine_config.h | 58 | ||||
-rw-r--r-- | blimp/engine/browser/blimp_engine_config_unittest.cc | 86 | ||||
-rw-r--r-- | blimp/engine/browser/switches.cc | 13 | ||||
-rw-r--r-- | blimp/engine/browser/switches.h | 17 |
11 files changed, 274 insertions, 5 deletions
diff --git a/blimp/docs/container.md b/blimp/docs/container.md index b817072..36a5cee 100644 --- a/blimp/docs/container.md +++ b/blimp/docs/container.md @@ -75,3 +75,15 @@ docker run blimp_engine --with-my-flags See the [blimp engine `Dockerfile`](../engine/Dockerfile) to find out what flags are passed by default. + +### Mapping Volumes into the Docker Container + +If you need to map a directory into the Docker container (eg. for necessary +files): + +```bash +docker run -v /path/to/srcdir:/path/to/docker/destdir blimp_engine +``` + +NB: The permissions of the directory and the files outside of the Docker +container will be carried over into the container. diff --git a/blimp/docs/running.md b/blimp/docs/running.md index b8bba0f..9678a09 100644 --- a/blimp/docs/running.md +++ b/blimp/docs/running.md @@ -42,3 +42,7 @@ client device via USB, you'll need remote port forwarding to allow the Blimp client to talk to your computer. Follow the instructions [here](https://developer.chrome.com/devtools/docs/remote-debugging) to get started. You'll probably want to remap 25467 to "localhost:25467". + +### Required flags +* `--blimp-client-token-path=$PATH`: Path to a file containing a nonempty +token string. If this is not present, the engine will fail to boot. diff --git a/blimp/engine/Dockerfile b/blimp/engine/Dockerfile index d939115..732ceae 100644 --- a/blimp/engine/Dockerfile +++ b/blimp/engine/Dockerfile @@ -18,8 +18,9 @@ RUN chown -R blimp_user /engine USER blimp_user WORKDIR "/engine" -ENTRYPOINT ["/engine/blimp_engine_app", "--disable-gpu", \ - "--use-remote-compositing", \ - # Retains display items for - # serialization on the engine. - "--disable-cached-picture-raster"] +ENTRYPOINT ["/engine/blimp_engine_app", \ + "--disable-gpu", \ + "--use-remote-compositing", \ + # Retains display items for serialization on the engine. + "--disable-cached-picture-raster", \ + "--blimp-client-token-path=/engine/data/client_token"] diff --git a/blimp/engine/browser/BUILD.gn b/blimp/engine/browser/BUILD.gn index 3539896..4fa99ba 100644 --- a/blimp/engine/browser/BUILD.gn +++ b/blimp/engine/browser/BUILD.gn @@ -10,6 +10,8 @@ source_set("browser") { "blimp_browser_main_parts.h", "blimp_content_browser_client.cc", "blimp_content_browser_client.h", + "blimp_engine_config.cc", + "blimp_engine_config.h", "blimp_engine_session.cc", "blimp_engine_session.h", "blimp_network_delegate.cc", @@ -20,6 +22,8 @@ source_set("browser") { "blimp_url_request_context_getter.h", "engine_render_widget_feature.cc", "engine_render_widget_feature.h", + "switches.cc", + "switches.h", ] deps = [ @@ -44,6 +48,7 @@ source_set("unit_tests") { testonly = true sources = [ + "blimp_engine_config_unittest.cc", "engine_render_widget_feature_unittest.cc", ] diff --git a/blimp/engine/browser/blimp_browser_main_parts.cc b/blimp/engine/browser/blimp_browser_main_parts.cc index ad20a38..c35d694 100644 --- a/blimp/engine/browser/blimp_browser_main_parts.cc +++ b/blimp/engine/browser/blimp_browser_main_parts.cc @@ -4,8 +4,11 @@ #include "blimp/engine/browser/blimp_browser_main_parts.h" +#include "base/command_line.h" +#include "base/threading/thread_restrictions.h" #include "blimp/common/proto/blimp_message.pb.h" #include "blimp/engine/browser/blimp_browser_context.h" +#include "blimp/engine/browser/blimp_engine_config.h" #include "blimp/engine/browser/blimp_engine_session.h" #include "blimp/net/blimp_connection.h" #include "content/public/browser/browser_thread.h" @@ -30,6 +33,15 @@ void BlimpBrowserMainParts::PreEarlyInitialization() { // TODO(haibinlu): Rename the method below. crbug/548330. ui::InitializeInputMethodForTesting(); #endif + // Fetch the engine config from the command line, and crash if invalid. Allow + // IO operations even though this is not in the FILE thread as this is + // necessary for Blimp startup and occurs before any user interaction. + { + const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); + base::ThreadRestrictions::ScopedAllowIO allow_io; + engine_config_ = BlimpEngineConfig::Create(*cmd_line); + CHECK(engine_config_); + } } void BlimpBrowserMainParts::PreMainMessageLoopRun() { diff --git a/blimp/engine/browser/blimp_browser_main_parts.h b/blimp/engine/browser/blimp_browser_main_parts.h index 3e42a3a..803ac79 100644 --- a/blimp/engine/browser/blimp_browser_main_parts.h +++ b/blimp/engine/browser/blimp_browser_main_parts.h @@ -22,6 +22,7 @@ namespace blimp { namespace engine { class BlimpBrowserContext; +class BlimpEngineConfig; class BlimpEngineSession; class BlimpBrowserMainParts : public content::BrowserMainParts { @@ -37,6 +38,7 @@ class BlimpBrowserMainParts : public content::BrowserMainParts { BlimpBrowserContext* GetBrowserContext(); private: + scoped_ptr<BlimpEngineConfig> engine_config_; scoped_ptr<net::NetLog> net_log_; scoped_ptr<BlimpEngineSession> engine_session_; diff --git a/blimp/engine/browser/blimp_engine_config.cc b/blimp/engine/browser/blimp_engine_config.cc new file mode 100644 index 0000000..da74bb7 --- /dev/null +++ b/blimp/engine/browser/blimp_engine_config.cc @@ -0,0 +1,59 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "blimp/engine/browser/blimp_engine_config.h" + +#include <string> + +#include "base/command_line.h" +#include "base/files/file_path.h" +#include "base/files/file_util.h" +#include "base/memory/scoped_ptr.h" +#include "base/strings/string_util.h" +#include "blimp/engine/browser/switches.h" + +namespace blimp { +namespace engine { + +namespace { +// Gets the client token from the file provided by the command line. If a read +// does not succeed, or the switch is malformed, an empty string is returned. +std::string GetClientToken(const base::CommandLine& cmd_line) { + std::string file_contents; + const base::FilePath path = cmd_line.GetSwitchValuePath(kClientTokenPath); + if (!base::ReadFileToString(path, &file_contents)) { + LOG(ERROR) << "Could not read client token file at " + << (path.empty() ? "(not provided)" : path.AsUTF8Unsafe()); + } + return base::CollapseWhitespaceASCII(file_contents, true); +} +} // namespace + +BlimpEngineConfig::~BlimpEngineConfig() {} + +// static +scoped_ptr<BlimpEngineConfig> BlimpEngineConfig::Create( + const base::CommandLine& cmd_line) { + const std::string client_token = GetClientToken(cmd_line); + if (!client_token.empty()) { + return make_scoped_ptr(new BlimpEngineConfig(client_token)); + } + return nullptr; +} + +// static +scoped_ptr<BlimpEngineConfig> BlimpEngineConfig::CreateForTest( + const std::string& client_token) { + return make_scoped_ptr(new BlimpEngineConfig(client_token)); +} + +const std::string& BlimpEngineConfig::client_token() const { + return client_token_; +} + +BlimpEngineConfig::BlimpEngineConfig(const std::string& client_token) + : client_token_(client_token) {} + +} // namespace engine +} // namespace blimp diff --git a/blimp/engine/browser/blimp_engine_config.h b/blimp/engine/browser/blimp_engine_config.h new file mode 100644 index 0000000..0c46b9f --- /dev/null +++ b/blimp/engine/browser/blimp_engine_config.h @@ -0,0 +1,58 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BLIMP_ENGINE_BROWSER_BLIMP_ENGINE_CONFIG_H_ +#define BLIMP_ENGINE_BROWSER_BLIMP_ENGINE_CONFIG_H_ + +#include <string> + +#include "base/macros.h" +#include "base/memory/scoped_ptr.h" + +namespace base { +class CommandLine; +} // namespace base + +namespace blimp { +namespace engine { + +// Class to hold all of the configuration bits necessary for the Blimp engine. +// +// The BlimpEngineConfig class is initialized from parameters provided on the +// command line. For the switches to pass verification: +// * A client token filepath must be provided and the file must have a +// non-empty token. +// +// The BlimpEngineConfig object is intended to live as long as the engine is +// running. It should also be one of the first things to be set up. +class BlimpEngineConfig { + public: + ~BlimpEngineConfig(); + + // Attempts to create a BlimpEngineConfig based on the parameters in the + // specified CommandLine. This validates all of the command line switches + // and parses all files specified. Returns a non-null scoped_ptr on success. + static scoped_ptr<BlimpEngineConfig> Create( + const base::CommandLine& cmd_line); + + // Creates a BlimpEngineConfig based on individual components. Should only + // be used for testing. + static scoped_ptr<BlimpEngineConfig> CreateForTest( + const std::string& client_token); + + // Returns the client token. + const std::string& client_token() const; + + private: + explicit BlimpEngineConfig(const std::string& client_token); + + const std::string client_token_; + + DISALLOW_COPY_AND_ASSIGN(BlimpEngineConfig); +}; + +} // namespace engine +} // namespace blimp + +#endif // BLIMP_ENGINE_BROWSER_BLIMP_ENGINE_CONFIG_H_ diff --git a/blimp/engine/browser/blimp_engine_config_unittest.cc b/blimp/engine/browser/blimp_engine_config_unittest.cc new file mode 100644 index 0000000..2268a56 --- /dev/null +++ b/blimp/engine/browser/blimp_engine_config_unittest.cc @@ -0,0 +1,86 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "blimp/engine/browser/blimp_engine_config.h" + +#include <string> +#include <vector> + +#include "base/command_line.h" +#include "base/files/file_util.h" +#include "base/files/scoped_temp_dir.h" +#include "base/memory/scoped_ptr.h" +#include "base/strings/stringprintf.h" +#include "blimp/engine/browser/switches.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace blimp { +namespace engine { +namespace { + +// Reference client token. +static const char kTestClientToken[] = "Reference client token"; + +class BlimpEngineConfigTest : public testing::Test { + protected: + void SetUp() override { + // Create a temporary directory and populate it with all of the switches + // If a test requires a switch's file to be missing, call + // RemoveFileForSwitch(). + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); + CreateFileForSwitch(kClientTokenPath, kTestClientToken); + } + + // Creates a file in the temp directory for a given filepath switch. + void CreateFileForSwitch(const std::string& filepath_switch, + const std::string& data) const { + ASSERT_TRUE(base::WriteFile(GetFilepathForSwitch(filepath_switch), + data.c_str(), data.size())); + } + + // Removes the associated file for a given filepath switch. + void RemoveFileForSwitch(const std::string& filepath_switch) const { + base::DeleteFile(GetFilepathForSwitch(filepath_switch), false); + } + + // Creates and returns a CommandLine object with specified filepath switches. + base::CommandLine CreateCommandLine( + const std::vector<std::string>& filepath_switches) { + base::CommandLine::StringVector cmd_vec = {"dummy_program"}; + for (const std::string& filepath_switch : filepath_switches) { + cmd_vec.push_back(base::StringPrintf( + "--%s=%s", filepath_switch.c_str(), + GetFilepathForSwitch(filepath_switch).AsUTF8Unsafe().c_str())); + } + return base::CommandLine(cmd_vec); + } + + base::FilePath GetFilepathForSwitch( + const std::string& filepath_switch) const { + return temp_dir_.path().Append(filepath_switch); + } + + const std::vector<std::string> all_filepath_switches_ = {kClientTokenPath}; + + base::ScopedTempDir temp_dir_; +}; + +TEST_F(BlimpEngineConfigTest, ClientTokenCorrect) { + auto cmd_line = CreateCommandLine(all_filepath_switches_); + auto engine_config = BlimpEngineConfig::Create(cmd_line); + EXPECT_NE(nullptr, engine_config); + EXPECT_EQ(kTestClientToken, engine_config->client_token()); +} + +TEST_F(BlimpEngineConfigTest, ClientTokenEmpty) { + RemoveFileForSwitch(kClientTokenPath); + CreateFileForSwitch(kClientTokenPath, " "); + auto cmd_line = CreateCommandLine(all_filepath_switches_); + auto engine_config = BlimpEngineConfig::Create(cmd_line); + EXPECT_EQ(nullptr, engine_config); +} + +} // namespace +} // namespace engine +} // namespace blimp diff --git a/blimp/engine/browser/switches.cc b/blimp/engine/browser/switches.cc new file mode 100644 index 0000000..3aca1f7 --- /dev/null +++ b/blimp/engine/browser/switches.cc @@ -0,0 +1,13 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "blimp/engine/browser/switches.h" + +namespace blimp { +namespace engine { + +const char kClientTokenPath[] = "blimp-client-token-path"; + +} // namespace engine +} // namespace blimp diff --git a/blimp/engine/browser/switches.h b/blimp/engine/browser/switches.h new file mode 100644 index 0000000..07096ff --- /dev/null +++ b/blimp/engine/browser/switches.h @@ -0,0 +1,17 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BLIMP_ENGINE_BROWSER_SWITCHES_H_ +#define BLIMP_ENGINE_BROWSER_SWITCHES_H_ + +namespace blimp { +namespace engine { + +// Path to the client token/shared secret between the engine and the client. +extern const char kClientTokenPath[]; + +} // namespace engine +} // namespace blimp + +#endif // BLIMP_ENGINE_BROWSER_SWITCHES_H_ |