projects.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "collection_registry.hpp"
  2. #include "db_gateway.hpp"
  3. #include "errors.hpp"
  4. #include "json_http.hpp"
  5. #include "file_store.hpp"
  6. #include "project_secret_store.hpp"
  7. #include "server.hpp"
  8. #include <algorithm>
  9. namespace svapi {
  10. void registerProjectRoutes(ApiServer& s) {
  11. auto& svr = s.raw(); ServerDeps* d = &s.deps();
  12. svr.Get("/api/v1/projects", [d](const httplib::Request& req, httplib::Response& res) {
  13. ApiKey k = requireKey(*d, req);
  14. auto all = d->db.listProjects();
  15. nlohmann::json arr = nlohmann::json::array();
  16. for (const auto& p : all) if (k.canAccess(p)) arr.push_back(p);
  17. sendJson(res, 200, {{"projects", arr}});
  18. });
  19. svr.Post("/api/v1/projects", [d](const httplib::Request& req, httplib::Response& res) {
  20. ApiKey k = requireKey(*d, req); requireAdmin(k);
  21. auto body = bodyJson(req);
  22. std::string name = body.value("name", "");
  23. if (name.empty()) throw ApiError(ErrCode::Unprocessable, "validation", "name is required");
  24. if (!d->db.ensureProject(name))
  25. throw ApiError(ErrCode::Unprocessable, "bad_project", "could not create project (check name rules)");
  26. d->registry.ensure(name);
  27. sendJson(res, 201, {{"name", name}});
  28. });
  29. svr.Get(R"(/api/v1/projects/([^/]+))", [d](const httplib::Request& req, httplib::Response& res) {
  30. ApiKey k = requireKey(*d, req);
  31. // NOT widened: the short shape of this route is the literal `/api/v1`,
  32. // which would hijack an unrelated path. Project CRUD is an operation on
  33. // the tenant rather than within it, so the full form is the only one
  34. // that makes sense here anyway - but it still resolves through
  35. // projectFor, so a project hostname cannot read another project.
  36. std::string project = projectFor(*d, req, 1);
  37. requireProjectAccess(k, project);
  38. auto all = d->db.listProjects();
  39. if (std::find(all.begin(), all.end(), project) == all.end())
  40. throw ApiError(ErrCode::NotFound, "not_found", "no such project");
  41. sendJson(res, 200, {{"name", project}, {"collections", d->registry.list(project).size()}});
  42. });
  43. svr.Delete(R"(/api/v1/projects/([^/]+))", [d](const httplib::Request& req, httplib::Response& res) {
  44. ApiKey k = requireKey(*d, req); requireAdmin(k);
  45. // See the GET above for why this pattern stays full-form. The tenancy
  46. // check matters more here than anywhere: this route DELETES a project.
  47. std::string project = projectFor(*d, req, 1);
  48. if (project == "default") throw ApiError(ErrCode::Unprocessable, "protected", "cannot drop the default project");
  49. for (const auto& m : d->registry.list(project))
  50. d->db.client().dropCollection(qualify(project, m.name));
  51. d->db.client().dropCollection(qualify(project, "vectorapi_collections"));
  52. // The project's function secrets go with it. dropProject does not
  53. // physically purge collection data, so without this a recycled project
  54. // name would inherit the previous tenant's live credentials and hand
  55. // them to the new tenant's functions as ctx.env.
  56. ProjectSecretStore(d->db).dropAll(project);
  57. // And its files, for the same reason. Without this a deleted project's
  58. // logo and signature images outlive it, and a recycled project name
  59. // hands the next tenant the previous one's assets - the same class of
  60. // bug as a recycled collection name inheriting its constraints.
  61. FileStore(d->db).dropAll(project);
  62. if (!d->db.dropProject(project)) throw ApiError(ErrCode::Unavailable, "db_error", "drop failed");
  63. sendJson(res, 200, {{"deleted", project}});
  64. });
  65. }
  66. }