Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zitilib: populate dial_identity according to intercept.dial_options.identity #722

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions library/zitilib.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,42 @@ static const char* find_service(ztx_wrap_t *wrap, int type, const char *host, ui
return best;
}

const char *fmt_identity(const ziti_intercept_cfg_v1 *intercept, const char* proto, const char *host, int port) {
if (intercept == NULL) return NULL;

tag *id_tag = model_map_get(&intercept->dial_options, "identity");
if (id_tag == NULL || id_tag->type != tag_string) {
return NULL;
}

static char identity[1024];
const char *p = id_tag->string_value;
char *o = identity;
while(*p != 0) {
if (*p == '$') {
p++;
if (strncmp(p, "dst_protocol", strlen("dst_protocol")) == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dst_protocol, dst_hostname, dst_port are used here multiple times as well as used in library/ziti_src.c and library/zitilib.c too... it feel like it should be centralized... maybe defined in a single location?

o += snprintf(o, sizeof(identity) - (o - identity), "%s", proto);
p += strlen("dst_protocol");
}
if (strncmp(p, "dst_hostname", strlen("dst_hostname")) == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't these be else ifs? why do the if's if not needed?

o += snprintf(o, sizeof(identity) - (o - identity), "%s", host);
p += strlen("dst_hostname");
}
if (strncmp(p, "dst_port", strlen("dst_port")) == 0) {
o += snprintf(o, sizeof(identity) - (o - identity), "%d", port);
p += strlen("dst_port");
}
} else {
*o++ = *p++;
}

if (o >= identity + sizeof(identity))
break;
}
return identity;
}

static void do_ziti_connect(struct conn_req_s *req, future_t *f, uv_loop_t *l) {
ZITI_LOG(DEBUG, "connecting fd[%d] to %s:%d", req->fd, req->host, req->port);
ziti_sock_t *zs = model_map_get_key(&ziti_sockets, &req->fd, sizeof(req->fd));
Expand All @@ -611,6 +647,7 @@ static void do_ziti_connect(struct conn_req_s *req, future_t *f, uv_loop_t *l) {
host = req->host;
}

ziti_intercept_cfg_v1 *intercept = NULL;
if (req->ztx == NULL) {
MODEL_MAP_FOR(it, ziti_contexts) {
ztx_wrap_t *wrap = model_map_it_value(it);
Expand All @@ -619,6 +656,7 @@ static void do_ziti_connect(struct conn_req_s *req, future_t *f, uv_loop_t *l) {
if (service_name != NULL) {
req->ztx = wrap->ztx;
req->service = service_name;
intercept = model_map_get(&wrap->intercepts, service_name);
break;
}
}
Expand All @@ -638,10 +676,13 @@ static void do_ziti_connect(struct conn_req_s *req, future_t *f, uv_loop_t *l) {
size_t len = snprintf(app_data, sizeof(app_data),
"{\"dst_protocol\": \"%s\", \"dst_hostname\": \"%s\", \"dst_port\": \"%u\"}",
proto_str, req->host, req->port);

ziti_dial_opts opts = {
.app_data = app_data,
.app_data_sz = len,
.identity = req->terminator,
.identity = (char*)(req->terminator ?
req->terminator :
fmt_identity(intercept, proto_str, req->host, req->port)),
};
ZITI_LOG(DEBUG, "connecting fd[%d] to service[%s]", zs->fd, req->service);
ziti_dial_with_options(zs->conn, req->service, &opts, on_ziti_connect, NULL);
Expand Down Expand Up @@ -690,7 +731,7 @@ int Ziti_connect(ziti_socket_t socket, ziti_context ztx, const char *service, co
.fd = socket,
.ztx = ztx,
.service = service,
.terminator = terminator,
.terminator = terminator ? strdup(terminator) : NULL,
};

future_t *f = schedule_on_loop((loop_work_cb) do_ziti_connect, &req, true);
Expand Down
Loading