Skip to main content

Credentials

Struct Credentials 

Source
pub struct Credentials {
    pub server_url: String,
    pub token: Option<String>,
    pub email: String,
    pub auth: Option<KeycloakSession>,
}
Expand description

credentials.json.

token and auth are both optional and mutually exclusive in practice: token is written by an API-key login, auth by tracevault login. Neither is written when absent, so an API-key file stays byte-identical in shape to what earlier CLI versions wrote.

Fields§

§server_url: String§token: Option<String>

SECRET. A tvk_ API key, when this file holds one.

§email: String

The signed-in identity, for display. Empty when a login saved credentials before /auth/me could resolve it (e.g. the account has no tracevault grant in Control Plane).

§auth: Option<KeycloakSession>

The Keycloak session, when this file holds one.

Implementations§

Source§

impl Credentials

Source

pub fn keycloak( server_url: &str, email: String, session: KeycloakSession, ) -> Self

A Keycloak session credential file (what tracevault login writes).

§The single policy both writers follow

A write never destroys a credential it did not come to replace. This constructor and Self::persist_session are the only two writers, and they now agree: persist_session replaces auth and keeps everything else, and this preserves any pre-existing token (an API key) instead of dropping it. Logging in supersedes a key without deleting it, which is safe because Self::credential already prefers a Keycloak session over a leftover token — so the key is inert while the session lives, and still there if the session is later removed.

The token is only carried over when the existing file is for the SAME server (same_server): moving another instance’s key into this instance’s file would be the same cross-instance mixing the resolution guards exist to prevent.

There is deliberately no api_key counterpart constructor: nothing in the CLI creates an API-key credentials file (a key comes from TRACEVAULT_API_KEY or config.toml); such a file is only ever READ, from one a user or an older CLI version wrote.

Source

pub fn credential(&self) -> Option<Credential>

Which credential this file carries. A Keycloak session wins over a leftover token so a re-login supersedes an old API key rather than silently continuing to use it.

Source

pub fn path() -> Result<PathBuf, Error>

Where the credential file lives, or an error naming what is missing.

Returns Err rather than inventing a path. The previous fallback, PathBuf::from("~/.config"), is a RELATIVE path — a shell expands ~, Path does not — so with no resolvable config directory login created ./~/.config/tracevault/ in the current working directory and wrote an offline refresh token into it, plausibly inside the user’s repo. There is no defensible guess for “where is this user’s config”, so failing loudly is the only correct answer.

Source

pub fn existing_path() -> Result<PathBuf, Error>

The file the login is in now: Self::path, else the legacy file when only that one exists.

Source

pub fn path_for_display() -> String

The credential file’s path for DISPLAY, when there is nothing useful to do about a missing config directory (a message is being printed either way). Never used to read or write.

Source

pub fn load() -> Option<Self>

Source

pub fn save(&self) -> Result<(), Error>

Write the credential file atomically, readable only by its owner.

Atomic because a token refresh rewrites this file while other processes (git hooks, a parallel tracevault stream) may be reading it: a plain truncate-then-write leaves a window in which a reader parses a half-written file and concludes the user is logged out. The temp file is created in the SAME directory so the rename stays within one filesystem and is therefore atomic.

The temp file is created with create_new(true), which is what makes the 0600 promise real: OpenOptions::mode() applies ONLY when the file is created, so opening a path that already exists inherits whatever mode (or symlink target) is already there, and the following rename would then publish that mode as the credential file’s. A stale .credentials.json.tmp* left by an earlier crash under a looser umask is enough to defeat it. create_new turns that case into AlreadyExists, which we resolve by picking a fresh name rather than failing the save.

Source

pub fn persist_session( server_url: &str, session: &KeycloakSession, ) -> Result<(), Error>

Persist refreshed Keycloak tokens for server_url, keeping every other field of the file as it is on disk — including a token (API key), per the single write policy documented on Self::keycloak.

Re-reads the file rather than rewriting a cached copy so a concurrently changed server_url/email isn’t reverted. A missing file is not an error: there is nothing to keep in sync (the session may be held by a process whose file was removed by tv logout).

Writes NOTHING when the file on disk belongs to a different server (see same_server). There is one credentials file, so a long-running process holding instance A’s session would otherwise overwrite the credentials of instance B that the user logged into meanwhile — silently signing them out of B. Skipping is not an error: A’s refreshed token still works for the rest of A’s process lifetime.

The skip warns, because it is otherwise undiagnosable from the outside: if the two URLs differ only by formatting the mismatch is PERMANENT, so persistence is disabled for every future invocation and the CLI silently re-refreshes forever.

Source

pub fn delete() -> Result<(), Error>

Remove the credential file — and any atomic-write temp files left behind.

The sweep is the difference between “logout” and “logout, except for the refresh tokens still on disk”: every .credentials.json.tmp* holds a full credential JSON, so a temp file abandoned by an earlier crash between open and rename would keep a working offline refresh token in the config directory after the user asked to be logged out. No age threshold here (unlike a save’s sweep): the user is logging out, so a save racing this is already a lost race — and its failed rename cannot resurrect the credential file, since the very next thing that happens is this delete.

The sweep is best-effort: a stray file that cannot be removed must not fail the logout.

Trait Implementations§

Source§

impl Debug for Credentials

Hand-written so no {:?} can leak the API key.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Credentials

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Credentials

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more