Skip to content

Commit a100eef

Browse files
authored
fix(rbac): check source before throwing duplicate warning (#1278)
1 parent 0c98279 commit a100eef

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

plugins/rbac-backend/src/file-permissions/csv.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,17 @@ describe('CSV file', () => {
798798
let enfDelegate: EnforcerDelegate;
799799

800800
beforeEach(async () => {
801+
policyMetadataStorageMock.findPolicyMetadata = jest
802+
.fn()
803+
.mockImplementation(
804+
async (
805+
_policy: string[],
806+
_trx: Knex.Knex.Transaction,
807+
): Promise<PermissionPolicyMetadata> => {
808+
return { source: 'csv-file' };
809+
},
810+
);
811+
801812
const adapter = new StringAdapter(
802813
`
803814
p, user:default/known_user, test.resource.deny, use, allow
@@ -828,6 +839,7 @@ describe('CSV file', () => {
828839

829840
afterEach(() => {
830841
(loggerMock.warn as jest.Mock).mockReset();
842+
(policyMetadataStorageMock.findPolicyMetadata as jest.Mock).mockReset();
831843
});
832844

833845
it('should add policies from the CSV file', async () => {
@@ -926,6 +938,23 @@ describe('CSV file', () => {
926938
'role:default/catalog-updater',
927939
];
928940

941+
policyMetadataStorageMock.findPolicyMetadata = jest
942+
.fn()
943+
.mockImplementation(
944+
async (
945+
policy: string[],
946+
_trx: Knex.Knex.Transaction,
947+
): Promise<PermissionPolicyMetadata> => {
948+
if (
949+
isEqual(policy, duplicatePolicyEnforcer) ||
950+
isEqual(policy, duplicateRoleEnforcer)
951+
) {
952+
return { source: 'rest' };
953+
}
954+
return { source: 'csv-file' };
955+
},
956+
);
957+
929958
await enfDelegate.addPolicy(duplicatePolicyEnforcer, 'rest');
930959
await enfDelegate.addGroupingPolicy(duplicateRoleEnforcer, 'rest');
931960

@@ -943,7 +972,7 @@ describe('CSV file', () => {
943972

944973
expect(loggerMock.warn).toHaveBeenNthCalledWith(
945974
1,
946-
`Duplicate policy: ${duplicatePolicyEnforcer} found in the file ${errorPolicyFile}`,
975+
`Duplicate policy: ${duplicatePolicyEnforcer} found in the file ${errorPolicyFile}, originates from source: rest`,
947976
);
948977
expect(loggerMock.warn).toHaveBeenNthCalledWith(
949978
2,

plugins/rbac-backend/src/service/permission-policy.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,19 @@ const policyMetadataStorageMock: PolicyMetadataStorage = {
100100
return [];
101101
},
102102
),
103-
findPolicyMetadata: jest.fn().mockImplementation(),
103+
findPolicyMetadata: jest
104+
.fn()
105+
.mockImplementation(
106+
async (
107+
_policy: string[],
108+
_trx: Knex.Knex.Transaction,
109+
): Promise<PermissionPolicyMetadata> => {
110+
const test: PermissionPolicyMetadata = {
111+
source: 'csv-file',
112+
};
113+
return test;
114+
},
115+
),
104116
createPolicyMetadata: jest.fn().mockImplementation(),
105117
removePolicyMetadata: jest.fn().mockImplementation(),
106118
};

plugins/rbac-backend/src/service/policies-validation.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ async function validateGroupingPolicy(
154154
const metadata = await roleMetadataStorage.findRoleMetadata(parent);
155155
if (metadata && metadata.source !== source && metadata.source !== 'legacy') {
156156
return new Error(
157-
`You could not add user or group to the role created with source ${metadata.source}`,
157+
`Group policy is invalid: ${groupPolicy}. You could not add user or group to the role created with source ${metadata.source}`,
158158
);
159159
}
160160
return undefined;
@@ -176,9 +176,12 @@ export async function validateAllPredefinedPolicies(
176176
}
177177

178178
if (await enforcer.hasPolicy(...policy)) {
179-
return new Error(
180-
`Duplicate policy: ${policy} found in the file ${preDefinedPoliciesFile}`,
181-
);
179+
const source = (await enforcer.getMetadata(policy)).source;
180+
if (source !== 'csv-file') {
181+
return new Error(
182+
`Duplicate policy: ${policy} found in the file ${preDefinedPoliciesFile}, originates from source: ${source}`,
183+
);
184+
}
182185
}
183186
}
184187

@@ -194,9 +197,12 @@ export async function validateAllPredefinedPolicies(
194197
}
195198

196199
if (await enforcer.hasGroupingPolicy(...groupPolicy)) {
197-
return new Error(
198-
`Duplicate role: ${groupPolicy} found in the file ${preDefinedPoliciesFile}`,
199-
);
200+
const source = (await enforcer.getMetadata(groupPolicy)).source;
201+
if (source !== 'csv-file') {
202+
return new Error(
203+
`Duplicate role: ${groupPolicy} found in the file ${preDefinedPoliciesFile}, originates from source: ${source}`,
204+
);
205+
}
200206
}
201207
}
202208
return undefined;

0 commit comments

Comments
 (0)