First release of open-appsec source code

This commit is contained in:
roybarda
2022-10-26 19:33:19 +03:00
parent 3883109caf
commit a883352f79
1353 changed files with 276290 additions and 1 deletions

View File

@@ -0,0 +1,3 @@
add_library(instance_awareness instance_awareness.cc)
add_subdirectory(instance_awareness_ut)

View File

@@ -0,0 +1,93 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "instance_awareness.h"
#include <algorithm>
#include <ctype.h>
#include "debug.h"
#include "config.h"
#include "common.h"
using namespace std;
USE_DEBUG_FLAG(D_CONFIG);
class InstanceAwareness::Impl : public Singleton::Provide<I_InstanceAwareness>::From<InstanceAwareness>
{
public:
Maybe<string>
getInstanceID() override
{
Maybe<string> instance_id = checkIfValueIsConfigured("id");
if (instance_id.ok()) return instance_id;
return genError("Instance Awareness isn't active");
}
Maybe<string>
getFamilyID() override
{
Maybe<string> family_id = checkIfValueIsConfigured("family");
if (family_id.ok()) return family_id;
return genError("Family ID isn't active");
}
Maybe<string>
getUniqueID() override
{
Maybe<string> instance_id(getInstanceID());
if (!instance_id.ok()) return genError("Instance Awareness isn't active");
Maybe<string> family_id(getFamilyID());
if (!family_id.ok()) return *instance_id;
return *family_id + "_" + *instance_id;
}
string getUniqueID(const string &val) override { return getIDWithDefault(getUniqueID(), val); }
string getFamilyID(const string &val) override { return getIDWithDefault(getFamilyID(), val); }
string getInstanceID(const string &val) override { return getIDWithDefault(getInstanceID(), val); }
private:
string
getIDWithDefault(const Maybe<string> &id, const string &default_val)
{
return id.ok() ? *id : default_val;
}
Maybe<string>
checkIfValueIsConfigured(const string &flag)
{
string flag_val = getConfigurationFlag(flag);
if (find_if(flag_val.begin(), flag_val.end(), isBadChar) != flag_val.end()) {
dbgError(D_CONFIG) << "Illegal flag: " << flag << "=" << flag_val;
return genError("Illegal flag: " + flag);
}
if (flag_val == "") {
dbgDebug(D_CONFIG) << "The flag is not configured: " << flag;
return genError("Flag not found");
}
return flag_val;
}
static bool isBadChar(char ch) { return !isalnum(ch) && ch != '-'; }
};
InstanceAwareness::InstanceAwareness() : Component("InstanceAwareness"), pimpl(make_unique<Impl>()) {}
InstanceAwareness::~InstanceAwareness() {}

View File

@@ -0,0 +1,5 @@
add_unit_test(
instance_awareness_ut
"instance_awareness_ut.cc"
"instance_awareness;environment;metric;event_is;-lboost_regex;agent_core_utilities"
)

View File

@@ -0,0 +1,130 @@
#include "instance_awareness.h"
#include <vector>
#include <string>
#include "cptest.h"
#include "config.h"
#include "config_component.h"
#include "environment.h"
using namespace std;
using namespace testing;
class InstanceAwarenessTest : public Test
{
public:
void
init(const vector<string> &args)
{
auto i_config = Singleton::Consume<Config::I_Config>::from(conf);
i_config->loadConfiguration(args);
}
Maybe<string> getInstanceID() { return getInterface()->getInstanceID(); }
Maybe<string> getFamilyID() { return getInterface()->getFamilyID(); }
Maybe<string> getUniqueID() { return getInterface()->getUniqueID(); }
string getInstanceID(const string &str) { return getInterface()->getInstanceID(str); }
string getFamilyID(const string &str) { return getInterface()->getFamilyID(str); }
string getUniqueID(const string &str) { return getInterface()->getUniqueID(str); }
private:
I_InstanceAwareness * getInterface() { return Singleton::Consume<I_InstanceAwareness>::from(inst_aware); }
InstanceAwareness inst_aware;
ConfigComponent conf;
::Environment env;
};
TEST_F(InstanceAwarenessTest, emptyInit)
{
vector<string> args;
init(args);
EXPECT_THAT(getInstanceID(), IsError("Instance Awareness isn't active"));
EXPECT_THAT(getFamilyID(), IsError("Family ID isn't active"));
EXPECT_THAT(getUniqueID(), IsError("Instance Awareness isn't active"));
}
TEST_F(InstanceAwarenessTest, badFamilyID)
{
vector<string> args({"--family=../../../etc/passwd", "--id=9"});
init(args);
EXPECT_THAT(getInstanceID(), IsValue("9"));
EXPECT_THAT(getFamilyID(), IsError("Family ID isn't active"));
EXPECT_THAT(getUniqueID(), IsValue("9"));
}
TEST_F(InstanceAwarenessTest, badInstanceID)
{
vector<string> args({"--family=073b8744b4c5", "--id=../../../etc/passwd"});
init(args);
EXPECT_THAT(getInstanceID(), IsError("Instance Awareness isn't active"));
EXPECT_THAT(getFamilyID(), IsValue("073b8744b4c5"));
EXPECT_THAT(getUniqueID(), IsError("Instance Awareness isn't active"));
}
TEST_F(InstanceAwarenessTest, emptyInstanceID)
{
vector<string> args({"--family=073b8744b4c5"});
init(args);
EXPECT_THAT(getInstanceID(), IsError("Instance Awareness isn't active"));
EXPECT_THAT(getFamilyID(), IsValue("073b8744b4c5"));
EXPECT_THAT(getUniqueID(), IsError("Instance Awareness isn't active"));
}
TEST_F(InstanceAwarenessTest, noInstanceID)
{
vector<string> args({"--family=073b8744b4c5", "--id="});
init(args);
EXPECT_THAT(getInstanceID(), IsError("Instance Awareness isn't active"));
EXPECT_THAT(getFamilyID(), IsValue("073b8744b4c5"));
EXPECT_THAT(getUniqueID(), IsError("Instance Awareness isn't active"));
}
TEST_F(InstanceAwarenessTest, init)
{
vector<string> args({"--family=073b8744b4c5", "--id=9"});
init(args);
EXPECT_THAT(getInstanceID(), IsValue("9"));
EXPECT_THAT(getFamilyID(), IsValue("073b8744b4c5"));
EXPECT_THAT(getUniqueID(), IsValue("073b8744b4c5_9"));
}
TEST_F(InstanceAwarenessTest, initIDOnly)
{
vector<string> args({"--id=9"});
init(args);
EXPECT_THAT(getUniqueID(), IsValue("9"));
EXPECT_THAT(getInstanceID(), IsValue("9"));
EXPECT_THAT(getFamilyID(), IsError("Family ID isn't active"));
}
TEST_F(InstanceAwarenessTest, defaultValues)
{
EXPECT_EQ(getInstanceID("8"), "8");
EXPECT_EQ(getFamilyID("98113aabd3f5"), "98113aabd3f5");
EXPECT_EQ(getUniqueID("98113aabd3f5_8"), "98113aabd3f5_8");
vector<string> args({"--family=073b8744b4c5", "--id=9"});
init(args);
EXPECT_EQ(getInstanceID("8"), "9");
EXPECT_EQ(getFamilyID("98113aabd3f5"), "073b8744b4c5");
EXPECT_EQ(getUniqueID("98113aabd3f5_8"), "073b8744b4c5_9");
}