Skip to content

Commit

Permalink
consolidate controller error cleanup (#288)
Browse files Browse the repository at this point in the history
* consolidate controller error cleanup
* fix error memory leak
* fix ctrl_tests
* cleanup warnings
* make FREE() look like a statement
  • Loading branch information
ekoby authored Jun 17, 2021
1 parent 2a3dc3c commit c43fa68
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 207 deletions.
2 changes: 1 addition & 1 deletion deps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include(FetchContent)

FetchContent_Declare(uv-mbed
GIT_REPOSITORY https://github.com/netfoundry/uv-mbed.git
GIT_TAG v0.13.1
GIT_TAG v0.13.2
)
set(ENABLE_UM_TESTS OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(uv-mbed)
Expand Down
2 changes: 1 addition & 1 deletion inc_internal/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ typedef int *(*cond_error_t)(int);

#define NEWP(var, type) type *var = calloc(1, sizeof(type))
#define VAL_OR_ELSE(v, def) ((v) != NULL ? (v) : (def))
#define FREE(v) if ((v) != NULL) { free((void*)v); (v) = NULL; }
#define FREE(v) do { if ((v) != NULL) { free((void*)(v)); (v) = NULL; } } while(0)

#define FMT(ex) _##ex##_fmt
#define COND(ex) _##ex##_cond
Expand Down
44 changes: 22 additions & 22 deletions inc_internal/ziti_ctrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,61 +45,61 @@ int ziti_ctrl_init(uv_loop_t *loop, ziti_controller *ctlr, const char *url, tls_

int ziti_ctrl_close(ziti_controller *ctrl);

void ziti_ctrl_get_version(ziti_controller *ctrl, void (*ver_cb)(ziti_version *, ziti_error *, void *), void *ctx);
void ziti_ctrl_get_version(ziti_controller *ctrl, void (*ver_cb)(ziti_version *, const ziti_error *, void *), void *ctx);

void
ziti_ctrl_login(ziti_controller *ctrl, const char **cfg_types, void (*login_cb)(ziti_session *, ziti_error *, void *),
ziti_ctrl_login(ziti_controller *ctrl, const char **cfg_types, void (*login_cb)(ziti_session *, const ziti_error *, void *),
void *ctx);

void ziti_ctrl_current_api_session(ziti_controller *ctrl, void(*cb)(ziti_session *, ziti_error *, void *), void *ctx);
void ziti_ctrl_current_api_session(ziti_controller *ctrl, void(*cb)(ziti_session *, const ziti_error *, void *), void *ctx);

void ziti_ctrl_current_identity(ziti_controller *ctrl, void(*cb)(ziti_identity_data *, ziti_error *, void *), void *ctx);
void ziti_ctrl_current_identity(ziti_controller *ctrl, void(*cb)(ziti_identity_data *, const ziti_error *, void *), void *ctx);

void ziti_ctrl_current_edge_routers(ziti_controller *ctrl, void(*cb)(ziti_edge_router_array, ziti_error *, void *),
void ziti_ctrl_current_edge_routers(ziti_controller *ctrl, void(*cb)(ziti_edge_router_array, const ziti_error *, void *),
void *ctx);

void ziti_ctrl_logout(ziti_controller *ctrl, void(*cb)(void *, ziti_error *, void *), void *ctx);
void ziti_ctrl_logout(ziti_controller *ctrl, void(*cb)(void *, const ziti_error *, void *), void *ctx);

void ziti_ctrl_get_services_update(ziti_controller *ctrl, void (*cb)(ziti_service_update *, ziti_error *, void *),
void ziti_ctrl_get_services_update(ziti_controller *ctrl, void (*cb)(ziti_service_update *, const 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_services(ziti_controller *ctrl, void (*srv_cb)(ziti_service_array, const 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);
void (*srv_cb)(ziti_service *, const ziti_error *, void *), void *ctx);

void ziti_ctrl_get_net_session(
ziti_controller *ctrl, const char *service_id, const char *type,
void (*cb)(ziti_net_session *, ziti_error *, void *), void *ctx);
void (*cb)(ziti_net_session *, const ziti_error *, void *), void *ctx);

void ziti_ctrl_get_sessions(
ziti_controller *ctrl, void (*cb)(ziti_net_session **, ziti_error *, void *), void *ctx);
ziti_controller *ctrl, void (*cb)(ziti_net_session **, const ziti_error *, void *), void *ctx);

void ziti_ctrl_get_well_known_certs(ziti_controller *ctrl, void (*cb)(char *, ziti_error *, void *), void *ctx);
void ziti_ctrl_get_well_known_certs(ziti_controller *ctrl, void (*cb)(char *, const ziti_error *, void *), void *ctx);

void ziti_ctrl_enroll(ziti_controller *ctrl, const char *method, const char *token, const char *csr,
void (*cb)(ziti_enrollment_resp *, ziti_error *, void *), void *ctx);
void (*cb)(ziti_enrollment_resp *, const ziti_error *, void *), void *ctx);

//Posture
void ziti_pr_post_bulk(ziti_controller *ctrl, char *body, size_t body_len, void(*cb)(void *, ziti_error *, void *), void *ctx);
void ziti_pr_post_bulk(ziti_controller *ctrl, char *body, size_t body_len, void(*cb)(void *, const ziti_error *, void *), void *ctx);

void ziti_pr_post(ziti_controller *ctrl, char *body, size_t body_len,void(*cb)(void *, ziti_error *, void *), void *ctx);
void ziti_pr_post(ziti_controller *ctrl, char *body, size_t body_len, void(*cb)(void *, const ziti_error *, void *), void *ctx);


//MFA
void ziti_ctrl_login_mfa(ziti_controller *ctrl, char* body, size_t body_len, void(*cb)(void *, ziti_error *, void *), void *ctx);
void ziti_ctrl_login_mfa(ziti_controller *ctrl, char *body, size_t body_len, void(*cb)(void *, const ziti_error *, void *), void *ctx);

void ziti_ctrl_post_mfa(ziti_controller *ctrl,void(*cb)(void *, ziti_error *, void *), void *ctx);
void ziti_ctrl_post_mfa(ziti_controller *ctrl, void(*cb)(void *, const ziti_error *, void *), void *ctx);

void ziti_ctrl_get_mfa(ziti_controller *ctrl, void(*cb)(ziti_mfa_enrollment *, ziti_error *, void *), void *ctx);
void ziti_ctrl_get_mfa(ziti_controller *ctrl, void(*cb)(ziti_mfa_enrollment *, const ziti_error *, void *), void *ctx);

void ziti_ctrl_delete_mfa(ziti_controller *ctrl, char* code, void(*cb)(void*, ziti_error *, void *), void *ctx);
void ziti_ctrl_delete_mfa(ziti_controller *ctrl, char *code, void(*cb)(void *, const ziti_error *, void *), void *ctx);

void ziti_ctrl_post_mfa_verify(ziti_controller *ctrl, char* body, size_t body_len, void(*cb)(void *, ziti_error *, void *), void *ctx);
void ziti_ctrl_post_mfa_verify(ziti_controller *ctrl, char *body, size_t body_len, void(*cb)(void *, const ziti_error *, void *), void *ctx);

void ziti_ctrl_get_mfa_recovery_codes(ziti_controller *ctrl, char* code, void(*cb)(ziti_mfa_recovery_codes *, ziti_error *, void *), void *ctx);
void ziti_ctrl_get_mfa_recovery_codes(ziti_controller *ctrl, char *code, void(*cb)(ziti_mfa_recovery_codes *, const ziti_error *, void *), void *ctx);

void ziti_ctrl_post_mfa_recovery_codes(ziti_controller *ctrl, char* body, size_t body_len, void(*cb)(void *, ziti_error *, void *), void *ctx);
void ziti_ctrl_post_mfa_recovery_codes(ziti_controller *ctrl, char *body, size_t body_len, void(*cb)(void *, const ziti_error *, void *), void *ctx);


#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion inc_internal/zt_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ void ziti_fmt_time(char *time_str, size_t time_str_len, uv_timeval64_t *tv);

void hexify(const uint8_t *bin, size_t bin_len, char sep, char **buf);

void ziti_re_auth_with_cb(ziti_context ztx, void(*cb)(ziti_session *, ziti_error *, void *), void* ctx);
void ziti_re_auth_with_cb(ziti_context ztx, void(*cb)(ziti_session *, const ziti_error *, void *), void *ctx);

#ifdef __cplusplus
}
Expand Down
67 changes: 30 additions & 37 deletions library/auth_queries.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ struct ziti_mfa_cb_ctx_s {
};
typedef struct ziti_mfa_cb_ctx_s ziti_mfa_cb_ctx;

void ziti_auth_query_mfa_auth_internal_cb(void *empty, ziti_error *err, void *ctx);
static void ziti_auth_query_mfa_auth_internal_cb(void *empty, const ziti_error *err, void *ctx);

void ziti_auth_query_mfa_process(ziti_mfa_auth_ctx *mfa_auth_ctx);
static void ziti_auth_query_mfa_process(ziti_mfa_auth_ctx *mfa_auth_ctx);

void ziti_mfa_re_auth_internal_cb(ziti_session *session, ziti_error *err, void *ctx);
static void ziti_mfa_re_auth_internal_cb(ziti_session *session, const ziti_error *err, void *ctx);

void ziti_mfa_verify_internal_cb(void *empty, ziti_error *err, void *ctx);
static void ziti_mfa_verify_internal_cb(void *empty, const ziti_error *err, void *ctx);

static void ziti_mfa_enroll_get_internal_cb(ziti_mfa_enrollment *mfa_enrollment, const ziti_error *err, void *ctx);

char *ziti_mfa_code_body(char *code) {
NEWP(code_req, ziti_mfa_code_req);
Expand All @@ -73,7 +75,7 @@ char *ziti_mfa_code_body(char *code) {
size_t len;
char *body = ziti_mfa_code_req_to_json(code_req, 0, &len);

FREE(code_req)
FREE(code_req);

return body;
}
Expand All @@ -88,7 +90,7 @@ extern void ziti_auth_query_init(ziti_context ztx) {
}

extern void ziti_auth_query_free(struct auth_queries *aq) {
FREE(aq)
FREE(aq);
}

void ziti_auth_query_mfa_cb(ziti_context ztx, void *v_mfa_ctx, char *code, ziti_ar_mfa_status_cb status_cb, void *status_ctx) {
Expand Down Expand Up @@ -164,40 +166,36 @@ void ziti_auth_query_process(ziti_context ztx, void(*cb)(ziti_context)) {
ziti_auth_query_mfa_process(mfa_auth_ctx);
}

void ziti_mfa_enroll_get_internal_cb(ziti_mfa_enrollment *mfa_enrollment, ziti_error *err, void *ctx);

void ziti_mfa_enroll_post_internal_cb(void *empty, ziti_error *err, void *ctx) {
void ziti_mfa_enroll_post_internal_cb(void *empty, const ziti_error *err, void *ctx) {
ziti_mfa_enroll_cb_ctx *mfa_enroll_cb_ctx = ctx;

if (err == NULL) {
ziti_ctrl_get_mfa(&mfa_enroll_cb_ctx->ztx->controller, ziti_mfa_enroll_get_internal_cb, ctx);
} else {
ZITI_LOG(ERROR, "error during create MFA call: %d - %s - %s", err->http_code, err->code, err->message);
mfa_enroll_cb_ctx->cb(mfa_enroll_cb_ctx->ztx, err->err, NULL, mfa_enroll_cb_ctx->cb_ctx);
FREE(err)
FREE(ctx)
FREE(ctx);
}
}

void ziti_mfa_enroll_get_internal_cb(ziti_mfa_enrollment *mfa_enrollment, ziti_error *err, void *ctx) {
void ziti_mfa_enroll_get_internal_cb(ziti_mfa_enrollment *mfa_enrollment, const ziti_error *err, void *ctx) {
ziti_mfa_enroll_cb_ctx *mfa_enroll_cb_ctx = ctx;

if (err != NULL) {
if (err->http_code != 404) {
ZITI_LOG(ERROR, "error during enroll MFA call: %d - %s - %s", err->http_code, err->code, err->message);
mfa_enroll_cb_ctx->cb(mfa_enroll_cb_ctx->ztx, err->err, NULL, mfa_enroll_cb_ctx->cb_ctx);
FREE(err)
FREE(ctx)
FREE(ctx);
return;
}
FREE(err)
}

if (mfa_enrollment == NULL) {
ziti_ctrl_post_mfa(&mfa_enroll_cb_ctx->ztx->controller, ziti_mfa_enroll_post_internal_cb, ctx);
} else {
mfa_enroll_cb_ctx->cb(mfa_enroll_cb_ctx->ztx, ZITI_OK, mfa_enrollment, mfa_enroll_cb_ctx->cb_ctx);
FREE(ctx)
FREE(ctx);
free_ziti_mfa_enrollment(mfa_enrollment);
}
}
Expand All @@ -212,19 +210,18 @@ void ziti_mfa_enroll(ziti_context ztx, ziti_mfa_enroll_cb enroll_cb, void *ctx)
ziti_ctrl_get_mfa(&ztx->controller, ziti_mfa_enroll_get_internal_cb, mfa_enroll_cb_ctx);
}

void ziti_mfa_remove_internal_cb(void *empty, ziti_error *err, void *ctx) {
void ziti_mfa_remove_internal_cb(void *empty, const ziti_error *err, void *ctx) {
ziti_mfa_cb_ctx *mfa_cb_ctx = ctx;

if (err != NULL) {
ZITI_LOG(ERROR, "error during remove MFA call: %d - %s - %s", err->http_code, err->code, err->message);
mfa_cb_ctx->cb(mfa_cb_ctx->ztx, err->err, mfa_cb_ctx->cb_ctx);
FREE(err)
} else {
mfa_cb_ctx->cb(mfa_cb_ctx->ztx, ZITI_OK, mfa_cb_ctx->cb_ctx);
}

FREE(mfa_cb_ctx->code)
FREE(ctx)
FREE(mfa_cb_ctx->code);
FREE(ctx);
}

void ziti_mfa_remove(ziti_context ztx, char *code, ziti_mfa_cb remove_cb, void *ctx) {
Expand All @@ -237,13 +234,12 @@ void ziti_mfa_remove(ziti_context ztx, char *code, ziti_mfa_cb remove_cb, void *
ziti_ctrl_delete_mfa(&ztx->controller, mfa_cb_ctx->code, ziti_mfa_remove_internal_cb, mfa_cb_ctx);
}

void ziti_mfa_re_auth_internal_cb(ziti_session *session, ziti_error *err, void *ctx) {
void ziti_mfa_re_auth_internal_cb(ziti_session *session, const ziti_error *err, void *ctx) {
ziti_mfa_auth_ctx *mfa_auth_ctx = ctx;

if (err != NULL) {
ZITI_LOG(ERROR, "error during verify MFA call, could not re-authenticate: %d - %s - %s", err->http_code, err->code, err->message);
mfa_auth_ctx->status_cb(mfa_auth_ctx->ztx, mfa_auth_ctx, err->err, mfa_auth_ctx->status_ctx);
FREE(err)
} else {
ziti_session *old_session = mfa_auth_ctx->ztx->session;
mfa_auth_ctx->ztx->session = session;
Expand All @@ -256,18 +252,17 @@ void ziti_mfa_re_auth_internal_cb(ziti_session *session, ziti_error *err, void *
}
}

void ziti_mfa_verify_internal_cb(void *empty, ziti_error *err, void *ctx) {
void ziti_mfa_verify_internal_cb(void *empty, const ziti_error *err, void *ctx) {
ziti_mfa_cb_ctx *mfa_cb_ctx = ctx;

if (err != NULL) {
ZITI_LOG(ERROR, "error during verify MFA call: %d - %s - %s", err->http_code, err->code, err->message);
mfa_cb_ctx->cb(mfa_cb_ctx->ztx, err->err, mfa_cb_ctx->cb_ctx);
FREE(err)
} else {
mfa_cb_ctx->cb(mfa_cb_ctx->ztx, ZITI_OK, mfa_cb_ctx->cb_ctx);
}

FREE(ctx)
FREE(ctx);
}

void ziti_mfa_verify(ziti_context ztx, char *code, ziti_mfa_cb verify_cb, void *ctx) {
Expand All @@ -281,7 +276,7 @@ void ziti_mfa_verify(ziti_context ztx, char *code, ziti_mfa_cb verify_cb, void *
ziti_ctrl_post_mfa_verify(&ztx->controller, body, strlen(body), ziti_mfa_verify_internal_cb, mfa_cb_ctx);
}

void ziti_auth_query_mfa_auth_internal_cb(void *empty, ziti_error *err, void *ctx) {
void ziti_auth_query_mfa_auth_internal_cb(void *empty, const ziti_error *err, void *ctx) {
ziti_mfa_auth_ctx *mfa_auth_ctx = ctx;
ziti_context ztx = mfa_auth_ctx->ztx;
if (err != NULL) {
Expand All @@ -298,9 +293,8 @@ void ziti_auth_query_mfa_auth_internal_cb(void *empty, ziti_error *err, void *ct
ZITI_LOG(WARN, "no mfa status callback provided, mfa failed, status was: %d", err->err);
//only free if there is no status handler, if there is a status handler it is up to the
//status handler to try again (submit another mfa code, or call ziti_mfa_abort()
FREE(ctx)
FREE(ctx);
}
FREE(err)
return;
}

Expand All @@ -314,28 +308,27 @@ void ziti_auth_query_mfa_auth_internal_cb(void *empty, ziti_error *err, void *ct
}

mfa_auth_ctx->cb(ztx);
FREE(ctx)
FREE(ctx);
}
}

void ziti_mfa_abort(void *mfa_ctx) {
FREE(mfa_ctx)
FREE(mfa_ctx);
}

void ziti_mfa_get_recovery_codes_internal_cb(ziti_mfa_recovery_codes *rc, ziti_error *err, void *ctx) {
void ziti_mfa_get_recovery_codes_internal_cb(ziti_mfa_recovery_codes *rc, const ziti_error *err, void *ctx) {
ziti_mfa_recovery_codes_cb_ctx *mfa_recovery_codes_cb_ctx = ctx;

if (err != NULL) {
ZITI_LOG(ERROR, "error during get recovery codes MFA call: %d - %s - %s", err->http_code, err->code, err->message);
mfa_recovery_codes_cb_ctx->cb(mfa_recovery_codes_cb_ctx->ztx, err->err, NULL, mfa_recovery_codes_cb_ctx->cb_ctx);
FREE(err)
} else {
mfa_recovery_codes_cb_ctx->cb(mfa_recovery_codes_cb_ctx->ztx, ZITI_OK, rc->recovery_codes, mfa_recovery_codes_cb_ctx->cb_ctx);
free_ziti_mfa_recovery_codes(rc);
}

FREE(mfa_recovery_codes_cb_ctx->code)
FREE(ctx)
FREE(mfa_recovery_codes_cb_ctx->code);
FREE(ctx);
}

void ziti_mfa_get_recovery_codes(ziti_context ztx, char *code, ziti_mfa_recovery_codes_cb get_cb, void *ctx) {
Expand All @@ -348,18 +341,18 @@ void ziti_mfa_get_recovery_codes(ziti_context ztx, char *code, ziti_mfa_recovery
ziti_ctrl_get_mfa_recovery_codes(&ztx->controller, mfa_rc_cb_ctx->code, ziti_mfa_get_recovery_codes_internal_cb, mfa_rc_cb_ctx);
}

void ziti_mfa_post_recovery_codes_internal_cb(void *empty, ziti_error *err, void *ctx) {
void ziti_mfa_post_recovery_codes_internal_cb(void *empty, const ziti_error *err, void *ctx) {
ziti_mfa_recovery_codes_cb_ctx *mfa_recovery_codes_cb_ctx = ctx;

if (err != NULL) {
ZITI_LOG(ERROR, "error during create recovery codes MFA call: %d - %s - %s", err->http_code, err->code, err->message);
mfa_recovery_codes_cb_ctx->cb(mfa_recovery_codes_cb_ctx->ztx, err->err, NULL, mfa_recovery_codes_cb_ctx->cb_ctx);
FREE(err)
} else {
ziti_mfa_get_recovery_codes(mfa_recovery_codes_cb_ctx->ztx, mfa_recovery_codes_cb_ctx->code, mfa_recovery_codes_cb_ctx->cb, mfa_recovery_codes_cb_ctx->cb_ctx);
ziti_mfa_get_recovery_codes(mfa_recovery_codes_cb_ctx->ztx, mfa_recovery_codes_cb_ctx->code, mfa_recovery_codes_cb_ctx->cb,
mfa_recovery_codes_cb_ctx->cb_ctx);
}

FREE(ctx)
FREE(ctx);
}

void ziti_mfa_new_recovery_codes(ziti_context ztx, char *code, ziti_mfa_recovery_codes_cb new_cb, void *ctx) {
Expand Down
14 changes: 4 additions & 10 deletions library/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ static int ziti_connect(struct ziti_ctx *ztx, const ziti_net_session *session, s
return 0;
}

static void connect_get_service_cb(ziti_service* s, ziti_error *err, void *ctx) {
static void connect_get_service_cb(ziti_service *s, const ziti_error *err, void *ctx) {
uv_async_t *ar = ctx;
struct ziti_conn *conn = ar->data;
struct ziti_conn_req *req = conn->conn_req;
Expand Down Expand Up @@ -406,11 +406,9 @@ static void connect_get_service_cb(ziti_service* s, ziti_error *err, void *ctx)
conn->encrypted = s->encryption;
ziti_connect_async(ar);
}

free_ziti_error(err);
}

static void connect_get_net_session_cb(ziti_net_session * s, ziti_error *err, void *ctx) {
static void connect_get_net_session_cb(ziti_net_session *s, const ziti_error *err, void *ctx) {
uv_async_t *ar = ctx;
struct ziti_conn *conn = ar->data;
struct ziti_conn_req *req = conn->conn_req;
Expand All @@ -421,11 +419,9 @@ static void connect_get_net_session_cb(ziti_net_session * s, ziti_error *err, vo
ziti_force_session_refresh(ztx);
restart_connect(conn);
} else {
if (err->err == ZITI_NOT_FOUND) {
err->err = ZITI_SERVICE_UNAVAILABLE;
}
int e = err->err == ZITI_NOT_FOUND ? ZITI_SERVICE_UNAVAILABLE : err->err;
CONN_LOG(ERROR, "failed to get session for service[%s]: %s(%s)", conn->service, err->code, err->message);
complete_conn_req(conn, err->err);
complete_conn_req(conn, e);
}
uv_close((uv_handle_t *) ar, free_handle);
}
Expand All @@ -447,8 +443,6 @@ static void connect_get_net_session_cb(ziti_net_session * s, ziti_error *err, vo
}
ziti_connect_async(ar);
}

free_ziti_error(err);
}

static void ziti_connect_async(uv_async_t *ar) {
Expand Down
Loading

0 comments on commit c43fa68

Please sign in to comment.