Skip to content

Commit

Permalink
Added new command line option: --ignore-preferred-augments
Browse files Browse the repository at this point in the history
This option causes the agent to ignore def_preferred.json
always reading def.json (old behavior).

Changelog: Commit
Ticket: CFE-3656
Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
  • Loading branch information
olehermanse committed May 26, 2021
1 parent bc84bbd commit 7ec9f73
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 13 deletions.
8 changes: 7 additions & 1 deletion cf-agent/cf-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ static const struct option OPTIONS[] =
{"no-extensions", no_argument, 0, 'E'},
{"timestamp", no_argument, 0, 'l'},
/* Only long option for the rest */
{"ignore-preferred-augments", no_argument, 0, 0},
{"log-modules", required_argument, 0, 0},
{"show-evaluated-classes", optional_argument, 0, 0 },
{"show-evaluated-vars", optional_argument, 0, 0 },
Expand Down Expand Up @@ -231,6 +232,7 @@ static const char *const HINTS[] =
"Enable colorized output. Possible values: 'always', 'auto', 'never'. If option is used, the default value is 'auto'",
"Disable extension loading (used while upgrading)",
"Log timestamps on each line of log output",
"Ignore def_preferred.json file in favor of def.json",
"Enable even more detailed debug logging for specific areas of the implementation. Use together with '-d'. Use --log-modules=help for a list of available modules",
"Show *final* evaluated classes, including those defined in common bundles in policy. Optionally can take a regular expression.",
"Show *final* evaluated variables, including those defined without dependency to user-defined classes in policy. Optionally can take a regular expression.",
Expand Down Expand Up @@ -662,7 +664,11 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
case 0:
{
const char *const option_name = OPTIONS[longopt_idx].name;
if (StringEqual(option_name, "log-modules"))
if (StringEqual(option_name, "ignore-preferred-augments"))
{
config->ignore_preferred_augments = true;
}
else if (StringEqual(option_name, "log-modules"))
{
bool ret = LogEnableModulesFromString(optarg);
if (!ret)
Expand Down
9 changes: 8 additions & 1 deletion cf-execd/cf-execd.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ static const struct option OPTIONS[] =
{"ld-library-path", required_argument, 0, 'L'},
{"color", optional_argument, 0, 'C'},
{"timestamp", no_argument, 0, 'l'},
/* Only long option for the rest */
{"ignore-preferred-augments", no_argument, 0, 0},
{"skip-db-check", optional_argument, 0, 0 },
{"with-runagent-socket", required_argument, 0, 0},
{NULL, 0, 0, '\0'}
Expand All @@ -156,6 +158,7 @@ static const char *const HINTS[] =
"Set the internal value of LD_LIBRARY_PATH for child processes",
"Enable colorized output. Possible values: 'always', 'auto', 'never'. If option is used, the default value is 'auto'",
"Log timestamps on each line of log output",
"Ignore def_preferred.json file in favor of def.json",
"Do not run database integrity checks and repairs at startup",
"Specify the directory for the socket for runagent requests or 'no' to disable the socket",
NULL
Expand Down Expand Up @@ -357,7 +360,11 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
case 0:
{
const char *const option_name = OPTIONS[longopt_idx].name;
if (StringEqual(option_name, "skip-db-check"))
if (StringEqual(option_name, "ignore-preferred-augments"))
{
config->ignore_preferred_augments = true;
}
else if (StringEqual(option_name, "skip-db-check"))
{
if (optarg == NULL)
{
Expand Down
17 changes: 16 additions & 1 deletion cf-monitord/cf-monitord.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ static const struct option OPTIONS[] =
{"tcpdump", no_argument, 0, 'T'},
{"color", optional_argument, 0, 'C'},
{"timestamp", no_argument, 0, 'l'},
/* Only long option for the rest */
{"ignore-preferred-augments", no_argument, 0, 0},
{NULL, 0, 0, '\0'}
};

Expand All @@ -109,6 +111,7 @@ static const char *const HINTS[] =
"Interface with tcpdump if available to collect data about network",
"Enable colorized output. Possible values: 'always', 'auto', 'never'. If option is used, the default value is 'auto'",
"Log timestamps on each line of log output",
"Ignore def_preferred.json file in favor of def.json",
NULL
};

Expand Down Expand Up @@ -144,8 +147,9 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
int c;
GenericAgentConfig *config = GenericAgentConfigNewDefault(AGENT_TYPE_MONITOR, GetTTYInteractive());

int longopt_idx;
while ((c = getopt_long(argc, argv, "dvnIf:g:VSxHTKMFhC::l",
OPTIONS, NULL)) != -1)
OPTIONS, &longopt_idx)) != -1)
{
switch (c)
{
Expand Down Expand Up @@ -231,6 +235,17 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
LoggingEnableTimestamps(true);
break;

/* long options only */
case 0:
{
const char *const option_name = OPTIONS[longopt_idx].name;
if (StringEqual(option_name, "ignore-preferred-augments"))
{
config->ignore_preferred_augments = true;
}
break;
}

default:
{
Writer *w = FileWriter(stdout);
Expand Down
13 changes: 10 additions & 3 deletions cf-promises/cf-promises.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ static const struct option OPTIONS[] =
{"tag-release", required_argument, 0, 'T'},
{"timestamp", no_argument, 0, 'l'},
/* Only long option for the rest */
{"ignore-preferred-augments", no_argument, 0, 0},
{"log-modules", required_argument, 0, 0},
{NULL, 0, 0, '\0'}
};
Expand Down Expand Up @@ -118,6 +119,7 @@ static const char *const HINTS[] =
"Enable colorized output. Possible values: 'always', 'auto', 'never'. If option is used, the default value is 'auto'",
"Tag a directory with promises.cf with cf_promises_validated and cf_promises_release_id",
"Log timestamps on each line of log output",
"Ignore def_preferred.json file in favor of def.json",
"Enable even more detailed debug logging for specific areas of the implementation. Use together with '-d'. Use --log-modules=help for a list of available modules",
NULL
};
Expand Down Expand Up @@ -472,8 +474,13 @@ GenericAgentConfig *CheckOpts(int argc, char **argv)

/* long options only */
case 0:

if (strcmp(OPTIONS[longopt_idx].name, "log-modules") == 0)
{
const char *const option_name = OPTIONS[longopt_idx].name;
if (StringEqual(option_name, "ignore-preferred-augments"))
{
config->ignore_preferred_augments = true;
}
else if (StringEqual(option_name, "log-modules"))
{
bool ret = LogEnableModulesFromString(optarg);
if (!ret)
Expand All @@ -482,7 +489,7 @@ GenericAgentConfig *CheckOpts(int argc, char **argv)
}
}
break;

}
default:
{
Writer *w = FileWriter(stdout);
Expand Down
13 changes: 10 additions & 3 deletions cf-runagent/cf-runagent.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ static const struct option OPTIONS[] =
{"color", optional_argument, 0, 'C'},
{"timestamp", no_argument, 0, 'l'},
/* Only long option for the rest */
{"ignore-preferred-augments", no_argument, 0, 0},
{"log-modules", required_argument, 0, 0},
{"remote-bundles", required_argument, 0, 0},
{NULL, 0, 0, '\0'}
Expand All @@ -139,6 +140,7 @@ static const char *const HINTS[] =
"Connection timeout, seconds",
"Enable colorized output. Possible values: 'always', 'auto', 'never'. If option is used, the default value is 'auto'",
"Log timestamps on each line of log output",
"Ignore def_preferred.json file in favor of def.json",
"Enable even more detailed debug logging for specific areas of the implementation. Use together with '-d'. Use --log-modules=help for a list of available modules",
"Bundles to execute on the remote agent",
NULL
Expand Down Expand Up @@ -442,8 +444,13 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)

/* long options only */
case 0:

if (strcmp(OPTIONS[longopt_idx].name, "log-modules") == 0)
{
const char *const option_name = OPTIONS[longopt_idx].name;
if (StringEqual(option_name, "ignore-preferred-augments"))
{
config->ignore_preferred_augments = true;
}
else if (strcmp(OPTIONS[longopt_idx].name, "log-modules") == 0)
{
bool ret = LogEnableModulesFromString(optarg);
if (!ret)
Expand All @@ -463,7 +470,7 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
}
}
break;

}
default:
{
Writer *w = FileWriter(stdout);
Expand Down
16 changes: 15 additions & 1 deletion cf-serverd/cf-serverd-functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ static const struct option OPTIONS[] =
{"color", optional_argument, 0, 'C'},
{"timestamp", no_argument, 0, 'l'},
{"graceful-detach", optional_argument, 0, 't'},
/* Only long option for the rest */
{"ignore-preferred-augments", no_argument, 0, 0},
{NULL, 0, 0, '\0'}
};

Expand All @@ -119,6 +121,7 @@ static const char *const HINTS[] =
"Enable colorized output. Possible values: 'always', 'auto', 'never'. If option is used, the default value is 'auto'",
"Log timestamps on each line of log output",
"Terminate gracefully on SIGHUP by detaching from systemd and waiting n seconds before terminating",
"Ignore def_preferred.json file in favor of def.json",
NULL
};

Expand Down Expand Up @@ -160,8 +163,9 @@ GenericAgentConfig *CheckOpts(int argc, char **argv)
int c;
GenericAgentConfig *config = GenericAgentConfigNewDefault(AGENT_TYPE_SERVER, GetTTYInteractive());

int longopt_idx;
while ((c = getopt_long(argc, argv, "dvIKf:g:D:N:VSxLFMhAC::lt::",
OPTIONS, NULL))
OPTIONS, &longopt_idx))
!= -1)
{
switch (c)
Expand Down Expand Up @@ -303,6 +307,16 @@ GenericAgentConfig *CheckOpts(int argc, char **argv)
}
break;

/* long options only */
case 0:
{
const char *const option_name = OPTIONS[longopt_idx].name;
if (StringEqual(option_name, "ignore-preferred-augments"))
{
config->ignore_preferred_augments = true;
}
break;
}
default:
{
Writer *w = FileWriter(stdout);
Expand Down
23 changes: 20 additions & 3 deletions libpromises/generic_agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -821,10 +821,21 @@ void LoadAugments(EvalContext *ctx, GenericAgentConfig *config)
{
assert(config != NULL);

char* def_json = StringFormat("%s%c%s", config->input_dir, FILE_SEPARATOR, "def_preferred.json");
if (access(def_json, R_OK) != 0)
char* def_json = NULL;
if (!config->ignore_preferred_augments)
{
free(def_json);
def_json = StringFormat("%s%c%s", config->input_dir, FILE_SEPARATOR, "def_preferred.json");
if (access(def_json, R_OK) != 0)
{
// def_preferred.json does not exist or we cannot read it
FREE_AND_NULL(def_json);
}
}
if (def_json == NULL)
{
// No def_preferred.json, either because the feature is disabled
// or we could not read the file.
// Fall back to old / default behavior, using def.json:
def_json = StringFormat("%s%c%s", config->input_dir, FILE_SEPARATOR, "def.json");
}
Log(LOG_LEVEL_VERBOSE, "Loading JSON augments from '%s' (input dir '%s', input file '%s'", def_json, config->input_dir, config->input_file);
Expand Down Expand Up @@ -1308,6 +1319,11 @@ bool GenericAgentArePromisesValid(const GenericAgentConfig *config)
strlcat(cmd, "\"", CF_BUFSIZE);
}

if (config->ignore_preferred_augments)
{
strlcat(cmd, " --ignore-preferred-augments", CF_BUFSIZE);
}

Log(LOG_LEVEL_VERBOSE, "Checking policy with command '%s'", cmd);

if (!ShellCommandReturnsZero(cmd, true))
Expand Down Expand Up @@ -2329,6 +2345,7 @@ GenericAgentConfig *GenericAgentConfigNewDefault(AgentType agent_type, bool tty_
config->check_runnable = agent_type != AGENT_TYPE_COMMON;
config->ignore_missing_bundles = false;
config->ignore_missing_inputs = false;
config->ignore_preferred_augments = false;

config->heap_soft = NULL;
config->heap_negated = NULL;
Expand Down
1 change: 1 addition & 0 deletions libpromises/generic_agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ typedef struct
// agent state
bool ignore_missing_bundles;
bool ignore_missing_inputs;
bool ignore_preferred_augments; // --ignore-preferred-augments

struct
{
Expand Down

0 comments on commit 7ec9f73

Please sign in to comment.