sync code

This commit is contained in:
Ned Wright
2025-08-08 11:06:28 +00:00
parent dd19bf6158
commit da20943c09
145 changed files with 4157 additions and 1016 deletions

View File

@@ -8,7 +8,7 @@ link_directories(${CMAKE_BINARY_DIR}/core)
link_directories(${CMAKE_BINARY_DIR}/core/compression)
SET(EXECUTABLE_NAME "nginx_conf_collector_bin")
add_executable(${EXECUTABLE_NAME} nginx_conf_collector.cc)
add_executable(${EXECUTABLE_NAME} nginx_conf_collector.cc fog_connection.cc)
target_compile_definitions(${EXECUTABLE_NAME} PRIVATE "NGINX_CONF_COLLECTOR_VERSION=\"$ENV{CI_PIPELINE_ID}\"")
target_link_libraries(${EXECUTABLE_NAME}
@@ -26,6 +26,7 @@ target_link_libraries(${EXECUTABLE_NAME}
report
config
environment
curl_http_client
singleton
rest
boost_context

View File

@@ -11,8 +11,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstddef>
#include <iostream>
#include <unistd.h>
#include <sstream>
#include <fstream>
#include <getopt.h>
#include "agent_core_utilities.h"
#include "debug.h"
@@ -20,6 +24,7 @@
#include "mainloop.h"
#include "nginx_utils.h"
#include "time_proxy.h"
#include "fog_connection.h"
using namespace std;
@@ -43,6 +48,7 @@ public:
environment.fini();
time_proxy.fini();
}
private:
ShellCmd shell_cmd;
MainloopComponent mainloop;
@@ -63,12 +69,17 @@ printVersion()
void
printUsage(const char *prog_name)
{
cout << "Usage: " << prog_name << " [-v] [-i /path/to/nginx.conf] [-o /path/to/output.conf]" << '\n';
cout << "Usage: " << prog_name << " [-v] [-i /path/to/nginx.conf] [-o /path/to/output.conf]" <<
" [--upload --token <token> [--fog <address>]]" << '\n';
cout << " -V Print version" << '\n';
cout << " -v Enable verbose output" << '\n';
cout << " -i input_file Specify input file (default is /etc/nginx/nginx.conf)" << '\n';
cout << " -o output_file Specify output file (default is ./full_nginx.conf)" << '\n';
cout << " -h Print this help message" << '\n';
cout << " --upload Upload configuration to FOG (requires --token)" << '\n';
cout << " --token <token> profile token for FOG upload" << '\n';
cout << " --fog <address> FOG server address (default: inext-agents.cloud.ngen.checkpoint.com)" << '\n';
cout << " --proxy <address> Proxy server to send the request through" << '\n';
}
int
@@ -76,9 +87,21 @@ main(int argc, char *argv[])
{
string nginx_input_file = "/etc/nginx/nginx.conf";
string nginx_output_file = "full_nginx.conf";
string fog_address = "inext-agents.cloud.ngen.checkpoint.com";
string token;
string proxy_host;
bool upload_flag = false;
int opt;
while ((opt = getopt(argc, argv, "Vvhi:o:h")) != -1) {
static struct option long_options[] = {
{"upload", no_argument, 0, 'u'},
{"token", required_argument, 0, 1001},
{"fog", required_argument, 0, 1002},
{"proxy", required_argument, 0, 1003},
{0, 0, 0, 0}
};
while ((opt = getopt_long(argc, argv, "Vvhi:o:", long_options, nullptr)) != -1) {
switch (opt) {
case 'V':
printVersion();
@@ -95,18 +118,36 @@ main(int argc, char *argv[])
case 'h':
printUsage(argv[0]);
return 0;
case 'u':
upload_flag = true;
break;
case 1001: // --token
token = optarg;
break;
case 1002: // --fog
fog_address = optarg;
break;
case 1003: // --proxy
proxy_host = optarg;
break;
default:
printUsage(argv[0]);
return 1;
}
}
for (int i = optind; i < argc;) {
for (int i = optind; i < argc; ++i) {
cerr << "Unknown argument: " << argv[i] << '\n';
printUsage(argv[0]);
return 1;
}
if (upload_flag && token.empty()) {
cerr << "Error: --upload requires --token to be specified" << '\n';
printUsage(argv[0]);
return 1;
}
dbgTrace(D_NGINX_MANAGER) << "Starting nginx configuration collector";
MainComponent main_component;
@@ -144,5 +185,43 @@ main(int argc, char *argv[])
cout << "Full nginx configuration file was successfully generated: " << result.unpack() << '\n';
if (upload_flag) {
cout << "Uploading configuration to FOG server: " << fog_address << '\n';
string full_fog_url = fog_address;
if (fog_address.find("http://") != 0 && fog_address.find("https://") != 0) {
full_fog_url = "https://" + fog_address;
}
FogConnection fog_connection(token, full_fog_url);
if (!proxy_host.empty()) {
fog_connection.setProxy(proxy_host);
}
auto credentials_result = fog_connection.getCredentials();
if (!credentials_result.ok()) {
cerr
<< "Failed to register agent with the FOG. with error: "
<< credentials_result.getErr()
<< '\n';
return 1;
}
auto jwt_result = fog_connection.getJWT();
if (!jwt_result.ok()) {
cerr << "Failed to get JWT token. with error:" << jwt_result.getErr() << '\n';
return 1;
}
auto upload_result = fog_connection.uploadNginxConfig(result.unpack());
if (!upload_result.ok()) {
cerr << "Failed to upload nginx config file to FOG. with error:" << upload_result.getErr() << '\n';
return 1;
}
cout << "Successfully uploaded configuration to FOG server." << '\n';
}
return 0;
}