Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions OPENAPI_DOC.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19987,6 +19987,11 @@ components:
- type: array
items:
type: string
organisation_id:
anyOf:
- type: string
format: uuid
- type: "null"
timezone:
anyOf:
- type: object
Expand Down Expand Up @@ -20368,6 +20373,11 @@ components:
- type: array
items:
type: string
organisation_id:
anyOf:
- type: string
format: uuid
- type: "null"
place_id:
anyOf:
- type: "null"
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ LOGSTASH_HOST=example.com
LOGSTASH_PORT=12345
```

### Domain isolation

Staff API uses the authenticated request domain for its calendar context, including
email-domain tenant variants and delegated credentials.

Configure the authority's `config.org_zone` to bound spaces and zones to that tree.
Without it, the API uses ownership only when the organisation has exactly one
PlaceOS domain and every owned room's zone references belong to that organisation.
Missing or ambiguous ownership returns HTTP 501 with an `org_zone` configuration
message. Unowned rooms and zones are excluded from the ownership fallback.

`PLACE_TENANCY_ENFORCE=true` enables tenant-administration isolation after domain
organisation ownership has been assigned. It defaults to false during rollout.
Authority grants cover one domain; organisation and partner grants cover their
respective domains. Each request requires the matching grant operation or Manage,
alongside its existing JWT scope and admin requirements. Management partner staff
organisations retain cluster administration; customer organisations do not.

## MCP Server

The API is also available to LLM clients (Claude Code, Claude Desktop, VS Code,
Expand Down
4 changes: 2 additions & 2 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ shards:
git: https://github.com/place-labs/log-backend.git
version: 0.13.0

placeos-models:
placeos-models: # Overridden
git: https://github.com/placeos/models.git
version: 9.123.5
version: 9.123.5+git.commit.716ca764f68e09a72b9028aa3380fdb06a50a635

pool:
git: https://github.com/ysbaddaden/pool.git
Expand Down
5 changes: 5 additions & 0 deletions shard.override.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# NOTE:: here because of a shards ambiguous sources error
dependencies:
# PPT-526: build against the multi-tenancy data layer until models #332 merges
placeos-models:
github: placeos/models
branch: PPT-526-partner-client

http-params-serializable:
github: place-labs/http-params-serializable
branch: chore/0.36.0
13 changes: 8 additions & 5 deletions spec/controllers/calendars_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,17 @@ describe Calendars do
WebMock.stub(:post, "https://graph.microsoft.com/v1.0/users/dev%40acaprojects.onmicrosoft.com/calendar/getSchedule")
.to_return(body: File.read("./spec/fixtures/events/o365/get_schedule.json"))
WebMock.stub(:post, "https://login.microsoftonline.com/bb89674a-238b-4b7d-91ec-6bebad83553a/oauth2/v2.0/token")
.to_return(body: "")
.to_return(body: File.read("./spec/fixtures/tokens/o365_token.json"))

# this should return only two events
tz = Time::Location.load("America/Los_Angeles")
now = Time.parse("2019-03-15T12:00:00", "%Y-%m-%dT%H:%M:%S", tz).to_unix
later = Time.parse("2019-03-15T12:15:00", "%Y-%m-%dT%H:%M:%S", tz).to_unix

route = "#{CALENDARS_BASE}/free_busy?calendars=dev@acaprojects.com&period_start=#{now}&period_end=#{later}&zone_ids=zone-EzcsmWbvUG6"
body = JSON.parse(client.get(route, headers: headers).body).as_a
response = client.get(route, headers: headers)
response.status_code.should eq 200
body = JSON.parse(response.body).as_a
body.should eq(CalendarsHelper.free_busy_output_15min)
end

Expand All @@ -111,14 +113,15 @@ describe Calendars do
WebMock.stub(:post, "https://graph.microsoft.com/v1.0/users/dev%40acaprojects.onmicrosoft.com/calendar/getSchedule")
.to_return(body: File.read("./spec/fixtures/events/o365/get_schedule.json"))
WebMock.stub(:post, "https://login.microsoftonline.com/bb89674a-238b-4b7d-91ec-6bebad83553a/oauth2/v2.0/token")
.to_return(body: "")
.to_return(body: File.read("./spec/fixtures/tokens/o365_token.json"))

tz = Time::Location.load("America/Los_Angeles")
now = Time.parse("2019-03-15T12:00:00", "%Y-%m-%dT%H:%M:%S", tz).to_unix
later = Time.parse("2019-03-15T12:04:00", "%Y-%m-%dT%H:%M:%S", tz).to_unix
route = "#{CALENDARS_BASE}/free_busy?calendars=dev@acaprojects.com&period_start=#{now}&period_end=#{later}&zone_ids=zone-EzcsmWbvUG6"
bad_request = client.get(route, headers: headers).status_code
bad_request.should eq(400)
response = client.get(route, headers: headers)
response.status_code.should eq(400)
response.body.should contain("period_end")
end

describe "#check_permission" do
Expand Down
164 changes: 164 additions & 0 deletions spec/controllers/domain_isolation_regression_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
require "./helpers/tenant_reach_helper"

module StaffDomainRegressionSpec
alias Model = PlaceOS::Model

def self.zone(org : Model::Organisation?)
Model::Generator.zone.tap(&.organisation_id = org.try(&.id)).save!
end

def self.system(org : Model::Organisation?, zone : Model::Zone, feature : String)
Model::Generator.control_system.tap do |system|
system.organisation_id = org.try(&.id)
system.zones = [zone.id.as(String)]
system.bookable = true
system.features = Set{feature}
end.save!
end

def self.type_body(category : Model::AssetCategory)
{name: "domain-type-#{UUID.random}", brand: "Regression", category_id: category.id}.to_json
end

def self.clean_categories(categories : Array(Model::AssetCategory))
Model::AssetType.where(category_id: categories.map(&.id)).each(&.delete)
categories.each(&.delete)
end
end

describe "domain isolation regressions" do
client = AC::SpecHelper.client

it "falls back to owned rooms and zones only for a single-authority organisation" do
TenantReachSpec.with_estate do |estate|
other_org = estate.new_organisation
estate.authority(other_org)
local_zone = StaffDomainRegressionSpec.zone(estate.organisation)
foreign_zone = StaffDomainRegressionSpec.zone(other_org)
unowned_zone = StaffDomainRegressionSpec.zone(nil)
local = StaffDomainRegressionSpec.system(estate.organisation, local_zone, "local-feature")
foreign = StaffDomainRegressionSpec.system(other_org, foreign_zone, "foreign-feature")
unowned = StaffDomainRegressionSpec.system(nil, unowned_zone, "unowned-feature")
begin
response = client.get(Spaces.base_route, headers: estate.headers)
response.status_code.should eq 200
JSON.parse(response.body).as_a.map(&.["id"].as_s).should eq [local.id]
response.headers["X-Total-Count"].should eq "1"
response = client.get("#{Spaces.base_route}/zones", headers: estate.headers)
response.status_code.should eq 200
JSON.parse(response.body).as_a.map(&.["id"].as_s).should eq [local_zone.id]
JSON.parse(client.get("#{Spaces.base_route}/features", headers: estate.headers).body).as_a.map(&.as_s).should eq ["local-feature"]
client.get("#{Spaces.base_route}/#{foreign.id}", headers: estate.headers).status_code.should eq 404
client.get("#{Spaces.base_route}/#{local.id}", headers: estate.headers).status_code.should eq 200
ensure
[local, foreign, unowned].each(&.delete)
[local_zone, foreign_zone, unowned_zone].each(&.delete)
end
end
end

it "requires org_zone when a domain has no organisation ownership" do
TenantReachSpec.with_estate do |estate|
estate.authority.organisation_id = nil
estate.authority.save!
response = client.get(Spaces.base_route, headers: estate.headers)
response.status_code.should eq 501
response.body.should contain("org_zone")
end
end

it "requires org_zone instead of guessing between domains sharing an organisation" do
TenantReachSpec.with_estate do |estate|
estate.authority(estate.organisation)
response = client.get("#{Spaces.base_route}/zones", headers: estate.headers)
response.status_code.should eq 501
response.body.should contain("org_zone")
end
end

it "requires org_zone when an owned room references a foreign zone" do
TenantReachSpec.with_estate do |estate|
foreign_zone = StaffDomainRegressionSpec.zone(estate.new_organisation)
mixed = StaffDomainRegressionSpec.system(estate.organisation, foreign_zone, "mixed-estate")
begin
response = client.get(Spaces.base_route, headers: estate.headers)
response.status_code.should eq 501
response.body.should contain("org_zone")
ensure
mixed.delete
foreign_zone.delete
end
end
end

it "preserves explicit org_zone scoping for an existing unowned estate" do
TenantReachSpec.with_estate do |estate|
zone = StaffDomainRegressionSpec.zone(nil)
system = StaffDomainRegressionSpec.system(nil, zone, "configured-estate")
begin
estate.authority.organisation_id = nil
estate.authority.config_will_change!
estate.authority.config["org_zone"] = JSON::Any.new(zone.id.as(String))
estate.authority.save!
response = client.get(Spaces.base_route, headers: estate.headers)
response.status_code.should eq 200
JSON.parse(response.body).as_a.map(&.["id"].as_s).should eq [system.id]
response = client.get("#{Spaces.base_route}/zones", headers: estate.headers)
JSON.parse(response.body).as_a.map(&.["id"].as_s).should eq [zone.id]
ensure
system.delete
zone.delete
end
end
end

it "rejects asset type creation in a foreign category" do
TenantReachSpec.with_estate do |estate|
foreign = estate.authority(estate.new_organisation)
category = PlaceOS::Model::Generator.asset_category(foreign).save!
begin
response = client.post(AssetTypes.base_route, headers: estate.headers, body: StaffDomainRegressionSpec.type_body(category))
response.status_code.should eq 403
PlaceOS::Model::AssetType.where(category_id: category.id).count.should eq 0
ensure
StaffDomainRegressionSpec.clean_categories([category])
end
end
end

it "rejects moving an owned asset type to a foreign category" do
TenantReachSpec.with_estate do |estate|
foreign = estate.authority(estate.new_organisation)
own_category = PlaceOS::Model::Generator.asset_category(estate.authority).save!
foreign_category = PlaceOS::Model::Generator.asset_category(foreign).save!
type = PlaceOS::Model::Generator.asset_type(own_category).save!
begin
response = client.patch("#{AssetTypes.base_route}/#{type.id}", headers: estate.headers, body: {category_id: foreign_category.id}.to_json)
response.status_code.should eq 403
PlaceOS::Model::AssetType.find!(type.id).category_id.should eq own_category.id
ensure
StaffDomainRegressionSpec.clean_categories([own_category, foreign_category])
end
end
end

it "preserves asset type changes within the domain and legacy unowned categories" do
TenantReachSpec.with_estate do |estate|
first = PlaceOS::Model::Generator.asset_category(estate.authority).save!
second = PlaceOS::Model::Generator.asset_category(estate.authority).save!
legacy = PlaceOS::Model::Generator.asset_category(estate.authority).save!
# Legacy NULLs are supported by the database, but new model writes require an authority.
PgORM::Database.connection(&.exec("UPDATE asset_category SET authority_id = NULL WHERE id = $1", legacy.id))
type = PlaceOS::Model::Generator.asset_type(first).save!
begin
client.post(AssetTypes.base_route, headers: estate.headers, body: StaffDomainRegressionSpec.type_body(second)).status_code.should eq 201
response = client.patch("#{AssetTypes.base_route}/#{type.id}", headers: estate.headers, body: {category_id: second.id}.to_json)
response.status_code.should eq 200
PlaceOS::Model::AssetType.find!(type.id).category_id.should eq second.id
client.post(AssetTypes.base_route, headers: estate.headers, body: StaffDomainRegressionSpec.type_body(legacy)).status_code.should eq 201
ensure
StaffDomainRegressionSpec.clean_categories([first, second, legacy])
end
end
end
end
82 changes: 82 additions & 0 deletions spec/controllers/helpers/tenant_reach_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require "../../spec_helper"

module TenantReachSpec
alias Model = PlaceOS::Model
alias Permissions = Model::Permissions

class Estate
getter partner : Model::Partner
getter organisation : Model::Organisation
getter authority : Model::Authority
getter user : Model::User
getter own_tenant : Tenant
@organisations = [] of Model::Organisation
@authorities = [] of Model::Authority
@grants = [] of Model::Grant

def initialize(management = false, staff = false, admin = true)
@partner = Model::Generator.partner(management: management).save!
@organisation = new_organisation(@partner, staff)
@authority = authority(@organisation)
@user = Model::Generator.user(@authority, admin: admin).save!
@own_tenant = Model::Generator.tenant(domain: @authority.domain)
end

def new_organisation(partner : Model::Partner? = nil, staff = false) : Model::Organisation
org = Model::Generator.organisation(partner: partner, partner_staff: staff).save!
@organisations << org
org
end

def authority(org : Model::Organisation) : Model::Authority
domain = "reach-#{UUID.random}.example.com"
authority = Model::Generator.authority(domain).tap(&.organisation_id = org.id).save!
@authorities << authority
authority
end

def tenant(authority : Model::Authority) : Tenant
Model::Generator.tenant(domain: authority.domain)
end

def grant(scope : String, id : String, permissions : Permissions, expires_at : Time? = nil) : Model::Grant
grant = Model::Generator.grant(@user, scope, id, permissions, expires_at).save!
@grants << grant
grant
end

def headers(scope = [UserJWT::Scope::PUBLIC]) : HTTP::Headers
generated = Model::Generator.jwt(@user, scope)
token = UserJWT.new(
iss: generated.iss, iat: generated.iat, exp: generated.exp,
domain: @authority.domain, id: generated.id, user: generated.user, scope: generated.scope,
)
HTTP::Headers{"Host" => @authority.domain, "Authorization" => "Bearer #{token.encode}", "Content-Type" => "application/json"}
end

def close
@grants.each(&.delete)
Tenant.where(domain: @authorities.map(&.domain)).each(&.delete)
@user.delete
@authorities.reverse_each(&.delete)
@organisations.reverse_each(&.delete)
@partner.delete
end
end

def self.with_estate(management = false, staff = false, admin = true, &)
estate = Estate.new(management, staff, admin)
Utils::Tenancy.enforce = true
begin
yield estate
ensure
Utils::Tenancy.enforce = false
estate.close
end
end

def self.body(domain : String, email_domain : String? = nil)
{name: "Reach regression", domain: domain, email_domain: email_domain, platform: "office365",
credentials: {tenant: "t", client_id: "c", client_secret: "s"}}.to_json
end
end
Loading
Loading