Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugeny committed Dec 22, 2024
1 parent 02b4982 commit 65aa01f
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 58 deletions.
2 changes: 1 addition & 1 deletion warpgate-admin/src/api/public_key_credentials.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use chrono::{DateTime, Utc};
use poem::web::Data;
use poem_openapi::param::Path;
use poem_openapi::payload::Json;
Expand All @@ -12,7 +13,6 @@ use tokio::sync::Mutex;
use uuid::Uuid;
use warpgate_common::{UserPublicKeyCredential, WarpgateError};
use warpgate_db_entities::PublicKeyCredential;
use chrono::{DateTime, Utc};

use super::AnySecurityScheme;

Expand Down
31 changes: 18 additions & 13 deletions warpgate-core/src/config_providers/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use async_trait::async_trait;
use chrono::Utc;
use data_encoding::BASE64;
use sea_orm::{
ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, ModelTrait, QueryFilter,
Expand All @@ -20,7 +21,6 @@ use warpgate_common::{
UserSsoCredential, UserTotpCredential, WarpgateError,
};
use warpgate_db_entities as entities;
use chrono::{Utc};

use super::ConfigProvider;

Expand Down Expand Up @@ -402,33 +402,38 @@ impl ConfigProvider for DatabaseConfigProvider {
let base64_bytes = data_encoding::BASE64.encode(&public_key_bytes);
let openssh_public_key = format!("{kind} {base64_bytes}");

debug!("Attempting to update last_used for public key: {}", openssh_public_key);
debug!(
"Attempting to update last_used for public key: {}",
openssh_public_key
);

// Find the public key credential
let public_key_credential = entities::PublicKeyCredential::Entity::find()
.filter(entities::PublicKeyCredential::Column::OpensshPublicKey.eq(openssh_public_key.clone()))
.filter(
entities::PublicKeyCredential::Column::OpensshPublicKey
.eq(openssh_public_key.clone()),
)
.one(&*db)
.await?;

let Some(public_key_credential) = public_key_credential else {
warn!("Public key not found in the database: {}", openssh_public_key);
warn!(
"Public key not found in the database: {}",
openssh_public_key
);
return Ok(()); // Gracefully return if the key is not found
};

// Update the `last_used` (last used) timestamp
let mut active_model: entities::PublicKeyCredential::ActiveModel =
let mut active_model: entities::PublicKeyCredential::ActiveModel =
public_key_credential.into();
active_model.last_used = Set(Some(Utc::now()));

active_model
.update(&*db)
.await
.map_err(|e| {
error!("Failed to update last_used for public key: {:?}", e);
WarpgateError::DatabaseError(e.into())
})?;
active_model.update(&*db).await.map_err(|e| {
error!("Failed to update last_used for public key: {:?}", e);
WarpgateError::DatabaseError(e.into())
})?;

info!("Successfully updated last_used for public key: {}", openssh_public_key);
Ok(())
}
}
2 changes: 1 addition & 1 deletion warpgate-db-entities/src/PublicKeyCredential.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use chrono::{DateTime, Utc};
use sea_orm::entity::prelude::*;
use sea_orm::sea_query::ForeignKeyAction;
use sea_orm::Set;
use serde::Serialize;
use uuid::Uuid;
use warpgate_common::{UserAuthCredential, UserPublicKeyCredential};
use chrono::{DateTime, Utc};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize)]
#[sea_orm(table_name = "credentials_public_key")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ impl MigrationTrait for Migration {
ColumnDef::new(Alias::new("label"))
.string()
.not_null()
.default("Public Key")
.default("Public Key"),
)
.to_owned()
.to_owned(),
)
.await
}
Expand All @@ -38,5 +38,4 @@ impl MigrationTrait for Migration {
)
.await
}

}
12 changes: 2 additions & 10 deletions warpgate-db-migrations/src/m00013_add_openssh_public_key_dates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ impl MigrationTrait for Migration {
.alter_table(
Table::alter()
.table(public_key_credential::Entity)
.add_column(
ColumnDef::new(Alias::new("date_added"))
.date_time()
.null(),
)
.add_column(ColumnDef::new(Alias::new("date_added")).date_time().null())
.to_owned(),
)
.await?;
Expand All @@ -32,11 +28,7 @@ impl MigrationTrait for Migration {
.alter_table(
Table::alter()
.table(public_key_credential::Entity)
.add_column(
ColumnDef::new(Alias::new("last_used"))
.date_time()
.null(),
)
.add_column(ColumnDef::new(Alias::new("last_used")).date_time().null())
.to_owned(),
)
.await?;
Expand Down
7 changes: 4 additions & 3 deletions warpgate-protocol-http/src/api/credentials.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chrono::{DateTime, Utc};
use http::StatusCode;
use poem::web::Data;
use poem::{Endpoint, EndpointExt, FromRequest, IntoResponse};
Expand All @@ -11,7 +12,7 @@ use uuid::Uuid;
use warpgate_common::{User, UserPasswordCredential, UserRequireCredentialsPolicy, WarpgateError};
use warpgate_core::Services;
use warpgate_db_entities::{self as entities, Parameters, PasswordCredential, PublicKeyCredential};
use chrono::{DateTime, Utc};

use crate::common::{endpoint_auth, RequestAuthorization};

pub struct Api;
Expand Down Expand Up @@ -93,8 +94,8 @@ fn abbreviate_public_key(k: &str) -> String {

format!(
"{}...{}",
&k[..l.min(k.len())], // Take the first `l` characters.
&k[k.len().saturating_sub(l)..] // Take the last `l` characters safely.
&k[..l.min(k.len())], // Take the first `l` characters.
&k[k.len().saturating_sub(l)..] // Take the last `l` characters safely.
)
}

Expand Down
9 changes: 2 additions & 7 deletions warpgate-protocol-ssh/src/server/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1227,12 +1227,7 @@ impl ServerSession {
public_key_bytes: Bytes::from(key.public_key_bytes()),
});

let result = self
.try_auth_lazy(
&selector,
key.clone(),
)
.await;
let result = self.try_auth_lazy(&selector, key.clone()).await;

match result {
Ok(AuthResult::Accepted { .. }) => {
Expand All @@ -1248,7 +1243,7 @@ impl ServerSession {
warn!(?err, "Failed to update last_used for public key");
}
russh::server::Auth::Accept
},
}
Ok(AuthResult::Rejected) => russh::server::Auth::Reject {
proceed_with_methods: Some(MethodSet::all()),
},
Expand Down
2 changes: 1 addition & 1 deletion warpgate-web/src/admin/CredentialEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@

{#if credential.kind === CredentialKind.PublicKey || credential.kind === CredentialKind.Sso}
<a
class="ms-2 hover-reveal"
class="ms-2"
href={''}
onclick={e => {
if (credential.kind === CredentialKind.Sso) {
Expand Down
13 changes: 6 additions & 7 deletions warpgate-web/src/gateway/CredentialManager.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import { faIdBadge, faKey, faKeyboard, faMobilePhone } from '@fortawesome/free-solid-svg-icons'
import Fa from 'svelte-fa'
import PublicKeyCredentialModal from 'admin/PublicKeyCredentialModal.svelte'
import { Button } from '@sveltestrap/sveltestrap'
import CreatePasswordModal from 'admin/CreatePasswordModal.svelte'
import CreateOtpModal from 'admin/CreateOtpModal.svelte'
import CredentialUsedStateBadge from 'common/CredentialUsedStateBadge.svelte'
Expand Down Expand Up @@ -114,9 +113,9 @@
<div class="d-flex align-items-center mt-4 mb-2">
<h4 class="m-0">One-time passwords</h4>
<span class="ms-auto"></span>
<Button size="sm" color="link" on:click={() => {
<a href={''} color="link" onclick={() => {
creatingOtpCredential = true
}}>Add device</Button>
}}>Add device</a>
</div>

<div class="list-group list-group-flush mb-3">
Expand All @@ -126,7 +125,7 @@
<span class="label">OTP device</span>
<span class="ms-auto"></span>
<a
class="hover-reveal ms-2"
class="ms-2"
href={''}
onclick={e => {
deleteOtp(credential)
Expand All @@ -148,9 +147,9 @@
<div class="d-flex align-items-center mt-4 mb-2">
<h4 class="m-0">Public keys</h4>
<span class="ms-auto"></span>
<Button size="sm" color="link" on:click={() => {
<a href={''} color="link" onclick={() => {
creatingPublicKeyCredential = true
}}>Add key</Button>
}}>Add key</a>
</div>

<div class="list-group list-group-flush mb-3">
Expand All @@ -164,7 +163,7 @@
<span class="ms-auto"></span>
<CredentialUsedStateBadge credential={credential} />
<a
class="hover-reveal ms-2"
class="ms-2"
href={''}
onclick={e => {
deletePublicKey(credential)
Expand Down
15 changes: 3 additions & 12 deletions warpgate/src/commands/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,7 @@ pub(crate) async fn command(cli: &crate::Cli) -> Result<()> {
.to_string_lossy()
.to_string();

store.http.key = data_path
.join("tls.key.pem")
.to_string_lossy()
.to_string();
store.http.key = data_path.join("tls.key.pem").to_string_lossy().to_string();

store.mysql.certificate = store.http.certificate.clone();
store.mysql.key = store.http.key.clone();
Expand All @@ -239,10 +236,7 @@ pub(crate) async fn command(cli: &crate::Cli) -> Result<()> {

// ---

store.ssh.keys = data_path
.join("ssh-keys")
.to_string_lossy()
.to_string();
store.ssh.keys = data_path.join("ssh-keys").to_string_lossy().to_string();

// ---

Expand All @@ -257,10 +251,7 @@ pub(crate) async fn command(cli: &crate::Cli) -> Result<()> {
.with_prompt("Do you want to record user sessions?")
.interact()?;
}
store.recordings.path = data_path
.join("recordings")
.to_string_lossy()
.to_string();
store.recordings.path = data_path.join("recordings").to_string_lossy().to_string();

// ---

Expand Down

0 comments on commit 65aa01f

Please sign in to comment.