use std::{
io::{self, IsTerminal, Write},
sync::{
mpsc::{self, RecvTimeoutError},
Arc, Mutex,
},
thread,
time::Duration,
};
use rho_providers::credentials::{CredentialResult, CredentialStore, CredentialStoreBackend};
const NOTICE_DELAY: Duration = Duration::from_millis(250);
static NOTICE: Mutex<NoticeState> = Mutex::new(NoticeState::Disabled);
enum NoticeState {
Disabled,
Pending,
Shown,
}
impl NoticeState {
fn show(&mut self, output: &mut impl Write) {
if matches!(self, Self::Pending) {
let _ = writeln!(
output,
"accessing keyring; check your desktop for an unlock prompt"
);
let _ = output.flush();
*self = Self::Shown;
}
}
}
pub(crate) struct StartupKeyringNotice;
impl StartupKeyringNotice {
pub(crate) fn begin(interactive: bool) -> Self {
let mut state = lock(&NOTICE);
*state = if interactive && crate::stdio::stdin_is_terminal() && io::stderr().is_terminal() {
NoticeState::Pending
} else {
NoticeState::Disabled
};
Self
}
}
impl Drop for StartupKeyringNotice {
fn drop(&mut self) {
*lock(&NOTICE) = NoticeState::Disabled;
}
}
fn lock(notice: &Mutex<NoticeState>) -> std::sync::MutexGuard<'_, NoticeState> {
notice.lock().unwrap_or_else(|error| error.into_inner())
}
fn access_with_notice<T>(
notice: &'static Mutex<NoticeState>,
delay: Duration,
mut output: impl Write + Send + 'static,
access: impl FnOnce() -> T,
) -> T {
if !matches!(*lock(notice), NoticeState::Pending) {
return access();
}
let (finished, wait_for_finish) = mpsc::channel::<()>();
let watcher = thread::Builder::new()
.name("keyring-notice".into())
.spawn(move || {
if wait_for_finish.recv_timeout(delay) == Err(RecvTimeoutError::Timeout) {
lock(notice).show(&mut output);
}
});
let result = access();
drop(finished);
if let Ok(watcher) = watcher {
let _ = watcher.join();
}
result
}
pub(crate) fn notifying_store(
inner: Arc<dyn CredentialStore>,
backend: CredentialStoreBackend,
) -> Arc<dyn CredentialStore> {
Arc::new(NotifyingCredentialStore { inner, backend })
}
struct NotifyingCredentialStore {
inner: Arc<dyn CredentialStore>,
backend: CredentialStoreBackend,
}
impl NotifyingCredentialStore {
fn notify_if_slow<T>(&self, access: impl FnOnce() -> T) -> T {
match self.backend {
CredentialStoreBackend::Os => {
access_with_notice(&NOTICE, NOTICE_DELAY, io::stderr(), access)
}
CredentialStoreBackend::File => access(),
}
}
}
impl CredentialStore for NotifyingCredentialStore {
fn get_secret(&self, account: &str) -> CredentialResult<Option<String>> {
self.notify_if_slow(|| self.inner.get_secret(account))
}
fn get_secret_fresh(&self, account: &str) -> CredentialResult<Option<String>> {
self.notify_if_slow(|| self.inner.get_secret_fresh(account))
}
fn set_secret(&self, account: &str, secret: &str) -> CredentialResult<()> {
self.notify_if_slow(|| self.inner.set_secret(account, secret))
}
fn delete_secret(&self, account: &str) -> CredentialResult<bool> {
self.notify_if_slow(|| self.inner.delete_secret(account))
}
fn environment_variable(&self, name: &str) -> Option<String> {
self.inner.environment_variable(name)
}
}
#[cfg(test)]
#[path = "startup_notice_tests.rs"]
mod tests;