Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #18976: Implement missing calls in network client #3533

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions agent/sources/client/config/agent.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# This file configures the way the Rudder agent connects to its server

# By default it is read from uuid.hive
#my_id = "root"

# By default server is read for policy_server.dat
#server = "rudder.example.com"

Expand All @@ -24,4 +27,12 @@
# Port used for CFEngine protocol (unix only)
#cfengine_port = 5309

# Skip certificate verification. Must not be used in production.
#insecure = false

# For storing temporary files
#tmp_dir = "/var/rudder/tmp"
#tmp_dir = "C:\Program Files\Rudder\tmp"

#policies_dir = "/var/rudder/cfengine-community/inputs/"
#policies_dir = "C:\Program Files\Rudder\policy\"
82 changes: 54 additions & 28 deletions agent/sources/client/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
#define REG_EXTENDED 1
#define REG_ICASE (REG_EXTENDED << 1)

void help(const char* progname, void* upload_report, void* upload_inventory,
void help(const char* progname, void* get_server_id, void* upload_report, void* upload_inventory,
void* argtable_no_action) {
printf("Client for Rudder's communcation protocol\n\n");
printf("USAGE:\n");
printf(" %s", progname);
arg_print_syntax(stdout, get_server_id, "\n");
printf(" %s", progname);
arg_print_syntax(stdout, upload_report, "\n");
printf(" %s", progname);
arg_print_syntax(stdout, upload_inventory, "\n");
Expand Down Expand Up @@ -60,46 +62,60 @@ int start(int argc, char* argv[]) {
// define cli arguments
/////////////////////////////////////////

// The goal here is to provide an interface abstracting the network part
// It should use business names, and hide the communication complexity.

// Adding new subcommands is quite inconvenient

// upload_report [-v] --user=<user> --password=<password> <file>
// common options
// --config local_config_file --policy_config policy_config_file

// get_server_id [-v]
struct arg_rex* get_server_id_cmd =
arg_rex1(NULL, NULL, "get_server_id", NULL, REG_ICASE, NULL);
struct arg_lit* get_server_id_verbose = arg_lit0("v", "verbose", "verbose output");
struct arg_str* get_server_id_config = arg_str0("c", "config", "<file>", "configuration file");
struct arg_end* get_server_id_end = arg_end(20);
void* get_server_id[] = { get_server_id_cmd, get_server_id_verbose, get_server_id_config,
get_server_id_end };
int get_server_id_errors;

*get_server_id_config->sval = DEFAULT_CONF_FILE;

// upload_report [-v] <file>
struct arg_rex* upload_report_cmd =
arg_rex1(NULL, NULL, "upload_report", NULL, REG_ICASE, NULL);
struct arg_lit* upload_report_verbose = arg_lit0("v", "verbose", "verbose output");
struct arg_str* upload_report_config = arg_str0("c", "config", "<file>", "configuration file");
struct arg_str* upload_report_policy_config =
arg_str0("p", "policy_config", "<file>", "policy configuration file");
struct arg_file* upload_report_file = arg_file1(NULL, NULL, NULL, NULL);
struct arg_end* upload_report_end = arg_end(20);
void* upload_report[] = { upload_report_cmd, upload_report_verbose,
upload_report_config, upload_report_policy_config,
upload_report_file, upload_report_end };
void* upload_report[] = { upload_report_cmd, upload_report_verbose, upload_report_config,
upload_report_file, upload_report_end };
int upload_report_errors;

*upload_report_config->sval = DEFAULT_CONF_FILE;
*upload_report_config->sval = DEFAULT_POLICY_CONF_FILE;

// upload_inventory [-v] [--new] [--user=<user>] [--password=<password>] <file>
// upload_inventory [-v] [--new] <file>
struct arg_rex* upload_inventory_cmd =
arg_rex1(NULL, NULL, "upload_inventory", NULL, REG_ICASE, NULL);
struct arg_lit* upload_inventory_verbose = arg_lit0("v", "verbose", "verbose output");
struct arg_str* upload_inventory_config =
arg_str0("c", "config", "<file>", "configuration file");
struct arg_str* upload_inventory_policy_config =
arg_str0("p", "policy_config", "<file>", "policy configuration file");
struct arg_lit* upload_inventory_new = arg_lit0("n", "new", "inventory for a new node");
struct arg_file* upload_inventory_file = arg_file1(NULL, NULL, NULL, NULL);
struct arg_end* upload_inventory_end = arg_end(20);
void* upload_inventory[] = { upload_inventory_cmd, upload_inventory_verbose,
upload_inventory_config, upload_report_policy_config,
upload_inventory_new, upload_inventory_file,
upload_inventory_end };
upload_inventory_config, upload_inventory_new,
upload_inventory_file, upload_inventory_end };
int upload_inventory_errors;

*upload_inventory_config->sval = DEFAULT_CONF_FILE;
*upload_inventory_config->sval = DEFAULT_POLICY_CONF_FILE;

/* no action: [--help] [--version] */
// handles tmp copy and replacement
// update_policies [-v]
// FIXME ifdef windows

// no action: [--help] [--version]
struct arg_lit* no_action_help = arg_lit0("h", "help", "print this help and exit");
struct arg_lit* no_action_version =
arg_lit0("V", "version", "print version information and exit");
Expand All @@ -111,8 +127,8 @@ int start(int argc, char* argv[]) {
int exitcode = EXIT_SUCCESS;

/* verify all argtable[] entries were allocated successfully */
if (arg_nullcheck(upload_report) != 0 || arg_nullcheck(upload_inventory) != 0
|| arg_nullcheck(no_action) != 0) {
if (arg_nullcheck(get_server_id) != 0 || arg_nullcheck(upload_report) != 0
|| arg_nullcheck(upload_inventory) != 0 || arg_nullcheck(no_action) != 0) {
/* NULL entries were detected, some allocations must have failed */
printf("%s: insufficient memory\n", progname);
exitcode = 1;
Expand All @@ -123,18 +139,19 @@ int start(int argc, char* argv[]) {
// try the different argument parsers
/////////////////////////////////////////

get_server_id_errors = arg_parse(argc, argv, get_server_id);
upload_report_errors = arg_parse(argc, argv, upload_report);
upload_inventory_errors = arg_parse(argc, argv, upload_inventory);
no_action_errors = arg_parse(argc, argv, no_action);

// help and version are special and treated first
if (no_action_errors == 0) {
if (no_action_help->count > 0) {
help(progname, upload_report, upload_inventory, no_action);
help(progname, get_server_id, upload_report, upload_inventory, no_action);
} else if (no_action_version->count > 0) {
version(progname);
} else {
help(progname, upload_report, upload_inventory, no_action);
help(progname, get_server_id, upload_report, upload_inventory, no_action);
exitcode = EXIT_FAILURE;
}
goto exit;
Expand All @@ -145,17 +162,22 @@ int start(int argc, char* argv[]) {
const char* config_file = NULL;
const char* policy_config_file = NULL;

if (upload_report_errors == 0) {
if (get_server_id_errors == 0) {
verbose = get_server_id_verbose->count > 0;
config_file = *get_server_id_config->sval;
} else if (upload_report_errors == 0) {
verbose = upload_report_verbose->count > 0;
config_file = *upload_report_config->sval;
policy_config_file = *upload_report_policy_config->sval;
} else if (upload_inventory_errors == 0) {
verbose = upload_inventory_verbose->count > 0;
config_file = *upload_inventory_config->sval;
policy_config_file = *upload_inventory_policy_config->sval;
} else {
/* We get here if the command line matched none of the possible syntaxes */
if (upload_report_cmd->count > 0) {
if (get_server_id_cmd->count > 0) {
arg_print_errors(stdout, get_server_id_end, progname);
printf("usage: %s ", progname);
arg_print_syntax(stdout, get_server_id, "\n");
} else if (upload_report_cmd->count > 0) {
arg_print_errors(stdout, upload_report_end, progname);
printf("usage: %s ", progname);
arg_print_syntax(stdout, upload_report, "\n");
Expand All @@ -164,7 +186,7 @@ int start(int argc, char* argv[]) {
printf("usage: %s ", progname);
arg_print_syntax(stdout, upload_inventory, "\n");
} else {
help(progname, upload_report, upload_inventory, no_action);
help(progname, get_server_id, upload_report, upload_inventory, no_action);
}
exitcode = EXIT_FAILURE;
goto exit;
Expand All @@ -187,14 +209,18 @@ int start(int argc, char* argv[]) {
// make actions
/////////////////////////////////////////

if (upload_report_errors == 0) {
upload_file(config, verbose, *upload_report_file->filename, UploadReport, false);
if (get_server_id_errors == 0) {
exitcode = get_id(config, verbose);
} else if (upload_report_errors == 0) {
exitcode = upload_file(config, verbose, *upload_report_file->filename, UploadReport, false);
} else if (upload_inventory_errors == 0) {
bool is_new = upload_inventory_new->count > 0;
upload_file(config, verbose, *upload_report_file->filename, UploadInventory, is_new);
exitcode =
upload_file(config, verbose, *upload_report_file->filename, UploadInventory, is_new);
}

exit:
arg_freetable(get_server_id, sizeof(get_server_id) / sizeof(get_server_id[0]));
arg_freetable(upload_inventory, sizeof(upload_inventory) / sizeof(upload_inventory[0]));
arg_freetable(upload_report, sizeof(upload_report) / sizeof(upload_report[0]));
arg_freetable(no_action, sizeof(no_action) / sizeof(no_action[0]));
Expand Down
Loading