From 019986425aac679545dea3dff34d1d3e61d2fd0f Mon Sep 17 00:00:00 2001 From: Case Wylie Date: Thu, 31 Oct 2024 08:19:22 -0400 Subject: [PATCH] chore: updates to file locations Signed-off-by: Case Wylie --- src/cli/build.helpers.ts | 28 ++++++++++++++++++++++++++++ src/cli/build.test.ts | 36 ++++++++++++++++++++++++++++++++++++ src/cli/build.ts | 2 +- 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/cli/build.helpers.ts create mode 100644 src/cli/build.test.ts diff --git a/src/cli/build.helpers.ts b/src/cli/build.helpers.ts new file mode 100644 index 00000000..21791394 --- /dev/null +++ b/src/cli/build.helpers.ts @@ -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"; +} diff --git a/src/cli/build.test.ts b/src/cli/build.test.ts new file mode 100644 index 00000000..1c310b55 --- /dev/null +++ b/src/cli/build.test.ts @@ -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"); + }); +}); diff --git a/src/cli/build.ts b/src/cli/build.ts index 0286977f..f0e5f09d 100644 --- a/src/cli/build.ts +++ b/src/cli/build.ts @@ -13,7 +13,7 @@ import { peprFormat } from "./format"; import { Option } from "commander"; import { createDirectoryIfNotExists, validateCapabilityNames, parseTimeout } from "../lib/helpers"; import { sanitizeResourceName } from "../sdk/sdk"; -import { determineRbacMode } from "../lib/cli-helpers/build"; +import { determineRbacMode } from "./build.helpers"; const peprTS = "pepr.ts"; let outputDir: string = "dist"; export type Reloader = (opts: BuildResult) => void | Promise;