Skip to content

Commit

Permalink
fix(rbac): fix sonarcloud issues
Browse files Browse the repository at this point in the history
  • Loading branch information
PatAKnight committed Jan 29, 2024
1 parent c340485 commit 271d6ea
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 41 deletions.
10 changes: 6 additions & 4 deletions plugins/rbac-backend/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ export async function removeTheDifference(
roleName: string,
enf: EnforcerDelegate,
): Promise<void> {
const missing = difference(originalGroup.sort(), addedGroup.sort());
originalGroup.sort((a, b) => a.localeCompare(b));
addedGroup.sort((a, b) => a.localeCompare(b));
const missing = difference(originalGroup, addedGroup);

missing.forEach(async name => {
const role = [name, roleName];
for (const missingRole of missing) {
const role = [missingRole, roleName];
await enf.removeGroupingPolicy(role, source, true);
});
}
}
28 changes: 14 additions & 14 deletions plugins/rbac-backend/src/service/enforcer-delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class EnforcerDelegate {
source: Source,
externalTrx?: Knex.Transaction,
): Promise<void> {
const trx = externalTrx || (await this.knex.transaction());
const trx = externalTrx ?? (await this.knex.transaction());

try {
await this.policyMetadataStorage.createPolicyMetadata(
Expand Down Expand Up @@ -95,7 +95,7 @@ export class EnforcerDelegate {
trx,
);
}
const ok = this.enforcer.addPolicies(policies);
const ok = await this.enforcer.addPolicies(policies);
if (!ok) {
throw new Error(
`Failed to store policies ${policiesToString(policies)}`,
Expand All @@ -118,7 +118,7 @@ export class EnforcerDelegate {
externalTrx?: Knex.Transaction,
isUpdate?: boolean,
): Promise<void> {
const trx = externalTrx || (await this.knex.transaction());
const trx = externalTrx ?? (await this.knex.transaction());
const entityRef = policy[1];
let metadata;

Expand Down Expand Up @@ -165,7 +165,7 @@ export class EnforcerDelegate {
externalTrx?: Knex.Transaction,
isUpdate?: boolean,
): Promise<void> {
const trx = externalTrx || (await this.knex.transaction());
const trx = externalTrx ?? (await this.knex.transaction());

try {
for (const policy of policies) {
Expand Down Expand Up @@ -215,7 +215,7 @@ export class EnforcerDelegate {
allowToDeleteCSVFilePolicy?: boolean,
externalTrx?: Knex.Transaction,
): Promise<void> {
const trx = externalTrx || (await this.knex.transaction());
const trx = externalTrx ?? (await this.knex.transaction());
const newRoleName = newRole.at(0)?.at(1)!;
const oldRoleName = oldRole.at(0)?.at(1)!;
try {
Expand Down Expand Up @@ -250,7 +250,7 @@ export class EnforcerDelegate {
allowToDeleteCSVFilePolicy?: boolean,
externalTrx?: Knex.Transaction,
): Promise<void> {
const trx = externalTrx || (await this.knex.transaction());
const trx = externalTrx ?? (await this.knex.transaction());

try {
await this.removePolicies(
Expand All @@ -277,7 +277,7 @@ export class EnforcerDelegate {
allowToDeleteCSVFilePolicy?: boolean,
externalTrx?: Knex.Transaction,
) {
const trx = externalTrx || (await this.knex.transaction());
const trx = externalTrx ?? (await this.knex.transaction());

try {
await this.checkIfPolicyModifiable(
Expand Down Expand Up @@ -308,7 +308,7 @@ export class EnforcerDelegate {
allowToDeleCSVFilePolicy?: boolean,
externalTrx?: Knex.Transaction,
): Promise<void> {
const trx = externalTrx || (await this.knex.transaction());
const trx = externalTrx ?? (await this.knex.transaction());

try {
for (const policy of policies) {
Expand Down Expand Up @@ -345,7 +345,7 @@ export class EnforcerDelegate {
allowToDeleCSVFilePolicy?: boolean,
externalTrx?: Knex.Transaction,
): Promise<void> {
const trx = externalTrx || (await this.knex.transaction());
const trx = externalTrx ?? (await this.knex.transaction());
const roleEntity = policy[1];

try {
Expand Down Expand Up @@ -381,7 +381,7 @@ export class EnforcerDelegate {
isUpdate?: boolean,
externalTrx?: Knex.Transaction,
): Promise<void> {
const trx = externalTrx || (await this.knex.transaction());
const trx = externalTrx ?? (await this.knex.transaction());
try {
for (const policy of policies) {
const roleEntity = policy[1];
Expand Down Expand Up @@ -421,7 +421,7 @@ export class EnforcerDelegate {
isCSV: boolean,
externalTrx?: Knex.Transaction,
): Promise<void> {
const trx = externalTrx || (await this.knex.transaction());
const trx = externalTrx ?? (await this.knex.transaction());
try {
if (!(await this.enforcer.hasPolicy(...policy))) {
await this.addPolicy(policy, source, trx);
Expand All @@ -446,7 +446,7 @@ export class EnforcerDelegate {
isCSV?: boolean,
externalTrx?: Knex.Transaction,
): Promise<void> {
const trx = externalTrx || (await this.knex.transaction());
const trx = externalTrx ?? (await this.knex.transaction());
try {
if (!(await this.hasGroupingPolicy(...groupPolicy))) {
await this.addGroupingPolicy(groupPolicy, source, trx);
Expand Down Expand Up @@ -478,7 +478,7 @@ export class EnforcerDelegate {
isCSV?: boolean,
externalTrx?: Knex.Transaction,
): Promise<void> {
const trx = externalTrx || (await this.knex.transaction());
const trx = externalTrx ?? (await this.knex.transaction());
try {
for (const groupPolicy of groupPolicies) {
if (!(await this.hasGroupingPolicy(...groupPolicy))) {
Expand Down Expand Up @@ -576,7 +576,7 @@ export class EnforcerDelegate {
policy: string[],
source: Source,
externalTrx?: Knex.Transaction,
): Promise<Boolean> {
): Promise<boolean> {
const metadata = await this.policyMetadataStorage.findPolicyMetadata(
policy,
externalTrx,
Expand Down
34 changes: 12 additions & 22 deletions plugins/rbac-backend/src/service/permission-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,15 @@ const useAdmins = async (
roleMetadataStorage: RoleMetadataStorage,
knex: Knex,
) => {
const rbacAdminsGroupPolicies: string[][] = [];
const groupPoliciesToCompare: string[] = [];
const addedGroupPolicies = new Map<string, string>();

admins.forEach(async localConfig => {
const entityRef = localConfig.getString('name');
for (const admin of admins) {
const entityRef = admin.getString('name');
validateEntityReference(entityRef);

const groupPolicy = [entityRef, adminRoleName];
if (!(await enf.hasGroupingPolicy(...groupPolicy))) {
rbacAdminsGroupPolicies.push(groupPolicy);
}
addedGroupPolicies.set(entityRef, adminRoleName);
});
}

const adminRoleMeta =
await roleMetadataStorage.findRoleMetadata(adminRoleName);
Expand Down Expand Up @@ -131,7 +126,7 @@ const removedOldPermissionPoliciesFileData = async (
fileEnf?: Enforcer,
) => {
const tempEnforcer =
fileEnf || (await newEnforcer(newModelFromString(MODEL)));
fileEnf ?? (await newEnforcer(newModelFromString(MODEL)));
const oldFilePolicies = new Set<string[]>();
const policiesMetadata = await enf.getFilteredPolicyMetadata('csv-file');
for (const policyMetadata of policiesMetadata) {
Expand Down Expand Up @@ -213,6 +208,14 @@ export class RBACPermissionPolicy implements PermissionPolicy {
'permission.rbac.policies-csv-file',
);

if (adminUsers && adminUsers.length > 0) {
await useAdmins(adminUsers, enforcerDelegate, roleMetadataStorage, knex);
} else {
logger.warn(
'There are no admins configured for the RBAC-backend plugin. The plugin may not work properly.',
);
}

if (policiesFile) {
await addPermissionPoliciesFileData(
policiesFile,
Expand All @@ -223,19 +226,6 @@ export class RBACPermissionPolicy implements PermissionPolicy {
await removedOldPermissionPoliciesFileData(enforcerDelegate);
}

if (adminUsers && adminUsers.length > 0) {
await useAdmins(
adminUsers || [],
enforcerDelegate,
roleMetadataStorage,
knex,
);
} else {
logger.warn(
'There are no admins configured for the RBAC-backend plugin. The plugin may not work properly.',
);
}

return new RBACPermissionPolicy(
enforcerDelegate,
logger,
Expand Down
2 changes: 1 addition & 1 deletion plugins/rbac-backend/src/service/policies-rest-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ export class PolicesServer {
const err = validatePolicy(policy);
if (err) {
throw new InputError(
`Invalid ${errorMessage || 'policy'} definition. Cause: ${
`Invalid ${errorMessage ?? 'policy'} definition. Cause: ${
err.message
}`,
); // 400
Expand Down

0 comments on commit 271d6ea

Please sign in to comment.