account key activity

This commit is contained in:
robert dennis
2026-10-12 05:18:56 -04:00
parent 7fcaeae4d3
commit 26463b47e4
10 changed files with 30 additions and 23 deletions
+9 -1
View File
@@ -9,6 +9,7 @@ use Illuminate\Support\Facades\Validator;
use Pterodactyl\Contracts\Api\CreatesAccountApiKeys;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Extensions\Laravel\Sanctum\NewAccessToken;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Models\User;
use Pterodactyl\Validation\Concerns\ValidatesAllowedIps;
@@ -60,11 +61,18 @@ final readonly class CreateAccountApiKey implements CreatesAccountApiKeys
{
$attributes = self::attributes(['description' => $description, 'allowed_ips' => $allowedIps]);
return DB::transaction(function () use ($user, $attributes): NewAccessToken {
$token = DB::transaction(function () use ($user, $attributes): NewAccessToken {
$user = User::query()->lockForUpdate()->findOrFail($user->id);
throw_if($user->apiKeys()->count() >= 25, DisplayException::class, 'You have reached the account limit for number of API keys.');
return $user->createToken($attributes['description'], $attributes['allowed_ips']);
});
Activity::event('user:api-key.create')
->subject($user, $token->accessToken)
->property('identifier', $token->accessToken->identifier)
->log();
return $token;
}
}
+7
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pterodactyl\Actions\Api;
use Pterodactyl\Contracts\Api\DeletesApiKeys;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Models\ApiKey;
final readonly class DeleteApiKey implements DeletesApiKeys
@@ -12,5 +13,11 @@ final readonly class DeleteApiKey implements DeletesApiKeys
public function delete(ApiKey $key): void
{
$key->delete();
// Application keys are panel resources, logged by the admin API; an account key
// belongs on its owner's feed.
if ($key->key_type === ApiKey::TYPE_ACCOUNT) {
Activity::event('user:api-key.delete')->subject($key->user)->property('identifier', $key->identifier)->log();
}
}
}
+6 -1
View File
@@ -7,6 +7,7 @@ namespace Pterodactyl\Actions\Users;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Pterodactyl\Contracts\Users\CreatesSshKeys;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Models\User;
use Pterodactyl\Models\UserSSHKey;
@@ -92,6 +93,10 @@ final readonly class CreateSshKey implements CreatesSshKeys
/** @throws ValidationException */
public function create(User $user, string $name, string $publicKey, string $fingerprint): UserSSHKey
{
return $user->sshKeys()->create(self::attributes(['name' => $name, 'public_key' => $publicKey, 'fingerprint' => $fingerprint]));
$key = $user->sshKeys()->create(self::attributes(['name' => $name, 'public_key' => $publicKey, 'fingerprint' => $fingerprint]));
Activity::event('user:ssh-key.create')->subject($user, $key)->property('fingerprint', $key->fingerprint)->log();
return $key;
}
}
+3
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pterodactyl\Actions\Users;
use Pterodactyl\Contracts\Users\DeletesSshKeys;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Models\UserSSHKey;
final readonly class DeleteSshKey implements DeletesSshKeys
@@ -12,5 +13,7 @@ final readonly class DeleteSshKey implements DeletesSshKeys
public function delete(UserSSHKey $key): void
{
$key->delete();
Activity::event('user:ssh-key.delete')->subject($key->user, $key)->property('fingerprint', $key->fingerprint)->log();
}
}
@@ -13,7 +13,6 @@ use Pterodactyl\Contracts\Api\CreatesAccountApiKeys;
use Pterodactyl\Contracts\Api\DeletesApiKeys;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromTransformer;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Facades\Fractal;
use Pterodactyl\Http\Requests\Api\Client\Account\StoreApiKeyRequest;
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
@@ -63,11 +62,6 @@ class ApiKeyController extends ClientApiController
{
$token = $creation->create($request->user(), $request->safe()->string('description')->toString(), $request->allowedIps());
Activity::event('user:api-key.create')
->subject($token->accessToken)
->property('identifier', $token->accessToken->identifier)
->log();
return Fractal::item($token->accessToken)
->transformWith($this->getTransformer(ApiKeyTransformer::class))
->addMeta(['secret_token' => $token->plainTextToken])
@@ -88,10 +82,6 @@ class ApiKeyController extends ClientApiController
$keys->delete($key);
Activity::event('user:api-key.delete')
->property('identifier', $key->identifier)
->log();
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
}
}
@@ -12,7 +12,6 @@ use Knuckles\Scribe\Attributes\Subgroup;
use Pterodactyl\Contracts\Users\CreatesSshKeys;
use Pterodactyl\Contracts\Users\DeletesSshKeys;
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromTransformer;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Facades\Fractal;
use Pterodactyl\Http\Requests\Api\Client\Account\DeleteSSHKeyRequest;
use Pterodactyl\Http\Requests\Api\Client\Account\StoreSSHKeyRequest;
@@ -50,11 +49,6 @@ class SSHKeyController extends ClientApiController
{
$model = $keys->create($request->user(), $request->string('name')->toString(), $request->getPublicKey(), $request->getKeyFingerprint());
Activity::event('user:ssh-key.create')
->subject($model)
->property('fingerprint', $request->getKeyFingerprint())
->log();
return Fractal::item($model)
->transformWith($this->getTransformer(UserSSHKeyTransformer::class))
->toResponseArray();
@@ -73,11 +67,6 @@ class SSHKeyController extends ClientApiController
if (($key) !== null) {
$keys->delete($key);
Activity::event('user:ssh-key.delete')
->subject($key)
->property('fingerprint', $key->fingerprint)
->log();
}
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
@@ -43,6 +43,7 @@ test('a key keeps its description and allowed ips, and one given none can be use
expect($restricted->memo)->toBe('Restricted')
->and($restricted->key_type)->toBe(ApiKey::TYPE_ACCOUNT)
->and($restricted->allowed_ips)->toBe(['127.0.0.1', '192.168.10.10/28', '::1']);
$this->assertActivityOn('user:api-key.create', $user, $restricted);
expect($keys->create($user, 'Open', null)->accessToken->refresh()->allowed_ips)->toBe([])
->and($keys->create($user, 'Empty', [])->accessToken->refresh()->allowed_ips)->toBe([]);
@@ -43,4 +43,5 @@ test('a key is stored with the name, key and fingerprint it is given', function
->and($created->name)->toBe('Laptop')
->and($created->public_key)->toBe($key->public_key)
->and($created->fingerprint)->toBe($key->fingerprint);
$this->assertActivityOn('user:ssh-key.create', $user, $created);
});
@@ -84,6 +84,8 @@ test('delete api key', function (): void {
$response = $this->delete(route('api.admin.api-keys.delete', ['identifier' => $key->identifier]));
$response->assertStatus(Response::HTTP_NO_CONTENT);
$this->assertDatabaseMissing('api_keys', ['id' => $key->id]);
// An application key is not the admin's account key, so only the admin log records it.
expect($this->getAdminUser()->activity()->where('event', 'user:api-key.delete')->exists())->toBeFalse();
});
test('delete missing api key', function (): void {
$response = $this->delete(route('api.admin.api-keys.delete', ['identifier' => 'nil']));
@@ -40,6 +40,7 @@ test('ssh key can be deleted', function (): void {
$this->postJson($endpoint, ['fingerprint' => $key->fingerprint])->assertNoContent();
$this->assertSoftDeleted($key);
$this->assertNotSoftDeleted($key2);
$this->assertActivityFor('user:ssh-key.delete', $user, $user, $key);
$this->postJson($endpoint, ['fingerprint' => $key->fingerprint])->assertNoContent();
$this->postJson($endpoint, ['fingerprint' => $key2->fingerprint])->assertNoContent();
$this->assertNotSoftDeleted($key2);