From 26463b47e4ca4286f45efa3eae70e9faaf231704 Mon Sep 17 00:00:00 2001 From: robert dennis <31261583+robertdrakedennis@users.noreply.github.com> Date: Sun, 11 Oct 2026 14:24:52 -0400 Subject: [PATCH] account key activity --- app/Actions/Api/CreateAccountApiKey.php | 10 +++++++++- app/Actions/Api/DeleteApiKey.php | 7 +++++++ app/Actions/Users/CreateSshKey.php | 7 ++++++- app/Actions/Users/DeleteSshKey.php | 3 +++ app/Http/Controllers/Api/Client/ApiKeyController.php | 10 ---------- app/Http/Controllers/Api/Client/SSHKeyController.php | 11 ----------- .../Actions/Api/CreateAccountApiKeyTest.php | 1 + tests/Integration/Actions/Users/CreateSshKeyTest.php | 1 + .../Api/Admin/ApiKeys/ApiKeyControllerTest.php | 2 ++ tests/Integration/Api/Client/SSHKeyControllerTest.php | 1 + 10 files changed, 30 insertions(+), 23 deletions(-) diff --git a/app/Actions/Api/CreateAccountApiKey.php b/app/Actions/Api/CreateAccountApiKey.php index 534ca26ca..63161c5f8 100644 --- a/app/Actions/Api/CreateAccountApiKey.php +++ b/app/Actions/Api/CreateAccountApiKey.php @@ -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; } } diff --git a/app/Actions/Api/DeleteApiKey.php b/app/Actions/Api/DeleteApiKey.php index 45a65b31f..1c7472c33 100644 --- a/app/Actions/Api/DeleteApiKey.php +++ b/app/Actions/Api/DeleteApiKey.php @@ -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(); + } } } diff --git a/app/Actions/Users/CreateSshKey.php b/app/Actions/Users/CreateSshKey.php index cc4d138a3..ac88a7e1a 100644 --- a/app/Actions/Users/CreateSshKey.php +++ b/app/Actions/Users/CreateSshKey.php @@ -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; } } diff --git a/app/Actions/Users/DeleteSshKey.php b/app/Actions/Users/DeleteSshKey.php index fc8e11074..f39dcb807 100644 --- a/app/Actions/Users/DeleteSshKey.php +++ b/app/Actions/Users/DeleteSshKey.php @@ -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(); } } diff --git a/app/Http/Controllers/Api/Client/ApiKeyController.php b/app/Http/Controllers/Api/Client/ApiKeyController.php index f9aa8aa27..1a7c27223 100644 --- a/app/Http/Controllers/Api/Client/ApiKeyController.php +++ b/app/Http/Controllers/Api/Client/ApiKeyController.php @@ -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); } } diff --git a/app/Http/Controllers/Api/Client/SSHKeyController.php b/app/Http/Controllers/Api/Client/SSHKeyController.php index 32644ebad..8c75e5083 100644 --- a/app/Http/Controllers/Api/Client/SSHKeyController.php +++ b/app/Http/Controllers/Api/Client/SSHKeyController.php @@ -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); diff --git a/tests/Integration/Actions/Api/CreateAccountApiKeyTest.php b/tests/Integration/Actions/Api/CreateAccountApiKeyTest.php index 97ead2598..4ba663774 100644 --- a/tests/Integration/Actions/Api/CreateAccountApiKeyTest.php +++ b/tests/Integration/Actions/Api/CreateAccountApiKeyTest.php @@ -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([]); diff --git a/tests/Integration/Actions/Users/CreateSshKeyTest.php b/tests/Integration/Actions/Users/CreateSshKeyTest.php index 0de38f240..f11aadff9 100644 --- a/tests/Integration/Actions/Users/CreateSshKeyTest.php +++ b/tests/Integration/Actions/Users/CreateSshKeyTest.php @@ -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); }); diff --git a/tests/Integration/Api/Admin/ApiKeys/ApiKeyControllerTest.php b/tests/Integration/Api/Admin/ApiKeys/ApiKeyControllerTest.php index fbaa421dc..f3317468f 100644 --- a/tests/Integration/Api/Admin/ApiKeys/ApiKeyControllerTest.php +++ b/tests/Integration/Api/Admin/ApiKeys/ApiKeyControllerTest.php @@ -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'])); diff --git a/tests/Integration/Api/Client/SSHKeyControllerTest.php b/tests/Integration/Api/Client/SSHKeyControllerTest.php index cb01b588b..fde371a4a 100644 --- a/tests/Integration/Api/Client/SSHKeyControllerTest.php +++ b/tests/Integration/Api/Client/SSHKeyControllerTest.php @@ -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);