Finishing blacklist managememtn

This commit is contained in:
stephb9959
2021-10-14 14:49:34 -07:00
parent a92601b285
commit e87d9efd09
14 changed files with 330 additions and 135 deletions

View File

@@ -76,7 +76,7 @@ add_executable( owgw
src/FileUploader.cpp src/FileUploader.h
src/RESTAPI_file.cpp src/RESTAPI_file.h
src/RESTAPI_system_command.cpp src/RESTAPI_system_command.h
src/RESTAPI_BlackList.cpp src/RESTAPI_BlackList.h
src/RESTAPI_blacklist.cpp src/RESTAPI_blacklist.h
src/Utils.h src/Utils.cpp src/storage_blacklist.cpp
src/storage_command.cpp src/storage_healthcheck.cpp src/storage_statistics.cpp src/storage_logs.cpp
src/storage_device.cpp src/storage_capabilities.cpp src/storage_defconfig.cpp
@@ -101,7 +101,7 @@ add_executable( owgw
src/RESTAPI_GenericServer.cpp src/RESTAPI_GenericServer.h
src/RESTAPI_errors.h src/RESTAPI_TelemetryWebSocket.cpp src/RESTAPI_TelemetryWebSocket.h
src/Storage.h
src/ConfigurationValidator.cpp src/ConfigurationValidator.h src/ConfigurationCache.cpp src/ConfigurationCache.h)
src/ConfigurationValidator.cpp src/ConfigurationValidator.h src/ConfigurationCache.cpp src/ConfigurationCache.h src/RESTAPI_blacklist_list.cpp src/RESTAPI_blacklist_list.h)
if(NOT SMALL_BUILD)
target_sources(owgw PUBLIC src/KafkaManager.cpp src/KafkaManager.h)

2
build
View File

@@ -1 +1 @@
84
86

View File

@@ -2189,17 +2189,76 @@ paths:
404:
$ref: '#/components/responses/NotFound'
/blacklist/{serialNumber}:
get:
tags:
- Blacklist
summary: Returns a blacklist entry
description: Get a list of blacklisteddevices.
operationId: getBlacklistDevice
parameters:
- in: path
description: Pagination start (starts at 1. If not specified, 1 is assumed)
name: serialNumber
schema:
type: string
required: true
responses:
200:
description: List blacklisted devices
content:
application/json:
schema:
$ref: '#/components/schemas/BlackDeviceInfo'
403:
$ref: '#/components/responses/Unauthorized'
404:
$ref: '#/components/responses/NotFound'
post:
tags:
- Blacklist
summary: Adds to the blacklist
operationId: addToBlackList
summary: Create to the blacklist
operationId: createBlackListDevice
parameters:
- in: path
description: Pagination start (starts at 1. If not specified, 1 is assumed)
name: serialNumber
schema:
type: string
required: true
requestBody:
description: Add blacklisted device
content:
application/json:
schema:
$ref: '#/components/schemas/BlackDeviceInfo'
responses:
200:
$ref: '#/components/responses/Success'
403:
$ref: '#/components/responses/Unauthorized'
404:
$ref: '#/components/responses/NotFound'
put:
tags:
- Blacklist
summary: Modify to the blacklist
operationId: modifyBlackList
parameters:
- in: path
description: Pagination start (starts at 1. If not specified, 1 is assumed)
name: serialNumber
schema:
type: string
required: true
requestBody:
description: Add blacklisted devices
content:
application/json:
schema:
$ref: '#/components/schemas/BlackDeviceList'
$ref: '#/components/schemas/BlackDeviceInfo'
responses:
200:
$ref: '#/components/responses/Success'
@@ -2214,8 +2273,7 @@ paths:
summary: Delete from the blacklist
operationId: deleteFromBlackList
parameters:
- in: query
description: Serial Number
- in: path
name: serialNumber
schema:
type: string

View File

@@ -1,89 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#include <ctime>
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/Stringifier.h"
#include "RESTAPI_BlackList.h"
#include "RESTAPI_protocol.h"
#include "StorageService.h"
#include "RESTAPI_errors.h"
namespace OpenWifi {
void RESTAPI_BlackList::DoDelete() {
auto SerialNumber = GetBinding(RESTAPI::Protocol::SERIALNUMBER, "");
if(SerialNumber.empty()) {
BadRequest(RESTAPI::Errors::MissingSerialNumber);
return;
}
GWObjects::BlackListedDevice D;
if(!Storage()->GetBlackListDevice(SerialNumber, D)) {
NotFound();
return;
}
if (Storage()->DeleteBlackListDevice(SerialNumber)) {
OK();
return;
}
BadRequest(RESTAPI::Errors::CouldNotBeDeleted);
}
void RESTAPI_BlackList::DoGet() {
std::vector<GWObjects::BlackListedDevice> Devices;
Poco::JSON::Array Objects;
if (Storage()->GetBlackListDevices(QB_.Offset, QB_.Limit, Devices)) {
for (const auto &i : Devices) {
Poco::JSON::Object Obj;
i.to_json(Obj);
Objects.add(Obj);
}
}
Poco::JSON::Object RetObj;
RetObj.set(RESTAPI::Protocol::DEVICES, Objects);
ReturnObject(RetObj);
}
void RESTAPI_BlackList::DoPost() {
auto Obj = ParseStream();
if (Obj->has(RESTAPI::Protocol::DEVICES) &&
Obj->isArray(RESTAPI::Protocol::DEVICES)) {
std::vector<GWObjects::BlackListedDevice> Devices;
auto DeviceArray = Obj->getArray(RESTAPI::Protocol::DEVICES);
for (const auto &i : *DeviceArray) {
Poco::JSON::Parser pp;
auto InnerObj = pp.parse(i).extract<Poco::JSON::Object::Ptr>();
Poco::DynamicStruct Vars = *InnerObj;
if (Vars.contains(RESTAPI::Protocol::SERIALNUMBER) &&
Vars.contains(RESTAPI::Protocol::REASON)) {
auto SerialNumber = Vars[RESTAPI::Protocol::SERIALNUMBER].toString();
auto Reason = Vars[RESTAPI::Protocol::REASON].toString();
GWObjects::BlackListedDevice D{.SerialNumber = SerialNumber,
.Reason = Reason,
.Author = UserInfo_.webtoken.username_,
.Created = (uint64_t)time(nullptr)};
Devices.push_back(D);
}
}
if (!Devices.empty()) {
if (Storage()->AddBlackListDevices(Devices)) {
OK();
return;
}
} else {
BadRequest(RESTAPI::Errors::MissingOrInvalidParameters);
}
} else {
BadRequest(RESTAPI::Errors::MissingOrInvalidParameters);
}
}
}

View File

@@ -162,10 +162,22 @@ namespace OpenWifi::GWObjects {
}
void BlackListedDevice::to_json(Poco::JSON::Object &Obj) const {
field_to_json(Obj,"serialNumber", SerialNumber);
field_to_json(Obj,"author", Author);
field_to_json(Obj,"reason", Reason);
field_to_json(Obj,"created", Created);
field_to_json(Obj,"serialNumber", serialNumber);
field_to_json(Obj,"author", author);
field_to_json(Obj,"reason", reason);
field_to_json(Obj,"created", created);
}
bool BlackListedDevice::from_json(Poco::JSON::Object::Ptr Obj) {
try {
field_from_json(Obj,"serialNumber",serialNumber);
field_from_json(Obj,"author",author);
field_from_json(Obj,"reason",reason);
field_from_json(Obj,"created",created);
return true;
} catch (const Poco::Exception &E) {
}
return false;
}
void ConnectionState::to_json(Poco::JSON::Object &Obj) const {

View File

@@ -142,11 +142,12 @@ namespace OpenWifi::GWObjects {
};
struct BlackListedDevice {
std::string SerialNumber;
std::string Reason;
std::string Author;
uint64_t Created;
std::string serialNumber;
std::string reason;
std::string author;
uint64_t created;
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(Poco::JSON::Object::Ptr Obj);
};
struct RttySessionDetails {

View File

@@ -6,7 +6,8 @@
#include "Poco/URI.h"
#include "RESTAPI_BlackList.h"
#include "RESTAPI_blacklist.h"
#include "RESTAPI_blacklist_list.h"
#include "RESTAPI_command.h"
#include "RESTAPI_commands.h"
#include "RESTAPI_default_configuration.h"
@@ -76,7 +77,8 @@ namespace OpenWifi {
RESTAPI_command,
RESTAPI_commands,
RESTAPI_ouis,
RESTAPI_file,
RESTAPI_BlackList>(Path,Bindings,Logger_, Server_); }
RESTAPI_file, RESTAPI_blacklist,
RESTAPI_blacklist_list>(Path,Bindings,Logger_, Server_);
}
}

120
src/RESTAPI_blacklist.cpp Normal file
View File

@@ -0,0 +1,120 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#include <ctime>
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/Stringifier.h"
#include "RESTAPI_blacklist.h"
#include "RESTAPI_errors.h"
#include "RESTAPI_protocol.h"
#include "StorageService.h"
namespace OpenWifi {
void RESTAPI_blacklist::DoDelete() {
auto SerialNumber = GetBinding(RESTAPI::Protocol::SERIALNUMBER, "");
if(SerialNumber.empty()) {
return BadRequest(RESTAPI::Errors::MissingSerialNumber);
}
GWObjects::BlackListedDevice D;
if(!Storage()->GetBlackListDevice(SerialNumber, D)) {
return NotFound();
}
if (Storage()->DeleteBlackListDevice(SerialNumber)) {
return OK();
}
BadRequest(RESTAPI::Errors::CouldNotBeDeleted);
}
void RESTAPI_blacklist::DoGet() {
auto SerialNumber = GetBinding(RESTAPI::Protocol::SERIALNUMBER, "");
if(SerialNumber.empty()) {
return BadRequest(RESTAPI::Errors::MissingSerialNumber);
}
GWObjects::BlackListedDevice D;
if(!Storage()->GetBlackListDevice(SerialNumber, D)) {
return NotFound();
}
Poco::JSON::Object Answer;
D.to_json(Answer);
return ReturnObject(Answer);
}
void RESTAPI_blacklist::DoPost() {
auto Obj = ParseStream();
GWObjects::BlackListedDevice D;
if(!D.from_json(Obj)) {
return BadRequest(RESTAPI::Errors::InvalidJSONDocument);
}
if(D.serialNumber.empty()) {
return BadRequest(RESTAPI::Errors::MissingSerialNumber);
}
Poco::toLowerInPlace(D.serialNumber);
if(Storage()->IsBlackListed(D.serialNumber)) {
return BadRequest(RESTAPI::Errors::SerialNumberExists);
}
D.author = UserInfo_.userinfo.email;
D.created = std::time(nullptr);
if(Storage()->AddBlackListDevice(D)) {
GWObjects::BlackListedDevice CreatedDevice;
Storage()->GetBlackListDevice(D.serialNumber,CreatedDevice);
Poco::JSON::Object Answer;
CreatedDevice.to_json(Answer);
return ReturnObject(Answer);
}
return BadRequest(RESTAPI::Errors::MissingOrInvalidParameters);
}
void RESTAPI_blacklist::DoPut() {
auto SerialNumber = Poco::toLower(GetBinding(RESTAPI::Protocol::SERIALNUMBER, ""));
if(SerialNumber.empty()) {
return BadRequest(RESTAPI::Errors::MissingSerialNumber);
}
auto Obj = ParseStream();
GWObjects::BlackListedDevice Existing;
if(!Storage()->GetBlackListDevice(SerialNumber, Existing)) {
return BadRequest(RESTAPI::Errors::InvalidJSONDocument);
}
GWObjects::BlackListedDevice NewDevice;
if(!NewDevice.from_json(Obj)) {
return BadRequest(RESTAPI::Errors::InvalidJSONDocument);
}
Existing.reason = NewDevice.reason;
Existing.author = UserInfo_.userinfo.email;
if(Storage()->UpdateBlackListDevice(SerialNumber, Existing)) {
GWObjects::BlackListedDevice CreatedDevice;
Storage()->GetBlackListDevice(SerialNumber,CreatedDevice);
Poco::JSON::Object Answer;
CreatedDevice.to_json(Answer);
return ReturnObject(Answer);
}
return BadRequest(RESTAPI::Errors::MissingOrInvalidParameters);
}
}

View File

@@ -12,21 +12,22 @@
#include "RESTAPI_handler.h"
namespace OpenWifi {
class RESTAPI_BlackList : public RESTAPIHandler {
class RESTAPI_blacklist : public RESTAPIHandler {
public:
RESTAPI_BlackList(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, bool Internal)
RESTAPI_blacklist(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, bool Internal)
: RESTAPIHandler(bindings, L,
std::vector<std::string>{Poco::Net::HTTPRequest::HTTP_GET,
Poco::Net::HTTPRequest::HTTP_POST,
Poco::Net::HTTPRequest::HTTP_PUT,
Poco::Net::HTTPRequest::HTTP_DELETE,
Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server,
Internal) {}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/blacklist"};}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/blacklist/{serialNumber}"};}
void DoGet() final;
void DoDelete() final;
void DoPost() final;
void DoPut() final {};
void DoPut() final;
};
}

View File

@@ -0,0 +1,28 @@
//
// Created by stephane bourque on 2021-10-14.
//
#include "RESTAPI_blacklist_list.h"
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/Stringifier.h"
#include "StorageService.h"
namespace OpenWifi {
void RESTAPI_blacklist_list::DoGet() {
std::vector<GWObjects::BlackListedDevice> Devices;
Poco::JSON::Array Arr;
Poco::JSON::Object Answer;
if(Storage()->GetBlackListDevices(QB_.Offset, QB_.Limit, Devices)) {
for(const auto &i:Devices) {
Poco::JSON::Object O;
i.to_json(O);
Arr.add(O);
}
}
Answer.set("devices", Arr);
return ReturnObject(Answer);
}
}

View File

@@ -0,0 +1,27 @@
//
// Created by stephane bourque on 2021-10-14.
//
#ifndef UCENTRALGW_RESTAPI_BLACKLIST_LIST_H
#define UCENTRALGW_RESTAPI_BLACKLIST_LIST_H
#include "RESTAPI_handler.h"
namespace OpenWifi {
class RESTAPI_blacklist_list : public RESTAPIHandler {
public:
RESTAPI_blacklist_list(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, bool Internal)
: RESTAPIHandler(bindings, L,
std::vector<std::string>{Poco::Net::HTTPRequest::HTTP_GET,
Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server,
Internal) {}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/blacklist"};}
void DoGet() final;
void DoDelete() final {};
void DoPost() final {};
void DoPut() final {};
};
}
#endif // UCENTRALGW_RESTAPI_BLACKLIST_LIST_H

View File

@@ -10,18 +10,19 @@
#include "Poco/URI.h"
#include "RESTAPI_BlackList.h"
#include "RESTAPI_blacklist.h"
#include "RESTAPI_blacklist_list.h"
#include "RESTAPI_command.h"
#include "RESTAPI_commands.h"
#include "RESTAPI_default_configuration.h"
#include "RESTAPI_default_configurations.h"
#include "RESTAPI_deviceDashboardHandler.h"
#include "RESTAPI_device_commandHandler.h"
#include "RESTAPI_device_handler.h"
#include "RESTAPI_devices_handler.h"
#include "RESTAPI_file.h"
#include "RESTAPI_system_command.h"
#include "RESTAPI_ouis.h"
#include "RESTAPI_deviceDashboardHandler.h"
#include "RESTAPI_system_command.h"
#include "Utils.h"
#include "RESTAPI_webSocketServer.h"
@@ -88,8 +89,8 @@ namespace OpenWifi {
RESTAPI_file,
RESTAPI_system_command,
RESTAPI_deviceDashboardHandler,
RESTAPI_webSocketServer,
RESTAPI_BlackList,
RESTAPI_webSocketServer, RESTAPI_blacklist,
RESTAPI_blacklist_list,
RESTAPI_TelemetryWebSocket>(Path,Bindings,Logger_, Server_);
}

View File

@@ -116,12 +116,13 @@ namespace OpenWifi {
bool GetNewestCommands(std::string &SerialNumber, uint64_t HowMany, std::vector<GWObjects::CommandDetails> & Commands);
bool SetCommandExecuted(std::string & CommandUUID);
bool AddBlackListDevices(const std::vector<GWObjects::BlackListedDevice> & Devices);
bool AddBlackListDevices(std::vector<GWObjects::BlackListedDevice> & Devices);
bool AddBlackListDevice(GWObjects::BlackListedDevice & Device);
bool GetBlackListDevice(std::string & SerialNumber, GWObjects::BlackListedDevice & Device);
bool DeleteBlackListDevice(std::string & SerialNumber);
bool IsBlackListed(std::string & SerialNumber);
bool GetBlackListDevices(uint64_t Offset, uint64_t HowMany, std::vector<GWObjects::BlackListedDevice> & Devices );
bool UpdateBlackListDevice(std::string & SerialNumber, GWObjects::BlackListedDevice & Device);
bool SetLifetimeStats(std::string & SerialNumber, std::string & Stats);
bool GetLifetimeStats(std::string & SerialNumber, std::string & Stats);
bool ResetLifetimeStats(std::string & SerialNumber);

View File

@@ -40,32 +40,43 @@ namespace OpenWifi {
typedef std::vector<BlackListDeviceRecordTuple> BlackListDeviceRecordList;
void ConvertBlackListDeviceRecord(const BlackListDeviceRecordTuple & R, GWObjects::BlackListedDevice &D) {
D.SerialNumber = Poco::toLower(R.get<0>());
D.Reason = R.get<1>();
D.Created = R.get<2>();
D.Author = R.get<3>();
D.serialNumber = Poco::toLower(R.get<0>());
D.reason = R.get<1>();
D.created = R.get<2>();
D.author = R.get<3>();
}
void ConvertBlackListDeviceRecord(const GWObjects::BlackListedDevice &D, BlackListDeviceRecordTuple & R) {
R.set<0>(Poco::toLower(D.SerialNumber));
R.set<1>(D.Reason);
R.set<2>(D.Created);
R.set<3>(D.Author);
R.set<0>(Poco::toLower(D.serialNumber));
R.set<1>(D.reason);
R.set<2>(D.created);
R.set<3>(D.author);
}
bool Storage::AddBlackListDevices(const std::vector<GWObjects::BlackListedDevice> &Devices) {
bool Storage::AddBlackListDevice(GWObjects::BlackListedDevice & Device) {
try {
Poco::Data::Session Sess = Pool_->get();
Poco::Data::Statement Insert(Sess);
std::string St{"INSERT INTO BlackList (" + DB_BlackListDeviceSelectFields + ") " + DB_BlackListDeviceInsertValues };
BlackListDeviceRecordTuple T;
ConvertBlackListDeviceRecord(Device,T);
Insert << ConvertParams(St),
Poco::Data::Keywords::use(T);
Insert.execute();
return true;
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
return false;
}
bool Storage::AddBlackListDevices(std::vector<GWObjects::BlackListedDevice> &Devices) {
try {
for (auto &i : Devices) {
std::string St{"INSERT INTO BlackList (" + DB_BlackListDeviceSelectFields + ") " + DB_BlackListDeviceInsertValues };
BlackListDeviceRecordTuple T;
ConvertBlackListDeviceRecord(i,T);
Insert << ConvertParams(St),
Poco::Data::Keywords::use(T);
Insert.execute();
AddBlackListDevice(i);
}
return true;
} catch (const Poco::Exception &E) {
@@ -116,6 +127,28 @@ namespace OpenWifi {
return false;
}
bool Storage::UpdateBlackListDevice(std::string & SerialNumber, GWObjects::BlackListedDevice & Device) {
try {
Poco::Data::Session Sess = Pool_->get();
Poco::Data::Statement Update(Sess);
std::string St{"UPDATE BlackList " + DB_BlackListDeviceUpdateFields + " where serialNumber=?" };
BlackListDeviceRecordTuple T;
ConvertBlackListDeviceRecord(Device,T);
Update << ConvertParams(St),
Poco::Data::Keywords::use(T),
Poco::Data::Keywords::use(SerialNumber);
Update.execute();
return true;
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
return false;
}
bool Storage::GetBlackListDevices(uint64_t Offset, uint64_t HowMany,
std::vector<GWObjects::BlackListedDevice> &Devices) {
try {