runx-cli 0.6.19

Cargo-installed launcher for the runx governed agent workflow CLI.
Documentation
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
427
428
// rust-style-allow: large-file - public API login keeps parse, HTTP exchange,
// encrypted-token storage, and focused tests together while the public auth API
// is still small.
use std::collections::BTreeMap;
use std::ffi::OsString;
use std::fmt;
use std::path::Path;
use std::process::ExitCode;
use std::thread;
use std::time::{Duration, Instant};

use runx_runtime::registry::{
    HttpMethod, HttpRequest, RuntimeHttpError, RuntimeHttpHeader, Transport,
};
use runx_runtime::{
    ConfigError, ConfigKey, load_runx_config_file, resolve_runx_home_dir, update_runx_config_value,
    write_runx_config_file,
};
use serde::Deserialize;

use crate::cli_args::{flag_value, os_arg, split_flag};

const DEFAULT_LOGIN_TIMEOUT_SECONDS: u64 = 180;

#[derive(Debug, Eq, PartialEq)]
pub struct LoginPlan {
    pub api_base_url: Option<String>,
    pub provider: Option<String>,
    pub purpose: Option<String>,
    pub allow_local_api: bool,
    pub json: bool,
}

#[derive(Debug)]
pub enum LoginCliError {
    UnknownFlag(String),
    TransportInit(RuntimeHttpError),
    Http(LoginHttpError),
    MissingSigninUrl,
    LoginTimedOut,
    MissingToken,
    Config(ConfigError),
    Serialize(serde_json::Error),
}

impl fmt::Display for LoginCliError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnknownFlag(flag) => write!(formatter, "unknown login flag {flag}"),
            Self::TransportInit(error) => {
                write!(formatter, "failed to initialize HTTP transport: {error}")
            }
            Self::Http(error) => write!(formatter, "{error}"),
            Self::MissingSigninUrl => write!(
                formatter,
                "public API login response did not include a browser sign-in URL"
            ),
            Self::LoginTimedOut => {
                write!(formatter, "public API login timed out before completion")
            }
            Self::MissingToken => {
                write!(formatter, "public API login completed without an API token")
            }
            Self::Config(error) => write!(formatter, "{error}"),
            Self::Serialize(error) => {
                write!(formatter, "failed to serialize login result: {error}")
            }
        }
    }
}

impl std::error::Error for LoginCliError {}

impl From<ConfigError> for LoginCliError {
    fn from(error: ConfigError) -> Self {
        Self::Config(error)
    }
}

impl From<LoginHttpError> for LoginCliError {
    fn from(error: LoginHttpError) -> Self {
        Self::Http(error)
    }
}

impl From<serde_json::Error> for LoginCliError {
    fn from(error: serde_json::Error) -> Self {
        Self::Serialize(error)
    }
}

#[derive(Debug)]
pub enum LoginHttpError {
    RuntimeHttp(RuntimeHttpError),
    HttpStatus { status: u16, body: String },
    InvalidJson(String),
    RunxApi { code: String, detail: String },
}

impl fmt::Display for LoginHttpError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::RuntimeHttp(error) => write!(formatter, "{error}"),
            Self::HttpStatus { status, body } => {
                write!(formatter, "runx-api login returned HTTP {status}: {body}")
            }
            Self::InvalidJson(message) => {
                write!(formatter, "runx-api login returned invalid JSON: {message}")
            }
            Self::RunxApi { code, detail } => {
                write!(
                    formatter,
                    "runx-api login returned error [{code}]: {detail}"
                )
            }
        }
    }
}

impl std::error::Error for LoginHttpError {}

impl From<RuntimeHttpError> for LoginHttpError {
    fn from(error: RuntimeHttpError) -> Self {
        Self::RuntimeHttp(error)
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
struct LoginStartResponse {
    status: String,
    session_id: String,
    login_token: String,
    #[serde(default)]
    authorization_url: Option<String>,
    #[serde(default)]
    poll_after_ms: Option<u64>,
}

#[derive(Clone, Debug, serde::Serialize, PartialEq, Eq)]
struct LoginStartRequest<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    provider: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    purpose: Option<&'a str>,
}

#[derive(Clone, Debug, serde::Serialize, PartialEq, Eq)]
struct LoginCompleteRequest<'a> {
    login_token: &'a str,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
struct LoginCompleteResponse {
    status: String,
    session_id: String,
    #[serde(default)]
    principal_id: Option<String>,
    #[serde(default)]
    credential_id: Option<String>,
    #[serde(default)]
    token: Option<String>,
    #[serde(default)]
    poll_after_ms: Option<u64>,
}

#[derive(Clone, Debug, serde::Serialize, PartialEq, Eq)]
struct LoginResult {
    status: &'static str,
    principal_id: String,
    credential_id: String,
}

pub fn parse_login_plan(args: &[OsString]) -> Result<LoginPlan, String> {
    let mut api_base_url = None;
    let mut provider = None;
    let mut purpose = None;
    let mut allow_local_api = false;
    let mut json = false;
    let mut index = 1;
    while index < args.len() {
        let arg = os_arg(args, index, "login")?;
        if !arg.starts_with('-') {
            return Err(format!("unexpected login argument {arg}"));
        }
        let (flag, inline_value) = split_flag(arg);
        match flag {
            "--json" | "-j" => {
                if inline_value.is_some() {
                    return Err("--json does not take a value".to_owned());
                }
                json = true;
                index += 1;
            }
            "--api-base-url" => {
                let (value, next_index) = flag_value(args, index, flag, inline_value, "login")?;
                api_base_url = Some(value);
                index = next_index;
            }
            "--provider" => {
                let (value, next_index) = flag_value(args, index, flag, inline_value, "login")?;
                provider = Some(value);
                index = next_index;
            }
            "--for" | "--purpose" => {
                let (value, next_index) = flag_value(args, index, flag, inline_value, "login")?;
                purpose = Some(value);
                index = next_index;
            }
            "--allow-local-api" => {
                if inline_value.is_some() {
                    return Err("--allow-local-api does not take a value".to_owned());
                }
                allow_local_api = true;
                index += 1;
            }
            _ => return Err(LoginCliError::UnknownFlag(flag.to_owned()).to_string()),
        }
    }
    Ok(LoginPlan {
        api_base_url,
        provider,
        purpose,
        allow_local_api,
        json,
    })
}

pub fn run_native_login(plan: LoginPlan) -> ExitCode {
    let cwd = match std::env::current_dir() {
        Ok(cwd) => cwd,
        Err(error) => {
            let _ignored = crate::cli_io::write_stderr(&format!(
                "runx login: failed to resolve cwd: {error}\n"
            ));
            return ExitCode::from(1);
        }
    };
    match run_login_command(&plan, &crate::history::env_map(), &cwd) {
        Ok(output) => crate::cli_io::write_stdout_code(&output, 0),
        Err(error) => {
            if plan.json {
                let body = serde_json::json!({
                    "status": "failure",
                    "error": {
                        "message": error.to_string(),
                        "code": "login_failed",
                    },
                });
                let serialized = serde_json::to_string_pretty(&body)
                    .unwrap_or_else(|_| "{\"status\":\"failure\"}".to_owned());
                return crate::cli_io::write_stdout_code(&format!("{serialized}\n"), 1);
            }
            let _ignored = crate::cli_io::write_stderr(&format!("runx login: {error}\n"));
            ExitCode::from(1)
        }
    }
}

pub fn run_login_command(
    plan: &LoginPlan,
    env: &BTreeMap<String, String>,
    cwd: &Path,
) -> Result<String, LoginCliError> {
    let transport = crate::public_api::transport(allow_local_api(plan, env))
        .map_err(LoginCliError::TransportInit)?;
    run_login_command_with_transport(plan, env, cwd, &transport, thread::sleep)
}

fn run_login_command_with_transport<T: Transport>(
    plan: &LoginPlan,
    env: &BTreeMap<String, String>,
    cwd: &Path,
    transport: &T,
    sleep: impl Fn(Duration),
) -> Result<String, LoginCliError> {
    let base_url = resolve_public_api_base_url(plan, env);
    let started = start_login_session(
        transport,
        &base_url,
        plan.provider.as_deref(),
        plan.purpose.as_deref(),
    )?;
    let signin_url = started
        .authorization_url
        .as_deref()
        .filter(|value| !value.trim().is_empty())
        .ok_or(LoginCliError::MissingSigninUrl)?;
    if !plan.json {
        let _ignored = crate::cli_io::write_stderr(&format!(
            "Open this URL to sign in to runx:\n{signin_url}\n\nWaiting for public API login...\n"
        ));
    }

    let deadline = Instant::now() + Duration::from_secs(DEFAULT_LOGIN_TIMEOUT_SECONDS);
    let mut poll_after = Duration::from_millis(started.poll_after_ms.unwrap_or(1000));
    loop {
        let completed = complete_login_session(
            transport,
            &base_url,
            &started.session_id,
            &started.login_token,
        )?;
        if completed.status == "success" {
            let token = completed
                .token
                .as_deref()
                .filter(|value| !value.trim().is_empty())
                .ok_or(LoginCliError::MissingToken)?;
            store_public_api_token(env, cwd, token)?;
            let result = LoginResult {
                status: "success",
                principal_id: completed.principal_id.unwrap_or_default(),
                credential_id: completed.credential_id.unwrap_or_default(),
            };
            return render_login_result(plan.json, &result);
        }
        if Instant::now() >= deadline {
            return Err(LoginCliError::LoginTimedOut);
        }
        if let Some(next_poll_after) = completed.poll_after_ms {
            poll_after = Duration::from_millis(next_poll_after);
        }
        sleep(poll_after);
    }
}

fn store_public_api_token(
    env: &BTreeMap<String, String>,
    cwd: &Path,
    token: &str,
) -> Result<(), LoginCliError> {
    let config_dir = resolve_runx_home_dir(env, cwd);
    let config_path = config_dir.join("config.json");
    let config = load_runx_config_file(&config_path)?;
    let next = update_runx_config_value(config, ConfigKey::PublicApiToken, token, &config_dir)?;
    write_runx_config_file(&config_path, &next)?;
    Ok(())
}

fn allow_local_api(plan: &LoginPlan, env: &BTreeMap<String, String>) -> bool {
    crate::public_api::private_network_allowed(
        plan.allow_local_api,
        env,
        "RUNX_LOGIN_ALLOW_LOCAL_API",
    )
}

fn resolve_public_api_base_url(plan: &LoginPlan, env: &BTreeMap<String, String>) -> String {
    crate::public_api::resolve_base_url(plan.api_base_url.as_deref(), env)
}

fn start_login_session<T: Transport>(
    transport: &T,
    base_url: &str,
    provider: Option<&str>,
    purpose: Option<&str>,
) -> Result<LoginStartResponse, LoginHttpError> {
    let request = LoginStartRequest {
        provider: provider.map(str::trim).filter(|value| !value.is_empty()),
        purpose: purpose.map(str::trim).filter(|value| !value.is_empty()),
    };
    let response = transport.send(HttpRequest {
        method: HttpMethod::Post,
        url: format!("{}/v1/login/sessions", base_url.trim_end_matches('/')),
        headers: vec![RuntimeHttpHeader::new("content-type", "application/json")],
        body: Some(
            serde_json::to_string(&request)
                .map_err(|error| LoginHttpError::InvalidJson(error.to_string()))?,
        ),
    })?;
    json_response(response.status, &response.body)
}

fn complete_login_session<T: Transport>(
    transport: &T,
    base_url: &str,
    session_id: &str,
    login_token: &str,
) -> Result<LoginCompleteResponse, LoginHttpError> {
    let body = serde_json::to_string(&LoginCompleteRequest { login_token })
        .map_err(|error| LoginHttpError::InvalidJson(error.to_string()))?;
    let response = transport.send(HttpRequest {
        method: HttpMethod::Post,
        url: format!(
            "{}/v1/login/sessions/{}/complete",
            base_url.trim_end_matches('/'),
            session_id
        ),
        headers: vec![RuntimeHttpHeader::new("content-type", "application/json")],
        body: Some(body),
    })?;
    json_response(response.status, &response.body)
}

fn json_response<T: for<'de> Deserialize<'de>>(
    status: u16,
    body: &str,
) -> Result<T, LoginHttpError> {
    if !(200..=299).contains(&status) {
        if let Some(error) = crate::public_api::parse_error(body) {
            return Err(LoginHttpError::RunxApi {
                code: error.code,
                detail: error.detail,
            });
        }
        return Err(LoginHttpError::HttpStatus {
            status,
            body: body.to_owned(),
        });
    }
    serde_json::from_str(body).map_err(|error| LoginHttpError::InvalidJson(error.to_string()))
}

fn render_login_result(json: bool, result: &LoginResult) -> Result<String, LoginCliError> {
    if json {
        return serde_json::to_string_pretty(result)
            .map(|value| format!("{value}\n"))
            .map_err(LoginCliError::Serialize);
    }
    Ok(format!(
        "\n  ✓  login  success\n  principal     {}\n  credential    {}\n\n",
        result.principal_id, result.credential_id
    ))
}

#[cfg(test)]
#[path = "login_tests.rs"]
mod login_tests;