Metadata-Version: 2.4
Name: scalio-m365
Version: 0.4.1
Summary: Scalio: read and write Microsoft 365 settings (Entra, SharePoint, Intune, Exchange, Teams) to a live tenant, shared by Foundry and Citadel
License: Proprietary
Requires-Python: >=3.11
Description-Content-Type: text/markdown
Requires-Dist: httpx>=0.27
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Requires-Dist: ruff>=0.6; extra == "dev"

# scalio-m365

Shared code that reads («Scalio Lesen») and writes («Scalio Schreiben») Microsoft 365 settings of a live tenant: one
reader and one writer per
Microsoft365DSC resource type (Entra ID, SharePoint, Intune, Exchange Online / Defender for Office 365, Teams), with the
property names of the governance catalog (scalio-catalog). Foundry rolls baselines out with it, Citadel heals
deviations. Decision: scalio-docs `concepts/m365-schreibpaket.md` (`m365pkg/P1`–`P4`), ADR 0089.

Nothing in this package is secret and it holds no content: the baselines stay in the private repo
`scalio_blueprint`, tokens and certificates stay in the app (scalio-tenant).

## Use in an app

```python
import scalio_m365 as m365

g = m365.Graph(call, exo, create=True)        # Foundry; Citadel: create=False (keeps existing objects only)
reason = m365.why_not(type_, desired)          # Citadel: why_not(type_, desired, change_only=True)
actual = await m365.read_one(g, type_, match)  # catalog names
after = m365.fields(type_, {**(actual or {}), **desired})
if not m365.same(actual, after):
    await m365.kind(type_).write(g, actual, after)
```

- `call(method, path, body) -> dict`: one Microsoft Graph call (`path` relative to `/v1.0`, or a full
  `@odata.nextLink`); raise on an error. `exo(cmdlet, parameters) -> list[dict]`: one cmdlet through the Exchange
  admin API (`InvokeCommand`, app-only token). `teams(method, path, body)`: one call of the Teams admin API
  (`path` below `teams.API`, app-only token for `teams.RESOURCE`, Entra role Teams Administrator); passed as
  `Graph(call, exo, teams=teams)`. The app brings token, HTTP client and logging.
- `WriteError`: the step cannot be written as asked (missing group, empty break-glass group, object missing,
  no reviewer, no notification address); the text says what to do.
- `SCOPES` (Graph permissions), `exchange.SCOPES`, `teams.SCOPES`, `DIRECTORY_ROLES` (Exchange and Teams Administrator), `READ_ALONG` and `needs(type_)`: what the
  «Scalio Schreiben» registration needs.
- Exchange in a fresh tenant: `exchange.prepare(g, is_dehydrated=..., customised_at=...)` runs
  `Enable-OrganizationCustomization` once and says when to wait; the app stores the time.
- `scalio_m365.mapping`: pure name mappings (Graph answer -> catalog names) for the apps' readers.

## Exchange admin API

```python
from scalio_m365 import AsyncExo

exo = AsyncExo(token, tenant_id, app="ScalioRosetta")     # token(): app-only token for EXO_RESOURCE (may be async)
rows = await exo("Get-OrganizationConfig")                # every page
await exo.invoke("Set-Mailbox", {"Identity": "a@b.ch", "EmailAddresses": DeltaUpdate(add=["smtp:x@b.ch"])})
g = m365.Graph(call, exo, create=False)                   # fits the writers
```

- One client for the API behind ExchangeOnlineManagement (`InvokeCommand`, not documented by Microsoft), ADR 0105.
  `SyncExo` is the same for synchronous code (the readers).
- Retries: 429 always (after `Retry-After`), 5xx and network errors only for `Get-` (a `New-` never runs twice),
  401 once with a new token. Paging ends after `MAX_PAGES`.
- `ExoError` with `status`, `kind` (not_found, denied, auth, throttled, server, cmdlet), `exception_type`,
  `request_id`; the reason comes from `details`. Never the token or the parameters.
- `observe(step)` gets every change and every error (for the app's log); `anchored(mailbox)` routes to a mailbox.
- Not here: `Get-EXO*` (own endpoints; use `Get-Mailbox`, `Get-CASMailbox`), Security & Compliance (IPPS), Teams.

## Read

```python
from scalio_m365 import readers

resources, fields, problems = readers.read(graph_token, types, client=httpx.Client(timeout=60),
                                           tokens=admin_token, tenant=tenant_id, app="ScalioCitadel")
```

- `resources`: `{"type", "properties"}` with the catalog names (ids of groups, users, roles and locations become
  display names, as in DSC). `fields[type]`: the properties read; a type left out counts as «not read», never as a
  deviation. `problems[type]`: why Microsoft refused a type (missing permission ...); a 401 of Graph stops the read.
- Graph v1.0/beta for Entra, SharePoint and Intune; Exchange through `SyncExo` and Teams through the
  Teams admin API with app-only tokens from `tokens(resource)` (`readers.EXO_RESOURCE`, `readers.TEAMS_RESOURCE`).
- `READERS` (type -> reader), `FIELDS`, `DYNAMIC` (types whose answer decides the fields), `ADMIN_APIS`.
- `scalio_m365.testing.FakeMicrosoft`: fake Graph, Exchange and Teams admin API for the apps' tests.

## Modes

| | Foundry (`create=True`) | Citadel (`create=False`, `change_only=True`) |
| --- | --- | --- |
| Entra, SharePoint | creates or changes | creates or changes |
| Intune policy missing | creates (compliance with a block action) | refuses: create it with its assignments first |
| Exchange policy missing | Defender policies with a rule per accepted domain, DKIM | refuses |
| Names of Intune/Exchange objects | set on create | never changed (`why_not` says so) |

## Development

```bash
pip install -e ".[dev]"
pytest && ruff check .
```

Release: bump `pyproject.toml` and `__version__`, reserve the version in scalio-docs `REGISTER.md`, run
`tools/changelog.py --repo scalio-m365 release <version>` there, then «Freigeben» in the cockpit (workflow
`publish.yml`, PyPI trusted publishing, environment `pypi`). Tags without `v`.
