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
7 changes: 6 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ COPY src src

RUN mkdir -p /app/bin

# Limit LLVM workers while Buildx compiles both architectures concurrently.
# Build init
RUN shards build \
--threads=1 \
--debug \
--ignore-crystal-version \
--skip-postinstall \
Expand All @@ -55,6 +57,7 @@ RUN shards build \
--link-flags "-no-pie -Wl,-no-pie -Wl,--eh-frame-hdr -Wl,--build-id -rdynamic -Wl,--export-dynamic -lunwind -llzma"

RUN crystal build src/sam.cr -o bin/task \
--threads=1 \
--debug \
--error-trace \
--no-color \
Expand All @@ -63,6 +66,7 @@ RUN crystal build src/sam.cr -o bin/task \
--link-flags "-no-pie -Wl,-no-pie -Wl,--eh-frame-hdr -Wl,--build-id -rdynamic -Wl,--export-dynamic -lunwind -llzma"

RUN crystal build src/generate-secrets.cr -o bin/generate-secrets \
--threads=1 \
--debug \
--error-trace \
--no-color \
Expand All @@ -81,7 +85,8 @@ RUN for binary in /app/bin/* /usr/bin/pg_dump /usr/bin/pg_restore /usr/bin/psql;
xargs -I % sh -c 'mkdir -p $(dirname deps%); cp % deps%;'; \
done

RUN git clone https://github.com/PlaceOS/models
# PPT-526: the tenancy migrations live on this branch until models #332 merges
RUN git clone --branch PPT-526-partner-client https://github.com/PlaceOS/models

# Create tmp directory with proper permissions
RUN rm -rf /tmp && mkdir -p /tmp && chmod 1777 /tmp
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,36 @@ The default entrypoint to the init container generates a User, Authority, and Ap
- `backoffice_branch`: `PLACE_BACKOFFICE_BRANCH`, `build/prod` or `build/dev` dependent on environment.
- `backoffice_commmit`: `PLACE_BACKOFFICE_COMMIT` ||`"HEAD"`

### Management access on an existing cluster

Normal startup preserves an existing authority's organisation, including its name,
configuration and grants. An existing unowned authority remains unowned for explicit
reconciliation; rerunning init does not promote it to management staff.

If a legacy cluster has no management-domain administrator, choose a **new**
management hostname routed to the existing deployment. It must not already have an
Authority record: do not pre-create it with `create:authority`. Run the normal init
startup against that hostname using the existing cluster's database connection and
platform secrets, with dedicated administrator credentials:

```bash
# Set NEW_PLATFORM_PASSWORD to a new strong password before running this command.
docker compose run --rm --no-deps \
-e PLACE_DOMAIN=management.example.com \
-e PLACE_TLS=true \
-e PLACE_EMAIL=platform-admin@example.com \
-e PLACE_USERNAME="Platform administrator" \
-e PLACE_PASSWORD="$NEW_PLATFORM_PASSWORD" \
-e PLACE_SKIP_PLACEHOLDERS=true \
init /app/bin/start
```

Only the newly created authority is attached to the management partner's staff
organisation, and its seeded administrator retains platform administration rights.
Sign in through the management hostname and use the REST tenancy inventory and
explicit reconciliation operations before enabling `PLACE_TENANCY_ENFORCE` for
customer traffic. Existing customer domains are never adopted or merged implicitly.

## Backup Container

`Dockerfile.pg-backup` generates a container that will backup the state of PG to `S3` or `Azure Storage` depending on the environment variables.
Expand Down
4 changes: 2 additions & 2 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,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.4
version: 9.123.5+git.commit.716ca764f68e09a72b9028aa3380fdb06a50a635

sam:
git: https://github.com/imdrasil/sam.cr.git
Expand Down
5 changes: 5 additions & 0 deletions shard.override.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
dependencies:
# PPT-526: build against the multi-tenancy data layer until models #332 merges
placeos-models:
github: placeos/models
branch: PPT-526-partner-client

retriable:
github: Sija/retriable.cr
db:
Expand Down
138 changes: 138 additions & 0 deletions spec/entities_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
require "./spec_helper"
require "../src/tasks/entities"

module PlaceOS::Tasks
# The organisation the backfill migration creates for a domain that has none
def self.backfilled_organisation(authority : Model::Authority) : Model::Organisation
organisation = Model::Organisation.new(
name: "#{authority.name} (#{authority.domain})",
description: "Auto-created from authority #{authority.id} during PPT-526 organisation backfill",
).save!
authority.organisation_id = organisation.id
authority.save!
organisation
end

def self.owned_zone(organisation : Model::Organisation) : Model::Zone
zone = Model::Zone.new(name: "zone-#{Random::Secure.hex(4)}", tags: Set{"org"})
zone.organisation_id = organisation.id
zone.save!
end

def self.organisation_grant(organisation : Model::Organisation) : Model::Grant
user = Model::Generator.user.save!
Model::Generator.grant(user, Model::Grant::SCOPE_ORGANISATION, organisation.id.to_s)
end

describe Entities do
it "creates organisation-specific placeholders while sharing driver and repository catalogs" do
previous_mode = ENV["PLACE_TENANCY_ENFORCE"]?
ENV["PLACE_TENANCY_ENFORCE"] = "false"
first = Model::Generator.organisation.save!
second = Model::Generator.organisation.save!
Entities.create_placeholders(first.id)
first_zones = Model::Zone.where(organisation_id: first.id).to_a
original_parents = first_zones.to_h { |zone| {zone.id.as(String), zone.parent_id} }
driver_count = Model::Driver.all.count
repository_count = Model::Repository.all.count

Entities.create_placeholders(second.id)

second_zones = Model::Zone.where(organisation_id: second.id).to_a
second_zones.size.should eq 4
(second_zones.map(&.name).to_set & first_zones.map(&.name).to_set).should be_empty
second_system = Model::ControlSystem.where(organisation_id: second.id).first.not_nil!
second_system.zones.to_set.should eq second_zones.map(&.id.as(String)).to_set
Model::Module.where(organisation_id: second.id).count.should eq 1
Model::Trigger.where(organisation_id: second.id).count.should eq 1
Model::Zone.where(organisation_id: first.id).to_a.to_h { |zone| {zone.id.as(String), zone.parent_id} }.should eq original_parents
Model::Driver.all.count.should eq driver_count
Model::Repository.all.count.should eq repository_count
ensure
if previous_mode
ENV["PLACE_TENANCY_ENFORCE"] = previous_mode
else
ENV.delete("PLACE_TENANCY_ENFORCE")
end
end

describe "create_management_organisation" do
it "seeds the management partner and attaches an unowned domain to its staff organisation" do
authority = Model::Generator.authority("https://first.example.com").save!

organisation = Entities.create_management_organisation(authority)

organisation.partner_staff.should be_true
organisation.partner.not_nil!.management.should be_true
Model::Authority.find!(authority.id.as(String)).organisation_id.should eq organisation.id
end

it "is idempotent" do
authority = Model::Generator.authority("https://first.example.com").save!

first = Entities.create_management_organisation(authority)
second = Entities.create_management_organisation(Model::Authority.find!(authority.id.as(String)))

second.id.should eq first.id
Model::Partner.where(management: true).count.should eq 1
Model::Organisation.where(partner_staff: true).count.should eq 1
end

it "preserves a backfilled customer's estate and grants" do
authority = Model::Generator.authority("https://first.example.com").save!
backfilled = backfilled_organisation(authority)
zone = owned_zone(backfilled)
grant = organisation_grant(backfilled).save!

Entities.create_management_organisation(authority)

Model::Authority.find!(authority.id.as(String)).organisation_id.should eq backfilled.id
Model::Zone.find!(zone.id.as(String)).organisation_id.should eq backfilled.id
Model::Grant.find!(grant.id.not_nil!).scope_id.should eq backfilled.id.to_s
Model::Organisation.find?(backfilled.id.not_nil!).should_not be_nil
end

it "preserves a renamed and configured customer organisation with its generated description" do
authority = Model::Generator.authority("https://customer.example.com").save!
backfilled = backfilled_organisation(authority)
backfilled.name = "Customer organisation"
backfilled.config = {"billing_reference" => JSON::Any.new("customer-owned")}
backfilled.save!
Model::Organisation.find!(backfilled.id.not_nil!).config["billing_reference"].as_s.should eq "customer-owned"

Entities.create_management_organisation(authority)

persisted = Model::Organisation.find!(backfilled.id.not_nil!)
persisted.name.should eq "Customer organisation"
persisted.config["billing_reference"].as_s.should eq "customer-owned"
Model::Authority.find!(authority.id.as(String)).organisation_id.should eq backfilled.id
end

it "leaves an organisation an operator has changed" do
authority = Model::Generator.authority("https://first.example.com").save!
backfilled = backfilled_organisation(authority)
backfilled.description = "Acme, merged by hand"
backfilled.save!
zone = owned_zone(backfilled)

Entities.create_management_organisation(authority)

Model::Authority.find!(authority.id.as(String)).organisation_id.should eq backfilled.id
Model::Zone.find!(zone.id.as(String)).organisation_id.should eq backfilled.id
end

it "leaves an organisation that owns other domains" do
authority = Model::Generator.authority("https://first.example.com").save!
backfilled = backfilled_organisation(authority)
other = Model::Generator.authority("https://second.example.com")
other.organisation_id = backfilled.id
other.save!

Entities.create_management_organisation(authority)

Model::Authority.find!(authority.id.as(String)).organisation_id.should eq backfilled.id
Model::Organisation.find?(backfilled.id.not_nil!).should_not be_nil
end
end
end
end
63 changes: 63 additions & 0 deletions spec/initialization_tenancy_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require "./spec_helper"
require "../src/tasks/initialization"

module PlaceOS
module Tasks
def self.production? : Bool
true
end

def self.start_customer_install(domain : String)
Initialization.start(
application_name: "backoffice",
domain: domain,
tls: false,
email: "administrator@example.com",
username: "Administrator",
password: "a-test-password-12345",
auth_host: "auth",
metrics_route: "monitor",
backoffice_branch: "master",
backoffice_commit: "HEAD",
)
end

describe "customer tenancy bootstrap" do
it "keeps an existing customer's configured domain out of management staff" do
authority = Model::Generator.authority("customer.example.com").save!
organisation = Model::Organisation.new(
name: "Existing customer",
description: "Auto-created from authority #{authority.id} during PPT-526 organisation backfill",
).save!
authority.organisation_id = organisation.id
authority.save!

start_customer_install(authority.domain)

persisted = Model::Authority.find!(authority.id.as(String))
persisted.organisation_id.should eq organisation.id
persisted.organisation.not_nil!.partner_staff.should be_false
Model::Partner.where(management: true).count.should eq 0
end

it "preserves management privileges for a fresh platform bootstrap" do
start_customer_install("fresh-customer.example.com")

authority = Model::Authority.find_by_domain("fresh-customer.example.com").not_nil!
organisation = authority.organisation.not_nil!
organisation.partner_staff.should be_true
organisation.partner.not_nil!.management.should be_true
Model::User.find_by_email(authority.id.as(String), "administrator@example.com").not_nil!.sys_admin.should be_true
end

it "does not treat an existing unowned authority as a fresh management bootstrap" do
authority = Model::Generator.authority("legacy-unowned.example.com").save!

start_customer_install(authority.domain)

Model::Authority.find!(authority.id.as(String)).organisation_id.should be_nil
Model::Partner.where(management: true).count.should eq 0
end
end
end
end
3 changes: 2 additions & 1 deletion spec/migration/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ WORKDIR /app
COPY . /app

# NOTE:: add `--branch my-new-model-branch` if you want to test a particular branch
RUN git clone https://github.com/PlaceOS/models
# PPT-526: the tenancy migrations live on this branch until models #332 merges
RUN git clone --branch PPT-526-partner-client https://github.com/PlaceOS/models
RUN mv ./models/migration/db ./db

ENV PATH /app/bin:$PATH
Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ PgORM::Database.configure { |_| }
def clear_tables
PgORM::Database.exec_sql <<-SQL
TRUNCATE "asset", "asset_type", "asset_category", "metadata", "zone",
"booking_instances", "bookings", "tenants", "shortener", "user", "authority", "history" CASCADE
"booking_instances", "bookings", "tenants", "shortener", "user", "authority", "history",
"grants", "organisations", "partners" CASCADE
SQL
end

Expand Down
4 changes: 3 additions & 1 deletion src/sam.cr
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ namespace "create" do

desc "Creates a representative set of documents in PostgreSQL DB"
task "placeholders" do
PlaceOS::Tasks.create_placeholders
# placeholders belong to the staff organisation when one exists
staff = PlaceOS::Model::Organisation.where(partner_staff: true).first?
PlaceOS::Tasks.create_placeholders(staff.try(&.id))
end

desc "Creates an authority"
Expand Down
Loading
Loading