From c609310402b60f5e40260dea805d952f56e1144c Mon Sep 17 00:00:00 2001 From: Eugene K Date: Wed, 13 Jan 2021 22:22:49 -0500 Subject: [PATCH 1/4] check if update needed before service refresh --- inc_internal/internal_model.h | 5 +++++ inc_internal/ziti_ctrl.h | 2 ++ inc_internal/zt_internal.h | 1 + library/internal_model.c | 2 ++ library/ziti.c | 22 +++++++++++++++++++++- library/ziti_ctrl.c | 11 +++++++++++ 6 files changed, 42 insertions(+), 1 deletion(-) diff --git a/inc_internal/internal_model.h b/inc_internal/internal_model.h index fd8a9566..32ce798a 100644 --- a/inc_internal/internal_model.h +++ b/inc_internal/internal_model.h @@ -111,6 +111,9 @@ XX(is_running, bool, none, isRunning, __VA_ARGS__) \ XX(hash, string, none, hash, __VA_ARGS__) \ XX(signers, string, array, signerFingerprints, __VA_ARGS__) +#define ZITI_SERVICE_UPDATE(XX, ...) \ +XX(last_change, string, none, lastChangedAt, __VA_ARGS__) + #ifdef __cplusplus extern "C" { #endif @@ -148,6 +151,8 @@ DECLARE_MODEL(ziti_pr_process_req, ZITI_PR_PROCESS_REQ) DECLARE_MODEL(ziti_pr_domain_req, ZITI_PR_DOMAIN_REQ) +DECLARE_MODEL(ziti_service_update, ZITI_SERVICE_UPDATE) + #ifdef __cplusplus } #endif diff --git a/inc_internal/ziti_ctrl.h b/inc_internal/ziti_ctrl.h index c1ccec02..28c4bcba 100644 --- a/inc_internal/ziti_ctrl.h +++ b/inc_internal/ziti_ctrl.h @@ -53,6 +53,8 @@ void ziti_ctrl_current_api_session(ziti_controller *ctrl, void(*cb)(ziti_session void ziti_ctrl_logout(ziti_controller *ctrl, void(*cb)(void*, ziti_error*, void*), void *ctx); +void ziti_ctrl_get_services_update(ziti_controller *ctrl, void (*cb)(ziti_service_update*, ziti_error*, void*), void *ctx); + void ziti_ctrl_get_services(ziti_controller *ctrl, void (*srv_cb)(ziti_service_array, ziti_error *, void *), void *ctx); void ziti_ctrl_get_service(ziti_controller *ctrl, const char* service_name, void (*srv_cb)(ziti_service *, ziti_error*, void*), void* ctx); diff --git a/inc_internal/zt_internal.h b/inc_internal/zt_internal.h index b9918b21..8441cb73 100644 --- a/inc_internal/zt_internal.h +++ b/inc_internal/zt_internal.h @@ -177,6 +177,7 @@ struct ziti_ctx { // map model_map sessions; + char *last_update; uv_timer_t session_timer; uv_timer_t refresh_timer; uv_prepare_t reaper; diff --git a/library/internal_model.c b/library/internal_model.c index 1bf64e53..bf3fd5f4 100644 --- a/library/internal_model.c +++ b/library/internal_model.c @@ -79,6 +79,8 @@ IMPL_MODEL(ziti_pr_process_req, ZITI_PR_PROCESS_REQ) IMPL_MODEL(ziti_pr_domain_req, ZITI_PR_DOMAIN_REQ) +IMPL_MODEL(ziti_service_update, ZITI_SERVICE_UPDATE) + const char *ziti_service_get_raw_config(ziti_service *service, const char *cfg_type) { return (const char *) model_map_get(&service->config, cfg_type); } diff --git a/library/ziti.c b/library/ziti.c index a993cd77..cea28706 100644 --- a/library/ziti.c +++ b/library/ziti.c @@ -617,9 +617,29 @@ static void update_services(ziti_service_array services, ziti_error *error, void model_map_clear(&updates, NULL); } +static void check_service_update(ziti_service_update *update, ziti_error *err, void *ctx) { + ziti_context ztx = ctx; + bool need_update = false; + + if (err) { // API not supported - do refresh + need_update = true; + } else if (ztx->last_update == NULL || strcmp(ztx->last_update, update->last_change) != 0) { + FREE(ztx->last_update); + ztx->last_update = update->last_change; + need_update = true; + } else { + free_ziti_service_update(update); + } + + if (need_update) { + ziti_ctrl_get_services(&ztx->controller, update_services, ztx); + } + FREE(update); +} + static void services_refresh(uv_timer_t *t) { ziti_context ztx = t->data; - ziti_ctrl_get_services(&ztx->controller, update_services, ztx); + ziti_ctrl_get_services_update(&ztx->controller, check_service_update, ztx); } static void session_cb(ziti_session *session, ziti_error *err, void *ctx) { diff --git a/library/ziti_ctrl.c b/library/ziti_ctrl.c index e52487d6..48328d2f 100644 --- a/library/ziti_ctrl.c +++ b/library/ziti_ctrl.c @@ -354,6 +354,17 @@ void ziti_ctrl_logout(ziti_controller *ctrl, void(*cb)(void *, ziti_error *, voi um_http_req(&ctrl->client, "DELETE", "/current-api-session", ctrl_resp_cb, resp); } +void ziti_ctrl_get_services_update(ziti_controller *ctrl, void (*cb)(ziti_service_update*, ziti_error*, void*), void *ctx) { + struct ctrl_resp *resp = calloc(1, sizeof(struct ctrl_resp)); + resp->body_parse_func = (int (*)(void *, const char *, size_t)) parse_ziti_service_update; + resp->resp_cb = (void (*)(void *, ziti_error *, void *)) cb; + resp->ctx = ctx; + resp->ctrl = ctrl; + resp->ctrl_cb = ctrl_default_cb; + + um_http_req(&ctrl->client, "GET", "/current-api-session/service-updates", ctrl_resp_cb, resp); +} + void ziti_ctrl_get_services(ziti_controller *ctrl, void (*cb)(ziti_service_array, ziti_error *, void *), void *ctx) { struct ctrl_resp *resp = calloc(1, sizeof(struct ctrl_resp)); From a01c0f718830e3efc49b01730750dcb7417367a4 Mon Sep 17 00:00:00 2001 From: Eugene K Date: Thu, 14 Jan 2021 14:11:02 -0500 Subject: [PATCH 2/4] avoid extra call to service-updates if controller is old --- .github/workflows/cmake.yml | 2 ++ inc_internal/internal_model.h | 1 + inc_internal/zt_internal.h | 2 ++ library/utils.c | 4 ++-- library/ziti.c | 11 ++++++++++- library/ziti_ctrl.c | 4 ++++ 6 files changed, 21 insertions(+), 3 deletions(-) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index b57a56f5..f86f921f 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -1,6 +1,8 @@ name: C/C++ CI on: + push: + branches: [ '**' ] pull_request: branches: [ master ] diff --git a/inc_internal/internal_model.h b/inc_internal/internal_model.h index 32ce798a..718a0145 100644 --- a/inc_internal/internal_model.h +++ b/inc_internal/internal_model.h @@ -47,6 +47,7 @@ XX(identity, ziti_identity, ptr, identity, __VA_ARGS__) \ XX(posture_query_set, ziti_posture_query_set, array, postureQueries, __VA_ARGS__) #define ZITI_ERROR_MODEL(XX, ...) \ +XX(http_code, int, none, , __VA_ARGS__) \ XX(code, string, none, code, __VA_ARGS__) \ XX(message, string, none, message, __VA_ARGS__) diff --git a/inc_internal/zt_internal.h b/inc_internal/zt_internal.h index 8441cb73..b6a12512 100644 --- a/inc_internal/zt_internal.h +++ b/inc_internal/zt_internal.h @@ -177,7 +177,9 @@ struct ziti_ctx { // map model_map sessions; + bool no_service_updates_api; // controller API has no last-update endpoint char *last_update; + uv_timer_t session_timer; uv_timer_t refresh_timer; uv_prepare_t reaper; diff --git a/library/utils.c b/library/utils.c index 7f9c349d..cf826b64 100644 --- a/library/utils.c +++ b/library/utils.c @@ -237,8 +237,8 @@ static const char *get_elapsed_time() { uint64_t now = uv_now(ts_loop); if (now > last_update) { last_update = now; - uint64_t elapsed = now - starttime; - snprintf(elapsed_buffer, sizeof(elapsed_buffer), "%9ld.%03ld", elapsed / 1000, elapsed % 1000); + unsigned long long elapsed = now - starttime; + snprintf(elapsed_buffer, sizeof(elapsed_buffer), "%9lld.%03lld", (elapsed / 1000), (elapsed % 1000)); } return elapsed_buffer; } diff --git a/library/ziti.c b/library/ziti.c index cea28706..d13bc651 100644 --- a/library/ziti.c +++ b/library/ziti.c @@ -623,6 +623,10 @@ static void check_service_update(ziti_service_update *update, ziti_error *err, v if (err) { // API not supported - do refresh need_update = true; + if (err->http_code == 404) { + ZITI_LOG(INFO, "Controller does not support /current-api-session/service-updates API"); + ztx->no_service_updates_api = true; + } } else if (ztx->last_update == NULL || strcmp(ztx->last_update, update->last_change) != 0) { FREE(ztx->last_update); ztx->last_update = update->last_change; @@ -639,7 +643,12 @@ static void check_service_update(ziti_service_update *update, ziti_error *err, v static void services_refresh(uv_timer_t *t) { ziti_context ztx = t->data; - ziti_ctrl_get_services_update(&ztx->controller, check_service_update, ztx); + if (ztx->no_service_updates_api) { + ziti_ctrl_get_services(&ztx->controller, update_services, ztx); + } + else { + ziti_ctrl_get_services_update(&ztx->controller, check_service_update, ztx); + } } static void session_cb(ziti_session *session, ziti_error *err, void *ctx) { diff --git a/library/ziti_ctrl.c b/library/ziti_ctrl.c index 48328d2f..345a1b7d 100644 --- a/library/ziti_ctrl.c +++ b/library/ziti_ctrl.c @@ -249,6 +249,10 @@ static void ctrl_body_cb(um_http_req_t *req, const char *b, ssize_t len) { } } + if (cr.error) { + cr.error->http_code = req->resp.code; + } + free_resp_meta(&cr.meta); FREE(cr.data); FREE(resp->body); From 9ae80080ae452d488efbc0b68f7ab1ca0ec02499 Mon Sep 17 00:00:00 2001 From: Eugene K Date: Thu, 21 Jan 2021 12:48:15 -0500 Subject: [PATCH 3/4] wrong field name --- inc_internal/internal_model.h | 2 +- library/ziti.c | 11 +++++++---- library/ziti_ctrl.c | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/inc_internal/internal_model.h b/inc_internal/internal_model.h index 718a0145..88534701 100644 --- a/inc_internal/internal_model.h +++ b/inc_internal/internal_model.h @@ -113,7 +113,7 @@ XX(hash, string, none, hash, __VA_ARGS__) \ XX(signers, string, array, signerFingerprints, __VA_ARGS__) #define ZITI_SERVICE_UPDATE(XX, ...) \ -XX(last_change, string, none, lastChangedAt, __VA_ARGS__) +XX(last_change, string, none, lastChangeAt, __VA_ARGS__) #ifdef __cplusplus extern "C" { diff --git a/library/ziti.c b/library/ziti.c index d13bc651..4a18537f 100644 --- a/library/ziti.c +++ b/library/ziti.c @@ -619,20 +619,23 @@ static void update_services(ziti_service_array services, ziti_error *error, void static void check_service_update(ziti_service_update *update, ziti_error *err, void *ctx) { ziti_context ztx = ctx; - bool need_update = false; + bool need_update = true; if (err) { // API not supported - do refresh - need_update = true; if (err->http_code == 404) { ZITI_LOG(INFO, "Controller does not support /current-api-session/service-updates API"); ztx->no_service_updates_api = true; } - } else if (ztx->last_update == NULL || strcmp(ztx->last_update, update->last_change) != 0) { + } + else if (ztx->last_update == NULL || strcmp(ztx->last_update, update->last_change) != 0) { + ZITI_LOG(VERBOSE, "ztx last_update = %s", update->last_change); FREE(ztx->last_update); ztx->last_update = update->last_change; - need_update = true; } else { + ZITI_LOG(VERBOSE, "not updating: last_update is same previous (%s == %s)", update->last_change, + ztx->last_update); free_ziti_service_update(update); + need_update = false; } if (need_update) { diff --git a/library/ziti_ctrl.c b/library/ziti_ctrl.c index 345a1b7d..d80e23f5 100644 --- a/library/ziti_ctrl.c +++ b/library/ziti_ctrl.c @@ -360,7 +360,7 @@ void ziti_ctrl_logout(ziti_controller *ctrl, void(*cb)(void *, ziti_error *, voi void ziti_ctrl_get_services_update(ziti_controller *ctrl, void (*cb)(ziti_service_update*, ziti_error*, void*), void *ctx) { struct ctrl_resp *resp = calloc(1, sizeof(struct ctrl_resp)); - resp->body_parse_func = (int (*)(void *, const char *, size_t)) parse_ziti_service_update; + resp->body_parse_func = (int (*)(void *, const char *, size_t)) parse_ziti_service_update_ptr; resp->resp_cb = (void (*)(void *, ziti_error *, void *)) cb; resp->ctx = ctx; resp->ctrl = ctrl; From ec486e78e92ba56e1f0b3062f9dc0fed52fa4d78 Mon Sep 17 00:00:00 2001 From: Eugene K Date: Thu, 21 Jan 2021 13:09:44 -0500 Subject: [PATCH 4/4] reschedule refresh check --- library/ziti.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/ziti.c b/library/ziti.c index 4a18537f..665cf5a5 100644 --- a/library/ziti.c +++ b/library/ziti.c @@ -636,6 +636,8 @@ static void check_service_update(ziti_service_update *update, ziti_error *err, v ztx->last_update); free_ziti_service_update(update); need_update = false; + + uv_timer_start(&ztx->refresh_timer, services_refresh, ztx->opts->refresh_interval * 1000, 0); } if (need_update) {