Line data Source code
1 : /*
2 : ___________________________________________
3 : | _ ___ _ |
4 : | | | |__ \ | | |
5 : | | |__ ) |__ _ __ _ ___ _ __ | |_ |
6 : | | '_ \ / // _` |/ _` |/ _ \ '_ \| __| | HTTP/2 AGENT FOR MOCK TESTING
7 : | | | | |/ /| (_| | (_| | __/ | | | |_ | Version 0.0.z
8 : | |_| |_|____\__,_|\__, |\___|_| |_|\__| | https://github.com/testillano/h2agent
9 : | __/ | |
10 : | |___/ |
11 : |___________________________________________|
12 :
13 : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
14 : SPDX-License-Identifier: MIT
15 : Copyright (c) 2021 Eduardo Ramos
16 :
17 : Permission is hereby granted, free of charge, to any person obtaining a copy
18 : of this software and associated documentation files (the "Software"), to deal
19 : in the Software without restriction, including without limitation the rights
20 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 : copies of the Software, and to permit persons to whom the Software is
22 : furnished to do so, subject to the following conditions:
23 :
24 : The above copyright notice and this permission notice shall be included in all
25 : copies or substantial portions of the Software.
26 :
27 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 : SOFTWARE.
34 : */
35 :
36 : #include <string>
37 :
38 : #include <ert/tracing/Logger.hpp>
39 :
40 : #include <Vault.hpp>
41 : #include <AdminSchemas.hpp>
42 : #include <WaitManager.hpp>
43 : #include <SseManager.hpp>
44 :
45 : namespace h2agent
46 : {
47 : namespace model
48 : {
49 :
50 302 : Vault::Vault() {
51 302 : vault_schema_.setJson(h2agent::adminSchemas::vault); // won't fail
52 302 : }
53 :
54 8 : void Vault::load(const std::string &variable, const nlohmann::json &value) {
55 8 : add(variable, value);
56 8 : if (wait_manager_) wait_manager_->notify();
57 8 : if (sse_manager_) sse_manager_->notify(variable, value);
58 8 : }
59 :
60 246 : void Vault::load(const std::string &variable, nlohmann::json &&value) {
61 246 : add(variable, std::move(value));
62 246 : if (wait_manager_) wait_manager_->notify();
63 246 : if (sse_manager_) {
64 0 : nlohmann::json v;
65 0 : tryGet(variable, v);
66 0 : sse_manager_->notify(variable, v);
67 0 : }
68 246 : }
69 :
70 8 : void Vault::loadAtPath(const std::string &variable, const std::string &path, const nlohmann::json &value) {
71 8 : modifyOrInsert(variable, [&](nlohmann::json ¤t) {
72 8 : if (!current.is_object()) current = nlohmann::json::object();
73 8 : current[nlohmann::json::json_pointer(path)] = value;
74 8 : });
75 8 : if (wait_manager_) wait_manager_->notify();
76 8 : if (sse_manager_) {
77 0 : nlohmann::json v;
78 0 : tryGet(variable, v);
79 0 : sse_manager_->notify(variable, v);
80 0 : }
81 8 : }
82 :
83 8 : bool Vault::loadJson(const nlohmann::json &j) {
84 :
85 8 : std::string error{};
86 8 : if (!vault_schema_.validate(j, error)) {
87 3 : return false;
88 : }
89 :
90 : // Reject keys containing dots (reserved for JSON path navigation)
91 23 : for (auto it = j.begin(); it != j.end(); ++it) {
92 9 : if (it.key().find('.') != std::string::npos) {
93 0 : LOGWARNING(ert::tracing::Logger::warning(ert::tracing::Logger::asString("Vault entry key '%s' contains dots (not allowed)", it.key().c_str()), ERT_FILE_LOCATION));
94 0 : return false;
95 : }
96 : }
97 :
98 : // Convert: schema still validates as object, but values can be any JSON type.
99 : // Map::add(map_t) expects unordered_map<string, json>, which nlohmann::json
100 : // object iteration provides directly.
101 23 : for (auto it = j.begin(); it != j.end(); ++it) {
102 9 : add(it.key(), it.value());
103 9 : if (sse_manager_) sse_manager_->notify(it.key(), it.value());
104 : }
105 5 : if (wait_manager_) wait_manager_->notify();
106 :
107 5 : return true;
108 8 : }
109 :
110 1 : std::string Vault::asJsonString() const {
111 :
112 3 : return ((size() != 0) ? getJson().dump() : "{}"); // server data is shown as an object
113 : }
114 :
115 1 : nlohmann::json Vault::getJson() const {
116 1 : return Map::getJson();
117 : }
118 :
119 : }
120 : }
121 :
|