From f9b7841572d5845c8d4abe3f662bb6d371476513 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Mon, 16 Sep 2024 12:54:48 +0200 Subject: [PATCH] Feat: Add a fingerprint to every private key on save, create... --- app/Models/PrivateKey.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/Models/PrivateKey.php b/app/Models/PrivateKey.php index 45bc6bc84..868210382 100644 --- a/app/Models/PrivateKey.php +++ b/app/Models/PrivateKey.php @@ -26,6 +26,7 @@ class PrivateKey extends BaseModel 'name', 'description', 'private_key', + 'fingerprint', 'is_git_related', 'team_id', ]; @@ -35,10 +36,10 @@ class PrivateKey extends BaseModel static::saving(function ($key) { $privateKey = data_get($key, 'private_key'); if (substr($privateKey, -1) !== "\n") { - $key->private_key = $privateKey."\n"; + $key->private_key = $privateKey . "\n"; } + $key->fingerprint = $key->generateFingerprint(); }); - } public static function ownedByCurrentTeam(array $select = ['*']) @@ -85,4 +86,14 @@ class PrivateKey extends BaseModel { return $this->hasMany(GitlabApp::class); } + + public function generateFingerprint() + { + try { + $key = PublicKeyLoader::load($this->private_key); + return $key->getPublicKey()->getFingerprint('sha256'); + } catch (\Throwable $e) { + return 'invalid_' . md5($this->private_key); // TODO: DO NOT ALLOW SAVING IF INVALID SSH KEYS SAY SSH KEY IS INVALID + } + } }