Skip to content

Commit

Permalink
Add title when adding public keys (#1171)
Browse files Browse the repository at this point in the history
  • Loading branch information
moalshak authored Dec 18, 2024
1 parent 409b382 commit 1dec4c9
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 13 deletions.
1 change: 1 addition & 0 deletions tests/test_ssh_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def setup_user_and_target(
api.create_public_key_credential(
user.id,
sdk.NewPublicKeyCredential(
label="Public Key",
openssh_public_key=open("ssh-keys/id_ed25519.pub").read().strip(),
),
)
Expand Down
1 change: 1 addition & 0 deletions tests/test_ssh_user_auth_otp.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def test_otp(
api.create_public_key_credential(
user.id,
sdk.NewPublicKeyCredential(
label="Public Key",
openssh_public_key=open("ssh-keys/id_ed25519.pub").read().strip()
),
)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_ssh_user_auth_pubkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def test_ed25519(
api.create_public_key_credential(
user.id,
sdk.NewPublicKeyCredential(
label="Public Key",
openssh_public_key=open("ssh-keys/id_ed25519.pub").read().strip()
),
)
Expand Down Expand Up @@ -104,6 +105,7 @@ def test_rsa(
api.create_public_key_credential(
user.id,
sdk.NewPublicKeyCredential(
label="Public Key",
openssh_public_key=open("ssh-keys/id_rsa.pub").read().strip()
),
)
Expand Down
5 changes: 5 additions & 0 deletions warpgate-admin/src/api/public_key_credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ use super::AnySecurityScheme;
#[derive(Object)]
struct ExistingPublicKeyCredential {
id: Uuid,
label: String,
openssh_public_key: String,
}

#[derive(Object)]
struct NewPublicKeyCredential {
label: String,
openssh_public_key: String,
}

impl From<PublicKeyCredential::Model> for ExistingPublicKeyCredential {
fn from(credential: PublicKeyCredential::Model) -> Self {
Self {
id: credential.id,
label: credential.label,
openssh_public_key: credential.openssh_public_key,
}
}
Expand Down Expand Up @@ -112,6 +115,7 @@ impl ListApi {
let object = PublicKeyCredential::ActiveModel {
id: Set(Uuid::new_v4()),
user_id: Set(*user_id),
label: Set(body.label.clone()),
..PublicKeyCredential::ActiveModel::from(UserPublicKeyCredential::try_from(&*body)?)
}
.insert(&*db)
Expand Down Expand Up @@ -154,6 +158,7 @@ impl DetailApi {
let model = PublicKeyCredential::ActiveModel {
id: Set(id.0),
user_id: Set(*user_id),
label: Set(body.label.clone()),
..<_>::from(UserPublicKeyCredential::try_from(&*body)?)
}
.update(&*db)
Expand Down
1 change: 1 addition & 0 deletions warpgate-db-entities/src/PublicKeyCredential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub user_id: Uuid,
pub label: String,
pub openssh_public_key: String,
}

Expand Down
2 changes: 2 additions & 0 deletions warpgate-db-migrations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod m00008_users;
mod m00009_credential_models;
mod m00010_parameters;
mod m00011_rsa_key_algos;
mod m00012_add_openssh_public_key_label;

pub struct Migrator;

Expand All @@ -31,6 +32,7 @@ impl MigratorTrait for Migrator {
Box::new(m00009_credential_models::Migration),
Box::new(m00010_parameters::Migration),
Box::new(m00011_rsa_key_algos::Migration),
Box::new(m00012_add_openssh_public_key_label::Migration),
]
}
}
Expand Down
42 changes: 42 additions & 0 deletions warpgate-db-migrations/src/m00012_add_openssh_public_key_label.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use sea_orm_migration::prelude::*;

pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m00012_add_openssh_public_key_label"
}
}

use crate::m00009_credential_models::public_key_credential;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
Table::alter()
.table(public_key_credential::Entity)
.add_column(
ColumnDef::new(Alias::new("label"))
.string()
.not_null()
.default("Public Key")
)
.to_owned()
)
.await
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
Table::alter()
.table(public_key_credential::Entity)
.drop_column(Alias::new("label"))
.to_owned(),
)
.await
}

}
14 changes: 11 additions & 3 deletions warpgate-protocol-http/src/api/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,36 @@ enum CredentialsStateResponse {

#[derive(Object)]
struct NewPublicKeyCredential {
label: String,
openssh_public_key: String,
}

#[derive(Object)]
struct ExistingPublicKeyCredential {
id: Uuid,
label: String,
abbreviated: String,
}

fn abbreviate_public_key(k: &str) -> String {
let l = 10;
if k.len() <= l {
return k.to_string(); // Return the full key if it's shorter than or equal to `l`.
}

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

impl From<entities::PublicKeyCredential::Model> for ExistingPublicKeyCredential {
fn from(credential: entities::PublicKeyCredential::Model) -> Self {
Self {
id: credential.id,
label: abbreviate_public_key(&credential.openssh_public_key),
label: credential.label,
abbreviated: abbreviate_public_key(&credential.openssh_public_key),
}
}
}
Expand Down Expand Up @@ -288,6 +295,7 @@ impl Api {
let object = PublicKeyCredential::ActiveModel {
id: Set(Uuid::new_v4()),
user_id: Set(user_model.id),
label: Set(body.label.clone()),
openssh_public_key: Set(body.openssh_public_key.clone()),
}
.insert(&*db)
Expand Down
6 changes: 4 additions & 2 deletions warpgate-web/src/admin/CredentialEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@
editingSsoCredentialInstance = null
}
async function savePublicKeyCredential (opensshPublicKey: string) {
async function savePublicKeyCredential (label: string, opensshPublicKey: string) {
if (editingPublicKeyCredentialInstance) {
editingPublicKeyCredentialInstance.label = label
editingPublicKeyCredentialInstance.opensshPublicKey = opensshPublicKey
await api.updatePublicKeyCredential({
userId,
Expand All @@ -196,6 +197,7 @@
const credential = await api.createPublicKeyCredential({
userId,
newPublicKeyCredential: {
label,
opensshPublicKey,
},
})
Expand Down Expand Up @@ -250,7 +252,7 @@
{/if}
{#if credential.kind === 'PublicKey'}
<Fa fw icon={faKey} />
<span class="type">Public key</span>
<span class="type">{credential.label}</span>
<span class="text-muted ms-2">{abbreviatePublicKey(credential.opensshPublicKey)}</span>
{/if}
{#if credential.kind === 'Totp'}
Expand Down
17 changes: 13 additions & 4 deletions warpgate-web/src/admin/PublicKeyCredentialModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
interface Props {
isOpen: boolean
instance?: ExistingPublicKeyCredential
save: (opensshPublicKey: string) => void
save: (label: string, opensshPublicKey: string) => void
}
let {
Expand All @@ -25,19 +25,20 @@
}: Props = $props()
let field: HTMLInputElement|undefined = $state()
let label: string = $state('')
let opensshPublicKey: string = $state('')
let validated = $state(false)
function _save () {
if (!opensshPublicKey) {
if (!opensshPublicKey || !label) {
return
}
if (opensshPublicKey.includes(' ')) {
const parts = opensshPublicKey.split(' ').filter(x => x)
opensshPublicKey = `${parts[0]} ${parts[1]}`
}
isOpen = false
save(opensshPublicKey)
save(label, opensshPublicKey)
}
function _cancel () {
Expand All @@ -47,6 +48,7 @@

<Modal toggle={_cancel} isOpen={isOpen} on:open={() => {
if (instance) {
label = instance.label
opensshPublicKey = instance.opensshPublicKey
}
field?.focus()
Expand All @@ -56,9 +58,16 @@
e.preventDefault()
}}>
<ModalHeader toggle={_cancel}>
Public key
Add an SSH public key
</ModalHeader>
<ModalBody>
<FormGroup floating label="Label">
<Input
bind:inner={field}
type="text"
required
bind:value={label} />
</FormGroup>
<FormGroup floating label="Public key in OpenSSH format">
<Input
style="font-family: monospace; height: 15rem"
Expand Down
10 changes: 9 additions & 1 deletion warpgate-web/src/admin/lib/openapi-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"openapi": "3.0.0",
"info": {
"title": "Warpgate Web Admin",
"version": "0.11.0"
"version": "0.12.0"
},
"servers": [
{
Expand Down Expand Up @@ -2154,13 +2154,17 @@
"type": "object",
"required": [
"id",
"label",
"openssh_public_key"
],
"properties": {
"id": {
"type": "string",
"format": "uuid"
},
"label": {
"type": "string"
},
"openssh_public_key": {
"type": "string"
}
Expand Down Expand Up @@ -2272,9 +2276,13 @@
"NewPublicKeyCredential": {
"type": "object",
"required": [
"label",
"openssh_public_key"
],
"properties": {
"label": {
"type": "string"
},
"openssh_public_key": {
"type": "string"
}
Expand Down
4 changes: 3 additions & 1 deletion warpgate-web/src/gateway/CredentialManager.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
creds!.password = state
}
async function createPublicKey (opensshPublicKey: string) {
async function createPublicKey (label: string, opensshPublicKey: string) {
const credential = await api.addMyPublicKey({
newPublicKeyCredential: {
label,
opensshPublicKey,
},
})
Expand Down Expand Up @@ -156,6 +157,7 @@
<div class="list-group-item credential">
<Fa fw icon={faKey} />
<span class="label">{credential.label}</span>
<span class="text-muted ms-2">{credential.abbreviated}</span>
<span class="ms-auto"></span>
<a
class="hover-reveal ms-2"
Expand Down
12 changes: 10 additions & 2 deletions warpgate-web/src/gateway/lib/openapi-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"openapi": "3.0.0",
"info": {
"title": "Warpgate HTTP proxy",
"version": "0.11.0"
"version": "0.12.0"
},
"servers": [
{
Expand Down Expand Up @@ -688,7 +688,8 @@
"type": "object",
"required": [
"id",
"label"
"label",
"abbreviated"
],
"properties": {
"id": {
Expand All @@ -697,6 +698,9 @@
},
"label": {
"type": "string"
},
"abbreviated": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -799,9 +803,13 @@
"NewPublicKeyCredential": {
"type": "object",
"required": [
"label",
"openssh_public_key"
],
"properties": {
"label": {
"type": "string"
},
"openssh_public_key": {
"type": "string"
}
Expand Down

0 comments on commit 1dec4c9

Please sign in to comment.