From ac2085ffa83bc4e2fd0810c2f3b2c4f6ddcbfe22 Mon Sep 17 00:00:00 2001 From: Bhaskar Singh Date: Sat, 18 Mar 2023 22:03:15 +0530 Subject: [PATCH 1/7] Updated error when _APP_USAGE_STATS is disabled for usage --- app/config/errors.php | 5 +++++ app/console | 2 +- app/controllers/api/databases.php | 9 +++++++++ app/controllers/api/functions.php | 6 ++++++ app/controllers/api/projects.php | 3 +++ app/controllers/api/storage.php | 6 ++++++ app/controllers/api/users.php | 3 +++ src/Appwrite/Extend/Exception.php | 1 + 8 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/config/errors.php b/app/config/errors.php index 266e017e93..852b103dc5 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -88,6 +88,11 @@ return [ 'description' => 'The request cannot be fulfilled with the current protocol. Please check the value of the _APP_OPTIONS_FORCE_HTTPS environment variable.', 'code' => 500, ], + Exception::GENERAL_USAGE_DISABLED => [ + 'name' => Exception::GENERAL_USAGE_DISABLED, + 'description' => 'Usage stats is not configured. Please check the value of the _APP_USAGE_STATS environment variable of your Appwrite server.', + 'code' => 501, + ], /** User Errors */ Exception::USER_COUNT_EXCEEDED => [ diff --git a/app/console b/app/console index 5e2a40c1e3..d30a4070d0 160000 --- a/app/console +++ b/app/console @@ -1 +1 @@ -Subproject commit 5e2a40c1e397bd341a432698c9d76a3f96315841 +Subproject commit d30a4070d0bdf8fe89be23a141e261a8a8688246 diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 9ec46817ac..4c80bfe645 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -2471,6 +2471,9 @@ App::get('/v1/databases/usage') ->action(function (string $range, Response $response, Database $dbForProject) { $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ @@ -2590,6 +2593,9 @@ App::get('/v1/databases/:databaseId/usage') ->action(function (string $databaseId, string $range, Response $response, Database $dbForProject) { $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ @@ -2710,6 +2716,9 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/usage') } $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 049d2cb850..f91784bb22 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -234,6 +234,9 @@ App::get('/v1/functions/:functionId/usage') } $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ @@ -337,6 +340,9 @@ App::get('/v1/functions/usage') ->action(function (string $range, Response $response, Database $dbForProject) { $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 29a193748d..b615b3555c 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -268,6 +268,9 @@ App::get('/v1/projects/:projectId/usage') } $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index 271f2af6b3..ff8dd3ca2c 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -1461,6 +1461,9 @@ App::get('/v1/storage/usage') ->action(function (string $range, Response $response, Database $dbForProject) { $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') === 'enabled') { $periods = [ '24h' => [ @@ -1578,6 +1581,9 @@ App::get('/v1/storage/:bucketId/usage') } $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') === 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index 2a84c06675..96d6017848 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -1114,6 +1114,9 @@ App::get('/v1/users/usage') ->action(function (string $range, string $provider, Response $response, Database $dbForProject) { $usage = []; + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 8b240aa971..f072fcfad3 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -51,6 +51,7 @@ class Exception extends \Exception public const GENERAL_CURSOR_NOT_FOUND = 'general_cursor_not_found'; public const GENERAL_SERVER_ERROR = 'general_server_error'; public const GENERAL_PROTOCOL_UNSUPPORTED = 'general_protocol_unsupported'; + public const GENERAL_USAGE_DISABLED = 'general_usage_disabled'; /** Users */ public const USER_COUNT_EXCEEDED = 'user_count_exceeded'; From 5071172b00196ec7b87a8eb82168a43996e85883 Mon Sep 17 00:00:00 2001 From: Bhaskar Singh Date: Tue, 21 Mar 2023 21:26:22 +0530 Subject: [PATCH 2/7] Updating the console code --- app/console | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/console b/app/console index d30a4070d0..b9f3d61d9f 160000 --- a/app/console +++ b/app/console @@ -1 +1 @@ -Subproject commit d30a4070d0bdf8fe89be23a141e261a8a8688246 +Subproject commit b9f3d61d9f97b6bb17a1078d6418d4141ad011c2 From 6324d78a0d0588d7e13c32b453e74053cfdcd4cd Mon Sep 17 00:00:00 2001 From: Bhaskar Singh Date: Tue, 21 Mar 2023 21:30:49 +0530 Subject: [PATCH 3/7] Update the console app commit --- app/console | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/console b/app/console index b9f3d61d9f..7bc1d187e0 160000 --- a/app/console +++ b/app/console @@ -1 +1 @@ -Subproject commit b9f3d61d9f97b6bb17a1078d6418d4141ad011c2 +Subproject commit 7bc1d187e0e9bb22f28425f04c75a79ceaa5e00e From 01840b38016643c5dbae3c2eb90d84ccc84b7fb9 Mon Sep 17 00:00:00 2001 From: Bhaskar Singh Date: Fri, 24 Mar 2023 21:46:12 +0530 Subject: [PATCH 4/7] Reverting console change from main repo --- app/console | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/console b/app/console index 7bc1d187e0..5e2a40c1e3 160000 --- a/app/console +++ b/app/console @@ -1 +1 @@ -Subproject commit 7bc1d187e0e9bb22f28425f04c75a79ceaa5e00e +Subproject commit 5e2a40c1e397bd341a432698c9d76a3f96315841 From 3865f48d320172aab8ea03d11c8a99b28dc69a3f Mon Sep 17 00:00:00 2001 From: Bhaskar Singh Date: Fri, 21 Apr 2023 23:09:01 +0530 Subject: [PATCH 5/7] Moved check for _APP_USAGE_STATS to create init hook for App --- app/controllers/api/databases.php | 15 +++------------ app/controllers/api/functions.php | 10 ++-------- app/controllers/api/projects.php | 5 +---- app/controllers/api/storage.php | 10 ++-------- app/controllers/api/users.php | 5 +---- app/controllers/shared/api.php | 9 +++++++++ 6 files changed, 18 insertions(+), 36 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 4c80bfe645..b97c3f77c3 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -2457,7 +2457,7 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/documents/:docu App::get('/v1/databases/usage') ->desc('Get usage stats for the database') - ->groups(['api', 'database']) + ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'databases') @@ -2471,9 +2471,6 @@ App::get('/v1/databases/usage') ->action(function (string $range, Response $response, Database $dbForProject) { $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ @@ -2578,7 +2575,7 @@ App::get('/v1/databases/usage') App::get('/v1/databases/:databaseId/usage') ->desc('Get usage stats for the database') - ->groups(['api', 'database']) + ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'databases') @@ -2593,9 +2590,6 @@ App::get('/v1/databases/:databaseId/usage') ->action(function (string $databaseId, string $range, Response $response, Database $dbForProject) { $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ @@ -2691,7 +2685,7 @@ App::get('/v1/databases/:databaseId/usage') App::get('/v1/databases/:databaseId/collections/:collectionId/usage') ->alias('/v1/database/:collectionId/usage', ['databaseId' => 'default']) ->desc('Get usage stats for a collection') - ->groups(['api', 'database']) + ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'databases') @@ -2716,9 +2710,6 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/usage') } $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index f91784bb22..1a7f9bdb2e 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -213,7 +213,7 @@ App::get('/v1/functions/:functionId') App::get('/v1/functions/:functionId/usage') ->desc('Get Function Usage') - ->groups(['api', 'functions']) + ->groups(['api', 'functions', 'usage']) ->label('scope', 'functions.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'functions') @@ -234,9 +234,6 @@ App::get('/v1/functions/:functionId/usage') } $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ @@ -326,7 +323,7 @@ App::get('/v1/functions/:functionId/usage') App::get('/v1/functions/usage') ->desc('Get Functions Usage') - ->groups(['api', 'functions']) + ->groups(['api', 'functions', 'usage']) ->label('scope', 'functions.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'functions') @@ -340,9 +337,6 @@ App::get('/v1/functions/usage') ->action(function (string $range, Response $response, Database $dbForProject) { $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index dd703be67b..55b3ef2bb8 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -245,7 +245,7 @@ App::get('/v1/projects/:projectId') App::get('/v1/projects/:projectId/usage') ->desc('Get usage stats for a project') - ->groups(['api', 'projects']) + ->groups(['api', 'projects', 'usage']) ->label('scope', 'projects.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'projects') @@ -268,9 +268,6 @@ App::get('/v1/projects/:projectId/usage') } $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index ff8dd3ca2c..0c20ffec3c 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -1447,7 +1447,7 @@ App::delete('/v1/storage/buckets/:bucketId/files/:fileId') App::get('/v1/storage/usage') ->desc('Get usage stats for storage') - ->groups(['api', 'storage']) + ->groups(['api', 'storage', 'usage']) ->label('scope', 'files.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'storage') @@ -1461,9 +1461,6 @@ App::get('/v1/storage/usage') ->action(function (string $range, Response $response, Database $dbForProject) { $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') === 'enabled') { $periods = [ '24h' => [ @@ -1560,7 +1557,7 @@ App::get('/v1/storage/usage') App::get('/v1/storage/:bucketId/usage') ->desc('Get usage stats for a storage bucket') - ->groups(['api', 'storage']) + ->groups(['api', 'storage', 'usage']) ->label('scope', 'files.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'storage') @@ -1581,9 +1578,6 @@ App::get('/v1/storage/:bucketId/usage') } $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') === 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index 96d6017848..930862994b 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -1098,7 +1098,7 @@ App::delete('/v1/users/:userId') App::get('/v1/users/usage') ->desc('Get usage stats for the users API') - ->groups(['api', 'users']) + ->groups(['api', 'users', 'usage']) ->label('scope', 'users.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'users') @@ -1114,9 +1114,6 @@ App::get('/v1/users/usage') ->action(function (string $range, string $provider, Response $response, Database $dbForProject) { $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 5655874c65..ae1501a5ad 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -554,3 +554,12 @@ App::shutdown() ->submit(); } }); + +App::init() + ->groups(['usage']) + ->inject('utopia') + ->action(function (App $utopia) { + if ($utopia::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } + }); From 7064f884aac66eb9a2a8325e2ed4c410bd883977 Mon Sep 17 00:00:00 2001 From: Bhaskar Singh Date: Thu, 27 Apr 2023 19:32:05 +0530 Subject: [PATCH 6/7] Refactor usage init hook to remove unecessary inject --- app/controllers/shared/api.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index ae1501a5ad..256e9973c2 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -557,9 +557,8 @@ App::shutdown() App::init() ->groups(['usage']) - ->inject('utopia') - ->action(function (App $utopia) { - if ($utopia::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + ->action(function () { + if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { throw new Exception(Exception::GENERAL_USAGE_DISABLED); } }); From a4299a69ef77864951aa3fe351b80e6455454182 Mon Sep 17 00:00:00 2001 From: Bhaskar Singh Date: Tue, 2 May 2023 23:47:50 +0530 Subject: [PATCH 7/7] Refactored the condition for strict inequality --- app/controllers/shared/api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 256e9973c2..14d0ff44e4 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -558,7 +558,7 @@ App::shutdown() App::init() ->groups(['usage']) ->action(function () { - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + if (App::getEnv('_APP_USAGE_STATS', 'enabled') !== 'enabled') { throw new Exception(Exception::GENERAL_USAGE_DISABLED); } });