buffet: Add D-Bus API to change GCD service settings Added function UpdateServiceConfig that can be used to switch servers for testing. Function modifies client_id, client_secret, api_key, oauth_url and service_url. BUG=brillo:1058 TEST='FEATURES=test emerge-gizmo buffet' Change-Id: Ia8f8ba3ad57372ecedbff2819ce1991ba391a41d Reviewed-on: https://chromium-review.googlesource.com/271387 Tested-by: Vitaly Buka <vitalybuka@chromium.org> Reviewed-by: Alex Vakulenko <avakulenko@chromium.org> Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/buffet/buffet_config.cc b/buffet/buffet_config.cc index 4d97098..a8a753f 100644 --- a/buffet/buffet_config.cc +++ b/buffet/buffet_config.cc
@@ -147,47 +147,61 @@ if (!value || !value->GetAsDictionary(&dict)) return; - std::string name; - if (dict->GetString(config_keys::kName, &name)) - set_name(name); + std::string tmp; + bool tmp_bool{false}; - std::string description; - if (dict->GetString(config_keys::kDescription, &description)) - set_description(description); + if (dict->GetString(config_keys::kClientId, &tmp)) + set_client_id(tmp); - std::string location; - if (dict->GetString(config_keys::kLocation, &location)) - set_location(location); + if (dict->GetString(config_keys::kClientSecret, &tmp)) + set_client_secret(tmp); - std::string access_role; - if (dict->GetString(config_keys::kLocalAnonymousAccessRole, &access_role)) - set_local_anonymous_access_role(access_role); + if (dict->GetString(config_keys::kApiKey, &tmp)) + set_api_key(tmp); - bool discovery_enabled{false}; - if (dict->GetBoolean(config_keys::kLocalDiscoveryEnabled, &discovery_enabled)) - set_local_discovery_enabled(discovery_enabled); + if (dict->GetString(config_keys::kOAuthURL, &tmp)) + set_oauth_url(tmp); - bool pairing_enabled{false}; - if (dict->GetBoolean(config_keys::kLocalPairingEnabled, &pairing_enabled)) - set_local_pairing_enabled(pairing_enabled); + if (dict->GetString(config_keys::kServiceURL, &tmp)) + set_service_url(tmp); - std::string token; - if (dict->GetString(config_keys::kRefreshToken, &token)) - set_refresh_token(token); + if (dict->GetString(config_keys::kName, &tmp)) + set_name(tmp); - std::string account; - if (dict->GetString(config_keys::kRobotAccount, &account)) - set_robot_account(account); + if (dict->GetString(config_keys::kDescription, &tmp)) + set_description(tmp); - std::string device_id; - if (dict->GetString(config_keys::kDeviceId, &device_id)) - set_device_id(device_id); + if (dict->GetString(config_keys::kLocation, &tmp)) + set_location(tmp); + + if (dict->GetString(config_keys::kLocalAnonymousAccessRole, &tmp)) + set_local_anonymous_access_role(tmp); + + if (dict->GetBoolean(config_keys::kLocalDiscoveryEnabled, &tmp_bool)) + set_local_discovery_enabled(tmp_bool); + + if (dict->GetBoolean(config_keys::kLocalPairingEnabled, &tmp_bool)) + set_local_pairing_enabled(tmp_bool); + + if (dict->GetString(config_keys::kRefreshToken, &tmp)) + set_refresh_token(tmp); + + if (dict->GetString(config_keys::kRobotAccount, &tmp)) + set_robot_account(tmp); + + if (dict->GetString(config_keys::kDeviceId, &tmp)) + set_device_id(tmp); } bool BuffetConfig::Save() { if (!storage_) return false; base::DictionaryValue dict; + dict.SetString(config_keys::kClientId, client_id_); + dict.SetString(config_keys::kClientSecret, client_secret_); + dict.SetString(config_keys::kApiKey, api_key_); + dict.SetString(config_keys::kOAuthURL, oauth_url_); + dict.SetString(config_keys::kServiceURL, service_url_); dict.SetString(config_keys::kRefreshToken, refresh_token_); dict.SetString(config_keys::kDeviceId, device_id_); dict.SetString(config_keys::kRobotAccount, robot_account_);
diff --git a/buffet/buffet_config.h b/buffet/buffet_config.h index 0470755..6b73911 100644 --- a/buffet/buffet_config.h +++ b/buffet/buffet_config.h
@@ -47,6 +47,15 @@ ~Transaction(); + void set_client_id(const std::string& id) { config_->client_id_ = id; } + void set_client_secret(const std::string& secret) { + config_->client_secret_ = secret; + } + void set_api_key(const std::string& key) { config_->api_key_ = key; } + void set_oauth_url(const std::string& url) { config_->oauth_url_ = url; } + void set_service_url(const std::string& url) { + config_->service_url_ = url; + } bool set_name(const std::string& name); void set_description(const std::string& description) { config_->description_ = description;
diff --git a/buffet/buffet_config_unittest.cc b/buffet/buffet_config_unittest.cc index cec8bbe..e1b4b30 100644 --- a/buffet/buffet_config_unittest.cc +++ b/buffet/buffet_config_unittest.cc
@@ -122,30 +122,40 @@ change.Commit(); auto expected = R"({ - 'description': 'conf_description', - 'device_id': '', - 'local_anonymous_access_role': 'user', - 'local_discovery_enabled': false, - 'local_pairing_enabled': false, - 'location': 'conf_location', - 'name': 'conf_name', - 'refresh_token': '', - 'robot_account': '' + 'api_key': 'conf_api_key', + 'client_id': 'conf_client_id', + 'client_secret': 'conf_client_secret', + 'description': 'conf_description', + 'device_id': '', + 'local_anonymous_access_role': 'user', + 'local_discovery_enabled': false, + 'local_pairing_enabled': false, + 'location': 'conf_location', + 'name': 'conf_name', + 'oauth_url': 'conf_oauth_url', + 'refresh_token': '', + 'robot_account': '', + 'service_url': 'conf_service_url' })"; EXPECT_JSON_EQ(expected, *storage_->Load()); } TEST_F(BuffetConfigTest, LoadState) { auto state = R"({ - 'description': 'state_description', - 'device_id': 'state_device_id', - 'local_anonymous_access_role': 'user', - 'local_discovery_enabled': false, - 'local_pairing_enabled': false, - 'location': 'state_location', - 'name': 'state_name', - 'refresh_token': 'state_refresh_token', - 'robot_account': 'state_robot_account' + 'api_key': 'state_api_key', + 'client_id': 'state_client_id', + 'client_secret': 'state_client_secret', + 'description': 'state_description', + 'device_id': 'state_device_id', + 'local_anonymous_access_role': 'user', + 'local_discovery_enabled': false, + 'local_pairing_enabled': false, + 'location': 'state_location', + 'name': 'state_name', + 'oauth_url': 'state_oauth_url', + 'refresh_token': 'state_refresh_token', + 'robot_account': 'state_robot_account', + 'service_url': 'state_service_url' })"; storage_->Save(*buffet::unittests::CreateDictionaryValue(state)); @@ -156,11 +166,11 @@ // Clear storage. storage_->Save(base::DictionaryValue()); - EXPECT_EQ(default_.client_id(), config_->client_id()); - EXPECT_EQ(default_.client_secret(), config_->client_secret()); - EXPECT_EQ(default_.api_key(), config_->api_key()); - EXPECT_EQ(default_.oauth_url(), config_->oauth_url()); - EXPECT_EQ(default_.service_url(), config_->service_url()); + EXPECT_EQ("state_client_id", config_->client_id()); + EXPECT_EQ("state_client_secret", config_->client_secret()); + EXPECT_EQ("state_api_key", config_->api_key()); + EXPECT_EQ("state_oauth_url", config_->oauth_url()); + EXPECT_EQ("state_service_url", config_->service_url()); EXPECT_EQ(default_.oem_name(), config_->oem_name()); EXPECT_EQ(default_.model_name(), config_->model_name()); EXPECT_EQ(default_.model_id(), config_->model_id()); @@ -189,6 +199,21 @@ TEST_F(BuffetConfigTest, Setters) { BuffetConfig::Transaction change{config_.get()}; + change.set_client_id("set_client_id"); + EXPECT_EQ("set_client_id", config_->client_id()); + + change.set_client_secret("set_client_secret"); + EXPECT_EQ("set_client_secret", config_->client_secret()); + + change.set_api_key("set_api_key"); + EXPECT_EQ("set_api_key", config_->api_key()); + + change.set_oauth_url("set_oauth_url"); + EXPECT_EQ("set_oauth_url", config_->oauth_url()); + + change.set_service_url("set_service_url"); + EXPECT_EQ("set_service_url", config_->service_url()); + change.set_name("set_name"); EXPECT_EQ("set_name", config_->name()); @@ -232,6 +257,9 @@ change.Commit(); auto expected = R"({ + 'api_key': 'set_api_key', + 'client_id': 'set_client_id', + 'client_secret': 'set_client_secret', 'description': 'set_description', 'device_id': 'set_id', 'local_anonymous_access_role': 'user', @@ -239,8 +267,10 @@ 'local_pairing_enabled': true, 'location': 'set_location', 'name': 'set_name', + 'oauth_url': 'set_oauth_url', 'refresh_token': 'set_token', - 'robot_account': 'set_account' + 'robot_account': 'set_account', + 'service_url': 'set_service_url' })"; EXPECT_JSON_EQ(expected, *storage_->Load()); }
diff --git a/buffet/dbus_bindings/org.chromium.Buffet.Manager.xml b/buffet/dbus_bindings/org.chromium.Buffet.Manager.xml index fefcfa0..67c265d 100644 --- a/buffet/dbus_bindings/org.chromium.Buffet.Manager.xml +++ b/buffet/dbus_bindings/org.chromium.Buffet.Manager.xml
@@ -27,6 +27,14 @@ <arg name="location" type="s" direction="in"/> <annotation name="org.chromium.DBus.Method.Kind" value="normal"/> </method> + <method name="UpdateServiceConfig"> + <arg name="client_id" type="s" direction="in"/> + <arg name="client_secret" type="s" direction="in"/> + <arg name="api_key" type="s" direction="in"/> + <arg name="oauth_url" type="s" direction="in"/> + <arg name="service_url" type="s" direction="in"/> + <annotation name="org.chromium.DBus.Method.Kind" value="normal"/> + </method> <method name="UpdateState"> <arg name="property_set" type="a{sv}" direction="in"/> <annotation name="org.chromium.DBus.Method.Kind" value="async"/>
diff --git a/buffet/device_registration_info.cc b/buffet/device_registration_info.cc index 3b176c3..7d8c608 100644 --- a/buffet/device_registration_info.cc +++ b/buffet/device_registration_info.cc
@@ -716,6 +716,7 @@ } change.set_description(description); change.set_location(location); + change.Commit(); if (HaveRegistrationCredentials(nullptr)) { UpdateDeviceResource(base::Bind(&base::DoNothing), @@ -725,6 +726,28 @@ return true; } +bool DeviceRegistrationInfo::UpdateServiceConfig( + const std::string& client_id, + const std::string& client_secret, + const std::string& api_key, + const std::string& oauth_url, + const std::string& service_url, + chromeos::ErrorPtr* error) { + if (HaveRegistrationCredentials(nullptr)) { + chromeos::Error::AddTo(error, FROM_HERE, kErrorDomainBuffet, + "already_registered", + "Unable to change config for registered device"); + return false; + } + BuffetConfig::Transaction change{config_.get()}; + change.set_client_id(client_id); + change.set_client_secret(client_secret); + change.set_api_key(api_key); + change.set_oauth_url(oauth_url); + change.set_service_url(service_url); + return true; +} + void DeviceRegistrationInfo::UpdateCommand( const std::string& command_id, const base::DictionaryValue& command_patch,
diff --git a/buffet/device_registration_info.h b/buffet/device_registration_info.h index 29c73d2..c9ca7b4 100644 --- a/buffet/device_registration_info.h +++ b/buffet/device_registration_info.h
@@ -126,6 +126,14 @@ const std::string& location, chromeos::ErrorPtr* error); + // Updates GCD service configuration. Usually for testing. + bool UpdateServiceConfig(const std::string& client_id, + const std::string& client_secret, + const std::string& api_key, + const std::string& oauth_url, + const std::string& service_url, + chromeos::ErrorPtr* error); + const BuffetConfig& GetConfig() const { return *config_; } base::WeakPtr<DeviceRegistrationInfo> AsWeakPtr() {
diff --git a/buffet/manager.cc b/buffet/manager.cc index 2002eb2..d21ec8b 100644 --- a/buffet/manager.cc +++ b/buffet/manager.cc
@@ -243,6 +243,16 @@ error); } +bool Manager::UpdateServiceConfig(chromeos::ErrorPtr* error, + const std::string& client_id, + const std::string& client_secret, + const std::string& api_key, + const std::string& oauth_url, + const std::string& service_url) { + return device_info_->UpdateServiceConfig(client_id, client_secret, api_key, + oauth_url, service_url, error); +} + void Manager::OnCommandDefsChanged() { chromeos::ErrorPtr error; // Limit only to commands that are visible to the local clients.
diff --git a/buffet/manager.h b/buffet/manager.h index 3961b68..479ac9b 100644 --- a/buffet/manager.h +++ b/buffet/manager.h
@@ -67,6 +67,12 @@ const std::string& in_name, const std::string& in_description, const std::string& in_location) override; + bool UpdateServiceConfig(chromeos::ErrorPtr* error, + const std::string& client_id, + const std::string& client_secret, + const std::string& api_key, + const std::string& oauth_url, + const std::string& service_url) override; void UpdateState(DBusMethodResponse<> response, const chromeos::VariantDictionary& property_set) override; bool GetState(chromeos::ErrorPtr* error, std::string* state) override;