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: StringThe 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
impl Credentials
Sourcepub fn keycloak(
server_url: &str,
email: String,
session: KeycloakSession,
) -> Self
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.
Sourcepub fn credential(&self) -> Option<Credential>
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.
Sourcepub fn path() -> Result<PathBuf, Error>
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.
Sourcepub fn existing_path() -> Result<PathBuf, Error>
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.
Sourcepub fn path_for_display() -> String
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.
pub fn load() -> Option<Self>
Sourcepub fn save(&self) -> Result<(), Error>
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.
Sourcepub fn persist_session(
server_url: &str,
session: &KeycloakSession,
) -> Result<(), Error>
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.
Sourcepub fn delete() -> Result<(), Error>
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.
impl Debug for Credentials
Hand-written so no {:?} can leak the API key.