@cloud-copilot/iam-expand 0.1.9 → 0.1.11

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.
@@ -1,184 +0,0 @@
1
- import { beforeEach } from "node:test";
2
- import { describe, expect, it, vi } from "vitest";
3
- import { expandIamActions } from "./expand.js";
4
- import { expandJsonDocument } from "./expand_file.js";
5
-
6
- vi.mock('./expand.js')
7
-
8
- beforeEach(() => {
9
- vi.resetAllMocks()
10
- })
11
-
12
- describe('expand_file', () => {
13
- describe('expandJsonDocument', () => {
14
- it('should return a document without actions as is', async () => {
15
- // Given a document without actions
16
- const document = {
17
- "key": "value",
18
- "key2": ["value1", "value2"]
19
- }
20
-
21
- // When the document is expanded
22
- const result = await expandJsonDocument({}, document)
23
-
24
- // Then the document should be returned as is
25
- expect(result).toEqual(document)
26
- })
27
-
28
- it('should expand a string action', async () => {
29
- // Given a document with an action
30
- const document = {
31
- a: {
32
- b: {
33
- "Action": "s3:Get*"
34
- }
35
- }
36
- }
37
- vi.mocked(expandIamActions).mockResolvedValue(["s3:GetObject", "s3:GetBucket"])
38
-
39
- // When the document is expanded
40
- const result = await expandJsonDocument({}, document)
41
-
42
- // Then the action should be expanded
43
- const expected = JSON.parse(JSON.stringify(document))
44
- expected.a.b.Action = ["s3:GetObject", "s3:GetBucket"]
45
- expect(result).toEqual(expected)
46
- })
47
-
48
- it('should expand an array of string actions', async () => {
49
- // Given a document with an action
50
- const document = {
51
- a: {
52
- b: {
53
- "Action": ["s3:Get*", "s3:Put*"]
54
- }
55
- }
56
- }
57
- vi.mocked(expandIamActions).mockImplementation(async (actions, options) =>{
58
- return ["s3:GetObject", "s3:GetBucket", "s3:PutObject", "s3:PutBucket"]
59
- })
60
-
61
- // When the document is expanded
62
- const result = await expandJsonDocument({}, document)
63
-
64
- // Then the action should be expanded
65
- const expected = JSON.parse(JSON.stringify(document))
66
- expected.a.b.Action = ["s3:GetObject", "s3:GetBucket", "s3:PutObject", "s3:PutBucket"]
67
- expect(result).toEqual(expected)
68
- })
69
-
70
- it('should not expand an Action if it is an object', async () => {
71
- // Given a document with an action
72
- const document = {
73
- a: {
74
- b: {
75
- "Action": {
76
- "key": "value"
77
- }
78
- }
79
- }
80
- }
81
-
82
- // When the document is expanded
83
- const result = await expandJsonDocument({}, document)
84
-
85
- // Then the document should be returned as is
86
- expect(result).toEqual(document)
87
- })
88
-
89
- it('should not expand an Action if it is an array of numbers', async () => {
90
- // Given a document with an action
91
- const document = {
92
- a: {
93
- b: {
94
- "Action": [1, 2, 3]
95
- }
96
- }
97
- }
98
-
99
- // When the document is expanded
100
- const result = await expandJsonDocument({}, document)
101
-
102
- // Then the document should be returned as is
103
- expect(result).toEqual(document)
104
- })
105
-
106
- it('should expand a string NotAction', async () => {
107
- // Given a document with an action
108
- const document = {
109
- a: {
110
- b: {
111
- "NotAction": "s3:Get*"
112
- }
113
- }
114
- }
115
- vi.mocked(expandIamActions).mockResolvedValue(["s3:GetObject", "s3:GetBucket"])
116
-
117
- // When the document is expanded
118
- const result = await expandJsonDocument({}, document)
119
-
120
- // Then the action should be expanded
121
- const expected = JSON.parse(JSON.stringify(document))
122
- expected.a.b.NotAction = ["s3:GetObject", "s3:GetBucket"]
123
- expect(result).toEqual(expected)
124
- })
125
-
126
- it('should expand an array of string NotActions', async () => {
127
- // Given a document with an action
128
- const document = {
129
- a: {
130
- b: {
131
- "NotAction": ["s3:Get*", "s3:Put*"]
132
- }
133
- }
134
- }
135
- vi.mocked(expandIamActions).mockImplementation(async (actions, options) =>{
136
- return ["s3:GetObject", "s3:GetBucket", "s3:PutObject", "s3:PutBucket"]
137
- })
138
-
139
- // When the document is expanded
140
- const result = await expandJsonDocument({}, document)
141
-
142
- // Then the action should be expanded
143
- const expected = JSON.parse(JSON.stringify(document))
144
- expected.a.b.NotAction = ["s3:GetObject", "s3:GetBucket", "s3:PutObject", "s3:PutBucket"]
145
- expect(result).toEqual(expected)
146
- })
147
-
148
- it('should not expand a NotAction if it is an object', async () => {
149
- // Given a document with an action
150
- const document = {
151
- a: {
152
- b: {
153
- "NotAction": {
154
- "key": "value"
155
- }
156
- }
157
- }
158
- }
159
-
160
- // When the document is expanded
161
- const result = await expandJsonDocument({}, document)
162
-
163
- // Then the document should be returned as is
164
- expect(result).toEqual(document)
165
- })
166
-
167
- it('should not expand a NotAction if it is an array of numbers', async () => {
168
- // Given a document with an action
169
- const document = {
170
- a: {
171
- b: {
172
- "NotAction": [1, 2, 3]
173
- }
174
- }
175
- }
176
-
177
- // When the document is expanded
178
- const result = await expandJsonDocument({}, document)
179
-
180
- // Then the document should be returned as is
181
- expect(result).toEqual(document)
182
- })
183
- })
184
- })
@@ -1,39 +0,0 @@
1
- import { expandIamActions, ExpandIamActionsOptions } from "./expand.js"
2
-
3
- /**
4
- * Takes any JSON document and expands any Action found in the document
5
- *
6
- * @param options the options to use when expanding the actions
7
- * @param document the JSON document to expand
8
- * @param key the key of the current node in the document
9
- * @returns the expanded JSON document
10
- */
11
- export async function expandJsonDocument(options: Partial<ExpandIamActionsOptions>, document: any, key?: string): Promise<any> {
12
- if(key === 'Action' || key === 'NotAction') {
13
- if(typeof document === 'string') {
14
- return await expandIamActions(document, options)
15
- }
16
- if(Array.isArray(document) && document.length > 0 && typeof document[0] === 'string') {
17
- const value = await expandIamActions(document, {...options})
18
- return value
19
- }
20
- }
21
-
22
- if(Array.isArray(document)) {
23
- return Promise.all(document.map(async (item) => {
24
- return expandJsonDocument(options, item)
25
- }))
26
- }
27
-
28
- if(typeof document === 'object') {
29
- const keys = Object.keys(document)
30
- const newObject: any = {}
31
- for(const key of keys) {
32
- const value = document[key]
33
- newObject[key] = await expandJsonDocument(options, value, key)
34
- }
35
- return newObject
36
- }
37
-
38
- return document
39
- }
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from './expand.js';
package/src/stdin.ts DELETED
@@ -1,34 +0,0 @@
1
- import { stdin } from 'process';
2
- /**
3
- * Read from stdin until the stream ends, timeout, or an error occurs
4
- *
5
- * @returns the string input from stdin
6
- */
7
- export async function readStdin(readWait: number | undefined): Promise<string> {
8
- return new Promise((resolve, reject) => {
9
- // If the input is not a TTY, we are most likely receiving data from a pipe.
10
- const definitelyReceivingData = !process.stdin.isTTY
11
- if(!readWait || readWait <= 0) {
12
- readWait = definitelyReceivingData ? 10_000 : 20
13
- }
14
-
15
- let data = '';
16
- setTimeout(() => {
17
- if(data.length === 0) {
18
- resolve(data)
19
- }
20
- }, readWait)
21
-
22
- stdin.on('data', (chunk) => {
23
- data += chunk;
24
- });
25
-
26
- stdin.on('end', () => {
27
- resolve(data);
28
- });
29
-
30
- stdin.on('error', (err) => {
31
- reject(err);
32
- });
33
- });
34
- }
package/tsconfig.cjs.json DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
-
4
- "include": ["src/**/*"],
5
- "exclude": ["**/*.test.ts"],
6
-
7
- "compilerOptions": {
8
- "rootDir": "src",
9
- "outDir": "dist/cjs"
10
- }
11
- }
package/tsconfig.esm.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
-
4
- "include": ["src/**/*"],
5
- "exclude": ["**/*.test.ts"],
6
-
7
- "compilerOptions": {
8
- "target": "ES2020",
9
- "module": "ES2020",
10
- "moduleResolution": "node",
11
- "rootDir": "src",
12
- "outDir": "dist/esm"
13
- }
14
- }
package/tsconfig.json DELETED
@@ -1,22 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "module": "commonjs",
4
- "target": "es2022",
5
- "outDir": "dist",
6
- "rootDir": "src",
7
- "sourceMap": true,
8
- "strict": true,
9
- "declaration": true,
10
- "declarationMap": true,
11
- "lib": ["es2023", "DOM"],
12
- "noUnusedLocals": false,
13
- "noUnusedParameters": false,
14
- "noImplicitReturns": true,
15
- "noFallthroughCasesInSwitch": false,
16
- "experimentalDecorators": true,
17
- "emitDecoratorMetadata": true,
18
- "esModuleInterop": false,
19
- "forceConsistentCasingInFileNames": true,
20
- },
21
- "exclude": ["tests", "test", "dist", "bin", "**/bin", "**/dist", "node_modules", "cdk.out"]
22
- }