From 16989c08c511b3492b9521ec839c13de6246d0f1 Mon Sep 17 00:00:00 2001 From: Eugene K Date: Tue, 12 Dec 2023 15:56:31 -0500 Subject: [PATCH 1/4] ziti controller + bearer token --- inc_internal/ziti_ctrl.h | 4 +++- library/ziti_ctrl.c | 47 +++++++++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/inc_internal/ziti_ctrl.h b/inc_internal/ziti_ctrl.h index 71900695..cf66b7d3 100644 --- a/inc_internal/ziti_ctrl.h +++ b/inc_internal/ziti_ctrl.h @@ -43,7 +43,7 @@ typedef struct ziti_controller_s { ziti_version version; - char *api_session_token; + bool has_token; char *instance_id; ziti_ctrl_redirect_cb redirect_cb; @@ -52,6 +52,8 @@ typedef struct ziti_controller_s { int ziti_ctrl_init(uv_loop_t *loop, ziti_controller *ctrl, const char *url, tls_context *tls); +int ziti_ctrl_set_token(ziti_controller *ctrl, const char *access_token); + int ziti_ctrl_cancel(ziti_controller *ctrl); void ziti_ctrl_set_page_size(ziti_controller *ctrl, unsigned int size); diff --git a/library/ziti_ctrl.c b/library/ziti_ctrl.c index 12d65137..f2fd0f56 100644 --- a/library/ziti_ctrl.c +++ b/library/ziti_ctrl.c @@ -247,7 +247,7 @@ static void ctrl_version_cb(ziti_version *v, ziti_error *e, struct ctrl_resp *re } void ziti_ctrl_clear_api_session(ziti_controller *ctrl) { - FREE(ctrl->api_session_token); + ctrl->has_token = false; if (ctrl->client) { CTRL_LOG(DEBUG, "clearing api session token for ziti_controller"); tlsuv_http_header(ctrl->client, "zt-session", NULL); @@ -263,9 +263,8 @@ static void ctrl_login_cb(ziti_api_session *s, ziti_error *e, struct ctrl_resp * if (s) { CTRL_LOG(DEBUG, "authenticated successfully session[%s]", s->id); - FREE(resp->ctrl->api_session_token); - resp->ctrl->api_session_token = strdup(s->token); - tlsuv_http_header(resp->ctrl->client, "zt-session", s->token); + ctrl->has_token = true; + tlsuv_http_header(ctrl->client, "zt-session", s->token); } ctrl_default_cb(s, e, resp); } @@ -274,8 +273,8 @@ static void ctrl_logout_cb(void *s, ziti_error *e, struct ctrl_resp *resp) { ziti_controller *ctrl = resp->ctrl; CTRL_LOG(DEBUG, "logged out"); - FREE(resp->ctrl->api_session_token); - tlsuv_http_header(resp->ctrl->client, "zt-session", NULL); + ctrl->has_token = false; + tlsuv_http_header(ctrl->client, "zt-session", NULL); ctrl_default_cb(s, e, resp); } @@ -406,7 +405,7 @@ int ziti_ctrl_init(uv_loop_t *loop, ziti_controller *ctrl, const char *url, tls_ tlsuv_http_idle_keepalive(ctrl->client, ZITI_CTRL_KEEPALIVE); tlsuv_http_connect_timeout(ctrl->client, ZITI_CTRL_TIMEOUT); tlsuv_http_header(ctrl->client, "Accept", "application/json"); - ctrl->api_session_token = NULL; + ctrl->has_token = false; ctrl->instance_id = NULL; CTRL_LOG(DEBUG, "ziti controller client initialized"); @@ -414,6 +413,26 @@ int ziti_ctrl_init(uv_loop_t *loop, ziti_controller *ctrl, const char *url, tls_ return ZITI_OK; } +int ziti_ctrl_set_token(ziti_controller *ctrl, const char *token) { + if (token == NULL) { + tlsuv_http_header(ctrl->client, "Authorization", NULL); + ctrl->has_token = false; + return 0; + } + + string_buf_t *b = new_string_buf(); + string_buf_fmt(b, "Bearer %s", token); + char *header = string_buf_to_string(b, NULL); + + ctrl->has_token = true; + tlsuv_http_header(ctrl->client, "Authorization", header); + + free(header); + delete_string_buf(b); + + return ZITI_OK; +} + void ziti_ctrl_set_page_size(ziti_controller *ctrl, unsigned int size) { ctrl->page_size = size; } @@ -433,7 +452,6 @@ int ziti_ctrl_cancel(ziti_controller *ctrl) { int ziti_ctrl_close(ziti_controller *ctrl) { free_ziti_version(&ctrl->version); - FREE(ctrl->api_session_token); FREE(ctrl->instance_id); FREE(ctrl->url); tlsuv_http_close(ctrl->client, on_http_close); @@ -483,7 +501,7 @@ void ziti_ctrl_login( } static bool verify_api_session(ziti_controller *ctrl, ctrl_resp_cb_t cb, void *ctx) { - if(ctrl->api_session_token == NULL) { + if(!ctrl->has_token) { CTRL_LOG(WARN, "no API session"); ziti_error err = { .err = ZITI_AUTHENTICATION_FAILED, @@ -545,7 +563,7 @@ void ziti_ctrl_current_edge_routers(ziti_controller *ctrl, void (*cb)(ziti_edge_ struct ctrl_resp *resp = MAKE_RESP(ctrl, cb, parse_ziti_edge_router_array, ctx); resp->paging = true; - resp->base_path = "/current-identity/edge-routers"; + resp->base_path = "/current-identity/edge-routers"; ctrl_paging_req(resp); } @@ -554,13 +572,16 @@ ziti_ctrl_get_service(ziti_controller *ctrl, const char *service_name, void (*cb void *ctx) { if(!verify_api_session(ctrl, (void (*)(void *, const ziti_error *, void *)) cb, ctx)) return; - char path[1024]; - snprintf(path, sizeof(path), "/services?filter=name=\"%s\"", service_name); + char name_clause[1024]; + snprintf(name_clause, sizeof(name_clause), "name=\"%s\"", service_name); struct ctrl_resp *resp = MAKE_RESP(ctrl, cb, parse_ziti_service_array, ctx); resp->ctrl_cb = (ctrl_cb_t) ctrl_service_cb; - start_request(ctrl->client, "GET", path, ctrl_resp_cb, resp); + tlsuv_http_req_t *req = start_request(ctrl->client, "GET", "/services", ctrl_resp_cb, resp); + tlsuv_http_req_query(req, 1, &(tlsuv_http_pair){ + "filter", name_clause + }); } void ziti_ctrl_get_session( From 0a32fc6c2804636de029c42e94137a5056e197f4 Mon Sep 17 00:00:00 2001 From: ekoby Date: Fri, 23 Feb 2024 14:07:22 -0500 Subject: [PATCH 2/4] update ziti-cli@v0.32.2 --- tests/integ/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integ/CMakeLists.txt b/tests/integ/CMakeLists.txt index e733f077..4570537b 100644 --- a/tests/integ/CMakeLists.txt +++ b/tests/integ/CMakeLists.txt @@ -22,7 +22,7 @@ else () set_property(TARGET integ-tests PROPERTY CXX_STANDARD 14) endif () -set(ZITI_CLI_VER "v0.32.1" CACHE STRING "ziti version for integration tests") +set(ZITI_CLI_VER "v0.32.2" CACHE STRING "ziti version for integration tests") add_custom_target(ziti-cli ALL COMMAND ${CMAKE_COMMAND} -E env GOBIN=${CMAKE_CURRENT_BINARY_DIR} ${GOLANG_EXE} install github.com/openziti/ziti/ziti@${ZITI_CLI_VER} From 1cbb4a140fc18bf0572999c264c559c9cd6d9605 Mon Sep 17 00:00:00 2001 From: ekoby Date: Fri, 23 Feb 2024 14:34:07 -0500 Subject: [PATCH 3/4] bootstrap tests --- tests/integ/CMakeLists.txt | 11 +++++++++++ tests/integ/test-data.h.in | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 tests/integ/test-data.h.in diff --git a/tests/integ/CMakeLists.txt b/tests/integ/CMakeLists.txt index 4570537b..4e18602c 100644 --- a/tests/integ/CMakeLists.txt +++ b/tests/integ/CMakeLists.txt @@ -9,8 +9,19 @@ execute_process(COMMAND ${EXPECTOR} -v) find_program(GOLANG_EXE NAMES go REQUIRED) +set(test_client_json ${CMAKE_CURRENT_BINARY_DIR}/test-client.json) +set(test_server_json ${CMAKE_CURRENT_BINARY_DIR}/test-server.json) + +CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/test-data.h.in + ${CMAKE_CURRENT_BINARY_DIR}/include/test-data.h + @ONLY +) + add_executable(integ-tests main.cpp) +target_include_directories(integ-tests + PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/include + PRIVATE ${ziti-sdk_SOURCE_DIR}/inc_internal) target_link_libraries(integ-tests PRIVATE ziti PRIVATE Catch2::Catch2WithMain diff --git a/tests/integ/test-data.h.in b/tests/integ/test-data.h.in new file mode 100644 index 00000000..def31d72 --- /dev/null +++ b/tests/integ/test-data.h.in @@ -0,0 +1,17 @@ + +// Copyright (c) 2024. NetFoundry Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// +// You may obtain a copy of the License at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#define TEST_CLIENT "@test_client_json@" +#define TEST_SERVER "@test_server_json@" \ No newline at end of file From e4c6d48f3d9bb4f1dba10d61ed6e70be3906738e Mon Sep 17 00:00:00 2001 From: Eugene K Date: Tue, 12 Dec 2023 15:13:00 -0500 Subject: [PATCH 4/4] inject test service name --- tests/integ/CMakeLists.txt | 1 + tests/integ/test-data.h.in | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/integ/CMakeLists.txt b/tests/integ/CMakeLists.txt index 4e18602c..dcb95af3 100644 --- a/tests/integ/CMakeLists.txt +++ b/tests/integ/CMakeLists.txt @@ -11,6 +11,7 @@ find_program(GOLANG_EXE NAMES go REQUIRED) set(test_client_json ${CMAKE_CURRENT_BINARY_DIR}/test-client.json) set(test_server_json ${CMAKE_CURRENT_BINARY_DIR}/test-server.json) +set(test_service test-service) CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/test-data.h.in ${CMAKE_CURRENT_BINARY_DIR}/include/test-data.h diff --git a/tests/integ/test-data.h.in b/tests/integ/test-data.h.in index def31d72..9c40a067 100644 --- a/tests/integ/test-data.h.in +++ b/tests/integ/test-data.h.in @@ -14,4 +14,5 @@ // limitations under the License. #define TEST_CLIENT "@test_client_json@" -#define TEST_SERVER "@test_server_json@" \ No newline at end of file +#define TEST_SERVER "@test_server_json@" +#define TEST_SERVICE "@test_service@" \ No newline at end of file