From cb694fe3fb34cae7de8810dd7e3e9b956042c437 Mon Sep 17 00:00:00 2001 From: "anna.cai" Date: Thu, 9 Jul 2026 19:50:54 -0400 Subject: [PATCH 1/2] expose current policy id via eval_ctx getter and regenerate C schema headers --- c/include/dd/policies/eval_ctx.h | 6 +++++ c/src/eval_ctx.c | 9 +++++++ c/src/eval_ctx.h | 9 +++++++ c/src/evaluator.c | 2 ++ c/src/schema/policy_builder.h | 35 +++++-------------------- c/src/schema/policy_reader.h | 45 +++----------------------------- c/src/schema/policy_verifier.h | 42 +---------------------------- c/src/test/test_eval_ctx.c | 12 +++++++++ 8 files changed, 49 insertions(+), 111 deletions(-) diff --git a/c/include/dd/policies/eval_ctx.h b/c/include/dd/policies/eval_ctx.h index e4316ab..971c425 100644 --- a/c/include/dd/policies/eval_ctx.h +++ b/c/include/dd/policies/eval_ctx.h @@ -172,6 +172,12 @@ plcs_errors plcs_eval_ctx_get_last_error(void); */ plcs_errors plcs_eval_ctx_peek_last_error(void); +/** + * @brief Gets the id of the policy currently being evaluated. + * @return the current policy's id, or PLCS_STR_NOT_SET (NULL) if none is being evaluated. + */ +const char *plcs_eval_ctx_get_current_policy_id(void); + /** * @brief Initializes the context model. * This function sets all evaluator function pointers to NULL and parameters to diff --git a/c/src/eval_ctx.c b/c/src/eval_ctx.c index a49b6c9..5cdc51a 100644 --- a/c/src/eval_ctx.c +++ b/c/src/eval_ctx.c @@ -94,6 +94,10 @@ plcs_errors plcs_eval_ctx_register_action(plcs_action_function_ptr action, plcs_ return PLCS_ESUCCESS; } +void plcs_eval_ctx_set_current_policy_id(const char *policy_id) { + ctx.current_policy_id = policy_id; +} + plcs_action_function_ptr plcs_eval_ctx_get_action(plcs_actions ix) { if (ix < 0 || ix >= PLCS_ACTIONS__COUNT) { ctx.error = PLCS_EIX_OVERFLOW; @@ -212,6 +216,10 @@ plcs_errors plcs_eval_ctx_peek_last_error(void) { return ctx.error; } +const char *plcs_eval_ctx_get_current_policy_id(void) { + return ctx.current_policy_id; +} + plcs_errors plcs_eval_ctx_get_last_error(void) { plcs_errors error = ctx.error; // reset @@ -244,6 +252,7 @@ void plcs_eval_ctx_reset(void) { } ctx.error = PLCS_ESUCCESS; + ctx.current_policy_id = PLCS_STR_NOT_SET; } plcs_errors plcs_eval_ctx_init(void) { diff --git a/c/src/eval_ctx.h b/c/src/eval_ctx.h index f4ff4a5..7eeef1c 100644 --- a/c/src/eval_ctx.h +++ b/c/src/eval_ctx.h @@ -111,6 +111,9 @@ typedef struct plcs_eval_ctx { /**< TODO: consider implementing this as a stack to preserve history of errors */ plcs_errors error; + /**< The id of the policy currently being evaluated */ + const char *current_policy_id; + } plcs_eval_ctx; /** @@ -119,6 +122,12 @@ typedef struct plcs_eval_ctx { */ void plcs_eval_ctx_set_error(plcs_errors error); +/** + * @brief Sets the id of the policy currently being evaluated. + * @param policy_id the policy's id, or PLCS_STR_NOT_SET (NULL) if none. + */ +void plcs_eval_ctx_set_current_policy_id(const char *policy_id); + /** * @brief Sets an error code for an action * @param ix An action ID from plcs_actions enum diff --git a/c/src/evaluator.c b/c/src/evaluator.c index a2875ed..125509d 100644 --- a/c/src/evaluator.c +++ b/c/src/evaluator.c @@ -289,6 +289,8 @@ plcs_errors evaluate_policy(dd_ns(Policy_table_t) policy) { // // evaluate rules if they exist, otherwise return EVAL_RESULT_ABSTAIN plcs_evaluation_result eval_res = rules ? evaluate_rules(rules, 0) : PLCS_EVAL_RESULT_ABSTAIN; + plcs_eval_ctx_set_current_policy_id(dd_ns(Policy_id)(policy)); + // perform actions given evaluation result return perform_actions(eval_res, actions); } diff --git a/c/src/schema/policy_builder.h b/c/src/schema/policy_builder.h index 87b9a5e..f101e52 100644 --- a/c/src/schema/policy_builder.h +++ b/c/src/schema/policy_builder.h @@ -23,29 +23,6 @@ #define flatbuffers_extension "bin" #endif -#define __dd_wls_UUID_formal_args , uint64_t v0, uint64_t v1 -#define __dd_wls_UUID_call_args , v0, v1 -static inline dd_wls_UUID_t *dd_wls_UUID_assign(dd_wls_UUID_t *p, uint64_t v0, uint64_t v1) -{ p->hi = v0; p->lo = v1; - return p; } -static inline dd_wls_UUID_t *dd_wls_UUID_copy(dd_wls_UUID_t *p, const dd_wls_UUID_t *p2) -{ p->hi = p2->hi; p->lo = p2->lo; - return p; } -static inline dd_wls_UUID_t *dd_wls_UUID_assign_to_pe(dd_wls_UUID_t *p, uint64_t v0, uint64_t v1) -{ flatbuffers_uint64_assign_to_pe(&p->hi, v0); flatbuffers_uint64_assign_to_pe(&p->lo, v1); - return p; } -static inline dd_wls_UUID_t *dd_wls_UUID_copy_to_pe(dd_wls_UUID_t *p, const dd_wls_UUID_t *p2) -{ flatbuffers_uint64_copy_to_pe(&p->hi, &p2->hi); flatbuffers_uint64_copy_to_pe(&p->lo, &p2->lo); - return p; } -static inline dd_wls_UUID_t *dd_wls_UUID_assign_from_pe(dd_wls_UUID_t *p, uint64_t v0, uint64_t v1) -{ flatbuffers_uint64_assign_from_pe(&p->hi, v0); flatbuffers_uint64_assign_from_pe(&p->lo, v1); - return p; } -static inline dd_wls_UUID_t *dd_wls_UUID_copy_from_pe(dd_wls_UUID_t *p, const dd_wls_UUID_t *p2) -{ flatbuffers_uint64_copy_from_pe(&p->hi, &p2->hi); flatbuffers_uint64_copy_from_pe(&p->lo, &p2->lo); - return p; } -__flatbuffers_build_struct(flatbuffers_, dd_wls_UUID, 16, 8, dd_wls_UUID_file_identifier, dd_wls_UUID_type_identifier) -__flatbuffers_define_fixed_array_primitives(flatbuffers_, dd_wls_UUID, dd_wls_UUID_t) - static const flatbuffers_voffset_t __dd_wls_Policy_required[] = { 0 }; typedef flatbuffers_ref_t dd_wls_Policy_ref_t; static dd_wls_Policy_ref_t dd_wls_Policy_clone(flatbuffers_builder_t *B, dd_wls_Policy_table_t t); @@ -57,7 +34,7 @@ static dd_wls_Policies_ref_t dd_wls_Policies_clone(flatbuffers_builder_t *B, dd_ __flatbuffers_build_table(flatbuffers_, dd_wls_Policies, 1) #define __dd_wls_Policy_formal_args ,\ - flatbuffers_string_ref_t v0, dd_wls_NodeTypeWrapper_ref_t v1, dd_wls_Action_vec_ref_t v2, dd_wls_UUID_t *v3, int64_t v4 + flatbuffers_string_ref_t v0, dd_wls_NodeTypeWrapper_ref_t v1, dd_wls_Action_vec_ref_t v2, flatbuffers_string_ref_t v3, int64_t v4 #define __dd_wls_Policy_call_args ,\ v0, v1, v2, v3, v4 static inline dd_wls_Policy_ref_t dd_wls_Policy_create(flatbuffers_builder_t *B __dd_wls_Policy_formal_args); @@ -71,17 +48,17 @@ __flatbuffers_build_table_prolog(flatbuffers_, dd_wls_Policies, dd_wls_Policies_ __flatbuffers_build_string_field(0, flatbuffers_, dd_wls_Policy_description, dd_wls_Policy) __flatbuffers_build_table_field(1, flatbuffers_, dd_wls_Policy_rules, dd_wls_NodeTypeWrapper, dd_wls_Policy) __flatbuffers_build_table_vector_field(2, flatbuffers_, dd_wls_Policy_actions, dd_wls_Action, dd_wls_Policy) -__flatbuffers_build_struct_field(3, flatbuffers_, dd_wls_Policy_id, dd_wls_UUID, 16, 8, dd_wls_Policy) +__flatbuffers_build_string_field(3, flatbuffers_, dd_wls_Policy_id, dd_wls_Policy) __flatbuffers_build_scalar_field(4, flatbuffers_, dd_wls_Policy_version, flatbuffers_int64, int64_t, 8, 8, INT64_C(0), dd_wls_Policy) static inline dd_wls_Policy_ref_t dd_wls_Policy_create(flatbuffers_builder_t *B __dd_wls_Policy_formal_args) { if (dd_wls_Policy_start(B) - || dd_wls_Policy_id_add(B, v3) || dd_wls_Policy_version_add(B, v4) || dd_wls_Policy_description_add(B, v0) || dd_wls_Policy_rules_add(B, v1) - || dd_wls_Policy_actions_add(B, v2)) { + || dd_wls_Policy_actions_add(B, v2) + || dd_wls_Policy_id_add(B, v3)) { return 0; } return dd_wls_Policy_end(B); @@ -91,11 +68,11 @@ static dd_wls_Policy_ref_t dd_wls_Policy_clone(flatbuffers_builder_t *B, dd_wls_ { __flatbuffers_memoize_begin(B, t); if (dd_wls_Policy_start(B) - || dd_wls_Policy_id_pick(B, t) || dd_wls_Policy_version_pick(B, t) || dd_wls_Policy_description_pick(B, t) || dd_wls_Policy_rules_pick(B, t) - || dd_wls_Policy_actions_pick(B, t)) { + || dd_wls_Policy_actions_pick(B, t) + || dd_wls_Policy_id_pick(B, t)) { return 0; } __flatbuffers_memoize_end(B, t, dd_wls_Policy_end(B)); diff --git a/c/src/schema/policy_reader.h b/c/src/schema/policy_reader.h index 89b1687..1fef010 100644 --- a/c/src/schema/policy_reader.h +++ b/c/src/schema/policy_reader.h @@ -24,11 +24,6 @@ #define flatbuffers_extension "bin" #endif -typedef struct dd_wls_UUID dd_wls_UUID_t; -typedef const dd_wls_UUID_t *dd_wls_UUID_struct_t; -typedef dd_wls_UUID_t *dd_wls_UUID_mutable_struct_t; -typedef const dd_wls_UUID_t *dd_wls_UUID_vec_t; -typedef dd_wls_UUID_t *dd_wls_UUID_mutable_vec_t; typedef const struct dd_wls_Policy_table *dd_wls_Policy_table_t; typedef struct dd_wls_Policy_table *dd_wls_Policy_mutable_table_t; @@ -38,18 +33,6 @@ typedef const struct dd_wls_Policies_table *dd_wls_Policies_table_t; typedef struct dd_wls_Policies_table *dd_wls_Policies_mutable_table_t; typedef const flatbuffers_uoffset_t *dd_wls_Policies_vec_t; typedef flatbuffers_uoffset_t *dd_wls_Policies_mutable_vec_t; -#ifndef dd_wls_UUID_file_identifier -#define dd_wls_UUID_file_identifier 0 -#endif -/* deprecated, use dd_wls_UUID_file_identifier */ -#ifndef dd_wls_UUID_identifier -#define dd_wls_UUID_identifier 0 -#endif -#define dd_wls_UUID_type_hash ((flatbuffers_thash_t)0x23726554) -#define dd_wls_UUID_type_identifier "\x54\x65\x72\x23" -#ifndef dd_wls_UUID_file_extension -#define dd_wls_UUID_file_extension "bin" -#endif #ifndef dd_wls_Policy_file_identifier #define dd_wls_Policy_file_identifier 0 #endif @@ -76,27 +59,6 @@ typedef flatbuffers_uoffset_t *dd_wls_Policies_mutable_vec_t; #endif -/** unfortunetly flatbuffers doesnt support fixed size arrays in go so we can't use it - * the solution is to split it into two unsigned longs :( - * this should be populate after caculation, and removed prior to verification. */ -struct dd_wls_UUID { - alignas(8) uint64_t hi; - alignas(8) uint64_t lo; -}; -static_assert(sizeof(dd_wls_UUID_t) == 16, "struct size mismatch"); - -static inline const dd_wls_UUID_t *dd_wls_UUID__const_ptr_add(const dd_wls_UUID_t *p, size_t i) { return p + i; } -static inline dd_wls_UUID_t *dd_wls_UUID__ptr_add(dd_wls_UUID_t *p, size_t i) { return p + i; } -static inline dd_wls_UUID_struct_t dd_wls_UUID_vec_at(dd_wls_UUID_vec_t vec, size_t i) -__flatbuffers_struct_vec_at(vec, i) -static inline size_t dd_wls_UUID__size(void) { return 16; } -static inline size_t dd_wls_UUID_vec_len(dd_wls_UUID_vec_t vec) -__flatbuffers_vec_len(vec) -__flatbuffers_struct_as_root(dd_wls_UUID) - -__flatbuffers_define_struct_scalar_field(dd_wls_UUID, hi, flatbuffers_uint64, uint64_t) -__flatbuffers_define_struct_scalar_field(dd_wls_UUID, lo, flatbuffers_uint64, uint64_t) - /** Represents a policy that contains rules and actions. * The rules are represented as a tree structure where each node can be a leaf (with an evaluator) or a root (with child nodes). */ @@ -114,9 +76,10 @@ __flatbuffers_define_string_field(0, dd_wls_Policy, description, 0) __flatbuffers_define_table_field(1, dd_wls_Policy, rules, dd_wls_NodeTypeWrapper_table_t, 0) /** The actions are taken post evaluation of the policy. */ __flatbuffers_define_vector_field(2, dd_wls_Policy, actions, dd_wls_Action_vec_t, 0) -/** 128 bit UUID represented as 16 raw bytes - * this can be used to verify integrity (but NOT origin - this is not a signature!) */ -__flatbuffers_define_struct_field(3, dd_wls_Policy, id, dd_wls_UUID_struct_t, 0) +/** Stable identifier for this policy, used to attribute an evaluation outcome + * back to its source. For Remote Config policies this is the rule's UUID; + * for hardcoded/built-in policies it is the sentinel "hardcoded". */ +__flatbuffers_define_string_field(3, dd_wls_Policy, id, 0) /** used to track the version number of this policy */ __flatbuffers_define_scalar_field(4, dd_wls_Policy, version, flatbuffers_int64, int64_t, INT64_C(0)) diff --git a/c/src/schema/policy_verifier.h b/c/src/schema/policy_verifier.h index 5735042..acebeef 100644 --- a/c/src/schema/policy_verifier.h +++ b/c/src/schema/policy_verifier.h @@ -18,53 +18,13 @@ static int dd_wls_Policy_verify_table(flatcc_table_verifier_descriptor_t *td); static int dd_wls_Policies_verify_table(flatcc_table_verifier_descriptor_t *td); -static inline int dd_wls_UUID_verify_as_root(const void *buf, size_t bufsiz) -{ - return flatcc_verify_struct_as_root(buf, bufsiz, dd_wls_UUID_identifier, 16, 8); -} - -static inline int dd_wls_UUID_verify_as_root_with_size(const void *buf, size_t bufsiz) -{ - return flatcc_verify_struct_as_root_with_size(buf, bufsiz, dd_wls_UUID_identifier, 16, 8); -} - -static inline int dd_wls_UUID_verify_as_typed_root(const void *buf, size_t bufsiz) -{ - return flatcc_verify_struct_as_typed_root(buf, bufsiz, dd_wls_UUID_type_hash, 16, 8); -} - -static inline int dd_wls_UUID_verify_as_typed_root_with_size(const void *buf, size_t bufsiz) -{ - return flatcc_verify_struct_as_typed_root_with_size(buf, bufsiz, dd_wls_UUID_type_hash, 16, 8); -} - -static inline int dd_wls_UUID_verify_as_root_with_type_hash(const void *buf, size_t bufsiz, flatbuffers_thash_t thash) -{ - return flatcc_verify_struct_as_typed_root(buf, bufsiz, thash, 16, 8); -} - -static inline int dd_wls_UUID_verify_as_root_with_type_hash_and_size(const void *buf, size_t bufsiz, flatbuffers_thash_t thash) -{ - return flatcc_verify_struct_as_typed_root_with_size(buf, bufsiz, thash, 16, 8); -} - -static inline int dd_wls_UUID_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const char *fid) -{ - return flatcc_verify_struct_as_root(buf, bufsiz, fid, 16, 8); -} - -static inline int dd_wls_UUID_verify_as_root_with_identifier_and_size(const void *buf, size_t bufsiz, const char *fid) -{ - return flatcc_verify_struct_as_root_with_size(buf, bufsiz, fid, 16, 8); -} - static int dd_wls_Policy_verify_table(flatcc_table_verifier_descriptor_t *td) { int ret; if ((ret = flatcc_verify_string_field(td, 0, 0) /* description */)) return ret; if ((ret = flatcc_verify_table_field(td, 1, 0, &dd_wls_NodeTypeWrapper_verify_table) /* rules */)) return ret; if ((ret = flatcc_verify_table_vector_field(td, 2, 0, &dd_wls_Action_verify_table) /* actions */)) return ret; - if ((ret = flatcc_verify_field(td, 3, 16, 8) /* id */)) return ret; + if ((ret = flatcc_verify_string_field(td, 3, 0) /* id */)) return ret; if ((ret = flatcc_verify_field(td, 4, 8, 8) /* version */)) return ret; return flatcc_verify_ok; } diff --git a/c/src/test/test_eval_ctx.c b/c/src/test/test_eval_ctx.c index 4136246..1ac5675 100644 --- a/c/src/test/test_eval_ctx.c +++ b/c/src/test/test_eval_ctx.c @@ -298,6 +298,18 @@ UTEST(eval_ctx, last_error_set_and_get) { ASSERT_EQ(plcs_eval_ctx_peek_last_error(), (plcs_errors)PLCS_ESUCCESS); } +UTEST(eval_ctx, current_policy_id_set_and_get) { + (void)plcs_eval_ctx_init(); + + ASSERT_TRUE(plcs_eval_ctx_get_current_policy_id() == PLCS_STR_NOT_SET); + + plcs_eval_ctx_set_current_policy_id("some-policy-id"); + ASSERT_STREQ(plcs_eval_ctx_get_current_policy_id(), "some-policy-id"); + + plcs_eval_ctx_reset(); + ASSERT_TRUE(plcs_eval_ctx_get_current_policy_id() == PLCS_STR_NOT_SET); +} + UTEST(eval_ctx, set_error_out_of_bound) { plcs_eval_ctx_set_action_error(PLCS_ACTIONS__COUNT, 0); int err = plcs_eval_ctx_get_last_error(); From 31159b803dc3d7333a050e1593aaa698f8602df7 Mon Sep 17 00:00:00 2001 From: "anna.cai" Date: Fri, 10 Jul 2026 14:02:53 -0400 Subject: [PATCH 2/2] move eval_ctx getter changes --- c/include/dd/policies/eval_ctx.h | 6 ------ c/src/eval_ctx.c | 9 --------- c/src/eval_ctx.h | 9 --------- c/src/evaluator.c | 2 -- c/src/test/test_eval_ctx.c | 12 ------------ 5 files changed, 38 deletions(-) diff --git a/c/include/dd/policies/eval_ctx.h b/c/include/dd/policies/eval_ctx.h index 971c425..e4316ab 100644 --- a/c/include/dd/policies/eval_ctx.h +++ b/c/include/dd/policies/eval_ctx.h @@ -172,12 +172,6 @@ plcs_errors plcs_eval_ctx_get_last_error(void); */ plcs_errors plcs_eval_ctx_peek_last_error(void); -/** - * @brief Gets the id of the policy currently being evaluated. - * @return the current policy's id, or PLCS_STR_NOT_SET (NULL) if none is being evaluated. - */ -const char *plcs_eval_ctx_get_current_policy_id(void); - /** * @brief Initializes the context model. * This function sets all evaluator function pointers to NULL and parameters to diff --git a/c/src/eval_ctx.c b/c/src/eval_ctx.c index 5cdc51a..a49b6c9 100644 --- a/c/src/eval_ctx.c +++ b/c/src/eval_ctx.c @@ -94,10 +94,6 @@ plcs_errors plcs_eval_ctx_register_action(plcs_action_function_ptr action, plcs_ return PLCS_ESUCCESS; } -void plcs_eval_ctx_set_current_policy_id(const char *policy_id) { - ctx.current_policy_id = policy_id; -} - plcs_action_function_ptr plcs_eval_ctx_get_action(plcs_actions ix) { if (ix < 0 || ix >= PLCS_ACTIONS__COUNT) { ctx.error = PLCS_EIX_OVERFLOW; @@ -216,10 +212,6 @@ plcs_errors plcs_eval_ctx_peek_last_error(void) { return ctx.error; } -const char *plcs_eval_ctx_get_current_policy_id(void) { - return ctx.current_policy_id; -} - plcs_errors plcs_eval_ctx_get_last_error(void) { plcs_errors error = ctx.error; // reset @@ -252,7 +244,6 @@ void plcs_eval_ctx_reset(void) { } ctx.error = PLCS_ESUCCESS; - ctx.current_policy_id = PLCS_STR_NOT_SET; } plcs_errors plcs_eval_ctx_init(void) { diff --git a/c/src/eval_ctx.h b/c/src/eval_ctx.h index 7eeef1c..f4ff4a5 100644 --- a/c/src/eval_ctx.h +++ b/c/src/eval_ctx.h @@ -111,9 +111,6 @@ typedef struct plcs_eval_ctx { /**< TODO: consider implementing this as a stack to preserve history of errors */ plcs_errors error; - /**< The id of the policy currently being evaluated */ - const char *current_policy_id; - } plcs_eval_ctx; /** @@ -122,12 +119,6 @@ typedef struct plcs_eval_ctx { */ void plcs_eval_ctx_set_error(plcs_errors error); -/** - * @brief Sets the id of the policy currently being evaluated. - * @param policy_id the policy's id, or PLCS_STR_NOT_SET (NULL) if none. - */ -void plcs_eval_ctx_set_current_policy_id(const char *policy_id); - /** * @brief Sets an error code for an action * @param ix An action ID from plcs_actions enum diff --git a/c/src/evaluator.c b/c/src/evaluator.c index 125509d..a2875ed 100644 --- a/c/src/evaluator.c +++ b/c/src/evaluator.c @@ -289,8 +289,6 @@ plcs_errors evaluate_policy(dd_ns(Policy_table_t) policy) { // // evaluate rules if they exist, otherwise return EVAL_RESULT_ABSTAIN plcs_evaluation_result eval_res = rules ? evaluate_rules(rules, 0) : PLCS_EVAL_RESULT_ABSTAIN; - plcs_eval_ctx_set_current_policy_id(dd_ns(Policy_id)(policy)); - // perform actions given evaluation result return perform_actions(eval_res, actions); } diff --git a/c/src/test/test_eval_ctx.c b/c/src/test/test_eval_ctx.c index 1ac5675..4136246 100644 --- a/c/src/test/test_eval_ctx.c +++ b/c/src/test/test_eval_ctx.c @@ -298,18 +298,6 @@ UTEST(eval_ctx, last_error_set_and_get) { ASSERT_EQ(plcs_eval_ctx_peek_last_error(), (plcs_errors)PLCS_ESUCCESS); } -UTEST(eval_ctx, current_policy_id_set_and_get) { - (void)plcs_eval_ctx_init(); - - ASSERT_TRUE(plcs_eval_ctx_get_current_policy_id() == PLCS_STR_NOT_SET); - - plcs_eval_ctx_set_current_policy_id("some-policy-id"); - ASSERT_STREQ(plcs_eval_ctx_get_current_policy_id(), "some-policy-id"); - - plcs_eval_ctx_reset(); - ASSERT_TRUE(plcs_eval_ctx_get_current_policy_id() == PLCS_STR_NOT_SET); -} - UTEST(eval_ctx, set_error_out_of_bound) { plcs_eval_ctx_set_action_error(PLCS_ACTIONS__COUNT, 0); int err = plcs_eval_ctx_get_last_error();