cedar-policy-core 4.10.0

Core implementation of the Cedar policy language
Documentation
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487

// These are sample policies for testing the Cedar parser.

@test_annotation("This is the annotation")
permit(
  principal == User::"alice",
  action == PhotoOp::"view",
  resource == Photo::"VacationPhoto94.jpg"
);

@anno1("first annotation")
@anno2("second annotation")
permit(
  principal in Group::"jane_friends",
  action == PhotoOp::"view",
  resource == Photo::"VacationPhoto94.jpg"
);

permit(
  principal == User::"alice",
  action == PhotoOp::"view",
  resource in Album::"jane_vacation"
);

permit(
  principal == User::"alice",
  action in [PhotoOp::"view", PhotoOp::"edit", PhotoOp::"delete"],
  resource in Album::"jane_vacation"
);

// Alice has "admin" permissions on the album
permit(
  principal == User::"alice",
  action in PhotoflashRole::"admin",
  resource in Album::"jane_vacation"
);

// Solution #1: Using multiple policies
permit(
  principal == User::"alice",
  action in PhotoflashRole::"admin",
  resource in Album::"jane_vacation"
);

permit(
  principal == User::"alice",
  action == PhotoOp::"edit",
  resource in Album::"jane_vacation"
);

// Solution #2: Using conditions in a single policy.
// Note - depending on the implementation of a backend datastore,
// shifting rules into the conditions may result in changes to
// performance or search/lookup capabilities, as the condition clauses
// can be less amenable to indexing.
permit(
  principal == User::"alice",
  action,
  resource in Album::"jane_vacation"
)
when {
  action in PhotoflashRole::"viewer" ||
  action == PhotoOp::"edit"
};

permit(
  principal:User,
  action == PhotoOp::"view",
  resource in Album::"jane_vacation"
);

permit(
  principal,
  action == PhotoOp::"view",
  resource in Album::"jane_vacation"
);

permit(
  principal == User::"alice",
  action in [AccountOp::"listAlbums", AlbumOp::"listPhotos", PhotoOp::"view"],
  resource in Account::"jane"
);

permit(
  principal == User::"alice",
  action,
  resource:Photo in Account::"jane"
);

permit(
  principal:User,
  action in [AlbumOp::"listPhotos", PhotoOp::"view"],
  resource in Album::"device_prototypes"
)
when {
  principal.department == "HardwareEngineering" &&
  principal.jobLevel >= 10 &&
  (context.time.now - principal.hireDate).getDays() >= 365
};

permit(
  principal == User::"alice",
  action == PhotoOp::"view",
  resource:Photo
)
when {
  resource.fileType == "JPEG" &&
  (context.time.now - resource.creationDate).getDays() <= 7
};

permit(
  principal == User::"alice",
  action,
  resource
)
when {
  action.readOnly &&
  action.appliesTo in [Photo, Album]
};

permit(
  principal,
  action == PhotoOp::"view",
  resource
)
when {
  principal.department == resource.department
};

permit(
  principal,
  action,
  resource
)
when {
  principal == resource.owner
};

permit(
  principal,
  action in PhotoOp::"view",
  resource:Photo
)
when {
  principal.department == resource.owner.department
};

permit(
  principal,
  action,
  resource
)
when {
  principal == resource.owner ||
  (resource has admins && principal in resource.admins)
};

permit(
  principal == User::"alice",
  action in PhotoflashRole::"viewer",
  resource in Album::"vacation"
)
when {
  context.http.source_ip.inIpRange(ip("192.0.2.0/24")) &&
  context.authentication.acr in ["phr", "phrh"]
};

permit(
  principal,
  action == AlbumOp::"addPhoto",
  resource in Account::"jane"
)
when {
  (context.photo.filetype in ["JPEG", "PNG"] &&
   context.photo.filesize_mb <= 1)
  ||
  (context.photo.filetype == "RAW" &&
   context.photo.filesize_mb <= 100 &&
   principal in Group::"AVTeam")
};

forbid (
  principal == User::"alice",
  action,
  resource
)
unless {
  action.readOnly
}
when {
  context.time.now < timestamp("2022-01-01T10:15:00.021-05:00")
};

forbid (
  principal,
  action,
  resource in Account::"jane"
)
unless {
  context.http.authentication.acr == "phrh" &&
  (context.time.now - context.authentication.time).getMinutes() < 30
};

permit (
  principal == User::"alice",
  action in PhotoflashRole::"viewer",
  resource in Account::"jane"
)
advice {
  "Anonymize"
};

permit (
  principal == User::"alice",
  action in PhotoflashRole::"viewer",
  resource in Account::"jane"
)
advice {
  "{\"type\":\"PhotoFilterInstruction\", \"anonymize\":true}"
};

forbid (
  principal == User::"alice",
  action,
  resource
)
unless {
  action.readOnly
}
when {
  context.time.now < timestamp("2022-01-01T10:15:00.021-05:00")
}
advice {
  "Go to sleep! Remember to feed the cat in the morning."
};

// Allow everyone who used MFA
permit (
  principal,
  action,
  resource in Account::"jane"
)
when {
  context.authentication.usedMFA
};

// If no MFA and before Jan 1, allow with a warning
permit (
  principal,
  action,
  resource in Account::"jane"
)
when {
  context.time.now < timestamp("2022-01-01T00:00:00.000-05:00") &&
  !context.authentication.usedMFA
}
advice {
  "Notice: MFA will be required starting January 1"
};

// If no MFA and after Jan 1, block with an error message
forbid (
  principal,
  action,
  resource in Account::"jane"
)
when {
  context.time.now >= timestamp("2022-01-01T00:00:00.000-05:00") &&
  !context.authentication.usedMFA
}
advice {
  "MFA is required starting January 1"
};

permit(
  principal == User::"alice",
  action == TableOp::"read",
  resource in Database::"photoflash_data_lake"
);

permit(
  principal == User::"alice",
  action == TableOp::"read",
  resource == Table::"customers"
)
advice {
  "[hash(EmailAddress),
    filter(SubscriptionLevel, \"Enterprise\"),
    filter(Country, \"US\"]"
};

permit(
  principal == User::"alice",
  action == TableOp::"read",
  resource == Table::"customers"
)
when {
  ["customer_id", "creation_date", "subscription_level"].containsAll(context.query.columns)
};

permit (
  principal == User::"alice",
  action == TableOp::"read",
  resource == Table::"customers"
)
unless {
  context.query.columns.containsAny(["email_address", "billing_address"])
};

permit (
  principal,
  action == Table::"query",
  resource in Database::"photoflash_data_lake"
)
unless {
  context.query has pii_columns &&
  context.query.pii_columns.size() > 0 &&
  !principal.approved_for_pii
};

permit (
  principal:User,
  action in [ForumOp::"post", ForumOp::"comment"],
  resource in Forum::"nature_photography"
);

permit (
  principal in Group::"Forum.NaturePhotography.Member",
  action in [ForumOp::"post", ForumOp::"comment"],
  resource in Forum::"nature_photography"
);

// The owner of every forum can add up to N = 5 new custom roles
// for their forum.
permit(
  principal,
  action == Forum::"addRole",
  resource:Forum
)
when {
  principal == resource.owner &&
  size(resource.roles) <= 5
};

// The owner of the "Nature Photography" forum can add / remove members of
// the "SeniorModerator" role as long as the total number of
// Senior Moderators is at most 10.
permit(
  principal,
  action in [ForumOp::"addMemberToRole", ForumOp::"removeMemberFromRole"],
  resource == Forum::"nature_photography"
)
when {
  principal == resource.owner &&
  context.role == "SeniorModerator" &&
  (action == ForumOp::"removeMemberFromRole" || size(resource.roles["SeniorModerator"]) < 10)
};

// The owner and Senior Moderators can add / remove members of the "JuniorModerator"
// role, as long as the total number of Junior Moderators is at most 10.
permit(
  principal,
  action in [ForumOp::"addMemberToRole", ForumOp::"removeMemberFromRole"],
  resource == Forum::"nature_photography"
)
when {
  (principal == resource.owner || principal in resource.roles["SeniorModerator"]) &&
  context.role == "JuniorModerator" &&
  (action == ForumOp::"removeMemberFromRole" || size(resource.roles["JuniorModerator"]) < 10)
};

// Both JuniorModerators and SeniorModerators can remove themselves
// from the role.
permit(
  principal,
  action == ForumOp::"removeMemberFromRole",
  resource == Forum::"nature_photography"
)
when {
  context.removedMember == principal &&
  context.role in ["SeniorModerator", "JuniorModerator"]
};

// JuniorModerators can delete forum posts
permit(
  principal,
  action == ForumOp::"deletePost",
  resource == Forum::"nature_photography"
)
when {
  principal in resource.roles["JuniorModerator"]
};

// SeniorModerators can delete forum posts and update the forum configuration
permit(
  principal,
  action in [ForumOp::"deletePost", ForumOp::"updateConfiguration"],
  resource == Forum::"nature_photography"
)
when {
  principal in resource.roles["SeniorModerator"]
};

permit (
  principal,
  action,
  resource == Album::{uid: "772358b3-de11-42dc-8681-f0a32e34aab8"}
);

permit (
  principal,
  action,
  resource == Album::"772358b3-de11-42dc-8681-f0a32e34aab8"
);

permit (
  principal,
  action,
  resource == Album::{displayName: "vacation_photos"}
);

permit (
  principal,
  action,
  resource == Album::"vacation_photos" //Requires custom resolver
);

permit (
  principal,
  action,
  resource == Album::{uid: "772358b3-de11-42dc-8681-f0a32e34aab8",
                      displayName: "vacation_photos"}
);

permit (
  principal == IAM::User::{awsAccountId: "123456789012", userName: "alice"},
  action,
  resource
);

permit (
  principal == IAM::Principal::"arn:aws:iam::123456789012:user/alice",
  action,
  resource
);

permit (
  principal == IAM::Principal::"arn:aws:iam::12345678901:user/Dave",
  action == S3::Action::"PutObject",
  resource == S3::Bucket::"arn:aws:s3:::awsexamplebucket1"
);

permit (
  principal == IAM::Principal::"arn:aws:iam::12345678901:user/Dave",
  action in [S3::Action::"CreateBucket",
             S3::Action::"ListAllMyBuckets",
             S3::Action::"GetBucketLocation"],
  resource in Account::"12345678901"
);

permit (
  principal == IAM::Principal::"arn:aws:iam::12345678901:user/Dave",
  action in [S3::Action::"GetObjectVersion",
             S3::Action::"GetBucketAcl"],
  resource in S3::Bucket::"DOC-EXAMPLE-BUCKET1"
);

permit (
  principal == IAM::Principal::"arn:aws:iam::12345678901:user/Dave",
  action in [S3::Action::"GetObjectVersion",
             S3::Action::"GetBucketAcl"],
  resource in S3::Bucket::"DOC-EXAMPLE-BUCKET1"
);

forbid (
  principal == IAM::Principal::"arn:aws:iam::12345678901:user/Dave",
  action in [S3::Action::"DeleteObject",
            S3::Action::"DeleteObjectVersion",
            S3::Action::"PutLifecycleConfiguration"],
  resource in S3::Bucket::"DOC-EXAMPLE-BUCKET1"
);

permit (
  principal == IAM::Principal::"arn:aws:iam::12345678901:user/Dave",
  action == S3::Action::"GetAccountPublicAccessBlock",
  resource == Account::"12345678901"
);