blob: 5d53442627780324fbc8d1c17fe72d27712ef8f6 [file] [log] [blame]
Vitaly Bukacad20f02015-10-16 17:27:15 -07001// 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 Buka1175a9b2015-08-15 10:42:17 -070014
15#include "buffet/buffet_config.h"
16
Vitaly Bukabecd4612015-08-16 23:31:55 -070017#include <map>
Vitaly Buka1175a9b2015-08-15 10:42:17 -070018#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 Vakulenko41705852015-10-13 10:12:06 -070024#include <brillo/errors/error.h>
25#include <brillo/errors/error_codes.h>
Alex Vakulenko9d448022015-10-30 16:57:43 -070026#include <brillo/osrelease_reader.h>
Alex Vakulenko41705852015-10-13 10:12:06 -070027#include <brillo/strings/string_utils.h>
Vitaly Buka1175a9b2015-08-15 10:42:17 -070028#include <weave/enum_to_string.h>
29
30namespace buffet {
31
Alex Vakulenkodf381642015-10-08 07:34:23 -070032namespace {
33
34const char kErrorDomain[] = "buffet";
35const char kFileReadError[] = "file_read_error";
Alex Vakulenko9d448022015-10-30 16:57:43 -070036const char kProductVersionKey[] = "product_version";
Alex Vakulenkodf381642015-10-08 07:34:23 -070037
38class 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 Buka1175a9b2015-08-15 10:42:17 -070051namespace config_keys {
52
53const char kClientId[] = "client_id";
54const char kClientSecret[] = "client_secret";
55const char kApiKey[] = "api_key";
56const char kOAuthURL[] = "oauth_url";
57const char kServiceURL[] = "service_url";
58const char kName[] = "name";
59const char kDescription[] = "description";
60const char kLocation[] = "location";
61const char kLocalAnonymousAccessRole[] = "local_anonymous_access_role";
62const char kLocalDiscoveryEnabled[] = "local_discovery_enabled";
63const char kLocalPairingEnabled[] = "local_pairing_enabled";
64const char kOemName[] = "oem_name";
65const char kModelName[] = "model_name";
66const char kModelId[] = "model_id";
Vitaly Buka1175a9b2015-08-15 10:42:17 -070067const char kWifiAutoSetupEnabled[] = "wifi_auto_setup_enabled";
Vitaly Buka1175a9b2015-08-15 10:42:17 -070068const char kEmbeddedCode[] = "embedded_code";
69const char kPairingModes[] = "pairing_modes";
70
71} // namespace config_keys
72
Alex Vakulenkodf381642015-10-08 07:34:23 -070073BuffetConfig::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 Buka1175a9b2015-08-15 10:42:17 -070079
80bool BuffetConfig::LoadDefaults(weave::Settings* settings) {
Alex Vakulenko2915a7b2015-10-07 17:04:00 -070081 // Keep this hardcoded default for sometime. This previously was set by
82 // libweave. It should be set by overlay's buffet.conf.
Alex Vakulenko0022b752015-10-02 11:09:59 -070083 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 Buka1175a9b2015-08-15 10:42:17 -070092 return true; // Nothing to load.
93
Alex Vakulenko41705852015-10-13 10:12:06 -070094 brillo::KeyValueStore store;
Alex Vakulenko0022b752015-10-02 11:09:59 -070095 if (!store.Load(options_.defaults))
Vitaly Buka1175a9b2015-08-15 10:42:17 -070096 return false;
Alex Vakulenko0022b752015-10-02 11:09:59 -070097 bool result = LoadDefaults(store, settings);
98 settings->disable_security = options_.disable_security;
99 settings->test_privet_ssid = options_.test_privet_ssid;
Vitaly Bukabecd4612015-08-16 23:31:55 -0700100
Alex Vakulenko2915a7b2015-10-07 17:04:00 -0700101 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 Bukabecd4612015-08-16 23:31:55 -0700111
Vitaly Bukabecd4612015-08-16 23:31:55 -0700112 return result;
113}
114
Alex Vakulenko41705852015-10-13 10:12:06 -0700115bool BuffetConfig::LoadDefaults(const brillo::KeyValueStore& store,
Vitaly Buka1175a9b2015-08-15 10:42:17 -0700116 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 Buka7b9ebee2015-08-16 01:55:41 -0700125
Alex Vakulenko9d448022015-10-30 16:57:43 -0700126 brillo::OsReleaseReader reader;
127 reader.Load();
128 if (!reader.GetString(kProductVersionKey, &settings->firmware_version)) {
129 LOG(ERROR) << "Could not read '" << kProductVersionKey << "' from OS";
Vitaly Buka7b9ebee2015-08-16 01:55:41 -0700130 }
131
Vitaly Buka1175a9b2015-08-15 10:42:17 -0700132 store.GetBoolean(config_keys::kWifiAutoSetupEnabled,
133 &settings->wifi_auto_setup_enabled);
Vitaly Buka1175a9b2015-08-15 10:42:17 -0700134 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 Vakulenko41705852015-10-13 10:12:06 -0700140 brillo::string_utils::Split(modes_str, ",", true, true)) {
Vitaly Buka1175a9b2015-08-15 10:42:17 -0700141 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 Vakulenko0022b752015-10-02 11:09:59 -0700152
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 Buka1175a9b2015-08-15 10:42:17 -0700158 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
165std::string BuffetConfig::LoadSettings() {
Alex Vakulenkodf381642015-10-08 07:34:23 -0700166 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 Buka1175a9b2015-08-15 10:42:17 -0700171 std::string json_string;
Alex Vakulenkodf381642015-10-08 07:34:23 -0700172 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 Buka1175a9b2015-08-15 10:42:17 -0700178 return json_string;
179}
180
181void BuffetConfig::SaveSettings(const std::string& settings) {
Alex Vakulenkodf381642015-10-08 07:34:23 -0700182 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
192bool BuffetConfig::LoadFile(const base::FilePath& file_path,
193 std::string* data,
Alex Vakulenko41705852015-10-13 10:12:06 -0700194 brillo::ErrorPtr* error) {
Alex Vakulenkodf381642015-10-08 07:34:23 -0700195 if (!file_io_->ReadFile(file_path, data)) {
Alex Vakulenko41705852015-10-13 10:12:06 -0700196 brillo::errors::system::AddSystemError(error, FROM_HERE, errno);
197 brillo::Error::AddToPrintf(error, FROM_HERE, kErrorDomain, kFileReadError,
Alex Vakulenkodf381642015-10-08 07:34:23 -0700198 "Failed to read file '%s'",
199 file_path.value().c_str());
200 return false;
201 }
202 return true;
Vitaly Buka1175a9b2015-08-15 10:42:17 -0700203}
204
205} // namespace buffet