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.
14 
15 #ifndef BUFFET_BUFFET_CONFIG_H_
16 #define BUFFET_BUFFET_CONFIG_H_
17 
18 #include <map>
19 #include <set>
20 #include <string>
21 #include <vector>
22 
23 #include <base/callback.h>
24 #include <base/files/file_path.h>
25 #include <brillo/errors/error.h>
26 #include <brillo/key_value_store.h>
27 #include <weave/provider/config_store.h>
28 
29 #include "buffet/encryptor.h"
30 
31 namespace buffet {
32 
33 class StorageInterface;
34 
35 // Handles reading buffet config and state files.
36 class BuffetConfig final : public weave::provider::ConfigStore {
37  public:
38   struct Options {
39     std::string client_id;
40     std::string client_secret;
41     std::string api_key;
42     std::string oauth_url;
43     std::string service_url;
44 
45     base::FilePath defaults;
46     base::FilePath settings;
47 
48     base::FilePath definitions;
49     base::FilePath test_definitions;
50 
51     std::string test_privet_ssid;
52   };
53 
54   // An IO abstraction to enable testing without using real files.
55   class FileIO {
56    public:
57     virtual bool ReadFile(const base::FilePath& path, std::string* content) = 0;
58     virtual bool WriteFile(const base::FilePath& path,
59                            const std::string& content) = 0;
60   };
61 
62   ~BuffetConfig() override = default;
63 
64   explicit BuffetConfig(const Options& options);
65 
66   // Config overrides.
67   bool LoadDefaults(weave::Settings* settings) override;
68   std::string LoadSettings(const std::string& name) override;
69   std::string LoadSettings() override;
70   void SaveSettings(const std::string& name,
71                     const std::string& settings,
72                     const weave::DoneCallback& callback) override;
73 
74   bool LoadDefaults(const brillo::KeyValueStore& store,
75                     weave::Settings* settings);
76 
77   // Allows injection of a non-default |encryptor| for testing. The caller
78   // retains ownership of the pointer.
SetEncryptor(Encryptor * encryptor)79   void SetEncryptor(Encryptor* encryptor) {
80     encryptor_ = encryptor;
81   }
82 
83   // Allows injection of non-default |file_io| for testing. The caller retains
84   // ownership of the pointer.
SetFileIO(FileIO * file_io)85   void SetFileIO(FileIO* file_io) {
86     file_io_ = file_io;
87   }
88 
89  private:
90   base::FilePath CreatePath(const std::string& name) const;
91   bool LoadFile(const base::FilePath& file_path,
92                 std::string* data,
93                 brillo::ErrorPtr* error);
94 
95   Options options_;
96   std::unique_ptr<Encryptor> default_encryptor_;
97   Encryptor* encryptor_{nullptr};
98   std::unique_ptr<FileIO> default_file_io_;
99   FileIO* file_io_{nullptr};
100 
101   DISALLOW_COPY_AND_ASSIGN(BuffetConfig);
102 };
103 
104 }  // namespace buffet
105 
106 #endif  // BUFFET_BUFFET_CONFIG_H_
107