aws_config/
sensitive_command.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6use std::fmt;
7
8#[derive(Clone)]
9pub(crate) struct CommandWithSensitiveArgs<T>(T);
10
11impl<T> CommandWithSensitiveArgs<T>
12where
13    T: AsRef<str>,
14{
15    pub(crate) fn new(value: T) -> Self {
16        Self(value)
17    }
18
19    #[allow(dead_code)]
20    pub(crate) fn to_owned_string(&self) -> CommandWithSensitiveArgs<String> {
21        CommandWithSensitiveArgs(self.0.as_ref().to_string())
22    }
23
24    #[allow(dead_code)]
25    pub(crate) fn unredacted(&self) -> &str {
26        self.0.as_ref()
27    }
28}
29
30impl<T> fmt::Display for CommandWithSensitiveArgs<T>
31where
32    T: AsRef<str>,
33{
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        // Security: The arguments for command must be redacted since they can be sensitive
36        let command = self.0.as_ref();
37        match command.find(char::is_whitespace) {
38            Some(index) => write!(f, "{} ** arguments redacted **", &command[0..index]),
39            None => write!(f, "{}", command),
40        }
41    }
42}
43
44impl<T> fmt::Debug for CommandWithSensitiveArgs<T>
45where
46    T: AsRef<str>,
47{
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        write!(f, "{:?}", format!("{}", self))
50    }
51}