casbin / node-casbin

An authorization library that supports access control models like ACL, RBAC, ABAC in Node.js and Browser
https://casbin.org
Apache License 2.0
2.57k stars 214 forks source link

Without enforcer.loadPolicy() Casbin behave unpredictably on mass updates. #463

Open mdg-mdrozd opened 10 months ago

mdg-mdrozd commented 10 months ago

In our ephemeral environments, we reconstruct permissions by frequently invoking methods like enforcer.removePolicy() and enforcer.removeGroupingPolicy() thousands of times. However, each change necessitates a call to enforcer.loadPolicy(). This is increasingly problematic as the service gets progressively burdened with the growing number of policies to reload with each update, ultimately leading to service downtime.

enforcer is setup as follows:

export const casbinProviders = [
  {
    provide: PROVIDER_CASBIN_ENFORCER,
    useFactory: async (dataSource: DataSource) => {
      const adapter = await TypeORMAdapter.newAdapter({ connection: dataSource });
      const enforcer = await newEnforcer(newModelFromString(PERMISSIONS_MODEL), adapter);
      await enforcer.addFunction('isMatch', casbinIsMatch);
      await enforcer.addFunction('includes', casbinIncludes);
      await enforcer.loadPolicy();
      await enforcer.enableAutoSave(true);
      return enforcer;
    },
    inject: [getDataSourceToken()]
  }
];

In our production environment, the substantial number of policies we manage also poses a significant challenge. The frequent use of methods like enforcer.removePolicy() and enforcer.removeGroupingPolicy(), followed by enforcer.loadPolicy() after each modification, is proving to be problematic. As the number of policies grows, these operations increasingly strain the server during normal usage, leading to severe performance issues and potential service disruptions.

casbin-bot commented 10 months ago

@nodece @Shivansh-yadav13