finish extremely basic implementation of docker credentials

This commit is contained in:
2024-11-25 07:57:59 -05:00
parent 8bcdc5420a
commit 9bc9cb56c1
6 changed files with 64 additions and 26 deletions

View File

@ -7,7 +7,8 @@ use crate::clientinfo::{self, Client};
use crate::credentials::{
Credential,
CredentialRecord,
Crypto
Crypto,
DockerCredential
};
use crate::errors::*;
use crate::ipc::{Approval, AwsRequestNotification, RequestNotificationDetail, RequestResponse};
@ -55,13 +56,16 @@ async fn handle(
CliRequest::GetAwsCredential{ name, base } => get_aws_credentials(
name, base, client, app_handle, waiter
).await,
CliRequest::GetDockerCredential{ server_url } => get_docker_credentials (
CliRequest::GetDockerCredential{ server_url } => get_docker_credential (
server_url, client, app_handle, waiter
).await,
CliRequest::SaveCredential{ name, is_default, credential } => save_credential(
name, is_default, credential, app_handle
CliRequest::StoreDockerCredential(docker_credential) => store_docker_credential(
docker_credential, app_handle, waiter
).await,
CliRequest::InvokeShortcut(action) => invoke_shortcut(action).await,
CliRequest::EraseDockerCredential { server_url } => erase_docker_credential(
server_url, app_handle, waiter
).await,
CliRequest::InvokeShortcut{ action } => invoke_shortcut(action).await,
};
// doesn't make sense to send the error to the client if the client has already left
@ -106,7 +110,7 @@ async fn get_aws_credentials(
}
}
async fn get_docker_credentials(
async fn get_docker_credential(
server_url: String,
client: Client,
app_handle: AppHandle,
@ -126,24 +130,39 @@ async fn get_docker_credentials(
}
}
pub async fn save_credential(
name: String,
is_default: bool,
credential: Credential,
async fn store_docker_credential(
docker_credential: DockerCredential,
app_handle: AppHandle,
_waiter: CloseWaiter<'_>,
) -> Result<CliResponse, HandlerError> {
let state = app_handle.state::<AppState>();
// eventually ask the frontend to unlock here
// eventually ask the frontend to confirm here
// a bit weird but convenient
let random_bytes = Crypto::salt();
let id = Uuid::from_slice(&random_bytes[..16]).unwrap();
let record = CredentialRecord {
id, name, is_default, credential
id,
name: docker_credential.server_url.clone(),
is_default: false,
credential: Credential::Docker(docker_credential)
};
state.save_credential(record).await?;
Ok(CliResponse::Empty)
}
async fn erase_docker_credential(
server_url: String,
app_handle: AppHandle,
_waiter: CloseWaiter<'_>
) -> Result<CliResponse, HandlerError> {
let state = app_handle.state::<AppState>();
// eventually ask the frontend to confirm here
state.delete_credential_by_name(&server_url).await?;
Ok(CliResponse::Empty)
}

View File

@ -13,7 +13,6 @@ use crate::clientinfo::Client;
use crate::credentials::{
AwsBaseCredential,
AwsSessionCredential,
Credential,
DockerCredential,
};
use crate::errors::*;
@ -30,6 +29,7 @@ use platform::Stream;
// so that we avoid polluting the standalone CLI with a bunch of dependencies
// that would make it impossible to build a completely static-linked version
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum CliRequest {
GetAwsCredential {
name: Option<String>,
@ -38,12 +38,13 @@ pub enum CliRequest {
GetDockerCredential {
server_url: String,
},
SaveCredential {
name: String,
is_default: bool,
credential: Credential,
StoreDockerCredential(DockerCredential),
EraseDockerCredential {
server_url: String,
},
InvokeShortcut{
action: ShortcutAction,
},
InvokeShortcut(ShortcutAction),
}

View File

@ -161,6 +161,13 @@ impl AppState {
Ok(())
}
pub async fn delete_credential_by_name(&self, name: &str) -> Result<(), SaveCredentialsError> {
sqlx::query!("DELETE FROM credentials WHERE name = ?", name)
.execute(&self.pool)
.await?;
Ok(())
}
pub async fn list_credentials(&self) -> Result<Vec<CredentialRecord>, GetCredentialsError> {
let session = self.app_session.read().await;
let crypto = session.try_get_crypto()?;