-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathfunctionurl_test.go
More file actions
376 lines (356 loc) · 11.6 KB
/
functionurl_test.go
File metadata and controls
376 lines (356 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package lambroll_test
import (
"encoding/json"
"testing"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/fujiwara/lambroll"
"github.com/go-test/deep"
)
var permissionsTestCases = []struct {
subject string
statementJSON []byte
expectedPrincipal *string
expectedPrincipalOrgID *string
expectedSourceArn *string
}{
{
subject: "AuthType NONE",
statementJSON: []byte(`{
"Action": "lambda:InvokeFunctionUrl",
"Condition": {
"StringEquals": {
"lambda:FunctionUrlAuthType": "NONE"
}
},
"Effect": "Allow",
"Principal": "*",
"Resource": "arn:aws:lambda:ap-northeast-1:123456789012:function:hello",
"Sid": "lambroll-8f4ec83e623a309d9ca15db9276da30b2129be9c"
}`),
expectedPrincipal: aws.String("*"),
expectedPrincipalOrgID: nil,
},
{
subject: "AuthType AWS_IAM with Principal OrgID",
statementJSON: []byte(`{
"Sid": "lambroll-622ed5c2bb0714ef0af1929fcea568e4ba0c4dbe",
"Effect": "Allow",
"Principal": "*",
"Action": "lambda:InvokeFunctionUrl",
"Resource": "arn:aws:lambda:ap-northeast-1:1234567890:function:hello",
"Condition": {
"StringEquals": {
"lambda:FunctionUrlAuthType": "AWS_IAM",
"aws:PrincipalOrgID": "o-xxxxxxxxxx"
}
}
}`),
expectedPrincipal: aws.String("*"),
expectedPrincipalOrgID: aws.String("o-xxxxxxxxxx"),
},
{
subject: "AuthType AWS_IAM with Principal",
statementJSON: []byte(`{
"Action": "lambda:InvokeFunctionUrl",
"Condition": {
"StringEquals": {
"lambda:FunctionUrlAuthType": "AWS_IAM"
}
},
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::123456789012:root"
},
"Resource": "arn:aws:lambda:ap-northeast-1:123456789012:function:hello",
"Sid": "lambroll-3b135eca4b14335775cda9f947966093a57d270f"
}`),
expectedPrincipal: aws.String("123456789012"),
expectedPrincipalOrgID: nil,
},
{
subject: "AuthType AWS_IAM with Principal CF OAC",
statementJSON: []byte(`{
"Action": "lambda:InvokeFunctionUrl",
"Condition": {
"ArnLike": {
"aws:SourceArn": "arn:aws:cloudfront::123456789012:distribution/ABCDEFG12345678"
}
},
"Effect": "Allow",
"Principal": {
"Service": "cloudfront.amazonaws.com"
},
"Resource": "arn:aws:lambda:ap-northeast-1:123456789012:function:hello",
"Sid": "lambroll-3b135eca4b14335775cda9f947966093a57d270f"
}`),
expectedPrincipal: aws.String("cloudfront.amazonaws.com"),
expectedPrincipalOrgID: nil,
expectedSourceArn: aws.String("arn:aws:cloudfront::123456789012:distribution/ABCDEFG12345678"),
},
}
func TestParseStatement(t *testing.T) {
for _, c := range permissionsTestCases {
st := &lambroll.PolicyStatement{}
if err := json.Unmarshal(c.statementJSON, st); err != nil {
t.Errorf("%s failed to unmarshal json: %s", c.subject, err)
}
if diff := deep.Equal(c.expectedPrincipal, st.PrincipalString()); diff != nil {
t.Errorf("%s PrincipalString diff %s", c.subject, diff)
}
if diff := deep.Equal(c.expectedPrincipalOrgID, st.PrincipalOrgID()); diff != nil {
t.Errorf("%s PrincipalOrgID diff %s", c.subject, diff)
}
if diff := deep.Equal(c.expectedSourceArn, st.SourceArn()); diff != nil {
t.Errorf("%s SourceArn diff %s", c.subject, diff)
}
}
}
func TestFunctionURLPermission_Equals(t *testing.T) {
tests := []struct {
name string
p1 *lambroll.FunctionURLPermission
p2 *lambroll.FunctionURLPermission
want bool
}{
{
name: "identical permissions",
p1: &lambroll.FunctionURLPermission{
Principal: aws.String("*"),
PrincipalOrgID: aws.String("o-xxxxxxxxxx"),
SourceArn: aws.String("arn:aws:cloudfront::123456789012:distribution/ABCDEFG12345678"),
SourceAccount: aws.String("123456789012"),
},
p2: &lambroll.FunctionURLPermission{
Principal: aws.String("*"),
PrincipalOrgID: aws.String("o-xxxxxxxxxx"),
SourceArn: aws.String("arn:aws:cloudfront::123456789012:distribution/ABCDEFG12345678"),
SourceAccount: aws.String("123456789012"),
},
want: true,
},
{
name: "different principal",
p1: &lambroll.FunctionURLPermission{
Principal: aws.String("*"),
},
p2: &lambroll.FunctionURLPermission{
Principal: aws.String("123456789012"),
},
want: false,
},
{
name: "different principal org ID",
p1: &lambroll.FunctionURLPermission{
Principal: aws.String("*"),
PrincipalOrgID: aws.String("o-aaaaaaaaaa"),
},
p2: &lambroll.FunctionURLPermission{
Principal: aws.String("*"),
PrincipalOrgID: aws.String("o-bbbbbbbbbb"),
},
want: false,
},
{
name: "different source arn",
p1: &lambroll.FunctionURLPermission{
Principal: aws.String("*"),
SourceArn: aws.String("arn:aws:cloudfront::123456789012:distribution/AAA"),
},
p2: &lambroll.FunctionURLPermission{
Principal: aws.String("*"),
SourceArn: aws.String("arn:aws:cloudfront::123456789012:distribution/BBB"),
},
want: false,
},
{
name: "nil vs empty string principal",
p1: &lambroll.FunctionURLPermission{
Principal: nil,
},
p2: &lambroll.FunctionURLPermission{
Principal: aws.String(""),
},
want: true,
},
{
name: "both nil principals",
p1: &lambroll.FunctionURLPermission{
Principal: nil,
},
p2: &lambroll.FunctionURLPermission{
Principal: nil,
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.p1.Equals(tt.p2)
if got != tt.want {
t.Errorf("Equals() = %v, want %v", got, tt.want)
}
})
}
}
func TestFunctionURLPermission_AddPermissionInputs(t *testing.T) {
fc := &lambroll.FunctionURL{
Config: &lambroll.FunctionURLConfig{
FunctionName: aws.String("test-function"),
Qualifier: aws.String("1"),
AuthType: "AWS_IAM",
},
}
tests := []struct {
name string
permission *lambroll.FunctionURLPermission
validate func(t *testing.T, perms []*lambda.AddPermissionInput)
}{
{
name: "basic permission without actualSids",
permission: &lambroll.FunctionURLPermission{
Principal: aws.String("*"),
PrincipalOrgID: aws.String("o-xxxxxxxxxx"),
},
validate: func(t *testing.T, perms []*lambda.AddPermissionInput) {
// Should return 2 permissions
if len(perms) != 2 {
t.Errorf("expected 2 permissions, got %d", len(perms))
return
}
// First permission: InvokeFunctionUrl
p1 := perms[0]
if aws.ToString(p1.Action) != "lambda:InvokeFunctionUrl" {
t.Errorf("first permission action = %v, want lambda:InvokeFunctionUrl", aws.ToString(p1.Action))
}
if aws.ToString(p1.FunctionName) != "test-function" {
t.Errorf("first permission FunctionName = %v, want test-function", aws.ToString(p1.FunctionName))
}
if aws.ToString(p1.Qualifier) != "1" {
t.Errorf("first permission Qualifier = %v, want 1", aws.ToString(p1.Qualifier))
}
if aws.ToString(p1.Principal) != "*" {
t.Errorf("first permission Principal = %v, want *", aws.ToString(p1.Principal))
}
if aws.ToString(p1.PrincipalOrgID) != "o-xxxxxxxxxx" {
t.Errorf("first permission PrincipalOrgID = %v, want o-xxxxxxxxxx", aws.ToString(p1.PrincipalOrgID))
}
if p1.FunctionUrlAuthType != "AWS_IAM" {
t.Errorf("first permission FunctionUrlAuthType = %v, want AWS_IAM", p1.FunctionUrlAuthType)
}
if p1.StatementId == nil {
t.Error("first permission StatementId is nil")
}
// Second permission: InvokeFunction
p2 := perms[1]
if aws.ToString(p2.Action) != "lambda:InvokeFunction" {
t.Errorf("second permission action = %v, want lambda:InvokeFunction", aws.ToString(p2.Action))
}
if aws.ToString(p2.FunctionName) != "test-function" {
t.Errorf("second permission FunctionName = %v, want test-function", aws.ToString(p2.FunctionName))
}
if aws.ToString(p2.Qualifier) != "1" {
t.Errorf("second permission Qualifier = %v, want 1", aws.ToString(p2.Qualifier))
}
if aws.ToString(p2.Principal) != "*" {
t.Errorf("second permission Principal = %v, want *", aws.ToString(p2.Principal))
}
if !aws.ToBool(p2.InvokedViaFunctionUrl) {
t.Error("second permission InvokedViaFunctionUrl should be true")
}
if p2.StatementId == nil {
t.Error("second permission StatementId is nil")
}
// StatementIds should be different
if aws.ToString(p1.StatementId) == aws.ToString(p2.StatementId) {
t.Errorf("StatementIds should be different, both are %v", aws.ToString(p1.StatementId))
}
},
},
{
name: "permission with actualSids",
permission: &lambroll.FunctionURLPermission{
Principal: aws.String("*"),
// actualSids would be set internally
},
validate: func(t *testing.T, perms []*lambda.AddPermissionInput) {
if len(perms) != 2 {
t.Errorf("expected 2 permissions, got %d", len(perms))
return
}
// Both should have auto-generated StatementIds
if perms[0].StatementId == nil || perms[1].StatementId == nil {
t.Error("StatementIds should not be nil")
}
},
},
{
name: "permission with SourceArn and SourceAccount",
permission: &lambroll.FunctionURLPermission{
Principal: aws.String("cloudfront.amazonaws.com"),
SourceArn: aws.String("arn:aws:cloudfront::123456789012:distribution/ABCDEFG"),
SourceAccount: aws.String("123456789012"),
},
validate: func(t *testing.T, perms []*lambda.AddPermissionInput) {
if len(perms) != 2 {
t.Errorf("expected 2 permissions, got %d", len(perms))
return
}
for i, p := range perms {
if aws.ToString(p.SourceArn) != "arn:aws:cloudfront::123456789012:distribution/ABCDEFG" {
t.Errorf("permission[%d] SourceArn = %v, want arn:aws:cloudfront::123456789012:distribution/ABCDEFG",
i, aws.ToString(p.SourceArn))
}
if aws.ToString(p.SourceAccount) != "123456789012" {
t.Errorf("permission[%d] SourceAccount = %v, want 123456789012",
i, aws.ToString(p.SourceAccount))
}
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
perms := tt.permission.AddPermissionInputs(fc)
tt.validate(t, perms)
})
}
}
func TestFunctionURLPermission_AddPermissionInputs_WithActualSids(t *testing.T) {
fc := &lambroll.FunctionURL{
Config: &lambroll.FunctionURLConfig{
FunctionName: aws.String("test-function"),
Qualifier: aws.String("1"),
AuthType: "AWS_IAM",
},
}
// Create permission (actualSids would be set internally when loading from remote)
// Since actualSids is a private field, we test that StatementIds are deterministic
// by generating permissions multiple times and verifying the SIDs match
permission := &lambroll.FunctionURLPermission{
Principal: aws.String("*"),
}
perms1 := permission.AddPermissionInputs(fc)
perms2 := permission.AddPermissionInputs(fc)
if len(perms1) != 2 || len(perms2) != 2 {
t.Fatalf("expected 2 permissions in each call, got %d and %d", len(perms1), len(perms2))
}
// StatementIds should be deterministic (same permission content = same SID)
for i := range 2 {
if aws.ToString(perms1[i].StatementId) != aws.ToString(perms2[i].StatementId) {
t.Errorf("StatementId[%d] not deterministic: %v != %v",
i, aws.ToString(perms1[i].StatementId), aws.ToString(perms2[i].StatementId))
}
}
// Verify StatementIds follow the expected format (lambroll-<sha1>)
for i, p := range perms1 {
sid := aws.ToString(p.StatementId)
if len(sid) != 49 { // "lambroll-" (9) + 40 hex chars
t.Errorf("permission[%d] StatementId length = %d, want 49 (lambroll- prefix + 40 char hash)",
i, len(sid))
}
if sid[:9] != "lambroll-" {
t.Errorf("permission[%d] StatementId should start with 'lambroll-', got %s", i, sid[:9])
}
}
}