1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Signing in with an API key and signing out, for the daemon, which acts
//! for a client rather than a person at a terminal (`rho/auth/key`,
//! `rho/auth/logout`, and the environment and store checks of
//! `rho/auth/login`), and the shell check of `rho login` (#1662):
//!
//! - A request starts with [`load_config_and_store`], so it writes the store
//! config selects when it runs, not the one the process opened at start:
//! a store a terminal login chose since then is the one written.
//! - A credential set in the process environment wins over a saved one, so
//! signing in or out of its auth profile refuses before writing anything.
//! - An API key is checked against the provider's models endpoint and saved
//! unless the provider refuses it.
//! - Every write moves the credential generation (see
//! [`credential_store::AppCredentialStore::generation`]), so the process's running sessions
//! use the change from their next prompt. A refused request writes
//! nothing.
use std::fmt;
use anyhow::Context;
use http::StatusCode;
use rho_providers::{
auth::login_dispatch::ProviderAuthentication,
credentials::{self, CredentialError, CredentialStore},
model::{
provider_models::{check_api_key, ProviderModelEndpoint},
ModelError,
},
provider::{self, ProviderAuthKind, ProviderDescriptor},
};
use super::{bootstrap, config_repository::ConfigRepository, endpoint_login};
use crate::{config::Config, credential_store};
/// Why a sign-in or sign-out wrote nothing, or failed partway. No variant's
/// message holds a key.
#[derive(Debug, thiserror::Error)]
pub(crate) enum CredentialWriteError {
#[error("unknown auth profile '{auth}'")]
UnknownAuth { auth: String },
#[error("'{auth}' does not take an API key")]
NotAnApiKey { auth: String },
/// A keyless auth profile, which names no credentials and, being every
/// keyless provider's, no provider.
#[error("'{auth}' has no credentials to sign out of")]
NoCredentials { auth: String },
/// A variable in the process environment holds `auth`'s credential,
/// which wins over a saved one: saving or deleting one would change
/// nothing.
#[error("'{auth}' reads its credential from {variable}; unset it to {write}")]
EnvironmentOverride {
auth: String,
variable: &'static str,
write: CredentialWrite,
},
#[error("the key is blank")]
BlankKey,
/// The provider refused the key: 400 or 401 from its models endpoint.
/// `reason` says what failed.
#[error("{provider} refused the key: {reason}")]
KeyRefused {
provider: &'static str,
reason: String,
},
/// Config could not be read or updated.
#[error("{0:#}")]
Config(anyhow::Error),
/// The credential store could not be opened, read, or written.
#[error("{0:#}")]
Store(anyhow::Error),
}
/// What an environment override refused.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum CredentialWrite {
SignIn,
/// An interactive login, whose credential is not a key.
Login,
SignOut,
}
/// What the user does once the variable is unset.
impl fmt::Display for CredentialWrite {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::SignIn => "sign in with a saved key",
Self::Login => "sign in with a login",
Self::SignOut => "sign out",
})
}
}
/// Loads `repository`'s config, publishes its custom providers, as a
/// session open does, and makes the store it selects the one
/// [`credential_store::AppCredentialStore`] uses (with no store chosen, the OS store).
/// Custom providers' auth profiles resolve, and their models endpoints
/// answer a key check, only once published; running sessions keep the
/// providers they were built with. Switching stores moves no session to it:
/// providers keep what they read until a write moves the generation or
/// their credentials are refused.
pub(crate) fn load_config_and_store(
repository: &ConfigRepository,
) -> Result<Config, CredentialWriteError> {
let unloaded =
|error: anyhow::Error| CredentialWriteError::Config(error.context("could not load config"));
let config = repository.load().map_err(unloaded)?;
config.providers.activate().map_err(unloaded)?;
let path = bootstrap::absolute_config_path(repository).map_err(unloaded)?;
credential_store::activate_from_config(&config, &path)
.context("could not open the credential store")
.map_err(CredentialWriteError::Store)?;
Ok(config)
}
/// The variable that holds auth profile `auth`'s credential in the
/// environment `environment` reads, where it wins over a saved one. A host
/// checks its own ([`refuse_environment_override`]); `rho login` checks its
/// shell's too, since every rho started there reads it.
pub(crate) fn environment_override(
auth: &str,
environment: impl Fn(&str) -> Option<String>,
) -> Option<&'static str> {
let (_, mode) = provider::resolve_auth_mode(auth)?;
mode.auth_kind
.env_var()
.filter(|_| credentials::auth_has_env_override_from(auth, environment))
}
/// Refuses auth profile `auth` when the environment of whoever
/// `credentials` belong to overrides its saved credential: this process's,
/// or a daemon client's.
pub(crate) fn refuse_environment_override(
auth: &str,
write: CredentialWrite,
credentials: &dyn CredentialStore,
) -> Result<(), CredentialWriteError> {
match environment_override(auth, |variable| credentials.environment_variable(variable)) {
Some(variable) => Err(CredentialWriteError::EnvironmentOverride {
auth: auth.into(),
variable,
write,
}),
None => Ok(()),
}
}
/// An account nothing saves, looked up to ask whether a store answers.
const REACHABILITY_ACCOUNT: &str = "rho-probe:reachable";
/// Refuses when the store `credentials` are kept in cannot be reached, as
/// the OS store of a daemon started without a session D-Bus cannot. For a
/// sign-in the user works for before anything is saved, an interactive
/// login, so it fails before the provider is asked rather than after the
/// user approved it. Looks up an account that does not exist, past the OS
/// store's cache, as [`credentials::check_credential_store`] does for the
/// daemon's status: it writes nothing, and a locked keyring asks for no
/// unlock. Only why goes in the error; choosing another store is the
/// client's advice to give. Blocks.
pub(crate) fn refuse_unreachable_store(
credentials: &dyn CredentialStore,
) -> Result<(), CredentialWriteError> {
credentials
.get_secret_fresh(REACHABILITY_ACCOUNT)
.map(drop)
.map_err(|error| {
CredentialWriteError::Store(match error {
CredentialError::StoreUnavailable(reason) => {
anyhow::anyhow!("the daemon cannot reach the credential store: {reason}")
}
error @ CredentialError::InvalidData(_) => error.into(),
})
})
}
/// Signs out of auth profile `auth`, which a client named, on the provider
/// it signs in to, as `rho/auth/logout` signs out, deleting from
/// `credentials`.
pub(crate) fn sign_out_named(
auth: &str,
repository: &ConfigRepository,
credentials: &dyn CredentialStore,
) -> Result<(), CredentialWriteError> {
load_config_and_store(repository)?;
let Some((descriptor, mode)) = provider::resolve_auth_mode(auth) else {
return Err(CredentialWriteError::UnknownAuth { auth: auth.into() });
};
if matches!(mode.auth_kind, ProviderAuthKind::None) {
return Err(CredentialWriteError::NoCredentials { auth: auth.into() });
}
refuse_environment_override(auth, CredentialWrite::SignOut, credentials)?;
write_sign_out(auth, Some(descriptor), repository, credentials)
}
/// Deletes `auth`'s stored credentials, and forgets `descriptor`'s login
/// when it is logged in through its endpoint (Ollama), whose login counts
/// with or without a key. Success always means the profile needs a sign-in:
/// callers refused an environment override.
fn write_sign_out(
auth: &str,
descriptor: Option<&'static ProviderDescriptor>,
repository: &ConfigRepository,
credentials: &dyn CredentialStore,
) -> Result<(), CredentialWriteError> {
ProviderAuthentication::delete_credentials(credentials, auth)
.with_context(|| format!("could not delete {auth} credentials"))
.map_err(CredentialWriteError::Store)?;
if let Some(descriptor) = descriptor {
endpoint_login::forget(repository, *descriptor)
.with_context(|| format!("could not remove the {} login", descriptor.name))
.map_err(CredentialWriteError::Config)?;
}
Ok(())
}
/// How a saved key was checked.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum KeyCheck {
/// The provider's models endpoint accepted it.
Accepted,
/// Rho lists no models for the provider, so it cannot ask about a key.
NoModelListing,
/// The provider could not be asked (an outage, a gateway without
/// `/models`): why, with any copy of the key removed.
Unreachable(String),
/// The models endpoint answered 403: the key may not list models, as a
/// key restricted to running them may not, which says nothing about
/// whether it works (#1662).
Forbidden,
}
/// An auth profile an API key can sign in to: it takes a key, and no
/// environment variable overrides a saved one. Keys passed to its methods
/// are trimmed and never empty.
#[derive(Clone, Copy, Debug)]
pub(crate) struct ApiKeyAuth {
auth: &'static str,
descriptor: &'static ProviderDescriptor,
}
impl ApiKeyAuth {
/// Checks that `auth` can take a saved key. A custom provider's profile
/// resolves once [`load_config_and_store`] (or startup) published it.
/// Refused when the environment of whoever `credentials` belong to
/// already holds the profile's key.
pub(crate) fn resolve(
auth: &str,
credentials: &dyn CredentialStore,
) -> Result<Self, CredentialWriteError> {
let (descriptor, mode, _) = api_key_profile(auth)?;
// The variable wins over a saved key in that party's sessions, so
// saving one would change nothing for them.
refuse_environment_override(auth, CredentialWrite::SignIn, credentials)?;
Ok(Self {
auth: mode.id,
descriptor,
})
}
pub(crate) fn auth(&self) -> &'static str {
self.auth
}
/// Asks the models endpoint `config` resolves for the provider about
/// `key`, failing only [`CredentialWriteError::KeyRefused`]. Callers
/// save a key the provider cannot be asked about (no model listing in
/// Rho, an outage, a gateway without `/models`, a key that may not list
/// models) and say it went unchecked, so such a provider can still be
/// signed in to.
///
/// Response bodies may quote the request, so only statuses show (see
/// [`status_check`]).
pub(crate) async fn check(
&self,
key: &str,
config: &Config,
) -> Result<KeyCheck, CredentialWriteError> {
if !self.descriptor.supports_model_refresh() {
return Ok(KeyCheck::NoModelListing);
}
let base = config.resolved_provider_endpoint(self.descriptor.name);
let endpoint = base.as_ref().map_or(
ProviderModelEndpoint::ProviderOwned,
ProviderModelEndpoint::OpenAiCompatible,
);
let Err(error) = check_api_key(self.auth, key, endpoint).await else {
return Ok(KeyCheck::Accepted);
};
let provider = self.descriptor.display_name;
let status = match &error {
ModelError::MissingCredentials(message) => {
return Err(CredentialWriteError::KeyRefused {
provider,
reason: (*message).to_owned(),
});
}
ModelError::HttpStatus { status, .. } => *status,
ModelError::Request(error) => match error.status() {
Some(status) => status,
// A transport error's text may quote the URL, where a key
// can sit percent-encoded beyond redaction's reach, so it
// never shows (#1662).
None if error.is_connect() || error.is_timeout() => {
return Ok(KeyCheck::Unreachable("could not reach the provider".into()));
}
None => {
return Ok(KeyCheck::Unreachable(
"the request to the provider failed".into(),
));
}
},
ModelError::Credentials(_)
| ModelError::Io(_)
| ModelError::Interrupted
| ModelError::StreamIdleTimeout { .. }
| ModelError::StreamFailedAfterOutput { .. }
| ModelError::InvalidResponse(_)
| ModelError::RetryableInvalidResponse { .. }
| ModelError::ProviderReported { .. }
| ModelError::UnsupportedReasoning { .. }
| ModelError::UnsupportedProvider(_) => {
return Ok(KeyCheck::Unreachable(redacted(error.to_string(), key)));
}
};
status_check(status, provider)
}
/// Saves `key` to `store`.
pub(crate) fn save(
&self,
key: &str,
store: &dyn CredentialStore,
) -> Result<(), CredentialWriteError> {
ProviderAuthentication::save_api_key(store, self.auth, key).map_err(|error| {
CredentialWriteError::Store(anyhow::anyhow!(
"could not save the key: {}",
redacted(error.to_string(), key)
))
})
}
/// Records the login of a host logged in through its endpoint (Ollama),
/// which counts only once recorded; other providers are left alone.
pub(crate) fn record_login(
&self,
repository: &ConfigRepository,
) -> Result<(), CredentialWriteError> {
endpoint_login::record(repository, *self.descriptor)
.context("saved the key, but could not record the login")
.map_err(CredentialWriteError::Config)
}
}
/// Auth profile `auth`, refused unless it takes an API key, with the
/// store account its key is saved under.
fn api_key_profile(
auth: &str,
) -> Result<
(
&'static ProviderDescriptor,
provider::AuthMode,
&'static str,
),
CredentialWriteError,
> {
let Some((descriptor, mode)) = provider::resolve_auth_mode(auth) else {
return Err(CredentialWriteError::UnknownAuth { auth: auth.into() });
};
let ProviderAuthKind::ApiKey { account, .. } = mode.auth_kind else {
return Err(CredentialWriteError::NotAnApiKey { auth: auth.into() });
};
Ok((descriptor, mode, account))
}
/// The store account auth profile `auth`'s API key is saved under,
/// refused unless it takes one.
pub(crate) fn api_key_account(auth: &str) -> Result<&'static str, CredentialWriteError> {
api_key_profile(auth).map(|(_, _, account)| account)
}
/// The key saved in `store` for auth profile `auth`, which must take an
/// API key. A blank value counts as none, as it does for sessions and
/// `rho/auth/list`. A key in an environment variable is not saved, so it
/// is not read. Used by `rho/sync/*`, which copies saved keys.
pub(crate) fn saved_api_key(
auth: &str,
store: &dyn CredentialStore,
) -> Result<Option<String>, CredentialWriteError> {
let account = api_key_account(auth)?;
// Fresh: the OS store caches reads, misses included, for the life of
// the process, and a key copied elsewhere must be the one saved now.
let saved = store.get_secret_fresh(account).map_err(|error| {
CredentialWriteError::Store(anyhow::anyhow!(
"could not read the credential store: {error}"
))
})?;
Ok(saved.filter(|key| !key.trim().is_empty()))
}
/// What a failing `status` from `provider`'s models endpoint says about the
/// key it was asked with. A plain `GET /models` that carries only the key
/// fails 400 or 401 only for the key itself; Google answers a malformed key
/// with 400. A 403 says the key may not list models, not that it cannot run
/// them, so the key is saved unchecked (#1662).
fn status_check(
status: StatusCode,
provider: &'static str,
) -> Result<KeyCheck, CredentialWriteError> {
match status.as_u16() {
400 | 401 => Err(CredentialWriteError::KeyRefused {
provider,
reason: format!("its models endpoint answered HTTP {status}"),
}),
403 => Ok(KeyCheck::Forbidden),
_ => Ok(KeyCheck::Unreachable(format!("HTTP {status}"))),
}
}
/// `message` with any copy of `key` removed, in case an error quoted it.
/// `message` with `key` blanked out, for an error that may quote it.
pub(crate) fn redacted(message: String, key: &str) -> String {
message.replace(key, "[key]")
}
#[cfg(test)]
#[path = "credential_writes_tests.rs"]
mod tests;