commit - 34b8eb419037662cb828555a0bd980240ca0e8ffeaf1d6cf181b5e66ee07294d
commit + db67adb16ce4aa1a633aa5320579cc0710be2b940f58e974291ec91d7ba1b58d
blob - 4dc090e70cafe1bb5eecf60971d300f5c725b4353660058a8fe7353e0d96ca92
blob + 7e56e63901f0be4e8706154a8f0e52f718876b271cd1b7b60b5bdbdf67cc2407
--- README.md
+++ README.md
without TLS), accept PROXY protocol headers from a load balancer, or sit
on a unix socket.
-A `snowflake` block sinks topics straight into a Snowflake table through
-the SQL REST API with key-pair authentication, with local queueing and
-at-least-once delivery; see `mqttd.conf(5)`.
-
Not yet implemented
-------------------
blob - dfd75581346f4d19be84c062c85ac85b0f8b2e789625238e1e67790bae9d0942
blob + dba0e4256ae2e8be6dd454cd44258cb3fb76653156798c34d674d42a944d75fb
--- ROADMAP.md
+++ ROADMAP.md
Dedicated login class in `login.conf.d` so the descriptor limit, and
with it client capacity, is a chosen number.
-Snowflake sink, first cut: a `snowflake` block is a bridge whose remote
-is a dedicated process (chrooted, `pledge("stdio inet")`). Rows go
-through the SQL REST API as multi-row INSERTs with key-pair JWT
-authentication; the parent holds the private key, signs tokens and
-resolves the hostname. Publishers are acknowledged only after the
-warehouse confirms, so delivery is at least once with the bridge queue
-and persistence covering outages. `host`, `tls servername`, `tls ca` and
-`tls noverify` cover the wildcard, regional and PrivateLink naming cases.
+Snowflake sink, first cut, since removed again: landing rows in a
+warehouse is a client's job, not the broker's, and every direction it
+wanted to grow in (Sparkplug decoding, Chariot's table layout, Snowpipe
+Streaming) was a sink feature. It moves to a daemon of its own.
## To do
that would rather a Sparkplug host take one reconnect than see sequence
gaps.
-Benchmarks: connect storm at `workers 1` and `workers 4`, steady state
-publish throughput, and per-sink Snowflake throughput, all recorded with
-the hardware and configuration used.
+Benchmarks: connect storm at `workers 1` and `workers 4` and steady
+state publish throughput, recorded with the hardware and configuration
+used.
Fan-out offload, only if the benchmarks show the broker core bound on
per-recipient encoding: the broker sends each sockets worker one message
thread-safe, the trie and session trees would need locking, and the
privsep model relies on single-loop processes.
-## Exploring
+## Elsewhere
-Snowpipe Streaming as a lower-latency, cheaper-per-row alternative to
-SQL INSERTs for the Snowflake sink.
-
-Decoding Sparkplug B payloads into columns, metrics as VARIANT, versus
-storing the protobuf as received; needs a decoder for the Sparkplug
-payload schema.
+Snowflake daemon, separate from mqttd: an MQTT 5 client with its own
+privsep parent for the key, subscribing with Receive Maximum as its
+batch size and acknowledging only after the warehouse confirms the
+insert, so a persistent session on any broker gives at-least-once
+delivery. Publishes the Sparkplug STATE message as a primary host.
+Keeps the SQL REST API batching and JWT signing from the first cut,
+then grows what the sink could not: Sparkplug B decoded into Chariot's
+SPARKPLUG_RAW layout so the existing views keep working, and Snowpipe
+Streaming as the cheaper insert path.
blob - 8b7dd5c6faf2308982f443ef48745c8eeaffdfd69f07571b9f34394686c9c64b
blob + fc6fac0036d9904de06664c80fe5d7f3ae16703120bdb3e2a9438650d63711d4
--- mqttd/Makefile
+++ mqttd/Makefile
SRCS += mqttd.c config.c control.c log.c
SRCS += listen.c sockets.c tls.c
SRCS += broker.c session.c topic.c mqtt.c db.c storage.c auth.c
-SRCS += bridge.c sparkplug.c snowflake.c jwt.c
+SRCS += bridge.c sparkplug.c
MAN = mqttd.8 mqttd.conf.5
blob - 19ce7d4e24960189292e841cdf2b452d305157e0e4d5bcb588309b2c3dd66273
blob + 0f420f77cda6c64561de6409fc2170dfc23aba5f6f02548b9c179cf2a60fa983
--- mqttd/bridge.c
+++ mqttd/bridge.c
static void bridge_reconnect_cb(int, short, void *);
static void bridge_ping_cb(int, short, void *);
static void bridge_drop(struct bridge *, const char *);
-static void bridge_sink_state(struct bridge *, int);
static void bridge_send_connect(struct bridge *);
static void bridge_send_subscribe(struct bridge *);
static int bridge_map_in(struct bridge *, const char *, size_t, char *,
memcpy(&br->conf, imsg->data, sizeof(br->conf));
br->conf.name[sizeof(br->conf.name) - 1] = '\0';
br->conf.host[sizeof(br->conf.host) - 1] = '\0';
- br->conf.primary_host[sizeof(br->conf.primary_host) - 1] = '\0';
br->conf.client_id[sizeof(br->conf.client_id) - 1] = '\0';
br->conf.username[sizeof(br->conf.username) - 1] = '\0';
br->conf.password[sizeof(br->conf.password) - 1] = '\0';
br->attempts++;
log_debug("bridge %s: connecting to %s (attempt %u)", br->conf.name,
br->conf.host, br->attempts);
- /* a sink's remote is the snowflake process, everything else dials */
- if (br->conf.proc == MQTTD_PROC_SNOW)
- broker_socks_send(br->conf.proc, IMSG_BRIDGE_CONNECT,
- &br->conf.id, sizeof(br->conf.id));
- else
- broker_listen_send(IMSG_BRIDGE_CONNECT, &br->conf.id,
- sizeof(br->conf.id));
+ broker_listen_send(IMSG_BRIDGE_CONNECT, &br->conf.id,
+ sizeof(br->conf.id));
}
static void
log_info("event=bridge-down bridge=\"%s\" remote=%s "
"reason=\"connection closed\"", br->conf.name,
br->conf.host);
- bridge_sink_state(br, 0);
}
bridge_schedule(br);
}
-/*
- * A sink stands in for a Sparkplug Host Application: while its virtual
- * connection is up the broker keeps a retained STATE message saying so,
- * which is what an edge node configured with this Primary Host ID waits
- * for before it publishes. The offline message repeats the timestamp of
- * the online one, as the specification asks of a host's will, so an
- * edge node can tell a stale offline from a current one.
- */
static void
-bridge_sink_state(struct bridge *br, int online)
-{
- struct message *m;
- struct timeval tv;
- char topic[MQTTD_MAXTEXT], payload[64];
- int n;
-
- if (!br->conf.sink || br->conf.primary_host[0] == '\0')
- return;
- if (online) {
- gettimeofday(&tv, NULL);
- br->state_ts = (uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
- }
- n = snprintf(topic, sizeof(topic), MQTTD_SPARKPLUG_PREFIX "STATE/%s",
- br->conf.primary_host);
- if (n < 0 || (size_t)n >= sizeof(topic))
- return;
- n = snprintf(payload, sizeof(payload),
- "{\"online\":%s,\"timestamp\":%llu}", online ? "true" : "false",
- (unsigned long long)br->state_ts);
- if ((m = message_new((const uint8_t *)topic, strlen(topic),
- (const uint8_t *)payload, n, 1, 1)) == NULL) {
- log_warn("%s: message_new", __func__);
- return;
- }
- broker_route(m);
- message_unref(m);
- log_info("event=sparkplug-state sink=\"%s\" host=\"%s\" online=%s",
- br->conf.name, br->conf.primary_host, online ? "true" : "false");
-}
-
-/* a clean shutdown leaves every sink's host offline in the last snapshot */
-void
-bridge_shutdown(void)
-{
- struct bridge *br;
-
- TAILQ_FOREACH(br, &broker_env->bridges, entry)
- if (br->state == BRIDGE_UP)
- bridge_sink_state(br, 0);
-}
-
-static void
bridge_ping_cb(int fd, short event, void *arg)
{
struct bridge *br = arg;
br->since = time(NULL);
log_info("event=bridge-up bridge=\"%s\" remote=%s version=%d",
br->conf.name, br->conf.host, br->conf.version);
- bridge_sink_state(br, 1);
bridge_send_subscribe(br);
session_resume(br->session);
- /* keepalive 0 (sinks) means no pinging and no timeout */
+ /* keepalive 0 means no pinging and no timeout */
if (br->conf.keepalive > 0) {
tv.tv_sec = br->conf.keepalive / 2 > 0 ?
br->conf.keepalive / 2 : 1;
blob - 7525fb5a39e3e3b6dcc79bff72b2a71ca8626de678672d5e2679f8af685d1dd9
blob + 9ece9da93fd0edc3f5735764d63c5bad1496f371a692b2392f555a7a0d19124f
--- mqttd/broker.c
+++ mqttd/broker.c
RB_GENERATE(usertree, user_entry, entry, user_entry_cmp);
static struct imsgev *broker_iev_auth;
-static struct imsgev *broker_iev_snow;
static uint32_t auth_next_id;
-/* sockets workers by index, or the snowflake process */
+/* sockets workers by index */
static struct imsgev *
broker_proc_iev(uint32_t proc)
{
- if (proc == MQTTD_PROC_SNOW)
- return (broker_iev_snow);
if (proc >= env->conf.workers)
return (NULL);
return (&broker_iev_socks[proc]);
static void broker_connect_finish(struct bconn *, struct mqtt_connect *,
const char *, const struct auth_reply *);
static void broker_ctl_disconnect_all(struct imsg *);
-static void broker_snow_lost(void);
static struct imsgev
*broker_proc_iev(uint32_t);
static void broker_dispatch_sockets(int, short, void *);
__dead void
broker(int fd_parent, int fd_control, int fd_storage, int fd_auth,
- int fd_snow, int fd_listen, int *fd_socks)
+ int fd_listen, int *fd_socks)
{
struct timeval tv;
int n;
fatal("%s: calloc", __func__);
imsg_event_init(broker_iev_auth, fd_auth, broker_dispatch_auth);
- if ((broker_iev_snow = calloc(1, sizeof(*broker_iev_snow))) == NULL)
- fatal("%s: calloc", __func__);
- imsg_event_init(broker_iev_snow, fd_snow, broker_dispatch_sockets);
-
if ((broker_iev_listen = calloc(1, sizeof(*broker_iev_listen))) == NULL)
fatal("%s: calloc", __func__);
imsg_event_init(broker_iev_listen, fd_listen, broker_dispatch_listen);
{
int n;
- /* sinks go offline first so the last snapshot carries their will */
- bridge_shutdown();
-
/* the last snapshot must reach storage before the pipe closes */
db_snapshot_sync();
imsg_event_shutdown(broker_iev_auth);
free(broker_iev_auth);
}
- if (broker_iev_snow != NULL) {
- imsg_event_shutdown(broker_iev_snow);
- free(broker_iev_snow);
- }
imsg_event_shutdown(broker_iev_listen);
free(broker_iev_listen);
for (n = 0; n < env->conf.workers; n++)
imsg_event_add(iev);
}
-/* the snowflake process speaks the sockets protocol; losing it is survivable */
static void
-broker_snow_lost(void)
-{
- struct bconn *bc, *tmp;
-
- log_warnx("lost snowflake process, sinks are down");
- imsg_event_shutdown(broker_iev_snow);
- free(broker_iev_snow);
- broker_iev_snow = NULL;
- RB_FOREACH_SAFE(bc, bconntree, &broker_env->conns, tmp)
- if (bc->proc == MQTTD_PROC_SNOW)
- broker_close(bc, RC_NONE, DETACH_WILL);
-}
-
-static void
broker_dispatch_sockets(int fd, short event, void *arg)
{
struct imsgev *iev = arg;
uint32_t proc;
int n;
- proc = iev == broker_iev_snow ? MQTTD_PROC_SNOW :
- (uint32_t)(iev - broker_iev_socks);
+ proc = (uint32_t)(iev - broker_iev_socks);
if (event & EV_READ) {
if ((n = imsgbuf_read(ibuf)) == -1)
fatal("%s: imsgbuf_read", __func__);
if (n == 0) {
- if (proc == MQTTD_PROC_SNOW) {
- broker_snow_lost();
- return;
- }
log_warnx("%s: lost sockets process %u", __func__,
proc);
broker_shutdown();
if (event & EV_WRITE) {
if (imsgbuf_write(ibuf) == -1) {
if (errno == EPIPE) {
- if (proc == MQTTD_PROC_SNOW) {
- broker_snow_lost();
- return;
- }
log_warnx("%s: lost sockets process %u",
__func__, proc);
broker_shutdown();
if (q > MQTTD_QUEUE_LOW)
above_low = 1;
}
- if (broker_iev_snow != NULL) {
- q = imsgbuf_queuelen(&broker_iev_snow->ibuf);
- if (q > MQTTD_QUEUE_HIGH)
- over = 1;
- if (q > MQTTD_QUEUE_LOW)
- above_low = 1;
- }
if (!broker_paused && over) {
broker_paused = 1;
for (n = 0; n < env->conf.workers; n++)
broker_socks_event_add(&broker_iev_socks[n]);
- if (broker_iev_snow != NULL)
- broker_socks_event_add(broker_iev_snow);
}
void
}
bc->version = c.version;
+ if (c.will) {
+ if (c.will_qos > env->conf.max_qos) {
+ broker_connack_reject(bc, MQTT_RC_QOS_NOT_SUPPORTED);
+ return;
+ }
+ if (c.will_retain && !env->conf.retain_available) {
+ broker_connack_reject(bc, MQTT_RC_RETAIN_NOT_SUPPORTED);
+ return;
+ }
+ if (!mqtt_topic_valid(c.will_topic.data, c.will_topic.len)) {
+ broker_connack_reject(bc, MQTT_RC_TOPIC_NAME_INVALID);
+ return;
+ }
+ if (MQTT_PROP_ISSET(&c.will_props, MQTT_PROP_PAYLOAD_FORMAT) &&
+ c.will_props.payload_format == 1 &&
+ !mqtt_utf8_valid(c.will_payload.data,
+ c.will_payload.len)) {
+ broker_connack_reject(bc,
+ MQTT_RC_PAYLOAD_FORMAT_INVALID);
+ return;
+ }
+ }
+
+ if (c.client_id.len == 0) {
+ if (c.version < MQTT_V5 && !c.clean_start) {
+ broker_connack_reject(bc, MQTT_RC_CLIENT_ID_INVALID);
+ return;
+ }
+ } else if (c.client_id.len > MQTTD_MAX_CLIENTID ||
+ !mqtt_utf8_valid(c.client_id.data, c.client_id.len)) {
+ broker_connack_reject(bc, MQTT_RC_CLIENT_ID_INVALID);
+ return;
+ }
+
if (MQTT_PROP_ISSET(&c.props, MQTT_PROP_AUTH_METHOD)) {
/* enhanced authentication: only the external helper does it */
if (!env->conf.auth_exec_set || broker_iev_auth == NULL ||
return;
}
- if (c.will) {
- if (c.will_qos > env->conf.max_qos) {
- broker_connack_reject(bc, MQTT_RC_QOS_NOT_SUPPORTED);
- return;
- }
- if (c.will_retain && !env->conf.retain_available) {
- broker_connack_reject(bc, MQTT_RC_RETAIN_NOT_SUPPORTED);
- return;
- }
- if (!mqtt_topic_valid(c.will_topic.data, c.will_topic.len)) {
- broker_connack_reject(bc, MQTT_RC_TOPIC_NAME_INVALID);
- return;
- }
- if (MQTT_PROP_ISSET(&c.will_props, MQTT_PROP_PAYLOAD_FORMAT) &&
- c.will_props.payload_format == 1 &&
- !mqtt_utf8_valid(c.will_payload.data,
- c.will_payload.len)) {
- broker_connack_reject(bc,
- MQTT_RC_PAYLOAD_FORMAT_INVALID);
- return;
- }
- }
-
- if (c.client_id.len == 0) {
- if (c.version < MQTT_V5 && !c.clean_start) {
- broker_connack_reject(bc, MQTT_RC_CLIENT_ID_INVALID);
- return;
- }
- } else if (c.client_id.len > MQTTD_MAX_CLIENTID ||
- !mqtt_utf8_valid(c.client_id.data, c.client_id.len)) {
- broker_connack_reject(bc, MQTT_RC_CLIENT_ID_INVALID);
- return;
- }
-
if (bc->cert_user[0] != '\0') {
/*
* A verified client certificate is the identity. A username
blob - bba6d57c50560df0126e6431b30349957063b0a76b9fe61e416d01c6f5f44655
blob + 569a53fd9609ac3c09d9218a8c2c2db072255b9b9aa8b718ead5fdee87d1cb62
--- mqttd/broker.h
+++ mqttd/broker.h
uint16_t sub_pid;
uint8_t state;
time_t since;
- uint64_t state_ts; /* last STATE online, ms */
uint64_t msgs_in;
uint64_t msgs_out;
};
int bridge_packet(struct bconn *, uint8_t, uint8_t, const uint8_t *,
size_t);
void bridge_conn_closed(struct bconn *);
-void bridge_shutdown(void);
int bridge_map_out(struct bridge *, const char *, size_t, char *,
size_t);
void bridge_ctl_show(struct imsg *, struct imsgev *);
blob - bf462de1116db0c01bef7e27216bd6a26d59e5bdcf73f04ae90edd0f3ba4b075
blob + b6ec3359f0d1141dfd35f15fa541253a5ba103c6643c0eed311190fcfd72694c
--- mqttd/config.c
+++ mqttd/config.c
len, &c->tls_ca, &c->tls_ca_len));
}
-int
-config_send_snow_ca(struct imsgev *iev, struct snow_config *c)
-{
- return (config_send_tls_blob(iev, c->id, c->name, TLS_CFG_BRIDGE_CA,
- "snowflake ca", c->tls_ca, c->tls_ca_len));
-}
-
void
config_free_bridge(struct bridge_def *b)
{
}
free(b->conf.tls_ca_file);
free(b->conf.tls_ca);
- if (b->snow != NULL) {
- freezero(b->snow->key, b->snow->key_len);
- free(b->snow->tls_ca_file);
- free(b->snow->tls_ca);
- explicit_bzero(b->snow, sizeof(*b->snow));
- free(b->snow);
- }
explicit_bzero(b, sizeof(*b));
free(b);
}
blob - 748bdf4acadfacba3c5c61fd8c86e958b7bc0fc67e8a6fdc7cc043bdad0471d3
blob + db495e119e15365b26bda3ab8757bc7b74dcf9b58189f241b5cd10863c970fb2
--- mqttd/examples/etc/mqttd.conf
+++ mqttd/examples/etc/mqttd.conf
#
# Keep this file readable by root only once it holds users.
-# Run four sockets processes; each handles its share of the connections.
-prefork 4
+# Run four sockets workers; each handles its share of the connections.
+workers 4
# Refuse CONNECT once this many clients are online (0 = unlimited).
max clients 100000
# topic "cmd/#" in qos 1 remote "site1/"
#}
-# Land sensor data in Snowflake; see mqttd.conf(5) for the table and key
-# setup. Messages queue locally while Snowflake is unreachable.
-#snowflake "warehouse" {
-# account "myorg-myaccount"
-# primary host "MQTT_SNOW_HOST"
-# user "MQTT_INGEST"
-# role "INGEST"
-# warehouse "INGEST_WH"
-# key "/etc/mqttd/snowflake.p8"
-# table "IOT.RAW.MQTT_EVENTS"
-# # insert every four hours to keep the warehouse asleep in between;
-# # hold up to 500000 rows locally while waiting or during outages
-# flush 14400
-# batch 1000
-# max queued 500000
-# topic "sensors/#" qos 1
-# topic "spBv1.0/#" qos 1
-#}
-
# First match wins; without an acl block every topic is open.
# %u is the username and %c the client id of the connecting client.
acl {
blob - 63e8945b397b8a2fbdf0d17a213c9f088b89c7efa6b89697081a31fbdec4d054 (mode 644)
blob + /dev/null
--- mqttd/jwt.c
+++ /dev/null
-/*
- * Copyright (c) 2026 Tracey Emery <tracey@openbsd.org>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-/*
- * Snowflake key-pair JWT: RS256 over {"iss":"ACCOUNT.USER.SHA256:<fp>",
- * "sub":"ACCOUNT.USER","iat":now,"exp":now+lifetime}, where fp is the
- * base64 SHA-256 of the DER encoded public key. Signed in the parent,
- * which is the only process that sees the private key.
- */
-
-#include <sys/types.h>
-#include <sys/queue.h>
-#include <sys/socket.h>
-#include <sys/tree.h>
-
-#include <netinet/in.h>
-#include <arpa/nameser.h>
-
-#include <ctype.h>
-#include <event.h>
-#include <imsg.h>
-#include <resolv.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-
-#include <openssl/bio.h>
-#include <openssl/evp.h>
-#include <openssl/pem.h>
-#include <openssl/sha.h>
-#include <openssl/x509.h>
-
-#include "mqttd.h"
-
-static int b64url(const uint8_t *, size_t, char *, size_t);
-static void upper(char *, const char *, size_t);
-
-/* base64 without padding, with the URL alphabet */
-static int
-b64url(const uint8_t *in, size_t inlen, char *out, size_t outlen)
-{
- char *p;
- int n;
-
- if ((n = b64_ntop(in, inlen, out, outlen)) == -1)
- return (-1);
- for (p = out; *p != '\0'; p++) {
- if (*p == '+')
- *p = '-';
- else if (*p == '/')
- *p = '_';
- }
- while (n > 0 && out[n - 1] == '=')
- out[--n] = '\0';
- return (n);
-}
-
-static void
-upper(char *dst, const char *src, size_t len)
-{
- size_t i;
-
- for (i = 0; i + 1 < len && src[i] != '\0'; i++)
- dst[i] = toupper((unsigned char)src[i]);
- dst[i] = '\0';
-}
-
-int
-jwt_sign(const struct snow_config *c, char *out, size_t outlen,
- time_t *expires)
-{
- BIO *bio = NULL;
- EVP_PKEY *pkey = NULL;
- EVP_MD_CTX *md = NULL;
- uint8_t *der = NULL, *sig = NULL;
- uint8_t digest[SHA256_DIGEST_LENGTH];
- char fp[64], account[MQTTD_SNOW_IDENT], user[MQTTD_SNOW_IDENT];
- char header[128], claims[1024], payload[1536];
- char hb64[256], pb64[2048], sb64[1024];
- size_t siglen;
- time_t now = time(NULL);
- int derlen, ret = -1;
-
- if (c->key == NULL || c->key_len == 0)
- return (-1);
-
- if ((bio = BIO_new_mem_buf(c->key, c->key_len)) == NULL)
- goto done;
- if ((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)) == NULL) {
- log_warnx("snowflake %s: cannot read private key %s (PKCS#8 "
- "PEM, unencrypted)", c->name, c->key_file);
- goto done;
- }
-
- /* fingerprint of the public key as Snowflake computes it */
- if ((derlen = i2d_PUBKEY(pkey, &der)) <= 0)
- goto done;
- SHA256(der, derlen, digest);
- if (b64_ntop(digest, sizeof(digest), fp, sizeof(fp)) == -1)
- goto done;
-
- upper(account, c->account, sizeof(account));
- upper(user, c->user, sizeof(user));
-
- snprintf(header, sizeof(header), "{\"alg\":\"RS256\",\"typ\":\"JWT\"}");
- snprintf(claims, sizeof(claims),
- "{\"iss\":\"%s.%s.SHA256:%s\",\"sub\":\"%s.%s\","
- "\"iat\":%lld,\"exp\":%lld}", account, user, fp, account, user,
- (long long)now, (long long)(now + MQTTD_JWT_LIFETIME));
-
- if (b64url((const uint8_t *)header, strlen(header), hb64,
- sizeof(hb64)) == -1 ||
- b64url((const uint8_t *)claims, strlen(claims), pb64,
- sizeof(pb64)) == -1)
- goto done;
- if ((size_t)snprintf(payload, sizeof(payload), "%s.%s", hb64, pb64) >=
- sizeof(payload))
- goto done;
-
- if ((md = EVP_MD_CTX_new()) == NULL)
- goto done;
- if (EVP_DigestSignInit(md, NULL, EVP_sha256(), NULL, pkey) != 1)
- goto done;
- if (EVP_DigestSign(md, NULL, &siglen, (const uint8_t *)payload,
- strlen(payload)) != 1)
- goto done;
- if ((sig = malloc(siglen)) == NULL)
- goto done;
- if (EVP_DigestSign(md, sig, &siglen, (const uint8_t *)payload,
- strlen(payload)) != 1)
- goto done;
- if (b64url(sig, siglen, sb64, sizeof(sb64)) == -1)
- goto done;
-
- if ((size_t)snprintf(out, outlen, "%s.%s", payload, sb64) >= outlen)
- goto done;
- *expires = now + MQTTD_JWT_LIFETIME;
- ret = 0;
-done:
- free(sig);
- OPENSSL_free(der);
- EVP_MD_CTX_free(md);
- EVP_PKEY_free(pkey);
- BIO_free(bio);
- return (ret);
-}
blob - 563c0839c26ddb12f7b6cf3b245cc9a9d96f7da0283b4afe875e1ae4063154c1
blob + 4dbce09a76ad092a7c6340a66057ef76ae1c6e6df936fa5550568f4952448779
--- mqttd/mqttd.8
+++ mqttd/mqttd.8
for banning addresses and to start the optional external authenticator,
which itself runs unprivileged.
It holds no broker state.
-.It snowflake
-Plays the remote end for Snowflake sinks: it batches the messages the
-broker hands it and inserts them over HTTPS, acknowledging only what the
-warehouse confirmed.
-The parent resolves names and signs its JWTs so it needs neither DNS nor
-the private key.
.It listen
Owns the listening sockets.
It accepts connections, consumes the PROXY protocol header where a
blob - 1e2cb276e6ff573b2a24c4eb69643a3045e182c517a989f3f67f29a8f5116d94
blob + d4d74841cd2c219cf773f081cd98e0ede0066e24ed393823ab54714223810b09
--- mqttd/mqttd.c
+++ mqttd/mqttd.c
static void parent_dispatch_broker(int, short, void *);
static void parent_dispatch_storage(int, short, void *);
static void parent_dispatch_auth(int, short, void *);
-static void parent_dispatch_snow(int, short, void *);
-static void parent_snow_resolve(struct imsg *);
-static void parent_snow_jwt(struct imsg *);
static void parent_dispatch_listen(int, short, void *);
static void parent_dispatch_sockets(int, short, void *);
static void parent_sighdlr(int, short, void *);
static struct imsgev *parent_iev_broker;
static struct imsgev *parent_iev_storage;
static struct imsgev *parent_iev_auth;
-static struct imsgev *parent_iev_snow;
static struct imsgev *parent_iev_listen;
static struct imsgev *parent_iev_socks;
imsg_event_add(iev);
}
-/* the chrooted sink process can neither resolve names nor hold the key */
static void
-parent_snow_resolve(struct imsg *imsg)
-{
- struct snow_resolve rq;
- struct snow_resolved rs;
- struct bridge_def *b;
- struct addrinfo hints, *res0, *res;
- char port[8];
- int error;
-
- IMSG_SIZE_CHECK(imsg, &rq);
- memcpy(&rq, imsg->data, sizeof(rq));
-
- memset(&rs, 0, sizeof(rs));
- rs.id = rq.id;
-
- TAILQ_FOREACH(b, &env->bridges, entry)
- if (b->snow != NULL && b->snow->id == rq.id)
- break;
- if (b == NULL)
- goto reply;
-
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- snprintf(port, sizeof(port), "%u", ntohs(b->snow->port));
- if ((error = getaddrinfo(b->snow->host, port, &hints, &res0)) != 0) {
- log_warnx("snowflake %s: %s: %s", b->snow->name, b->snow->host,
- gai_strerror(error));
- goto reply;
- }
- for (res = res0; res != NULL && rs.n < MQTTD_SNOW_MAXADDR;
- res = res->ai_next) {
- if (res->ai_addrlen > sizeof(rs.addrs[0]))
- continue;
- memcpy(&rs.addrs[rs.n], res->ai_addr, res->ai_addrlen);
- rs.n++;
- }
- freeaddrinfo(res0);
-reply:
- imsg_compose_event(parent_iev_snow, IMSG_SNOW_RESOLVED, 0, 0, -1, &rs,
- sizeof(rs));
-}
-
-static void
-parent_snow_jwt(struct imsg *imsg)
-{
- struct snow_jwt_reply r;
- struct bridge_def *b;
- uint32_t id;
-
- IMSG_SIZE_CHECK(imsg, &id);
- memcpy(&id, imsg->data, sizeof(id));
-
- memset(&r, 0, sizeof(r));
- r.id = id;
- TAILQ_FOREACH(b, &env->bridges, entry)
- if (b->snow != NULL && b->snow->id == id)
- break;
- if (b != NULL && jwt_sign(b->snow, r.token, sizeof(r.token),
- &r.expires) == 0)
- r.ok = 1;
- imsg_compose_event(parent_iev_snow, IMSG_SNOW_JWT_REPLY, 0, 0, -1, &r,
- sizeof(r));
- explicit_bzero(&r, sizeof(r));
-}
-
-static void
-parent_dispatch_snow(int fd, short event, void *arg)
-{
- struct imsgev *iev = arg;
- struct imsgbuf *ibuf = &iev->ibuf;
- struct imsg imsg;
- int n;
-
- if (event & EV_READ) {
- if ((n = imsgbuf_read(ibuf)) == -1)
- fatal("%s: imsgbuf_read", __func__);
- if (n == 0) {
- log_warnx("%s: lost snowflake process", __func__);
- parent_shutdown(env);
- }
- }
- if (event & EV_WRITE) {
- if (imsgbuf_write(ibuf) == -1) {
- if (errno == EPIPE) {
- log_warnx("%s: lost snowflake process",
- __func__);
- parent_shutdown(env);
- }
- fatal("%s: imsgbuf_write", __func__);
- }
- }
-
- for (;;) {
- if ((n = imsgbuf_get(ibuf, &imsg)) == -1)
- fatal("%s: imsgbuf_get", __func__);
- if (n == 0)
- break;
-
- switch (imsg.hdr.type) {
- case IMSG_CFG_DONE:
- parent_configure_done(env);
- break;
- case IMSG_SNOW_RESOLVE:
- parent_snow_resolve(&imsg);
- break;
- case IMSG_SNOW_JWT_REQUEST:
- parent_snow_jwt(&imsg);
- break;
- default:
- log_warnx("%s: unexpected imsg %d from snowflake",
- __func__, imsg.hdr.type);
- break;
- }
- imsg_free(&imsg);
- }
- imsg_event_add(iev);
-}
-
-static void
parent_dispatch_listen(int fd, short event, void *arg)
{
struct imsgev *iev = arg;
int pipe_broker_storage[2];
int pipe_auth[2];
int pipe_broker_auth[2];
- int pipe_snow[2];
- int pipe_broker_snow[2];
int pipe_listen[2];
int pipe_control_listen[2];
int pipe_broker_listen[2];
socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC,
pipe_broker_auth) == -1 ||
socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC,
- pipe_snow) == -1 ||
- socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC,
- pipe_broker_snow) == -1 ||
- socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC,
pipe_listen) == -1 ||
socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC,
pipe_control_listen) == -1 ||
close(pipe_storage[1]);
close(pipe_auth[0]);
close(pipe_auth[1]);
- close(pipe_snow[0]);
- close(pipe_snow[1]);
close(pipe_control_broker[1]);
close(pipe_broker_storage[0]);
close(pipe_broker_storage[1]);
close(pipe_broker_auth[0]);
close(pipe_broker_auth[1]);
- close(pipe_broker_snow[0]);
- close(pipe_broker_snow[1]);
close(pipe_listen[0]);
close(pipe_listen[1]);
close(pipe_control_listen[1]);
/* NOTREACHED */
}
- /* snowflake: snow[1], broker_snow[1] */
- switch (fork()) {
- case -1:
- fatal("%s: fork", __func__);
- case 0:
- close(pipe_control[0]);
- close(pipe_control[1]);
- close(pipe_broker[0]);
- close(pipe_broker[1]);
- close(pipe_storage[0]);
- close(pipe_storage[1]);
- close(pipe_auth[0]);
- close(pipe_auth[1]);
- close(pipe_snow[0]);
- close(pipe_control_broker[0]);
- close(pipe_control_broker[1]);
- close(pipe_broker_storage[0]);
- close(pipe_broker_storage[1]);
- close(pipe_broker_auth[0]);
- close(pipe_broker_auth[1]);
- close(pipe_broker_snow[0]);
- close(pipe_listen[0]);
- close(pipe_listen[1]);
- close(pipe_control_listen[0]);
- close(pipe_control_listen[1]);
- close(pipe_broker_listen[0]);
- close(pipe_broker_listen[1]);
- for (i = 0; i < e->conf.workers; i++) {
- close(pipe_socks[i][0]);
- close(pipe_socks[i][1]);
- close(pipe_listen_socks[i][0]);
- close(pipe_listen_socks[i][1]);
- close(pipe_broker_socks[i][0]);
- close(pipe_broker_socks[i][1]);
- }
- snowflake(pipe_snow[1], pipe_broker_snow[1]);
- /* NOTREACHED */
- }
-
/* storage: storage[1], broker_storage[1] */
switch (fork()) {
case -1:
close(pipe_storage[0]);
close(pipe_auth[0]);
close(pipe_auth[1]);
- close(pipe_snow[0]);
- close(pipe_snow[1]);
close(pipe_control_broker[0]);
close(pipe_control_broker[1]);
close(pipe_broker_storage[0]);
close(pipe_broker_auth[0]);
close(pipe_broker_auth[1]);
- close(pipe_broker_snow[0]);
- close(pipe_broker_snow[1]);
close(pipe_listen[0]);
close(pipe_listen[1]);
close(pipe_control_listen[0]);
close(pipe_storage[0]);
close(pipe_storage[1]);
close(pipe_auth[0]);
- close(pipe_snow[0]);
- close(pipe_snow[1]);
close(pipe_control_broker[0]);
close(pipe_control_broker[1]);
close(pipe_broker_storage[0]);
close(pipe_broker_storage[1]);
close(pipe_broker_auth[0]);
- close(pipe_broker_snow[0]);
- close(pipe_broker_snow[1]);
close(pipe_listen[0]);
close(pipe_listen[1]);
close(pipe_control_listen[0]);
close(pipe_storage[1]);
close(pipe_auth[0]);
close(pipe_auth[1]);
- close(pipe_snow[0]);
- close(pipe_snow[1]);
close(pipe_control_broker[0]);
close(pipe_broker_storage[1]);
close(pipe_broker_auth[1]);
- close(pipe_broker_snow[1]);
close(pipe_listen[0]);
close(pipe_listen[1]);
close(pipe_control_listen[0]);
}
broker(pipe_broker[1], pipe_control_broker[1],
pipe_broker_storage[0], pipe_broker_auth[0],
- pipe_broker_snow[0], pipe_broker_listen[0], fds);
+ pipe_broker_listen[0], fds);
/* NOTREACHED */
}
close(pipe_storage[1]);
close(pipe_auth[0]);
close(pipe_auth[1]);
- close(pipe_snow[0]);
- close(pipe_snow[1]);
close(pipe_control_broker[0]);
close(pipe_control_broker[1]);
close(pipe_broker_storage[0]);
close(pipe_broker_storage[1]);
close(pipe_broker_auth[0]);
close(pipe_broker_auth[1]);
- close(pipe_broker_snow[0]);
- close(pipe_broker_snow[1]);
close(pipe_listen[0]);
close(pipe_control_listen[0]);
close(pipe_broker_listen[0]);
close(pipe_storage[1]);
close(pipe_auth[0]);
close(pipe_auth[1]);
- close(pipe_snow[0]);
- close(pipe_snow[1]);
close(pipe_control_broker[0]);
close(pipe_control_broker[1]);
close(pipe_broker_storage[0]);
close(pipe_broker_storage[1]);
close(pipe_broker_auth[0]);
close(pipe_broker_auth[1]);
- close(pipe_broker_snow[0]);
- close(pipe_broker_snow[1]);
close(pipe_listen[0]);
close(pipe_listen[1]);
close(pipe_control_listen[0]);
close(pipe_broker[1]);
close(pipe_storage[1]);
close(pipe_auth[1]);
- close(pipe_snow[1]);
close(pipe_control_broker[0]);
close(pipe_control_broker[1]);
close(pipe_broker_storage[0]);
close(pipe_broker_storage[1]);
close(pipe_broker_auth[0]);
close(pipe_broker_auth[1]);
- close(pipe_broker_snow[0]);
- close(pipe_broker_snow[1]);
close(pipe_listen[1]);
close(pipe_control_listen[0]);
close(pipe_control_listen[1]);
fatal("%s: calloc", __func__);
imsg_event_init(parent_iev_auth, pipe_auth[0], parent_dispatch_auth);
- if ((parent_iev_snow = calloc(1, sizeof(*parent_iev_snow))) == NULL)
- fatal("%s: calloc", __func__);
- imsg_event_init(parent_iev_snow, pipe_snow[0], parent_dispatch_snow);
-
if ((parent_iev_listen = calloc(1, sizeof(*parent_iev_listen))) == NULL)
fatal("%s: calloc", __func__);
imsg_event_init(parent_iev_listen, pipe_listen[0],
return (-1);
if (config_send_global(parent_iev_auth, e) == -1)
return (-1);
- if (config_send_global(parent_iev_snow, e) == -1)
- return (-1);
if (config_send_global(parent_iev_listen, e) == -1)
return (-1);
for (n = 0; n < e->conf.workers; n++)
* the password.
*/
TAILQ_FOREACH(b, &e->bridges, entry) {
- if (b->snow != NULL) {
- /* a sink: the snowflake process plays the remote */
- b->conf.proc = MQTTD_PROC_SNOW;
- b->snow->key = tls_load_file(b->snow->key_file,
- &b->snow->key_len, NULL);
- if (b->snow->key == NULL) {
- log_warn("snowflake %s: key file %s",
- b->snow->name, b->snow->key_file);
- return (-1);
- }
- b->snow->tls_ca = tls_load_file(b->snow->tls_ca_file !=
- NULL ? b->snow->tls_ca_file : MQTTD_BRIDGE_CA,
- &b->snow->tls_ca_len, NULL);
- if (b->snow->tls_ca == NULL) {
- log_warn("snowflake %s: ca file",
- b->snow->name);
- return (-1);
- }
- if (config_send_bridge(parent_iev_broker, b) == -1)
- return (-1);
- if (imsg_compose_event(parent_iev_snow, IMSG_CFG_SNOW,
- 0, 0, -1, b->snow, sizeof(*b->snow)) == -1)
- return (-1);
- if (config_send_snow_ca(parent_iev_snow,
- b->snow) == -1)
- return (-1);
- continue;
- }
b->conf.proc = 0;
if (b->conf.tls) {
b->conf.tls_ca = tls_load_file(b->conf.tls_ca_file !=
* The broker only acknowledges once storage has replayed the last
* snapshot into it, so no client connects to a half restored state.
*/
- e->cfg_pending = 5 + e->conf.workers;
+ e->cfg_pending = 4 + e->conf.workers;
imsg_compose_event(parent_iev_storage, IMSG_CFG_DONE, 0, 0, -1, NULL,
0);
imsg_compose_event(parent_iev_auth, IMSG_CFG_DONE, 0, 0, -1, NULL, 0);
- imsg_compose_event(parent_iev_snow, IMSG_CFG_DONE, 0, 0, -1, NULL, 0);
imsg_compose_event(parent_iev_broker, IMSG_CFG_DONE, 0, 0, -1, NULL, 0);
imsg_compose_event(parent_iev_listen, IMSG_CFG_DONE, 0, 0, -1, NULL, 0);
for (n = 0; n < e->conf.workers; n++)
imsg_compose_event(parent_iev_storage, IMSG_CTL_START, 0, 0, -1, NULL,
0);
imsg_compose_event(parent_iev_auth, IMSG_CTL_START, 0, 0, -1, NULL, 0);
- imsg_compose_event(parent_iev_snow, IMSG_CTL_START, 0, 0, -1, NULL, 0);
/* workers first, so listen never hands over to an unprepared one */
for (n = 0; n < e->conf.workers; n++)
imsg_compose_event(&parent_iev_socks[n], IMSG_CTL_START, 0, 0,
config_send_global(parent_iev_broker, e);
config_send_global(parent_iev_storage, e);
config_send_global(parent_iev_auth, e);
- config_send_global(parent_iev_snow, e);
config_send_global(parent_iev_listen, e);
for (i = 0; i < e->conf.workers; i++)
config_send_global(&parent_iev_socks[i], e);
free(parent_iev_storage);
imsg_event_shutdown(parent_iev_auth);
free(parent_iev_auth);
- imsg_event_shutdown(parent_iev_snow);
- free(parent_iev_snow);
imsg_event_shutdown(parent_iev_listen);
free(parent_iev_listen);
for (n = 0; n < e->conf.workers; n++)
blob - d62955118e502ed546c4e3cd004abcf48cfea203b74f13d8f0453009e12d724b
blob + 817fef06a4e8e7db9a30294f3d1731b6f04b14c41a55bf130c8717cdab1a4e78
--- mqttd/mqttd.conf.5
+++ mqttd/mqttd.conf.5
and included in persistence snapshots.
Reconnection backs off from 1 to 60 seconds.
Changes to bridges need a restart.
-.Sh SNOWFLAKE SINKS
-A
-.Ic snowflake
-block is a bridge whose remote is a Snowflake table.
-Matching publishes are collected into batches and inserted through the
-Snowflake SQL REST API with key-pair JWT authentication; a message is
-acknowledged towards its publisher only once the warehouse has confirmed
-the insert, and messages queue locally, bounded by
-.Ic max queued
-and included in persistence snapshots, while Snowflake is unreachable.
-.Bd -ragged -offset indent
-.Ic snowflake Ar name Brq ...
-.Ed
-.Pp
-The block accepts:
-.Bl -tag -width Ds
-.It Ic account Ar identifier
-The account identifier in the
-.Dq orgname-accountname
-form.
-Required.
-.It Ic user Ar name
-The Snowflake user whose public key was registered with
-.Ql ALTER USER ... SET RSA_PUBLIC_KEY .
-Required.
-.It Ic key Ar file
-The matching unencrypted PKCS#8 private key.
-Only the parent process reads it; it signs tokens for the sink process.
-Required.
-.It Ic table Ar database.schema.table
-Target table.
-Required.
-It needs the columns
-.Bd -literal -offset indent
-CREATE TABLE mqtt_events (
- received_at TIMESTAMP_LTZ,
- topic VARCHAR,
- qos NUMBER(1),
- retain BOOLEAN,
- payload BINARY
-);
-.Ed
-.Pp
-Text payloads are read back with
-.Ql TRY_PARSE_JSON(TO_VARCHAR(payload, 'UTF-8')) ;
-Sparkplug B payloads stay protobuf.
-.It Ic role Ar name
-.It Ic warehouse Ar name
-Role and warehouse to run the inserts with.
-.It Ic host Ar name Op Ic port Ar number
-Endpoint to connect to when it differs from
-.Ar account Ns Pa .snowflakecomputing.com ,
-for example a PrivateLink URL.
-.It Ic tls servername Ar name
-Name to verify the server certificate against when it differs from
-.Ic host .
-.It Ic tls ca Ar file
-CA bundle, default
-.Pa /etc/ssl/cert.pem .
-.It Ic tls noverify
-Skip certificate verification.
-For debugging PrivateLink or intercepting proxies only.
-.It Ic flush Ar seconds
-How long rows accumulate before they are inserted, up to 86400.
-This is the cost control: every insert wakes the warehouse, so a site
-that can tolerate the delay inserts every few hours instead of every
-few seconds.
-The default is 2.
-.It Ic batch Ar rows
-Rows per INSERT statement when a flush runs; a flush issues as many
-statements as needed, each also kept under Snowflake's statement size
-limit, and acknowledges rows statement by statement.
-The default is 500.
-.It Ic max queued Ar number
-Rows held for this sink beyond the 65535 the broker keeps in flight,
-replacing the global
-.Ic max queued .
-Size it for the longest
-.Ic flush
-plus the longest outage at the expected message rate.
-.It Ic timeout Ar seconds
-HTTP and statement timeout.
-The default is 60.
-.It Ic topic Ar pattern Oo Ic qos Ar number Oc Op Ic local Ar prefix
-A topic filter to sink, QoS 0 or 1.
-At least one is required.
-.It Ic primary host Ar id
-Announce the sink as a Sparkplug B Host Application under
-.Ar id .
-While the sink is up the broker keeps a retained
-.Pa spBv1.0/STATE/ Ns Ar id
-message whose payload says
-.Ql online
-true; when the sink goes down, or the daemon shuts down cleanly, it is
-replaced by one saying false with the same timestamp.
-An edge node configured with this Primary Host ID, such as an Ignition
-MQTT Transmission module, publishes nothing until it sees the online
-message and stores and forwards while the host is offline, so an outage
-long enough to close the sink is bridged on the edge instead of in
-.Ic max queued .
-The id is a single topic level.
-The default is the sink's name.
-Deny client publishes below
-.Pa spBv1.0/STATE/
-in the
-.Ic acl
-so no client can forge it.
-.El
-.Pp
-Snowflake certificates: account endpoints live under wildcard
-certificates for
-.Pa *.snowflakecomputing.com
-and regional variants, so the name verified must be the exact URL form
-Snowflake gives for the account; multi-label legacy locators and
-PrivateLink names need
-.Ic host
-or
-.Ic tls servername
-set accordingly.
-Snowflake's own drivers additionally require OCSP; mqttd validates the
-chain against
-.Ic tls ca
-and does not check revocation.
-.Pp
-Key setup:
-.Bd -literal -offset indent
-openssl genrsa 2048 | openssl pkcs8 -topk8 -nocrypt \e
- -out /etc/mqttd/snowflake.p8
-openssl rsa -in /etc/mqttd/snowflake.p8 -pubout -out snowflake.pub
-# in Snowflake, paste the PEM body without header and footer:
-ALTER USER mqtt_ingest SET RSA_PUBLIC_KEY='MIIBIjANBgkq...';
-.Ed
.Sh ACL
Topic access is controlled by an optional
.Ic acl
blob - f4a4b7a06650d22afbbb6f2fd84c7b58ed3015cf9deb254ce4d0cb1332e44dd2
blob + 6b29555e3b62738d7be4aeb5e3b2cf4453f2da53075690b45d34eb0e483784fc
--- mqttd/mqttd.h
+++ mqttd/mqttd.h
#define MQTTD_SPARKPLUG_PREFIX "spBv1.0/"
#define MQTTD_SPARKPLUG_CERTS "$sparkplug/certificates/"
-/* a bridge whose remote is the snowflake process, not a sockets process */
-#define MQTTD_PROC_SNOW UINT32_MAX
-#define MQTTD_SNOW_SUFFIX ".snowflakecomputing.com"
-#define MQTTD_SNOW_IDENT 256
-#define MQTTD_SNOW_MAXADDR 8
-#define MQTTD_SNOW_DEFAULT_BATCH 500
-#define MQTTD_SNOW_DEFAULT_FLUSH 2
-#define MQTTD_SNOW_DEFAULT_TIMEOUT 60
-#define MQTTD_SNOW_MAXFLUSH 86400
-#define MQTTD_SNOW_MAXSQL (900 * 1024)
-#define MQTTD_SNOW_WINDOW 65535
-#define MQTTD_SNOW_MAXFAIL 3
-#define MQTTD_SNOW_MAXRESP (1024 * 1024)
-#define MQTTD_SNOW_POLLS 30
-#define MQTTD_JWT_MAX 4096
-#define MQTTD_JWT_LIFETIME 3540
-
#define MQTTD_MAXTEXT 511
#define MQTTD_MAXNAME 64
#define MQTTD_MAXPEER 64
IMSG_CTL_SHOW_BRIDGES_DATA,
IMSG_CTL_SHOW_BRIDGES_END,
- /* snowflake sinks */
- IMSG_CFG_SNOW,
- IMSG_SNOW_RESOLVE,
- IMSG_SNOW_RESOLVED,
- IMSG_SNOW_JWT_REQUEST,
- IMSG_SNOW_JWT_REPLY,
-
/* sockets <-> broker */
IMSG_CONN_OPEN,
IMSG_CONN_PACKET,
* Bridges. The parent resolves the remote address and loads the CA;
* the listen process dials the remote on request and hands the
* connection to a sockets worker, which runs TLS, and the broker drives
- * the MQTT client side of it. proc is MQTTD_PROC_SNOW for a sink, whose
- * remote is the snowflake process instead.
+ * the MQTT client side of it.
*/
#define BRIDGE_IN 0x01
#define BRIDGE_OUT 0x02
struct bridge_config {
uint32_t id;
uint32_t proc;
- uint8_t sink;
char name[MQTTD_MAXNAME];
- char primary_host[MQTTD_MAXNAME];
char host[MQTTD_MAXHOST];
struct sockaddr_storage ss;
in_port_t port;
};
TAILQ_HEAD(bridge_topics, bridge_topic);
-/*
- * Snowflake sink. Rows are inserted through the SQL REST API with
- * key-pair JWT authentication; the parent keeps the private key and
- * signs tokens for the sink process on request.
- */
-struct snow_config {
- uint32_t id;
- char name[MQTTD_MAXNAME];
- char account[MQTTD_SNOW_IDENT];
- char user[MQTTD_SNOW_IDENT];
- char role[MQTTD_SNOW_IDENT];
- char warehouse[MQTTD_SNOW_IDENT];
- char database[MQTTD_SNOW_IDENT];
- char schema[MQTTD_SNOW_IDENT];
- char table[MQTTD_SNOW_IDENT];
- char host[MQTTD_MAXHOST];
- char servername[MQTTD_MAXHOST];
- in_port_t port;
- uint8_t noverify;
- uint16_t batch;
- uint32_t flush;
- uint16_t timeout;
- /* parent only */
- char key_file[PATH_MAX];
- uint8_t *key;
- size_t key_len;
- char *tls_ca_file;
- uint8_t *tls_ca;
- size_t tls_ca_len;
-};
-
-struct snow_resolve {
- uint32_t id;
-};
-
-struct snow_resolved {
- uint32_t id;
- int n;
- struct sockaddr_storage addrs[MQTTD_SNOW_MAXADDR];
-};
-
-struct snow_jwt_reply {
- uint32_t id;
- int ok;
- time_t expires;
- char token[MQTTD_JWT_MAX];
-};
-
/* parent side only */
struct bridge_def {
TAILQ_ENTRY(bridge_def) entry;
struct bridge_config conf;
struct bridge_topics topics;
- struct snow_config *snow;
};
TAILQ_HEAD(bridge_defs, bridge_def);
/* broker.c */
__dead void
- broker(int, int, int, int, int, int, int *);
+ broker(int, int, int, int, int, int *);
/* storage.c */
__dead void
__dead void
auth(int, int);
-/* snowflake.c */
-__dead void
- snowflake(int, int);
-
-/* jwt.c */
-int jwt_sign(const struct snow_config *, char *, size_t, time_t *);
-
/* control.c */
__dead void
control(int, int, int);
int config_send_bridge(struct imsgev *, struct bridge_def *);
int config_send_bridge_ca(struct imsgev *, struct bridge_def *);
int config_recv_bridge_ca(struct bridge_config *, struct imsg *);
-int config_send_snow_ca(struct imsgev *, struct snow_config *);
void config_free_bridge(struct bridge_def *);
struct listener
*config_listener_byid(struct mqttd *, uint32_t);
blob - 18c2434f84b188f9785d2d13c4d1265002f3fb42da7e1df627b9259759bdea3d
blob + ecbb81fb6c5a5efb767bf7cf94891d833cc89f1d5d10c452d225bdfe06618b1d
--- mqttd/parse.y
+++ mqttd/parse.y
static struct user *cur_user;
static struct bridge_def *cur_bridge;
static struct bridge_topic *cur_topic;
-static struct snow_config *cur_snow;
static uint32_t listener_id;
static uint32_t bridge_id;
static int errors;
-static int snow_set(char *, size_t, char *, const char *);
-
typedef struct {
union {
long long number;
%}
-%token ACCOUNT ACL AFTER ALIAS ALL ALLOW ANONYMOUS ANY AUTH AWARE BAN BATCH
-%token BOTH BRIDGE CA CERTIFICATE CIPHERS CLIENT CLIENTS CRL DENY DHE ECDHE
-%token ERROR EXEC EXPIRY FAILURES FLUSH HOST IN INCLUDE INFLIGHT INTERVAL
-%token KEEPALIVE KEY LISTEN LOCAL MAX NAME NO NOVERIFY OCSP ON OPTIONAL OUT
-%token PACKET PASSWORD PATH PERSISTENCE PF PORT PREFORK PRIMARY PROTOCOLS PROXY
-%token PUBLISH QOS QUEUED RATE REMOTE RETAIN ROLE SERVERNAME SESSION SIZE
-%token SNOWFLAKE SPARKPLUG SUBSCRIBE SYS TABLE TIMEOUT TLS TOPIC USER VERSION
-%token WAREHOUSE WEBSOCKET WITHIN WORKERS YES
+%token ACL AFTER ALIAS ALL ALLOW ANONYMOUS ANY AUTH AWARE BAN BOTH BRIDGE CA
+%token CERTIFICATE CIPHERS CLIENT CLIENTS CRL DENY DHE ECDHE ERROR EXEC EXPIRY
+%token FAILURES IN INCLUDE INFLIGHT INTERVAL KEEPALIVE KEY LISTEN LOCAL MAX
+%token NAME NO OCSP ON OPTIONAL OUT PACKET PASSWORD PATH PERSISTENCE PF PORT
+%token PREFORK PROTOCOLS PROXY PUBLISH QOS QUEUED RATE REMOTE RETAIN SESSION
+%token SIZE SPARKPLUG SUBSCRIBE SYS TABLE TLS TOPIC USER VERSION WEBSOCKET
+%token WITHIN WORKERS YES
%token <v.string> STRING
%token <v.number> NUMBER
| grammar listen '\n'
| grammar acl '\n'
| grammar bridge '\n'
- | grammar snowflake '\n'
| grammar error '\n' { file->errors++; }
;
}
;
-/*
- * A Snowflake sink is a bridge whose remote is the snowflake process:
- * version 5 so the sink can announce its batch size as Receive Maximum,
- * no keepalive because the "remote" is local.
- */
-snowflake : SNOWFLAKE STRING {
- struct bridge_def *b;
-
- if (strlen($2) == 0 || strlen($2) >= MQTTD_MAXNAME) {
- yyerror("invalid snowflake name: %s", $2);
- free($2);
- YYERROR;
- }
- TAILQ_FOREACH(b, &conf->bridges, entry) {
- if (strcmp(b->conf.name, $2) == 0) {
- yyerror("duplicate bridge or snowflake "
- "name: %s", $2);
- free($2);
- YYERROR;
- }
- }
- if ((b = calloc(1, sizeof(*b))) == NULL ||
- (b->snow = calloc(1, sizeof(*b->snow))) == NULL)
- fatal("%s: calloc", __func__);
- TAILQ_INIT(&b->topics);
- strlcpy(b->conf.name, $2, sizeof(b->conf.name));
- snprintf(b->conf.client_id, sizeof(b->conf.client_id),
- "mqttd-snowflake-%s", $2);
- b->conf.id = ++bridge_id;
- b->conf.sink = 1;
- b->conf.proc = MQTTD_PROC_SNOW;
- b->conf.version = MQTT_V5;
- b->conf.keepalive = 0;
- b->conf.tls = 1;
- b->snow->id = b->conf.id;
- strlcpy(b->snow->name, $2, sizeof(b->snow->name));
- strlcpy(b->conf.primary_host, $2,
- sizeof(b->conf.primary_host));
- b->snow->port = htons(443);
- b->snow->batch = MQTTD_SNOW_DEFAULT_BATCH;
- b->snow->flush = MQTTD_SNOW_DEFAULT_FLUSH;
- b->snow->timeout = MQTTD_SNOW_DEFAULT_TIMEOUT;
- TAILQ_INSERT_TAIL(&conf->bridges, b, entry);
- cur_bridge = b;
- cur_snow = b->snow;
- free($2);
- } '{' optnl snowopts_l '}' {
- struct snow_config *s = cur_snow;
- size_t i;
-
- if (s->account[0] == '\0' || s->user[0] == '\0' ||
- s->key_file[0] == '\0' || s->table[0] == '\0') {
- yyerror("snowflake %s needs account, user, key "
- "and table", s->name);
- YYERROR;
- }
- if (TAILQ_EMPTY(&cur_bridge->topics)) {
- yyerror("snowflake %s has no topics", s->name);
- YYERROR;
- }
- if (s->host[0] == '\0') {
- /* org-account.snowflakecomputing.com */
- for (i = 0; s->account[i] != '\0' &&
- i < sizeof(s->host) - 1; i++)
- s->host[i] = tolower((unsigned char)
- s->account[i]);
- s->host[i] = '\0';
- if (strlcat(s->host, MQTTD_SNOW_SUFFIX,
- sizeof(s->host)) >= sizeof(s->host)) {
- yyerror("snowflake %s host too long",
- s->name);
- YYERROR;
- }
- }
- strlcpy(cur_bridge->conf.host, s->host,
- sizeof(cur_bridge->conf.host));
- cur_bridge->conf.port = s->port;
- cur_bridge = NULL;
- cur_snow = NULL;
- }
- ;
-
-snowopts_l : snowopts_l snowopt nl
- | snowopt optnl
- ;
-
-snowopt : ACCOUNT STRING {
- if (snow_set(cur_snow->account,
- sizeof(cur_snow->account), $2, "account") == -1)
- YYERROR;
- }
- | HOST STRING optport {
- if (snow_set(cur_snow->host, sizeof(cur_snow->host),
- $2, "host") == -1)
- YYERROR;
- if ($3 != 0)
- cur_snow->port = $3;
- }
- | USER STRING {
- if (snow_set(cur_snow->user, sizeof(cur_snow->user), $2,
- "user") == -1)
- YYERROR;
- }
- | ROLE STRING {
- if (snow_set(cur_snow->role, sizeof(cur_snow->role), $2,
- "role") == -1)
- YYERROR;
- }
- | WAREHOUSE STRING {
- if (snow_set(cur_snow->warehouse,
- sizeof(cur_snow->warehouse), $2, "warehouse") == -1)
- YYERROR;
- }
- | KEY STRING {
- if ($2[0] != '/') {
- yyerror("snowflake key path must be absolute");
- free($2);
- YYERROR;
- }
- if (snow_set(cur_snow->key_file,
- sizeof(cur_snow->key_file), $2, "key") == -1)
- YYERROR;
- }
- | TABLE STRING {
- char *p = $2, *db, *sc, *tb;
-
- db = strsep(&p, ".");
- sc = strsep(&p, ".");
- tb = strsep(&p, ".");
- if (db == NULL || sc == NULL || tb == NULL ||
- p != NULL || *db == '\0' || *sc == '\0' ||
- *tb == '\0') {
- yyerror("snowflake table must be "
- "DATABASE.SCHEMA.TABLE");
- free($2);
- YYERROR;
- }
- if (strlcpy(cur_snow->database, db,
- sizeof(cur_snow->database)) >=
- sizeof(cur_snow->database) ||
- strlcpy(cur_snow->schema, sc,
- sizeof(cur_snow->schema)) >=
- sizeof(cur_snow->schema) ||
- strlcpy(cur_snow->table, tb,
- sizeof(cur_snow->table)) >=
- sizeof(cur_snow->table)) {
- yyerror("snowflake table name too long");
- free($2);
- YYERROR;
- }
- free($2);
- }
- | BATCH NUMBER {
- if ($2 < 1 || $2 > UINT16_MAX) {
- yyerror("invalid snowflake batch: %lld", $2);
- YYERROR;
- }
- cur_snow->batch = $2;
- }
- | FLUSH NUMBER {
- if ($2 < 1 || $2 > MQTTD_SNOW_MAXFLUSH) {
- yyerror("invalid snowflake flush: %lld", $2);
- YYERROR;
- }
- cur_snow->flush = $2;
- }
- | MAX QUEUED NUMBER {
- if ($3 < 1 || $3 > UINT32_MAX) {
- yyerror("invalid snowflake max queued: %lld",
- $3);
- YYERROR;
- }
- cur_bridge->conf.max_queued = $3;
- }
- | PRIMARY HOST STRING {
- /* a Sparkplug host id is a single topic level */
- if (strpbrk($3, "/+#") != NULL) {
- yyerror("invalid snowflake primary host: %s",
- $3);
- free($3);
- YYERROR;
- }
- if (snow_set(cur_bridge->conf.primary_host,
- sizeof(cur_bridge->conf.primary_host), $3,
- "primary host") == -1)
- YYERROR;
- }
- | TIMEOUT NUMBER {
- if ($2 < 1 || $2 > 3600) {
- yyerror("invalid snowflake timeout: %lld", $2);
- YYERROR;
- }
- cur_snow->timeout = $2;
- }
- | TLS CA STRING {
- free(cur_snow->tls_ca_file);
- if ((cur_snow->tls_ca_file = strdup($3)) == NULL)
- fatal("%s: strdup", __func__);
- free($3);
- }
- | TLS SERVERNAME STRING {
- if (snow_set(cur_snow->servername,
- sizeof(cur_snow->servername), $3,
- "servername") == -1)
- YYERROR;
- }
- | TLS NOVERIFY {
- cur_snow->noverify = 1;
- }
- | TOPIC STRING optqos {
- struct bridge_topic *bt;
-
- if (strlen($2) >= MQTTD_MAXTEXT ||
- !mqtt_filter_valid((const uint8_t *)$2,
- strlen($2))) {
- yyerror("invalid snowflake topic: %s", $2);
- free($2);
- YYERROR;
- }
- if ($3 > 1) {
- yyerror("snowflake topics take qos 0 or 1");
- free($2);
- YYERROR;
- }
- if ((bt = calloc(1, sizeof(*bt))) == NULL)
- fatal("%s: calloc", __func__);
- bt->conf.bridge_id = cur_bridge->conf.id;
- bt->conf.dir = BRIDGE_OUT;
- bt->conf.qos = $3;
- strlcpy(bt->conf.pattern, $2, sizeof(bt->conf.pattern));
- TAILQ_INSERT_TAIL(&cur_bridge->topics, bt, entry);
- cur_topic = bt;
- free($2);
- } optlocal
- ;
-
-optlocal : /* empty */
- | LOCAL STRING {
- if (strlcpy(cur_topic->conf.local_prefix, $2,
- sizeof(cur_topic->conf.local_prefix)) >=
- sizeof(cur_topic->conf.local_prefix)) {
- yyerror("local prefix too long");
- free($2);
- YYERROR;
- }
- free($2);
- }
- ;
-
optprefixes : /* empty */
| optprefixes LOCAL STRING {
if (strlcpy(cur_topic->conf.local_prefix, $3,
{
/* This has to be sorted always. */
static const struct keywords keywords[] = {
- { "account", ACCOUNT },
{ "acl", ACL },
{ "after", AFTER },
{ "alias", ALIAS },
{ "auth", AUTH },
{ "aware", AWARE },
{ "ban", BAN },
- { "batch", BATCH },
{ "both", BOTH },
{ "bridge", BRIDGE },
{ "ca", CA },
{ "exec", EXEC },
{ "expiry", EXPIRY },
{ "failures", FAILURES },
- { "flush", FLUSH },
- { "host", HOST },
{ "in", IN },
{ "include", INCLUDE },
{ "inflight", INFLIGHT },
{ "max", MAX },
{ "name", NAME },
{ "no", NO },
- { "noverify", NOVERIFY },
{ "ocsp", OCSP },
{ "on", ON },
{ "optional", OPTIONAL },
{ "pf", PF },
{ "port", PORT },
{ "prefork", PREFORK },
- { "primary", PRIMARY },
{ "protocols", PROTOCOLS },
{ "proxy", PROXY },
{ "publish", PUBLISH },
{ "rate", RATE },
{ "remote", REMOTE },
{ "retain", RETAIN },
- { "role", ROLE },
- { "servername", SERVERNAME },
{ "session", SESSION },
{ "size", SIZE },
- { "snowflake", SNOWFLAKE },
{ "sparkplug", SPARKPLUG },
{ "subscribe", SUBSCRIBE },
{ "sys", SYS },
{ "table", TABLE },
- { "timeout", TIMEOUT },
{ "tls", TLS },
{ "topic", TOPIC },
{ "user", USER },
{ "version", VERSION },
- { "warehouse", WAREHOUSE },
{ "websocket", WEBSOCKET },
{ "within", WITHIN },
{ "workers", WORKERS },
return (file ? 0 : EOF);
}
-/* copy a snowflake string option, consuming the token's string */
-static int
-snow_set(char *dst, size_t dstlen, char *val, const char *what)
-{
- if (strlen(val) == 0 || strlcpy(dst, val, dstlen) >= dstlen) {
- yyerror("invalid snowflake %s: %s", what, val);
- free(val);
- return (-1);
- }
- free(val);
- return (0);
-}
-
static void
listen_init(void)
{
blob - df38f903acf73f445e297d30f3a94aed3fcaf5d19a5784b74d3a76be37a283bc
blob + d6a1aed8d776e598b45367dd58df293d2cafb48a33d8cd0ab23704a0d9970d47
--- mqttd/session.c
+++ mqttd/session.c
/*
* Clients get the smaller of their receive maximum and our limit. A
- * bridge or sink negotiated its window with the remote and may hold far
- * more, which is how a sink accumulates hours of rows before an insert.
+ * bridge negotiated its window with the remote and may hold far more.
*/
static uint32_t
session_window(struct session *s)
blob - c32f39c3180ab89146c41d52a0ceda11ae00b804a172efac57658c912c8a1277 (mode 644)
blob + /dev/null
--- mqttd/snowflake.c
+++ /dev/null
-/*
- * Copyright (c) 2026 Tracey Emery <tracey@openbsd.org>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-/*
- * The snowflake process looks like a sockets process to the broker and
- * like a remote broker to a bridge: each sink is a virtual connection
- * that accepts CONNECT, answers CONNACK with Receive Maximum set to the
- * batch size, collects the PUBLISHes it is sent into rows, inserts them
- * through the Snowflake SQL REST API and acknowledges only what the
- * warehouse confirmed. Repeated failures close the virtual connection
- * so the broker's bridge logic queues, backs off and replays.
- *
- * HTTPS here is synchronous: the broker never has more than one batch
- * outstanding per sink, so a slow warehouse stalls only this process.
- */
-
-#include <sys/types.h>
-#include <sys/queue.h>
-#include <sys/socket.h>
-#include <sys/tree.h>
-#include <sys/uio.h>
-
-#include <netinet/in.h>
-
-#include <errno.h>
-#include <event.h>
-#include <imsg.h>
-#include <poll.h>
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <tls.h>
-#include <unistd.h>
-
-#include "mqttd.h"
-#include "mqtt.h"
-
-struct row {
- TAILQ_ENTRY(row) entry;
- uint64_t received_ms;
- uint16_t pid;
- uint8_t qos;
- uint8_t retain;
- char *topic;
- uint8_t *payload;
- uint32_t payload_len;
-};
-TAILQ_HEAD(rows, row);
-
-struct sink {
- TAILQ_ENTRY(sink) entry;
- struct snow_config conf;
- struct tls_config *tls_config;
- struct sockaddr_storage addrs[MQTTD_SNOW_MAXADDR];
- int naddrs;
- int resolving;
- char token[MQTTD_JWT_MAX];
- time_t token_expires;
- int token_pending;
- /* the virtual connection the broker's bridge sees */
- uint32_t conn_id;
- int open;
- uint8_t version;
- uint8_t *inbuf;
- uint32_t inlen;
- uint32_t intotal;
- struct rows rows;
- uint32_t nrows;
- struct event flush_ev;
- int failures;
- uint64_t inserted;
-};
-TAILQ_HEAD(sinklist, sink);
-
-static void snow_dispatch_parent(int, short, void *);
-static void snow_dispatch_broker(int, short, void *);
-static void snow_sighdlr(int, short, void *);
-static __dead void
- snow_shutdown(void);
-static struct sink
- *sink_find(uint32_t);
-static struct sink
- *sink_byconn(uint32_t);
-static void snow_config_recv(struct imsg *);
-static void snow_ca_recv(struct imsg *);
-static void snow_resolved(struct imsg *);
-static void snow_jwt_reply(struct imsg *);
-static void snow_want_resolve(struct sink *);
-static void snow_want_token(struct sink *);
-static void snow_bridge_connect(struct imsg *);
-static void snow_conn_packet(struct imsg *);
-static void snow_conn_close(struct imsg *);
-static void snow_packet(struct sink *, const uint8_t *, size_t);
-static void snow_send(struct sink *, const struct mqtt_buf *);
-static void snow_virtual_close(struct sink *, const char *);
-static void snow_flush_cb(int, short, void *);
-static void snow_flush(struct sink *);
-static void snow_rows_free(struct sink *);
-static int snow_insert(struct sink *, uint32_t *, int *);
-static int snow_https(struct sink *, const char *, const char *,
- const char *, int *, char **, size_t *);
-static int tls_io(struct tls *, int, int);
-static void sql_quote(struct mqtt_buf *, const char *, size_t);
-static void json_quote(struct mqtt_buf *, const char *, size_t);
-static int json_string(const char *, const char *, char *, size_t);
-
-static struct imsgev *snow_iev_parent;
-static struct imsgev *snow_iev_broker;
-static struct sinklist sinks = TAILQ_HEAD_INITIALIZER(sinks);
-static uint32_t snow_next_conn;
-
-static struct event ev_sigint;
-static struct event ev_sigterm;
-static struct event ev_sighup;
-static struct event ev_sigpipe;
-static struct event ev_sigusr1;
-
-__dead void
-snowflake(int fd_parent, int fd_broker)
-{
- log_procinit("snowflake");
- proc_privdrop("snowflake");
-
- event_init();
-
- signal_set(&ev_sigint, SIGINT, snow_sighdlr, NULL);
- signal_set(&ev_sigterm, SIGTERM, snow_sighdlr, NULL);
- signal_set(&ev_sighup, SIGHUP, snow_sighdlr, NULL);
- signal_set(&ev_sigpipe, SIGPIPE, snow_sighdlr, NULL);
- signal_set(&ev_sigusr1, SIGUSR1, snow_sighdlr, NULL);
- signal_add(&ev_sigint, NULL);
- signal_add(&ev_sigterm, NULL);
- signal_add(&ev_sighup, NULL);
- signal_add(&ev_sigpipe, NULL);
- signal_add(&ev_sigusr1, NULL);
-
- if ((snow_iev_parent = calloc(1, sizeof(*snow_iev_parent))) == NULL)
- fatal("%s: calloc", __func__);
- imsg_event_init(snow_iev_parent, fd_parent, snow_dispatch_parent);
-
- if ((snow_iev_broker = calloc(1, sizeof(*snow_iev_broker))) == NULL)
- fatal("%s: calloc", __func__);
- imsg_event_init(snow_iev_broker, fd_broker, snow_dispatch_broker);
-
- if (pledge("stdio inet", NULL) == -1)
- fatal("pledge");
-
- event_dispatch();
-
- snow_shutdown();
-}
-
-static void
-snow_sighdlr(int sig, short event, void *arg)
-{
- switch (sig) {
- case SIGINT:
- case SIGTERM:
- snow_shutdown();
- break;
- case SIGHUP:
- case SIGPIPE:
- case SIGUSR1:
- break;
- default:
- fatalx("%s: unexpected signal", __func__);
- }
-}
-
-static __dead void
-snow_shutdown(void)
-{
- struct sink *s;
-
- while ((s = TAILQ_FIRST(&sinks)) != NULL) {
- TAILQ_REMOVE(&sinks, s, entry);
- snow_rows_free(s);
- if (s->tls_config != NULL)
- tls_config_free(s->tls_config);
- free(s->conf.tls_ca);
- free(s->inbuf);
- explicit_bzero(s, sizeof(*s));
- free(s);
- }
-
- imsg_event_shutdown(snow_iev_parent);
- free(snow_iev_parent);
- imsg_event_shutdown(snow_iev_broker);
- free(snow_iev_broker);
-
- log_info("snowflake exiting, pid %d", getpid());
- exit(0);
-}
-
-static struct sink *
-sink_find(uint32_t id)
-{
- struct sink *s;
-
- TAILQ_FOREACH(s, &sinks, entry)
- if (s->conf.id == id)
- return (s);
- return (NULL);
-}
-
-static struct sink *
-sink_byconn(uint32_t conn_id)
-{
- struct sink *s;
-
- TAILQ_FOREACH(s, &sinks, entry)
- if (s->open && s->conn_id == conn_id)
- return (s);
- return (NULL);
-}
-
-static void
-snow_dispatch_parent(int fd, short event, void *arg)
-{
- struct imsgev *iev = arg;
- struct imsgbuf *ibuf = &iev->ibuf;
- struct imsg imsg;
- int n, verbose;
-
- if (event & EV_READ) {
- if ((n = imsgbuf_read(ibuf)) == -1)
- fatal("%s: imsgbuf_read", __func__);
- if (n == 0) {
- log_warnx("%s: lost parent process", __func__);
- snow_shutdown();
- }
- }
- if (event & EV_WRITE) {
- if (imsgbuf_write(ibuf) == -1) {
- if (errno == EPIPE)
- snow_shutdown();
- fatal("%s: imsgbuf_write", __func__);
- }
- }
-
- for (;;) {
- if ((n = imsgbuf_get(ibuf, &imsg)) == -1)
- fatal("%s: imsgbuf_get", __func__);
- if (n == 0)
- break;
-
- switch (imsg.hdr.type) {
- case IMSG_CFG_GLOBAL:
- config_recv_global(env, &imsg);
- break;
- case IMSG_CFG_SNOW:
- snow_config_recv(&imsg);
- break;
- case IMSG_CFG_TLS:
- snow_ca_recv(&imsg);
- break;
- case IMSG_CFG_DONE:
- imsg_compose_event(iev, IMSG_CFG_DONE, 0, 0, -1, NULL,
- 0);
- break;
- case IMSG_CTL_START:
- break;
- case IMSG_CTL_VERBOSE:
- IMSG_SIZE_CHECK(&imsg, &verbose);
- memcpy(&verbose, imsg.data, sizeof(verbose));
- log_setverbose(verbose);
- break;
- case IMSG_SNOW_RESOLVED:
- snow_resolved(&imsg);
- break;
- case IMSG_SNOW_JWT_REPLY:
- snow_jwt_reply(&imsg);
- break;
- default:
- log_warnx("%s: unexpected imsg %d from parent",
- __func__, imsg.hdr.type);
- break;
- }
- imsg_free(&imsg);
- }
- imsg_event_add(iev);
-}
-
-static void
-snow_dispatch_broker(int fd, short event, void *arg)
-{
- struct imsgev *iev = arg;
- struct imsgbuf *ibuf = &iev->ibuf;
- struct imsg imsg;
- int n;
-
- if (event & EV_READ) {
- if ((n = imsgbuf_read(ibuf)) == -1)
- fatal("%s: imsgbuf_read", __func__);
- if (n == 0) {
- log_warnx("%s: lost broker process", __func__);
- snow_shutdown();
- }
- }
- if (event & EV_WRITE) {
- if (imsgbuf_write(ibuf) == -1) {
- if (errno == EPIPE)
- snow_shutdown();
- fatal("%s: imsgbuf_write", __func__);
- }
- }
-
- for (;;) {
- if ((n = imsgbuf_get(ibuf, &imsg)) == -1)
- fatal("%s: imsgbuf_get", __func__);
- if (n == 0)
- break;
-
- switch (imsg.hdr.type) {
- case IMSG_BRIDGE_CONNECT:
- snow_bridge_connect(&imsg);
- break;
- case IMSG_CONN_PACKET:
- snow_conn_packet(&imsg);
- break;
- case IMSG_CONN_CLOSE:
- snow_conn_close(&imsg);
- break;
- case IMSG_CONN_KEEPALIVE:
- break;
- default:
- log_warnx("%s: unexpected imsg %d from broker",
- __func__, imsg.hdr.type);
- break;
- }
- imsg_free(&imsg);
- }
- imsg_event_add(iev);
-}
-
-static void
-snow_config_recv(struct imsg *imsg)
-{
- struct sink *s;
-
- if ((s = calloc(1, sizeof(*s))) == NULL)
- fatal("%s: calloc", __func__);
- IMSG_SIZE_CHECK(imsg, &s->conf);
- memcpy(&s->conf, imsg->data, sizeof(s->conf));
- s->conf.name[sizeof(s->conf.name) - 1] = '\0';
- s->conf.host[sizeof(s->conf.host) - 1] = '\0';
- s->conf.servername[sizeof(s->conf.servername) - 1] = '\0';
- s->conf.key = NULL;
- s->conf.key_len = 0;
- s->conf.tls_ca_file = NULL;
- s->conf.tls_ca = NULL;
- s->conf.tls_ca_len = 0;
- TAILQ_INIT(&s->rows);
- evtimer_set(&s->flush_ev, snow_flush_cb, s);
- TAILQ_INSERT_TAIL(&sinks, s, entry);
- log_debug("%s: sink %s to %s configured", __func__, s->conf.name,
- s->conf.host);
-}
-
-static void
-snow_ca_recv(struct imsg *imsg)
-{
- struct tls_config_msg tls;
- struct sink *s;
- struct bridge_config shim;
-
- IMSG_SIZE_CHECK(imsg, &tls);
- memcpy(&tls, imsg->data, sizeof(tls));
- if ((s = sink_find(tls.id)) == NULL)
- return;
-
- /* reuse the bridge CA receiver by lending it our fields */
- memset(&shim, 0, sizeof(shim));
- shim.id = s->conf.id;
- strlcpy(shim.name, s->conf.name, sizeof(shim.name));
- shim.tls_ca = s->conf.tls_ca;
- shim.tls_ca_len = s->conf.tls_ca_len;
- if (config_recv_bridge_ca(&shim, imsg) == -1)
- log_warnx("snowflake %s: bad ca material", s->conf.name);
- s->conf.tls_ca = shim.tls_ca;
- s->conf.tls_ca_len = shim.tls_ca_len;
-}
-
-static void
-snow_want_resolve(struct sink *s)
-{
- struct snow_resolve r;
-
- if (s->resolving)
- return;
- s->resolving = 1;
- r.id = s->conf.id;
- imsg_compose_event(snow_iev_parent, IMSG_SNOW_RESOLVE, 0, 0, -1, &r,
- sizeof(r));
-}
-
-static void
-snow_resolved(struct imsg *imsg)
-{
- struct snow_resolved r;
- struct sink *s;
-
- IMSG_SIZE_CHECK(imsg, &r);
- memcpy(&r, imsg->data, sizeof(r));
- if ((s = sink_find(r.id)) == NULL)
- return;
- s->resolving = 0;
- if (r.n <= 0 || r.n > MQTTD_SNOW_MAXADDR) {
- log_warnx("snowflake %s: %s does not resolve", s->conf.name,
- s->conf.host);
- s->naddrs = 0;
- return;
- }
- memcpy(s->addrs, r.addrs, sizeof(s->addrs));
- s->naddrs = r.n;
-}
-
-static void
-snow_want_token(struct sink *s)
-{
- uint32_t id = s->conf.id;
-
- if (s->token_pending)
- return;
- s->token_pending = 1;
- imsg_compose_event(snow_iev_parent, IMSG_SNOW_JWT_REQUEST, 0, 0, -1,
- &id, sizeof(id));
-}
-
-static void
-snow_jwt_reply(struct imsg *imsg)
-{
- struct snow_jwt_reply r;
- struct sink *s;
-
- IMSG_SIZE_CHECK(imsg, &r);
- memcpy(&r, imsg->data, sizeof(r));
- r.token[sizeof(r.token) - 1] = '\0';
- if ((s = sink_find(r.id)) == NULL)
- goto done;
- s->token_pending = 0;
- if (!r.ok) {
- log_warnx("snowflake %s: parent could not sign a token",
- s->conf.name);
- s->token[0] = '\0';
- s->token_expires = 0;
- goto done;
- }
- strlcpy(s->token, r.token, sizeof(s->token));
- s->token_expires = r.expires;
-done:
- explicit_bzero(&r, sizeof(r));
-}
-
-/* the broker's bridge wants its remote; we are it */
-static void
-snow_bridge_connect(struct imsg *imsg)
-{
- struct conn_open co;
- struct sink *s;
- uint32_t id;
-
- IMSG_SIZE_CHECK(imsg, &id);
- memcpy(&id, imsg->data, sizeof(id));
- if ((s = sink_find(id)) == NULL) {
- log_warnx("%s: unknown sink %u", __func__, id);
- imsg_compose_event(snow_iev_broker, IMSG_BRIDGE_FAILED, 0, 0,
- -1, &id, sizeof(id));
- return;
- }
-
- do {
- s->conn_id = ++snow_next_conn;
- } while (s->conn_id == 0 || sink_byconn(s->conn_id) != NULL);
- s->open = 1;
- s->failures = 0;
- free(s->inbuf);
- s->inbuf = NULL;
-
- snow_want_resolve(s);
- snow_want_token(s);
-
- memset(&co, 0, sizeof(co));
- co.id = s->conn_id;
- co.bridge_id = s->conf.id;
- co.tls = 1;
- snprintf(co.peer, sizeof(co.peer), "snowflake:%s", s->conf.host);
- imsg_compose_event(snow_iev_broker, IMSG_CONN_OPEN, 0, 0, -1, &co,
- sizeof(co));
-}
-
-static void
-snow_conn_close(struct imsg *imsg)
-{
- struct conn_close cc;
- struct sink *s;
-
- IMSG_SIZE_CHECK(imsg, &cc);
- memcpy(&cc, imsg->data, sizeof(cc));
- if ((s = sink_byconn(cc.id)) == NULL)
- return;
- s->open = 0;
- evtimer_del(&s->flush_ev);
- snow_rows_free(s);
-}
-
-static void
-snow_conn_packet(struct imsg *imsg)
-{
- struct conn_pkt hdr;
- struct sink *s;
- uint8_t *p = imsg->data, *buf;
- size_t len = IMSG_DATA_SIZE(imsg), total;
-
- if (len < sizeof(hdr))
- return;
- memcpy(&hdr, p, sizeof(hdr));
- p += sizeof(hdr);
- len -= sizeof(hdr);
- if (hdr.len != len || (s = sink_byconn(hdr.id)) == NULL)
- return;
- if (hdr.total > env->conf.max_packet_size || hdr.offset > hdr.total ||
- hdr.len > hdr.total - hdr.offset)
- return;
-
- if (hdr.offset == 0) {
- free(s->inbuf);
- s->inbuf = NULL;
- if (hdr.total == hdr.len) {
- snow_packet(s, p, len);
- return;
- }
- if ((s->inbuf = malloc(hdr.total)) == NULL)
- return;
- s->intotal = hdr.total;
- s->inlen = 0;
- }
- if (s->inbuf == NULL || hdr.total != s->intotal ||
- hdr.offset != s->inlen)
- return;
- memcpy(s->inbuf + hdr.offset, p, len);
- s->inlen += len;
- if (s->inlen == s->intotal) {
- buf = s->inbuf;
- total = s->intotal;
- s->inbuf = NULL;
- snow_packet(s, buf, total);
- free(buf);
- }
-}
-
-static void
-snow_send(struct sink *s, const struct mqtt_buf *b)
-{
- struct conn_pkt hdr;
- struct iovec iov[2];
- size_t off = 0, n;
-
- hdr.id = s->conn_id;
- hdr.total = b->len;
- while (off < b->len) {
- n = b->len - off;
- if (n > CONN_PKT_CHUNK)
- n = CONN_PKT_CHUNK;
- hdr.offset = off;
- hdr.len = n;
- iov[0].iov_base = &hdr;
- iov[0].iov_len = sizeof(hdr);
- iov[1].iov_base = b->data + off;
- iov[1].iov_len = n;
- if (imsg_composev_event(snow_iev_broker, IMSG_CONN_PACKET, 0,
- 0, -1, iov, 2) == -1)
- return;
- off += n;
- }
-}
-
-/* drop the virtual connection; the broker queues and comes back later */
-static void
-snow_virtual_close(struct sink *s, const char *why)
-{
- struct conn_close cc;
-
- log_info("event=snowflake-down sink=\"%s\" reason=\"%s\"",
- s->conf.name, why);
- cc.id = s->conn_id;
- cc.reason = CONN_CLOSE_ERROR;
- imsg_compose_event(snow_iev_broker, IMSG_CONN_CLOSE, 0, 0, -1, &cc,
- sizeof(cc));
- s->open = 0;
- evtimer_del(&s->flush_ev);
- snow_rows_free(s);
- s->naddrs = 0;
-}
-
-static void
-snow_packet(struct sink *s, const uint8_t *data, size_t len)
-{
- struct mqtt_fixed f;
- struct mqtt_connect c;
- struct mqtt_publish p;
- struct mqtt_buf props, out;
- struct row *r;
- struct timeval tv, now;
- const uint8_t *body;
-
- if (mqtt_parse_fixed(data, len, &f) != 0 ||
- (size_t)f.hdrlen + f.remaining != len)
- return;
- body = data + f.hdrlen;
-
- mqtt_buf_init(&props);
- mqtt_buf_init(&out);
-
- switch (f.type) {
- case MQTT_CONNECT:
- if (mqtt_parse_connect(body, f.remaining, &c) != 0) {
- snow_virtual_close(s, "bad CONNECT");
- break;
- }
- s->version = c.version;
- /* let the broker hand us as much as it will ever hold */
- if (s->version == MQTT_V5)
- mqtt_prop_u16(&props, MQTT_PROP_RECEIVE_MAXIMUM,
- MQTTD_SNOW_WINDOW);
- if (mqtt_encode_connack(&out, s->version, 0, MQTT_RC_SUCCESS,
- &props) == 0)
- snow_send(s, &out);
- log_info("event=snowflake-up sink=\"%s\" host=%s table=%s.%s.%s",
- s->conf.name, s->conf.host, s->conf.database,
- s->conf.schema, s->conf.table);
- break;
- case MQTT_PUBLISH:
- if (mqtt_parse_publish(body, f.remaining, f.flags, s->version,
- &p) != 0) {
- snow_virtual_close(s, "bad PUBLISH");
- break;
- }
- if ((r = calloc(1, sizeof(*r))) == NULL ||
- (r->topic = strndup((const char *)p.topic.data,
- p.topic.len)) == NULL ||
- (p.payload_len > 0 &&
- (r->payload = malloc(p.payload_len)) == NULL)) {
- if (r != NULL) {
- free(r->topic);
- free(r);
- }
- snow_virtual_close(s, "out of memory");
- break;
- }
- if (p.payload_len > 0)
- memcpy(r->payload, p.payload, p.payload_len);
- r->payload_len = p.payload_len;
- r->pid = p.packet_id;
- r->qos = p.qos;
- r->retain = p.retain;
- gettimeofday(&now, NULL);
- r->received_ms = (uint64_t)now.tv_sec * 1000 +
- now.tv_usec / 1000;
- TAILQ_INSERT_TAIL(&s->rows, r, entry);
- s->nrows++;
-
- /*
- * The flush interval is the cost knob: one insert every
- * few hours wakes the warehouse far less often than one
- * every few seconds. Rows wait here until then.
- */
- if (!evtimer_pending(&s->flush_ev, NULL)) {
- tv.tv_sec = s->conf.flush;
- tv.tv_usec = 0;
- evtimer_add(&s->flush_ev, &tv);
- }
- break;
- case MQTT_PINGREQ:
- if (mqtt_encode_pingresp(&out) == 0)
- snow_send(s, &out);
- break;
- case MQTT_PUBREL:
- /* QoS 2 is refused at configuration time; be tolerant */
- break;
- case MQTT_DISCONNECT:
- s->open = 0;
- evtimer_del(&s->flush_ev);
- snow_rows_free(s);
- break;
- default:
- break;
- }
-
- mqtt_buf_free(&props);
- mqtt_buf_free(&out);
-}
-
-static void
-snow_rows_free(struct sink *s)
-{
- struct row *r;
-
- while ((r = TAILQ_FIRST(&s->rows)) != NULL) {
- TAILQ_REMOVE(&s->rows, r, entry);
- free(r->topic);
- free(r->payload);
- free(r);
- }
- s->nrows = 0;
-}
-
-static void
-snow_flush_cb(int fd, short event, void *arg)
-{
- snow_flush(arg);
-}
-
-static void
-snow_flush(struct sink *s)
-{
- struct mqtt_buf out;
- struct row *r;
- struct timeval tv;
- int status = 0;
-
- if (!s->open || s->nrows == 0)
- return;
-
- /* wait for the parent's answers, but not forever */
- if (s->naddrs == 0 || s->token[0] == '\0' ||
- s->token_expires - 60 < time(NULL)) {
- if (s->naddrs == 0)
- snow_want_resolve(s);
- if (s->token[0] == '\0' || s->token_expires - 60 < time(NULL))
- snow_want_token(s);
- if (++s->failures >= MQTTD_SNOW_MAXFAIL * 10) {
- snow_virtual_close(s, "no address or token");
- return;
- }
- tv.tv_sec = 1;
- tv.tv_usec = 0;
- evtimer_add(&s->flush_ev, &tv);
- return;
- }
-
- /*
- * Everything accumulated goes out now, as many statements as the
- * row and statement size limits require. Each statement's rows
- * are acknowledged as soon as it is committed, so a failure part
- * way keeps only what was not yet inserted.
- */
- mqtt_buf_init(&out);
- while (s->open && s->nrows > 0) {
- uint32_t sent = 0, done = 0;
-
- if (snow_insert(s, &sent, &status) == -1) {
- s->failures++;
- log_warnx("snowflake %s: insert failed with %u row(s) "
- "left (http %d, failure %d)", s->conf.name,
- s->nrows, status, s->failures);
- if (status == 401 || status == 403) {
- s->token[0] = '\0';
- s->token_expires = 0;
- }
- if (s->failures >= MQTTD_SNOW_MAXFAIL) {
- snow_virtual_close(s,
- "repeated insert failures");
- break;
- }
- tv.tv_sec = s->failures * 2;
- tv.tv_usec = 0;
- evtimer_add(&s->flush_ev, &tv);
- break;
- }
-
- while (done < sent && (r = TAILQ_FIRST(&s->rows)) != NULL) {
- if (r->qos != 0 && mqtt_encode_ack(&out, MQTT_PUBACK,
- s->version, r->pid, MQTT_RC_SUCCESS) == 0)
- snow_send(s, &out);
- TAILQ_REMOVE(&s->rows, r, entry);
- s->nrows--;
- free(r->topic);
- free(r->payload);
- free(r);
- done++;
- }
- s->inserted += sent;
- s->failures = 0;
- log_debug("event=snowflake-flush sink=\"%s\" rows=%u "
- "left=%u total=%llu", s->conf.name, sent, s->nrows,
- (unsigned long long)s->inserted);
- }
- mqtt_buf_free(&out);
-}
-
-static void
-sql_quote(struct mqtt_buf *b, const char *s, size_t len)
-{
- size_t i;
-
- mqtt_buf_u8(b, '\'');
- for (i = 0; i < len; i++) {
- if (s[i] == '\'')
- mqtt_buf_u8(b, '\'');
- if (s[i] == '\\')
- mqtt_buf_u8(b, '\\');
- mqtt_buf_u8(b, s[i]);
- }
- mqtt_buf_u8(b, '\'');
-}
-
-static void
-json_quote(struct mqtt_buf *b, const char *s, size_t len)
-{
- static const char hex[] = "0123456789abcdef";
- size_t i;
- unsigned char c;
-
- mqtt_buf_u8(b, '"');
- for (i = 0; i < len; i++) {
- c = s[i];
- switch (c) {
- case '"':
- case '\\':
- mqtt_buf_u8(b, '\\');
- mqtt_buf_u8(b, c);
- break;
- case '\n':
- mqtt_buf_bytes(b, "\\n", 2);
- break;
- case '\r':
- mqtt_buf_bytes(b, "\\r", 2);
- break;
- case '\t':
- mqtt_buf_bytes(b, "\\t", 2);
- break;
- default:
- if (c < 0x20) {
- mqtt_buf_bytes(b, "\\u00", 4);
- mqtt_buf_u8(b, hex[c >> 4]);
- mqtt_buf_u8(b, hex[c & 0x0f]);
- } else
- mqtt_buf_u8(b, c);
- break;
- }
- }
- mqtt_buf_u8(b, '"');
-}
-
-/* pull the string value of key out of a flat JSON object, crudely */
-static int
-json_string(const char *json, const char *key, char *out, size_t outlen)
-{
- const char *p, *e;
- char pat[64];
- size_t n;
-
- snprintf(pat, sizeof(pat), "\"%s\"", key);
- if ((p = strstr(json, pat)) == NULL)
- return (-1);
- p += strlen(pat);
- while (*p == ' ' || *p == ':')
- p++;
- if (*p != '"')
- return (-1);
- p++;
- if ((e = strchr(p, '"')) == NULL)
- return (-1);
- n = e - p;
- if (n >= outlen)
- return (-1);
- memcpy(out, p, n);
- out[n] = '\0';
- return (0);
-}
-
-/*
- * INSERT INTO db.schema.table (received_at, topic, qos, retain, payload)
- * SELECT TO_TIMESTAMP_LTZ(column1, 3), column2, column3, column4,
- * TO_BINARY(column5, 'HEX') FROM VALUES (...), (...)
- *
- * Takes rows from the head of the queue up to the batch size or the
- * statement size limit and reports how many it covered.
- */
-static int
-snow_insert(struct sink *s, uint32_t *nsent, int *status)
-{
- static const char hex[] = "0123456789abcdef";
- struct mqtt_buf sql, body, path;
- struct row *r;
- char num[32], handle[128], *resp = NULL;
- size_t resplen, i, mark;
- int first = 1, ret = -1, polls;
-
- *nsent = 0;
- mqtt_buf_init(&sql);
- mqtt_buf_init(&body);
- mqtt_buf_init(&path);
-
- mqtt_buf_bytes(&sql, "INSERT INTO ", 12);
- mqtt_buf_bytes(&sql, s->conf.database, strlen(s->conf.database));
- mqtt_buf_u8(&sql, '.');
- mqtt_buf_bytes(&sql, s->conf.schema, strlen(s->conf.schema));
- mqtt_buf_u8(&sql, '.');
- mqtt_buf_bytes(&sql, s->conf.table, strlen(s->conf.table));
- mqtt_buf_bytes(&sql, " (received_at, topic, qos, retain, payload) "
- "SELECT TO_TIMESTAMP_LTZ(column1, 3), column2, column3, column4, "
- "TO_BINARY(column5, 'HEX') FROM VALUES ", 123);
-
- TAILQ_FOREACH(r, &s->rows, entry) {
- if (*nsent >= s->conf.batch)
- break;
- mark = sql.len;
- if (!first)
- mqtt_buf_u8(&sql, ',');
- snprintf(num, sizeof(num), "(%llu,",
- (unsigned long long)r->received_ms);
- mqtt_buf_bytes(&sql, num, strlen(num));
- sql_quote(&sql, r->topic, strlen(r->topic));
- snprintf(num, sizeof(num), ",%u,%s,'", r->qos,
- r->retain ? "true" : "false");
- mqtt_buf_bytes(&sql, num, strlen(num));
- for (i = 0; i < r->payload_len; i++) {
- mqtt_buf_u8(&sql, hex[r->payload[i] >> 4]);
- mqtt_buf_u8(&sql, hex[r->payload[i] & 0x0f]);
- }
- mqtt_buf_bytes(&sql, "')", 2);
- /* Snowflake refuses statements over a megabyte */
- if (sql.len > MQTTD_SNOW_MAXSQL && !first) {
- sql.len = mark;
- break;
- }
- first = 0;
- (*nsent)++;
- }
- if (*nsent == 0)
- goto done;
-
- mqtt_buf_bytes(&body, "{\"statement\":", 13);
- json_quote(&body, (const char *)sql.data, sql.len);
- snprintf(num, sizeof(num), ",\"timeout\":%u", s->conf.timeout);
- mqtt_buf_bytes(&body, num, strlen(num));
- if (s->conf.warehouse[0] != '\0') {
- mqtt_buf_bytes(&body, ",\"warehouse\":", 13);
- json_quote(&body, s->conf.warehouse, strlen(s->conf.warehouse));
- }
- if (s->conf.role[0] != '\0') {
- mqtt_buf_bytes(&body, ",\"role\":", 8);
- json_quote(&body, s->conf.role, strlen(s->conf.role));
- }
- mqtt_buf_bytes(&body, ",\"database\":", 12);
- json_quote(&body, s->conf.database, strlen(s->conf.database));
- mqtt_buf_bytes(&body, ",\"schema\":", 10);
- json_quote(&body, s->conf.schema, strlen(s->conf.schema));
- mqtt_buf_u8(&body, '}');
- mqtt_buf_u8(&body, '\0');
- body.len--;
-
- if (snow_https(s, "POST", "/api/v2/statements", (const char *)body.data,
- status, &resp, &resplen) == -1)
- goto done;
-
- /* 202 means still running: poll the statement handle */
- for (polls = 0; *status == 202 && polls < MQTTD_SNOW_POLLS; polls++) {
- if (resp == NULL ||
- json_string(resp, "statementHandle", handle,
- sizeof(handle)) == -1)
- break;
- free(resp);
- resp = NULL;
- sleep(1);
- mqtt_buf_reset(&path);
- mqtt_buf_bytes(&path, "/api/v2/statements/", 19);
- mqtt_buf_bytes(&path, handle, strlen(handle));
- mqtt_buf_u8(&path, '\0');
- if (snow_https(s, "GET", (const char *)path.data, NULL, status,
- &resp, &resplen) == -1)
- goto done;
- }
-
- if (*status == 200)
- ret = 0;
- else if (resp != NULL)
- log_warnx("snowflake %s: %.*s", s->conf.name,
- (int)(resplen > 300 ? 300 : resplen), resp);
-done:
- free(resp);
- mqtt_buf_free(&sql);
- mqtt_buf_free(&body);
- mqtt_buf_free(&path);
- return (ret);
-}
-
-/* drive a libtls call to completion on a blocking socket with a timeout */
-static int
-tls_io(struct tls *ctx, int fd, int want)
-{
- struct pollfd pfd;
-
- pfd.fd = fd;
- pfd.events = want == TLS_WANT_POLLIN ? POLLIN : POLLOUT;
- return (poll(&pfd, 1, MQTTD_SNOW_DEFAULT_TIMEOUT * 1000) > 0 ? 0 : -1);
-}
-
-/*
- * One HTTPS request per call, Connection: close. Returns -1 on a
- * transport failure, 0 with the status code and body otherwise.
- */
-static int
-snow_https(struct sink *s, const char *method, const char *path,
- const char *body, int *status, char **resp, size_t *resplen)
-{
- struct sockaddr_storage ss;
- struct tls *ctx = NULL;
- struct mqtt_buf req, in;
- struct timeval tv;
- const char *servername;
- char *hdr_end, *p, *e;
- size_t off, clen = 0, hlen, chunk;
- ssize_t n;
- int fd = -1, ret = -1, chunked = 0, i;
-
- *resp = NULL;
- *resplen = 0;
- *status = 0;
- mqtt_buf_init(&req);
- mqtt_buf_init(&in);
-
- if (s->tls_config == NULL) {
- if ((s->tls_config = tls_config_new()) == NULL)
- goto done;
- if (s->conf.noverify) {
- tls_config_insecure_noverifycert(s->tls_config);
- tls_config_insecure_noverifyname(s->tls_config);
- } else if (s->conf.tls_ca != NULL &&
- tls_config_set_ca_mem(s->tls_config, s->conf.tls_ca,
- s->conf.tls_ca_len) == -1) {
- log_warnx("snowflake %s: %s", s->conf.name,
- tls_config_error(s->tls_config));
- goto done;
- }
- }
-
- /* try the resolved addresses in turn */
- for (i = 0; i < s->naddrs && fd == -1; i++) {
- memcpy(&ss, &s->addrs[i], sizeof(ss));
- if (ss.ss_family == AF_INET)
- ((struct sockaddr_in *)&ss)->sin_port = s->conf.port;
- else if (ss.ss_family == AF_INET6)
- ((struct sockaddr_in6 *)&ss)->sin6_port = s->conf.port;
- if ((fd = socket(ss.ss_family, SOCK_STREAM, 0)) == -1)
- continue;
- tv.tv_sec = s->conf.timeout;
- tv.tv_usec = 0;
- setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
- setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
- if (connect(fd, (struct sockaddr *)&ss, ss.ss_len) == -1) {
- log_warn("snowflake %s: connect", s->conf.name);
- close(fd);
- fd = -1;
- }
- }
- if (fd == -1)
- goto done;
-
- servername = s->conf.servername[0] != '\0' ? s->conf.servername :
- s->conf.host;
- if ((ctx = tls_client()) == NULL ||
- tls_configure(ctx, s->tls_config) == -1 ||
- tls_connect_socket(ctx, fd, servername) == -1) {
- log_warnx("snowflake %s: tls: %s", s->conf.name,
- ctx != NULL ? tls_error(ctx) : "no context");
- goto done;
- }
- for (;;) {
- n = tls_handshake(ctx);
- if (n == 0)
- break;
- if (n == TLS_WANT_POLLIN || n == TLS_WANT_POLLOUT) {
- if (tls_io(ctx, fd, n) == -1)
- goto done;
- continue;
- }
- log_warnx("snowflake %s: tls handshake with %s: %s",
- s->conf.name, servername, tls_error(ctx));
- goto done;
- }
-
- mqtt_buf_bytes(&req, method, strlen(method));
- mqtt_buf_u8(&req, ' ');
- mqtt_buf_bytes(&req, path, strlen(path));
- mqtt_buf_bytes(&req, " HTTP/1.1\r\nHost: ", 17);
- mqtt_buf_bytes(&req, s->conf.host, strlen(s->conf.host));
- mqtt_buf_bytes(&req, "\r\nAuthorization: Bearer ", 24);
- mqtt_buf_bytes(&req, s->token, strlen(s->token));
- mqtt_buf_bytes(&req, "\r\nX-Snowflake-Authorization-Token-Type: "
- "KEYPAIR_JWT\r\nAccept: application/json\r\n"
- "User-Agent: mqttd/" MQTTD_VERSION "\r\nConnection: close\r\n",
- strlen("\r\nX-Snowflake-Authorization-Token-Type: KEYPAIR_JWT\r\n"
- "Accept: application/json\r\nUser-Agent: mqttd/" MQTTD_VERSION
- "\r\nConnection: close\r\n"));
- if (body != NULL) {
- char cl[64];
-
- snprintf(cl, sizeof(cl), "Content-Type: application/json\r\n"
- "Content-Length: %zu\r\n", strlen(body));
- mqtt_buf_bytes(&req, cl, strlen(cl));
- }
- mqtt_buf_bytes(&req, "\r\n", 2);
- if (body != NULL)
- mqtt_buf_bytes(&req, body, strlen(body));
-
- for (off = 0; off < req.len; ) {
- n = tls_write(ctx, req.data + off, req.len - off);
- if (n == TLS_WANT_POLLIN || n == TLS_WANT_POLLOUT) {
- if (tls_io(ctx, fd, n) == -1)
- goto done;
- continue;
- }
- if (n < 0) {
- log_warnx("snowflake %s: tls_write: %s", s->conf.name,
- tls_error(ctx));
- goto done;
- }
- off += n;
- }
-
- for (;;) {
- if (mqtt_buf_reserve(&in, 16384) == -1)
- goto done;
- n = tls_read(ctx, in.data + in.len, in.cap - in.len - 1);
- if (n == TLS_WANT_POLLIN || n == TLS_WANT_POLLOUT) {
- if (tls_io(ctx, fd, n) == -1)
- goto done;
- continue;
- }
- if (n < 0) {
- log_warnx("snowflake %s: tls_read: %s", s->conf.name,
- tls_error(ctx));
- goto done;
- }
- if (n == 0)
- break;
- in.len += n;
- if (in.len > MQTTD_SNOW_MAXRESP)
- goto done;
- }
- in.data[in.len] = '\0';
-
- if (strncmp((char *)in.data, "HTTP/1.", 7) != 0 ||
- (hdr_end = strstr((char *)in.data, "\r\n\r\n")) == NULL)
- goto done;
- *status = atoi((char *)in.data + 9);
- hlen = hdr_end + 4 - (char *)in.data;
-
- for (p = (char *)in.data; p < hdr_end; p = e + 2) {
- if ((e = strstr(p, "\r\n")) == NULL)
- break;
- if (strncasecmp(p, "Transfer-Encoding:", 18) == 0 &&
- strcasestr(p, "chunked") != NULL && strcasestr(p,
- "chunked") < e)
- chunked = 1;
- else if (strncasecmp(p, "Content-Length:", 15) == 0)
- clen = strtoul(p + 15, NULL, 10);
- }
-
- if (chunked) {
- /* decode chunks in place into the response buffer */
- if ((*resp = malloc(in.len - hlen + 1)) == NULL)
- goto done;
- p = (char *)in.data + hlen;
- for (;;) {
- chunk = strtoul(p, &e, 16);
- if (e == p || (e = strstr(e, "\r\n")) == NULL)
- break;
- e += 2;
- if (chunk == 0)
- break;
- if ((size_t)((char *)in.data + in.len - e) < chunk)
- break;
- memcpy(*resp + *resplen, e, chunk);
- *resplen += chunk;
- p = e + chunk + 2;
- }
- (*resp)[*resplen] = '\0';
- } else {
- *resplen = in.len - hlen;
- if (clen > 0 && clen < *resplen)
- *resplen = clen;
- if ((*resp = malloc(*resplen + 1)) == NULL)
- goto done;
- memcpy(*resp, in.data + hlen, *resplen);
- (*resp)[*resplen] = '\0';
- }
- ret = 0;
-done:
- if (ctx != NULL) {
- tls_close(ctx);
- tls_free(ctx);
- }
- if (fd != -1)
- close(fd);
- mqtt_buf_free(&req);
- mqtt_buf_free(&in);
- if (ret == -1) {
- free(*resp);
- *resp = NULL;
- *resplen = 0;
- }
- return (ret);
-}