2024-07-15 14:34:51 +00:00
|
|
|
use std::env;
|
2024-07-12 18:33:09 +00:00
|
|
|
use std::path::PathBuf;
|
2023-05-06 19:01:56 +00:00
|
|
|
use std::process::Command as ChildCommand;
|
2024-07-15 14:34:51 +00:00
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::process::CommandExt;
|
2023-09-23 18:10:07 +00:00
|
|
|
#[cfg(windows)]
|
2023-09-19 03:13:29 +00:00
|
|
|
use std::time::Duration;
|
2023-05-06 19:01:56 +00:00
|
|
|
|
2024-07-15 14:34:51 +00:00
|
|
|
use anyhow::{bail, Context};
|
2023-05-06 19:01:56 +00:00
|
|
|
use clap::{
|
|
|
|
Command,
|
2023-09-21 17:44:35 +00:00
|
|
|
Arg,
|
|
|
|
ArgMatches,
|
|
|
|
ArgAction,
|
|
|
|
builder::PossibleValuesParser,
|
2024-07-12 18:33:09 +00:00
|
|
|
value_parser,
|
2023-09-19 03:13:29 +00:00
|
|
|
};
|
2024-07-15 14:34:51 +00:00
|
|
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
2023-09-19 03:13:29 +00:00
|
|
|
|
2024-07-15 14:34:51 +00:00
|
|
|
use crate::proto::{
|
|
|
|
CliCredential,
|
|
|
|
CliRequest,
|
|
|
|
CliResponse,
|
|
|
|
ServerError,
|
|
|
|
ShortcutAction,
|
2023-09-19 03:13:29 +00:00
|
|
|
};
|
2023-05-06 19:01:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
pub fn parser() -> Command<'static> {
|
|
|
|
Command::new("creddy")
|
2023-09-14 22:22:38 +00:00
|
|
|
.version(env!("CARGO_PKG_VERSION"))
|
2024-07-15 14:34:51 +00:00
|
|
|
.about("A friendly credential manager")
|
2024-07-12 18:33:09 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("server_addr")
|
|
|
|
.short('a')
|
|
|
|
.long("server-addr")
|
|
|
|
.takes_value(true)
|
|
|
|
.value_parser(value_parser!(PathBuf))
|
|
|
|
.help("Connect to the main Creddy process at this address")
|
|
|
|
)
|
2023-05-06 19:01:56 +00:00
|
|
|
.subcommand(
|
|
|
|
Command::new("run")
|
|
|
|
.about("Launch Creddy")
|
|
|
|
)
|
|
|
|
.subcommand(
|
2023-09-19 03:13:29 +00:00
|
|
|
Command::new("get")
|
|
|
|
.about("Request AWS credentials from Creddy and output to stdout")
|
2023-05-06 19:01:56 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("base")
|
|
|
|
.short('b')
|
|
|
|
.long("base")
|
|
|
|
.action(ArgAction::SetTrue)
|
|
|
|
.help("Use base credentials instead of session credentials")
|
|
|
|
)
|
2024-07-03 18:54:10 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("name")
|
|
|
|
.help("If unspecified, use default credentials")
|
|
|
|
)
|
2023-05-06 19:01:56 +00:00
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
Command::new("exec")
|
|
|
|
.about("Inject AWS credentials into the environment of another command")
|
|
|
|
.trailing_var_arg(true)
|
|
|
|
.arg(
|
|
|
|
Arg::new("base")
|
|
|
|
.short('b')
|
|
|
|
.long("base")
|
|
|
|
.action(ArgAction::SetTrue)
|
|
|
|
.help("Use base credentials instead of session credentials")
|
|
|
|
)
|
2024-07-03 18:54:10 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("name")
|
|
|
|
.short('n')
|
|
|
|
.long("name")
|
2024-07-12 18:33:09 +00:00
|
|
|
.takes_value(true)
|
2024-07-03 18:54:10 +00:00
|
|
|
.help("If unspecified, use default credentials")
|
|
|
|
)
|
2023-05-06 19:01:56 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("command")
|
|
|
|
.multiple_values(true)
|
|
|
|
)
|
|
|
|
)
|
2023-09-21 17:44:35 +00:00
|
|
|
.subcommand(
|
|
|
|
Command::new("shortcut")
|
|
|
|
.about("Invoke an action normally trigged by hotkey (e.g. launch terminal)")
|
|
|
|
.arg(
|
|
|
|
Arg::new("action")
|
|
|
|
.value_parser(
|
|
|
|
PossibleValuesParser::new(["show_window", "launch_terminal"])
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2023-05-06 19:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-15 14:34:51 +00:00
|
|
|
pub fn get(args: &ArgMatches, global_args: &ArgMatches) -> anyhow::Result<()> {
|
2024-07-03 18:54:10 +00:00
|
|
|
let name = args.get_one("name").cloned();
|
|
|
|
let base = *args.get_one("base").unwrap_or(&false);
|
2024-07-12 18:33:09 +00:00
|
|
|
let addr = global_args.get_one("server_addr").cloned();
|
|
|
|
|
2024-07-15 14:34:51 +00:00
|
|
|
let output = match make_request(addr, &CliRequest::GetCredential { name, base })?? {
|
|
|
|
CliResponse::Credential(c) => serde_json::to_string_pretty(&c).unwrap(),
|
|
|
|
r => bail!("Unexpected response from server: {r}"),
|
2023-09-19 03:13:29 +00:00
|
|
|
};
|
|
|
|
println!("{output}");
|
2023-05-06 19:01:56 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-15 14:34:51 +00:00
|
|
|
pub fn exec(args: &ArgMatches, global_args: &ArgMatches) -> anyhow::Result<()> {
|
2024-07-03 18:54:10 +00:00
|
|
|
let name = args.get_one("name").cloned();
|
2023-05-06 19:01:56 +00:00
|
|
|
let base = *args.get_one("base").unwrap_or(&false);
|
2024-07-12 18:33:09 +00:00
|
|
|
let addr = global_args.get_one("server_addr").cloned();
|
2024-07-15 14:34:51 +00:00
|
|
|
// Clap guarantees that cmd_line will be a sequence of at least 1 item
|
|
|
|
// test this!
|
|
|
|
let mut cmd_line = args.get_many("command").unwrap();
|
|
|
|
let cmd_name: &String = cmd_line.next().unwrap();
|
2023-05-06 19:01:56 +00:00
|
|
|
let mut cmd = ChildCommand::new(cmd_name);
|
|
|
|
cmd.args(cmd_line);
|
2024-07-12 18:33:09 +00:00
|
|
|
|
2024-07-15 14:34:51 +00:00
|
|
|
match make_request(addr, &CliRequest::GetCredential { name, base })?? {
|
|
|
|
CliResponse::Credential(CliCredential::AwsBase(creds)) => {
|
2023-09-19 03:13:29 +00:00
|
|
|
cmd.env("AWS_ACCESS_KEY_ID", creds.access_key_id);
|
|
|
|
cmd.env("AWS_SECRET_ACCESS_KEY", creds.secret_access_key);
|
|
|
|
},
|
2024-07-15 14:34:51 +00:00
|
|
|
CliResponse::Credential(CliCredential::AwsSession(creds)) => {
|
2023-09-19 03:13:29 +00:00
|
|
|
cmd.env("AWS_ACCESS_KEY_ID", creds.access_key_id);
|
|
|
|
cmd.env("AWS_SECRET_ACCESS_KEY", creds.secret_access_key);
|
|
|
|
cmd.env("AWS_SESSION_TOKEN", creds.session_token);
|
2024-06-19 09:10:55 +00:00
|
|
|
},
|
2024-07-15 14:34:51 +00:00
|
|
|
r => bail!("Unexpected response from server: {r}"),
|
2023-05-06 19:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
2023-05-09 16:40:49 +00:00
|
|
|
{
|
2023-08-04 04:57:55 +00:00
|
|
|
// cmd.exec() never returns if successful
|
|
|
|
let e = cmd.exec();
|
2024-07-15 14:34:51 +00:00
|
|
|
Err(e).with_context(|| {
|
|
|
|
// eventually figure out how to display the actual command
|
|
|
|
format!("Failed to execute command")
|
|
|
|
})?;
|
|
|
|
Ok(())
|
2023-05-09 16:40:49 +00:00
|
|
|
}
|
2023-05-06 19:01:56 +00:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
2023-08-04 04:57:55 +00:00
|
|
|
let mut child = match cmd.spawn() {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
|
|
|
|
let name: OsString = cmd_name.into();
|
|
|
|
return Err(ExecError::NotFound(name).into());
|
|
|
|
}
|
|
|
|
Err(e) => return Err(ExecError::ExecutionFailed(e).into()),
|
|
|
|
};
|
|
|
|
|
2023-05-06 19:01:56 +00:00
|
|
|
let status = child.wait()
|
|
|
|
.map_err(|e| ExecError::ExecutionFailed(e))?;
|
|
|
|
std::process::exit(status.code().unwrap_or(1));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-15 14:34:51 +00:00
|
|
|
pub fn invoke_shortcut(args: &ArgMatches, global_args: &ArgMatches) -> anyhow::Result<()> {
|
2024-07-12 18:33:09 +00:00
|
|
|
let addr = global_args.get_one("server_addr").cloned();
|
2023-09-21 17:44:35 +00:00
|
|
|
let action = match args.get_one::<String>("action").map(|s| s.as_str()) {
|
|
|
|
Some("show_window") => ShortcutAction::ShowWindow,
|
|
|
|
Some("launch_terminal") => ShortcutAction::LaunchTerminal,
|
|
|
|
Some(&_) | None => unreachable!("Unknown shortcut action"), // guaranteed by clap
|
|
|
|
};
|
|
|
|
|
2024-07-15 14:34:51 +00:00
|
|
|
let req = CliRequest::InvokeShortcut(action);
|
|
|
|
match make_request(addr, &req)?? {
|
|
|
|
CliResponse::Empty => Ok(()),
|
|
|
|
r => bail!("Unexpected response from server: {r}"),
|
2023-09-21 17:44:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-15 14:34:51 +00:00
|
|
|
// Explanation for double-result: the server will return a (serialized) Result
|
|
|
|
// to indicate when the operation succeeded or failed, which we deserialize.
|
|
|
|
// However, the operation may fail to even communicate with the server, in
|
|
|
|
// which case we return the outer Result
|
2023-09-21 17:44:35 +00:00
|
|
|
#[tokio::main]
|
2024-07-15 14:34:51 +00:00
|
|
|
async fn make_request(
|
|
|
|
addr: Option<PathBuf>,
|
|
|
|
req: &CliRequest
|
|
|
|
) -> anyhow::Result<Result<CliResponse, ServerError>> {
|
2023-09-21 17:44:35 +00:00
|
|
|
let mut data = serde_json::to_string(req).unwrap();
|
2023-09-19 03:13:29 +00:00
|
|
|
// server expects newline marking end of request
|
|
|
|
data.push('\n');
|
2023-05-06 19:01:56 +00:00
|
|
|
|
2024-07-15 14:34:51 +00:00
|
|
|
let mut stream = crate::connect(addr).await?;
|
2023-09-19 03:13:29 +00:00
|
|
|
stream.write_all(&data.as_bytes()).await?;
|
2023-05-06 19:01:56 +00:00
|
|
|
|
2023-09-19 03:13:29 +00:00
|
|
|
let mut buf = Vec::with_capacity(1024);
|
2023-05-06 23:56:45 +00:00
|
|
|
stream.read_to_end(&mut buf).await?;
|
2024-07-15 14:34:51 +00:00
|
|
|
let res: Result<CliResponse, ServerError> = serde_json::from_slice(&buf)?;
|
|
|
|
Ok(res)
|
2023-05-06 19:01:56 +00:00
|
|
|
}
|