Skip to content

Commit 139847e

Browse files
authored
Onboarded CE to Telemetry (#1207)
1 parent e05d93c commit 139847e

30 files changed

+130
-10
lines changed

src/common/telemetry/Telemetry.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define _GNU_SOURCE
99
#endif
1010

11+
#include <CommonUtils.h>
1112
#include <Logging.h>
1213
#include <dlfcn.h>
1314
#include <errno.h>

src/modules/complianceengine/src/lib/ComplianceEngineInterface.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "Logging.h"
1313
#include "Mmi.h"
1414
#include "Result.h"
15+
#include "Telemetry.h"
1516

1617
#include <cerrno>
1718
#include <cstddef>
@@ -75,6 +76,7 @@ MMI_HANDLE ComplianceEngineMmiOpen(const char* clientName, const unsigned int ma
7576
if (nullptr == context)
7677
{
7778
OsConfigLogError(g_log, "ComplianceEngineMmiOpen(%s, %u): failed to create context", clientName, maxPayloadSizeBytes);
79+
OSConfigTelemetryStatusTrace("ComplianceEngineMmiOpen", ENOMEM);
7880
return nullptr;
7981
}
8082
std::unique_ptr<ComplianceEngine::PayloadFormatter> formatter;
@@ -99,6 +101,7 @@ MMI_HANDLE ComplianceEngineMmiOpen(const char* clientName, const unsigned int ma
99101
if (error)
100102
{
101103
OsConfigLogError(g_log, "ComplianceEngineMmiOpen(%s, %u): failed to load distribution info: %s", clientName, maxPayloadSizeBytes, error->message.c_str());
104+
OSConfigTelemetryStatusTrace("ComplianceEngineMmiOpen", error->code);
102105
delete engine;
103106
return nullptr;
104107
}
@@ -118,13 +121,15 @@ int ComplianceEngineMmiGetInfo(const char* clientName, char** payload, int* payl
118121
if ((nullptr == payload) || (nullptr == payloadSizeBytes))
119122
{
120123
OsConfigLogError(g_log, "ComplianceEngineMmiGetInfo(%s, %p, %p) called with invalid arguments", clientName, payload, payloadSizeBytes);
124+
OSConfigTelemetryStatusTrace("ComplianceEngineMmiGetInfo", EINVAL);
121125
return EINVAL;
122126
}
123127

124128
*payload = strdup(Engine::GetModuleInfo());
125129
if (!*payload)
126130
{
127131
OsConfigLogError(g_log, "ComplianceEngineMmiGetInfo: failed to duplicate module info");
132+
OSConfigTelemetryStatusTrace("ComplianceEngineMmiGetInfo", ENOMEM);
128133
return ENOMEM;
129134
}
130135

@@ -137,18 +142,21 @@ int ComplianceEngineMmiGet(MMI_HANDLE clientSession, const char* componentName,
137142
if ((nullptr == componentName) || (nullptr == objectName) || (nullptr == payload) || (nullptr == payloadSizeBytes))
138143
{
139144
OsConfigLogError(g_log, "ComplianceEngineMmiGet(%s, %s, %p, %p) called with invalid arguments", componentName, objectName, payload, payloadSizeBytes);
145+
OSConfigTelemetryStatusTrace("ComplianceEngineMmiGet", EINVAL);
140146
return EINVAL;
141147
}
142148

143149
if (nullptr == clientSession)
144150
{
145151
OsConfigLogError(g_log, "ComplianceEngineMmiGet(%s, %s) called outside of a valid session", componentName, objectName);
152+
OSConfigTelemetryStatusTrace("ComplianceEngineMmiGet", EINVAL);
146153
return EINVAL;
147154
}
148155

149156
if (0 != strcmp(componentName, "ComplianceEngine"))
150157
{
151158
OsConfigLogError(g_log, "ComplianceEngineMmiGet called for an unsupported component name (%s)", componentName);
159+
OSConfigTelemetryStatusTrace("ComplianceEngineMmiGet", EINVAL);
152160
return EINVAL;
153161
}
154162
auto& engine = *reinterpret_cast<Engine*>(clientSession);

src/modules/complianceengine/src/lib/Engine.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "Optional.h"
1111
#include "Procedure.h"
1212
#include "Result.h"
13+
#include "Telemetry.h"
1314

1415
#include <cerrno>
1516
#include <cstring>
@@ -66,6 +67,7 @@ Optional<Error> Engine::LoadDistributionInfo()
6667
{
6768
OsConfigLogError(Log(), "ComplianceEngineValidatePayload failed to parse %s: %s", DistributionInfo::cDefaultOverrideFilePath,
6869
overrideInfo.Error().message.c_str());
70+
OSConfigTelemetryStatusTrace("ParseOverrideFile", overrideInfo.Error().code);
6971
return overrideInfo.Error();
7072
}
7173

@@ -80,6 +82,7 @@ Optional<Error> Engine::LoadDistributionInfo()
8082
{
8183
OsConfigLogError(Log(), "ComplianceEngineValidatePayload failed to parse %s: %s", DistributionInfo::cDefaultEtcOsReleasePath,
8284
osReleaseInfo.Error().message.c_str());
85+
OSConfigTelemetryStatusTrace("ParseEtcOsRelease", osReleaseInfo.Error().code);
8386
return osReleaseInfo.Error();
8487
}
8588

@@ -89,6 +92,7 @@ Optional<Error> Engine::LoadDistributionInfo()
8992
{
9093
int status = errno;
9194
OsConfigLogError(Log(), "ComplianceEngineValidatePayload failed to access %s: %s", DistributionInfo::cDefaultOverrideFilePath, strerror(status));
95+
OSConfigTelemetryStatusTrace("stat", status);
9296
return Error("Failed to access override file", status);
9397
}
9498

@@ -203,6 +207,7 @@ Optional<Error> Engine::SetProcedure(const std::string& ruleName, const std::str
203207
if (nullptr == procedure.Audit())
204208
{
205209
OsConfigLogError(Log(), "Failed to copy 'audit' object");
210+
OSConfigTelemetryStatusTrace("Audit", ENOMEM);
206211
return Error("Out of memory");
207212
}
208213

@@ -222,6 +227,7 @@ Optional<Error> Engine::SetProcedure(const std::string& ruleName, const std::str
222227
if (nullptr == procedure.Remediation())
223228
{
224229
OsConfigLogError(Log(), "Failed to copy 'remediate' object");
230+
OSConfigTelemetryStatusTrace("Remediation", ENOMEM);
225231
return Error("Out of memory");
226232
}
227233
}
@@ -243,6 +249,7 @@ Optional<Error> Engine::SetProcedure(const std::string& ruleName, const std::str
243249
if ((nullptr == key) || (nullptr == val))
244250
{
245251
OsConfigLogError(Log(), "Failed to get parameter name and value");
252+
OSConfigTelemetryStatusTrace("json_object_get_string", EINVAL);
246253
return Error("Failed to get parameter name and value");
247254
}
248255

@@ -308,6 +315,7 @@ Result<Status> Engine::MmiSet(const char* objectName, const std::string& payload
308315
if (nullptr == objectName)
309316
{
310317
OsConfigLogError(Log(), "Object name is null");
318+
OSConfigTelemetryStatusTrace("objectName", EINVAL);
311319
return Error("Invalid argument", EINVAL);
312320
}
313321

@@ -345,6 +353,7 @@ Result<Status> Engine::MmiSet(const char* objectName, const std::string& payload
345353
}
346354

347355
OsConfigLogError(Log(), "Invalid object name: Must start with %s, %s or %s prefix", initPrefix, procedurePrefix, remediatePrefix);
356+
OSConfigTelemetryStatusTrace("objectName", EINVAL);
348357
return Error("Invalid object name");
349358
}
350359
} // namespace ComplianceEngine

src/modules/complianceengine/src/lib/FileTreeWalk.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
#include <FileTreeWalk.h>
5+
#include <Telemetry.h>
56
#include <dirent.h>
67

78
namespace ComplianceEngine
@@ -26,6 +27,7 @@ Result<Status> FileTreeWalk(const std::string& path, FtwCallback callback, Break
2627
}
2728

2829
OsConfigLogError(context.GetLogHandle(), "Failed to open directory '%s': %s", path.c_str(), strerror(status));
30+
OSConfigTelemetryStatusTrace("opendir", status);
2931
return Error("Failed to open directory '" + path + "': " + strerror(status), status);
3032
}
3133

@@ -67,6 +69,7 @@ Result<Status> FileTreeWalk(const std::string& path, FtwCallback callback, Break
6769
{
6870
int status = errno;
6971
OsConfigLogError(context.GetLogHandle(), "Failed to lstat '%s': %s", directory.c_str(), strerror(status));
72+
OSConfigTelemetryStatusTrace("lstat", status);
7073
result = Error("Failed to lstat '" + directory + "': " + strerror(status), status);
7174
break;
7275
}
@@ -100,6 +103,7 @@ Result<Status> FileTreeWalk(const std::string& path, FtwCallback callback, Break
100103
if (0 != status)
101104
{
102105
OsConfigLogError(context.GetLogHandle(), "Failed to iterate directory '%s': %s", path.c_str(), strerror(status));
106+
OSConfigTelemetryStatusTrace("readdir", status);
103107
return Error("Failed to iterate directory '" + path + "': " + strerror(status), status);
104108
}
105109

src/modules/complianceengine/src/lib/GroupsIterator.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
#include <GroupsIterator.h>
5+
#include <Telemetry.h>
56
#include <cerrno>
67
#include <pwd.h>
78

@@ -25,6 +26,7 @@ Result<GroupsRange> GroupsRange::Make(std::string path, OsConfigLogHandle logHan
2526
{
2627
int status = errno;
2728
OsConfigLogError(logHandle, "Failed to open file '%s': %s", path.c_str(), strerror(status));
29+
OSConfigTelemetryStatusTrace("fopen", status);
2830
return Error("Failed to create GroupsRange: " + std::string(strerror(status)), status);
2931
}
3032

src/modules/complianceengine/src/lib/KernelModuleTools.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <Evaluator.h>
33
#include <KernelModuleTools.h>
44
#include <Regex.h>
5+
#include <Telemetry.h>
56
#include <dirent.h>
67
#include <fts.h>
78
#include <iostream>
@@ -59,6 +60,7 @@ Result<bool> SearchFilesystemForModuleName(std::string& moduleName, ContextInter
5960
if (!fts)
6061
{
6162
OsConfigLogError(context.GetLogHandle(), "Failed to open %s - errno %d", modulesVersionDir.c_str(), errno);
63+
OSConfigTelemetryStatusTrace("fts_open", errno);
6264
continue;
6365
}
6466
auto ftspDeleter = std::unique_ptr<FTS, int (*)(FTS*)>(fts, fts_close);

src/modules/complianceengine/src/lib/ListValidShells.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
#include <ListValidShells.h>
5+
#include <Telemetry.h>
56
#include <fstream>
67

78
namespace ComplianceEngine
@@ -17,6 +18,7 @@ Result<set<string>> ListValidShells(ContextInterface& context)
1718
if (!shellsFile.is_open())
1819
{
1920
OsConfigLogError(context.GetLogHandle(), "Failed to open %s file", etcShellsPath);
21+
OSConfigTelemetryStatusTrace("fopen", EINVAL);
2022
return Error(std::string("Failed to open ") + etcShellsPath + " file", EINVAL);
2123
}
2224

src/modules/complianceengine/src/lib/LuaEvaluator.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <CommonUtils.h>
99
#include <Result.h>
10+
#include <Telemetry.h>
1011
#include <iostream>
1112
#include <map>
1213
#include <memory>
@@ -75,6 +76,7 @@ Result<Status> LuaEvaluator::Evaluate(const string& script, IndicatorsTree& indi
7576
error += lua_tostring(L, -1);
7677
}
7778
OsConfigLogError(log, "%s", error.c_str());
79+
OSConfigTelemetryStatusTrace("luaL_loadstring", -1);
7880
lua_pop(L, 1);
7981
return Error(error);
8082
}
@@ -86,6 +88,7 @@ Result<Status> LuaEvaluator::Evaluate(const string& script, IndicatorsTree& indi
8688
if (!upvalueName)
8789
{
8890
OsConfigLogError(log, "Could not set restricted Lua environment");
91+
OSConfigTelemetryStatusTrace("lua_setupvalue", -1);
8992
lua_pop(L, 1);
9093
lua_settop(L, 0);
9194
return Error("Could not set restricted Lua environment");
@@ -100,6 +103,7 @@ Result<Status> LuaEvaluator::Evaluate(const string& script, IndicatorsTree& indi
100103
lua_pop(L, 1);
101104
lua_settop(L, 0);
102105
OsConfigLogError(log, "Restricted Lua environment not found");
106+
OSConfigTelemetryStatusTrace("lua_getfield", -1);
103107
return Error("Restricted Lua environment not found");
104108
}
105109

@@ -112,6 +116,7 @@ Result<Status> LuaEvaluator::Evaluate(const string& script, IndicatorsTree& indi
112116
error += lua_tostring(L, -1);
113117
}
114118
OsConfigLogError(log, "%s", error.c_str());
119+
OSConfigTelemetryStatusTrace("lua_pcall", result);
115120
luaL_traceback(L, L, NULL, 1);
116121
const char* traceback = lua_tostring(L, -1);
117122
if (traceback)
@@ -130,6 +135,7 @@ Result<Status> LuaEvaluator::Evaluate(const string& script, IndicatorsTree& indi
130135
{
131136
lua_settop(L, 0);
132137
OsConfigLogError(log, "Lua script did not return a value");
138+
OSConfigTelemetryStatusTrace("lua_gettop", -1);
133139
return Error("Lua script did not return a value");
134140
}
135141

@@ -295,6 +301,7 @@ int LuaEvaluator::LuaProcedureWrapper(lua_State* L)
295301
if (!lua_isstring(L, -1))
296302
{
297303
OsConfigLogError(log, "Failed to get procedure name from upvalue");
304+
OSConfigTelemetryStatusTrace("lua_upvalueindex", -1);
298305
lua_pushstring(L, "Failed to get procedure name from upvalue");
299306
lua_error(L);
300307
return 0;
@@ -305,6 +312,7 @@ int LuaEvaluator::LuaProcedureWrapper(lua_State* L)
305312
if ((callContext->action != ComplianceEngine::Action::Remediate) && (procedureName.substr(0, 9) == "Remediate"))
306313
{
307314
OsConfigLogError(log, "Remediation not allowed in audit mode");
315+
OSConfigTelemetryStatusTrace("action", EPERM);
308316
lua_pushstring(L, "Remediation not allowed in audit mode");
309317
lua_error(L);
310318
return 0;
@@ -314,6 +322,7 @@ int LuaEvaluator::LuaProcedureWrapper(lua_State* L)
314322
if (!lua_islightuserdata(L, -1))
315323
{
316324
OsConfigLogError(log, "Failed to get function pointer from upvalue");
325+
OSConfigTelemetryStatusTrace("lua_islightuserdata", -1);
317326
lua_pushstring(L, "Failed to get function pointer from upvalue");
318327
lua_error(L);
319328
return 0;
@@ -324,6 +333,7 @@ int LuaEvaluator::LuaProcedureWrapper(lua_State* L)
324333
if (!actionFunc)
325334
{
326335
OsConfigLogError(log, "No function for procedure %s", procedureName.c_str());
336+
OSConfigTelemetryStatusTrace("actionFunc", ENOENT);
327337
lua_pushstring(L, ("No function for procedure: " + procedureName).c_str());
328338
lua_error(L);
329339
return 0;

src/modules/complianceengine/src/lib/NetworkTools.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
#include <NetworkTools.h>
5+
#include <Telemetry.h>
56
#include <sstream>
67

78
namespace ComplianceEngine
@@ -64,6 +65,7 @@ Result<std::vector<OpenPort>> GetOpenPorts(ContextInterface& context)
6465
if (pos == std::string::npos)
6566
{
6667
OsConfigLogError(context.GetLogHandle(), "Invalid local address format: %s", local.c_str());
68+
OSConfigTelemetryStatusTrace("rfind", EINVAL);
6769
continue;
6870
}
6971
try
@@ -73,6 +75,7 @@ Result<std::vector<OpenPort>> GetOpenPorts(ContextInterface& context)
7375
catch (const std::exception& e)
7476
{
7577
OsConfigLogError(context.GetLogHandle(), "Invalid port number: %s", local.substr(pos + 1).c_str());
78+
OSConfigTelemetryStatusTrace("stoi", EINVAL);
7679
continue;
7780
}
7881

@@ -102,6 +105,7 @@ Result<std::vector<OpenPort>> GetOpenPorts(ContextInterface& context)
102105
if (r <= 0)
103106
{
104107
OsConfigLogError(context.GetLogHandle(), "Invalid IP address: %s", ip.c_str());
108+
OSConfigTelemetryStatusTrace("inet_pton", EINVAL);
105109
continue;
106110
}
107111
openPort.family = AF_INET6;

src/modules/complianceengine/src/lib/PasswordEntriesIterator.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <Logging.h>
55
#include <PasswordEntriesIterator.h>
6+
#include <Telemetry.h>
67
#include <cerrno>
78
#include <stdexcept>
89

@@ -105,6 +106,7 @@ void PasswordEntryIterator::next() // NOLINT(*-identifier-naming)
105106
}
106107

107108
OsConfigLogError(mRange->GetLogHandle(), "Failed to read /etc/shadow entry: %s (%d)", strerror(status), status);
109+
OSConfigTelemetryStatusTrace("fgetspent_r", status);
108110
throw std::runtime_error("Failed to read /etc/shadow entry: " + string(strerror(status)) + ", errno: " + std::to_string(status));
109111
}
110112
}

0 commit comments

Comments
 (0)