Skip to content

OCI: support profiles that authenticate with a security token - #4308

Open
fede-kamel wants to merge 1 commit into
dstackai:masterfrom
fede-kamel:fix-oci-session-token-auth
Open

fede-kamel wants to merge 1 commit into
dstackai:masterfrom
fede-kamel:fix-oci-session-token-auth

Conversation

@fede-kamel

Copy link
Copy Markdown

Steps to reproduce

Create an OCI profile with oci session authenticate (the flow the OCI docs recommend for federated/SSO logins) and point the oci backend at it:

- type: oci
  regions: [us-phoenix-1]
  creds:
    type: default
    profile: <session token profile>

Actual behaviour

The backend cannot be configured. The credentials are reported invalid even though the same profile works with the OCI CLI:

creds_valid(OCIDefaultCreds(profile="<session token profile>"))  # False

get_client_config returns the profile as the SDK config, and region.py then builds every client from it without a signer, so the SDK constructs its default API key signer. A profile written by oci session authenticate has a short-lived security_token_file and an ephemeral key pair instead of a registered API key, and no user entry, so that signer cannot sign for it.

Expected behaviour

Session token profiles authenticate, like they do with the OCI CLI and the OCI SDK when it is given a SecurityTokenSigner.

Change

Build an oci.auth.signers.SecurityTokenSigner when the config carries security_token_file, and pass it to every client the backend constructs. API key profiles are unaffected: get_signer returns None and the SDK keeps building its default signer.

Tests

New src/tests/_internal/core/backends/oci/test_auth.py covering the API key case, the security token case, and token whitespace handling. OCI backend suite 23 passed, whole backends suite 453 passed, ruff check and ruff format clean.

Verified against a real session token profile: creds_valid returns True, the backend configures, offers are returned, and a run provisions.

Note

Security tokens are short-lived (about an hour). The signer reads the token file when a client is constructed, so a token refreshed with oci session refresh is picked up by later clients, but a server holding a backend open across an expiry will still see failures. Happy to look at refresh handling separately if you think it is worth it.

`get_client_config` returns the profile as the SDK config and every OCI client
is then built from it without a signer, so the SDK builds its default API key
signer. A profile created by `oci session authenticate` carries a short-lived
security token and an ephemeral key pair rather than a registered API key, and
has no `user` entry, so that signer cannot authenticate with it: configuring
the backend fails with `creds_valid()` returning False even though the same
profile works with the OCI CLI.

Build an `oci.auth.signers.SecurityTokenSigner` when the config has
`security_token_file`, and pass it to every client the backend constructs. API
key profiles keep using the SDK default signer.

Verified against a session token profile: `creds_valid` now returns True, the
backend configures, and offers are returned.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant