Skip to content

Commit

Permalink
chore: updates to file locations
Browse files Browse the repository at this point in the history
Signed-off-by: Case Wylie <[email protected]>
  • Loading branch information
cmwylie19 committed Oct 31, 2024
1 parent 1cb0f0a commit 0199864
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/cli/build.helpers.ts
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";
}
36 changes: 36 additions & 0 deletions src/cli/build.test.ts
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");
});
});
2 changes: 1 addition & 1 deletion src/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BuildOptions>) => void | Promise<void>;
Expand Down

0 comments on commit 0199864

Please sign in to comment.