storage のセキュリティ保護
@secureStorage デコレーターは storage bucket を保護し、認可されたユーザーのみがファイルにアクセスできるようにします。
Squid の storage 機能を使用すると、Squid Client SDK でファイルを管理でき、任意の client からファイルを操作できます。Squid Client SDK で Squid storage を使用する方法について詳しくは、Squid storage のドキュメントを参照してください。
security function は boolean を返します。true は request を許可し、false は拒否します。
Squid storage bucket integration をセキュリティ保護するには、Squid backend の SquidService class 内で @secureStorage デコレーターを使用し、action type と integration ID を渡します。次のコードでは、組み込み storage bucket への完全なアクセスを許可します。
- TypeScript
- Python
import { secureStorage, SquidService } from '@squidcloud/backend';
@secureStorage('all', 'built_in_storage')
allowAllAccessToBuiltInStorage(): boolean {
return true;
}
from squidcloud_backend import SquidService, secure_storage
class ExampleService(SquidService):
@secure_storage('all', 'built_in_storage')
def allow_all_access_to_built_in_storage(self, context: dict) -> bool:
return True
別の storage integration の Squid storage bucket をセキュリティ保護するには、デコレーターに integration ID を指定します。
- TypeScript
- Python
import { secureStorage, SquidService } from '@squidcloud/backend';
@secureStorage('all', 'YOUR_STORAGE_INTEGRATION_ID')
allowAllAccessToStorageIntegration(): boolean {
return true;
}
from squidcloud_backend import SquidService, secure_storage
class ExampleService(SquidService):
@secure_storage('all', 'YOUR_STORAGE_INTEGRATION_ID')
def allow_all_access_to_storage_integration(self, context: dict) -> bool:
return True
利用可能な action type は次のとおりです。
'read'
'read' action には、metadata の読み取り、download URL の生成、ファイルのダウンロード、directory contents の一覧表示が含まれます。
'write'
'write' action には、新しいファイルの挿入、既存ファイルの更新、ファイルの削除が含まれます。
'insert'
'insert' action では新しいファイルの挿入が許可されますが、既存ファイルの内容の更新やファイルの削除は許可されません。
'delete'
'delete' action では既存ファイルの削除が許可されます。
'all'
'all' action には、利用可能なすべての bucket action が含まれます。
書き込みのセキュリティ保護
次の関数は、認証済みユーザーが組み込み storage bucket にファイルを upload、update、delete できるようにします。
- TypeScript
- Python
import { secureStorage, SquidService } from '@squidcloud/backend';
@secureStorage('write', 'built_in_storage')
allowAuthenticatedWrites(): boolean {
return this.isAuthenticated();
}
from squidcloud_backend import SquidService, secure_storage
class ExampleService(SquidService):
@secure_storage('write', 'built_in_storage')
def allow_authenticated_writes(self, context: dict) -> bool:
return self.is_authenticated()
読み取りのセキュリティ保護
directory name、file metadata の読み取り、および download URL の生成をセキュリティ保護するには、'read' action type を使用します。次の関数は、file path がユーザー ID である場合に、ユーザーが download URL を生成できるようにします。
- TypeScript
- Python
import { secureStorage, SquidService, StorageContext } from '@squidcloud/backend';
@secureStorage('read', 'built_in_storage')
allowReadUserFiles(context: StorageContext): boolean {
// Validate if the requested action is to get a download URL
if (context.functionality !== 'getDownloadUrl') {
return false;
}
const userId = this.getUserAuth()?.userId;
if (!userId) return false;
// Check any paths the user is trying to read to verify they're in the user's directory
for (const path of context.pathsInBucket) {
if (!path.startsWith(`user/${userId}`)) {
return false;
}
}
return true;
}
from squidcloud_backend import SquidService, secure_storage
class ExampleService(SquidService):
@secure_storage('read', 'built_in_storage')
def allow_read_user_files(self, context: dict) -> bool:
# Validate if the requested action is to get a download URL
if context['functionality'] != 'getDownloadUrl':
return False
user_auth = self.get_user_auth()
if not user_auth:
return False
user_id = user_auth['userId']
# Check any paths the user is trying to read to verify they're in the user's directory
for path in context['pathsInBucket']:
if not path.startswith(f'user/{user_id}'):
return False
return True
StorageContext object を関数の parameter として含めると、security function は client が実行しようとしている action に関する情報へアクセスできます。Python では、同じ情報が dict として渡されます。次に StorageContext object の例を示します。
{
integrationId: 'built_in_storage',
pathsInBucket: [ 'test/path/img.jpg' ],
action: 'read',
functionality: 'getFileMetadata'
}