Line data Source code
1 : /* 2 : * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #include <cmn/agent.h> 7 : #include <oper_db.h> 8 : #include <vnc_cfg_types.h> 9 : #include <agent_types.h> 10 : #include <oper_db.h> 11 : #include <oper/config_manager.h> 12 : #include <oper/agent_sandesh.h> 13 : #include <security_logging_object.h> 14 : #include <oper/ifmap_dependency_manager.h> 15 : #include <cfg/cfg_init.h> 16 : 17 0 : SecurityLoggingObject::SecurityLoggingObject(const boost::uuids::uuid &uuid): 18 0 : uuid_(uuid), firewall_policy_list_(), firewall_rule_list_() { 19 0 : } 20 : 21 0 : SecurityLoggingObject::~SecurityLoggingObject() { 22 0 : } 23 : 24 0 : DBEntryBase::KeyPtr SecurityLoggingObject::GetDBRequestKey() const { 25 0 : SecurityLoggingObjectKey *key = new SecurityLoggingObjectKey(uuid_); 26 0 : return DBEntryBase::KeyPtr(key); 27 : } 28 : 29 0 : void SecurityLoggingObject::SetKey(const DBRequestKey *key) { 30 0 : const SecurityLoggingObjectKey *slo_key = 31 : static_cast<const SecurityLoggingObjectKey *>(key); 32 0 : uuid_ = slo_key->uuid_; 33 0 : } 34 : 35 0 : std::string SecurityLoggingObject::ToString() const { 36 0 : std::ostringstream buffer; 37 0 : buffer << "UUID : " << uuid_; 38 0 : buffer << " rate : " << rate_; 39 0 : return buffer.str(); 40 0 : } 41 : 42 0 : bool SecurityLoggingObject::DBEntrySandesh(Sandesh *sresp, std::string &name) 43 : const { 44 0 : SLOListResp *resp = static_cast<SLOListResp *>(sresp); 45 0 : SLOSandeshData data; 46 0 : vector<SLOSandeshRule> rule_list; 47 0 : data.set_name(name_); 48 0 : data.set_uuid(to_string(uuid_)); 49 0 : data.set_rate(rate_); 50 0 : data.set_status(status_); 51 : vector<autogen::SecurityLoggingObjectRuleEntryType>::const_iterator it = 52 0 : rules_.begin(); 53 0 : while (it != rules_.end()) { 54 0 : SLOSandeshRule rule; 55 0 : rule.set_uuid(it->rule_uuid); 56 0 : rule.set_rate(it->rate); 57 0 : rule_list.push_back(rule); 58 0 : ++it; 59 0 : } 60 0 : data.set_rules(rule_list); 61 : 62 0 : vector<SLOFwPolicyEntry> fp_list; 63 0 : SloRuleList::const_iterator fp_it = firewall_policy_list_.begin(); 64 0 : while (fp_it != firewall_policy_list_.end()) { 65 0 : PolicyLinkData item; 66 0 : item.set_firewall_policy(to_string(fp_it->uuid_)); 67 0 : SLOFwPolicyEntry entry; 68 0 : entry.set_uuid(item); 69 0 : entry.set_rate(fp_it->rate_); 70 0 : fp_list.push_back(entry); 71 0 : ++fp_it; 72 0 : } 73 0 : data.set_firewall_policy_list(fp_list); 74 : 75 0 : vector<SLOSandeshRule> fr_list; 76 0 : SloRuleList::const_iterator fr_it = firewall_rule_list_.begin(); 77 0 : while (fr_it != firewall_rule_list_.end()) { 78 0 : SLOSandeshRule item; 79 0 : item.set_uuid(to_string(fr_it->uuid_)); 80 0 : item.set_rate(fr_it->rate_); 81 0 : fr_list.push_back(item); 82 0 : ++fr_it; 83 0 : } 84 0 : data.set_firewall_rule_list(fr_list); 85 : 86 : vector<SLOSandeshData> &list = 87 0 : const_cast<std::vector<SLOSandeshData>&>(resp->get_slo_list()); 88 0 : list.push_back(data); 89 : 90 0 : return true; 91 0 : } 92 : 93 0 : bool SecurityLoggingObject::IsLess(const DBEntry &rhs) const { 94 0 : const SecurityLoggingObject &fc = static_cast<const SecurityLoggingObject &> 95 : (rhs); 96 0 : return (uuid_ < fc.uuid_); 97 : } 98 : 99 0 : bool SecurityLoggingObject::IsEqual 100 : (const std::vector<autogen::SecurityLoggingObjectRuleEntryType> &lhs, 101 : const std::vector<autogen::SecurityLoggingObjectRuleEntryType> &rhs) const { 102 0 : if (lhs.size() != rhs.size()) { 103 0 : return false; 104 : } 105 : vector<autogen::SecurityLoggingObjectRuleEntryType>::const_iterator lit = 106 0 : lhs.begin(); 107 : vector<autogen::SecurityLoggingObjectRuleEntryType>::const_iterator rit = 108 0 : rhs.begin(); 109 0 : while (lit != lhs.end() && rit != rhs.end()) { 110 0 : if (lit->rule_uuid != rit->rule_uuid) { 111 0 : return false; 112 : } 113 0 : if (lit->rate != rit->rate) { 114 0 : return false; 115 : } 116 0 : ++lit; 117 0 : ++rit; 118 : } 119 0 : return true; 120 : } 121 : 122 0 : bool SecurityLoggingObject::Change(const DBRequest *req) { 123 0 : bool ret = false; 124 : const SecurityLoggingObjectData *data = 125 0 : static_cast<const SecurityLoggingObjectData *>(req->data.get()); 126 : 127 0 : if (status_ != data->status_) { 128 0 : status_ = data->status_; 129 : } 130 : 131 0 : if (rate_ != data->rate_) { 132 0 : rate_ = data->rate_; 133 0 : ret = true; 134 : } 135 : 136 0 : if (!IsEqual(rules_, data->rules_)) { 137 0 : rules_ = data->rules_; 138 0 : ret = true; 139 : } 140 : 141 0 : if (name_ != data->name_) { 142 0 : name_ = data->name_; 143 0 : ret = true; 144 : } 145 : 146 0 : if (firewall_policy_list_ != data->firewall_policy_list_) { 147 0 : firewall_policy_list_ = data->firewall_policy_list_; 148 0 : ret = true; 149 : } 150 : 151 0 : if (firewall_rule_list_ != data->firewall_rule_list_) { 152 0 : firewall_rule_list_ = data->firewall_rule_list_; 153 0 : ret = true; 154 : } 155 : 156 0 : return ret; 157 : } 158 : 159 : ///////////////////////////////////////////////////////////////////////////// 160 : // SecurityLoggingObjectTable routines 161 : ///////////////////////////////////////////////////////////////////////////// 162 2 : SecurityLoggingObjectTable::SecurityLoggingObjectTable(Agent *agent, 163 2 : DB *db, const std::string &name): 164 2 : AgentOperDBTable(db, name) { 165 2 : set_agent(agent); 166 2 : } 167 : 168 4 : SecurityLoggingObjectTable::~SecurityLoggingObjectTable() { 169 4 : } 170 : 171 : DBTableBase* 172 2 : SecurityLoggingObjectTable::CreateTable(Agent *agent, DB *db, 173 : const std::string &name) { 174 : SecurityLoggingObjectTable *table = new SecurityLoggingObjectTable(agent, 175 : db, 176 2 : name); 177 2 : (static_cast<DBTable *>(table))->Init(); 178 2 : return table; 179 : } 180 : 181 : std::unique_ptr<DBEntry> 182 0 : SecurityLoggingObjectTable::AllocEntry(const DBRequestKey *k) const { 183 0 : const SecurityLoggingObjectKey *key = 184 : static_cast<const SecurityLoggingObjectKey *>(k); 185 0 : SecurityLoggingObject *slo = new SecurityLoggingObject(key->uuid_); 186 0 : return std::unique_ptr<DBEntry>(static_cast<DBEntry *>(slo)); 187 : } 188 : 189 0 : DBEntry* SecurityLoggingObjectTable::OperDBAdd(const DBRequest *req) { 190 : const SecurityLoggingObjectKey *key = 191 0 : static_cast<const SecurityLoggingObjectKey *>(req->key.get()); 192 0 : SecurityLoggingObject *slo = new SecurityLoggingObject(key->uuid_); 193 0 : slo->Change(req); 194 0 : return static_cast<DBEntry *>(slo); 195 : } 196 : 197 0 : bool SecurityLoggingObjectTable::OperDBOnChange(DBEntry *entry, 198 : const DBRequest *req) { 199 0 : SecurityLoggingObject *slo = static_cast<SecurityLoggingObject *>(entry); 200 0 : return slo->Change(req); 201 : } 202 : 203 : /* 204 : * Do we need resync 205 : bool SecurityLoggingObjectTable::OperDBResync(DBEntry *entry, const DBRequest *req) { 206 : return OperDBOnChange(entry, req); 207 : } 208 : */ 209 : 210 0 : bool SecurityLoggingObjectTable::OperDBDelete(DBEntry *entry, 211 : const DBRequest *req) { 212 0 : return true; 213 : } 214 : 215 0 : bool SecurityLoggingObjectTable::IFNodeToReq(IFMapNode *node, DBRequest &req, 216 : const boost::uuids::uuid &u) { 217 0 : assert(!u.is_nil()); 218 0 : if ((req.oper == DBRequest::DB_ENTRY_DELETE) || node->IsDeleted()) { 219 0 : req.key.reset(new SecurityLoggingObjectKey(u)); 220 0 : req.oper = DBRequest::DB_ENTRY_DELETE; 221 0 : Enqueue(&req); 222 0 : return false; 223 : } 224 : 225 0 : agent()->config_manager()->AddSecurityLoggingObjectNode(node); 226 0 : return false; 227 : } 228 : 229 0 : bool SecurityLoggingObjectTable::IFNodeToUuid(IFMapNode *node, 230 : boost::uuids::uuid &u) { 231 : autogen::SecurityLoggingObject *cfg = 232 0 : static_cast <autogen::SecurityLoggingObject *> (node->GetObject()); 233 0 : assert(cfg); 234 0 : autogen::IdPermsType id_perms = cfg->id_perms(); 235 0 : CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong, u); 236 0 : return true; 237 0 : } 238 : 239 0 : bool SecurityLoggingObjectTable::ProcessConfig(IFMapNode *node, DBRequest &req, 240 : const boost::uuids::uuid &u) { 241 0 : if (node->IsDeleted()) { 242 0 : return false; 243 : } 244 : 245 0 : req.oper = DBRequest::DB_ENTRY_ADD_CHANGE; 246 0 : req.key.reset(new SecurityLoggingObjectKey(u)); 247 0 : req.data.reset(BuildData(node)); 248 0 : Enqueue(&req); 249 0 : return false; 250 : } 251 : 252 : SecurityLoggingObjectData* 253 0 : SecurityLoggingObjectTable::BuildData(IFMapNode *node) const { 254 : autogen::SecurityLoggingObject *data = 255 0 : static_cast<autogen::SecurityLoggingObject *>(node->GetObject()); 256 : 257 0 : autogen::IdPermsType id_perms = data->id_perms(); 258 : SecurityLoggingObjectData *slo_data = 259 0 : new SecurityLoggingObjectData(agent(), node, data->rules(), 260 0 : data->rate(), 261 0 : id_perms.enable, node->name()); 262 0 : IFMapAgentTable *table = static_cast<IFMapAgentTable *>(node->table()); 263 0 : for (DBGraphVertex::adjacency_iterator iter = 264 0 : node->begin(table->GetGraph()); 265 0 : iter != node->end(table->GetGraph()); ++iter) { 266 : 267 0 : IFMapNode *adj_node = static_cast<IFMapNode *>(iter.operator->()); 268 0 : if (agent()->config_manager()->SkipNode(adj_node)) { 269 0 : continue; 270 : } 271 : 272 0 : if (strcmp(adj_node->table()->Typename(), 273 0 : "firewall-policy-security-logging-object") == 0) { 274 : autogen::FirewallPolicySecurityLoggingObject *fp_slo_link = 275 : static_cast<autogen::FirewallPolicySecurityLoggingObject *> 276 0 : (adj_node->GetObject()); 277 0 : const autogen::SloRateType &slo_rate = fp_slo_link->data(); 278 : IFMapNode *fp_node = agent()->config_manager()-> 279 0 : FindAdjacentIFMapNode(adj_node, "firewall-policy"); 280 0 : if (fp_node) { 281 0 : boost::uuids::uuid fp_uuid = boost::uuids::nil_uuid(); 282 : autogen::FirewallPolicy *fp = 283 0 : static_cast<autogen::FirewallPolicy *>(fp_node->GetObject()); 284 0 : autogen::IdPermsType id_perms = fp->id_perms(); 285 0 : CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong, 286 : fp_uuid); 287 0 : SloRuleInfo info(fp_uuid, slo_rate.rate); 288 0 : slo_data->firewall_policy_list_.push_back(info); 289 0 : } 290 : } 291 : 292 0 : if (strcmp(adj_node->table()->Typename(), 293 0 : "firewall-rule-security-logging-object") == 0) { 294 : autogen::FirewallRuleSecurityLoggingObject *fr_slo_link = 295 : static_cast<autogen::FirewallRuleSecurityLoggingObject *> 296 0 : (adj_node->GetObject()); 297 0 : const autogen::SloRateType &slo_rate = fr_slo_link->data(); 298 : IFMapNode *fr_node = agent()->config_manager()-> 299 0 : FindAdjacentIFMapNode(adj_node, "firewall-rule"); 300 0 : if (fr_node) { 301 0 : boost::uuids::uuid fr_uuid = boost::uuids::nil_uuid(); 302 : autogen::FirewallRule *fr = 303 0 : static_cast<autogen::FirewallRule *>(fr_node->GetObject()); 304 0 : autogen::IdPermsType id_perms = fr->id_perms(); 305 0 : CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong, 306 : fr_uuid); 307 0 : SloRuleInfo info(fr_uuid, slo_rate.rate); 308 0 : slo_data->firewall_rule_list_.push_back(info); 309 0 : } 310 : } 311 : } 312 0 : return slo_data; 313 0 : } 314 : 315 0 : void SLOListReq::HandleRequest() const { 316 0 : AgentSandeshPtr sand(new AgentSecurityLoggingObjectSandesh(context(), 317 0 : get_uuid())); 318 0 : sand->DoSandesh(sand); 319 0 : } 320 : 321 : AgentSandeshPtr 322 0 : SecurityLoggingObjectTable::GetAgentSandesh(const AgentSandeshArguments *args, 323 : const std::string &context) { 324 : return AgentSandeshPtr(new AgentSecurityLoggingObjectSandesh(context, 325 0 : args->GetString("uuid"))); 326 : }