Vitaly Buka | cad20f0 | 2015-10-16 17:27:15 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 14 | |
| 15 | #include "buffet/buffet_config.h" |
| 16 | |
Vitaly Buka | becd461 | 2015-08-16 23:31:55 -0700 | [diff] [blame] | 17 | #include <map> |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 18 | #include <set> |
| 19 | |
| 20 | #include <base/files/file_util.h> |
| 21 | #include <base/files/important_file_writer.h> |
| 22 | #include <base/logging.h> |
| 23 | #include <base/strings/string_number_conversions.h> |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 24 | #include <brillo/errors/error.h> |
| 25 | #include <brillo/errors/error_codes.h> |
Alex Vakulenko | 9d44802 | 2015-10-30 16:57:43 -0700 | [diff] [blame^] | 26 | #include <brillo/osrelease_reader.h> |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 27 | #include <brillo/strings/string_utils.h> |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 28 | #include <weave/enum_to_string.h> |
| 29 | |
| 30 | namespace buffet { |
| 31 | |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | const char kErrorDomain[] = "buffet"; |
| 35 | const char kFileReadError[] = "file_read_error"; |
Alex Vakulenko | 9d44802 | 2015-10-30 16:57:43 -0700 | [diff] [blame^] | 36 | const char kProductVersionKey[] = "product_version"; |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 37 | |
| 38 | class DefaultFileIO : public BuffetConfig::FileIO { |
| 39 | public: |
| 40 | bool ReadFile(const base::FilePath& path, std::string* content) override { |
| 41 | return base::ReadFileToString(path, content); |
| 42 | } |
| 43 | bool WriteFile(const base::FilePath& path, |
| 44 | const std::string& content) override { |
| 45 | return base::ImportantFileWriter::WriteFileAtomically(path, content); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | } // namespace |
| 50 | |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 51 | namespace config_keys { |
| 52 | |
| 53 | const char kClientId[] = "client_id"; |
| 54 | const char kClientSecret[] = "client_secret"; |
| 55 | const char kApiKey[] = "api_key"; |
| 56 | const char kOAuthURL[] = "oauth_url"; |
| 57 | const char kServiceURL[] = "service_url"; |
| 58 | const char kName[] = "name"; |
| 59 | const char kDescription[] = "description"; |
| 60 | const char kLocation[] = "location"; |
| 61 | const char kLocalAnonymousAccessRole[] = "local_anonymous_access_role"; |
| 62 | const char kLocalDiscoveryEnabled[] = "local_discovery_enabled"; |
| 63 | const char kLocalPairingEnabled[] = "local_pairing_enabled"; |
| 64 | const char kOemName[] = "oem_name"; |
| 65 | const char kModelName[] = "model_name"; |
| 66 | const char kModelId[] = "model_id"; |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 67 | const char kWifiAutoSetupEnabled[] = "wifi_auto_setup_enabled"; |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 68 | const char kEmbeddedCode[] = "embedded_code"; |
| 69 | const char kPairingModes[] = "pairing_modes"; |
| 70 | |
| 71 | } // namespace config_keys |
| 72 | |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 73 | BuffetConfig::BuffetConfig(const Options& options) |
| 74 | : options_(options), |
| 75 | default_encryptor_(Encryptor::CreateDefaultEncryptor()), |
| 76 | encryptor_(default_encryptor_.get()), |
| 77 | default_file_io_(new DefaultFileIO), |
| 78 | file_io_(default_file_io_.get()) {} |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 79 | |
| 80 | bool BuffetConfig::LoadDefaults(weave::Settings* settings) { |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 81 | // Keep this hardcoded default for sometime. This previously was set by |
| 82 | // libweave. It should be set by overlay's buffet.conf. |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 83 | settings->client_id = "58855907228.apps.googleusercontent.com"; |
| 84 | settings->client_secret = "eHSAREAHrIqPsHBxCE9zPPBi"; |
| 85 | settings->api_key = "AIzaSyDSq46gG-AxUnC3zoqD9COIPrjolFsMfMA"; |
| 86 | settings->name = "Developer device"; |
| 87 | settings->oem_name = "Chromium"; |
| 88 | settings->model_name = "Brillo"; |
| 89 | settings->model_id = "AAAAA"; |
| 90 | |
| 91 | if (!base::PathExists(options_.defaults)) |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 92 | return true; // Nothing to load. |
| 93 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 94 | brillo::KeyValueStore store; |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 95 | if (!store.Load(options_.defaults)) |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 96 | return false; |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 97 | bool result = LoadDefaults(store, settings); |
| 98 | settings->disable_security = options_.disable_security; |
| 99 | settings->test_privet_ssid = options_.test_privet_ssid; |
Vitaly Buka | becd461 | 2015-08-16 23:31:55 -0700 | [diff] [blame] | 100 | |
Alex Vakulenko | 2915a7b | 2015-10-07 17:04:00 -0700 | [diff] [blame] | 101 | if (!options_.client_id.empty()) |
| 102 | settings->client_id = options_.client_id; |
| 103 | if (!options_.client_secret.empty()) |
| 104 | settings->client_secret = options_.client_secret; |
| 105 | if (!options_.api_key.empty()) |
| 106 | settings->api_key = options_.api_key; |
| 107 | if (!options_.oauth_url.empty()) |
| 108 | settings->oauth_url = options_.oauth_url; |
| 109 | if (!options_.service_url.empty()) |
| 110 | settings->service_url = options_.service_url; |
Vitaly Buka | becd461 | 2015-08-16 23:31:55 -0700 | [diff] [blame] | 111 | |
Vitaly Buka | becd461 | 2015-08-16 23:31:55 -0700 | [diff] [blame] | 112 | return result; |
| 113 | } |
| 114 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 115 | bool BuffetConfig::LoadDefaults(const brillo::KeyValueStore& store, |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 116 | weave::Settings* settings) { |
| 117 | store.GetString(config_keys::kClientId, &settings->client_id); |
| 118 | store.GetString(config_keys::kClientSecret, &settings->client_secret); |
| 119 | store.GetString(config_keys::kApiKey, &settings->api_key); |
| 120 | store.GetString(config_keys::kOAuthURL, &settings->oauth_url); |
| 121 | store.GetString(config_keys::kServiceURL, &settings->service_url); |
| 122 | store.GetString(config_keys::kOemName, &settings->oem_name); |
| 123 | store.GetString(config_keys::kModelName, &settings->model_name); |
| 124 | store.GetString(config_keys::kModelId, &settings->model_id); |
Vitaly Buka | 7b9ebee | 2015-08-16 01:55:41 -0700 | [diff] [blame] | 125 | |
Alex Vakulenko | 9d44802 | 2015-10-30 16:57:43 -0700 | [diff] [blame^] | 126 | brillo::OsReleaseReader reader; |
| 127 | reader.Load(); |
| 128 | if (!reader.GetString(kProductVersionKey, &settings->firmware_version)) { |
| 129 | LOG(ERROR) << "Could not read '" << kProductVersionKey << "' from OS"; |
Vitaly Buka | 7b9ebee | 2015-08-16 01:55:41 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 132 | store.GetBoolean(config_keys::kWifiAutoSetupEnabled, |
| 133 | &settings->wifi_auto_setup_enabled); |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 134 | store.GetString(config_keys::kEmbeddedCode, &settings->embedded_code); |
| 135 | |
| 136 | std::string modes_str; |
| 137 | if (store.GetString(config_keys::kPairingModes, &modes_str)) { |
| 138 | std::set<weave::PairingType> pairing_modes; |
| 139 | for (const std::string& mode : |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 140 | brillo::string_utils::Split(modes_str, ",", true, true)) { |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 141 | weave::PairingType pairing_mode; |
| 142 | if (!StringToEnum(mode, &pairing_mode)) |
| 143 | return false; |
| 144 | pairing_modes.insert(pairing_mode); |
| 145 | } |
| 146 | settings->pairing_modes = std::move(pairing_modes); |
| 147 | } |
| 148 | |
| 149 | store.GetString(config_keys::kName, &settings->name); |
| 150 | store.GetString(config_keys::kDescription, &settings->description); |
| 151 | store.GetString(config_keys::kLocation, &settings->location); |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 152 | |
| 153 | std::string role_str; |
| 154 | if (store.GetString(config_keys::kLocalAnonymousAccessRole, &role_str)) { |
| 155 | if (!StringToEnum(role_str, &settings->local_anonymous_access_role)) |
| 156 | return false; |
| 157 | } |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 158 | store.GetBoolean(config_keys::kLocalDiscoveryEnabled, |
| 159 | &settings->local_discovery_enabled); |
| 160 | store.GetBoolean(config_keys::kLocalPairingEnabled, |
| 161 | &settings->local_pairing_enabled); |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | std::string BuffetConfig::LoadSettings() { |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 166 | std::string settings_blob; |
| 167 | if (!file_io_->ReadFile(options_.settings, &settings_blob)) { |
| 168 | LOG(WARNING) << "Failed to read settings, proceeding with empty settings."; |
| 169 | return std::string(); |
| 170 | } |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 171 | std::string json_string; |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 172 | if (!encryptor_->DecryptWithAuthentication(settings_blob, &json_string)) { |
| 173 | LOG(WARNING) |
| 174 | << "Failed to decrypt settings, proceeding with empty settings."; |
| 175 | SaveSettings(std::string()); |
| 176 | return std::string(); |
| 177 | } |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 178 | return json_string; |
| 179 | } |
| 180 | |
| 181 | void BuffetConfig::SaveSettings(const std::string& settings) { |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 182 | std::string encrypted_settings; |
| 183 | if (!encryptor_->EncryptWithAuthentication(settings, &encrypted_settings)) { |
| 184 | LOG(ERROR) << "Failed to encrypt settings, writing empty settings."; |
| 185 | encrypted_settings.clear(); |
| 186 | } |
| 187 | if (!file_io_->WriteFile(options_.settings, encrypted_settings)) { |
| 188 | LOG(ERROR) << "Failed to write settings."; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | bool BuffetConfig::LoadFile(const base::FilePath& file_path, |
| 193 | std::string* data, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 194 | brillo::ErrorPtr* error) { |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 195 | if (!file_io_->ReadFile(file_path, data)) { |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 196 | brillo::errors::system::AddSystemError(error, FROM_HERE, errno); |
| 197 | brillo::Error::AddToPrintf(error, FROM_HERE, kErrorDomain, kFileReadError, |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame] | 198 | "Failed to read file '%s'", |
| 199 | file_path.value().c_str()); |
| 200 | return false; |
| 201 | } |
| 202 | return true; |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | } // namespace buffet |