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