mirror of
https://github.com/lemonade-sdk/lemonade.git
synced 2026-11-23 08:49:28 -10:00
* feat(cloud): configurable auth header name and prefix per provider
Some OpenAI-compatible gateways authenticate with a differently-named
header and no "Bearer " prefix. Add optional auth_header_name and
auth_header_prefix to the cloud provider record, defaulting to the
previously hardcoded Authorization/Bearer so existing providers and
config files are unaffected.
install() now takes an options struct rather than a run of positional
strings, and the insecure-http opt-in uses a dedicated setter so a
partial update cannot silently reset the auth header to its default.
* fix(cloud): validate auth headers and stop re-install clobbering them
Address review feedback on the per-provider auth header settings.
Validate auth_header_name as a non-empty RFC 7230 token and reject
control characters in auth_header_prefix, at /v1/install and again in
load_from_config() for values arriving from a hand-edited config.json.
This closes the CRLF header-injection path and the empty-name case that
produced a malformed ": Bearer <key>" line, sending requests out
effectively unauthenticated. A bad value on disk falls back to the
default rather than disabling the provider.
InstallOptions fields are now optional and applied only when the request
supplies them, so a re-install that omits the auth flags — the desktop
add-provider form, or a CLI call that changes only --base-url — keeps
the provider's configured header instead of silently resetting it to
Authorization/Bearer and 401ing against the gateway.
Surface auth_header_prefix alongside auth_header_name in system-info and
the install response, and print both from `cloud list` when either is
non-default, quoting the prefix so an empty one is visible.
discover_models() and ResolvedCreds take the registry's AuthHeader
struct rather than two loose strings, and the three outbound auth sites
share one header-building helper.
* docs(cloud): explain the auth-header concept once
The comment-slop pre-commit hook flagged the auth header name/prefix
concept as re-explained at three sites. Keep the explanation on
CloudProviderRegistry::Record, where the fields are declared, and point
at it from the others.
* test(cloud): run the cloud discovery policy test in CI
The auth-header construction it covers was untested in packaging CI.
* feat(cloud): relay Anthropic-wire-format providers through /v1/messages
Some providers serve their models over the Anthropic Messages wire format
rather than OpenAI chat completions. Add a per-provider wire_format field
(default "openai") and, when it is "anthropic", relay /v1/messages to the
provider's /messages endpoint byte-for-byte, rewriting only the model id.
Passing the body through rather than converting it is both smaller and
lossless: thinking blocks, tool use, cache control, and future Anthropic
fields all survive. The OpenAI-shaped endpoints reject these providers with
a 400 pointing at /v1/messages, since the provider does not serve that shape.
* fix(cloud): send anthropic-version on discovery too
An anthropic-wire-format provider was only sent anthropic-version on the
/v1/messages relay, so a strict gateway rejected GET /models and the
provider registered zero models -- leaving the relay path unreachable.
Extract CloudServer::upstream_headers() so relay and discovery build the
same header map and cannot drift.
Document that the wire format and the auth header are independent
settings that usually go together, since the Anthropic API authenticates
with x-api-key rather than Authorization: Bearer.
* docs(cloud): trim restated wire-format and validator comments
* test(cloud): cover upstream header and URL construction
Adds the /v1-stripping upstream_url() helper so a gateway serving the
OpenAI shape at a base with no version segment is addressed correctly,
and unit-tests it alongside the default-auth-header case.
* docs(cloud): tighten anthropic wire-format prose
* docs(cloud): tighten wire-format comments
* refactor(cloud): build relay errors with the shared Anthropic helper
The relay hardcoded its own status-to-error-type mapping, duplicating
build_anthropic_error() and flattening 529 to a generic api_error.
* refactor(cloud): tighten upstream URL joining and relay ownership
Match /v1 on a segment boundary so an endpoint like /v1x is not
mis-joined, drop a redundant normalization now handled by upstream_url,
and move the relay context into the streaming provider instead of
copying it twice.
* fix(cloud): address review feedback on the Anthropic wire-format relay
- Forward `anthropic-beta` / `anthropic-version` from the client, and
reattach `?beta=true` to the upstream URL where the API reads it.
- Start the upstream request before the handler returns so a non-200 is
relayed as a real status instead of a 200 with an error body; httplib
commits the status line only after the handler returns, so a chunked
content provider cannot influence it.
- Relay `retry-after`, `request-id` and `anthropic-ratelimit-*` back to
the client, which requires capturing response headers in HttpClient.
- Reject a registry entry with an empty checkpoint with an actionable
500, matching what CloudServer::load() does for the OpenAI endpoints.
- Emit X-Lemonade-Warning on the relay path too, not just the converted
one.
- Document that /chat/completions against an anthropic-wire provider
reports its 400 as an SSE error frame when stream is true.
* fix(cloud): revert the threaded Anthropic streaming relay
The threaded handoff let the relay report a real upstream status on a
streaming request, but it blocked the handler before it returned — a
window where neither DataSink::is_writable nor ~Response's resource
releaser exists yet. A client that disconnected in that window stranded
the worker thread on an untimed wait with the upstream transfer still
open, permanently.
Measured against a hung upstream, 260 aborted streaming requests: the
server wedged at health=000 with 512 upstream connections held and never
recovered. The same load on the pre-change relay kept health=200 and held
zero connections.
Streaming goes back to doing all of its blocking inside the content
provider, where the peer is observable on every iteration. The cost is
that an upstream non-200 is again reported as an SSE error event on a
200; the docs now state that directly rather than claiming the status is
preserved on both paths.
The non-streaming path keeps the real status and the relayed
retry-after / request-id / anthropic-ratelimit-* headers.
* fix(http): honor the 0-means-default timeout convention on POST
HttpClient::get treated timeout_seconds=0 as "use default_timeout_seconds_",
but post, post_multipart, and post_stream passed it straight to CURLOPT_TIMEOUT,
where curl reads 0 as "no timeout". forward_request, post_with_auth, and the
router's streaming calls all default to 0, so a silent upstream held the calling
httplib worker indefinitely. The pool is bounded, so enough stalled requests
left the server unable to answer anything, including /health.
This also made global_timeout inert: it is pushed into set_default_timeout,
which only get consulted.
post_stream bounds silence rather than total duration, since a flat timeout
would cut off a long but healthy generation. kNoTimeout is the explicit opt-out
now that 0 has a meaning.
* fix(cloud): trim oversized test file-header comments
check_comment_slop's MAX_BLOCK_LINES=12 flags the file-header blocks
added in efb47a5e (review-fix commit). Trims restated per-function
walkthroughs, keeping the one non-obvious invariant per file, matching
the diff the reviewer verified in PR #3223.
---------
Co-authored-by: Ramakrishnan Sivakumar <[email protected]>
302 lines
14 KiB
C++
302 lines
14 KiB
C++
// Unit tests for the CloudProviderRegistry per-provider config fields that
|
|
// describe how to reach a gateway. All must reproduce the previously
|
|
// hardcoded behavior when omitted, and an explicitly empty auth header prefix
|
|
// must stay distinct from "not provided" -- some gateways have no prefix.
|
|
//
|
|
// Checks use an explicit pass/fail counter (not assert()) so the test stays
|
|
// effective under the Release build the CI `default` preset uses, where
|
|
// -DNDEBUG would compile assert() to a no-op.
|
|
|
|
#include <cstdio>
|
|
#include <string>
|
|
|
|
#include <lemon/cloud_provider_registry.h>
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
using lemon::CloudProviderRegistry;
|
|
using json = nlohmann::json;
|
|
|
|
struct TestResult {
|
|
int passed = 0;
|
|
int failed = 0;
|
|
|
|
void check(bool cond, const std::string& name) {
|
|
if (cond) {
|
|
printf("[PASS] %s\n", name.c_str());
|
|
++passed;
|
|
} else {
|
|
printf("[FAIL] %s\n", name.c_str());
|
|
++failed;
|
|
}
|
|
}
|
|
};
|
|
|
|
int main() {
|
|
TestResult r;
|
|
printf("=== CloudProviderRegistry config Unit Tests ===\n\n");
|
|
|
|
{
|
|
CloudProviderRegistry registry;
|
|
registry.install("fireworks", "https://api.fireworks.ai/inference/v1");
|
|
const auto header = registry.auth_header_for("fireworks");
|
|
r.check(header.name == "Authorization",
|
|
"install() with default options -> default header name Authorization");
|
|
r.check(header.prefix == "Bearer ",
|
|
"install() with default options -> default header prefix 'Bearer '");
|
|
}
|
|
|
|
{
|
|
CloudProviderRegistry::InstallOptions options;
|
|
options.auth_header_name = "X-Api-Key";
|
|
options.auth_header_prefix = "";
|
|
|
|
CloudProviderRegistry registry;
|
|
registry.install("acme", "https://gateway.example.com/v1", options);
|
|
const auto header = registry.auth_header_for("acme");
|
|
r.check(header.name == "X-Api-Key",
|
|
"install() with explicit header name -> stored verbatim");
|
|
r.check(header.prefix.empty(),
|
|
"install() with explicit empty prefix -> stored as empty, not defaulted");
|
|
}
|
|
|
|
{
|
|
// Round-trip through to_config_array() / load_from_config(), the path
|
|
// used to persist and reload config.json across a server restart.
|
|
CloudProviderRegistry::InstallOptions options;
|
|
options.auth_header_name = "X-Api-Key";
|
|
options.auth_header_prefix = "";
|
|
|
|
CloudProviderRegistry registry;
|
|
registry.install("acme", "https://gateway.example.com/v1", options);
|
|
|
|
CloudProviderRegistry reloaded;
|
|
reloaded.load_from_config(registry.to_config_array());
|
|
const auto header = reloaded.auth_header_for("acme");
|
|
r.check(header.name == "X-Api-Key",
|
|
"round-trip through config array -> header name preserved");
|
|
r.check(header.prefix.empty(),
|
|
"round-trip through config array -> empty prefix preserved");
|
|
}
|
|
|
|
{
|
|
// A default-configured provider must not gain new keys in config.json,
|
|
// so upgrading lemonade leaves an existing file byte-identical.
|
|
CloudProviderRegistry registry;
|
|
registry.install("fireworks", "https://api.fireworks.ai/inference/v1");
|
|
const json array = registry.to_config_array();
|
|
r.check(array.size() == 1 && !array[0].contains("auth_header_name") &&
|
|
!array[0].contains("auth_header_prefix"),
|
|
"default auth header is omitted from the serialized config");
|
|
}
|
|
|
|
{
|
|
// An old config.json written before these fields existed has no
|
|
// auth_header_* keys at all -- must fall back to the previously
|
|
// hardcoded Bearer behavior, not an empty header.
|
|
CloudProviderRegistry registry;
|
|
json legacy_array = json::array({
|
|
{{"name", "openai"}, {"base_url", "https://api.openai.com/v1"}}
|
|
});
|
|
registry.load_from_config(legacy_array);
|
|
const auto header = registry.auth_header_for("openai");
|
|
r.check(header.name == "Authorization",
|
|
"legacy config entry missing header fields -> defaults to Authorization");
|
|
r.check(header.prefix == "Bearer ",
|
|
"legacy config entry missing header fields -> defaults to 'Bearer '");
|
|
}
|
|
|
|
{
|
|
CloudProviderRegistry registry;
|
|
const auto header = registry.auth_header_for("not-installed");
|
|
r.check(header.name == "Authorization" && header.prefix == "Bearer ",
|
|
"auth_header_for() on unknown provider -> default header");
|
|
}
|
|
|
|
{
|
|
// Re-installing with identical options reports "unchanged" so callers
|
|
// can skip a config write; changing only the header must report a change.
|
|
CloudProviderRegistry::InstallOptions options;
|
|
options.auth_header_name = "X-Api-Key";
|
|
options.auth_header_prefix = "";
|
|
|
|
CloudProviderRegistry registry;
|
|
registry.install("acme", "https://gateway.example.com/v1", options);
|
|
r.check(!registry.install("acme", "https://gateway.example.com/v1", options),
|
|
"re-install with identical options -> reports no change");
|
|
|
|
options.auth_header_name = "X-Other-Key";
|
|
r.check(registry.install("acme", "https://gateway.example.com/v1", options),
|
|
"re-install with a changed header name -> reports a change");
|
|
r.check(registry.auth_header_for("acme").name == "X-Other-Key",
|
|
"re-install with a changed header name -> new value stored");
|
|
}
|
|
|
|
{
|
|
// The clobbering case: a re-install that doesn't repeat the auth flags
|
|
// (the desktop add-provider form, or a base-url-only CLI re-install)
|
|
// must leave the configured header alone rather than resetting it to
|
|
// Authorization/Bearer and silently 401ing against the gateway.
|
|
CloudProviderRegistry::InstallOptions options;
|
|
options.auth_header_name = "X-Api-Key";
|
|
options.auth_header_prefix = "";
|
|
options.allow_insecure_http = true;
|
|
|
|
CloudProviderRegistry registry;
|
|
registry.install("acme", "http://gateway.example.com/v1", options);
|
|
|
|
r.check(registry.install("acme", "http://gateway.example.com/v2"),
|
|
"re-install with only a new base_url -> reports a change");
|
|
const auto header = registry.auth_header_for("acme");
|
|
r.check(header.name == "X-Api-Key" && header.prefix.empty(),
|
|
"re-install without auth options -> custom header preserved");
|
|
r.check(registry.allow_insecure_http_for("acme"),
|
|
"re-install without allow_insecure_http -> flag preserved");
|
|
r.check(registry.base_url_for("acme") == "http://gateway.example.com/v2",
|
|
"re-install without auth options -> base_url still updated");
|
|
}
|
|
|
|
{
|
|
// Header values reach libcurl as a header line, so anything that could
|
|
// terminate that line or blank out the name has to be refused at the
|
|
// boundary rather than sanitized on the way out.
|
|
r.check(CloudProviderRegistry::validate_auth_header_name("X-Api-Key").empty(),
|
|
"validate_auth_header_name() accepts a token");
|
|
r.check(!CloudProviderRegistry::validate_auth_header_name("").empty(),
|
|
"validate_auth_header_name() rejects empty");
|
|
r.check(!CloudProviderRegistry::validate_auth_header_name("X-Api-Key\r\nX: y").empty(),
|
|
"validate_auth_header_name() rejects CRLF injection");
|
|
r.check(!CloudProviderRegistry::validate_auth_header_name("X Api Key").empty(),
|
|
"validate_auth_header_name() rejects spaces");
|
|
r.check(!CloudProviderRegistry::validate_auth_header_name("X:Api").empty(),
|
|
"validate_auth_header_name() rejects a colon");
|
|
|
|
r.check(CloudProviderRegistry::validate_auth_header_prefix("Bearer ").empty(),
|
|
"validate_auth_header_prefix() accepts the default");
|
|
r.check(CloudProviderRegistry::validate_auth_header_prefix("").empty(),
|
|
"validate_auth_header_prefix() accepts empty");
|
|
r.check(!CloudProviderRegistry::validate_auth_header_prefix("a\r\nX: y").empty(),
|
|
"validate_auth_header_prefix() rejects CRLF injection");
|
|
r.check(!CloudProviderRegistry::validate_auth_header_prefix("a\nb").empty(),
|
|
"validate_auth_header_prefix() rejects a bare newline");
|
|
}
|
|
|
|
{
|
|
// config.json is hand-editable, so a malformed value on disk must fall
|
|
// back to the default instead of reaching curl.
|
|
CloudProviderRegistry registry;
|
|
registry.load_from_config(json::array({
|
|
{{"name", "acme"},
|
|
{"base_url", "https://gateway.example.com/v1"},
|
|
{"auth_header_name", "X-Bad\r\nInjected: 1"},
|
|
{"auth_header_prefix", "Bad\r\n"}}
|
|
}));
|
|
const auto header = registry.auth_header_for("acme");
|
|
r.check(header.name == "Authorization" && header.prefix == "Bearer ",
|
|
"load_from_config() rejects injected header values -> defaults");
|
|
}
|
|
|
|
{
|
|
// The insecure-http opt-in path must not disturb the auth header: it
|
|
// is a separate setter precisely so a partial update cannot silently
|
|
// reset unrelated fields back to their defaults.
|
|
CloudProviderRegistry::InstallOptions options;
|
|
options.auth_header_name = "X-Api-Key";
|
|
options.auth_header_prefix = "";
|
|
|
|
CloudProviderRegistry registry;
|
|
registry.install("acme", "http://gateway.example.com/v1", options);
|
|
r.check(registry.set_allow_insecure_http("acme", true),
|
|
"set_allow_insecure_http() on an installed provider -> true");
|
|
r.check(registry.allow_insecure_http_for("acme"),
|
|
"set_allow_insecure_http() -> flag is set");
|
|
|
|
const auto header = registry.auth_header_for("acme");
|
|
r.check(header.name == "X-Api-Key" && header.prefix.empty(),
|
|
"set_allow_insecure_http() preserves the custom auth header");
|
|
r.check(!registry.set_allow_insecure_http("not-installed", true),
|
|
"set_allow_insecure_http() on unknown provider -> false");
|
|
}
|
|
|
|
{
|
|
r.check(CloudProviderRegistry::validate_wire_format("openai").empty(),
|
|
"validate_wire_format() accepts openai");
|
|
r.check(CloudProviderRegistry::validate_wire_format("anthropic").empty(),
|
|
"validate_wire_format() accepts anthropic");
|
|
r.check(!CloudProviderRegistry::validate_wire_format("ollama").empty(),
|
|
"validate_wire_format() rejects an unknown format");
|
|
r.check(!CloudProviderRegistry::validate_wire_format("").empty(),
|
|
"validate_wire_format() rejects an empty format");
|
|
}
|
|
|
|
{
|
|
CloudProviderRegistry registry;
|
|
registry.install("acme", "https://gateway.example.com/v1");
|
|
r.check(registry.wire_format_for("acme") == "openai",
|
|
"wire_format defaults to openai");
|
|
r.check(registry.wire_format_for("not-installed") == "openai",
|
|
"wire_format_for() on unknown provider -> openai");
|
|
r.check(!registry.to_config_array()[0].contains("wire_format"),
|
|
"default wire_format is omitted from the serialized config");
|
|
}
|
|
|
|
{
|
|
CloudProviderRegistry::InstallOptions options;
|
|
options.wire_format = "anthropic";
|
|
|
|
CloudProviderRegistry registry;
|
|
registry.install("acme", "https://gateway.example.com/v1", options);
|
|
r.check(!registry.install("acme", "https://gateway.example.com/v1", options),
|
|
"re-install with an identical wire_format -> reports no change");
|
|
|
|
CloudProviderRegistry reloaded;
|
|
reloaded.load_from_config(registry.to_config_array());
|
|
r.check(reloaded.wire_format_for("acme") == "anthropic",
|
|
"non-default wire_format round-trips through the config");
|
|
}
|
|
|
|
{
|
|
// Same clobbering case as the auth header, and worse in effect: a
|
|
// re-install that drops back to "openai" stops /v1/messages relaying
|
|
// and starts /v1/chat/completions forwarding an OpenAI-shaped body to
|
|
// a provider that doesn't serve one.
|
|
CloudProviderRegistry::InstallOptions options;
|
|
options.wire_format = "anthropic";
|
|
options.auth_header_name = "x-api-key";
|
|
options.auth_header_prefix = "";
|
|
|
|
CloudProviderRegistry registry;
|
|
registry.install("acme", "https://gateway.example.com/v1", options);
|
|
|
|
r.check(registry.install("acme", "https://gateway.example.com/v2"),
|
|
"re-install with only a new base_url -> reports a change");
|
|
r.check(registry.wire_format_for("acme") == "anthropic",
|
|
"re-install without wire_format -> anthropic preserved");
|
|
r.check(registry.base_url_for("acme") == "https://gateway.example.com/v2",
|
|
"re-install without wire_format -> base_url still updated");
|
|
|
|
CloudProviderRegistry::InstallOptions to_openai;
|
|
to_openai.wire_format = "openai";
|
|
r.check(registry.install("acme", "https://gateway.example.com/v2", to_openai),
|
|
"re-install with an explicit wire_format -> reports a change");
|
|
r.check(registry.wire_format_for("acme") == "openai",
|
|
"re-install with an explicit wire_format -> new value stored");
|
|
}
|
|
|
|
{
|
|
// An unrecognized persisted value would leave the provider
|
|
// undispatchable, so it must not survive a load.
|
|
json bad_array = json::array({
|
|
{{"name", "acme"}, {"base_url", "https://gateway.example.com/v1"},
|
|
{"wire_format", "ollama"}}
|
|
});
|
|
CloudProviderRegistry registry;
|
|
registry.load_from_config(bad_array);
|
|
r.check(registry.wire_format_for("acme") == "openai",
|
|
"invalid persisted wire_format falls back to openai");
|
|
}
|
|
|
|
printf("\n=== %d passed, %d failed ===\n", r.passed, r.failed);
|
|
return r.failed == 0 ? 0 : 1;
|
|
}
|