CodamAIDocs
Topicdone

The tenant key

Why the key cannot change, which characters are allowed, and that it is also the database name. How it is built during a self-registration.

Variants
from the company namefrom the email domaincustom building rule

What this is about

Every tenant has a key, for example nordbau or stadtwerke-nord. The key is the name by which the whole system knows the tenant:

  • It is in the token, as the attribute tenant or as the alias of the organization.
  • It is the name of the tenant’s database.
  • It is what the tenant gate asks CIAS about.

Besides that, a tenant has a display name (displayName), for example “Nordbau GmbH”. People read it. If it is missing, CIAS shows the key.

The rules

RuleReason
only lowercase letters a–z, digits 0–9 and hyphen -These are the characters CDMS accepts as database names. CDMS checks them again right before the CREATE DATABASE
at least one characteran empty name names nothing
at most 64 charactersMySQL does not allow more for a name
not system and not singleCDMS uses these names itself: system is the system database, single the one database in SINGLE operation. A tenant with such a name would end up there
unique in the whole installationone database per tenant; the table cias_tenant enforces this with a uniqueness constraint
immutablesee above; there is no endpoint that changes it

CIAS checks these rules already when a tenant is created, not only when the database is set up. Otherwise there might already be an organization in Keycloak before the name fails at the database.

Is this a valid key?
KeyResult
nordbauvalid
stadtwerke-nord-2valid
systeminvalid, reserved
Nordbauinvalid, uppercase letter
nord_bauinvalid, underscore
müllerinvalid, umlaut
65 characters longinvalid, too long

An invalid key when creating results in 400 cias.tenancy.invalid-request, and the message says which rule is broken. A key that is already taken results in 409 cias.tenancy.key-already-used.

Display name instead of renaming

Companies change their names. That is what the display name is for: It may contain anything, including umlauts and spaces, and it is not part of any database. “Nordbau GmbH” can later become “Nordbau Holding GmbH”, and the key stays nordbau.

You can set the display name through the admin API when creating. There is no separate endpoint to change it.

How the key is built during a self-registration

During a self-registration, a person fills in a form. The form does not ask for a database name. That is why CIAS builds the key itself if neither the configuration of the flow nor a hook names one.

Key building during self-registration
  1. CIAS
    Choose source
    Field company filled in? Otherwise the domain of the email address without its ending
    ↳ no no source → registration fails with a configuration error
  2. CIAS
    Transform
    remove accents, lowercase, turn all other characters into -, at most 48 characters
    ↳ no nothing usable left → configuration error
  3. CIAS
    Free?
    Does the key exist already, or is it reserved? Then append -2, -3 … up to -20. The company “System” thus becomes system-2
    ↳ no all taken → configuration error
  4. Key for the new tenant
The sources of the key

When: The form contains company: "Café Ruiz & Söhne".

Accents are removed, not spelled out: é becomes e, ö becomes o, not oe. & and spaces become a single hyphen.

Result: cafe-ruiz-sohne

When: No company name, address anna@muster-bau.de.

CIAS takes the domain without its last ending. The part before the @ is never used: info or d.mertins would either be the same for many customers or named after a single person.

Result: muster-bau; example.co.uk would become example-co

When: muster-bau already exists.

CIAS tries muster-bau-2, then muster-bau-3 and so on up to -20. That is why the stem is limited to 48 characters: This way the suffix still fits into the 64.

Result: muster-bau-2

When: An installation wants a different rule, for example ue instead of u for ü, or a customer number.

It provides its own bean of type TenantKeyFactory. CIAS then uses this one instead of the built-in one. The suffix -2 to -20 and the rule check still run.

Result: If the custom rule returns an invalid key, the registration fails with a configuration error.

“Free?” is checked at registration, but the tenant is only created once the person has confirmed their address, so minutes or days later. If two registrations take the same key during this time, the second one fails because of the uniqueness constraint. CIAS does not reserve a name for a registration that may never be confirmed.

Pitfalls

Next

Sources in the code and the knowledge base
  • CIAS/cias-kernel – TenantKey (^[a-z0-9-]+$, at most 64 characters, isValid, RESERVED, isReserved); CIAS/cias-tenancy – Tenant.create; CIAS/cias-authentication – TenantGate; parent-commons – ReservedTenantKeys
  • CIAS/cias-tenancy – Tenant (key immutable, displayName, rename), V1__cias_tenant.sql (tenant_key VARCHAR(64), uq_cias_tenant_key)
  • CIAS/cias-registration – SlugTenantKeyFactory (company, email domain, at most 48 characters), TenantKeyFactory, RegistrationService.deriveTenantKey (suffix -2 to -20), CiasRegistrationConfiguration
  • commons-persistence – MySqlDatabaseCreator (name check before CREATE DATABASE)
Search