stephb9959
2024-02-20 13:26:49 -08:00
parent 4ded8997cd
commit fd85c70c2f
10 changed files with 4716 additions and 119 deletions

2
build
View File

@@ -1 +1 @@
14
18

View File

@@ -815,6 +815,17 @@ components:
type: string
minLength: 2
maxLength: 2
imported:
type: integer
format: int64
connected:
type: integer
format: int64
platform:
type: string
enum:
- AP
- SWITCH
VenueDeviceList:
type: object
@@ -3240,6 +3251,15 @@ paths:
schema:
type: boolean
required: false
- in: query
name: deviceType
schema:
type: string
enum:
- AP
- SWITCH
required: false
default: AP
requestBody:
description: Information used to create the new entity
content:
@@ -3268,6 +3288,15 @@ paths:
format: uuid
example: When modifying the root entity, the uuid 0000-0000-0000 must be entered.
required: true
- in: query
name: deviceType
schema:
type: string
enum:
- AP
- SWITCH
required: false
default: AP
requestBody:
description: Information used to modify the new entity
content:

View File

@@ -91,9 +91,10 @@ namespace OpenWifi {
}
auto Config = RawObject->get("configuration").toString();
Poco::JSON::Object Answer;
std::vector<std::string> Error;
auto deviceType = GetParameter("deviceType", "AP");
std::vector<std::string> Error;
auto Res =
ValidateUCentralConfiguration(Config, Error, GetBoolParameter("strict", true));
ValidateUCentralConfiguration(ConfigurationValidator::GetType(deviceType),Config, Error, GetBoolParameter("strict", true));
Answer.set("valid", Res);
Answer.set("error", Error);
return ReturnObject(Answer);
@@ -134,9 +135,10 @@ namespace OpenWifi {
}
std::vector<std::string> Errors;
if (!ValidateConfigBlock(NewObject, Errors)) {
return BadRequest(RESTAPI::Errors::ConfigBlockInvalid);
}
auto deviceType = GetParameter("deviceType", "AP");
if (!ValidateConfigBlock(ConfigurationValidator::GetType(deviceType), NewObject, Errors)) {
return BadRequest(RESTAPI::Errors::ConfigBlockInvalid);
}
if (DB_.CreateRecord(NewObject)) {
MoveUsage(StorageService()->PolicyDB(), DB_, "", NewObject.managementPolicy,
@@ -185,9 +187,10 @@ namespace OpenWifi {
Existing.deviceTypes = NewObject.deviceTypes;
std::vector<std::string> Errors;
if (!ValidateConfigBlock(NewObject, Errors)) {
return BadRequest(RESTAPI::Errors::ConfigBlockInvalid);
}
auto deviceType = GetParameter("deviceType", "AP");
if (!ValidateConfigBlock(ConfigurationValidator::GetType(deviceType), NewObject, Errors)) {
return BadRequest(RESTAPI::Errors::ConfigBlockInvalid);
}
if (RawObject->has("configuration")) {
Existing.configuration = NewObject.configuration;

View File

@@ -431,47 +431,47 @@ namespace OpenWifi {
return EntityDB::RootUUID();
}
inline bool ValidateConfigBlock(const ProvObjects::DeviceConfiguration &Config,
std::vector<std::string> &Errors) {
static const std::vector<std::string> SectionNames{
"globals", "interfaces", "metrics", "radios", "services", "unit",
"definitions", "ethernet", "switch", "config-raw", "third-party"};
inline bool ValidateConfigBlock(ConfigurationValidator::ConfigurationType Type, const ProvObjects::DeviceConfiguration &Config,
std::vector<std::string> &Errors) {
static const std::vector<std::string> SectionNames{
"globals", "interfaces", "metrics", "radios", "services", "unit",
"definitions", "ethernet", "switch", "config-raw", "third-party"};
for (const auto &i : Config.configuration) {
Poco::JSON::Parser P;
if (i.name.empty()) {
Errors.push_back("Name is empty");
return false;
}
for (const auto &i : Config.configuration) {
Poco::JSON::Parser P;
if (i.name.empty()) {
Errors.push_back("Name is empty");
return false;
}
try {
auto Blocks = P.parse(i.configuration).extract<Poco::JSON::Object::Ptr>();
auto N = Blocks->getNames();
for (const auto &j : N) {
if (std::find(SectionNames.cbegin(), SectionNames.cend(), j) ==
SectionNames.cend()) {
Errors.push_back("Unknown block name");
return false;
}
}
} catch (const Poco::JSON::JSONException &E) {
Errors.push_back("Invalid JSON document");
return false;
}
try {
auto Blocks = P.parse(i.configuration).extract<Poco::JSON::Object::Ptr>();
auto N = Blocks->getNames();
for (const auto &j : N) {
if (std::find(SectionNames.cbegin(), SectionNames.cend(), j) ==
SectionNames.cend()) {
Errors.push_back("Unknown block name");
return false;
}
}
} catch (const Poco::JSON::JSONException &E) {
Errors.push_back("Invalid JSON document");
return false;
}
try {
if (ValidateUCentralConfiguration(i.configuration, Errors, true)) {
// std::cout << "Block: " << i.name << " is valid" << std::endl;
} else {
return false;
}
} catch (...) {
Errors.push_back("Invalid configuration caused an exception");
return false;
}
}
return true;
}
try {
if (ValidateUCentralConfiguration(Type,i.configuration, Errors, true)) {
// std::cout << "Block: " << i.name << " is valid" << std::endl;
} else {
return false;
}
} catch (...) {
Errors.push_back("Invalid configuration caused an exception");
return false;
}
}
return true;
}
template <typename Type>
std::map<std::string, std::string> CreateObjects(Type &NewObject, RESTAPIHandler &R,
@@ -535,7 +535,7 @@ namespace OpenWifi {
ProvObjects::DeviceConfiguration DC;
if (DC.from_json(ConfigurationDetails)) {
if constexpr (std::is_same_v<Type, ProvObjects::InventoryTag>) {
if (!ValidateConfigBlock(DC, Errors)) {
if (!ValidateConfigBlock(ConfigurationValidator::ConfigurationType::AP,DC, Errors)) {
break;
}
ProvObjects::CreateObjectInfo(R.UserInfo_.userinfo, DC.info);

View File

@@ -587,6 +587,9 @@ namespace OpenWifi::ProvObjects {
field_to_json(Obj, "locale", locale);
field_to_json(Obj, "realMacAddress", realMacAddress);
field_to_json(Obj, "doNotAllowOverrides", doNotAllowOverrides);
field_to_json(Obj, "imported", imported);
field_to_json(Obj, "connected", connected);
field_to_json(Obj, "platform", platform);
}
bool InventoryTag::from_json(const Poco::JSON::Object::Ptr &Obj) {
@@ -609,6 +612,9 @@ namespace OpenWifi::ProvObjects {
field_from_json(Obj, "locale", locale);
field_from_json(Obj, "realMacAddress", realMacAddress);
field_from_json(Obj, "doNotAllowOverrides", doNotAllowOverrides);
field_from_json(Obj, "imported", imported);
field_from_json(Obj, "connected", connected);
field_from_json(Obj, "platform", platform);
return true;
} catch (...) {
}

View File

@@ -490,9 +490,11 @@ namespace OpenWifi::ProvObjects {
std::string locale;
std::string realMacAddress;
bool doNotAllowOverrides = false;
std::uint64_t imported=0;
std::uint64_t connected=0;
std::string platform{"AP"};
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(const Poco::JSON::Object::Ptr &Obj);
};

File diff suppressed because it is too large Load Diff

View File

@@ -17,33 +17,45 @@
namespace OpenWifi {
class ConfigurationValidator : public SubSystemServer {
public:
enum class ConfigurationType { AP = 0 , SWITCH = 1};
static auto instance() {
static auto instance_ = new ConfigurationValidator;
return instance_;
}
bool Validate(const std::string &C, std::vector<std::string> &Errors, bool Strict);
bool Validate(ConfigurationType Type, const std::string &C, std::vector<std::string> &Errors, bool Strict);
int Start() override;
void Stop() override;
void reinitialize(Poco::Util::Application &self) override;
inline static ConfigurationType GetType(const std::string &type) {
std::string Type = Poco::toUpper(type);
if (Type == "AP")
return ConfigurationType::AP;
if (Type == "SWITCH")
return ConfigurationType::SWITCH;
return ConfigurationType::AP;
}
private:
bool Initialized_ = false;
bool Working_ = false;
void Init();
std::unique_ptr<valijson::Schema> RootSchema_;
std::unique_ptr<valijson::SchemaParser> SchemaParser_;
std::unique_ptr<valijson::adapters::PocoJsonAdapter> PocoJsonAdapter_;
Poco::JSON::Object::Ptr SchemaDocPtr_;
bool SetSchema(const std::string &SchemaStr);
std::array<std::unique_ptr<valijson::Schema>,2> RootSchema_;
std::array<std::unique_ptr<valijson::SchemaParser>,2> SchemaParser_;
std::array<std::unique_ptr<valijson::adapters::PocoJsonAdapter>,2> PocoJsonAdapter_;
std::array<Poco::JSON::Object::Ptr,2> SchemaDocPtr_;
bool SetSchema(ConfigurationType Type, const std::string &SchemaStr);
ConfigurationValidator()
: SubSystemServer("ConfigValidator", "CFG-VALIDATOR", "config.validator") {}
};
inline auto ConfigurationValidator() { return ConfigurationValidator::instance(); }
inline bool ValidateUCentralConfiguration(const std::string &C, std::vector<std::string> &Error,
inline bool ValidateUCentralConfiguration(ConfigurationValidator::ConfigurationType Type, const std::string &C, std::vector<std::string> &Error,
bool strict) {
return ConfigurationValidator::instance()->Validate(C, Error, strict);
return ConfigurationValidator::instance()->Validate(Type, C, Error, strict);
}
} // namespace OpenWifi

View File

@@ -44,7 +44,10 @@ namespace OpenWifi {
ORM::Field{"devClass", ORM::FieldType::FT_TEXT},
ORM::Field{"locale", ORM::FieldType::FT_TEXT},
ORM::Field{"realMacAddress", ORM::FieldType::FT_TEXT},
ORM::Field{"doNotAllowOverrides", ORM::FieldType::FT_BOOLEAN}};
ORM::Field{"doNotAllowOverrides", ORM::FieldType::FT_BOOLEAN},
ORM::Field{"imported", ORM::FieldType::FT_BIGINT},
ORM::Field{"connected", ORM::FieldType::FT_BIGINT},
ORM::Field{"platform", ORM::FieldType::FT_TEXT}};
static ORM::IndexVec InventoryDB_Indexes{
{std::string("inventory_name_index"),
@@ -60,6 +63,9 @@ namespace OpenWifi {
"alter table " + TableName_ + " add column realMacAddress text",
"alter table " + TableName_ + " add column devClass text",
"alter table " + TableName_ + " add column deviceRules text",
"alter table " + TableName_ + " add column platform text default 'AP'",
"alter table " + TableName_ + " add column imported bigint",
"alter table " + TableName_ + " add column connected bigint",
"alter table " + TableName_ + " add column doNotAllowOverrides boolean"};
for (const auto &i : Script) {
@@ -106,6 +112,8 @@ namespace OpenWifi {
StateDoc["date"] = Utils::Now();
NewDevice.state = to_string(StateDoc);
NewDevice.devClass = "any";
NewDevice.connected = Now;
NewDevice.imported = 0;
if (!IP.empty()) {
StorageService()->VenueDB().GetByIP(IP, NewDevice.venue);
if (NewDevice.venue.empty()) {
@@ -176,6 +184,7 @@ namespace OpenWifi {
if (modified) {
ExistingDevice.info.modified = Utils::Now();
ExistingDevice.connected = Utils::Now();
StorageService()->InventoryDB().UpdateRecord("id", ExistingDevice.info.id,
ExistingDevice);
}
@@ -326,6 +335,9 @@ void ORM::DB<OpenWifi::InventoryDBRecordType, OpenWifi::ProvObjects::InventoryTa
Out.locale = In.get<21>();
Out.realMacAddress = In.get<22>();
Out.doNotAllowOverrides = In.get<23>();
Out.imported = In.get<24>();
Out.connected = In.get<25>();
Out.platform = In.get<26>();
}
template <>
@@ -355,4 +367,7 @@ void ORM::DB<OpenWifi::InventoryDBRecordType, OpenWifi::ProvObjects::InventoryTa
Out.set<21>(In.locale);
Out.set<22>(In.realMacAddress);
Out.set<23>(In.doNotAllowOverrides);
Out.set<24>(In.imported);
Out.set<25>(In.connected);
Out.set<26>(In.platform);
}

View File

@@ -16,7 +16,7 @@ namespace OpenWifi {
std::string, std::string, std::string, std::string, std::string,
std::string, std::string, std::string, std::string, std::string,
std::string, std::string, std::string, std::string, std::string,
std::string, std::string, bool>
std::string, std::string, bool, uint64_t, uint64_t, std::string>
InventoryDBRecordType;
class InventoryDB : public ORM::DB<InventoryDBRecordType, ProvObjects::InventoryTag> {