@cloud-copilot/iam-expand 0.1.3 → 0.1.5
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 +84 -20
- package/dist/cjs/cli.js +17 -9
- package/dist/cjs/cli.js.map +1 -1
- package/dist/cjs/cli_utils.d.ts +4 -1
- package/dist/cjs/cli_utils.d.ts.map +1 -1
- package/dist/cjs/cli_utils.js +10 -4
- package/dist/cjs/cli_utils.js.map +1 -1
- package/dist/cjs/expand.d.ts +3 -3
- package/dist/cjs/expand.d.ts.map +1 -1
- package/dist/cjs/expand.js +26 -15
- package/dist/cjs/expand.js.map +1 -1
- package/dist/cjs/expand_file.d.ts +11 -0
- package/dist/cjs/expand_file.d.ts.map +1 -0
- package/dist/cjs/expand_file.js +39 -0
- package/dist/cjs/expand_file.js.map +1 -0
- package/dist/esm/cli.js +18 -10
- package/dist/esm/cli.js.map +1 -1
- package/dist/esm/cli_utils.d.ts +4 -1
- package/dist/esm/cli_utils.d.ts.map +1 -1
- package/dist/esm/cli_utils.js +9 -3
- package/dist/esm/cli_utils.js.map +1 -1
- package/dist/esm/expand.d.ts +3 -3
- package/dist/esm/expand.d.ts.map +1 -1
- package/dist/esm/expand.js +26 -15
- package/dist/esm/expand.js.map +1 -1
- package/dist/esm/expand_file.d.ts +11 -0
- package/dist/esm/expand_file.d.ts.map +1 -0
- package/dist/esm/expand_file.js +36 -0
- package/dist/esm/expand_file.js.map +1 -0
- package/package.json +5 -3
- package/src/cli.ts +17 -10
- package/src/cli_utils.test.ts +26 -11
- package/src/cli_utils.ts +10 -4
- package/src/expand.test.ts +112 -106
- package/src/expand.ts +30 -19
- package/src/expand_file.test.ts +107 -0
- package/src/expand_file.ts +39 -0
package/src/expand.test.ts
CHANGED
|
@@ -10,48 +10,48 @@ beforeEach(() => {
|
|
|
10
10
|
})
|
|
11
11
|
|
|
12
12
|
describe("expand", () => {
|
|
13
|
-
it("should return an empty array when actionString is null", () => {
|
|
13
|
+
it("should return an empty array when actionString is null", async () => {
|
|
14
14
|
//Given actionString is null
|
|
15
15
|
const actionString = null
|
|
16
16
|
//When expand is called with actionString
|
|
17
|
-
const result = expandIamActions(actionString as any)
|
|
17
|
+
const result = await expandIamActions(actionString as any)
|
|
18
18
|
//Then result should be an empty array
|
|
19
19
|
expect(result).toEqual([])
|
|
20
20
|
})
|
|
21
21
|
|
|
22
|
-
it("should return '*' when actionString is '*' and
|
|
22
|
+
it("should return '*' when actionString is '*' and expandAsterisk is false", async () => {
|
|
23
23
|
//Given actionString is '*'
|
|
24
24
|
const actionString = '*'
|
|
25
25
|
//When expand is called with actionString
|
|
26
|
-
const result = expandIamActions(actionString)
|
|
26
|
+
const result = await expandIamActions(actionString)
|
|
27
27
|
//Then result should be '*'
|
|
28
28
|
expect(result).toEqual(['*'])
|
|
29
29
|
})
|
|
30
30
|
|
|
31
|
-
it('should return "*" when action string multiple
|
|
32
|
-
//Given actionString is multiple
|
|
31
|
+
it('should return "*" when action string multiple asterisks and expandAsterisk is false', async () => {
|
|
32
|
+
//Given actionString is multiple asterisks
|
|
33
33
|
const actionString = '***'
|
|
34
34
|
|
|
35
|
-
//And
|
|
36
|
-
const options = {
|
|
35
|
+
//And expandAsterisk is false
|
|
36
|
+
const options = { expandAsterisk: false }
|
|
37
37
|
|
|
38
38
|
//When expand is called with actionString and options
|
|
39
|
-
const result = expandIamActions(actionString, options)
|
|
39
|
+
const result = await expandIamActions(actionString, options)
|
|
40
40
|
|
|
41
41
|
//Then result should be '*'
|
|
42
42
|
expect(result).toEqual(['*'])
|
|
43
43
|
})
|
|
44
44
|
|
|
45
|
-
it("should expand all actions for all services when actionString is '*' and
|
|
45
|
+
it("should expand all actions for all services when actionString is '*' and expandAsterisk is true", async () => {
|
|
46
46
|
//Given actionString is '*'
|
|
47
47
|
const actionString = '*'
|
|
48
|
-
//And
|
|
49
|
-
const options = {
|
|
48
|
+
//And expandAsterisk is true
|
|
49
|
+
const options = { expandAsterisk: true }
|
|
50
50
|
//And there are services
|
|
51
|
-
vi.mocked(iamServiceKeys).
|
|
51
|
+
vi.mocked(iamServiceKeys).mockResolvedValue(['s3', 'ec2'])
|
|
52
52
|
|
|
53
53
|
//And there are actions for the services
|
|
54
|
-
vi.mocked(iamActionsForService).mockImplementation(service => {
|
|
54
|
+
vi.mocked(iamActionsForService).mockImplementation(async (service) => {
|
|
55
55
|
if(service === 's3') {
|
|
56
56
|
return ['action1', 'action2']
|
|
57
57
|
}
|
|
@@ -61,10 +61,8 @@ describe("expand", () => {
|
|
|
61
61
|
return []
|
|
62
62
|
})
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
|
|
66
64
|
//When expand is called with actionString and options
|
|
67
|
-
const result = expandIamActions(actionString, options)
|
|
65
|
+
const result = await expandIamActions(actionString, options)
|
|
68
66
|
//Then result should be an array of all actions for all services
|
|
69
67
|
expect(result.sort()).toEqual([
|
|
70
68
|
'ec2:action3',
|
|
@@ -74,116 +72,118 @@ describe("expand", () => {
|
|
|
74
72
|
])
|
|
75
73
|
})
|
|
76
74
|
|
|
77
|
-
it("should do a case insensitive match for the service in the action string", () => {
|
|
75
|
+
it("should do a case insensitive match for the service in the action string", async () => {
|
|
78
76
|
//Given actionString is 'S3:GetObject'
|
|
79
77
|
const actionString = 'S3:get*'
|
|
80
78
|
//And s3 service exists
|
|
81
|
-
vi.mocked(iamServiceExists).mockImplementation((s) => s === 's3')
|
|
79
|
+
vi.mocked(iamServiceExists).mockImplementation(async (s) => s === 's3')
|
|
82
80
|
//And there are matching actions
|
|
83
|
-
vi.mocked(iamActionsForService).
|
|
81
|
+
vi.mocked(iamActionsForService).mockResolvedValue(['GetObject'])
|
|
84
82
|
|
|
85
83
|
//When expand is called with actionString
|
|
86
|
-
const result = expandIamActions(actionString)
|
|
84
|
+
const result = await expandIamActions(actionString)
|
|
87
85
|
|
|
88
86
|
//Then result should be an array with the actionString
|
|
89
87
|
expect(result).toEqual(['s3:GetObject'])
|
|
90
88
|
})
|
|
91
89
|
|
|
92
90
|
describe("invalid action name", () => {
|
|
93
|
-
it('should return an action without wildcards if it is a valid action', () => {
|
|
91
|
+
it('should return an action without wildcards if it is a valid action', async () => {
|
|
94
92
|
//Given actionString contains a valid action
|
|
95
93
|
const actionString = 's3:getobject'
|
|
96
94
|
//And s3 the service exists
|
|
97
|
-
vi.mocked(iamServiceExists).
|
|
95
|
+
vi.mocked(iamServiceExists).mockResolvedValue(true)
|
|
98
96
|
//And the action does not
|
|
99
|
-
vi.mocked(iamActionExists).
|
|
100
|
-
vi.mocked(iamActionDetails).
|
|
97
|
+
vi.mocked(iamActionExists).mockResolvedValue(true)
|
|
98
|
+
vi.mocked(iamActionDetails).mockResolvedValue({name: 'GetObject'} as any)
|
|
101
99
|
|
|
102
100
|
//When expand is called with actionString
|
|
103
|
-
const result = expandIamActions(actionString)
|
|
101
|
+
const result = await expandIamActions(actionString)
|
|
104
102
|
|
|
105
103
|
//Then result should be an array with the actionString
|
|
106
104
|
expect(result).toEqual(['s3:GetObject'])
|
|
107
105
|
})
|
|
108
106
|
|
|
109
|
-
it("should remove an invalid action if invalidActionBehavior is Remove", () => {
|
|
107
|
+
it("should remove an invalid action if invalidActionBehavior is Remove", async () => {
|
|
110
108
|
//Given actionString contains an invalid action
|
|
111
109
|
const actionString = 's3:DoSomethingDumb'
|
|
112
110
|
//And invalidActionBehavior is Remove
|
|
113
111
|
const options = { invalidActionBehavior: InvalidActionBehavior.Remove }
|
|
114
112
|
//And s3 the service exists
|
|
115
|
-
vi.mocked(iamServiceExists).
|
|
113
|
+
vi.mocked(iamServiceExists).mockResolvedValue(true)
|
|
116
114
|
//And the action does not
|
|
117
|
-
vi.mocked(iamActionExists).
|
|
115
|
+
vi.mocked(iamActionExists).mockResolvedValue(false)
|
|
118
116
|
|
|
119
117
|
//When expand is called with actionString
|
|
120
|
-
const result = expandIamActions(actionString, options)
|
|
118
|
+
const result = await expandIamActions(actionString, options)
|
|
121
119
|
|
|
122
120
|
//Then result should be an array with the valid action
|
|
123
121
|
expect(result).toEqual([])
|
|
124
122
|
})
|
|
125
123
|
|
|
126
|
-
it("should include an invalid action if invalidActionBehavior is Include", () => {
|
|
124
|
+
it("should include an invalid action if invalidActionBehavior is Include", async () => {
|
|
127
125
|
//Given actionString contains an invalid action
|
|
128
126
|
const actionString = 's3:DoSomethingSilly'
|
|
129
127
|
//And invalidActionBehavior is Include
|
|
130
128
|
const options = { invalidActionBehavior: InvalidActionBehavior.Include }
|
|
131
129
|
//And s3 the service exists
|
|
132
|
-
vi.mocked(iamServiceExists).
|
|
130
|
+
vi.mocked(iamServiceExists).mockResolvedValue(true)
|
|
133
131
|
//And the action does not
|
|
134
|
-
vi.mocked(iamActionExists).
|
|
132
|
+
vi.mocked(iamActionExists).mockResolvedValue(false)
|
|
135
133
|
|
|
136
134
|
//When expand is called with actionString
|
|
137
|
-
const result = expandIamActions(actionString, options)
|
|
135
|
+
const result = await expandIamActions(actionString, options)
|
|
138
136
|
|
|
139
137
|
//Then result should be an array with the invalid action
|
|
140
138
|
expect(result).toEqual([actionString])
|
|
141
139
|
})
|
|
142
140
|
|
|
143
|
-
it('should throw an error if the action is invalid and invalidActionBehavior is Error', () => {
|
|
141
|
+
it('should throw an error if the action is invalid and invalidActionBehavior is Error', async () => {
|
|
144
142
|
//Given actionString contains an invalid action
|
|
145
143
|
const actionString = 's3:AbsurdlyInvalidAction'
|
|
146
144
|
//And invalidActionBehavior is Error
|
|
147
145
|
const options = { invalidActionBehavior: InvalidActionBehavior.Error }
|
|
148
146
|
//And s3 the service exists
|
|
149
|
-
vi.mocked(iamServiceExists).
|
|
147
|
+
vi.mocked(iamServiceExists).mockResolvedValue(true)
|
|
150
148
|
//And the action does not
|
|
151
|
-
vi.mocked(iamActionExists).
|
|
149
|
+
vi.mocked(iamActionExists).mockResolvedValue(false)
|
|
152
150
|
|
|
153
151
|
//When expand is called with actionString
|
|
154
152
|
//Then an error should be thrown
|
|
155
|
-
expect(
|
|
153
|
+
expect(
|
|
154
|
+
expandIamActions(actionString, options)
|
|
155
|
+
).rejects.toThrowError('Invalid action')
|
|
156
156
|
})
|
|
157
157
|
})
|
|
158
158
|
|
|
159
159
|
describe("when the actions string is in the wrong format", () => {
|
|
160
|
-
it("should return an empty array when there are too many parts and errorOnInvalidFormat is false", () => {
|
|
160
|
+
it("should return an empty array when there are too many parts and errorOnInvalidFormat is false", async () => {
|
|
161
161
|
//Given actionString is in the wrong format
|
|
162
162
|
const actionString = 's3:GetObject:Extra*'
|
|
163
163
|
//And errorOnInvalidFormat is false
|
|
164
164
|
const options = { errorOnInvalidFormat: false }
|
|
165
165
|
|
|
166
166
|
//When expand is called with actionString
|
|
167
|
-
const result = expandIamActions(actionString, options)
|
|
167
|
+
const result = await expandIamActions(actionString, options)
|
|
168
168
|
|
|
169
169
|
//Then result should be an empty array
|
|
170
170
|
expect(result).toEqual([])
|
|
171
171
|
})
|
|
172
172
|
|
|
173
|
-
it("should return an empty array when there are too few parts and errorOnInvalidFormat is false", () => {
|
|
173
|
+
it("should return an empty array when there are too few parts and errorOnInvalidFormat is false", async () => {
|
|
174
174
|
//Given actionString has no :
|
|
175
175
|
const actionString = 's3GetObject*'
|
|
176
176
|
//And errorOnInvalidFormat is false
|
|
177
177
|
const options = { errorOnInvalidFormat: false }
|
|
178
178
|
|
|
179
179
|
//When expand is called with actionString
|
|
180
|
-
const result = expandIamActions(actionString, options)
|
|
180
|
+
const result = await expandIamActions(actionString, options)
|
|
181
181
|
|
|
182
182
|
//Then result should be an empty array
|
|
183
183
|
expect(result).toEqual([])
|
|
184
184
|
})
|
|
185
185
|
|
|
186
|
-
it("should throw an error when there are too many parts and errorOnInvalidFormat is true", () => {
|
|
186
|
+
it("should throw an error when there are too many parts and errorOnInvalidFormat is true", async () => {
|
|
187
187
|
//Given actionString is in the wrong format
|
|
188
188
|
const actionString = 's3:GetObject:Extra*'
|
|
189
189
|
//And errorOnInvalidFormat is true
|
|
@@ -191,10 +191,12 @@ describe("expand", () => {
|
|
|
191
191
|
|
|
192
192
|
//When expand is called with actionString
|
|
193
193
|
//Then an error should be thrown
|
|
194
|
-
expect(
|
|
194
|
+
expect(
|
|
195
|
+
() => expandIamActions(actionString, options)
|
|
196
|
+
).rejects.toThrowError('Invalid action format')
|
|
195
197
|
})
|
|
196
198
|
|
|
197
|
-
it("should throw an error when there are too few parts and errorOnInvalidFormat is true", () => {
|
|
199
|
+
it("should throw an error when there are too few parts and errorOnInvalidFormat is true", async () => {
|
|
198
200
|
//Given actionString has no :
|
|
199
201
|
const actionString = 's3GetObject*'
|
|
200
202
|
//And errorOnInvalidFormat is true
|
|
@@ -202,25 +204,27 @@ describe("expand", () => {
|
|
|
202
204
|
|
|
203
205
|
//When expand is called with actionString
|
|
204
206
|
//Then an error should be thrown
|
|
205
|
-
expect(
|
|
207
|
+
expect(
|
|
208
|
+
() => expandIamActions(actionString, options)
|
|
209
|
+
).rejects.toThrowError('Invalid action format')
|
|
206
210
|
})
|
|
207
211
|
})
|
|
208
212
|
|
|
209
213
|
describe("when the service in the action string does not exist", () => {
|
|
210
|
-
it("should return an empty array when errorOnMissingService is false", () => {
|
|
214
|
+
it("should return an empty array when errorOnMissingService is false", async () => {
|
|
211
215
|
//Given actionString contains a service that does not exist
|
|
212
216
|
const actionString = 'fake:GetObject*'
|
|
213
217
|
//And errorOnMissingService is false
|
|
214
218
|
const options = { errorOnMissingService: false }
|
|
215
219
|
|
|
216
220
|
//When expand is called with actionString
|
|
217
|
-
const result = expandIamActions(actionString, options)
|
|
221
|
+
const result = await expandIamActions(actionString, options)
|
|
218
222
|
|
|
219
223
|
//Then result should be an empty array
|
|
220
224
|
expect(result).toEqual([])
|
|
221
225
|
})
|
|
222
226
|
|
|
223
|
-
it("should throw an error when errorOnMissingService is true", () => {
|
|
227
|
+
it("should throw an error when errorOnMissingService is true", async () => {
|
|
224
228
|
//Given actionString contains a service that does not exist
|
|
225
229
|
const actionString = 'fake:GetObject*'
|
|
226
230
|
//And errorOnMissingService is true
|
|
@@ -228,57 +232,59 @@ describe("expand", () => {
|
|
|
228
232
|
|
|
229
233
|
//When expand is called with actionString
|
|
230
234
|
//Then an error should be thrown
|
|
231
|
-
expect(
|
|
235
|
+
expect(
|
|
236
|
+
() => expandIamActions(actionString, options)
|
|
237
|
+
).rejects.toThrowError('Service not found')
|
|
232
238
|
})
|
|
233
239
|
})
|
|
234
240
|
|
|
235
241
|
describe("when the action string contains a wildcard for a service", () => {
|
|
236
|
-
it("should expand not expand the wildcard when
|
|
242
|
+
it("should expand not expand the wildcard when expandServiceAsterisk is false", async () => {
|
|
237
243
|
//Given actionString is 's3:*'
|
|
238
244
|
const actionString = 's3:*'
|
|
239
|
-
//And
|
|
240
|
-
const options = {
|
|
245
|
+
//And expandServiceAsterisk is false
|
|
246
|
+
const options = { expandServiceAsterisk: false }
|
|
241
247
|
//And s3 service exists
|
|
242
|
-
vi.mocked(iamServiceExists).
|
|
248
|
+
vi.mocked(iamServiceExists).mockResolvedValue(true)
|
|
243
249
|
//And there are matching actions
|
|
244
|
-
vi.mocked(iamActionsForService).
|
|
250
|
+
vi.mocked(iamActionsForService).mockResolvedValue(['GetObject', 'PutObject'])
|
|
245
251
|
|
|
246
252
|
//When expand is called with actionString
|
|
247
|
-
const result = expandIamActions(actionString, options)
|
|
253
|
+
const result = await expandIamActions(actionString, options)
|
|
248
254
|
|
|
249
255
|
//Then result should be an array with the original string
|
|
250
256
|
expect(result).toEqual([actionString])
|
|
251
257
|
})
|
|
252
258
|
|
|
253
|
-
it("should expand not expand the wildcard when there are multiple
|
|
254
|
-
//Given actionString has multiple
|
|
259
|
+
it("should expand not expand the wildcard when there are multiple asterisks and expandServiceAsterisk is false", async () => {
|
|
260
|
+
//Given actionString has multiple asterisks for the actions
|
|
255
261
|
const actionString = 's3:****'
|
|
256
|
-
//And
|
|
257
|
-
const options = {
|
|
262
|
+
//And expandServiceAsterisk is false
|
|
263
|
+
const options = { expandServiceAsterisk: false }
|
|
258
264
|
//And s3 service exists
|
|
259
|
-
vi.mocked(iamServiceExists).
|
|
265
|
+
vi.mocked(iamServiceExists).mockResolvedValue(true)
|
|
260
266
|
//And there are matching actions
|
|
261
|
-
vi.mocked(iamActionsForService).
|
|
267
|
+
vi.mocked(iamActionsForService).mockResolvedValue(['GetObject', 'PutObject'])
|
|
262
268
|
|
|
263
269
|
//When expand is called with actionString
|
|
264
|
-
const result = expandIamActions(actionString, options)
|
|
270
|
+
const result = await expandIamActions(actionString, options)
|
|
265
271
|
|
|
266
272
|
//Then result should be an array with the original string
|
|
267
273
|
expect(result).toEqual(['s3:*'])
|
|
268
274
|
})
|
|
269
275
|
|
|
270
|
-
it("should expand the wildcard when
|
|
276
|
+
it("should expand the wildcard when expandServiceAsterisk is true", async () => {
|
|
271
277
|
//Given actionString is 's3:*'
|
|
272
278
|
const actionString = 's3:*'
|
|
273
|
-
//And
|
|
274
|
-
const options = {
|
|
279
|
+
//And expandServiceAsterisk is true
|
|
280
|
+
const options = { expandServiceAsterisk: true }
|
|
275
281
|
//And s3 service exists
|
|
276
|
-
vi.mocked(iamServiceExists).
|
|
282
|
+
vi.mocked(iamServiceExists).mockResolvedValue(true)
|
|
277
283
|
//And there are matching actions
|
|
278
|
-
vi.mocked(iamActionsForService).
|
|
284
|
+
vi.mocked(iamActionsForService).mockResolvedValue(['GetObject', 'PutObject'])
|
|
279
285
|
|
|
280
286
|
//When expand is called with actionString
|
|
281
|
-
const result = expandIamActions(actionString, options)
|
|
287
|
+
const result = await expandIamActions(actionString, options)
|
|
282
288
|
|
|
283
289
|
//Then result should be an array of actions
|
|
284
290
|
expect(result).toEqual([
|
|
@@ -290,13 +296,13 @@ describe("expand", () => {
|
|
|
290
296
|
|
|
291
297
|
|
|
292
298
|
describe("when the action string contains wildcards", () => {
|
|
293
|
-
it('should expand the wildcard actions at the end', () => {
|
|
299
|
+
it('should expand the wildcard actions at the end', async () => {
|
|
294
300
|
//Given actionString is 's3:Get*'
|
|
295
301
|
const actionString = 's3:Get*'
|
|
296
302
|
//And s3 service exists
|
|
297
|
-
vi.mocked(iamServiceExists).
|
|
303
|
+
vi.mocked(iamServiceExists).mockResolvedValue(true)
|
|
298
304
|
//And there are matching actions
|
|
299
|
-
vi.mocked(iamActionsForService).
|
|
305
|
+
vi.mocked(iamActionsForService).mockResolvedValue([
|
|
300
306
|
'GetObject',
|
|
301
307
|
'GetObjectAcl',
|
|
302
308
|
'GetObjectTagging',
|
|
@@ -307,7 +313,7 @@ describe("expand", () => {
|
|
|
307
313
|
])
|
|
308
314
|
|
|
309
315
|
//When expand is called with actionString
|
|
310
|
-
const result = expandIamActions(actionString)
|
|
316
|
+
const result = await expandIamActions(actionString)
|
|
311
317
|
//Then result should be an array of actions
|
|
312
318
|
expect(result).toEqual([
|
|
313
319
|
's3:GetObject',
|
|
@@ -317,13 +323,13 @@ describe("expand", () => {
|
|
|
317
323
|
])
|
|
318
324
|
})
|
|
319
325
|
|
|
320
|
-
it('should expand the wildcard actions at the beginning', () => {
|
|
326
|
+
it('should expand the wildcard actions at the beginning', async () => {
|
|
321
327
|
//Given actionString is 's3:*Object'
|
|
322
328
|
const actionString = 's3:*Object'
|
|
323
329
|
//And s3 service exists
|
|
324
330
|
vi.mocked(iamServiceExists).mockReturnValue(true)
|
|
325
331
|
//And there are matching actions
|
|
326
|
-
vi.mocked(iamActionsForService).
|
|
332
|
+
vi.mocked(iamActionsForService).mockResolvedValue([
|
|
327
333
|
'GetObject',
|
|
328
334
|
'GetObjectAcl',
|
|
329
335
|
'GetObjectTagging',
|
|
@@ -334,7 +340,7 @@ describe("expand", () => {
|
|
|
334
340
|
])
|
|
335
341
|
|
|
336
342
|
//When expand is called with actionString
|
|
337
|
-
const result = expandIamActions(actionString)
|
|
343
|
+
const result = await expandIamActions(actionString)
|
|
338
344
|
//Then result should be an array of actions
|
|
339
345
|
expect(result).toEqual([
|
|
340
346
|
's3:GetObject',
|
|
@@ -342,13 +348,13 @@ describe("expand", () => {
|
|
|
342
348
|
])
|
|
343
349
|
})
|
|
344
350
|
|
|
345
|
-
it('should expand the wildcard actions in the middle', () => {
|
|
351
|
+
it('should expand the wildcard actions in the middle', async () => {
|
|
346
352
|
//Given actionString is 's3:Get*Tagging'
|
|
347
353
|
const actionString = 's3:Get*Tagging'
|
|
348
354
|
//And s3 service exists
|
|
349
|
-
vi.mocked(iamServiceExists).
|
|
355
|
+
vi.mocked(iamServiceExists).mockResolvedValue(true)
|
|
350
356
|
//And there are matching actions
|
|
351
|
-
vi.mocked(iamActionsForService).
|
|
357
|
+
vi.mocked(iamActionsForService).mockResolvedValue([
|
|
352
358
|
'GetObject',
|
|
353
359
|
'GetObjectAcl',
|
|
354
360
|
'GetObjectTagging',
|
|
@@ -360,7 +366,7 @@ describe("expand", () => {
|
|
|
360
366
|
])
|
|
361
367
|
|
|
362
368
|
//When expand is called with actionString
|
|
363
|
-
const result = expandIamActions(actionString)
|
|
369
|
+
const result = await expandIamActions(actionString)
|
|
364
370
|
//Then result should be an array of actions
|
|
365
371
|
expect(result).toEqual([
|
|
366
372
|
's3:GetObjectTagging',
|
|
@@ -368,13 +374,13 @@ describe("expand", () => {
|
|
|
368
374
|
])
|
|
369
375
|
})
|
|
370
376
|
|
|
371
|
-
it('should expand multiple wildcards', () => {
|
|
377
|
+
it('should expand multiple wildcards', async () => {
|
|
372
378
|
//Given actionString is 's3:Get*Tagging*'
|
|
373
379
|
const actionString = 's3:Get*Tagging*'
|
|
374
380
|
//And s3 service exists
|
|
375
|
-
vi.mocked(iamServiceExists).
|
|
381
|
+
vi.mocked(iamServiceExists).mockResolvedValue(true)
|
|
376
382
|
//And there are matching actions
|
|
377
|
-
vi.mocked(iamActionsForService).
|
|
383
|
+
vi.mocked(iamActionsForService).mockResolvedValue([
|
|
378
384
|
'GetObject',
|
|
379
385
|
'GetObjectAcl',
|
|
380
386
|
'GetObjectTagging',
|
|
@@ -388,7 +394,7 @@ describe("expand", () => {
|
|
|
388
394
|
])
|
|
389
395
|
|
|
390
396
|
//When expand is called with actionString
|
|
391
|
-
const result = expandIamActions(actionString)
|
|
397
|
+
const result = await expandIamActions(actionString)
|
|
392
398
|
//Then result should be an array of actions
|
|
393
399
|
expect(result).toEqual([
|
|
394
400
|
's3:GetObjectTagging',
|
|
@@ -400,27 +406,27 @@ describe("expand", () => {
|
|
|
400
406
|
})
|
|
401
407
|
|
|
402
408
|
describe("when actionStrings is an array", () => {
|
|
403
|
-
it("should return an empty array when actionStrings is an empty array", () => {
|
|
409
|
+
it("should return an empty array when actionStrings is an empty array", async () => {
|
|
404
410
|
//Given actionStrings is an empty array
|
|
405
411
|
const actionStrings: string[] = []
|
|
406
412
|
|
|
407
413
|
//When expand is called with actionStrings
|
|
408
|
-
const result = expandIamActions(actionStrings)
|
|
414
|
+
const result = await expandIamActions(actionStrings)
|
|
409
415
|
|
|
410
416
|
//Then result should be an empty array
|
|
411
417
|
expect(result).toEqual([])
|
|
412
418
|
})
|
|
413
419
|
|
|
414
|
-
it("should return an array of expanded actions when actionStrings is an array of action strings", () => {
|
|
420
|
+
it("should return an array of expanded actions when actionStrings is an array of action strings", async () => {
|
|
415
421
|
//Given actionStrings is an array of action strings
|
|
416
422
|
const actionStrings = [
|
|
417
423
|
's3:Get*',
|
|
418
424
|
'ec2:*Instances'
|
|
419
425
|
]
|
|
420
426
|
//And s3 and ec2 services exist
|
|
421
|
-
vi.mocked(iamServiceExists).
|
|
427
|
+
vi.mocked(iamServiceExists).mockResolvedValue(true)
|
|
422
428
|
//And there are actions for the services
|
|
423
|
-
vi.mocked(iamActionsForService).mockImplementation(service => {
|
|
429
|
+
vi.mocked(iamActionsForService).mockImplementation(async (service) => {
|
|
424
430
|
if(service === 's3') {
|
|
425
431
|
return ['GetObject', 'GetObjectTagging', 'PutObject', 'PutObjectTagging']
|
|
426
432
|
}
|
|
@@ -431,7 +437,7 @@ describe("expand", () => {
|
|
|
431
437
|
})
|
|
432
438
|
|
|
433
439
|
//When expand is called with actionStrings
|
|
434
|
-
const result = expandIamActions(actionStrings)
|
|
440
|
+
const result = await expandIamActions(actionStrings)
|
|
435
441
|
|
|
436
442
|
//Then result should be an array of expanded actions
|
|
437
443
|
expect(result.sort()).toEqual([
|
|
@@ -444,44 +450,44 @@ describe("expand", () => {
|
|
|
444
450
|
})
|
|
445
451
|
|
|
446
452
|
describe("distinct option", () => {
|
|
447
|
-
it('should return all values when distinct is false', () => {
|
|
453
|
+
it('should return all values when distinct is false', async () => {
|
|
448
454
|
//Given two action strings
|
|
449
455
|
const actionString = ['s3:Get*','s3:*Object']
|
|
450
456
|
//And s3 service exists
|
|
451
|
-
vi.mocked(iamServiceExists).
|
|
457
|
+
vi.mocked(iamServiceExists).mockResolvedValue(true)
|
|
452
458
|
//And there are matching actions
|
|
453
|
-
vi.mocked(iamActionsForService).
|
|
459
|
+
vi.mocked(iamActionsForService).mockResolvedValue(['GetObject', 'PutObject', 'GetOtherObject'])
|
|
454
460
|
|
|
455
461
|
//When expand is called with actionString and distinct is false
|
|
456
|
-
const result = expandIamActions(actionString, { distinct: false })
|
|
462
|
+
const result = await expandIamActions(actionString, { distinct: false })
|
|
457
463
|
|
|
458
464
|
//Then result should be an array of actions, even if they are duplicates
|
|
459
465
|
expect(result).toEqual(['s3:GetObject', 's3:GetOtherObject', 's3:GetObject', 's3:PutObject', 's3:GetOtherObject'])
|
|
460
466
|
})
|
|
461
467
|
|
|
462
|
-
it('should return only unique values when distinct is true, and maintain order', () => {
|
|
468
|
+
it('should return only unique values when distinct is true, and maintain order', async () => {
|
|
463
469
|
//Given two action strings
|
|
464
470
|
const actionString = ['s3:Get*','s3:*Object']
|
|
465
471
|
//And s3 service exists
|
|
466
|
-
vi.mocked(iamServiceExists).
|
|
472
|
+
vi.mocked(iamServiceExists).mockResolvedValue(true)
|
|
467
473
|
//And there are matching actions
|
|
468
|
-
vi.mocked(iamActionsForService).
|
|
474
|
+
vi.mocked(iamActionsForService).mockResolvedValue(['GetObject', 'PutObject', 'GetOtherObject'])
|
|
469
475
|
|
|
470
476
|
//When expand is called with actionStrings and distinct is true
|
|
471
|
-
const result = expandIamActions(actionString, { distinct: true })
|
|
477
|
+
const result = await expandIamActions(actionString, { distinct: true })
|
|
472
478
|
//Then result should be an array of unique actions
|
|
473
479
|
expect(result).toEqual(['s3:GetObject', 's3:GetOtherObject', 's3:PutObject'])
|
|
474
480
|
})
|
|
475
481
|
})
|
|
476
482
|
|
|
477
483
|
describe("sort option", () => {
|
|
478
|
-
it('should return values in the order they were expanded when sort is false', () => {
|
|
484
|
+
it('should return values in the order they were expanded when sort is false', async () => {
|
|
479
485
|
//Given two action strings
|
|
480
486
|
const actionString = ['s3:Get*','ec2:Describe*']
|
|
481
487
|
//And s3 service exists
|
|
482
|
-
vi.mocked(iamServiceExists).
|
|
488
|
+
vi.mocked(iamServiceExists).mockResolvedValue(true)
|
|
483
489
|
//And there are matching actions
|
|
484
|
-
vi.mocked(iamActionsForService).mockImplementation(service => {
|
|
490
|
+
vi.mocked(iamActionsForService).mockImplementation(async (service) => {
|
|
485
491
|
if(service === 's3') {
|
|
486
492
|
return ['GetObject', 'GetBucket']
|
|
487
493
|
}
|
|
@@ -492,18 +498,18 @@ describe("expand", () => {
|
|
|
492
498
|
})
|
|
493
499
|
|
|
494
500
|
//When expand is called with actionStrings and sort is false
|
|
495
|
-
const result = expandIamActions(actionString, { sort: false })
|
|
501
|
+
const result = await expandIamActions(actionString, { sort: false })
|
|
496
502
|
//Then result should be an array of actions in the order they were expanded
|
|
497
503
|
expect(result).toEqual(['s3:GetObject', 's3:GetBucket', 'ec2:DescribeInstances', 'ec2:DescribeVolumes'])
|
|
498
504
|
})
|
|
499
505
|
|
|
500
|
-
it('should return values sorted when sort is true', () => {
|
|
506
|
+
it('should return values sorted when sort is true', async () => {
|
|
501
507
|
//Given two action strings
|
|
502
508
|
const actionString = ['s3:Get*','ec2:Describe*']
|
|
503
509
|
//And s3 service exists
|
|
504
|
-
vi.mocked(iamServiceExists).
|
|
510
|
+
vi.mocked(iamServiceExists).mockResolvedValue(true)
|
|
505
511
|
//And there are matching actions
|
|
506
|
-
vi.mocked(iamActionsForService).mockImplementation(service => {
|
|
512
|
+
vi.mocked(iamActionsForService).mockImplementation(async (service) => {
|
|
507
513
|
if(service === 's3') {
|
|
508
514
|
return ['GetObject', 'GetBucket']
|
|
509
515
|
}
|
|
@@ -514,7 +520,7 @@ describe("expand", () => {
|
|
|
514
520
|
})
|
|
515
521
|
|
|
516
522
|
//When expand is called with actionStrings and sort is false
|
|
517
|
-
const result = expandIamActions(actionString, { sort: true })
|
|
523
|
+
const result = await expandIamActions(actionString, { sort: true })
|
|
518
524
|
//Then result should be an array of actions in the order they were expanded
|
|
519
525
|
expect(result).toEqual(['ec2:DescribeInstances', 'ec2:DescribeVolumes', 's3:GetBucket', 's3:GetObject'])
|
|
520
526
|
})
|