First-run setup and tenant connection

Setup is entirely web-based. No app registration is needed beforehand: the setup wizard creates and configures the Entra app registrations itself, using a one-time device code sign-in by a tenant administrator.

Before you start

Nothing is required in advance. Optionally:

  • Create the alerts mailbox yourself. In the Exchange admin center, create a shared mailbox backup-alerts@<your domain>. The wizard finds it and uses it instead of creating one. The send permission still has to be scoped to it, so the Exchange sign-in step (or the manual commands) still runs; only mailbox creation is skipped.

  • Check device code sign-in is allowed for the admin account (see Fallbacks).

Wizard steps

Note

Built so far: step 1, and steps 4 and 7 as one action, Microsoft 365 tenant → Connect tenant. It creates the Backup, Restore and Sign-in apps. The repository, notifications and verify steps come next.

  1. Create the break-glass admin. The first visit asks for the one-time setup token printed in the api container log (docker compose logs api | grep setup_token), then a username, password and authenticator enrolment, and shows recovery codes once. This account is for emergencies when Microsoft 365 sign-in is unavailable; see Authentication and roles.

  2. Create repository. Choose the storage location (default: the backup-repo volume) and a repository passphrase; download the recovery kit.

  3. Choose modules. Toggle Entra, Exchange, OneDrive, SharePoint. The page shows the Graph permissions this selection needs.

  4. Connect tenant (device code). The wizard shows a code and the link https://microsoft.com/devicelogin. The admin signs in on any device, approves the requested delegated permissions, and the wizard continues on its own (see below).

  5. Notifications. Choose Graph or SMTP and add admin recipients. For Graph, the wizard offers to create the backup-alerts@… shared mailbox and scope the send permission to it, which takes a second device code sign-in for Exchange Online. See Notifications.

  6. Verify. The wizard runs each enabled module’s health check and shows what it can see (number of users, mailboxes, sites).

  7. Entra sign-in for the web UI (recommended). Creates the “Mail Safehouse – Sign-in” app registration with the redirect URI and the four app roles, requires assignment, and assigns the signed-in admin the Admin role. Day-to-day users then sign in with Microsoft.

What happens during “Connect tenant”

        sequenceDiagram
    participant UI as Setup wizard
    participant API as s365b api
    participant AAD as Entra ID
    participant Admin as Tenant admin (any browser)

    UI->>API: start connection
    API->>AAD: device code request (public client, delegated scopes)
    AAD-->>API: user code + verification URL
    API-->>UI: show code
    Admin->>AAD: microsoft.com/devicelogin, sign in, consent
    API->>AAD: poll until token issued
    API->>API: generate key pair (private key stays on server)
    API->>AAD: create "Mail Safehouse – Backup" app + service principal, upload public cert
    API->>AAD: grant application permissions (app role assignments) for enabled modules
    API->>AAD: create "Mail Safehouse – Restore" app the same way (write permissions)
    API->>AAD: if Graph email chosen: create "Mail Safehouse – Notifications" app (no Entra permissions)
    API->>API: discard delegated token, store tenant ID, app IDs, encrypted private keys
    API->>AAD: test app-only sign-in with certificate
    API-->>UI: connected
    

Key points:

  • Delegated token is used once and thrown away. No refresh token is stored. From then on the app runs app-only with certificate credentials.

  • Private keys are generated on the server and never leave it. Only the public certificate is uploaded to Entra.

  • Admin consent is granted by the wizard by creating app role assignments on the Microsoft Graph service principal. Permission IDs are looked up by name from the tenant’s Microsoft Graph service principal, not hard-coded.

  • Least privilege by module. Only the enabled modules’ permissions are granted. Enabling a module later shows “additional permissions needed” and runs the device code step again to add just those (and removes permissions for modules that were disabled, if the admin agrees).

  • Certificate renewal. Certificates are valid for 1 year. Re-running Connect tenant issues new ones; the certificate in use stays registered next to the new one until the following renewal, so running jobs and Entra servers that have not yet seen the new key keep working. Expiry warnings 30 days ahead are planned.

  • Idempotent. Re-running setup finds the existing app registrations by a tag and updates them rather than creating duplicates.

Who has to sign in

The admin approving the device code needs a role that can create apps and grant application permissions to Microsoft Graph: Global Administrator, or Privileged Role Administrator together with Application Administrator. The delegated scopes requested are:

Scope

Why

Application.ReadWrite.All

Create/update the app registrations and certificates

AppRoleAssignment.ReadWrite.All

Grant (admin-consent) application permissions, assign you the Admin role

DelegatedPermissionGrant.ReadWrite.All

Consent the sign-in app’s openid profile email User.Read for all users

User.Read

Show who signed in, for the audit log

The optional Exchange Online sign-in (for the notifications mailbox) uses the Exchange Online PowerShell public client and the admin’s Exchange admin rights, which a Global Administrator already has.

Which client starts the device code flow

A device code flow needs a public client application ID before our own apps exist. Default: the Microsoft first-party Microsoft Graph Command Line Tools public client, which is what Microsoft Graph PowerShell uses and is already trusted in most tenants. The client ID is a setting, so an admin can point it at their own public client registration instead.

Fallbacks

  • Device code flow blocked. Many tenants block device code flow with Conditional Access (Microsoft recommends this). The wizard detects the error and offers: (a) temporarily exclude the admin from that policy, or (b) manual setup: a step-by-step guide (with the exact permission list for the chosen modules) to create the app in the Entra portal, then upload or generate a certificate in the wizard.

  • Exchange mailbox scoping (RBAC for Applications) needs Exchange Online PowerShell and cannot be done through Graph. It is optional; the docs give the commands for admins who want to limit which mailboxes the app can read.