icinga2/lib/redis/rediswriter-utility.cpp

112 lines
3.5 KiB
C++
Raw Normal View History

2017-09-25 14:41:43 +02:00
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#include "redis/rediswriter.hpp"
#include "icinga/customvarobject.hpp"
#include "base/object-packer.hpp"
2017-09-25 14:41:43 +02:00
#include "base/logger.hpp"
#include "base/serializer.hpp"
#include "base/tlsutility.hpp"
#include "base/initialize.hpp"
#include "base/objectlock.hpp"
2017-09-25 14:41:43 +02:00
using namespace icinga;
String RedisWriter::FormatCheckSumBinary(const String& str)
{
char output[20*2+1];
for (int i = 0; i < 20; i++)
sprintf(output + 2 * i, "%02x", str[i]);
return output;
}
2017-10-02 10:05:56 +02:00
String RedisWriter::CalculateCheckSumString(const String& str)
2017-09-25 14:41:43 +02:00
{
2017-10-02 10:05:56 +02:00
return SHA1(str);
2017-09-25 14:41:43 +02:00
}
2017-10-02 10:05:56 +02:00
String RedisWriter::CalculateCheckSumGroups(const Array::Ptr& groups)
2017-09-25 14:41:43 +02:00
{
/* Ensure that checksums happen in a defined order. */
Array::Ptr tmpGroups = groups->ShallowClone();
tmpGroups->Sort();
return SHA1(PackObject(tmpGroups));
2017-09-25 14:41:43 +02:00
}
String RedisWriter::CalculateCheckSumProperties(const ConfigObject::Ptr& object, const std::set<String>& propertiesBlacklist)
2017-09-25 14:41:43 +02:00
{
//TODO: consider precision of 6 for double values; use specific config fields for hashing?
return HashValue(object, propertiesBlacklist);
2017-09-25 14:41:43 +02:00
}
2017-10-02 10:05:56 +02:00
String RedisWriter::CalculateCheckSumVars(const CustomVarObject::Ptr& object)
2017-09-25 14:41:43 +02:00
{
2017-10-02 10:05:56 +02:00
Dictionary::Ptr vars = object->GetVars();
2017-09-25 14:41:43 +02:00
if (!vars)
2017-10-02 10:05:56 +02:00
return HashValue(Empty);
2017-09-25 14:41:43 +02:00
2017-10-02 10:05:56 +02:00
return HashValue(vars);
2017-09-25 14:41:43 +02:00
}
static const std::set<String> propertiesBlacklistEmpty;
2017-10-02 10:05:56 +02:00
String RedisWriter::HashValue(const Value& value)
{
return HashValue(value, propertiesBlacklistEmpty);
}
String RedisWriter::HashValue(const Value& value, const std::set<String>& propertiesBlacklist)
2017-09-25 14:41:43 +02:00
{
Value temp;
bool mutabl;
2017-09-25 14:41:43 +02:00
Type::Ptr type = value.GetReflectionType();
if (ConfigObject::TypeInstance->IsAssignableFrom(type)) {
2017-09-25 14:41:43 +02:00
temp = Serialize(value, FAConfig);
mutabl = true;
} else {
2017-09-25 14:41:43 +02:00
temp = value;
mutabl = false;
}
if (propertiesBlacklist.size() && temp.IsObject()) {
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>((Object::Ptr)temp);
if (dict) {
if (!mutabl)
dict = dict->ShallowClone();
ObjectLock olock(dict);
for (auto& property : propertiesBlacklist)
dict->Remove(property);
if (!mutabl)
temp = dict;
}
}
2017-09-25 14:41:43 +02:00
return SHA1(PackObject(temp));
2017-09-25 14:41:43 +02:00
}