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
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions inc_internal/zt_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@

#define ZTX_LOG(lvl, fmt, ...) ZITI_LOG(lvl, "ztx[%u] " fmt, ztx->id, ##__VA_ARGS__)

#define DST_PROTOCOL "dst_protocol"
#define DST_HOSTNAME "dst_hostname"
#define DST_PORT "dst_port"


extern const char *APP_ID;
extern const char *APP_VERSION;

Expand Down
49 changes: 46 additions & 3 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) {
o += snprintf(o, sizeof(identity) - (o - identity), "%s", proto);
p += strlen(DST_PROTOCOL);
} else if (strncmp(p, DST_HOSTNAME, strlen(DST_HOSTNAME)) == 0) {
o += snprintf(o, sizeof(identity) - (o - identity), "%s", host);
p += strlen(DST_HOSTNAME);
} else if (strncmp(p, DST_PORT, strlen(DST_PORT)) == 0) {
o += snprintf(o, sizeof(identity) - (o - identity), "%d", port);
p += strlen(DST_PORT);
} else {
*o++ = '$';
}
} 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 @@ -636,12 +674,17 @@ static void do_ziti_connect(struct conn_req_s *req, future_t *f, uv_loop_t *l) {
ziti_conn_init(req->ztx, &zs->conn, zs);
char app_data[1024];
size_t len = snprintf(app_data, sizeof(app_data),
"{\"dst_protocol\": \"%s\", \"dst_hostname\": \"%s\", \"dst_port\": \"%u\"}",
"{\"" 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 +733,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