1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
// Copyright (c) 2013 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 <string>
#include "base/base64url.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/memory/ref_counted.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/policy/profile_policy_connector_factory.h"
#include "chrome/browser/policy/test/local_policy_test_server.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_paths.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/cloud/cloud_policy_client.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
#include "components/policy/core/common/cloud/policy_builder.h"
#include "components/policy/core/common/policy_service.h"
#include "components/policy/core/common/policy_switches.h"
#include "components/policy/core/common/policy_test_utils.h"
#include "components/policy/proto/chrome_extension_policy.pb.h"
#include "components/policy/proto/cloud_policy.pb.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "extensions/common/extension.h"
#include "extensions/test/extension_test_message_listener.h"
#include "net/url_request/url_request_context_getter.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
#include "chrome/browser/chromeos/policy/user_policy_manager_factory_chromeos.h"
#include "chromeos/chromeos_switches.h"
#else
#include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
#include "components/signin/core/browser/signin_manager.h"
#endif
using testing::InvokeWithoutArgs;
using testing::Mock;
using testing::Return;
using testing::_;
namespace em = enterprise_management;
namespace policy {
namespace {
const char kDMToken[] = "dmtoken";
const char kDeviceID[] = "deviceid";
const char kTestExtension[] = "kjmkgkdkpedkejedfhmfcenooemhbpbo";
const base::FilePath::CharType kTestExtensionPath[] =
FILE_PATH_LITERAL("extensions/managed_extension");
const char kTestPolicy[] =
"{"
" \"Name\": {"
" \"Value\": \"disable_all_the_things\""
" }"
"}";
const char kTestExtension2[] = "behllobkkfkfnphdnhnkndlbkcpglgmj";
const base::FilePath::CharType kTestExtension2Path[] =
FILE_PATH_LITERAL("extensions/managed_extension2");
const char kTestPolicyJSON[] = "{\"Name\":\"disable_all_the_things\"}";
const char kTestPolicy2[] =
"{"
" \"Another\": {"
" \"Value\": \"turn_it_off\""
" }"
"}";
const char kTestPolicy2JSON[] = "{\"Another\":\"turn_it_off\"}";
} // namespace
class ComponentCloudPolicyTest : public ExtensionBrowserTest {
protected:
ComponentCloudPolicyTest() {}
~ComponentCloudPolicyTest() override {}
void SetUpCommandLine(base::CommandLine* command_line) override {
ExtensionBrowserTest::SetUpCommandLine(command_line);
#if defined(OS_CHROMEOS)
// ExtensionBrowserTest sets the login users to a non-managed value;
// replace it. This is the default username sent in policy blobs from the
// testserver.
command_line->AppendSwitchASCII(::chromeos::switches::kLoginUser,
PolicyBuilder::kFakeUsername);
#endif
}
void SetUpInProcessBrowserTestFixture() override {
test_server_.RegisterClient(kDMToken, kDeviceID);
EXPECT_TRUE(test_server_.UpdatePolicyData(
dm_protocol::kChromeExtensionPolicyType, kTestExtension, kTestPolicy));
ASSERT_TRUE(test_server_.Start());
std::string url = test_server_.GetServiceURL().spec();
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
command_line->AppendSwitchASCII(switches::kDeviceManagementUrl, url);
ExtensionBrowserTest::SetUpInProcessBrowserTestFixture();
}
void SetUpOnMainThread() override {
ExtensionBrowserTest::SetUpOnMainThread();
ASSERT_TRUE(PolicyServiceIsEmpty(g_browser_process->policy_service()))
<< "Pre-existing policies in this machine will make this test fail.";
// Install the initial extension.
ExtensionTestMessageListener ready_listener("ready", false);
event_listener_.reset(new ExtensionTestMessageListener("event", true));
extension_ = LoadExtension(kTestExtensionPath);
ASSERT_TRUE(extension_.get());
ASSERT_EQ(kTestExtension, extension_->id());
EXPECT_TRUE(ready_listener.WaitUntilSatisfied());
// And start with a signed-in user.
SignInAndRegister();
// The extension will receive an update event.
EXPECT_TRUE(event_listener_->WaitUntilSatisfied());
ExtensionBrowserTest::SetUpOnMainThread();
}
void TearDownOnMainThread() override {
event_listener_.reset();
ExtensionBrowserTest::TearDownOnMainThread();
}
scoped_refptr<const extensions::Extension> LoadExtension(
const base::FilePath::CharType* path) {
base::FilePath full_path;
if (!PathService::Get(chrome::DIR_TEST_DATA, &full_path)) {
ADD_FAILURE();
return NULL;
}
scoped_refptr<const extensions::Extension> extension(
ExtensionBrowserTest::LoadExtension(full_path.Append(path)));
if (!extension.get()) {
ADD_FAILURE();
return NULL;
}
return extension;
}
void SignInAndRegister() {
BrowserPolicyConnector* connector =
g_browser_process->browser_policy_connector();
connector->ScheduleServiceInitialization(0);
#if defined(OS_CHROMEOS)
UserCloudPolicyManagerChromeOS* policy_manager =
UserPolicyManagerFactoryChromeOS::GetCloudPolicyManagerForProfile(
browser()->profile());
ASSERT_TRUE(policy_manager);
#else
// Mock a signed-in user. This is used by the UserCloudPolicyStore to pass
// the username to the UserCloudPolicyValidator.
SigninManager* signin_manager =
SigninManagerFactory::GetForProfile(browser()->profile());
ASSERT_TRUE(signin_manager);
signin_manager->SetAuthenticatedAccountInfo("12345",
PolicyBuilder::kFakeUsername);
UserCloudPolicyManager* policy_manager =
UserCloudPolicyManagerFactory::GetForBrowserContext(
browser()->profile());
ASSERT_TRUE(policy_manager);
policy_manager->SetSigninUsername(PolicyBuilder::kFakeUsername);
policy_manager->Connect(g_browser_process->local_state(),
g_browser_process->system_request_context(),
UserCloudPolicyManager::CreateCloudPolicyClient(
connector->device_management_service(),
g_browser_process->system_request_context()));
#endif // defined(OS_CHROMEOS)
// Register the cloud policy client.
client_ = policy_manager->core()->client();
ASSERT_TRUE(client_);
base::RunLoop run_loop;
MockCloudPolicyClientObserver observer;
EXPECT_CALL(observer, OnRegistrationStateChanged(_))
.WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
client_->AddObserver(&observer);
client_->SetupRegistration(kDMToken, kDeviceID);
run_loop.Run();
Mock::VerifyAndClearExpectations(&observer);
client_->RemoveObserver(&observer);
}
#if !defined(OS_CHROMEOS)
void SignOut() {
SigninManager* signin_manager =
SigninManagerFactory::GetForProfile(browser()->profile());
ASSERT_TRUE(signin_manager);
signin_manager->SignOut(signin_metrics::SIGNOUT_TEST,
signin_metrics::SignoutDelete::IGNORE_METRIC);
}
#endif
void RefreshPolicies() {
ProfilePolicyConnector* profile_connector =
ProfilePolicyConnectorFactory::GetForBrowserContext(
browser()->profile());
PolicyService* policy_service = profile_connector->policy_service();
base::RunLoop run_loop;
policy_service->RefreshPolicies(run_loop.QuitClosure());
run_loop.Run();
}
LocalPolicyTestServer test_server_;
scoped_refptr<const extensions::Extension> extension_;
std::unique_ptr<ExtensionTestMessageListener> event_listener_;
CloudPolicyClient* client_ = nullptr;
};
IN_PROC_BROWSER_TEST_F(ComponentCloudPolicyTest, FetchExtensionPolicy) {
// Read the initial policy.
ExtensionTestMessageListener policy_listener(kTestPolicyJSON, false);
event_listener_->Reply("get-policy-Name");
EXPECT_TRUE(policy_listener.WaitUntilSatisfied());
}
IN_PROC_BROWSER_TEST_F(ComponentCloudPolicyTest, UpdateExtensionPolicy) {
// Read the initial policy.
ExtensionTestMessageListener policy_listener(kTestPolicyJSON, true);
event_listener_->Reply("get-policy-Name");
EXPECT_TRUE(policy_listener.WaitUntilSatisfied());
// Update the policy at the server and reload policy.
event_listener_.reset(new ExtensionTestMessageListener("event", true));
policy_listener.Reply("idle");
EXPECT_TRUE(test_server_.UpdatePolicyData(
dm_protocol::kChromeExtensionPolicyType, kTestExtension, kTestPolicy2));
RefreshPolicies();
// Check that the update event was received, and verify the new policy
// values.
EXPECT_TRUE(event_listener_->WaitUntilSatisfied());
// This policy was removed.
ExtensionTestMessageListener policy_listener1("{}", true);
event_listener_->Reply("get-policy-Name");
EXPECT_TRUE(policy_listener1.WaitUntilSatisfied());
ExtensionTestMessageListener policy_listener2(kTestPolicy2JSON, false);
policy_listener1.Reply("get-policy-Another");
EXPECT_TRUE(policy_listener2.WaitUntilSatisfied());
}
IN_PROC_BROWSER_TEST_F(ComponentCloudPolicyTest, InstallNewExtension) {
event_listener_->Reply("idle");
event_listener_.reset();
EXPECT_TRUE(test_server_.UpdatePolicyData(
dm_protocol::kChromeExtensionPolicyType, kTestExtension2, kTestPolicy2));
// Installing a new extension doesn't trigger another policy fetch because
// the server always sends down the list of all extensions that have policy.
// Fetch now that the configuration has been updated and before installing
// the extension.
RefreshPolicies();
ExtensionTestMessageListener result_listener("ok", false);
result_listener.set_failure_message("fail");
scoped_refptr<const extensions::Extension> extension2 =
LoadExtension(kTestExtension2Path);
ASSERT_TRUE(extension2.get());
ASSERT_EQ(kTestExtension2, extension2->id());
// This extension only sends the 'policy' signal once it receives the policy,
// and after verifying it has the expected value. Otherwise it sends 'fail'.
EXPECT_TRUE(result_listener.WaitUntilSatisfied());
}
// Signing out on Chrome OS is a different process from signing out on the
// Desktop platforms. On Chrome OS the session is ended, and the user goes back
// to the sign-in screen; the Profile data is not affected. On the Desktop the
// session goes on though, and all the signed-in services are disconnected;
// in particular, the policy caches are dropped if the user signs out.
// This test verifies that when the user signs out then any existing component
// policy caches are dropped, and that it's still possible to sign back in and
// get policy for components working again.
#if !defined(OS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(ComponentCloudPolicyTest, SignOutAndBackIn) {
// Read the initial policy.
ExtensionTestMessageListener initial_policy_listener(kTestPolicyJSON, true);
event_listener_->Reply("get-policy-Name");
EXPECT_TRUE(initial_policy_listener.WaitUntilSatisfied());
// Verify that the policy cache exists.
std::string cache_key;
base::Base64UrlEncode(
"extension-policy", base::Base64UrlEncodePolicy::INCLUDE_PADDING,
&cache_key);
std::string cache_subkey;
base::Base64UrlEncode(
kTestExtension, base::Base64UrlEncodePolicy::INCLUDE_PADDING,
&cache_subkey);
base::FilePath cache_path = browser()->profile()->GetPath()
.Append(FILE_PATH_LITERAL("Policy"))
.Append(FILE_PATH_LITERAL("Components"))
.AppendASCII(cache_key)
.AppendASCII(cache_subkey);
EXPECT_TRUE(base::PathExists(cache_path));
// Now sign-out. The policy cache should be removed, and the extension should
// get an empty policy update.
ExtensionTestMessageListener event_listener("event", true);
initial_policy_listener.Reply("idle");
SignOut();
EXPECT_TRUE(event_listener.WaitUntilSatisfied());
// The extension got an update event; verify that the policy was empty.
ExtensionTestMessageListener signout_policy_listener("{}", false);
event_listener.Reply("get-policy-Name");
EXPECT_TRUE(signout_policy_listener.WaitUntilSatisfied());
// Verify that the cache is gone.
EXPECT_FALSE(base::PathExists(cache_path));
// Verify that the policy is fetched again if the user signs back in.
ExtensionTestMessageListener event_listener2("event", true);
SignInAndRegister();
EXPECT_TRUE(event_listener2.WaitUntilSatisfied());
// The extension got updated policy; verify it.
ExtensionTestMessageListener signin_policy_listener(kTestPolicyJSON, false);
event_listener2.Reply("get-policy-Name");
EXPECT_TRUE(signin_policy_listener.WaitUntilSatisfied());
// And the cache is back.
EXPECT_TRUE(base::PathExists(cache_path));
}
#endif
// Test of the component cloud policy when the policy test server is configured
// to perform the signing key rotation for each policy fetch.
class KeyRotationComponentCloudPolicyTest : public ComponentCloudPolicyTest {
protected:
void SetUpInProcessBrowserTestFixture() override {
test_server_.EnableAutomaticRotationOfSigningKeys();
ComponentCloudPolicyTest::SetUpInProcessBrowserTestFixture();
}
int GetFetchedPolicyPublicKeyVersion(const std::string& extension_id) {
const em::PolicyFetchResponse* fetched_policy = client_->GetPolicyFor(
dm_protocol::kChromeExtensionPolicyType, extension_id);
if (!fetched_policy || !fetched_policy->has_policy_data())
return -1;
em::PolicyData policy_data;
if (!policy_data.ParseFromString(fetched_policy->policy_data()) ||
!policy_data.has_public_key_version())
return -1;
return policy_data.public_key_version();
}
};
IN_PROC_BROWSER_TEST_F(KeyRotationComponentCloudPolicyTest, Basic) {
// Read the initial policy.
ExtensionTestMessageListener policy_listener(kTestPolicyJSON, true);
event_listener_->Reply("get-policy-Name");
EXPECT_TRUE(policy_listener.WaitUntilSatisfied());
const int public_key_version =
GetFetchedPolicyPublicKeyVersion(kTestExtension);
EXPECT_NE(-1, public_key_version);
// Update the policy at the server and reload the policy, causing also the key
// rotation to be performed by the policy test server.
event_listener_.reset(new ExtensionTestMessageListener("event", true));
policy_listener.Reply("idle");
EXPECT_TRUE(test_server_.UpdatePolicyData(
dm_protocol::kChromeExtensionPolicyType, kTestExtension, kTestPolicy2));
RefreshPolicies();
// Check that the update event was received, and verify that the policy has
// the new value and that the key rotation happened.
EXPECT_TRUE(event_listener_->WaitUntilSatisfied());
const int new_public_key_version =
GetFetchedPolicyPublicKeyVersion(kTestExtension);
EXPECT_LT(public_key_version, new_public_key_version);
ExtensionTestMessageListener policy_listener1("{}", true);
event_listener_->Reply("get-policy-Name");
EXPECT_TRUE(policy_listener1.WaitUntilSatisfied());
ExtensionTestMessageListener policy_listener2(kTestPolicy2JSON, false);
policy_listener1.Reply("get-policy-Another");
EXPECT_TRUE(policy_listener2.WaitUntilSatisfied());
}
} // namespace policy
|