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,13 @@
set(VERSION_VARS_H_FILE ${CMAKE_CURRENT_BINARY_DIR}/version_vars.h)
set(BUILD_SCRIPT build_version_vars_h.py)
add_custom_command(
OUTPUT ${VERSION_VARS_H_FILE}
COMMAND CI_PIPELINE_ID=00000001 CI_BUILD_REF_NAME=open-source python3 ${BUILD_SCRIPT} "userspace" > ${VERSION_VARS_H_FILE}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${BUILD_SCRIPT}
)
add_library(version version.cc ${VERSION_VARS_H_FILE})
target_include_directories(version PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
add_subdirectory(version_ut)

View File

@@ -0,0 +1,52 @@
import os
import getpass
import datetime
import time
import subprocess
import sys
# collect build data
now = datetime.datetime.now()
build_id = "0.0"
is_public = "true"
username = "%s" % getpass.getuser()
timestamp = "%s%s" % (now.replace(microsecond=0).isoformat(), time.strftime("%z"))
version_prefix = "1."
full_version = "%s%s" % (version_prefix, build_id)
branch = os.getenv("CI_BUILD_REF_NAME")
if branch is None:
branch = "private"
# Generate a h file with static varaibles to return the version:
h_code = '''
#ifndef __VERSION_VARS_H__
#define __VERSION_VARS_H__
static const bool is_public = %s;
static const char *id = "%s";
static const char *user = "%s";
static const char *timestamp = "%s";
static const char *version_prefix = "%s";
static const char *version_branch = "%s";
#endif // __VERSION_VARS_H__
'''
kernel_h_code = '''
#ifndef __KERNEL_VERSION_VARS_H__
#define __KERNEL_VERSION_VARS_H__
#define AGENT_FULL_VERSION "%s"
#endif // __KERNEL_VERSION_VARS_H__
'''
if sys.argv[1] == 'print-version-only':
print(full_version)
elif sys.argv[1] == 'kernel':
print(kernel_h_code % (full_version))
else:
print(h_code % (is_public, build_id, username, timestamp, version_prefix, branch))

125
core/version/version.cc Executable file
View File

@@ -0,0 +1,125 @@
// 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 "version.h"
#include "version_vars.h"
#include "singleton.h"
#include "rest.h"
#include <string>
#include <sstream>
using namespace std;
// All variables (is_public, id, user, timestamp, version_prefix) are defined in version_vars.h.
// version_vars.h generated by build_version_vars_h.py at compilation
class VersionRest : public ServerRest
{
public:
void
doCall() override
{
timestamp = Version::getTimestamp();
if (Version::isPublic()) {
type = "public";
version = Version::getFullVersion();
} else {
type = "private";
commit = Version::getID();
user = Version::getUser();
}
}
private:
S2C_PARAM(string, type);
S2C_PARAM(string, timestamp);
S2C_OPTIONAL_PARAM(string, version);
S2C_OPTIONAL_PARAM(string, user);
S2C_OPTIONAL_PARAM(string, commit);
};
void
Version::init()
{
Singleton::Consume<I_RestApi>::by<Version>()->addRestCall<VersionRest>(RestAction::SHOW, "version-info");
Singleton::Consume<I_Environment>::by<Version>()->registerValue<string>("Service Version", getFullVersion());
}
bool
Version::isPublic()
{
return is_public;
}
string
Version::getID()
{
return id;
}
string
Version::getFullVersion()
{
static string version =
(isPublic() ? getVerPrefix() : "") +
getID() +
(isPublic() && getBranch() != "master" ? ("-" + getBranch()) : "");
return version;
}
string
Version::getUser()
{
return user;
}
string
Version::getTimestamp()
{
return timestamp;
}
string
Version::getVerPrefix()
{
return version_prefix;
}
string
Version::getBranch()
{
return version_branch;
}
string
Version::get()
{
ostringstream version_stream;
if (isPublic()) {
version_stream
<< "Type: Public, Version: "
<< getFullVersion()
<< ", Created at: "
<< getTimestamp();
} else {
version_stream
<< "Type: Private, Git ID: "
<< getFullVersion()
<< ", Created at: "
<< getTimestamp()
<< ", Created by: "
<< getUser();
}
return version_stream.str();
}

View File

@@ -0,0 +1,5 @@
add_unit_test(
version_ut
"version_ut.cc"
"version;rest;singleton;environment;event_is;metric;"
)

View File

@@ -0,0 +1,101 @@
#include "version.h"
#include "cptest.h"
#include "mock/mock_rest_api.h"
#include "environment.h"
#include "config.h"
#include "time_proxy.h"
#include "mainloop.h"
using namespace std;
using namespace testing;
TEST(Version, format)
{
// Time format: 2016-11-20T11:09:58+0200
EXPECT_THAT(
Version::getTimestamp(),
ContainsRegex("[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}[-+][0-9]{4}")
);
// "Build 123" or "GitID 7d67870"
EXPECT_THAT(Version::getID(), ContainsRegex("([0-9]+)|[0-9]{4}.([0-9]+)"));
// get() return all parts of information, timestamp and id.
EXPECT_THAT(Version::get(), ContainsRegex("([0-9]+)|[0-9]{4}.([0-9]+)"));
EXPECT_THAT(Version::get(), ContainsRegex("[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}[-+][0-9]{4}"));
}
TEST(Version, getVerPrefix)
{
EXPECT_EQ("1.", Version::getVerPrefix());
}
TEST(Version, getUser)
{
if (Version::isPublic()) {
// public builds call this function but don't use the return value
// ut will do the same, as the user name is not accessible in public builds.
auto user = Version::getUser();
const char* buffer = getenv("CI_BUILD_REF_NAME");
ASSERT_FALSE(!buffer);
EXPECT_THAT(Version::getBranch(), AnyOf(buffer, StartsWith("pipeline")));
} else {
// Version::getUser is define by the python function: getpass.getuser().
// The getuser() function displays the login name of the user.
// This function checks the environment variables LOGNAME, USER, LNAME and USERNAME, in order,
// and returns the value of the first non-empty string.
const char* buffer = getenv("LOGNAME");
if (!buffer) {
buffer = getenv("USER");
if (!buffer) {
buffer = getenv("LNAME");
if (!buffer) {
buffer = getenv("USERNAME");
}
}
}
ASSERT_FALSE(!buffer);
EXPECT_EQ(buffer, Version::getUser());
EXPECT_EQ(Version::getBranch(), "private");
}
}
unique_ptr<ServerRest> show_version;
bool showVersion(const unique_ptr<RestInit> &p) { show_version = p->getRest(); return true; }
TEST(Version, init)
{
StrictMock<MockRestApi> mock_rs;
::Environment env;
EXPECT_CALL(mock_rs, mockRestCall(RestAction::SHOW, "version-info", _)).WillOnce(WithArg<2>(Invoke(showVersion)));
Version::init();
stringstream is;
is << "{}";
auto output = show_version->performRestCall(is);
string res;
if (Version::isPublic()) {
string branch;
if (Version::getBranch() != "master") branch = ("-" + Version::getBranch());
res =
"{\n"
" \"type\": \"public\",\n"
" \"timestamp\": \"" + Version::getTimestamp() + "\",\n"
" \"version\": \"" + Version::getVerPrefix() + Version::getID() + branch + "\"\n"
"}";
} else {
res =
"{\n"
" \"type\": \"private\",\n"
" \"timestamp\": \"" + Version::getTimestamp() + "\",\n"
" \"user\": \"" + Version::getUser() + "\",\n"
" \"commit\": \"" + Version::getID() + "\"\n"
"}";
}
EXPECT_THAT(output, IsValue(res));
}