int main(int argc, char *argv[]) { EvalContext *ctx = EvalContextNew(); GenericAgentConfig *config = CheckOpts(ctx, argc, argv); GenericAgentConfigApply(ctx, config); GenericAgentDiscoverContext(ctx, config); Policy *policy = GenericAgentLoadPolicy(ctx, config); if (!policy) { Log(LOG_LEVEL_ERR, "Input files contain errors."); exit(EXIT_FAILURE); } if (SHOWREPORTS) { ShowPromises(policy->bundles, policy->bodies); } switch (config->agent_specific.common.policy_output_format) { case GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_CF: { Policy *output_policy = ParserParseFile(config->input_file, config->agent_specific.common.parser_warnings, config->agent_specific.common.parser_warnings_error); Writer *writer = FileWriter(stdout); PolicyToString(policy, writer); WriterClose(writer); PolicyDestroy(output_policy); } break; case GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_JSON: { Policy *output_policy = ParserParseFile(config->input_file, config->agent_specific.common.parser_warnings, config->agent_specific.common.parser_warnings_error); JsonElement *json_policy = PolicyToJson(output_policy); Writer *writer = FileWriter(stdout); JsonWrite(writer, json_policy, 2); WriterClose(writer); JsonDestroy(json_policy); PolicyDestroy(output_policy); } break; case GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_NONE: break; } GenericAgentConfigDestroy(config); EvalContextDestroy(ctx); }
int main(int argc, char *argv[]) { EvalContext *ctx = EvalContextNew(); GenericAgentConfig *config = CheckOpts(ctx, argc, argv); GenericAgentConfigApply(ctx, config); ReportContext *report_context = OpenReports(config->agent_type); GenericAgentDiscoverContext(ctx, config, report_context); Policy *policy = GenericAgentLoadPolicy(ctx, config->agent_type, config, report_context); if (SHOWREPORTS) { ShowPromises(ctx, policy->bundles, policy->bodies); } CheckLicenses(ctx); switch (config->agent_specific.common.policy_output_format) { case GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_CF: { Policy *output_policy = ParserParseFile(GenericAgentResolveInputPath(config->input_file, config->input_file)); Writer *writer = FileWriter(stdout); PolicyToString(policy, writer); WriterClose(writer); PolicyDestroy(output_policy); } break; case GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_JSON: { Policy *output_policy = ParserParseFile(GenericAgentResolveInputPath(config->input_file, config->input_file)); JsonElement *json_policy = PolicyToJson(output_policy); Writer *writer = FileWriter(stdout); JsonElementPrint(writer, json_policy, 2); WriterClose(writer); JsonElementDestroy(json_policy); PolicyDestroy(output_policy); } break; case GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_NONE: break; } GenericAgentConfigDestroy(config); CloseReports("commmon", report_context); EvalContextDestroy(ctx); if (ERRORCOUNT > 0) { CfOut(OUTPUT_LEVEL_VERBOSE, "", " !! Inputs are invalid\n"); exit(1); } else { CfOut(OUTPUT_LEVEL_VERBOSE, "", " -> Inputs are valid\n"); exit(0); } }
static void test_policy_json_offsets(void) { JsonElement *json = NULL; { Policy *original = LoadPolicy("benchmark.cf"); json = PolicyToJson(original); PolicyDestroy(original); } assert_true(json); JsonElement *json_bundles = JsonObjectGetAsArray(json, "bundles"); { JsonElement *main_bundle = JsonArrayGetAsObject(json_bundles, 0); int line = JsonPrimitiveGetAsInteger(JsonObjectGet(main_bundle, "line")); assert_int_equal(9, line); JsonElement *json_promise_types = JsonObjectGetAsArray(main_bundle, "promiseTypes"); { JsonElement *json_reports_type = JsonArrayGetAsObject(json_promise_types, 0); line = JsonPrimitiveGetAsInteger(JsonObjectGet(json_reports_type, "line")); assert_int_equal(11, line); JsonElement *json_contexts = JsonObjectGetAsArray(json_reports_type, "contexts"); JsonElement *cf_context = JsonArrayGetAsObject(json_contexts, 0); JsonElement *cf_context_promises = JsonObjectGetAsArray(cf_context, "promises"); JsonElement *hello_cf_promise = JsonArrayGetAsObject(cf_context_promises, 0); line = JsonPrimitiveGetAsInteger(JsonObjectGet(hello_cf_promise, "line")); assert_int_equal(13, line); JsonElement *hello_cf_attribs = JsonObjectGetAsArray(hello_cf_promise, "attributes"); { JsonElement *friend_pattern_attrib = JsonArrayGetAsObject(hello_cf_attribs, 0); line = JsonPrimitiveGetAsInteger(JsonObjectGet(friend_pattern_attrib, "line")); assert_int_equal(14, line); } } } JsonElement *json_bodies = JsonObjectGetAsArray(json, "bodies"); { JsonElement *control_body = JsonArrayGetAsObject(json_bodies, 0); int line = JsonPrimitiveGetAsInteger(JsonObjectGet(control_body, "line")); assert_int_equal(4, line); JsonElement *myperms_body = JsonArrayGetAsObject(json_bodies, 1); line = JsonPrimitiveGetAsInteger(JsonObjectGet(myperms_body, "line")); assert_int_equal(28, line); JsonElement *myperms_contexts = JsonObjectGetAsArray(myperms_body, "contexts"); JsonElement *any_context = JsonArrayGetAsObject(myperms_contexts, 0); JsonElement *any_attribs = JsonObjectGetAsArray(any_context, "attributes"); { JsonElement *mode_attrib = JsonArrayGetAsObject(any_attribs, 0); line = JsonPrimitiveGetAsInteger(JsonObjectGet(mode_attrib, "line")); assert_int_equal(30, line); } } JsonDestroy(json); }
static void test_policy_json_to_from(void) { EvalContext *ctx = EvalContextNew(); Policy *policy = NULL; { Policy *original = LoadPolicy("benchmark.cf"); JsonElement *json = PolicyToJson(original); PolicyDestroy(original); policy = PolicyFromJson(json); JsonDestroy(json); } assert_true(policy); assert_int_equal(1, SeqLength(policy->bundles)); assert_int_equal(2, SeqLength(policy->bodies)); { Bundle *main_bundle = PolicyGetBundle(policy, NULL, "agent", "main"); assert_true(main_bundle); { { PromiseType *files = BundleGetPromiseType(main_bundle, "files"); assert_true(files); assert_int_equal(1, SeqLength(files->promises)); for (size_t i = 0; i < SeqLength(files->promises); i++) { Promise *promise = SeqAt(files->promises, i); if (strcmp("/tmp/stuff", promise->promiser) == 0) { assert_string_equal("any", promise->classes); assert_int_equal(2, SeqLength(promise->conlist)); { Constraint *create = PromiseGetConstraint(ctx, promise, "create"); assert_true(create); assert_string_equal("create", create->lval); assert_string_equal("true", RvalScalarValue(create->rval)); } { Constraint *create = PromiseGetConstraint(ctx, promise, "perms"); assert_true(create); assert_string_equal("perms", create->lval); assert_string_equal("myperms", RvalScalarValue(create->rval)); } } else { fprintf(stderr, "Found unknown promise"); fail(); } } } { const char* reportOutput[2] = { "Hello, CFEngine", "Hello, world" }; const char* reportClass[2] = { "cfengine", "any" }; PromiseType *reports = BundleGetPromiseType(main_bundle, "reports"); assert_true(reports); assert_int_equal(2, SeqLength(reports->promises)); for (size_t i = 0; i < SeqLength(reports->promises); i++) { Promise *promise = SeqAt(reports->promises, i); if (strcmp(reportOutput[i], promise->promiser) == 0) { assert_string_equal(reportClass[i], promise->classes); assert_int_equal(1, SeqLength(promise->conlist)); { Constraint *friend_pattern = SeqAt(promise->conlist, 0); assert_true(friend_pattern); assert_string_equal("friend_pattern", friend_pattern->lval); assert_int_equal(RVAL_TYPE_FNCALL, friend_pattern->rval.type); FnCall *fn = RvalFnCallValue(friend_pattern->rval); assert_string_equal("hash", fn->name); assert_int_equal(2, RlistLen(fn->args)); } } else { fprintf(stderr, "Found unknown promise"); fail(); } } } } } { Body *myperms = PolicyGetBody(policy, NULL, "perms", "myperms"); assert_true(myperms); { Seq *mode_cps = BodyGetConstraint(myperms, "mode"); assert_int_equal(1, SeqLength(mode_cps)); Constraint *mode = SeqAt(mode_cps, 0); assert_string_equal("mode", mode->lval); assert_string_equal("555", RvalScalarValue(mode->rval)); } } PolicyDestroy(policy); EvalContextDestroy(ctx); }
int main(int argc, char *argv[]) { GenericAgentConfig *config = CheckOpts(argc, argv); EvalContext *ctx = EvalContextNew(); GenericAgentConfigApply(ctx, config); GenericAgentDiscoverContext(ctx, config); Policy *policy = LoadPolicy(ctx, config); if (!policy) { Log(LOG_LEVEL_ERR, "Input files contain errors."); exit(EXIT_FAILURE); } if (NULL != config->tag_release_dir) { // write the validated file and the release ID bool tagged = GenericAgentTagReleaseDirectory(config, config->tag_release_dir, true, true); if (tagged) { Log(LOG_LEVEL_VERBOSE, "Release tagging done!"); } else { Log(LOG_LEVEL_ERR, "The given directory could not be tagged, sorry."); exit(EXIT_FAILURE); } } if (SHOWREPORTS) { ShowPromises(policy->bundles, policy->bodies); } switch (config->agent_specific.common.policy_output_format) { case GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_CF: { Policy *output_policy = ParserParseFile(AGENT_TYPE_COMMON, config->input_file, config->agent_specific.common.parser_warnings, config->agent_specific.common.parser_warnings_error); Writer *writer = FileWriter(stdout); PolicyToString(policy, writer); WriterClose(writer); PolicyDestroy(output_policy); } break; case GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_JSON: { Policy *output_policy = ParserParseFile(AGENT_TYPE_COMMON, config->input_file, config->agent_specific.common.parser_warnings, config->agent_specific.common.parser_warnings_error); JsonElement *json_policy = PolicyToJson(output_policy); Writer *writer = FileWriter(stdout); JsonWrite(writer, json_policy, 2); WriterClose(writer); JsonDestroy(json_policy); PolicyDestroy(output_policy); } break; case GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_NONE: break; } if(config->agent_specific.common.show_classes) { ShowContextsFormatted(ctx); } if(config->agent_specific.common.show_variables) { ShowVariablesFormatted(ctx); } PolicyDestroy(policy); GenericAgentFinalize(ctx, config); }
int main(int argc, char *argv[]) { SetupSignalsForAgent(); GenericAgentConfig *config = CheckOpts(argc, argv); enum generic_agent_config_common_policy_output_format format = config->agent_specific.common.policy_output_format; if (format == GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_CF || format == GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_JSON) { // Just parse and write content to output Policy *output_policy = ParserParseFile(AGENT_TYPE_COMMON, config->input_file, config->agent_specific.common.parser_warnings, config->agent_specific.common.parser_warnings_error); Writer *writer = FileWriter(stdout); if (format == GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_CF) { PolicyToString(output_policy, writer); } else { JsonElement *json_policy = PolicyToJson(output_policy); JsonWrite(writer, json_policy, 2); JsonDestroy(json_policy); } WriterClose(writer); PolicyDestroy(output_policy); return EXIT_SUCCESS; } EvalContext *ctx = EvalContextNew(); GenericAgentConfigApply(ctx, config); GenericAgentDiscoverContext(ctx, config); Policy *policy = LoadPolicy(ctx, config); if (!policy) { Log(LOG_LEVEL_ERR, "Input files contain errors."); exit(EXIT_FAILURE); } GenericAgentPostLoadInit(ctx); if (NULL != config->tag_release_dir) { // write the validated file and the release ID bool tagged = GenericAgentTagReleaseDirectory(config, config->tag_release_dir, true, true); if (tagged) { Log(LOG_LEVEL_VERBOSE, "Release tagging done!"); } else { Log(LOG_LEVEL_ERR, "The given directory could not be tagged, sorry."); exit(EXIT_FAILURE); } } switch (config->agent_specific.common.policy_output_format) { case GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_CF_FULL: { Writer *writer = FileWriter(stdout); PolicyToString(policy, writer); WriterClose(writer); } break; case GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_JSON_FULL: { Writer *writer = FileWriter(stdout); JsonElement *json_policy = PolicyToJson(policy); JsonWrite(writer, json_policy, 2); JsonDestroy(json_policy); WriterClose(writer); } break; // already handled, but avoids compiler warnings case GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_CF: case GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_JSON: case GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_NONE: break; } if(config->agent_specific.common.show_classes) { ShowContextsFormatted(ctx); } if(config->agent_specific.common.show_variables) { ShowVariablesFormatted(ctx); } PolicyDestroy(policy); GenericAgentFinalize(ctx, config); }