Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ voltage profiles get staging
voltage profiles delete staging
```

`voltage context` shows the effective profile, account, organization, environments, wallet, API URL, and configuration directory, and where each came from (`flag`, `profile`, `environment`, `default`, or `unset`). It reports why commands would reject the credential selection as `credential_problem` (`empty_api_key`, `no_credential`, `multiple_credentials`, `unknown_credential`, or `bound_elsewhere` for an API key used outside its organization or environment), using the same selection commands use. It also lists `VOLTAGE_*` variables that are set but ignored because a flag or the selected profile takes precedence. It reads no saved secret, makes no network request, and creates no files:

```sh
voltage context --profile staging
voltage context --json | jq -r '.data.organization_id.value'
```

Configuration lives in `$XDG_CONFIG_HOME/voltage` or `~/.config/voltage`; override the directory with `VOLTAGE_CONFIG_DIR` or `--config-dir`. The directory and everything in it are owner-only.

Only endpoints that filter by environment receive `--env`. Organization-wide commands say so in their help, and wallet mutations verify a supplied environment against the wallet before changing anything.
Expand Down
1 change: 1 addition & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ These commands use the auth service (`https://auth.voltage.cloud/api/v1`, overri
| `voltage auth import-key` | local only |
| `voltage organizations list` | GET `/api/v1/users/current` |
| `voltage environments list` | POST `/api/v1/oauth/token`<br>GET `/api/v1/organizations/{organization_id}/environments` |
| `voltage context` | local only |
| `voltage profiles create NAME` | local only |
| `voltage profiles get NAME` | local only |
| `voltage profiles list` | local only |
Expand Down
1 change: 1 addition & 0 deletions scripts/check-coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
('voltage auth import-key', []),
('voltage organizations list', [('GET', '/api/v1/users/current')]),
('voltage environments list', [('POST', '/api/v1/oauth/token'), ('GET', '/api/v1/organizations/{organization_id}/environments')]),
('voltage context', []),
('voltage profiles create NAME', []),
('voltage profiles get NAME', []),
('voltage profiles list', []),
Expand Down
90 changes: 65 additions & 25 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::{
api::{SessionChange, SubmissionState},
cli::{GlobalFlags, ImportKeyFlags, LoginFlags},
config::{
AUTH_URL, Account, AccountKind, Credential, InputSource, LOCK_TIMEOUT, Login, Scope,
Settings, read_secret,
AUTH_URL, Account, AccountKind, Credential, CredentialProblem, InputSource, LOCK_TIMEOUT,
Login, Scope, Settings, read_secret,
},
secret::Secret,
terminal::Terminal,
Expand Down Expand Up @@ -333,6 +333,63 @@ fn ambient_api_key(flags: &GlobalFlags) -> Option<Secret> {
std::env::var("VOLTAGE_API_KEY").ok().map(Secret::new)
}

/// `VOLTAGE_API_KEY` is set, but a profile or account selection takes precedence over it.
pub(crate) fn ignores_ambient_api_key(flags: &GlobalFlags) -> bool {
std::env::var_os("VOLTAGE_API_KEY").is_some() && ambient_api_key(flags).is_none()
}

/// The credential a command would use, chosen without reading a saved secret.
pub(crate) enum Selected<'a> {
/// `VOLTAGE_API_KEY`.
Ambient(Secret),
Saved {
name: String,
account: &'a Account,
},
}

/// A selection commands would reject, with the credential it named when there is one.
pub(crate) struct Rejected<'a> {
pub(crate) problem: CredentialProblem,
pub(crate) selected: Option<Selected<'a>>,
}

/// Choose the credential every command uses, and why commands would reject it. `resolve`
/// and `voltage context` share this, so inspection cannot drift from what commands do.
pub(crate) fn select<'a>(
settings: &'a Settings,
scope: &Scope,
flags: &GlobalFlags,
) -> std::result::Result<Selected<'a>, Rejected<'a>> {
if let Some(key) = ambient_api_key(flags) {
// A blank key still shadows saved credentials, so commands fail rather than fall back.
if key.expose().trim().is_empty() {
return Err(Rejected {
problem: CredentialProblem::EmptyApiKey,
selected: Some(Selected::Ambient(key)),
});
}
return Ok(Selected::Ambient(key));
}
let rejected = |problem| Rejected {
problem,
selected: None,
};
let name = settings.select_account(scope).map_err(rejected)?;
let account = settings
.config
.accounts
.get(&name)
.ok_or(rejected(CredentialProblem::UnknownCredential))?;
if account.bound_elsewhere(scope) {
return Err(Rejected {
problem: CredentialProblem::BoundElsewhere,
selected: Some(Selected::Saved { name, account }),
});
}
Ok(Selected::Saved { name, account })
}

pub fn status(settings: &Settings, scope: &Scope, flags: &GlobalFlags) -> Result<AuthStatus> {
if let Some(key) = ambient_api_key(flags) {
return Ok(AuthStatus::Ambient {
Expand Down Expand Up @@ -553,30 +610,13 @@ pub async fn resolve(
flags: &GlobalFlags,
submission: &SubmissionState,
) -> Result<Credential> {
if let Some(key) = ambient_api_key(flags) {
if key.expose().trim().is_empty() {
return Err(Error::auth("VOLTAGE_API_KEY is empty"));
}
return Ok(Credential::ApiKey(key));
}
let name = settings.account_name(scope)?;
let account = settings.account(&name)?;
let (name, account) =
match select(settings, scope, flags).map_err(|rejected| rejected.problem.error(scope))? {
Selected::Ambient(key) => return Ok(Credential::ApiKey(key)),
Selected::Saved { name, account } => (name, account),
};
match account.kind {
AccountKind::ApiKey => {
let organization_mismatch = scope
.org
.zip(account.organization_id)
.is_some_and(|(selected, bound)| selected != bound);
let environment_mismatch = account
.environment_id
.is_some_and(|bound| scope.envs.iter().any(|env| *env != bound));
if organization_mismatch || environment_mismatch {
return Err(Error::usage(
"API key is bound to a different organization or environment; select a matching credential",
));
}
settings.read_credential(&name)
}
AccountKind::ApiKey => settings.read_credential(&name),
AccountKind::User => {
let url = base_url(flags.auth_url.as_deref().unwrap_or(&account.auth_url))?;
if url != account.auth_url {
Expand Down
8 changes: 8 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ pub enum LocalCommand {
#[command(subcommand)]
command: ProfileCommand,
},
/// Show the effective profile, scope, credential, and config path without secrets
Context,
/// Show the BTC/USD price and the sats one dollar buys
Price(PriceFlags),
/// Convert an amount between BTC and USD at the current or a past price
Expand Down Expand Up @@ -1268,6 +1270,12 @@ mod tests {
assert_eq!(flags.to, Currency::Btc);
assert_eq!(flags.at.as_deref(), Some("2026-09-18T17:30:00Z"));
assert!(Invocation::try_parse_from(["voltage", "convert", "10", "usd"]).is_err());
assert!(matches!(
Invocation::try_parse_from(["voltage", "context"])
.unwrap()
.command,
Command::Local(LocalCommand::Context)
));
assert!(matches!(
Invocation::try_parse_from(["voltage", "price"])
.unwrap()
Expand Down
Loading
Loading