v0.3.9 - API work

This commit is contained in:
Your Name
2025-09-21 15:53:03 -04:00
parent 9f3b3dd773
commit 01836a4b4c
3 changed files with 1008 additions and 18 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1 +1 @@
295261
1307796

View File

@@ -2156,15 +2156,28 @@ int process_admin_config_event(cJSON* event, char* error_message, size_t error_s
// Handle kind 33335 auth rule events
int process_admin_auth_event(cJSON* event, char* error_message, size_t error_size) {
log_info("=== SERVER-SIDE AUTH RULE EVENT DEBUG ===");
// Print the entire received event for debugging
char* debug_event_str = cJSON_Print(event);
if (debug_event_str) {
printf("Received Auth Event JSON: %s\n", debug_event_str);
free(debug_event_str);
}
cJSON* tags_obj = cJSON_GetObjectItem(event, "tags");
if (!tags_obj || !cJSON_IsArray(tags_obj)) {
log_error("Auth event missing or invalid tags array");
snprintf(error_message, error_size, "invalid: auth rule event must have tags");
return -1;
}
printf("Tags array size: %d\n", cJSON_GetArraySize(tags_obj));
// Extract action from content or tags
cJSON* content_obj = cJSON_GetObjectItem(event, "content");
const char* content = content_obj ? cJSON_GetStringValue(content_obj) : "";
printf("Event content: '%s'\n", content);
// Parse the action from content (should be "add" or "remove")
cJSON* content_json = cJSON_Parse(content);
@@ -2176,6 +2189,7 @@ int process_admin_auth_event(cJSON* event, char* error_message, size_t error_siz
}
cJSON_Delete(content_json);
}
printf("Parsed action: '%s'\n", action);
// Begin transaction for atomic auth rule updates
int rc = sqlite3_exec(g_db, "BEGIN IMMEDIATE TRANSACTION", NULL, NULL, NULL);
@@ -2185,11 +2199,33 @@ int process_admin_auth_event(cJSON* event, char* error_message, size_t error_siz
}
int rules_processed = 0;
int tags_examined = 0;
int tags_skipped = 0;
// Process each tag as an auth rule specification
cJSON* tag = NULL;
cJSON_ArrayForEach(tag, tags_obj) {
if (!cJSON_IsArray(tag) || cJSON_GetArraySize(tag) < 3) {
tags_examined++;
printf("Examining tag #%d:\n", tags_examined);
char* tag_debug_str = cJSON_Print(tag);
if (tag_debug_str) {
printf(" Tag JSON: %s\n", tag_debug_str);
free(tag_debug_str);
}
if (!cJSON_IsArray(tag)) {
printf(" SKIPPED: Not an array\n");
tags_skipped++;
continue;
}
int tag_size = cJSON_GetArraySize(tag);
printf(" Tag array size: %d\n", tag_size);
if (tag_size < 3) {
printf(" SKIPPED: Array size < 3 (need at least 3 elements for auth rules)\n");
tags_skipped++;
continue;
}
@@ -2200,6 +2236,8 @@ int process_admin_auth_event(cJSON* event, char* error_message, size_t error_siz
if (!cJSON_IsString(rule_type_obj) ||
!cJSON_IsString(pattern_type_obj) ||
!cJSON_IsString(pattern_value_obj)) {
printf(" SKIPPED: One or more elements are not strings\n");
tags_skipped++;
continue;
}
@@ -2207,18 +2245,42 @@ int process_admin_auth_event(cJSON* event, char* error_message, size_t error_siz
const char* pattern_type = cJSON_GetStringValue(pattern_type_obj);
const char* pattern_value = cJSON_GetStringValue(pattern_value_obj);
printf(" Extracted rule: type='%s', pattern_type='%s', pattern_value='%s'\n",
rule_type, pattern_type, pattern_value);
// Map rule_type to correct action (FIX THE BUG HERE)
const char* mapped_action = "allow"; // default
if (strcmp(rule_type, "pubkey_blacklist") == 0 || strcmp(rule_type, "hash_blacklist") == 0) {
mapped_action = "deny";
} else if (strcmp(rule_type, "pubkey_whitelist") == 0) {
mapped_action = "allow";
}
printf(" Mapped action for rule_type '%s': '%s'\n", rule_type, mapped_action);
// Process the auth rule based on action
if (strcmp(action, "add") == 0) {
if (add_auth_rule_from_config(rule_type, pattern_type, pattern_value, "allow") == 0) {
printf(" Attempting to add rule to database...\n");
if (add_auth_rule_from_config(rule_type, pattern_type, pattern_value, mapped_action) == 0) {
printf(" SUCCESS: Rule added to database\n");
rules_processed++;
} else {
printf(" FAILED: Could not add rule to database\n");
}
} else if (strcmp(action, "remove") == 0) {
printf(" Attempting to remove rule from database...\n");
if (remove_auth_rule_from_config(rule_type, pattern_type, pattern_value) == 0) {
printf(" SUCCESS: Rule removed from database\n");
rules_processed++;
} else {
printf(" FAILED: Could not remove rule from database\n");
}
}
}
printf("Processing summary: examined=%d, skipped=%d, processed=%d\n",
tags_examined, tags_skipped, rules_processed);
log_info("=== END SERVER-SIDE AUTH RULE EVENT DEBUG ===");
if (rules_processed > 0) {
sqlite3_exec(g_db, "COMMIT", NULL, NULL, NULL);