-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Case Wylie <[email protected]>
- Loading branch information
Showing
3 changed files
with
65 additions
and
1 deletion.
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,28 @@ | ||
/** | ||
* Determine the RBAC mode based on the CLI options and the module's config | ||
* @param opts CLI options | ||
* @param cfg Module's config | ||
* @returns The determined RBAC mode | ||
* @example | ||
* const opts = { rbacMode: "admin" }; | ||
* const cfg = { pepr: { rbacMode: "scoped" } }; | ||
* const result = determineRbacMode(opts, cfg); | ||
* console.log(result); // "admin" | ||
*/ | ||
export function determineRbacMode( | ||
opts: { rbacMode?: string }, | ||
cfg: { pepr: { rbacMode?: string } }, | ||
): string { | ||
// CLI overrides the module's config | ||
if (opts.rbacMode) { | ||
return opts.rbacMode; | ||
} | ||
|
||
// if rbacMode is defined and not scoped, return admin | ||
if (cfg.pepr.rbacMode && cfg.pepr.rbacMode !== "scoped") { | ||
return "admin"; | ||
} | ||
|
||
// if nothing is defined return admin, else return scoped | ||
return cfg.pepr.rbacMode || "admin"; | ||
} |
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,36 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors | ||
|
||
import { determineRbacMode } from "./build.helpers"; | ||
|
||
import { expect, describe, test } from "@jest/globals"; | ||
|
||
describe("determineRbacMode", () => { | ||
test("should allow CLI options to overwrite module config", () => { | ||
const opts = { rbacMode: "admin" }; | ||
const cfg = { pepr: { rbacMode: "scoped" } }; | ||
const result = determineRbacMode(opts, cfg); | ||
expect(result).toBe("admin"); | ||
}); | ||
|
||
test('should return "admin" when cfg.pepr.rbacMode is provided and not "scoped"', () => { | ||
const opts = {}; | ||
const cfg = { pepr: { rbacMode: "admin" } }; | ||
const result = determineRbacMode(opts, cfg); | ||
expect(result).toBe("admin"); | ||
}); | ||
|
||
test('should return "scoped" when cfg.pepr.rbacMode is "scoped"', () => { | ||
const opts = {}; | ||
const cfg = { pepr: { rbacMode: "scoped" } }; | ||
const result = determineRbacMode(opts, cfg); | ||
expect(result).toBe("scoped"); | ||
}); | ||
|
||
test("should default to admin when neither option is provided", () => { | ||
const opts = {}; | ||
const cfg = { pepr: {} }; | ||
const result = determineRbacMode(opts, cfg); | ||
expect(result).toBe("admin"); | ||
}); | ||
}); |
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