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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ comfy-table = "7.2.2"
webbrowser = "1"
console = "0.16.2"
chrono = "0.4"
uuid = "1"
axoupdater = { version = "0.10.2", default-features = false, features = ["blocking", "github_releases"] }

[dev-dependencies]
Expand Down
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ API keys remain restricted to their bound cluster regardless of which selector s
Top-level commands:

- `login`, `logout`
- `database`, `organization`, `integration`, `service-account`, `federation-issuer`, `federation-rule`, `cluster`, `key`, `table`, `workflow`
- `database`, `organization`, `integration`, `service-account`, `federation-issuer`, `federation-rule`, `cluster`, `api-key`, `table`, `workflow`
- `query`, `insert`
- `ping`, `docs`, `status`, `open`, `completions`

Expand Down Expand Up @@ -411,10 +411,10 @@ With `--json`, the result is
### Keys and tables

```sh
rtree key list
rtree key create --name ci --permission read_write
rtree key create --name temporary-ci --permission read_only --expires-at 2027-01-01T00:00:00Z
rtree key create --database analytics --name analytics-ci --permission read_write
rtree api-key list
rtree api-key create --name ci --permission read_write
rtree api-key create --name temporary-ci --permission read_only --expires-at 2027-01-01T00:00:00Z
rtree api-key create --database analytics --name analytics-ci --permission read_write

rtree table list --database analytics
rtree table describe --database analytics events
Expand All @@ -424,16 +424,19 @@ rtree table update --database analytics events --sorting-key 'region, toStartOfH

Omit `--sorting-key` when creating a table to choose a key automatically per part. `table describe` shows the current sorting key as a string. Updating the key affects new parts and later merges; existing parts may keep their previous key until they are merged. A custom sorting key cannot be reset to automatic.

API keys belong to a cluster. `key list` and `key delete` do not accept `--database`;
listing includes each key's default database. `key create --database` selects the
Use `rtree api-key delete <ID>` to revoke a key by its UUID from `api-key list`.
Deletion requires the UUID; full API key tokens are not accepted.

API keys belong to a cluster. `api-key list` and `api-key delete` do not accept `--database`;
listing includes each key's default database. `api-key create --database` selects the
new key's default database, using `RAWTREE_DATABASE` or the saved selection when
omitted. If none is selected, the server uses `default`.

`key create --expires-at` accepts a future RFC 3339 timestamp with a timezone.
`api-key create --expires-at` accepts a future RFC 3339 timestamp with a timezone.
The server validates and normalizes it to UTC. Omit the flag for a key that never
expires. Create/list output includes expiration (`never` in text, `null` in JSON);
older servers that omit the field are also supported. Expiration is fixed at
creation; create a replacement key to change it. There is no `key update` command.
creation; create a replacement key to change it. There is no `api-key update` command.

### Workflows

Expand Down
11 changes: 5 additions & 6 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,9 @@ pub enum Command {
action: DatabaseCommand,
},
/// Manage API keys
#[command(name = "key")]
Key {
ApiKey {
#[command(subcommand)]
action: KeyCommand,
action: ApiKeyCommand,
},
/// Manage organizations
Organization {
Expand Down Expand Up @@ -444,7 +443,7 @@ pub enum ClusterCommand {
}

#[derive(Subcommand)]
pub enum KeyCommand {
pub enum ApiKeyCommand {
/// List API keys for a cluster
List,
/// Create a new API key
Expand All @@ -464,8 +463,8 @@ pub enum KeyCommand {
},
/// Delete an API key
Delete {
/// Key ID or full API key token to delete
id_or_token: String,
/// API key UUID to delete
id: uuid::Uuid,
},
}

Expand Down
48 changes: 29 additions & 19 deletions src/cli/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
Cli, ClusterCommand, ClusterSizeArg, Command, KeyCommand, TableCommand, WorkflowCommand,
ApiKeyCommand, Cli, ClusterCommand, ClusterSizeArg, Command, TableCommand, WorkflowCommand,
};
use clap::{error::ErrorKind, CommandFactory, Parser};

Expand Down Expand Up @@ -414,25 +414,25 @@ fn query_format_flag_is_rejected() {
}

#[test]
fn key_command_is_singular() {
let cli = Cli::try_parse_from(["rtree", "key", "list"]).unwrap();
fn api_key_command_is_singular() {
let cli = Cli::try_parse_from(["rtree", "api-key", "list"]).unwrap();
assert!(matches!(
cli.command,
Command::Key {
action: KeyCommand::List
Command::ApiKey {
action: ApiKeyCommand::List
}
));
}

#[test]
fn cluster_scoped_commands_reject_obsolete_database_flags() {
for args in [
vec!["rtree", "key", "list", "--database", "analytics"],
vec!["rtree", "api-key", "list", "--database", "analytics"],
vec![
"rtree",
"key",
"api-key",
"delete",
"key-id",
"11111111-1111-4111-8111-111111111111",
"--database",
"analytics",
],
Expand All @@ -450,16 +450,23 @@ fn cluster_scoped_commands_reject_obsolete_database_flags() {
}

#[test]
fn keys_command_is_rejected() {
let result = Cli::try_parse_from(["rtree", "keys", "list", "--database", "analytics"]);
assert!(result.is_err(), "keys should not be accepted as a command");
fn obsolete_key_commands_are_rejected() {
for name in ["key", "keys", "api-keys"] {
assert_eq!(
Cli::try_parse_from(["rtree", name, "list"])
.err()
.expect("obsolete command must fail")
.kind(),
ErrorKind::InvalidSubcommand,
);
}
}

#[test]
fn key_create_uses_name_flag() {
let cli = Cli::try_parse_from([
"rtree",
"key",
"api-key",
"create",
"--database",
"analytics",
Expand All @@ -468,24 +475,24 @@ fn key_create_uses_name_flag() {
"--permission",
"read_write",
])
.expect("key create with --name should parse");
.expect("api-key create with --name should parse");

match cli.command {
Command::Key { action } => match action {
KeyCommand::Create { name, .. } => {
Command::ApiKey { action } => match action {
ApiKeyCommand::Create { name, .. } => {
assert_eq!(name, "ci");
}
_ => panic!("expected key create command"),
_ => panic!("expected api-key create command"),
},
_ => panic!("expected key command"),
_ => panic!("expected api-key command"),
}
}

#[test]
fn key_create_label_flag_is_rejected() {
let result = Cli::try_parse_from([
"rtree",
"key",
"api-key",
"create",
"--database",
"analytics",
Expand All @@ -494,7 +501,10 @@ fn key_create_label_flag_is_rejected() {
"--permission",
"read_write",
]);
assert!(result.is_err(), "key create should use --name, not --label");
assert!(
result.is_err(),
"api-key create should use --name, not --label"
);
}

#[test]
Expand Down
8 changes: 4 additions & 4 deletions src/commands/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ fn api_key_context_paths(
cli_database: Option<&str>,
) -> (String, String) {
let keys_path = cli_database
.map(|database| org::database_scoped_path(database, "/keys", cli_org, cli_cluster))
.unwrap_or_else(|| org::scoped_path("/v1/keys", cli_org, cli_cluster));
.map(|database| org::database_scoped_path(database, "/api-keys", cli_org, cli_cluster))
.unwrap_or_else(|| org::scoped_path("/v1/api-keys", cli_org, cli_cluster));
let tables_path = cli_database
.map(|database| org::database_scoped_path(database, "/tables", cli_org, cli_cluster))
.unwrap_or_else(|| org::scoped_path("/v1/tables", cli_org, cli_cluster));
Expand Down Expand Up @@ -714,7 +714,7 @@ mod tests {
fn api_key_context_paths_do_not_invent_organization() {
let (keys_path, tables_path) = api_key_context_paths(None, None, Some("analytics"));

assert_eq!(keys_path, "/v1/keys?database=analytics");
assert_eq!(keys_path, "/v1/api-keys?database=analytics");
assert_eq!(tables_path, "/v1/tables?database=analytics");
}

Expand All @@ -725,7 +725,7 @@ mod tests {

assert_eq!(
keys_path,
"/v1/keys?database=analytics&organization=team%20alpha&cluster=prod%2Feu"
"/v1/api-keys?database=analytics&organization=team%20alpha&cluster=prod%2Feu"
);
assert_eq!(
tables_path,
Expand Down
15 changes: 7 additions & 8 deletions src/commands/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn list(
cluster: Option<&str>,
json_mode: bool,
) -> Result<()> {
let path = org::scoped_path("/v1/keys", organization, cluster);
let path = org::scoped_path("/v1/api-keys", organization, cluster);
let resp: ListApiKeysResponse = client.get(&path)?;
output::print_result(&resp, json_mode, |_| {
if resp.keys.is_empty() {
Expand Down Expand Up @@ -105,8 +105,8 @@ pub fn create(
json_mode: bool,
) -> Result<()> {
let path = match database {
Some(database) => org::database_scoped_path(database, "/keys", organization, cluster),
None => org::scoped_path("/v1/keys", organization, cluster),
Some(database) => org::database_scoped_path(database, "/api-keys", organization, cluster),
None => org::scoped_path("/v1/api-keys", organization, cluster),
};
let resp: CreateApiKeyResponse = client.post(&path, &serde_json::to_value(request)?)?;
output::print_result(&resp, json_mode, |_| {
Expand All @@ -127,18 +127,17 @@ pub fn delete(
client: &ApiClient,
organization: Option<&str>,
cluster: Option<&str>,
id_or_token: &str,
id: uuid::Uuid,
json_mode: bool,
) -> Result<()> {
let encoded_key = urlencoding::encode(id_or_token);
let path = org::scoped_path(&format!("/v1/keys/{encoded_key}"), organization, cluster);
let path = org::scoped_path(&format!("/v1/api-keys/{id}"), organization, cluster);
let resp: DeleteApiKeyResponse = client.delete(&path)?;
output::print_result(
&json!({"deleted": resp.deleted, "id_or_token": id_or_token}),
&json!({"deleted": resp.deleted, "id": id.to_string()}),
json_mode,
|_| {
if resp.deleted {
println!("API key '{}' deleted.", id_or_token);
println!("API key '{}' deleted.", id);
}
},
);
Expand Down
16 changes: 8 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use clap::{CommandFactory, Parser};
use clap_complete::generate;

use cli::{
Cli, ClusterCommand, Command, DatabaseCommand, IntegrationCommand, IntegrationKind, KeyCommand,
OrganizationCommand, ServiceAccountCommand, ShellType, TableCommand, WorkflowCommand,
WorkflowSinkCommand,
ApiKeyCommand, Cli, ClusterCommand, Command, DatabaseCommand, IntegrationCommand,
IntegrationKind, OrganizationCommand, ServiceAccountCommand, ShellType, TableCommand,
WorkflowCommand, WorkflowSinkCommand,
};
use client::ApiClient;
use constants::DEFAULT_API_URL;
Expand Down Expand Up @@ -412,16 +412,16 @@ fn run(cli: Cli) -> Result<ExitCode> {
}
}
}
Command::Key { action } => {
Command::ApiKey { action } => {
let effective_org = resolve_effective_org(&client, cli_org.clone());
match action {
KeyCommand::List => commands::keys::list(
ApiKeyCommand::List => commands::keys::list(
&client,
effective_org.as_deref(),
effective_cluster.as_deref(),
json,
),
KeyCommand::Create {
ApiKeyCommand::Create {
database,
name,
permission,
Expand All @@ -441,11 +441,11 @@ fn run(cli: Cli) -> Result<ExitCode> {
json,
)
}
KeyCommand::Delete { id_or_token } => commands::keys::delete(
ApiKeyCommand::Delete { id } => commands::keys::delete(
&client,
effective_org.as_deref(),
effective_cluster.as_deref(),
&id_or_token,
id,
json,
),
}
Expand Down
17 changes: 10 additions & 7 deletions tests/api_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,24 @@ fn key_list_and_delete_are_cluster_scoped_even_with_a_saved_database() {
]});
for config in [json!({}), config] {
let (result, _, _) = run(
&["key", "list"],
&["api-key", "list"],
"GET",
"/v1/keys?organization=team%20alpha&cluster=prod%2Feu",
"/v1/api-keys?organization=team%20alpha&cluster=prod%2Feu",
response.clone(),
config.clone(),
);
assert_eq!(result, response);
let (result, _, _) = run(
&["key", "delete", "key-1"],
&["api-key", "delete", "11111111-1111-4111-8111-111111111111"],
"DELETE",
"/v1/keys/key-1?organization=team%20alpha&cluster=prod%2Feu",
"/v1/api-keys/11111111-1111-4111-8111-111111111111?organization=team%20alpha&cluster=prod%2Feu",
json!({"deleted": true}),
config,
);
assert_eq!(result["deleted"], true);
assert_eq!(
result,
json!({"deleted": true, "id": "11111111-1111-4111-8111-111111111111"})
);
}
}

Expand Down Expand Up @@ -144,7 +147,7 @@ fn key_create_uses_server_default_or_an_explicit_or_saved_database() {
] {
let response = json!({"id": "key-1", "token": "rt_fixture", "name": "ci", "permission": "read_write", "expires_at": null, "database": {"name": database}});
let mut args = vec![
"key",
"api-key",
"create",
"--name",
"ci",
Expand All @@ -155,7 +158,7 @@ fn key_create_uses_server_default_or_an_explicit_or_saved_database() {
let (result, _, body) = run(
&args,
"POST",
&format!("/v1/keys?{query}"),
&format!("/v1/api-keys?{query}"),
response.clone(),
config,
);
Expand Down
Loading