# `Ankusa.BlobStore.Azure.ManagedIdentity`
[🔗](https://github.com/jamescarr/ankusa/blob/ankusa-v0.7.0/packages/ankusa/lib/ankusa/blob_store/azure/managed_identity.ex#L1)

Azure AD access tokens from the Instance Metadata Service (IMDS) — Azure's
*managed identity*, the best credential for a service running on Azure: no
secret to store or rotate, short-lived tokens, and least privilege via a
role assignment (`Storage Blob Data Contributor`) on the identity rather
than an account key.

Used as the `:token_provider` for `Ankusa.BlobStore.Azure`:

    # system-assigned identity — no config at all
    {Ankusa.BlobStore.Azure.ManagedIdentity, :token, []}

    # user-assigned identity
    {Ankusa.BlobStore.Azure.ManagedIdentity,
     :token, [client_id: System.get_env("AZURE_CLIENT_ID")]}

The token is fetched from `http://169.254.169.254/metadata/identity/oauth2/token`
(only reachable from inside Azure) with the required `Metadata: true` header.
It is cached and refreshed five minutes before expiry, so the adapter's
per-request `:token_provider` call is an ETS read, not an IMDS round-trip.

opts:

  * `:client_id` — a user-assigned managed identity's client id; omit for the
                    system-assigned identity
  * `:resource`  — default `"https://storage.azure.com"` (Blob Storage)
  * `:endpoint`  — default the IMDS token URL; override in tests
  * `:timeout_ms` — default `10_000`
  * `:req_options` — transport options, e.g. `plug:` for `Req.Test`

# `invalidate`

```elixir
@spec invalidate(keyword()) :: :ok
```

Drop the cached token for `opts`: the next `token/1` asks IMDS for a new one.
`Ankusa.BlobStore.Azure` calls it (with the provider's arguments) on a `401`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
