Skip to content

Commit 1feb371

Browse files
committed
Add TDEI workspace tenant selection by header
1 parent 885d8e7 commit 1feb371

File tree

13 files changed

+116
-5
lines changed

13 files changed

+116
-5
lines changed

include/cgimap/backend/apidb/pgsql_update.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class pgsql_update : public data_update {
5353
const std::string& email,
5454
const std::string& display_name) override;
5555

56+
// TDEI Workspace tenant selection
57+
void set_tdei_workspace(const workspace_id_t id) override;
58+
5659
/**
5760
* abstracts the creation of transactions for the
5861
* data updates.

include/cgimap/backend/apidb/readonly_pgsql_selection.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class readonly_pgsql_selection : public data_selection {
7979
const std::string &token_id, bool &expired, bool &revoked,
8080
bool &allow_api_write) override;
8181
std::optional<osm_user_id_t> get_user_id_for_tdei_token(const std::string& email) override;
82+
void set_tdei_workspace(const workspace_id_t id) override;
8283
bool is_user_active(const osm_user_id_t id) override;
8384

8485

include/cgimap/data_selection.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "cgimap/types.hpp"
1414
#include "cgimap/output_formatter.hpp"
15+
#include "cgimap/workspaces/types.hpp"
1516

1617
#include <chrono>
1718
#include <memory>
@@ -181,6 +182,9 @@ class data_selection {
181182
// Authentication using TDEI access token for TDEI Workspaces..
182183
virtual std::optional<osm_user_id_t> get_user_id_for_tdei_token(const std::string& email) = 0;
183184

185+
// TDEI Workspace tenant selection
186+
virtual void set_tdei_workspace(const workspace_id_t id) = 0;
187+
184188
// is user status confirmed or active?
185189
virtual bool is_user_active(const osm_user_id_t) = 0;
186190

include/cgimap/data_update.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "cgimap/output_formatter.hpp"
1414
#include "cgimap/types.hpp"
1515
#include "cgimap/util.hpp"
16+
#include "cgimap/workspaces/types.hpp"
1617

1718
#include "cgimap/api06/changeset_upload/changeset_updater.hpp"
1819
#include "cgimap/api06/changeset_upload/node_updater.hpp"
@@ -67,6 +68,9 @@ class data_update {
6768
const std::string& email,
6869
const std::string& display_name) = 0;
6970

71+
// TDEI Workspace tenant selection
72+
virtual void set_tdei_workspace(const workspace_id_t id) = 0;
73+
7074
/**
7175
* factory for the creation of data updates. this abstracts away
7276
* the creation process of transactions, and allows some up-front
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef WORKSPACES_TENANT_HPP
2+
#define WORKSPACES_TENANT_HPP
3+
4+
#include "cgimap/request.hpp"
5+
#include "cgimap/workspaces/types.hpp"
6+
7+
namespace workspaces {
8+
std::optional<workspace_id_t> try_id_from_request(const request& req);
9+
}
10+
11+
#endif /* WORKSPACES_TENANT_HPP */
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef WORKSPACES_TYPES_HPP
2+
#define WORKSPACES_TYPES_HPP
3+
4+
#include <cstdint>
5+
6+
using workspace_id_t = uint64_t;
7+
8+
#endif /* WORKSPACES_TYPES_HPP */

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ target_sources(cgimap_core PRIVATE
6767
api06/changeset_upload/osmchange_tracking.cpp
6868

6969
workspaces/tdei_auth.cpp
70+
workspaces/tenant.cpp
7071

7172
$<$<BOOL:${ENABLE_YAJL}>:json_formatter.cpp>
7273
$<$<BOOL:${ENABLE_YAJL}>:json_writer.cpp>

src/backend/apidb/pgsql_update.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ std::string connect_db_str(const po::variables_map &options) {
5858
} // anonymous namespace
5959

6060
pgsql_update::pgsql_update(Transaction_Owner_Base& to, bool readonly)
61-
: m{ to },
61+
: m{ to },
6262
m_readonly{ readonly } {
6363

6464
if (is_api_write_disabled())
6565
return;
6666

67-
m.exec(R"(CREATE TEMPORARY TABLE tmp_create_nodes
67+
m.exec(R"(CREATE TEMPORARY TABLE tmp_create_nodes
6868
(
6969
id bigint NOT NULL DEFAULT nextval('current_nodes_id_seq'::regclass),
7070
latitude integer NOT NULL,
@@ -79,7 +79,7 @@ pgsql_update::pgsql_update(Transaction_Owner_Base& to, bool readonly)
7979
ON COMMIT DROP
8080
)");
8181

82-
m.exec(R"(CREATE TEMPORARY TABLE tmp_create_ways
82+
m.exec(R"(CREATE TEMPORARY TABLE tmp_create_ways
8383
(
8484
id bigint NOT NULL DEFAULT nextval('current_ways_id_seq'::regclass),
8585
changeset_id bigint NOT NULL,
@@ -91,7 +91,7 @@ pgsql_update::pgsql_update(Transaction_Owner_Base& to, bool readonly)
9191
ON COMMIT DROP
9292
)");
9393

94-
m.exec(R"(CREATE TEMPORARY TABLE tmp_create_relations
94+
m.exec(R"(CREATE TEMPORARY TABLE tmp_create_relations
9595
(
9696
id bigint NOT NULL DEFAULT nextval('current_relations_id_seq'::regclass),
9797
changeset_id bigint NOT NULL,

src/backend/apidb/readonly_pgsql_selection.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,11 @@ std::optional<osm_user_id_t> readonly_pgsql_selection::get_user_id_for_tdei_toke
869869
return {};
870870
}
871871

872+
void readonly_pgsql_selection::set_tdei_workspace(const workspace_id_t id)
873+
{
874+
m.exec(fmt::format(R"(SET search_path TO "workspace-{:d}", public)", id));
875+
}
876+
872877
bool readonly_pgsql_selection::is_user_active(const osm_user_id_t id)
873878
{
874879
m.prepare("is_user_active",

src/process_request.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "cgimap/output_formatter.hpp"
1717
#include "cgimap/output_writer.hpp"
1818
#include "cgimap/workspaces/tdei_auth.hpp"
19+
#include "cgimap/workspaces/tenant.hpp"
1920

2021
#include <chrono>
2122
#include <memory>
@@ -233,6 +234,10 @@ std::tuple<std::string, size_t>
233234
process_get_request(request& req, const handler& handler,
234235
data_selection& selection,
235236
const std::string &ip, const std::string &generator) {
237+
if (const auto workspace_id = workspaces::try_id_from_request(req)) {
238+
selection.set_tdei_workspace(*workspace_id);
239+
}
240+
236241
// request start logging
237242
const std::string request_name = handler.log_name();
238243
logger::message(fmt::format("Started request for {} from {}", request_name, ip));
@@ -250,7 +255,7 @@ process_get_request(request& req, const handler& handler,
250255
* process a POST/PUT request.
251256
*/
252257
std::tuple<std::string, size_t>
253-
process_post_put_request(RequestContext& req_ctx,
258+
process_post_put_request(RequestContext& req_ctx,
254259
const handler& handler,
255260
const data_selection::factory& factory,
256261
data_update::factory& update_factory,
@@ -265,6 +270,7 @@ process_post_put_request(RequestContext& req_ctx,
265270

266271
try {
267272
const auto & pe_handler = dynamic_cast< const payload_enabled_handler& >(handler);
273+
const auto workspace_id = workspaces::try_id_from_request(req_ctx.req);
268274

269275
// Process request, perform database update
270276
{
@@ -273,6 +279,10 @@ process_post_put_request(RequestContext& req_ctx,
273279
auto data_update = update_factory.make_data_update(*rw_transaction);
274280
check_db_readonly_mode(*data_update);
275281

282+
if (workspace_id) {
283+
data_update->set_tdei_workspace(*workspace_id);
284+
}
285+
276286
// Executing the responder constructor parses the payload, performs db CRUD operations
277287
// and eventually calls db commit(), in case there are no issues with the data.
278288
auto responder = pe_handler.responder(*data_update, payload, req_ctx);
@@ -298,6 +308,11 @@ process_post_put_request(RequestContext& req_ctx,
298308

299309
// create a data selection for the request
300310
auto data_selection = factory.make_selection(*read_only_transaction);
311+
312+
if (workspace_id) {
313+
data_selection->set_tdei_workspace(*workspace_id);
314+
}
315+
301316
auto sel_responder = pe_handler.responder(*data_selection);
302317
bytes_written = generate_response(req_ctx.req, *sel_responder, generator);
303318

0 commit comments

Comments
 (0)