aboutsummaryrefslogtreecommitdiff
path: root/buffet/buffet_config.cc
blob: 16672f6349b52e8604e31aaccf99a7d583bd73a4 (plain)
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
// Copyright 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "buffet/buffet_config.h"

#include <map>
#include <set>

#include <base/files/file_util.h>
#include <base/files/important_file_writer.h>
#include <base/logging.h>
#include <base/message_loop/message_loop.h>
#include <base/strings/string_number_conversions.h>
#include <brillo/errors/error.h>
#include <brillo/errors/error_codes.h>
#include <brillo/osrelease_reader.h>
#include <brillo/strings/string_utils.h>
#include <weave/enum_to_string.h>

namespace buffet {

namespace {

const char kErrorDomain[] = "buffet";
const char kFileReadError[] = "file_read_error";
const char kProductVersionKey[] = "product_version";

class DefaultFileIO : public BuffetConfig::FileIO {
 public:
  bool ReadFile(const base::FilePath& path, std::string* content) override {
    return base::ReadFileToString(path, content);
  }
  bool WriteFile(const base::FilePath& path,
                 const std::string& content) override {
    return base::ImportantFileWriter::WriteFileAtomically(path, content);
  }
};

}  // namespace

namespace config_keys {

const char kClientId[] = "client_id";
const char kClientSecret[] = "client_secret";
const char kApiKey[] = "api_key";
const char kOAuthURL[] = "oauth_url";
const char kServiceURL[] = "service_url";
const char kName[] = "name";
const char kDescription[] = "description";
const char kLocation[] = "location";
const char kLocalAnonymousAccessRole[] = "local_anonymous_access_role";
const char kLocalDiscoveryEnabled[] = "local_discovery_enabled";
const char kLocalPairingEnabled[] = "local_pairing_enabled";
const char kOemName[] = "oem_name";
const char kModelName[] = "model_name";
const char kModelId[] = "model_id";
const char kWifiAutoSetupEnabled[] = "wifi_auto_setup_enabled";
const char kEmbeddedCode[] = "embedded_code";
const char kPairingModes[] = "pairing_modes";
const char kAllowEndpointsOverride[] = "allow_endpoints_override";

}  // namespace config_keys

BuffetConfig::BuffetConfig(const Options& options)
    : options_(options),
      default_encryptor_(Encryptor::CreateDefaultEncryptor()),
      encryptor_(default_encryptor_.get()),
      default_file_io_(new DefaultFileIO),
      file_io_(default_file_io_.get()) {}

bool BuffetConfig::LoadDefaults(weave::Settings* settings) {
  // Keep this hardcoded default for sometime. This previously was set by
  // libweave. It should be set by overlay's buffet.conf.
  // Keys owners: avakulenko, gene, vitalybuka.
  settings->client_id =
      "338428340000-vkb4p6h40c7kja1k3l70kke8t615cjit.apps.googleusercontent."
      "com";
  settings->client_secret = "LS_iPYo_WIOE0m2VnLdduhnx";
  settings->api_key = "AIzaSyACK3oZtmIylUKXiTMqkZqfuRiCgQmQSAQ";

  settings->name = "Developer device";
  settings->oem_name = "Chromium";
  settings->model_name = "Brillo";
  settings->model_id = "AAAAA";

  if (!base::PathExists(options_.defaults))
    return true;  // Nothing to load.

  brillo::KeyValueStore store;
  if (!store.Load(options_.defaults))
    return false;
  bool result = LoadDefaults(store, settings);
  settings->test_privet_ssid = options_.test_privet_ssid;

  return result;
}

bool BuffetConfig::LoadDefaults(const brillo::KeyValueStore& store,
                                weave::Settings* settings) {
  store.GetString(config_keys::kClientId, &settings->client_id);
  store.GetString(config_keys::kClientSecret, &settings->client_secret);
  store.GetString(config_keys::kApiKey, &settings->api_key);
  store.GetString(config_keys::kOAuthURL, &settings->oauth_url);
  store.GetString(config_keys::kServiceURL, &settings->service_url);
  store.GetString(config_keys::kOemName, &settings->oem_name);
  store.GetString(config_keys::kModelName, &settings->model_name);
  store.GetString(config_keys::kModelId, &settings->model_id);

  brillo::OsReleaseReader reader;
  reader.Load();
  if (!reader.GetString(kProductVersionKey, &settings->firmware_version)) {
    LOG(ERROR) << "Could not read '" << kProductVersionKey << "' from OS";
  }

  store.GetBoolean(config_keys::kWifiAutoSetupEnabled,
                   &settings->wifi_auto_setup_enabled);
  store.GetString(config_keys::kEmbeddedCode, &settings->embedded_code);

  std::string modes_str;
  if (store.GetString(config_keys::kPairingModes, &modes_str)) {
    std::set<weave::PairingType> pairing_modes;
    for (const std::string& mode :
         brillo::string_utils::Split(modes_str, ",", true, true)) {
      weave::PairingType pairing_mode;
      if (!StringToEnum(mode, &pairing_mode))
        return false;
      pairing_modes.insert(pairing_mode);
    }
    settings->pairing_modes = std::move(pairing_modes);
  }

  store.GetString(config_keys::kName, &settings->name);
  store.GetString(config_keys::kDescription, &settings->description);
  store.GetString(config_keys::kLocation, &settings->location);

  std::string role_str;
  if (store.GetString(config_keys::kLocalAnonymousAccessRole, &role_str)) {
    if (!StringToEnum(role_str, &settings->local_anonymous_access_role))
      return false;
  }
  store.GetBoolean(config_keys::kLocalDiscoveryEnabled,
                   &settings->local_discovery_enabled);
  store.GetBoolean(config_keys::kLocalPairingEnabled,
                   &settings->local_pairing_enabled);
  store.GetBoolean(config_keys::kAllowEndpointsOverride,
                   &settings->allow_endpoints_override);
  return true;
}

std::string BuffetConfig::LoadSettings(const std::string& name) {
  std::string settings_blob;
  base::FilePath path = CreatePath(name);
  if (!file_io_->ReadFile(path, &settings_blob)) {
    LOG(WARNING) << "Failed to read \'" + path.value() +
                        "\', proceeding with empty settings.";
    return std::string();
  }
  std::string json_string;
  if (!encryptor_->DecryptWithAuthentication(settings_blob, &json_string)) {
    LOG(WARNING)
        << "Failed to decrypt settings, proceeding with empty settings.";
    SaveSettings(std::string(), name, {});
    return std::string();
  }
  return json_string;
}

std::string BuffetConfig::LoadSettings() {
  return LoadSettings("");
}

void BuffetConfig::SaveSettings(const std::string& name,
                                const std::string& settings,
                                const weave::DoneCallback& callback) {
  std::string encrypted_settings;
  weave::ErrorPtr error;
  base::FilePath path = CreatePath(name);
  if (!encryptor_->EncryptWithAuthentication(settings, &encrypted_settings)) {
    weave::Error::AddTo(&error, FROM_HERE, "file_write_error",
                        "Failed to encrypt settings.");
    encrypted_settings.clear();
  }
  if (!file_io_->WriteFile(path, encrypted_settings)) {
    weave::Error::AddTo(&error, FROM_HERE, "file_write_error",
                        "Failed to write \'" + path.value() +
                            "\', proceeding with empty settings.");
  }
  if (!callback.is_null()) {
    base::MessageLoop::current()->PostTask(
        FROM_HERE, base::Bind(callback, base::Passed(&error)));
  }
}

base::FilePath BuffetConfig::CreatePath(const std::string& name) const {
  return name.empty() ? options_.settings
                      : options_.settings.InsertBeforeExtension(
                            base::FilePath::kExtensionSeparator + name);
}

bool BuffetConfig::LoadFile(const base::FilePath& file_path,
                            std::string* data,
                            brillo::ErrorPtr* error) {
  if (!file_io_->ReadFile(file_path, data)) {
    brillo::errors::system::AddSystemError(error, FROM_HERE, errno);
    brillo::Error::AddToPrintf(error, FROM_HERE, kErrorDomain, kFileReadError,
                                 "Failed to read file '%s'",
                                 file_path.value().c_str());
    return false;
  }
  return true;
}

}  // namespace buffet