Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugeny committed Dec 7, 2024
1 parent 809fc21 commit f98d3d9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
19 changes: 12 additions & 7 deletions warpgate-admin/src/api/public_key_credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ impl From<PublicKeyCredential::Model> for ExistingPublicKeyCredential {
}
}

impl From<&NewPublicKeyCredential> for UserPublicKeyCredential {
fn from(credential: &NewPublicKeyCredential) -> Self {
Self {
key: credential.openssh_public_key.clone().into(),
}
impl TryFrom<&NewPublicKeyCredential> for UserPublicKeyCredential {
type Error = WarpgateError;

fn try_from(credential: &NewPublicKeyCredential) -> Result<Self, WarpgateError> {
let key = russh::keys::PublicKey::from_openssh(&credential.openssh_public_key)
.map_err(russh::keys::Error::from)?;

Ok(Self {
key: key.to_openssh().map_err(russh::keys::Error::from)?.into(),
})
}
}

Expand Down Expand Up @@ -107,7 +112,7 @@ impl ListApi {
let object = PublicKeyCredential::ActiveModel {
id: Set(Uuid::new_v4()),
user_id: Set(*user_id),
..PublicKeyCredential::ActiveModel::from(UserPublicKeyCredential::from(&*body))
..PublicKeyCredential::ActiveModel::from(UserPublicKeyCredential::try_from(&*body)?)
}
.insert(&*db)
.await
Expand Down Expand Up @@ -149,7 +154,7 @@ impl DetailApi {
let model = PublicKeyCredential::ActiveModel {
id: Set(id.0),
user_id: Set(*user_id),
..<_>::from(UserPublicKeyCredential::from(&*body))
..<_>::from(UserPublicKeyCredential::try_from(&*body)?)
}
.update(&*db)
.await;
Expand Down
10 changes: 8 additions & 2 deletions warpgate-db-migrations/src/m00011_rsa_key_algos.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use sea_orm::{ActiveModelTrait, EntityTrait, IntoActiveModel, Set};
use sea_orm_migration::prelude::*;
use tracing::error;

use crate::m00009_credential_models::public_key_credential as PKC;

Expand All @@ -19,8 +20,13 @@ impl MigrationTrait for Migration {
let connection = manager.get_connection();
let creds = PKC::Entity::find().all(connection).await?;
for cred in creds.into_iter() {
let parsed = russh_keys::PublicKey::from_openssh(&cred.openssh_public_key)
.map_err(|e| DbErr::Custom(format!("Failed to parse public key: {e}")))?;
let parsed = match russh_keys::PublicKey::from_openssh(&cred.openssh_public_key) {
Ok(parsed) => parsed,
Err(e) => {
error!("Failed to parse public key '{cred:?}': {e}");
continue;
}
};
let serialized = parsed
.to_openssh()
.map_err(|e| DbErr::Custom(format!("Failed to serialize public key: {e}")))?;
Expand Down

0 comments on commit f98d3d9

Please sign in to comment.