# `Ankusa.BlobStore.OCI.InstancePrincipal`
[🔗](https://github.com/jamescarr/ankusa/blob/ankusa-v0.7.0/packages/ankusa/lib/ankusa/blob_store/oci/instance_principal.ex#L1)

Session credentials for `Ankusa.BlobStore.OCI` from the instance's own
identity, for a node on OCI compute or OKE: no API key to store or rotate.

    {Ankusa.BlobStore.OCI,
     region: "us-ashburn-1",
     namespace: "…",
     bucket: "ankusa-segments",
     key_provider:
       {Ankusa.BlobStore.OCI.InstancePrincipal, :credentials, [[region: "us-ashburn-1"]]}}

The federation the OCI SDKs perform, in four steps:

  1. the instance metadata service (`http://169.254.169.254/opc/v2`, header
     `Authorization: Bearer Oracle`) gives the instance's leaf certificate, its
     private key and the intermediate certificates;
  2. a fresh RSA session key pair is generated;
  3. `POST https://auth.<region>.oraclecloud.com/v1/x509` sends the leaf
     certificate, the intermediates and the session public key, signed with the
     leaf key (`keyId` `<tenancy>/fed-x509-sha256/<sha256 fingerprint>`, the
     tenancy read from the certificate's `opc-tenant:` organizational unit);
  4. the answer is a short-lived `ST$<token>` session token; objects are then
     signed with the **session** private key under `key_id: "ST$<token>"`.

The credentials are cached node-wide until five minutes before the token's own
`exp`, so the adapter's per-request call is an ETS read; a `401` makes the
adapter call `invalidate/1`, and its retry federates again.

Arguments (a keyword list, the one optional argument of `credentials/1` and
`invalidate/1`):

  * `:region` — required, e.g. `"us-ashburn-1"`; picks the auth service
  * `:tenancy_ocid` — override the tenancy read from the certificate
  * `:imds_endpoint` — default `"http://169.254.169.254/opc/v2"`
  * `:auth_endpoint` — default `"https://auth.<region>.oraclecloud.com"`
  * `:timeout_ms` — default `10_000`
  * `:req_options` — transport options, e.g. `plug:` for `Req.Test`

# `credentials`

```elixir
@spec credentials(keyword()) ::
  {:ok, %{key_id: String.t(), private_key: String.t()}} | :error
```

`{:ok, %{key_id: "ST$…", private_key: pem}}`, or `:error` when federation failed.

# `invalidate`

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

Drop the cached session: the next `credentials/1` federates again.

---

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