s365b.core.services

Process-wide services, built once at startup.

The API and the worker both create a Services object and pass it where needed, instead of using module-level globals. Tests build one with fakes.

Attributes

Classes

AppPurpose

What an app registration is used for.

Services

Shared services for one process.

Functions

app_key_context(app) → str

Associated-data string for an app registration's encrypted private key.

repo_passphrase_context(repo) → str

Associated-data string for a repository's encrypted passphrase.

Module Contents

class s365b.core.services.AppPurpose[source]

Bases: enum.StrEnum

What an app registration is used for.

BACKUP = 'backup'
RESTORE = 'restore'
NOTIFICATIONS = 'notifications'
SIGNIN = 'signin'
s365b.core.services.app_key_context(app: s365b.db.models.AppRegistration) → str[source]

Associated-data string for an app registration’s encrypted private key.

Parameters:

app – The app registration row.

Returns:

Context string passed to SecretBox.

s365b.core.services.repo_passphrase_context(repo: s365b.db.models.RepositoryConfig) → str[source]

Associated-data string for a repository’s encrypted passphrase.

Parameters:

repo – The repository row.

Returns:

Context string passed to SecretBox.

s365b.core.services.GraphFactory[source]
class s365b.core.services.Services[source]

Shared services for one process.

Variables:
  • settings – Process settings.

  • db – Database access.

  • registry – Allowed modules.

  • secrets – Encrypts/decrypts secrets stored in the database.

  • graph_factory – Builds a Graph client for an app registration; replaced in tests.

  • msal_app_factory – MSAL app class for Entra sign-in; replaced in tests.

  • device_code_factory – Builds the setup wizard’s device code client from a public client ID; replaced in tests.

  • credential_factory – Builds the app-only token provider for an app registration; replaced in tests.

settings: s365b.core.config.Settings[source]
db: s365b.db.session.Database[source]
registry: s365b.modules.registry.ModuleRegistry[source]
secrets: s365b.core.secrets.SecretBox[source]
graph_factory: GraphFactory | None = None[source]
msal_app_factory: Any = None[source]
device_code_factory: collections.abc.Callable[[str], Any] | None = None[source]
credential_factory: collections.abc.Callable[[s365b.db.models.Tenant, s365b.db.models.AppRegistration], s365b.graph.auth.TokenProvider] | None = None[source]
enqueue_factory: collections.abc.Callable[[int, str], collections.abc.Awaitable[None]] | None = None[source]
retention_factory: collections.abc.Callable[[int, str], collections.abc.Awaitable[None]] | None = None[source]
restore_factory: collections.abc.Callable[[int], collections.abc.Awaitable[None]] | None = None[source]
notifier_factory: collections.abc.Callable[[str], Any] | None = None[source]
classmethod from_settings(settings: s365b.core.config.Settings) → Services[source]

Build the real services from settings.

Parameters:

settings – Process settings.

Returns:

The services.

graph_for(tenant: s365b.db.models.Tenant, app: s365b.db.models.AppRegistration) → s365b.graph.client.GraphClient[source]

Return a Graph client authenticated as an app registration.

Parameters:
  • tenant – The tenant.

  • app – The Backup, Restore or Notifications app.

Returns:

A new Graph client; close it with aclose().

async enqueue_backup(job_id: int, requested_by: str) → None[source]

Queue a backup job for the worker.

Uses the same queueing lock as the scheduler, so a job that is already waiting is not queued twice.

Parameters:
  • job_id – Backup job ID.

  • requested_by – Username shown on the run.

Raises:

procrastinate.exceptions.AlreadyEnqueued – If the job is already waiting to run.

async enqueue_retention(repository_id: int, requested_by: str) → None[source]

Queue a retention run for a repository.

Parameters:
  • repository_id – Repository row ID.

  • requested_by – Username shown on the run.

Raises:

procrastinate.exceptions.AlreadyEnqueued – If one is already waiting.

async enqueue_restore(run_id: int) → None[source]

Hand a queued restore run to a worker.

Parameters:

run_id – The job run, already recorded as queued.

async aclose() → None[source]

Close the job queue connection and the database pool.

credential_for(tenant: s365b.db.models.Tenant, app: s365b.db.models.AppRegistration) → s365b.graph.auth.TokenProvider[source]

Return the app-only token provider (certificate credential) for an app.

Parameters:
  • tenant – The tenant.

  • app – The app registration.

Returns:

A token provider.

backend_for(repo: s365b.db.models.RepositoryConfig) → s365b.storage.backends.StorageBackend[source]

Return the storage backend of a repository.

Parameters:

repo – The repository row.

Returns:

The backend.

Raises:

ValueError – For backends not implemented yet.

async open_repository(repo: s365b.db.models.RepositoryConfig) → s365b.storage.repository.Repository[source]

Open (or reuse) a repository, refreshing its index.

Parameters:

repo – The repository row.

Returns:

The open repository.