Skip to main content

Creating a Device Manager tenant

By now, Device Manager should already be properly initialized and we are ready to create our first tenant.

We first need to have a backing Data Orchestrator instance with an already existing Data Orchestrator realm which will be a 1:1 match with the Device Manager tenant.

Then we can create the Device Manager tenant with a couple of steps.

Creating a key pair for the tenant​

A cryptographic keypair is needed to emit and validate tokens to access your tenant. You can generate an EC keypair with the following OpenSSL commands:

$ openssl ecparam -name prime256v1 -genkey -noout > tenant_private.pem
$ openssl ec -in tenant_private.pem -pubout > tenant_public.pem

Creating a tenant with the Admin API​

The next step is generating a token to access Device Manager Admin Rest API. You can do so using the gen-edgehog-jwt tool contained in the tools directory of the Device Manager repo. Starting from the private key we generated earlier in the deployment process for the Admin API, admin_private.pem, this command should give you a valid auth token to access the API. The tool requires Elixir in your system, which you can install with asdf install using the asdf version manager.

$ ./tools/gen-edgehog-jwt -t admin -k <PATH-TO-ADMIN-PRIVATE-KEY>

Note that the token expires after 24 hours by default. If you want to have a token with a different expiry time, you can pass -e <EXPIRY-SECONDS> to the gen-edgehog-jwt command.

You can test the auth token by listing the existing tenants in the Device Manager instance:

curl -X GET --location 'https://<EDGEHOG-API-HOST>/admin-api/v1/tenants' \
--header 'Content-Type: application/vnd.api+json' \
--header 'Authorization: Bearer <ADMIN-TOKEN>'

Then, to actually create the tenant. Note that both the tenant public key and the Data Orchestrator realm private key are multi-line PEM files, so they cannot be pasted directly into a JSON payload: we use jq to build a valid request body, which takes care of quoting and escaping their content.

$ jq -n \
--arg name '<TENANT-NAME>' \
--arg slug '<TENANT-SLUG>' \
--arg public_key "$(cat tenant_public.pem)" \
--arg base_api_url '<ASTARTE-BASE-API-URL>' \
--arg realm_name '<ASTARTE-REALM-NAME>' \
--arg realm_private_key "$(cat <REALM-PRIVATE-KEY-FILE>)" \
'{
data: {
type: "tenant",
relationships: {},
attributes: {
name: $name,
slug: $slug,
public_key: $public_key,
default_locale: "en-US",
astarte_config: {
base_api_url: $base_api_url,
realm_name: $realm_name,
realm_private_key: $realm_private_key
}
}
}
}' > tenant_payload.json

$ curl -X POST --location 'https://<BACKEND-HOST>/admin-api/v1/tenants' \
--header 'Content-Type: application/vnd.api+json' \
--header 'Authorization: Bearer <ADMIN-TOKEN>' \
--data @tenant_payload.json

Values to be replaced:

  • BACKEND-HOST: the domain which exposes Device Manager API. This is the same as BACKEND-HOST from the Ingress definition.
  • ADMIN-TOKEN: the auth token generated from the admin private key to access Device Manager Admin API.
  • TENANT-NAME: the name of the new tenant.
  • TENANT-SLUG: the slug of the tenant, must contain only lowercase letters and hyphens.
  • ASTARTE-BASE-API-URL: the base API url of the Data Orchestrator instance (e.g. https://api.astarte.example.com).
  • ASTARTE-REALM-NAME: the name of the Data Orchestrator realm you're using.
  • REALM-PRIVATE-KEY-FILE: the path to the file containing the Data Orchestrator realm's private key.

Creating a tenant with an iEx session​

If the Admin API cannot be used for some reason, an alternative way can be to establish a live terminal session with Device Manager's backend and issue commands in the Elixir shell.

Connect to the iex interactive shell of the Device Manager backend using:

$ kubectl exec -it deploy/edgehog-backend -n edgehog -- /app/bin/edgehog remote

All the following commands have to be executed inside that shell, in a single session (since some commands will reuse the result of previous commands).

The following commands will create a database entry representing the tenant, with its associated Data Orchestrator cluster and Realm:

iex> alias Edgehog.Tenants
iex> tenant_name = "<TENANT-NAME>"
iex> tenant_slug = "<TENANT-SLUG>"
iex> tenant_public_key = """
<TENANT-PUBLIC-KEY>
"""
iex> base_api_url = "<ASTARTE-BASE-API-URL>"
iex> realm_name = "<ASTARTE-REALM-NAME>"
iex> realm_private_key = """
<ASTARTE-REALM-PRIVATE-KEY>
"""
iex> {:ok, tenant} = Tenants.provision_tenant(
%{
name: tenant_name,
slug: tenant_slug,
public_key: tenant_public_key,
astarte_config: %{
base_api_url: base_api_url,
realm_name: realm_name,
realm_private_key: realm_private_key
}
})

Values to be replaced:

  • TENANT-NAME: the name of the new tenant.
  • TENANT-SLUG: the slug of the tenant, must contain only lowercase letters and hyphens.
  • TENANT-PUBLIC-KEY: the content of tenant_public.pem created in the previous step. Open a multiline string with """, press Enter, paste the content of the file in the iex shell and then close the multiline string with """ on a new line.
  • ASTARTE-BASE-API-URL: the base API url of the Data Orchestrator instance (e.g. https://api.astarte.example.com).
  • ASTARTE-REALM-NAME: the name of the Data Orchestrator realm you're using.
  • ASTARTE-REALM-PRIVATE-KEY: the content of the Data Orchestrator realm's private key. Open a multiline string with """, press Enter, paste the content of the file in the iex shell and then close the multiline string with """ on a new line.