feat(clients): openai admin: organization admin API, usage, costs, audit logs, webhooks
121-call table checked against the OpenAI OpenAPI document, admin-key-only client, destructive guards, WebhookVerifier (Standard Webhooks HMAC-SHA256), demo routes under /ai/openai/admin, local and opt-in live tests. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
// Route: POST /ai/openai/admin/read (422 first, then 503 without a credential)
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
unsetenv("OPENAI_ADMIN_KEY");
|
||||
unsetenv("OPENAI_API_KEY");
|
||||
Fixture f;
|
||||
auto post = [&](const std::string& b) {
|
||||
return f.client->post("/ai/openai/admin/read", b, "application/json");
|
||||
};
|
||||
auto ok = post("{\"call\":\"users.list\",\"query\":{\"limit\":5}}");
|
||||
check(ok.status == 503 && contains(ok.body, "not_configured"), "valid read without key is 503");
|
||||
check(post("{\"call\":\"webhook_event_types.list\"}").status == 503, "project-key call without key is 503");
|
||||
check(post("{}").status == 422, "missing call is 422");
|
||||
check(post("{\"call\":\"nope.list\"}").status == 422, "unknown call is 422");
|
||||
check(post("{\"call\":\"users.update\",\"ids\":[\"u\"]}").status == 422, "write via read is 422");
|
||||
check(post("{\"call\":\"users.get\"}").status == 422, "missing id is 422");
|
||||
check(post("{\"call\":\"users.get\",\"ids\":[\"../x\"]}").status == 422, "bad id is 422");
|
||||
check(post("{\"call\":\"users.list\",\"query\":{\"nope\":1}}").status == 422, "foreign query name is 422");
|
||||
check(post("{\"call\":\"users.list\",\"region\":\"zz\"}").status == 422, "bad region is 422");
|
||||
check(post("[1]").status == 422, "non-object body is 422");
|
||||
check(post("{bad").status == 422, "bad json is 422");
|
||||
check(f.client->get("/ai/openai/admin/read").status == 405, "get is 405");
|
||||
return finish("ai/openai/admin/read");
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Route: GET /ai/openai/admin, /status, /calls, /audit-events, /webhook-events (no credential needed)
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
unsetenv("OPENAI_ADMIN_KEY");
|
||||
unsetenv("OPENAI_API_KEY");
|
||||
Fixture f;
|
||||
auto page = f.client->get("/ai/openai/admin");
|
||||
check(page.status == 200 && contains(page.body, "OpenAI administration"), "page 200");
|
||||
check(contains(page.body, "/embedded/ai_openai_admin.js"), "page loads its script");
|
||||
auto st = f.client->get("/ai/openai/admin/status");
|
||||
check(st.status == 200 && contains(st.body, "\"configured\":false"), "status without key");
|
||||
check(contains(st.body, "\"calls\":121"), "status counts the table");
|
||||
auto calls = f.client->get("/ai/openai/admin/calls");
|
||||
check(calls.status == 200 && contains(calls.body, "projects.service_accounts.api_keys.create"), "call table listed");
|
||||
check(contains(calls.body, "\"destructive\":true"), "destructive rows flagged");
|
||||
auto audit = f.client->get("/ai/openai/admin/audit-events");
|
||||
check(audit.status == 200 && contains(audit.body, "project.archived"), "audit catalog");
|
||||
auto hooks = f.client->get("/ai/openai/admin/webhook-events");
|
||||
check(hooks.status == 200 && contains(hooks.body, "response.completed"), "webhook catalog");
|
||||
check(f.client->post("/ai/openai/admin/status", "{}", "application/json").status == 405, "post status is 405");
|
||||
check(contains(f.client->get("/").body, "/ai/openai/admin"), "matrix row for the page");
|
||||
return finish("ai/openai/admin/status");
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Route: POST /ai/openai/admin/usage, /costs, /audit
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
unsetenv("OPENAI_ADMIN_KEY");
|
||||
unsetenv("OPENAI_API_KEY");
|
||||
Fixture f;
|
||||
auto p = [&](const char* path, const std::string& b) {
|
||||
return f.client->post(std::string("/ai/openai/admin/") + path, b, "application/json");
|
||||
};
|
||||
check(p("usage", "{\"kind\":\"completions\",\"start_time\":1730419200}").status == 503, "usage without key is 503");
|
||||
check(p("usage", "{\"kind\":\"completions\"}").status == 422, "missing start_time is 422");
|
||||
check(p("usage", "{\"kind\":\"nope\",\"start_time\":1}").status == 422, "unknown kind is 422");
|
||||
check(p("usage", "{\"kind\":\"completions\",\"start_time\":1,\"limit\":999}").status == 422, "limit over cap is 422");
|
||||
check(p("usage", "{\"kind\":\"completions\",\"start_time\":1,\"sizes\":[\"1024x1024\"]}").status == 422, "foreign filter is 422");
|
||||
check(p("usage", "{\"kind\":\"images\",\"start_time\":1,\"group_by\":[\"size\"],\"walk\":true}").status == 503, "walk without key is 503");
|
||||
check(p("costs", "{\"start_time\":1730419200,\"limit\":7}").status == 503, "costs without key is 503");
|
||||
check(p("costs", "{\"start_time\":1730419200,\"limit\":181}").status == 422, "costs limit over 180 is 422");
|
||||
check(p("costs", "{\"start_time\":1,\"bucket_width\":\"1h\"}").status == 422, "costs are daily only");
|
||||
check(p("audit", "{\"limit\":20,\"event_types\":[\"project.created\"]}").status == 503, "audit without key is 503");
|
||||
check(p("audit", "{\"event_types\":[\"project.exploded\"]}").status == 422, "unknown event type is 422");
|
||||
check(p("audit", "{\"after\":\"a\",\"before\":\"b\"}").status == 422, "after and before together is 422");
|
||||
check(f.client->get("/ai/openai/admin/usage").status == 405, "get is 405");
|
||||
return finish("ai/openai/admin/usage");
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// Route: POST /ai/openai/admin/webhook/verify (local, no credential)
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
Fixture f;
|
||||
auto v = [&](const std::string& b) {
|
||||
return f.client->post("/ai/openai/admin/webhook/verify", b, "application/json");
|
||||
};
|
||||
// Standard Webhooks reference vector; signature recomputed independently with python3 hmac.
|
||||
const std::string head = "{\"secret\":\"whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw\",\"now\":";
|
||||
const std::string tail =
|
||||
",\"headers\":{\"Webhook-Id\":\"msg_p5jXN8AQM9LWM0D4loKWxJek\",\"Webhook-Timestamp\":\"1614265330\","
|
||||
"\"Webhook-Signature\":\"v1,g0hM9SsE+OTPJTGt/tmIKtSyZlE3uFJELVlNIOLJ1OE=\"},";
|
||||
auto ok = v(head + "1614265330" + tail + "\"body\":\"{\\\"test\\\": 2432232314}\"}");
|
||||
check(ok.status == 200 && contains(ok.body, "\"ok\":true"), "reference vector verifies");
|
||||
check(contains(ok.body, "event_error"), "non-event body is reported, not fatal");
|
||||
auto old = v(head + "1614266000" + tail + "\"body\":\"{\\\"test\\\": 2432232314}\"}");
|
||||
check(old.status == 200 && contains(old.body, "timestamp_too_old"), "stale timestamp rejected");
|
||||
auto tampered = v(head + "1614265330" + tail + "\"body\":\"{\\\"test\\\": 2432232315}\"}");
|
||||
check(tampered.status == 200 && contains(tampered.body, "signature_mismatch"), "tampered body rejected");
|
||||
check(v("{\"headers\":{},\"body\":\"x\"}").status == 422, "missing secret is 422");
|
||||
check(v("{\"secret\":\"whsec_!!\",\"headers\":{},\"body\":\"x\"}").status == 422, "bad secret is 422");
|
||||
check(v("{\"secret\":\"whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw\",\"body\":\"x\"}").status == 422, "missing headers is 422");
|
||||
check(contains(f.client->get("/ai/openai/admin/status").body, "\"signature_mismatch\":1"), "counters exposed");
|
||||
check(f.client->get("/ai/openai/admin/webhook/verify").status == 405, "get is 405");
|
||||
return finish("ai/openai/admin/webhook_verify");
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Route: POST /ai/openai/admin/write and /destroy (destructive calls are dry runs unless confirm:true)
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
unsetenv("OPENAI_ADMIN_KEY");
|
||||
unsetenv("OPENAI_API_KEY");
|
||||
Fixture f;
|
||||
auto w = [&](const std::string& b) { return f.client->post("/ai/openai/admin/write", b, "application/json"); };
|
||||
auto d = [&](const std::string& b) { return f.client->post("/ai/openai/admin/destroy", b, "application/json"); };
|
||||
check(w("{\"call\":\"invites.create\",\"body\":{\"email\":\"a@example.com\",\"role\":\"reader\"}}").status == 503,
|
||||
"valid write without key is 503");
|
||||
check(w("{\"call\":\"invites.create\",\"body\":{\"email\":\"a@example.com\"}}").status == 422, "missing required field is 422");
|
||||
check(w("{\"call\":\"invites.create\",\"body\":{\"email\":\"a@example.com\",\"role\":\"reader\",\"x\":1}}").status == 422,
|
||||
"foreign body field is 422");
|
||||
check(w("{\"call\":\"users.delete\",\"ids\":[\"u\"]}").status == 422, "destructive via write is 422");
|
||||
check(w("{\"call\":\"users.list\"}").status == 422, "read via write is 422");
|
||||
auto dry = d("{\"call\":\"users.delete\",\"ids\":[\"user_1\"]}");
|
||||
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true"), "default is a dry run");
|
||||
check(contains(dry.body, "DELETE /organization/users/user_1"), "dry run names the call");
|
||||
auto arch = d("{\"call\":\"projects.archive\",\"ids\":[\"proj_1\"]}");
|
||||
check(arch.status == 200 && contains(arch.body, "dry_run"), "archive is a dry run");
|
||||
auto real = d("{\"call\":\"users.delete\",\"ids\":[\"user_1\"],\"confirm\":true}");
|
||||
check(real.status == 503 && contains(real.body, "not_configured"), "confirmed delete without key is 503");
|
||||
check(d("{\"call\":\"users.get\",\"ids\":[\"u\"]}").status == 422, "non-destructive call on destroy is 422");
|
||||
check(d("{\"call\":\"users.delete\",\"confirm\":\"yes\"}").status == 422, "non-bool confirm is 422");
|
||||
check(d("{\"call\":\"users.delete\"}").status == 422, "dry run still needs the id");
|
||||
check(f.client->get("/ai/openai/admin/destroy").status == 405, "get is 405");
|
||||
return finish("ai/openai/admin/write");
|
||||
}
|
||||
Reference in New Issue
Block a user