forked from backstage/community-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rbac): lowercase user and group entity references (backstage#1937)
* Introduce LowercaseFileAdapter Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Introduce transformPolicyGroupToLowercase Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Introduce transformRolesGroupToLowercase Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Convert uppercase groups to lowecase in csv watcher Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Convert uppercase groups to lowercase in providers Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Add v0 migration of users groups to lowercase Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Update lowercase-file-adapter reference Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Fix prettier Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Introduce lowercasing memberReferences in role endpoints Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Lowercase admins entityRef from config Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Fix uppercase role Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Fix delete of role user Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Catch invalid calls to enforcer delegate Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Fix return type of mock implementation Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Add newline to duplicate-policy.csv Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Add changeset Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Unify lowecasing Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> Co-authored-by: Oleksandr Andriienko <oandriie@redhat.com> Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> --------- Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> Co-authored-by: Oleksandr Andriienko <oandriie@redhat.com>
- Loading branch information
Showing
16 changed files
with
473 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@backstage-community/plugin-rbac-backend': minor | ||
--- | ||
|
||
Roles and permissions were not correctly applied for users and groups with names containing uppercase letters. To address this issue, we now convert user and group references in all user inputs to lowercase. This change migrates `v0` column in `casbin_rule` table in `backstage_plugin_permission` database. Conditions containing claims with uppercase letters are not resolved yet. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
workspaces/rbac/plugins/rbac-backend/__fixtures__/data/valid-csv/uppercase-policy.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
p, role:default/CATALOG-USER, catalog-entity, read, allow | ||
p, role:default/known_role, test.resource.deny, use, allow | ||
|
||
g, user:default/known_user, role:default/known_role | ||
g, user:default/TOM, role:default/CATALOG-USER | ||
g, group:default/READER-GROUP, role:default/CATALOG-USER | ||
g, group:default/READER-GROUP, role:default/known_role |
35 changes: 35 additions & 0 deletions
35
workspaces/rbac/plugins/rbac-backend/migrations/20241108093910_migrations.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2024 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
exports.up = async function up(knex) { | ||
const casbinExists = await knex.schema.hasTable('casbin_rule'); | ||
if (casbinExists) { | ||
await knex('casbin_rule') | ||
.whereNotNull('v0') | ||
.where(function groups() { | ||
this.where('v0', 'like', 'user:%').orWhere('v0', 'like', 'group:%'); | ||
}) | ||
.update({ | ||
v0: knex.raw('LOWER(??)', ['v0']), | ||
}); | ||
} | ||
}; | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
exports.down = async function down(_knex) {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
workspaces/rbac/plugins/rbac-backend/src/file-permissions/lowercase-file-adapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2024 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { FileAdapter, Helper, Model, mustGetDefaultFileSystem } from 'casbin'; | ||
|
||
export class LowercaseFileAdapter extends FileAdapter { | ||
public async loadPolicy(model: Model): Promise<void> { | ||
if (!this.filePath) { | ||
return; | ||
} | ||
await this.loadLowercasePolicyFile(model, Helper.loadPolicyLine); | ||
} | ||
|
||
private transformLineToLowercaseGroupsUsers(line: string): string { | ||
if (line.trim().startsWith('g')) { | ||
const policyArray = line.split(','); | ||
if (policyArray.length >= 1 && policyArray[0].trim().startsWith('g')) { | ||
policyArray[1] = policyArray[1].toLocaleLowerCase('en-US'); | ||
} | ||
return policyArray.join(','); | ||
} | ||
return line; | ||
} | ||
|
||
private async loadLowercasePolicyFile( | ||
model: Model, | ||
handler: (line: string, model: Model) => void, | ||
): Promise<void> { | ||
// Reference: https://github.com/casbin/node-casbin/blob/master/src/persist/fileAdapter.ts#L34-#L43 | ||
const bodyBuf = await ( | ||
this.fs ? this.fs : mustGetDefaultFileSystem() | ||
).readFileSync(this.filePath); | ||
const lines = bodyBuf.toString().split('\n'); | ||
|
||
lines.forEach((line: string) => { | ||
if (!line) { | ||
return; | ||
} | ||
const lowercasedLine = this.transformLineToLowercaseGroupsUsers(line); | ||
handler(lowercasedLine, model); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.