メインコンテンツまでスキップ

Role-Based Access Control(RBAC)

resource への access を保護するため、Squid backend で user role を管理します

Squid は、あらゆる enterprise use case に適合するよう、security control を中核として設計されています。Squid Console では、Admin と Developer の role および permissionが out of the box で定義されていますが、Squid は application 固有の requirement に対応する Role-Based Access Control(RBAC)capability をさらに構築・customize する機能も提供します。

これは、fine-grained control と permission をサポートするために最高水準の compliance を必要とする regulated industry 向けに、高度な security と authorization を提供する堅牢な mechanism です。

RBAC の設定

この guide では、効率的な RBAC implementation のために Squid framework を使用する方法を説明します。

Squid は built-in NoSQL database を提供しており、application の user role をプログラムで管理できます。新しい collection を作成せずに users collection を使用してこれらの role の保存・管理を開始でき、application に必要な数だけ role type を定義できます。

user が追加または削除されるたびに、users collection をその role で update する必要があります。これは Squid backend code を使用して行います。たとえば、user state が変更されるたびに呼び出せる webhook または executable を追加できます。

auth provider が user change 用 webhook を提供すると仮定した場合、TypeScript decorator を使用した基本例は以下のとおりです。

Backend code
@webhook('userAdded')
async handleUserAdded(request: WebhookRequest) {
const apiKey = request.headers['apiKey'];
if (apiKey !== this.secrets['AUTH_PROVIDER_API_KEY']) {
throw Error('not authorized');
}
const userId = request.body.userId;
const role = request.body.role;
await this.squid.collection<User>('users').doc(userId).insert(
{userId, role});
}

// TODO - Implement webhooks for user updated and deleted

user role に基づく authorization: users collection で user とその role を設定した後、これらの role に基づく request の authorize 方法を決定できます。これは backend code の @secureCollection またはその他の @secure function 内で行います。

たとえば、client が collection から data を read しようとする場合、access を許可する前に user role を検証するため、@secureCollection で decorate された function を invoke できます。

Backend code
@secureCollection('name_of_the_collection', 'id of the integration', 'read')
secureReadCollection(request: SecureCollectionRequest): Promise<boolean> {
const userId = this.getUserId();
const user = await this.squid.collection<User>('users').doc(userId).snapshot();
const role = user.role;

// Check the role and decide whether to authorize the request
return role === 'admin';
}

結論として、Squid は specific need に合致する custom RBAC system を実装するために必要な tool と flexibility を提供します。users collection、backend code、Client SDK の組み合わせにより、application 向けの安全かつ効率的な RBAC system を制御できます。