access-control-rbac について
RBACやABACなどを用いたきめ細かなアクセス制御の設計を支援するスキルです。Node.js、Python、Java Spring Securityの実装例とセキュリティ上の実践事項を扱います。
主な機能
- 01RBAC・ABACなどのモデル比較
- 02Node.jsのRBAC実装パターン
- 03PythonのABACポリシーパターン
- 04Java Spring Securityの実装案内
- 05アクセス制御のベストプラクティス
活用シーン
- 01アプリケーションの権限制御モデル選定
- 02Node.jsでのロールベース認可の実装
- 03Pythonでの属性ベースポリシーの定義
- 04Springのメソッドレベルセキュリティ設定
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ secondsky/ claude-skills --skill access-control-rbacインストール後、Claude Code で/access-control-rbacを入力して読み込まれたか確認します。
同梱ファイルreferences/java-spring-security.mdreferences/python-abac.md
詳細
アクセス制御と RBAC
RBAC、ABAC、またはハイブリッド方式を使用して、きめ細かな権限を備えた安全なアクセス制御システムを実装します。
アクセス制御モデル
| モデル | 説明 | 最適な用途 |
|---|---|---|
| RBAC | ロールベース:ユーザーに権限を持つロールを割り当てます | ほとんどのアプリケーション |
| ABAC | 属性ベース:ポリシーでユーザーやリソースの属性を評価します | 複雑なルール |
| MAC | 強制アクセス制御:システムが分類レベルを適用します | 政府・軍事 |
| DAC | 任意アクセス制御:リソースの所有者がアクセスを管理します | ファイルシステム |
| ReBAC | 関係ベース:エンティティ間の関係を介してアクセスを許可します | ソーシャルアプリ |
Node.js RBAC の実装
class Permission {
constructor(resource, action) {
this.resource = resource;
this.action = action;
}
matches(resource, action) {
return (this.resource === '*' || this.resource === resource) &&
(this.action === '*' || this.action === action);
}
}
class Role {
constructor(name, permissions = [], parent = null) {
this.name = name;
this.permissions = permissions;
this.parent = parent;
}
hasPermission(resource, action) {
if (this.permissions.some(p => p.matches(resource, action))) return true;
return this.parent?.hasPermission(resource, action) ?? false;
}
}
class RBACSystem {
constructor() {
this.roles = new Map();
this.userRoles = new Map();
}
createRole(name, permissions = [], parentRole = null) {
const parent = parentRole ? this.roles.get(parentRole) : null;
this.roles.set(name, new Role(name, permissions, parent));
}
assignRole(userId, roleName) {
const userRoles = this.userRoles.get(userId) || [];
userRoles.push(this.roles.get(roleName));
this.userRoles.set(userId, userRoles);
}
can(userId, resource, action) {
const roles = this.userRoles.get(userId) || [];
return roles.some(role => role.hasPermission(resource, action));
}
}
// Express middleware
const requirePermission = (resource, action) => (req, res, next) => {
if (!rbac.can(req.user.id, resource, action)) {
return res.status(403).json({ error: 'Forbidden' });
}
next();
};
// Setup default roles
const rbac = new RBACSystem();
rbac.createRole('viewer', [new Permission('*', 'read')]);
rbac.createRole('editor', [new Permission('*', 'write')], 'viewer');
rbac.createRole('admin', [new Permission('*', '*')], 'editor');
Python ABAC パターン
class Policy:
def __init__(self, name, effect, resource, action, conditions):
self.name = name
self.effect = effect # 'allow' or 'deny'
self.resource = resource
self.action = action
self.conditions = conditions
def matches(self, context):
if self.resource != "*" and self.resource != context.get("resource"):
return False
if self.action != "*" and self.action != context.get("action"):
return False
return True
def evaluate(self, context):
return all(cond(context) for cond in self.conditions)
class ABACEngine:
def __init__(self):
self.policies = []
def add_policy(self, policy):
self.policies.append(policy)
def check_access(self, context):
for policy in self.policies:
if policy.matches(context) and policy.evaluate(context):
return policy.effect == 'allow'
return False # Deny by default
# Condition functions
def is_resource_owner(ctx):
return ctx.get("user_id") == ctx.get("resource_owner_id")
def is_within_business_hours(ctx):
from datetime import datetime
return 9 <= datetime.now().hour < 18
Flask との統合を含む完全な実装については、references/python-abac.md を参照してください。
Java Spring Security
エンタープライズ向けの実装については、以下を含む references/java-spring-security.md を参照してください。
- Spring Security の設定
@PreAuthorizeを使用したメソッドレベルのセキュリティ- カスタム権限サービス
- カスタムセキュリティ式
ベストプラクティス
推奨事項:
- 最小権限の原則を適用します
- ロール階層を使用して重複を減らします
- すべてのアクセス変更を監査します
- 四半期ごとに権限を見直します
- パフォーマンス向上のため、権限チェックをキャッシュします
- 認証と認可を分離します
避けるべきこと:
- 権限チェックをハードコードします
- 見直しを行わずに権限が徐々に拡大することを許します
- 監査ログを省略します
- 範囲が広すぎるワイルドカードを使用します
参考資料
コメント
Markdown 対応