qcs-api-client-common 0.19.2

Common code for QCS API clients
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
//! Support for credentials whose access tokens are produced by an external program.

use std::{path::PathBuf, process::Stdio, time::Duration};

use serde::{Deserialize, Serialize};
use tokio::io::AsyncReadExt as _;

use crate::configuration::tokens::ExternallyManaged;

use super::secret_string::SecretAccessToken;

/// The most stdout (tokens) or stderr (error messages) retained.
const MAX_PIPE_BYTES: usize = 4 * 1024;

/// How long the command is given to produce an access token when `timeout_seconds` is unset.
const DEFAULT_TIMEOUT_SECONDS: u64 = 30;

const fn default_timeout_seconds() -> u64 {
    DEFAULT_TIMEOUT_SECONDS
}

#[allow(clippy::trivially_copy_pass_by_ref, reason = "serde needs a reference")]
fn is_default_timeout_seconds(timeout_seconds: &u64) -> bool {
    timeout_seconds == &DEFAULT_TIMEOUT_SECONDS
}

/// A credential whose access tokens come from running an external program, rather than from
/// tokens stored in `secrets.toml`.
///
/// This is how you integrate QCS clients with a credential helper in e.g. hosted environments.
/// Valid tokens are cached in-process so the subcommand is only called when a new token is needed.
///
/// With this method, tokens are never written back to `secrets.toml`.
///
/// # Schema
///
/// ```toml
/// [credentials.coder.externally_managed]
/// # Required.
/// command = "/usr/bin/coder"
/// # Optional. Passed to the program verbatim; no shell is involved, so no quoting or
/// # escaping is applied or required.
/// args = ["external-auth", "access-token", "qcs"]
/// # Optional, defaults to 30. How long the program may run before it is killed.
/// timeout_seconds = 30
/// ```
///
/// All [`super::settings::Profile`]s still reference an `auth_server`, although this credential
/// method does not use the auth server when fetching credential.
///
/// # Security
///
/// This turns `secrets.toml` into a file that causes code to run: anyone who can write to it, or
/// to the program it names, can run arbitrary code as you. The program runs with this process's
/// environment, so it sees the same `PATH` and variables the client does.
///
/// The usual precautions apply if that matters for your environment: keep `secrets.toml` writable
/// only by your own user, and give `command` an absolute path so it can't be resolved through an
/// attacker-controlled `PATH`.
///
/// The subcommand's stderr is included in error messages, so avoid emitting sensitive values there.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ExternallyManagedCredential {
    /// Path to the program that produces an access token on stdout.
    pub command: PathBuf,
    /// Arguments passed to the program verbatim, without shell interpretation.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub args: Vec<String>,
    /// How long the program may run before it is killed. Defaults to 30 seconds.
    #[serde(
        default = "default_timeout_seconds",
        skip_serializing_if = "is_default_timeout_seconds"
    )]
    pub timeout_seconds: u64,
}

impl From<ExternallyManagedCredential> for ExternallyManaged {
    fn from(credential: ExternallyManagedCredential) -> Self {
        Self::from_async(move |_auth_server| {
            let credential = credential.clone();
            async move {
                credential
                    .request_access_token()
                    .await
                    .map(|token| token.secret().to_string())
                    .map_err(Into::into)
            }
        })
    }
}

impl ExternallyManagedCredential {
    /// Run the program and return the access token it prints to stdout.
    ///
    /// # Errors
    ///
    /// See [`ExternalCommandError`].
    pub async fn request_access_token(&self) -> Result<SecretAccessToken, ExternalCommandError> {
        // Boxed because `run`'s state holds a `Command`, which is large enough that leaving it
        // inline bloats the future of every caller that awaits a token.
        let output = Box::pin(self.run()).await?;

        let token = String::from_utf8(output)
            .map_err(|_| ExternalCommandError::InvalidUtf8 {
                program: self.command.clone(),
            })?
            .trim()
            .to_string();

        if token.is_empty() {
            return Err(ExternalCommandError::EmptyOutput {
                program: self.command.clone(),
            });
        }

        Ok(SecretAccessToken::from(token))
    }

    /// Spawn the program and collect its (capped) stdout, enforcing [`Self::timeout_seconds`].
    async fn run(&self) -> Result<Vec<u8>, ExternalCommandError> {
        let mut command = tokio::process::Command::new(&self.command);

        command
            .args(&self.args)
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .kill_on_drop(true);

        let mut child = command
            .spawn()
            .map_err(|source| ExternalCommandError::Spawn {
                program: self.command.clone(),
                source,
            })?;

        let stdout = child.stdout.take().expect("stdout is piped");
        let stderr = child.stderr.take().expect("stderr is piped");

        let timeout = Duration::from_secs(self.timeout_seconds);
        let (status, stdout_buf, stderr_buf, stdout_truncated) =
            tokio::time::timeout(timeout, async {
                let (stdout_result, stderr_result) = futures::future::join(
                    read_capped(stdout, MAX_PIPE_BYTES),
                    read_capped(stderr, MAX_PIPE_BYTES),
                )
                .await;

                let (stdout_buf, stdout_truncated) =
                    stdout_result.map_err(|source| ExternalCommandError::Read {
                        program: self.command.clone(),
                        source,
                    })?;

                // Don't fail just because we couldn't capture the error message
                let (stderr_buf, _) = stderr_result.unwrap_or_default();

                let status = child
                    .wait()
                    .await
                    .map_err(|source| ExternalCommandError::Read {
                        program: self.command.clone(),
                        source,
                    })?;

                Ok::<_, ExternalCommandError>((status, stdout_buf, stderr_buf, stdout_truncated))
            })
            .await
            .map_err(|_| ExternalCommandError::Timeout {
                program: self.command.clone(),
                timeout,
            })??;

        if !status.success() {
            return Err(ExternalCommandError::ExitStatus {
                program: self.command.clone(),
                status: status.to_string(),
                stderr: String::from_utf8_lossy(&stderr_buf).trim().to_string(),
            });
        }

        if stdout_truncated {
            return Err(ExternalCommandError::OutputTooLarge {
                program: self.command.clone(),
                limit: MAX_PIPE_BYTES,
            });
        }

        Ok(stdout_buf)
    }
}

/// Returns at most `cap` bytes and whether anything was discarded.
async fn read_capped(
    mut reader: impl tokio::io::AsyncRead + Unpin,
    cap: usize,
) -> std::io::Result<(Vec<u8>, bool)> {
    let mut retained = Vec::new();

    // Heap-allocated rather than a stack array: this buffer lives across an await point, so an
    // inline one would be carried in the future of everything that requests a token.
    let mut chunk = vec![0_u8; 1024];
    let mut truncated = false;

    loop {
        let read = reader.read(&mut chunk).await?;
        if read == 0 {
            return Ok((retained, truncated));
        }

        let room = cap - retained.len();
        if read > room {
            truncated = true;
        }

        retained.extend_from_slice(&chunk[..read.min(room)]);
    }
}

/// Errors that can occur while getting an access token from an [`ExternallyManagedCredential`].
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ExternalCommandError {
    /// The program could not be started.
    #[error("failed to run {program:?}: {source}")]
    Spawn {
        /// The program that could not be started.
        program: PathBuf,
        /// The underlying error.
        source: std::io::Error,
    },
    /// The program's output could not be read.
    #[error("failed to read the output of {program:?}: {source}")]
    Read {
        /// The program whose output could not be read.
        program: PathBuf,
        /// The underlying error.
        source: std::io::Error,
    },
    /// The program did not finish within its timeout and was killed.
    #[error("{program:?} did not produce an access token within {timeout:?}")]
    Timeout {
        /// The program that timed out.
        program: PathBuf,
        /// The timeout that elapsed.
        timeout: Duration,
    },
    /// The program exited unsuccessfully.
    #[error("{program:?} failed with {status}: {stderr}")]
    ExitStatus {
        /// The program that failed.
        program: PathBuf,
        /// The exit status it reported.
        status: String,
        /// What it wrote to stderr, truncated.
        stderr: String,
    },
    /// The program wrote more to stdout than an access token could plausibly need.
    #[error("{program:?} wrote more than {limit} bytes to stdout")]
    OutputTooLarge {
        /// The program that wrote too much.
        program: PathBuf,
        /// The limit it exceeded.
        limit: usize,
    },
    /// The program's output was not valid UTF-8.
    #[error("{program:?} did not write a valid UTF-8 access token to stdout")]
    InvalidUtf8 {
        /// The program with invalid output.
        program: PathBuf,
    },
    /// The program wrote nothing to stdout.
    #[error("{program:?} did not write an access token to stdout")]
    EmptyOutput {
        /// The program that wrote nothing.
        program: PathBuf,
    },
}

#[cfg(test)]
pub(super) use tests::shell;

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use super::{DEFAULT_TIMEOUT_SECONDS, ExternalCommandError, ExternallyManagedCredential};

    /// An [`ExternallyManagedCredential`] that runs `command` with no arguments and default
    /// settings.
    fn credential(command: impl Into<PathBuf>) -> ExternallyManagedCredential {
        ExternallyManagedCredential {
            command: command.into(),
            args: Vec::new(),
            timeout_seconds: DEFAULT_TIMEOUT_SECONDS,
        }
    }

    /// The absolute path of a program that can run a one-line script, plus the flag that
    /// introduces it. Tests avoid depending on any tool that isn't part of a base install.
    pub(in super::super) fn shell() -> (PathBuf, &'static str) {
        #[cfg(windows)]
        {
            let comspec = std::env::var_os("COMSPEC")
                .unwrap_or_else(|| r"C:\Windows\System32\cmd.exe".into());
            (PathBuf::from(comspec), "/C")
        }
        #[cfg(not(windows))]
        {
            (PathBuf::from("/bin/sh"), "-c")
        }
    }

    /// A credential whose program runs `script` through the platform shell.
    fn script_credential(script: &str) -> ExternallyManagedCredential {
        let (program, flag) = shell();
        ExternallyManagedCredential {
            args: vec![flag.to_string(), script.to_string()],
            ..credential(program)
        }
    }

    /// A credential that prints `token` and exits successfully.
    fn echo_credential(token: &str) -> ExternallyManagedCredential {
        #[cfg(windows)]
        let script = format!("echo {token}");
        #[cfg(not(windows))]
        let script = format!("printf '%s\\n' '{token}'");
        script_credential(&script)
    }

    #[tokio::test]
    async fn returns_trimmed_stdout_as_the_access_token() {
        let token = echo_credential("an-access-token")
            .request_access_token()
            .await
            .expect("the command should produce a token");

        assert_eq!(token.secret(), "an-access-token");
    }

    #[tokio::test]
    async fn reports_stderr_when_the_command_fails() {
        #[cfg(windows)]
        let script = "echo something went wrong 1>&2 && exit 3";
        #[cfg(not(windows))]
        let script = "echo 'something went wrong' >&2; exit 3";

        let error = script_credential(script)
            .request_access_token()
            .await
            .expect_err("a failing command should be an error");

        let message = error.to_string();
        assert!(
            message.contains("something went wrong"),
            "stderr should be reported: {message}"
        );
    }

    #[tokio::test]
    async fn times_out_a_command_that_hangs() {
        #[cfg(windows)]
        let script = "ping -n 30 127.0.0.1 > nul";
        #[cfg(not(windows))]
        let script = "sleep 30";

        let error = ExternallyManagedCredential {
            timeout_seconds: 1,
            ..script_credential(script)
        }
        .request_access_token()
        .await
        .expect_err("a hanging command should time out");

        assert!(
            matches!(error, ExternalCommandError::Timeout { .. }),
            "unexpected error: {error}"
        );
    }

    #[tokio::test]
    async fn rejects_empty_output() {
        let error = script_credential("exit 0")
            .request_access_token()
            .await
            .expect_err("a command that prints nothing should be an error");

        assert!(
            matches!(error, ExternalCommandError::EmptyOutput { .. }),
            "unexpected error: {error}"
        );
    }

    #[tokio::test]
    async fn rejects_output_that_is_too_large() {
        #[cfg(windows)]
        let script = "for /L %i in (1,1,20000) do @echo aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        #[cfg(not(windows))]
        let script = "yes aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | head -c 200000";

        let error = script_credential(script)
            .request_access_token()
            .await
            .expect_err("an oversized output should be an error");

        assert!(
            matches!(error, ExternalCommandError::OutputTooLarge { .. }),
            "unexpected error: {error}"
        );
    }
}