Skip to content

eBPF-based security platform

otel: send a log whose body is a string

John Fastabend1990cf1254b5
diff --git a/testing/functional/otel/t8004-status-otel.sh b/testing/functional/otel/t8004-status-otel.sh
index a57e7e6d..9c86eb22 100644
--- a/testing/functional/otel/t8004-status-otel.sh
+++ b/testing/functional/otel/t8004-status-otel.sh
@@ -215,4 +215,83 @@ test_expect_success 'status --include-system prints kernel config' '
rm -rf "$work"
'
+test_expect_success 'a keepalive record carries its body as a string and one quote attribute' '
+ work=$(mktemp -d) || return 1
+ quote="attest=/1RDR4AY;sig=ABgACwAg;ak=ACMACwAE" &&
+ printf "%s\303\251%s" \
+ "{\"schema\":\"shirudo.keepalive/1\",\"image_id\":\"a\\\\b \\u00e9 " \
+ "\",\"tamper\":[0,1],\"mask\":null}" >"$work/input" || return 1
+ for verdict in ok degraded init-pending reboot-pending lsm-off tampered
+ do
+ python3 helper/otel-mock.py success "$work/port" "$work/body" \
+ "$work/meta" &
+ server=$!
+ trap "kill $server 2>/dev/null || true; wait $server 2>/dev/null || true; rm -rf $work" EXIT
+ i=0
+ while test ! -s "$work/port" && test "$i" -lt 100; do
+ sleep .02
+ i=$((i + 1))
+ done
+ port=$(cat "$work/port") &&
+ "$H" send-keepalive "http://127.0.0.1:$port" "$verdict" \
+ "$work/input" "$quote" &&
+ wait "$server" &&
+ python3 - "$verdict" "$work/body" "$work/meta" "$work/input" \
+ "$quote" <<'\''PY'\'' || return 1
+import json, socket, sys
+verdict, quote = sys.argv[1], sys.argv[5]
+sent = open(sys.argv[4], "rb").read()
+assert b"\\u00e9 \xc3\xa9" in sent and b"a\\\\b" in sent, sent
+assert json.load(open(sys.argv[3]))["path"] == "/v1/logs"
+rl = json.load(open(sys.argv[2]))["resourceLogs"][0]
+resource = {item["key"]: item["value"] for item in rl["resource"]["attributes"]}
+assert resource == {"service.name": {"stringValue": "shirudo"},
+ "service.version": {"stringValue": "test-1"},
+ "host.name": {"stringValue": socket.gethostname()}}, resource
+assert rl["scopeLogs"][0]["scope"] == {"name": "shirudo.keepalive",
+ "version": "test-1"}
+record = rl["scopeLogs"][0]["logRecords"][0]
+expected = {"ok": (9, "INFO"), "degraded": (13, "WARN"),
+ "init-pending": (13, "WARN"), "reboot-pending": (13, "WARN"),
+ "lsm-off": (17, "ERROR"),
+ "tampered": (17, "ERROR")}[verdict]
+assert (record["severityNumber"], record["severityText"]) == expected
+assert record["eventName"] == "shirudo.keepalive"
+assert record["timeUnixNano"].isdigit()
+assert list(record["body"]) == ["stringValue"], record["body"]
+assert record["body"]["stringValue"].encode() == sent, record["body"]
+assert record["attributes"] == [
+ {"key": "shirudo.quote", "value": {"stringValue": quote}}
+], record["attributes"]
+PY
+ rm -f "$work/port" "$work/body" "$work/meta" || return 1
+ done
+ rm -rf "$work"
+'
+
+test_expect_success 'a keepalive body JSON cannot carry unchanged is not sent' '
+ work=$(mktemp -d) || return 1
+ printf "{\"image_id\":\"\\377\"}" >"$work/latin1" &&
+ printf "{\"image_id\":\"\\000\"}" >"$work/nul" || return 1
+ for input in latin1 nul
+ do
+ python3 helper/otel-mock.py success "$work/port" "$work/body" \
+ "$work/meta" &
+ server=$!
+ trap "kill $server 2>/dev/null || true; wait $server 2>/dev/null || true; rm -rf $work" EXIT
+ i=0
+ while test ! -s "$work/port" && test "$i" -lt 100; do
+ sleep .02
+ i=$((i + 1))
+ done
+ port=$(cat "$work/port") &&
+ ! "$H" send-keepalive "http://127.0.0.1:$port" ok \
+ "$work/$input" quote &&
+ test ! -e "$work/body" || return 1
+ kill "$server" && { wait "$server" || true; } &&
+ rm -f "$work/port" || return 1
+ done
+ rm -rf "$work"
+'
+
test_done
diff --git a/testing/helper/test-otel-map.c b/testing/helper/test-otel-map.c
index ba44902d..f1835aa4 100644
--- a/testing/helper/test-otel-map.c
+++ b/testing/helper/test-otel-map.c
@@ -28,6 +28,8 @@
const char shirudo_version[] = "test-1";
+#define KEEPALIVE_BODY_FILE_MAX (64u * 1024u)
+
struct fixture {
struct map_view view;
struct map_entry entry;
@@ -423,6 +425,29 @@ static int send_status(const char *url, const char *status)
body, (size_t)n) != 0;
}
+static int send_keepalive(const char *url, const char *verdict,
+ const char *body_path, const char *quote)
+{
+ struct otel_opts opts = {
+ .endpoint = url,
+ .retries = 0,
+ };
+ const char *severity_text;
+ char *body = NULL;
+ int severity, ret;
+ size_t len = 0;
+
+ if (read_file(body_path, KEEPALIVE_BODY_FILE_MAX, &body, &len))
+ return 1;
+ severity = status_otel_severity(verdict, &severity_text);
+ ret = otel_send_log_string(&opts, "shirudo.keepalive",
+ "shirudo.keepalive", severity,
+ severity_text, body, len,
+ "shirudo.quote", quote);
+ xfree(body, len + 1);
+ return ret != 0;
+}
+
/*
* A status body of a list of count ones, which the AnyValue form spells
* in some eight times the bytes.
@@ -701,6 +726,8 @@ int main(int argc, char **argv)
return send_metric_env();
if (argc == 4 && strcmp(argv[1], "send-status") == 0)
return send_status(argv[2], argv[3]);
+ if (argc == 6 && strcmp(argv[1], "send-keepalive") == 0)
+ return send_keepalive(argv[2], argv[3], argv[4], argv[5]);
if (argc == 4 && strcmp(argv[1], "send-wide-status") == 0)
return send_wide_status(argv[2], argv[3]);
if (argc == 4 && strcmp(argv[1], "retry-after") == 0) {
diff --git a/testing/unit/json.c b/testing/unit/json.c
index cf391914..b1b95213 100644
--- a/testing/unit/json.c
+++ b/testing/unit/json.c
@@ -453,6 +453,29 @@ static void emitter_takes_well_formed_utf8_only(void)
EXPECT_EQ(emit_string(bad[i], buf, sizeof(buf)), -EILSEQ);
}
+static void emitter_takes_a_string_of_its_length(void)
+{
+ struct json_emit e;
+ char buf[64];
+
+ e = (struct json_emit){ .buf = buf, .cap = sizeof(buf) };
+ json_obj_open(&e);
+ json_kv_string_n(&e, "v", "a\"bc", 3);
+ json_obj_close(&e);
+ ASSERT_EQ(e.err, 0);
+ EXPECT_STREQ(buf, "{\"v\":\"a\\\"b\"}");
+
+ e = (struct json_emit){ .buf = buf, .cap = sizeof(buf) };
+ json_obj_open(&e);
+ json_kv_string_n(&e, "v", "a\0b", 3);
+ EXPECT_EQ(e.err, -EILSEQ);
+
+ e = (struct json_emit){ .buf = buf, .cap = sizeof(buf) };
+ json_obj_open(&e);
+ json_kv_string_n(&e, "v", "a\xc3", 2);
+ EXPECT_EQ(e.err, -EILSEQ);
+}
+
static void emitter_grows_up_to_its_max(void)
{
struct json_emit deep = { .max = 1u << 20 };
@@ -785,6 +808,7 @@ static const struct unit_case cases[] = {
UNIT_CASE(discarded_number_follows_the_grammar),
UNIT_CASE(malformed_utf8_is_refused),
UNIT_CASE(emitter_takes_well_formed_utf8_only),
+ UNIT_CASE(emitter_takes_a_string_of_its_length),
UNIT_CASE(emitter_grows_up_to_its_max),
UNIT_CASE(lossy_writes_replace_bad_bytes),
UNIT_CASE(walk_reports_every_token),
diff --git a/util/json.c b/util/json.c
index 59e3dcf9..26b03519 100644
--- a/util/json.c
+++ b/util/json.c
@@ -223,11 +223,17 @@ void json_arr_close(struct json_emit *e)
void json_kv_string(struct json_emit *e,
const char *key, const char *val)
+{
+ json_kv_string_n(e, key, val, strlen(val));
+}
+
+void json_kv_string_n(struct json_emit *e, const char *key,
+ const char *val, size_t len)
{
sep(e);
put_string(e, key);
put(e, ':');
- put_string(e, val);
+ put_string_n(e, val, len, false);
}
void json_kv_int(struct json_emit *e,
diff --git a/util/json.h b/util/json.h
index 904a4b69..d08dd21a 100644
--- a/util/json.h
+++ b/util/json.h
@@ -61,6 +61,9 @@ void json_arr_close(struct json_emit *e);
void json_key(struct json_emit *e, const char *key);
void json_kv_string(struct json_emit *e,
const char *key, const char *val);
+/* A NUL among the len bytes is -EILSEQ, as malformed UTF-8 is. */
+void json_kv_string_n(struct json_emit *e, const char *key,
+ const char *val, size_t len);
void json_kv_int(struct json_emit *e,
const char *key, __s64 val);
void json_kv_bool(struct json_emit *e,
diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.c
index bbe252db..8fc98efd 100644
--- a/util/upatch_maps_otel.c
+++ b/util/upatch_maps_otel.c
@@ -513,6 +513,20 @@ static void log_close(struct json_emit *e)
otlp_close(e);
}
+static int log_payload_take(struct json_emit *e, char **out,
+ size_t *out_len, size_t *alloc_len)
+{
+ log_close(e);
+ if (e->err) {
+ xfree(e->buf, e->cap);
+ return e->err;
+ }
+ *out = e->buf;
+ *out_len = e->len;
+ *alloc_len = e->cap;
+ return 0;
+}
+
static void emit_map_attributes(struct json_emit *e,
const struct map_view *v)
{
@@ -1582,15 +1596,52 @@ static int build_generic_log_payload(const char *scope_name,
if (tamper_attempts >= 0)
otlp_kv_int(&e, "shirudo.tamper_attempts", tamper_attempts);
json_arr_close(&e);
- log_close(&e);
- if (e.err) {
- xfree(e.buf, e.cap);
- return e.err;
- }
- *out = e.buf;
- *out_len = e.len;
- *alloc_len = e.cap;
- return 0;
+ return log_payload_take(&e, out, out_len, alloc_len);
+}
+
+/*
+ * The body goes out as a string, so that no collector can re-encode it
+ * and the bytes a signature covers arrive as they were signed. JSON
+ * cannot carry a NUL or malformed UTF-8 unchanged; both are refused.
+ */
+static int build_string_log_payload(const char *scope_name,
+ const char *event_name,
+ int severity_number,
+ const char *severity_text,
+ const char *body, size_t body_len,
+ const char *attr_key,
+ const char *attr_value,
+ char **out, size_t *out_len,
+ size_t *alloc_len)
+{
+ struct json_emit e = { .max = JSON_MAX_BYTES };
+
+ if (!scope_name || !event_name || !severity_text || !body ||
+ !attr_key || !attr_value || !out || !out_len || !alloc_len)
+ return -EINVAL;
+ log_open(&e, scope_name, severity_number, severity_text, event_name);
+ json_obj_open_key(&e, "body");
+ json_kv_string_n(&e, "stringValue", body, body_len);
+ json_obj_close(&e);
+ json_arr_open(&e, "attributes");
+ otlp_kv_string(&e, attr_key, attr_value);
+ json_arr_close(&e);
+ return log_payload_take(&e, out, out_len, alloc_len);
+}
+
+static int otel_log_post(const struct otel_opts *opts, const char *payload,
+ size_t payload_len)
+{
+ void *state = NULL;
+ int ret;
+
+ ret = otel_open_transport(OTEL_LOG, opts ? opts->endpoint : NULL,
+ opts, NULL, &state);
+ if (ret)
+ return ret;
+ ret = otel_post(state, payload, payload_len);
+ otel_close(state);
+ return ret;
}
int otel_send_log_json(const struct otel_opts *opts,
@@ -1603,25 +1654,43 @@ int otel_send_log_json(const struct otel_opts *opts,
__s64 tamper_attempts,
const char *body_json, size_t body_len)
{
- char *payload = NULL;
- void *state = NULL;
size_t payload_len = 0, alloc_len = 0;
+ char *payload = NULL;
int ret;
- ret = otel_open_transport(OTEL_LOG,
- opts ? opts->endpoint : NULL, opts, NULL, &state);
- if (ret)
- return ret;
ret = build_generic_log_payload(scope_name, event_name,
severity_number, severity_text,
status, version_id,
tamper_attempts,
body_json, body_len,
&payload, &payload_len, &alloc_len);
- if (!ret)
- ret = otel_post(state, payload, payload_len);
+ if (ret)
+ return ret;
+ ret = otel_log_post(opts, payload, payload_len);
+ xfree(payload, alloc_len);
+ return ret;
+}
+
+int otel_send_log_string(const struct otel_opts *opts,
+ const char *scope_name,
+ const char *event_name,
+ int severity_number,
+ const char *severity_text,
+ const char *body, size_t body_len,
+ const char *attr_key, const char *attr_value)
+{
+ size_t payload_len = 0, alloc_len = 0;
+ char *payload = NULL;
+ int ret;
+
+ ret = build_string_log_payload(scope_name, event_name,
+ severity_number, severity_text,
+ body, body_len, attr_key, attr_value,
+ &payload, &payload_len, &alloc_len);
+ if (ret)
+ return ret;
+ ret = otel_log_post(opts, payload, payload_len);
xfree(payload, alloc_len);
- otel_close(state);
return ret;
}
diff --git a/util/upatch_maps_otel.h b/util/upatch_maps_otel.h
index cd2bc514..22f57410 100644
--- a/util/upatch_maps_otel.h
+++ b/util/upatch_maps_otel.h
@@ -71,6 +71,19 @@ int otel_send_log_json(const struct otel_opts *opts,
const char *version_id,
__s64 tamper_attempts,
const char *body_json, size_t body_len);
+/*
+ * Send body_len bytes as the string body of one OTLP log with a single
+ * string attribute. The receiver decodes the very bytes passed in; a
+ * body with a NUL or malformed UTF-8 is -EILSEQ and nothing is sent.
+ */
+int otel_send_log_string(const struct otel_opts *opts,
+ const char *scope_name,
+ const char *event_name,
+ int severity_number,
+ const char *severity_text,
+ const char *body, size_t body_len,
+ const char *attr_key,
+ const char *attr_value) __must_check;
int otel_retry_after(const char *value, time_t now, __u64 *seconds);
int status_otel_severity(const char *status, const char **text);