Feat: Add a fingerprint to every private key on save, create...

This commit is contained in:
peaklabs-dev
2024-09-16 12:54:48 +02:00
parent 7d39a5089c
commit f9b7841572

View File

@@ -26,6 +26,7 @@ class PrivateKey extends BaseModel
'name', 'name',
'description', 'description',
'private_key', 'private_key',
'fingerprint',
'is_git_related', 'is_git_related',
'team_id', 'team_id',
]; ];
@@ -35,10 +36,10 @@ class PrivateKey extends BaseModel
static::saving(function ($key) { static::saving(function ($key) {
$privateKey = data_get($key, 'private_key'); $privateKey = data_get($key, 'private_key');
if (substr($privateKey, -1) !== "\n") { 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 = ['*']) public static function ownedByCurrentTeam(array $select = ['*'])
@@ -85,4 +86,14 @@ class PrivateKey extends BaseModel
{ {
return $this->hasMany(GitlabApp::class); 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
}
}
} }