@blossm/permissions-lookup 0.0.182 → 0.0.183

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1 +1,5 @@
1
1
  Looks up permissions for a role.
2
+
3
+ Every `roles.yaml` in the roles bucket is read. Files that define the same
4
+ role id each add to that role's permissions, so a domain lists only what the
5
+ role may do in that domain.
package/index.js CHANGED
@@ -15,93 +15,95 @@ const unlinkAsync = promisify(unlink);
15
15
  const cacheKeyPrefix = "_permissions";
16
16
  const FIVE_MINUTES_IN_SECONDS = 60 * 5;
17
17
 
18
- export default ({ downloadFileFn }) => async ({
19
- internalTokenFn,
20
- externalTokenFn,
21
- principal,
22
- context,
23
- }) => {
24
- if (!principal) throw forbidden.message("Missing required permissions.");
18
+ export default ({ downloadFileFn }) =>
19
+ async ({ internalTokenFn, externalTokenFn, principal, context }) => {
20
+ if (!principal) throw forbidden.message("Missing required permissions.");
25
21
 
26
- const cacheKey = `${cacheKeyPrefix}.${JSON.stringify([
27
- process.env.NETWORK,
28
- principal.root,
29
- principal.service,
30
- principal.network,
31
- context ? [context.root, context.service, context.network] : null,
32
- ])}`;
22
+ const cacheKey = `${cacheKeyPrefix}.${JSON.stringify([
23
+ process.env.NETWORK,
24
+ principal.root,
25
+ principal.service,
26
+ principal.network,
27
+ context ? [context.root, context.service, context.network] : null,
28
+ ])}`;
33
29
 
34
- //Lookup permissions in cache. If found, use them.
35
- let { permissions: cachedPermissions } =
36
- (await redis.readObject(cacheKey)) || {};
37
- if (cachedPermissions) return JSON.parse(cachedPermissions);
30
+ //Lookup permissions in cache. If found, use them.
31
+ let { permissions: cachedPermissions } =
32
+ (await redis.readObject(cacheKey)) || {};
33
+ if (cachedPermissions) return JSON.parse(cachedPermissions);
38
34
 
39
- //Download files if they aren't downloaded already.
40
- const fileName = uuid();
41
- const extension = ".yaml";
42
- let defaultRoles = {};
43
- await downloadFileFn({ fileName, extension });
44
- const files = (await readDirAsync(".")).filter(
45
- (file) => file.startsWith(fileName) && file.endsWith(extension)
46
- );
35
+ //Download files if they aren't downloaded already.
36
+ const fileName = uuid();
37
+ const extension = ".yaml";
38
+ const defaultRoles = {};
39
+ await downloadFileFn({ fileName, extension });
40
+ const files = (await readDirAsync(".")).filter(
41
+ (file) => file.startsWith(fileName) && file.endsWith(extension),
42
+ );
47
43
 
48
- await Promise.all(
49
- files.map(async (file) => {
50
- const role = await readFileAsync(file);
51
- const defaultRole = yaml.parse(role.toString());
52
- defaultRoles = {
53
- ...defaultRoles,
54
- ...defaultRole,
55
- };
56
- await unlinkAsync(file);
57
- })
58
- );
44
+ await Promise.all(
45
+ files.map(async (file) => {
46
+ const roles = yaml.parse((await readFileAsync(file)).toString());
47
+ // Each domain's file lists what a role may do in that domain, so every
48
+ // file that names a role adds to it.
49
+ for (const id in roles)
50
+ defaultRoles[id] = {
51
+ ...defaultRoles[id],
52
+ ...roles[id],
53
+ permissions: [
54
+ ...(defaultRoles[id]?.permissions ?? []),
55
+ ...(roles[id].permissions ?? []),
56
+ ],
57
+ };
58
+ await unlinkAsync(file);
59
+ }),
60
+ );
59
61
 
60
- const { body: roles } = await fact({
61
- name: "roles",
62
- domain: "principal",
63
- service: principal.service,
64
- network: principal.network,
65
- })
66
- .set({
67
- token: { internalFn: internalTokenFn, externalFn: externalTokenFn },
68
- context: {
69
- principal,
70
- network: process.env.NETWORK,
71
- },
62
+ const { body: roles } = await fact({
63
+ name: "roles",
64
+ domain: "principal",
65
+ service: principal.service,
66
+ network: principal.network,
72
67
  })
73
- .read();
74
-
75
- const permissions = await rolePermissions({
76
- roles,
77
- defaultRoles,
78
- context,
79
- customRolePermissionsFn: async ({ roleId }) => {
80
- const { body: permissions } = await fact({
81
- name: "permissions",
82
- domain: "role",
83
- service: "core",
84
- ...(process.env.CORE_NETWORK && {
85
- network: process.env.CORE_NETWORK,
86
- }),
68
+ .set({
69
+ token: { internalFn: internalTokenFn, externalFn: externalTokenFn },
70
+ context: {
71
+ principal,
72
+ network: process.env.NETWORK,
73
+ },
87
74
  })
88
- .set({
89
- token: {
90
- internalFn: internalTokenFn,
91
- externalFn: nodeExternalToken,
92
- },
93
- context: { principal, network: process.env.NETWORK },
75
+ .read();
76
+
77
+ const permissions = await rolePermissions({
78
+ roles,
79
+ defaultRoles,
80
+ context,
81
+ customRolePermissionsFn: async ({ roleId }) => {
82
+ const { body: permissions } = await fact({
83
+ name: "permissions",
84
+ domain: "role",
85
+ service: "core",
86
+ ...(process.env.CORE_NETWORK && {
87
+ network: process.env.CORE_NETWORK,
88
+ }),
94
89
  })
95
- .read({ query: { id: roleId } });
90
+ .set({
91
+ token: {
92
+ internalFn: internalTokenFn,
93
+ externalFn: nodeExternalToken,
94
+ },
95
+ context: { principal, network: process.env.NETWORK },
96
+ })
97
+ .read({ query: { id: roleId } });
96
98
 
97
- return permissions;
98
- },
99
- });
99
+ return permissions;
100
+ },
101
+ });
100
102
 
101
- await redis.writeObject(cacheKey, {
102
- permissions: JSON.stringify(permissions),
103
- });
104
- await redis.setExpiry(cacheKey, { seconds: FIVE_MINUTES_IN_SECONDS });
103
+ await redis.writeObject(cacheKey, {
104
+ permissions: JSON.stringify(permissions),
105
+ });
106
+ await redis.setExpiry(cacheKey, { seconds: FIVE_MINUTES_IN_SECONDS });
105
107
 
106
- return permissions;
107
- };
108
+ return permissions;
109
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blossm/permissions-lookup",
3
- "version": "0.0.182",
3
+ "version": "0.0.183",
4
4
  "description": "Looks up permissions for a role.",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/test/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { expect } from "chai";
2
2
  import { replace, restore } from "sinon";
3
+ import { writeFileSync } from "fs";
3
4
  import redis from "@blossm/redis";
4
5
  import permissionsLookup from "../index.js";
5
6
  import factDeps from "../../../../rpc/fact/deps.js";
@@ -12,9 +13,11 @@ const permission = { service: "core", domain: "document", privilege: "read" };
12
13
  describe("Contextual permission cache", () => {
13
14
  let network;
14
15
  let lookup;
16
+ let held;
15
17
  beforeEach(() => {
16
18
  network = process.env.NETWORK;
17
19
  process.env.NETWORK = "example.test";
20
+ held = [{ id: "reader", subject: contextA }];
18
21
  const cache = new Map();
19
22
  replace(redis, "readObject", async (key) => cache.get(key));
20
23
  replace(redis, "writeObject", async (key, value) => cache.set(key, value));
@@ -23,10 +26,7 @@ describe("Contextual permission cache", () => {
23
26
  get: () => ({
24
27
  in: () => ({
25
28
  with: async () => ({
26
- body:
27
- name === "roles"
28
- ? [{ id: "reader", subject: contextA }]
29
- : [permission],
29
+ body: name === "roles" ? held : [permission],
30
30
  }),
31
31
  }),
32
32
  }),
@@ -54,6 +54,27 @@ describe("Contextual permission cache", () => {
54
54
  expect(await lookup({ principal })).to.deep.equal([permission]);
55
55
  expect(await lookup({ principal, context: contextB })).to.deep.equal([]);
56
56
  });
57
+ it("adds up what each file lets a role do", async () => {
58
+ held = [{ id: "admin", subject: contextA }];
59
+ const combined = permissionsLookup({
60
+ downloadFileFn: async ({ fileName, extension }) => {
61
+ writeFileSync(
62
+ fileName + extension,
63
+ "admin:\n title: Admin\n permissions:\n - core:group:update\n",
64
+ );
65
+ writeFileSync(
66
+ `${fileName}_1${extension}`,
67
+ "admin:\n permissions:\n - system:node:delete\n",
68
+ );
69
+ },
70
+ });
71
+ expect(
72
+ await combined({ principal, context: contextA }),
73
+ ).to.have.deep.members([
74
+ { service: "core", domain: "group", privilege: "update" },
75
+ { service: "system", domain: "node", privilege: "delete" },
76
+ ]);
77
+ });
57
78
  it("distinguishes the network whose roles are being resolved", async () => {
58
79
  expect(await lookup({ principal, context: contextA })).to.deep.equal([
59
80
  permission,