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 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985
/*
* Copyright 2022-2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//! Defines structures for entity type and action id information used by the
//! validator. The contents of these structures should be populated from and schema
//! with a few transformations applied to the data. Specifically, the
//! `member_of` relation from the schema is reversed and the transitive closure is
//! computed to obtain a `descendants` relation.
use std::collections::{hash_map::Entry, HashMap, HashSet};
use std::sync::Arc;
use cedar_policy_core::{
ast::{Eid, Entity, EntityType, EntityUID, Id, Name, RestrictedExpr},
entities::{Entities, JSONValue, TCComputation},
parser::err::ParseErrors,
transitive_closure::{compute_tc, TCNode},
FromNormalizedStr,
};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use smol_str::SmolStr;
use crate::types::OpenTag;
use crate::{
schema_file_format,
types::{AttributeType, Attributes, EntityRecordKind, Type},
ActionEntityUID, ActionType, SchemaFragment, SchemaType, SchemaTypeVariant, TypeOfAttribute,
SCHEMA_TYPE_VARIANT_TAGS,
};
use super::err::*;
use super::NamespaceDefinition;
/// The current schema format specification does not include multiple action entity
/// types. All action entities are required to use a single `Action` entity
/// type. However, the action entity type may be namespaced, so an action entity
/// may have a fully qualified entity type `My::Namespace::Action`.
/// This string must be parsable by as an entity type name.
pub(crate) static ACTION_ENTITY_TYPE: &str = "Action";
#[test]
fn action_entity_type_parses() {
Id::from_normalized_str(ACTION_ENTITY_TYPE).unwrap();
}
/// Return true when an entity type is an action entity type. This compares the
/// base name for the type, so this will return true for any entity type named
/// `Action` regardless of namespaces.
pub(crate) fn is_action_entity_type(ty: &Name) -> bool {
ty.basename().as_ref() == ACTION_ENTITY_TYPE
}
// We do not have a dafny model for action attributes, so we disable them by defualt.
#[derive(Eq, PartialEq, Copy, Clone, Default)]
pub enum ActionBehavior {
/// Action entities cannot have attributes. Attempting to declare attributes
/// will result in a error when constructing the schema.
#[default]
ProhibitAttributes,
/// Action entities may have attributes.
PermitAttributes,
}
/// A single namespace definition from the schema json processed into a form
/// which is closer to that used by the validator. The processing includes
/// detection of some errors, for example, parse errors in entity type names or
/// entity type which are declared multiple times. This does not detect
/// references to undeclared entity types because any entity type may be
/// declared in a different fragment that will only be known about when building
/// the complete `ValidatorSchema`.
#[derive(Debug)]
pub struct ValidatorNamespaceDef {
/// The namespace declared for the schema fragment. We track a namespace for
/// fragments because they have at most one namespace that is applied
/// everywhere. It would be less useful to track all namespaces for a
/// complete schema.
namespace: Option<Name>,
/// Preprocessed common type definitions which can be used to define entity
/// type attributes and action contexts.
type_defs: TypeDefs,
/// The preprocessed entity type declarations from the schema fragment json.
entity_types: EntityTypesDef,
/// The preprocessed action declarations from the schema fragment json.
actions: ActionsDef,
}
/// Holds a map from `Name`s of common type definitions to their corresponding
/// `Type`.
#[derive(Debug)]
pub struct TypeDefs {
type_defs: HashMap<Name, Type>,
}
/// Entity type declarations held in a `ValidatorNamespaceDef`. Entity type
/// parents and attributes may reference undeclared entity types.
#[derive(Debug)]
pub struct EntityTypesDef {
entity_types: HashMap<Name, EntityTypeFragment>,
}
/// Defines an EntityType where we have not resolved typedefs occurring in the
/// attributes or verified that the parent entity types and entity types
/// occurring in attributes are defined.
#[derive(Debug)]
pub struct EntityTypeFragment {
/// The attributes record type for this entity type. The type is wrapped in
/// a `WithUnresolvedTypeDefs` because it may contain typedefs which are not
/// defined in this schema fragment. All entity type `Name` keys in this map
/// are declared in this schema fragment.
attributes: WithUnresolvedTypeDefs<Type>,
/// The direct parent entity types for this entity type come from the
/// `memberOfTypes` list. These types might be declared in a different
/// namespace, so we will check if they are declared in any fragment when
/// constructing a `ValidatorSchema`.
parents: HashSet<Name>,
}
/// Action declarations held in a `ValidatorNamespaceDef`. Entity types
/// referenced here do not need to be declared in the schema.
#[derive(Debug)]
pub struct ActionsDef {
actions: HashMap<EntityUID, ActionFragment>,
}
#[derive(Debug)]
pub struct ActionFragment {
/// The type of the context record for this actions. The types is wrapped in
/// a `WithUnresolvedTypeDefs` because it may refer to common types which
/// are not defined in this fragment.
context: WithUnresolvedTypeDefs<Type>,
/// The principals and resources that an action can be applied to.
applies_to: ValidatorApplySpec,
/// The direct parent action entities for this action.
parents: HashSet<EntityUID>,
/// The types for the attributes defined for this actions entity.
attribute_types: Attributes,
/// The values for the attributes defined for this actions entity, stored
/// separately so that we can later extract use these values to construct
/// the actual `Entity` objects defined by the schema.
attributes: HashMap<SmolStr, RestrictedExpr>,
}
type ResolveFunc<T> = dyn FnOnce(&HashMap<Name, Type>) -> Result<T>;
/// Represent a type that might be defined in terms of some type definitions
/// which are not necessarily available in the current namespace.
pub enum WithUnresolvedTypeDefs<T> {
WithUnresolved(Box<ResolveFunc<T>>),
WithoutUnresolved(T),
}
impl<T: 'static> WithUnresolvedTypeDefs<T> {
pub fn new(f: impl FnOnce(&HashMap<Name, Type>) -> Result<T> + 'static) -> Self {
Self::WithUnresolved(Box::new(f))
}
pub fn map<U: 'static>(self, f: impl FnOnce(T) -> U + 'static) -> WithUnresolvedTypeDefs<U> {
match self {
Self::WithUnresolved(_) => {
WithUnresolvedTypeDefs::new(|type_defs| self.resolve_type_defs(type_defs).map(f))
}
Self::WithoutUnresolved(v) => WithUnresolvedTypeDefs::WithoutUnresolved(f(v)),
}
}
/// Instantiate any names referencing types with the definition of the type
/// from the input HashMap.
pub fn resolve_type_defs(self, type_defs: &HashMap<Name, Type>) -> Result<T> {
match self {
WithUnresolvedTypeDefs::WithUnresolved(f) => f(type_defs),
WithUnresolvedTypeDefs::WithoutUnresolved(v) => Ok(v),
}
}
}
impl<T: 'static> From<T> for WithUnresolvedTypeDefs<T> {
fn from(value: T) -> Self {
Self::WithoutUnresolved(value)
}
}
impl<T: std::fmt::Debug> std::fmt::Debug for WithUnresolvedTypeDefs<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
WithUnresolvedTypeDefs::WithUnresolved(_) => f.debug_tuple("WithUnresolved").finish(),
WithUnresolvedTypeDefs::WithoutUnresolved(v) => {
f.debug_tuple("WithoutUnresolved").field(v).finish()
}
}
}
}
impl TryInto<ValidatorNamespaceDef> for NamespaceDefinition {
type Error = SchemaError;
fn try_into(self) -> Result<ValidatorNamespaceDef> {
ValidatorNamespaceDef::from_namespace_definition(None, self, ActionBehavior::default())
}
}
impl ValidatorNamespaceDef {
// We need to treat this as if it had `pub(crate)` visibility to avoid sharing
// the file format. However, our fuzzing library currently needs it to be public.
/// Construct a new `ValidatorSchema` from the underlying `SchemaFragment`.
pub fn from_namespace_definition(
namespace: Option<SmolStr>,
namespace_def: NamespaceDefinition,
action_behavior: ActionBehavior,
) -> Result<ValidatorNamespaceDef> {
// Check that each entity types and action is only declared once.
let mut e_types_ids: HashSet<SmolStr> = HashSet::new();
for name in namespace_def.entity_types.keys() {
if !e_types_ids.insert(name.clone()) {
// insert returns false for duplicates
return Err(SchemaError::DuplicateEntityType(name.to_string()));
}
}
let mut a_name_eids: HashSet<SmolStr> = HashSet::new();
for name in namespace_def.actions.keys() {
if !a_name_eids.insert(name.clone()) {
// insert returns false for duplicates
return Err(SchemaError::DuplicateAction(name.to_string()));
}
}
let schema_namespace = match namespace.as_deref() {
None => None,
Some("") => None, // we consider "" to be the same as the empty namespace for this purpose
Some(ns) => {
Some(Name::from_normalized_str(ns).map_err(SchemaError::NamespaceParseError)?)
}
};
// Return early with an error if actions cannot be in groups or have
// attributes, but the schema contains action groups or attributes.
Self::check_action_behavior(&namespace_def, action_behavior)?;
// Convert the type defs, actions and entity types from the schema file
// into the representation used by the validator.
let type_defs =
Self::build_type_defs(namespace_def.common_types, schema_namespace.as_ref())?;
let actions = Self::build_action_ids(namespace_def.actions, schema_namespace.as_ref())?;
let entity_types =
Self::build_entity_types(namespace_def.entity_types, schema_namespace.as_ref())?;
Ok(ValidatorNamespaceDef {
namespace: schema_namespace,
type_defs,
entity_types,
actions,
})
}
fn is_builtin_type_name(name: &SmolStr) -> bool {
SCHEMA_TYPE_VARIANT_TAGS
.iter()
.any(|type_name| name == type_name)
}
fn build_type_defs(
schema_file_type_def: HashMap<SmolStr, SchemaType>,
schema_namespace: Option<&Name>,
) -> Result<TypeDefs> {
let type_defs = schema_file_type_def
.into_iter()
.map(|(name_str, schema_ty)| -> Result<_> {
if Self::is_builtin_type_name(&name_str) {
return Err(SchemaError::DuplicateCommonType(name_str.to_string()));
}
let name = Self::parse_unqualified_name_with_namespace(
&name_str,
schema_namespace.cloned(),
)
.map_err(SchemaError::CommonTypeParseError)?;
let ty = Self::try_schema_type_into_validator_type(schema_namespace, schema_ty)?
.resolve_type_defs(&HashMap::new())?;
Ok((name, ty))
})
.collect::<Result<HashMap<_, _>>>()?;
Ok(TypeDefs { type_defs })
}
// Transform the schema data structures for entity types into the structures
// used internally by the validator. This is mostly accomplished by directly
// copying data between fields.
fn build_entity_types(
schema_files_types: HashMap<SmolStr, schema_file_format::EntityType>,
schema_namespace: Option<&Name>,
) -> Result<EntityTypesDef> {
Ok(EntityTypesDef {
entity_types: schema_files_types
.into_iter()
.map(|(name_str, entity_type)| -> Result<_> {
let name = Self::parse_unqualified_name_with_namespace(
&name_str,
schema_namespace.cloned(),
)
.map_err(SchemaError::EntityTypeParseError)?;
let parents = entity_type
.member_of_types
.iter()
.map(|parent| -> Result<_> {
Self::parse_possibly_qualified_name_with_default_namespace(
parent,
schema_namespace,
)
.map_err(SchemaError::EntityTypeParseError)
})
.collect::<Result<HashSet<_>>>()?;
let attributes = Self::try_schema_type_into_validator_type(
schema_namespace,
entity_type.shape.into_inner(),
)?;
Ok((
name,
EntityTypeFragment {
attributes,
parents,
},
))
})
.collect::<Result<HashMap<_, _>>>()?,
})
}
// Helper to get types from JSONValues. Currently doesn't support all
// JSONValue types. Note: If this function is extended to cover move
// `JSONValue`s, we must update `convert_attr_jsonval_map_to_attributes` to
// handle errors that may occur when parsing these values. This will require
// a breaking change in the `SchemaError` type in the public API.
fn jsonval_to_type_helper(v: &JSONValue, action_id: &EntityUID) -> Result<Type> {
match v {
JSONValue::Bool(_) => Ok(Type::primitive_boolean()),
JSONValue::Long(_) => Ok(Type::primitive_long()),
JSONValue::String(_) => Ok(Type::primitive_string()),
JSONValue::Record(r) => {
let mut required_attrs: HashMap<SmolStr, Type> = HashMap::new();
for (k, v_prime) in r {
let t = Self::jsonval_to_type_helper(v_prime, action_id);
match t {
Ok(ty) => required_attrs.insert(k.clone(), ty),
Err(e) => return Err(e),
};
}
Ok(Type::record_with_required_attributes(
required_attrs,
OpenTag::ClosedAttributes,
))
}
JSONValue::Set(v) => match v.get(0) {
//sets with elements of different types will be rejected elsewhere
None => Err(SchemaError::ActionAttributesContainEmptySet(
action_id.clone(),
)),
Some(element) => {
let element_type = Self::jsonval_to_type_helper(element, action_id);
match element_type {
Ok(t) => Ok(Type::Set {
element_type: Some(Box::new(t)),
}),
Err(_) => element_type,
}
}
},
_ => Err(SchemaError::UnsupportedActionAttributeType(
action_id.clone(),
)),
}
}
//Convert jsonval map to attributes
fn convert_attr_jsonval_map_to_attributes(
m: HashMap<SmolStr, JSONValue>,
action_id: &EntityUID,
) -> Result<(Attributes, HashMap<SmolStr, RestrictedExpr>)> {
let mut attr_types: HashMap<SmolStr, Type> = HashMap::new();
let mut attr_values: HashMap<SmolStr, RestrictedExpr> = HashMap::new();
for (k, v) in m {
let t = Self::jsonval_to_type_helper(&v, action_id);
match t {
Ok(ty) => attr_types.insert(k.clone(), ty),
Err(e) => return Err(e),
};
// As an artifact of the limited `JSONValue` variants accepted by
// `Self::jsonval_to_type_helper`, we know that this function will
// never error. Also note that this is only ever executed when
// action attributes are enabled, but they cannot be enabled when
// using Cedar through the public API. This is fortunate because
// handling an error here would mean adding a new error variant to
// `SchemaError` in the public API, but we didn't make that enum
// `non_exhaustive`, so any new variants are a breaking change.
// PANIC SAFETY: see above
#[allow(clippy::expect_used)]
let e = v.into_expr().expect("`Self::jsonval_to_type_helper` will always return `Err` for a `JSONValue` that might make `into_expr` return `Err`");
attr_values.insert(k.clone(), e);
}
Ok((
Attributes::with_required_attributes(attr_types),
attr_values,
))
}
// Transform the schema data structures for actions into the structures used
// internally by the validator. This is mostly accomplished by directly
// copying data between fields.
fn build_action_ids(
schema_file_actions: HashMap<SmolStr, ActionType>,
schema_namespace: Option<&Name>,
) -> Result<ActionsDef> {
Ok(ActionsDef {
actions: schema_file_actions
.into_iter()
.map(|(action_id_str, action_type)| -> Result<_> {
let action_id = Self::parse_action_id_with_namespace(
&ActionEntityUID::default_type(action_id_str),
schema_namespace,
)?;
let (principal_types, resource_types, context) = action_type
.applies_to
.map(|applies_to| {
(
applies_to.principal_types,
applies_to.resource_types,
applies_to.context,
)
})
.unwrap_or_default();
// Convert the entries in the `appliesTo` lists into sets of
// `EntityTypes`. If one of the lists is `None` (absent from the
// schema), then the specification is undefined.
let applies_to = ValidatorApplySpec::new(
Self::parse_apply_spec_type_list(principal_types, schema_namespace)?,
Self::parse_apply_spec_type_list(resource_types, schema_namespace)?,
);
let context = Self::try_schema_type_into_validator_type(
schema_namespace,
context.into_inner(),
)?;
let parents = action_type
.member_of
.unwrap_or_default()
.iter()
.map(|parent| -> Result<_> {
Self::parse_action_id_with_namespace(parent, schema_namespace)
})
.collect::<Result<HashSet<_>>>()?;
let (attribute_types, attributes) =
Self::convert_attr_jsonval_map_to_attributes(
action_type.attributes.unwrap_or_default(),
&action_id,
)?;
Ok((
action_id,
ActionFragment {
context,
applies_to,
parents,
attribute_types,
attributes,
},
))
})
.collect::<Result<HashMap<_, _>>>()?,
})
}
// Check that `schema_file` uses actions in a way consistent with the
// specified `action_behavior`. When the behavior specifies that actions
// should not be used in groups and should not have attributes, then this
// function will return `Err` if it sees any action groups or attributes
// declared in the schema.
fn check_action_behavior(
schema_file: &NamespaceDefinition,
action_behavior: ActionBehavior,
) -> Result<()> {
if schema_file
.entity_types
.iter()
// The `name` in an entity type declaration cannot be qualified
// with a namespace (it always implicitly takes the schema
// namespace), so we do this comparison directly.
.any(|(name, _)| name == ACTION_ENTITY_TYPE)
{
return Err(SchemaError::ActionEntityTypeDeclared);
}
if action_behavior == ActionBehavior::ProhibitAttributes {
let mut actions_with_attributes: Vec<String> = Vec::new();
for (name, a) in &schema_file.actions {
if a.attributes.is_some() {
actions_with_attributes.push(name.to_string());
}
}
if !actions_with_attributes.is_empty() {
return Err(SchemaError::ActionHasAttributes(actions_with_attributes));
}
}
Ok(())
}
/// Given the attributes for an entity type or action context as written in
/// a schema file, convert the types of the attributes into the `Type` data
/// structure used by the typechecker, and return the result as a map from
/// attribute name to type.
fn parse_record_attributes(
schema_namespace: Option<&Name>,
attrs: impl IntoIterator<Item = (SmolStr, TypeOfAttribute)>,
) -> Result<WithUnresolvedTypeDefs<Attributes>> {
let attrs_with_type_defs = attrs
.into_iter()
.map(|(attr, ty)| -> Result<_> {
Ok((
attr,
(
Self::try_schema_type_into_validator_type(schema_namespace, ty.ty)?,
ty.required,
),
))
})
.collect::<Result<Vec<_>>>()?;
Ok(WithUnresolvedTypeDefs::new(|typ_defs| {
attrs_with_type_defs
.into_iter()
.map(|(s, (attr_ty, is_req))| {
attr_ty
.resolve_type_defs(typ_defs)
.map(|ty| (s, AttributeType::new(ty, is_req)))
})
.collect::<Result<Vec<_>>>()
.map(Attributes::with_attributes)
}))
}
/// Take an optional list of entity type name strings from an action apply
/// spec and parse it into a set of `Name`s for those entity types. If any
/// of the entity type names cannot be parsed, then the `Err` case is
/// returned, and it will indicate which name did not parse.
fn parse_apply_spec_type_list(
types: Option<Vec<SmolStr>>,
namespace: Option<&Name>,
) -> Result<HashSet<EntityType>> {
types
.map(|types| {
types
.iter()
// Parse each type name string into a `Name`, generating an
// `EntityTypeParseError` when the string is not a valid
// name.
.map(|ty_str| {
Ok(EntityType::Concrete(
Self::parse_possibly_qualified_name_with_default_namespace(
ty_str, namespace,
)
.map_err(SchemaError::EntityTypeParseError)?,
))
})
// Fail if any of the types failed.
.collect::<Result<HashSet<_>>>()
})
.unwrap_or_else(|| Ok(HashSet::from([EntityType::Unspecified])))
}
// Parse a `Name` from a string (possibly including namespaces). If it is
// not qualified with any namespace, then apply the default namespace to
// create a qualified name. Do not modify any existing namespace on the
// type.
pub(crate) fn parse_possibly_qualified_name_with_default_namespace(
name_str: &SmolStr,
default_namespace: Option<&Name>,
) -> std::result::Result<Name, ParseErrors> {
let name = Name::from_normalized_str(name_str)?;
let qualified_name = if name.namespace_components().next().is_some() {
// The name is already qualified. Don't touch it.
name
} else {
// The name does not have a namespace, so qualify the type to
// use the default.
match default_namespace {
Some(namespace) => {
Name::type_in_namespace(name.basename().clone(), namespace.clone())
}
None => name,
}
};
Ok(qualified_name)
}
/// Parse a name from a string into the `Id` (basename only). Then
/// initialize the namespace for this type with the provided namespace vec
/// to create the qualified `Name`.
fn parse_unqualified_name_with_namespace(
type_name: impl AsRef<str>,
namespace: Option<Name>,
) -> std::result::Result<Name, ParseErrors> {
let type_name = Id::from_normalized_str(type_name.as_ref())?;
match namespace {
Some(namespace) => Ok(Name::type_in_namespace(type_name, namespace)),
None => Ok(Name::unqualified_name(type_name)),
}
}
/// Take an action identifier as a string and use it to construct an
/// EntityUID for that action. The entity type of the action will always
/// have the base type `Action`. The type will be qualified with any
/// namespace provided in the `namespace` argument or with the namespace
/// inside the ActionEntityUID if one is present.
fn parse_action_id_with_namespace(
action_id: &ActionEntityUID,
namespace: Option<&Name>,
) -> Result<EntityUID> {
let namespaced_action_type = if let Some(action_ty) = &action_id.ty {
Self::parse_possibly_qualified_name_with_default_namespace(action_ty, namespace)
.map_err(SchemaError::EntityTypeParseError)?
} else {
// PANIC SAFETY: The constant ACTION_ENTITY_TYPE is valid entity type.
#[allow(clippy::expect_used)]
let id = Id::from_normalized_str(ACTION_ENTITY_TYPE).expect(
"Expected that the constant ACTION_ENTITY_TYPE would be a valid entity type.",
);
match namespace {
Some(namespace) => Name::type_in_namespace(id, namespace.clone()),
None => Name::unqualified_name(id),
}
};
Ok(EntityUID::from_components(
namespaced_action_type,
Eid::new(action_id.id.clone()),
))
}
/// Implemented to convert a type as written in the schema json format into the
/// `Type` type used by the validator. Conversion can fail if an entity or
/// record attribute name is invalid. It will also fail for some types that can
/// be written in the schema, but are not yet implemented in the typechecking
/// logic.
pub(crate) fn try_schema_type_into_validator_type(
default_namespace: Option<&Name>,
schema_ty: SchemaType,
) -> Result<WithUnresolvedTypeDefs<Type>> {
match schema_ty {
SchemaType::Type(SchemaTypeVariant::String) => Ok(Type::primitive_string().into()),
SchemaType::Type(SchemaTypeVariant::Long) => Ok(Type::primitive_long().into()),
SchemaType::Type(SchemaTypeVariant::Boolean) => Ok(Type::primitive_boolean().into()),
SchemaType::Type(SchemaTypeVariant::Set { element }) => Ok(
Self::try_schema_type_into_validator_type(default_namespace, *element)?
.map(Type::set),
),
SchemaType::Type(SchemaTypeVariant::Record {
attributes,
additional_attributes,
}) => {
if additional_attributes {
Err(SchemaError::UnsupportedFeature(
UnsupportedFeature::OpenRecordsAndEntities,
))
} else {
Ok(
Self::parse_record_attributes(default_namespace, attributes)?.map(
|attrs| Type::record_with_attributes(attrs, OpenTag::ClosedAttributes),
),
)
}
}
SchemaType::Type(SchemaTypeVariant::Entity { name }) => {
let entity_type_name = Self::parse_possibly_qualified_name_with_default_namespace(
&name,
default_namespace,
)
.map_err(SchemaError::EntityTypeParseError)?;
Ok(Type::named_entity_reference(entity_type_name).into())
}
SchemaType::Type(SchemaTypeVariant::Extension { name }) => {
let extension_type_name = Name::from_normalized_str(&name)
.map_err(SchemaError::ExtensionTypeParseError)?;
Ok(Type::extension(extension_type_name).into())
}
SchemaType::TypeDef { type_name } => {
let defined_type_name = Self::parse_possibly_qualified_name_with_default_namespace(
&type_name,
default_namespace,
)
.map_err(SchemaError::CommonTypeParseError)?;
Ok(WithUnresolvedTypeDefs::new(move |typ_defs| {
typ_defs.get(&defined_type_name).cloned().ok_or(
SchemaError::UndeclaredCommonTypes(HashSet::from([type_name.to_string()])),
)
}))
}
}
}
/// Access the `Name` for the namespace of this definition.
pub fn namespace(&self) -> &Option<Name> {
&self.namespace
}
}
#[derive(Debug)]
pub struct ValidatorSchemaFragment(Vec<ValidatorNamespaceDef>);
impl TryInto<ValidatorSchemaFragment> for SchemaFragment {
type Error = SchemaError;
fn try_into(self) -> Result<ValidatorSchemaFragment> {
ValidatorSchemaFragment::from_schema_fragment(self, ActionBehavior::default())
}
}
impl ValidatorSchemaFragment {
pub fn from_namespaces(namespaces: impl IntoIterator<Item = ValidatorNamespaceDef>) -> Self {
Self(namespaces.into_iter().collect())
}
pub fn from_schema_fragment(
fragment: SchemaFragment,
action_behavior: ActionBehavior,
) -> Result<Self> {
Ok(Self(
fragment
.0
.into_iter()
.map(|(fragment_ns, ns_def)| {
ValidatorNamespaceDef::from_namespace_definition(
Some(fragment_ns),
ns_def,
action_behavior,
)
})
.collect::<Result<Vec<_>>>()?,
))
}
/// Access the `Name`s for the namespaces in this fragment.
pub fn namespaces(&self) -> impl Iterator<Item = &Option<Name>> {
self.0.iter().map(|d| d.namespace())
}
}
#[serde_as]
#[derive(Clone, Debug, Serialize)]
pub struct ValidatorSchema {
/// Map from entity type names to the ValidatorEntityType object.
#[serde(rename = "entityTypes")]
#[serde_as(as = "Vec<(_, _)>")]
entity_types: HashMap<Name, ValidatorEntityType>,
/// Map from action id names to the ValidatorActionId object.
#[serde(rename = "actionIds")]
#[serde_as(as = "Vec<(_, _)>")]
action_ids: HashMap<EntityUID, ValidatorActionId>,
}
impl std::str::FromStr for ValidatorSchema {
type Err = SchemaError;
fn from_str(s: &str) -> Result<Self> {
serde_json::from_str::<SchemaFragment>(s)?.try_into()
}
}
impl TryFrom<NamespaceDefinition> for ValidatorSchema {
type Error = SchemaError;
fn try_from(nsd: NamespaceDefinition) -> Result<ValidatorSchema> {
ValidatorSchema::from_schema_fragments([ValidatorSchemaFragment::from_namespaces([
nsd.try_into()?
])])
}
}
impl TryFrom<SchemaFragment> for ValidatorSchema {
type Error = SchemaError;
fn try_from(frag: SchemaFragment) -> Result<ValidatorSchema> {
ValidatorSchema::from_schema_fragments([frag.try_into()?])
}
}
impl ValidatorSchema {
// Create a ValidatorSchema without any entity types or actions ids.
pub fn empty() -> ValidatorSchema {
Self {
entity_types: HashMap::new(),
action_ids: HashMap::new(),
}
}
/// Construct a `ValidatorSchema` from a JSON value (which should be an
/// object matching the `SchemaFileFormat` shape).
pub fn from_json_value(json: serde_json::Value) -> Result<Self> {
Self::from_schema_file(
SchemaFragment::from_json_value(json)?,
ActionBehavior::default(),
)
}
/// Construct a `ValidatorSchema` directly from a file.
pub fn from_file(file: impl std::io::Read) -> Result<Self> {
Self::from_schema_file(SchemaFragment::from_file(file)?, ActionBehavior::default())
}
pub fn from_schema_file(
schema_file: SchemaFragment,
action_behavior: ActionBehavior,
) -> Result<ValidatorSchema> {
Self::from_schema_fragments([ValidatorSchemaFragment::from_schema_fragment(
schema_file,
action_behavior,
)?])
}
/// Construct a new `ValidatorSchema` from some number of schema fragments.
pub fn from_schema_fragments(
fragments: impl IntoIterator<Item = ValidatorSchemaFragment>,
) -> Result<ValidatorSchema> {
let mut type_defs = HashMap::new();
let mut entity_type_fragments = HashMap::new();
let mut action_fragments = HashMap::new();
for ns_def in fragments.into_iter().flat_map(|f| f.0.into_iter()) {
// Build aggregate maps for the declared typedefs, entity types, and
// actions, checking that nothing is defined twice. Namespaces were
// already added by the `ValidatorNamespaceDef`, so the same base
// type name may appear multiple times so long as the namespaces are
// different.
for (name, ty) in ns_def.type_defs.type_defs {
match type_defs.entry(name) {
Entry::Vacant(v) => v.insert(ty),
Entry::Occupied(o) => {
return Err(SchemaError::DuplicateCommonType(o.key().to_string()));
}
};
}
for (name, entity_type) in ns_def.entity_types.entity_types {
match entity_type_fragments.entry(name) {
Entry::Vacant(v) => v.insert(entity_type),
Entry::Occupied(o) => {
return Err(SchemaError::DuplicateEntityType(o.key().to_string()))
}
};
}
for (action_euid, action) in ns_def.actions.actions {
match action_fragments.entry(action_euid) {
Entry::Vacant(v) => v.insert(action),
Entry::Occupied(o) => {
return Err(SchemaError::DuplicateAction(o.key().to_string()))
}
};
}
}
// Invert the `parents` relation defined by entities and action so far
// to get a `children` relation.
let mut entity_children = HashMap::new();
for (name, entity_type) in entity_type_fragments.iter() {
for parent in entity_type.parents.iter() {
entity_children
.entry(parent.clone())
.or_insert_with(HashSet::new)
.insert(name.clone());
}
}
let mut entity_types = entity_type_fragments
.into_iter()
.map(|(name, entity_type)| -> Result<_> {
// Keys of the `entity_children` map were values of an
// `memberOfTypes` list, so they might not have been declared in
// their fragment. By removing entries from `entity_children`
// where the key is a declared name, we will be left with a map
// where the keys are undeclared. These keys are used to report
// an error when undeclared entity types are referenced inside a
// `memberOfTypes` list. The error is reported alongside the
// error for any other undeclared entity types by
// `check_for_undeclared`.
let descendants = entity_children.remove(&name).unwrap_or_default();
Ok((
name.clone(),
ValidatorEntityType {
name: name.clone(),
descendants,
attributes: Self::record_attributes_or_none(
entity_type.attributes.resolve_type_defs(&type_defs)?,
)
.ok_or(SchemaError::ContextOrShapeNotRecord(
ContextOrShape::EntityTypeShape(name),
))?,
},
))
})
.collect::<Result<HashMap<_, _>>>()?;
let mut action_children = HashMap::new();
for (euid, action) in action_fragments.iter() {
for parent in action.parents.iter() {
action_children
.entry(parent.clone())
.or_insert_with(HashSet::new)
.insert(euid.clone());
}
}
let mut action_ids = action_fragments
.into_iter()
.map(|(name, action)| -> Result<_> {
let descendants = action_children.remove(&name).unwrap_or_default();
Ok((
name.clone(),
ValidatorActionId {
name: name.clone(),
applies_to: action.applies_to,
descendants,
context: Self::record_attributes_or_none(
action.context.resolve_type_defs(&type_defs)?,
)
.ok_or(SchemaError::ContextOrShapeNotRecord(
ContextOrShape::ActionContext(name),
))?,
attribute_types: action.attribute_types,
attributes: action.attributes,
},
))
})
.collect::<Result<HashMap<_, _>>>()?;
// We constructed entity types and actions with child maps, but we need
// transitively closed descendants.
compute_tc(&mut entity_types, false)?;
// Pass `true` here so that we also check that the action hierarchy does
// not contain cycles.
compute_tc(&mut action_ids, true)?;
// Return with an error if there is an undeclared entity or action
// referenced in any fragment. `{entity,action}_children` are provided
// for the `undeclared_parent_{entities,actions}` arguments because
// removed keys from these maps as we encountered declarations for the
// entity types or actions. Any keys left in the map are therefore
// undeclared.
Self::check_for_undeclared(
&entity_types,
entity_children.into_keys(),
&action_ids,
action_children.into_keys(),
)?;
Ok(ValidatorSchema {
entity_types,
action_ids,
})
}
/// Check that all entity types and actions referenced in the schema are in
/// the set of declared entity type or action names. Point of caution: this
/// function assumes that all entity types are fully qualified. This is
/// handled by the `SchemaFragment` constructor.
fn check_for_undeclared(
entity_types: &HashMap<Name, ValidatorEntityType>,
undeclared_parent_entities: impl IntoIterator<Item = Name>,
action_ids: &HashMap<EntityUID, ValidatorActionId>,
undeclared_parent_actions: impl IntoIterator<Item = EntityUID>,
) -> Result<()> {
// When we constructed `entity_types`, we removed entity types from the
// `entity_children` map as we encountered a declaration for that type.
// Any entity types left in the map are therefore undeclared. These are
// any undeclared entity types which appeared in a `memberOf` list.
let mut undeclared_e = undeclared_parent_entities
.into_iter()
.map(|n| n.to_string())
.collect::<HashSet<_>>();
// Looking at entity types, we need to check entity references in
// attribute types. We already know that all elements of the
// `descendants` list were declared because the list is a result of
// inverting the `memberOf` relationship which mapped declared entity
// types to their parent entity types.
for entity_type in entity_types.values() {
for (_, attr_typ) in entity_type.attributes() {
Self::check_undeclared_in_type(
&attr_typ.attr_type,
entity_types,
&mut undeclared_e,
);
}
}
// Undeclared actions in a `memberOf` list.
let undeclared_a = undeclared_parent_actions
.into_iter()
.map(|n| n.to_string())
.collect::<HashSet<_>>();
// For actions, we check entity references in the context attribute
// types and `appliesTo` lists. See the `entity_types` loop for why the
// `descendants` list is not checked.
for action in action_ids.values() {
for (_, attr_typ) in action.context.iter() {
Self::check_undeclared_in_type(
&attr_typ.attr_type,
entity_types,
&mut undeclared_e,
);
}
for p_entity in action.applies_to.applicable_principal_types() {
match p_entity {
EntityType::Concrete(p_entity) => {
if !entity_types.contains_key(p_entity) {
undeclared_e.insert(p_entity.to_string());
}
}
EntityType::Unspecified => (),
}
}
for r_entity in action.applies_to.applicable_resource_types() {
match r_entity {
EntityType::Concrete(r_entity) => {
if !entity_types.contains_key(r_entity) {
undeclared_e.insert(r_entity.to_string());
}
}
EntityType::Unspecified => (),
}
}
}
if !undeclared_e.is_empty() {
return Err(SchemaError::UndeclaredEntityTypes(undeclared_e));
}
if !undeclared_a.is_empty() {
return Err(SchemaError::UndeclaredActions(undeclared_a));
}
Ok(())
}
fn record_attributes_or_none(ty: Type) -> Option<Attributes> {
match ty {
Type::EntityOrRecord(EntityRecordKind::Record { attrs, .. }) => Some(attrs),
_ => None,
}
}
// Check that all entity types appearing inside a type are in the set of
// declared entity types, adding any undeclared entity types to the
// `undeclared_types` set.
fn check_undeclared_in_type(
ty: &Type,
entity_types: &HashMap<Name, ValidatorEntityType>,
undeclared_types: &mut HashSet<String>,
) {
match ty {
Type::EntityOrRecord(EntityRecordKind::Entity(lub)) => {
for name in lub.iter() {
if !entity_types.contains_key(name) {
undeclared_types.insert(name.to_string());
}
}
}
Type::EntityOrRecord(EntityRecordKind::Record { attrs, .. }) => {
for (_, attr_ty) in attrs.iter() {
Self::check_undeclared_in_type(
&attr_ty.attr_type,
entity_types,
undeclared_types,
);
}
}
Type::Set {
element_type: Some(element_type),
} => Self::check_undeclared_in_type(element_type, entity_types, undeclared_types),
_ => (),
}
}
/// Lookup the ValidatorActionId object in the schema with the given name.
pub fn get_action_id(&self, action_id: &EntityUID) -> Option<&ValidatorActionId> {
self.action_ids.get(action_id)
}
/// Lookup the ValidatorEntityType object in the schema with the given name.
pub fn get_entity_type(&self, entity_type_id: &Name) -> Option<&ValidatorEntityType> {
self.entity_types.get(entity_type_id)
}
/// Return true when the entity_type_id corresponds to a valid entity type.
pub(crate) fn is_known_action_id(&self, action_id: &EntityUID) -> bool {
self.action_ids.contains_key(action_id)
}
/// Return true when the entity_type_id corresponds to a valid entity type.
pub(crate) fn is_known_entity_type(&self, entity_type: &Name) -> bool {
self.entity_types.contains_key(entity_type)
}
/// An iterator over the action ids in the schema.
pub(crate) fn known_action_ids(&self) -> impl Iterator<Item = &EntityUID> {
self.action_ids.keys()
}
/// An iterator over the entity type names in the schema.
pub(crate) fn known_entity_types(&self) -> impl Iterator<Item = &Name> {
self.entity_types.keys()
}
/// An iterator matching the entity Types to their Validator Types
pub fn entity_types(&self) -> impl Iterator<Item = (&Name, &ValidatorEntityType)> {
self.entity_types.iter()
}
/// Get the validator entity equal to an EUID using the component for a head
/// var kind.
pub(crate) fn get_entity_eq<'a, H, K>(&self, var: H, euid: EntityUID) -> Option<K>
where
H: 'a + HeadVar<K>,
K: 'a,
{
var.get_euid_component(euid)
}
/// Get the validator entities that are in the descendants of an EUID using
/// the component for a head var kind.
pub(crate) fn get_entities_in<'a, H, K>(
&'a self,
var: H,
euid: EntityUID,
) -> impl Iterator<Item = K> + 'a
where
H: 'a + HeadVar<K>,
K: 'a + Clone,
{
var.get_descendants_if_present(self, euid.clone())
.into_iter()
.flatten()
.map(Clone::clone)
.chain(var.get_euid_component_if_present(self, euid).into_iter())
}
/// Get the validator entities that are in the descendants of any of the
/// entities in a set of EUID using the component for a head var kind.
pub(crate) fn get_entities_in_set<'a, H, K>(
&'a self,
var: H,
euids: impl IntoIterator<Item = EntityUID> + 'a,
) -> impl Iterator<Item = K> + 'a
where
H: 'a + HeadVar<K>,
K: 'a + Clone,
{
euids
.into_iter()
.flat_map(move |e| self.get_entities_in(var, e))
}
/// Since different Actions have different schemas for `Context`, you must
/// specify the `Action` in order to get a `ContextSchema`.
///
/// Returns `None` if the action is not in the schema.
pub fn get_context_schema(
&self,
action: &EntityUID,
) -> Option<impl cedar_policy_core::entities::ContextSchema> {
self.get_action_id(action).map(|action_id| {
// The invariant on `ContextSchema` requires that the inner type is
// representable as a schema type. Here we build a closed record
// type, which are representable as long as their values are
// representable. The values are representable because they are
// taken from the context of a `ValidatorActionId` which was
// constructed directly from a schema.
ContextSchema(crate::types::Type::record_with_attributes(
action_id
.context
.iter()
.map(|(k, v)| (k.clone(), v.clone())),
OpenTag::ClosedAttributes,
))
})
}
/// Construct an `Entity` object for each action in the schema
fn action_entities_iter(&self) -> impl Iterator<Item = cedar_policy_core::ast::Entity> + '_ {
// We could store the un-inverted `memberOf` relation for each action,
// but I [john-h-kastner-aws] judge that the current implementation is
// actually less error prone, as it minimizes the threading of data
// structures through some complicated bits of schema construction code,
// and avoids computing the TC twice.
let mut action_ancestors: HashMap<&EntityUID, HashSet<EntityUID>> = HashMap::new();
for (action_euid, action_def) in &self.action_ids {
for descendant in &action_def.descendants {
action_ancestors
.entry(descendant)
.or_default()
.insert(action_euid.clone());
}
}
self.action_ids.iter().map(move |(action_id, action)| {
Entity::new(
action_id.clone(),
action.attributes.clone(),
action_ancestors.remove(action_id).unwrap_or_default(),
)
})
}
/// Invert the action hierarchy to get the ancestor relation expected for
/// the `Entity` datatype instead of descendant as stored by the schema.
pub fn action_entities(&self) -> cedar_policy_core::entities::Result<Entities> {
Entities::from_entities(
self.action_entities_iter(),
TCComputation::AssumeAlreadyComputed,
)
}
}
/// Struct which carries enough information that it can (efficiently) impl Core's `Schema`
pub struct CoreSchema<'a> {
/// Contains all the information
schema: &'a ValidatorSchema,
/// For easy lookup, this is a map from action name to `Entity` object
/// for each action in the schema. This information is contained in the
/// `ValidatorSchema`, but not efficient to extract -- getting the `Entity`
/// from the `ValidatorSchema` is O(N) as of this writing, but with this
/// cache it's O(1).
actions: HashMap<EntityUID, Arc<Entity>>,
}
impl<'a> CoreSchema<'a> {
pub fn new(schema: &'a ValidatorSchema) -> Self {
Self {
actions: schema
.action_entities_iter()
.map(|e| (e.uid(), Arc::new(e)))
.collect(),
schema,
}
}
}
impl<'a> cedar_policy_core::entities::Schema for CoreSchema<'a> {
type EntityTypeDescription = EntityTypeDescription;
fn entity_type(
&self,
entity_type: &cedar_policy_core::ast::EntityType,
) -> Option<EntityTypeDescription> {
match entity_type {
cedar_policy_core::ast::EntityType::Unspecified => None, // Unspecified entities cannot be declared in the schema and should not appear in JSON data
cedar_policy_core::ast::EntityType::Concrete(name) => {
EntityTypeDescription::new(self.schema, name)
}
}
}
fn action(&self, action: &EntityUID) -> Option<Arc<cedar_policy_core::ast::Entity>> {
self.actions.get(action).map(Arc::clone)
}
fn entity_types_with_basename<'b>(
&'b self,
basename: &'b Id,
) -> Box<dyn Iterator<Item = EntityType> + 'b> {
Box::new(self.schema.entity_types().filter_map(move |(name, _)| {
if name.basename() == basename {
Some(EntityType::Concrete(name.clone()))
} else {
None
}
}))
}
}
/// Struct which carries enough information that it can impl Core's `EntityTypeDescription`
pub struct EntityTypeDescription {
/// Core `EntityType` this is describing
core_type: cedar_policy_core::ast::EntityType,
/// Contains most of the schema information for this entity type
validator_type: ValidatorEntityType,
/// Allowed parent types for this entity type. (As of this writing, this
/// information is not contained in the `validator_type` by itself.)
allowed_parent_types: Arc<HashSet<cedar_policy_core::ast::EntityType>>,
}
impl EntityTypeDescription {
/// Create a description of the given type in the given schema.
/// Returns `None` if the given type is not in the given schema.
pub fn new(schema: &ValidatorSchema, type_name: &Name) -> Option<Self> {
Some(Self {
core_type: cedar_policy_core::ast::EntityType::Concrete(type_name.clone()),
validator_type: schema.get_entity_type(type_name).cloned()?,
allowed_parent_types: {
let mut set = HashSet::new();
for (possible_parent_typename, possible_parent_et) in &schema.entity_types {
if possible_parent_et.descendants.contains(type_name) {
set.insert(cedar_policy_core::ast::EntityType::Concrete(
possible_parent_typename.clone(),
));
}
}
Arc::new(set)
},
})
}
}
impl cedar_policy_core::entities::EntityTypeDescription for EntityTypeDescription {
fn entity_type(&self) -> cedar_policy_core::ast::EntityType {
self.core_type.clone()
}
fn attr_type(&self, attr: &str) -> Option<cedar_policy_core::entities::SchemaType> {
let attr_type: &crate::types::Type = &self.validator_type.attr(attr)?.attr_type;
// This converts a type from a schema into the representation of schema
// types used by core. `attr_type` is taken from a `ValidatorEntityType`
// which was constructed from a schema.
// PANIC SAFETY: see above
#[allow(clippy::expect_used)]
let core_schema_type: cedar_policy_core::entities::SchemaType = attr_type
.clone()
.try_into()
.expect("failed to convert validator type into Core SchemaType");
debug_assert!(attr_type.is_consistent_with(&core_schema_type));
Some(core_schema_type)
}
fn required_attrs<'s>(&'s self) -> Box<dyn Iterator<Item = SmolStr> + 's> {
Box::new(
self.validator_type
.attributes
.iter()
.filter(|(_, ty)| ty.is_required)
.map(|(attr, _)| attr.clone()),
)
}
fn allowed_parent_types(&self) -> Arc<HashSet<cedar_policy_core::ast::EntityType>> {
Arc::clone(&self.allowed_parent_types)
}
}
/// Struct which carries enough information that it can impl Core's
/// `ContextSchema` INVARIANT: The `Type` stored in this struct must be
/// representable as a `SchemaType` to avoid panicking in `context_type`.
struct ContextSchema(crate::types::Type);
/// A `Type` contains all the information we need for a Core `ContextSchema`.
impl cedar_policy_core::entities::ContextSchema for ContextSchema {
fn context_type(&self) -> cedar_policy_core::entities::SchemaType {
// PANIC SAFETY: By `ContextSchema` invariant, `self.0` is representable as a schema type.
#[allow(clippy::expect_used)]
self.0
.clone()
.try_into()
.expect("failed to convert validator type into Core SchemaType")
}
}
/// Contains entity type information for use by the validator. The contents of
/// the struct are the same as the schema entity type structure, but the
/// `member_of` relation is reversed to instead be `descendants`.
#[derive(Clone, Debug, Serialize)]
pub struct ValidatorEntityType {
/// The name of the entity type.
pub(crate) name: Name,
/// The set of entity types that can be members of this entity type. When
/// this structure is initially constructed, the field will contain direct
/// children, but it will be updated to contain the closure of all
/// descendants before it is used in any validation.
pub descendants: HashSet<Name>,
/// The attributes associated with this entity. Keys are the attribute
/// identifiers while the values are the type of the attribute.
pub(crate) attributes: Attributes,
}
impl ValidatorEntityType {
/// Get the type of the attribute with the given name, if it exists
pub fn attr(&self, attr: &str) -> Option<&AttributeType> {
self.attributes.get_attr(attr)
}
/// An iterator over the attributes of this entity
pub fn attributes(&self) -> impl Iterator<Item = (&SmolStr, &AttributeType)> {
self.attributes.iter()
}
/// Return `true` if this entity type has an `EntityType` declared as a
/// possible descendant in the schema. This takes an `EntityType` rather
/// than a `Name`, It's not possible to declare the unspecified entity type
/// is a descendant of an entity type in the schema, so we can return false
/// in the unspecified case.
pub fn has_descendant_entity_type(&self, ety: &EntityType) -> bool {
match ety {
EntityType::Concrete(ety) => self.descendants.contains(ety),
EntityType::Unspecified => false,
}
}
}
impl TCNode<Name> for ValidatorEntityType {
fn get_key(&self) -> Name {
self.name.clone()
}
fn add_edge_to(&mut self, k: Name) {
self.descendants.insert(k);
}
fn out_edges(&self) -> Box<dyn Iterator<Item = &Name> + '_> {
Box::new(self.descendants.iter())
}
fn has_edge_to(&self, e: &Name) -> bool {
self.descendants.contains(e)
}
}
/// Contains information about actions used by the validator. The contents of
/// the struct are the same as the schema entity type structure, but the
/// `member_of` relation is reversed to instead be `descendants`.
#[derive(Clone, Debug, Serialize)]
pub struct ValidatorActionId {
/// The name of the action.
pub(crate) name: EntityUID,
/// The principals and resources that the action can be applied to.
#[serde(rename = "appliesTo")]
pub(crate) applies_to: ValidatorApplySpec,
/// The set of actions that can be members of this action. When this
/// structure is initially constructed, the field will contain direct
/// children, but it will be updated to contain the closure of all
/// descendants before it is used in any validation.
pub(crate) descendants: HashSet<EntityUID>,
/// The context attributes associated with this action. Keys are the context
/// attribute identifiers while the values are the type of the attribute.
pub(crate) context: Attributes,
/// The attribute types for this action, used for typechecking.
pub(crate) attribute_types: Attributes,
/// The actual attribute value for this action, used to construct an
/// `Entity` for this action. Could also be used for more precise
/// typechecking by partial evaluation.
pub(crate) attributes: HashMap<SmolStr, RestrictedExpr>,
}
impl ValidatorActionId {
/// An iterator over the attributes of this action's required context
pub fn context(&self) -> impl Iterator<Item = (&SmolStr, &AttributeType)> {
self.context.iter()
}
}
impl TCNode<EntityUID> for ValidatorActionId {
fn get_key(&self) -> EntityUID {
self.name.clone()
}
fn add_edge_to(&mut self, k: EntityUID) {
self.descendants.insert(k);
}
fn out_edges(&self) -> Box<dyn Iterator<Item = &EntityUID> + '_> {
Box::new(self.descendants.iter())
}
fn has_edge_to(&self, e: &EntityUID) -> bool {
self.descendants.contains(e)
}
}
/// The principals and resources that an action can be applied to.
#[derive(Clone, Debug, Serialize)]
pub(crate) struct ValidatorApplySpec {
/// The principal entity types the action can be applied to. This set may
/// be a singleton set containing the unspecified entity type when the
/// `principalTypes` list is omitted in the schema. A non-singleton set
/// shouldn't contain the unspecified entity type, but validation will give
/// the same success/failure result as when it is the only element of the
/// set, perhaps with extra type errors.
#[serde(rename = "principalApplySpec")]
principal_apply_spec: HashSet<EntityType>,
/// The resource entity types the action can be applied to. See comments on
/// `principal_apply_spec` about the unspecified entity type.
#[serde(rename = "resourceApplySpec")]
resource_apply_spec: HashSet<EntityType>,
}
impl ValidatorApplySpec {
/// Create an apply spec for an action that can only be applied to some
/// specific entities.
pub(crate) fn new(
principal_apply_spec: HashSet<EntityType>,
resource_apply_spec: HashSet<EntityType>,
) -> Self {
Self {
principal_apply_spec,
resource_apply_spec,
}
}
/// Get the applicable principal types for this spec.
pub(crate) fn applicable_principal_types(&self) -> impl Iterator<Item = &EntityType> {
self.principal_apply_spec.iter()
}
/// Get the applicable resource types for this spec.
pub(crate) fn applicable_resource_types(&self) -> impl Iterator<Item = &EntityType> {
self.resource_apply_spec.iter()
}
}
/// This trait configures what sort of entity (principals, actions, or resources)
/// are returned by the function `get_entities_satisfying_constraint`.
pub(crate) trait HeadVar<K>: Copy {
/// For a validator, get the known entities for this sort of head variable.
/// This is all entity types (for principals and resources), or actions ids
/// (for actions) that appear in the service description.
fn get_known_vars<'a>(
&self,
schema: &'a ValidatorSchema,
) -> Box<dyn Iterator<Item = &'a K> + 'a>;
/// Extract the relevant component of an entity uid. This is the entity type
/// for principals and resources, and the entity id for actions.
fn get_euid_component(&self, euid: EntityUID) -> Option<K>;
/// Extract the relevant component of an entity uid if the entity uid is in
/// the schema. Otherwise return None.
fn get_euid_component_if_present(&self, schema: &ValidatorSchema, euid: EntityUID)
-> Option<K>;
/// Get and iterator containing the valid descendants of an entity, if that
/// entity exists in the schema. Otherwise None.
fn get_descendants_if_present<'a>(
&self,
schema: &'a ValidatorSchema,
euid: EntityUID,
) -> Option<Box<dyn Iterator<Item = &'a K> + 'a>>;
}
/// Used to have `get_entities_satisfying_constraint` return the
/// `EntityTypeNames` for either principals or resources satisfying the head
/// constraints.
#[derive(Debug, Clone, Copy)]
pub(crate) enum PrincipalOrResourceHeadVar {
PrincipalOrResource,
}
impl HeadVar<Name> for PrincipalOrResourceHeadVar {
fn get_known_vars<'a>(
&self,
schema: &'a ValidatorSchema,
) -> Box<dyn Iterator<Item = &'a Name> + 'a> {
Box::new(schema.known_entity_types())
}
fn get_euid_component(&self, euid: EntityUID) -> Option<Name> {
let (ty, _) = euid.components();
match ty {
EntityType::Unspecified => None,
EntityType::Concrete(name) => Some(name),
}
}
fn get_euid_component_if_present(
&self,
schema: &ValidatorSchema,
euid: EntityUID,
) -> Option<Name> {
let euid_component = self.get_euid_component(euid)?;
if schema.is_known_entity_type(&euid_component) {
Some(euid_component)
} else {
None
}
}
fn get_descendants_if_present<'a>(
&self,
schema: &'a ValidatorSchema,
euid: EntityUID,
) -> Option<Box<dyn Iterator<Item = &'a Name> + 'a>> {
let euid_component = self.get_euid_component(euid)?;
match schema.get_entity_type(&euid_component) {
Some(entity_type) => Some(Box::new(entity_type.descendants.iter())),
None => None,
}
}
}
/// Used to have `get_entities_satisfying_constraint` return the
/// `ActionIdNames` for actions satisfying the head constraints
#[derive(Debug, Clone, Copy)]
pub(crate) enum ActionHeadVar {
Action,
}
impl HeadVar<EntityUID> for ActionHeadVar {
fn get_known_vars<'a>(
&self,
schema: &'a ValidatorSchema,
) -> Box<dyn Iterator<Item = &'a EntityUID> + 'a> {
Box::new(schema.known_action_ids())
}
fn get_euid_component(&self, euid: EntityUID) -> Option<EntityUID> {
Some(euid)
}
fn get_euid_component_if_present(
&self,
schema: &ValidatorSchema,
euid: EntityUID,
) -> Option<EntityUID> {
let euid_component = self.get_euid_component(euid)?;
if schema.is_known_action_id(&euid_component) {
Some(euid_component)
} else {
None
}
}
fn get_descendants_if_present<'a>(
&self,
schema: &'a ValidatorSchema,
euid: EntityUID,
) -> Option<Box<dyn Iterator<Item = &'a EntityUID> + 'a>> {
let euid_component = self.get_euid_component(euid)?;
match schema.get_action_id(&euid_component) {
Some(action_id) => Some(Box::new(action_id.descendants.iter())),
None => None,
}
}
}
/// Used to write a schema implicitly overriding the default handling of action
/// groups.
#[derive(Debug, Clone, Deserialize)]
#[serde(transparent)]
pub(crate) struct NamespaceDefinitionWithActionAttributes(pub(crate) NamespaceDefinition);
impl TryInto<ValidatorSchema> for NamespaceDefinitionWithActionAttributes {
type Error = SchemaError;
fn try_into(self) -> Result<ValidatorSchema> {
ValidatorSchema::from_schema_fragments([ValidatorSchemaFragment::from_namespaces([
ValidatorNamespaceDef::from_namespace_definition(
None,
self.0,
crate::ActionBehavior::PermitAttributes,
)?,
])])
}
}
// PANIC SAFETY unit tests
#[allow(clippy::panic)]
// PANIC SAFETY unit tests
#[allow(clippy::indexing_slicing)]
#[cfg(test)]
mod test {
use std::{collections::BTreeMap, str::FromStr};
use crate::types::Type;
use serde_json::json;
use super::*;
// Well-formed schema
#[test]
fn test_from_schema_file() {
let src = json!(
{
"entityTypes": {
"User": {
"memberOfTypes": [ "Group" ]
},
"Group": {
"memberOfTypes": []
},
"Photo": {
"memberOfTypes": [ "Album" ]
},
"Album": {
"memberOfTypes": []
}
},
"actions": {
"view_photo": {
"appliesTo": {
"principalTypes": ["User", "Group"],
"resourceTypes": ["Photo"]
}
}
}
});
let schema_file: NamespaceDefinition = serde_json::from_value(src).expect("Parse Error");
let schema: Result<ValidatorSchema> = schema_file.try_into();
assert!(schema.is_ok());
}
// Duplicate entity "Photo"
#[test]
fn test_from_schema_file_duplicate_entity() {
// Test written using `from_str` instead of `from_value` because the
// `json!` macro silently ignores duplicate map keys.
let src = r#"
{"": {
"entityTypes": {
"User": {
"memberOfTypes": [ "Group" ]
},
"Group": {
"memberOfTypes": []
},
"Photo": {
"memberOfTypes": [ "Album" ]
},
"Photo": {
"memberOfTypes": []
}
},
"actions": {
"view_photo": {
"memberOf": [],
"appliesTo": {
"principalTypes": ["User", "Group"],
"resourceTypes": ["Photo"]
}
}
}
}}"#;
match ValidatorSchema::from_str(src) {
Err(SchemaError::Serde(_)) => (),
_ => panic!("Expected serde error due to duplicate entity type."),
}
}
// Duplicate action "view_photo"
#[test]
fn test_from_schema_file_duplicate_action() {
// Test written using `from_str` instead of `from_value` because the
// `json!` macro silently ignores duplicate map keys.
let src = r#"
{"": {
"entityTypes": {
"User": {
"memberOfTypes": [ "Group" ]
},
"Group": {
"memberOfTypes": []
},
"Photo": {
"memberOfTypes": []
}
},
"actions": {
"view_photo": {
"memberOf": [],
"appliesTo": {
"principalTypes": ["User", "Group"],
"resourceTypes": ["Photo"]
}
},
"view_photo": { }
}
}"#;
match ValidatorSchema::from_str(src) {
Err(SchemaError::Serde(_)) => (),
_ => panic!("Expected serde error due to duplicate action type."),
}
}
// Undefined entity types "Grop", "Usr", "Phoot"
#[test]
fn test_from_schema_file_undefined_entities() {
let src = json!(
{
"entityTypes": {
"User": {
"memberOfTypes": [ "Grop" ]
},
"Group": {
"memberOfTypes": []
},
"Photo": {
"memberOfTypes": []
}
},
"actions": {
"view_photo": {
"appliesTo": {
"principalTypes": ["Usr", "Group"],
"resourceTypes": ["Phoot"]
}
}
}
});
let schema_file: NamespaceDefinition = serde_json::from_value(src).expect("Parse Error");
let schema: Result<ValidatorSchema> = schema_file.try_into();
match schema {
Ok(_) => panic!("from_schema_file should have failed"),
Err(SchemaError::UndeclaredEntityTypes(v)) => {
assert_eq!(v.len(), 3)
}
_ => panic!("Unexpected error from from_schema_file"),
}
}
#[test]
fn undefined_entity_namespace_member_of() {
let src = json!(
{"Foo": {
"entityTypes": {
"User": {
"memberOfTypes": [ "Foo::Group", "Bar::Group" ]
},
"Group": { }
},
"actions": {}
}});
let schema_file: SchemaFragment = serde_json::from_value(src).expect("Parse Error");
let schema: Result<ValidatorSchema> = schema_file.try_into();
match schema {
Ok(_) => panic!("try_into should have failed"),
Err(SchemaError::UndeclaredEntityTypes(v)) => {
assert_eq!(v, HashSet::from(["Bar::Group".to_string()]))
}
_ => panic!("Unexpected error from try_into"),
}
}
#[test]
fn undefined_entity_namespace_applies_to() {
let src = json!(
{"Foo": {
"entityTypes": { "User": { }, "Photo": { } },
"actions": {
"view_photo": {
"appliesTo": {
"principalTypes": ["Foo::User", "Bar::User"],
"resourceTypes": ["Photo", "Bar::Photo"],
}
}
}
}});
let schema_file: SchemaFragment = serde_json::from_value(src).expect("Parse Error");
let schema: Result<ValidatorSchema> = schema_file.try_into();
match schema {
Ok(_) => panic!("try_into should have failed"),
Err(SchemaError::UndeclaredEntityTypes(v)) => {
assert_eq!(
v,
HashSet::from(["Bar::Photo".to_string(), "Bar::User".to_string()])
)
}
_ => panic!("Unexpected error from try_into"),
}
}
// Undefined action "photo_actions"
#[test]
fn test_from_schema_file_undefined_action() {
let src = json!(
{
"entityTypes": {
"User": {
"memberOfTypes": [ "Group" ]
},
"Group": {
"memberOfTypes": []
},
"Photo": {
"memberOfTypes": []
}
},
"actions": {
"view_photo": {
"memberOf": [ {"id": "photo_action"} ],
"appliesTo": {
"principalTypes": ["User", "Group"],
"resourceTypes": ["Photo"]
}
}
}
});
let schema_file: NamespaceDefinition = serde_json::from_value(src).expect("Parse Error");
let schema: Result<ValidatorSchema> = schema_file.try_into();
match schema {
Ok(_) => panic!("from_schema_file should have failed"),
Err(SchemaError::UndeclaredActions(v)) => assert_eq!(v.len(), 1),
_ => panic!("Unexpected error from from_schema_file"),
}
}
// Trivial cycle in action hierarchy
// view_photo -> view_photo
#[test]
fn test_from_schema_file_action_cycle1() {
let src = json!(
{
"entityTypes": {},
"actions": {
"view_photo": {
"memberOf": [ {"id": "view_photo"} ]
}
}
});
let schema_file: NamespaceDefinition = serde_json::from_value(src).expect("Parse Error");
let schema: Result<ValidatorSchema> = schema_file.try_into();
match schema {
Ok(_) => panic!("from_schema_file should have failed"),
Err(SchemaError::CycleInActionHierarchy) => (), // expected result
e => panic!("Unexpected error from from_schema_file: {:?}", e),
}
}
// Slightly more complex cycle in action hierarchy
// view_photo -> edit_photo -> delete_photo -> view_photo
#[test]
fn test_from_schema_file_action_cycle2() {
let src = json!(
{
"entityTypes": {},
"actions": {
"view_photo": {
"memberOf": [ {"id": "edit_photo"} ]
},
"edit_photo": {
"memberOf": [ {"id": "delete_photo"} ]
},
"delete_photo": {
"memberOf": [ {"id": "view_photo"} ]
},
"other_action": {
"memberOf": [ {"id": "edit_photo"} ]
}
}
});
let schema_file: NamespaceDefinition = serde_json::from_value(src).expect("Parse Error");
let schema: Result<ValidatorSchema> = schema_file.try_into();
match schema {
Ok(x) => {
println!("{:?}", x);
panic!("from_schema_file should have failed");
}
Err(SchemaError::CycleInActionHierarchy) => (), // expected result
e => panic!("Unexpected error from from_schema_file: {:?}", e),
}
}
#[test]
fn namespaced_schema() {
let src = r#"
{ "N::S": {
"entityTypes": {
"User": {},
"Photo": {}
},
"actions": {
"view_photo": {
"appliesTo": {
"principalTypes": ["User"],
"resourceTypes": ["Photo"]
}
}
}
} }
"#;
let schema_file: SchemaFragment = serde_json::from_str(src).expect("Parse Error");
let schema: ValidatorSchema = schema_file
.try_into()
.expect("Namespaced schema failed to convert.");
dbg!(&schema);
let user_entity_type = &"N::S::User"
.parse()
.expect("Namespaced entity type should have parsed");
let photo_entity_type = &"N::S::Photo"
.parse()
.expect("Namespaced entity type should have parsed");
assert!(
schema.entity_types.contains_key(user_entity_type),
"Expected and entity type User."
);
assert!(
schema.entity_types.contains_key(photo_entity_type),
"Expected an entity type Photo."
);
assert_eq!(
schema.entity_types.len(),
2,
"Expected exactly 2 entity types."
);
assert!(
schema.action_ids.contains_key(
&"N::S::Action::\"view_photo\""
.parse()
.expect("Namespaced action should have parsed")
),
"Expected an action \"view_photo\"."
);
assert_eq!(schema.action_ids.len(), 1, "Expected exactly 1 action.");
let apply_spec = &schema
.action_ids
.values()
.next()
.expect("Expected Action")
.applies_to;
assert_eq!(
apply_spec.applicable_principal_types().collect::<Vec<_>>(),
vec![&EntityType::Concrete(user_entity_type.clone())]
);
assert_eq!(
apply_spec.applicable_resource_types().collect::<Vec<_>>(),
vec![&EntityType::Concrete(photo_entity_type.clone())]
);
}
#[test]
fn cant_use_namespace_in_entity_type() {
let src = r#"
{
"entityTypes": { "NS::User": {} },
"actions": {}
}
"#;
let schema_file: NamespaceDefinition = serde_json::from_str(src).expect("Parse Error");
assert!(
matches!(TryInto::<ValidatorSchema>::try_into(schema_file), Err(SchemaError::EntityTypeParseError(_))),
"Expected that namespace in the entity type NS::User would cause a EntityType parse error.");
}
#[test]
fn entity_attribute_entity_type_with_namespace() {
let schema_json: SchemaFragment = serde_json::from_str(
r#"
{"A::B": {
"entityTypes": {
"Foo": {
"shape": {
"type": "Record",
"attributes": {
"name": { "type": "Entity", "name": "C::D::Foo" }
}
}
}
},
"actions": {}
}}
"#,
)
.expect("Expected valid schema");
let schema: Result<ValidatorSchema> = schema_json.try_into();
match schema {
Err(SchemaError::UndeclaredEntityTypes(tys)) => {
assert_eq!(tys, HashSet::from(["C::D::Foo".to_string()]))
}
_ => panic!("Schema construction should have failed due to undeclared entity type."),
}
}
#[test]
fn entity_attribute_entity_type_with_declared_namespace() {
let schema_json: SchemaFragment = serde_json::from_str(
r#"
{"A::B": {
"entityTypes": {
"Foo": {
"shape": {
"type": "Record",
"attributes": {
"name": { "type": "Entity", "name": "A::B::Foo" }
}
}
}
},
"actions": {}
}}
"#,
)
.expect("Expected valid schema");
let schema: ValidatorSchema = schema_json
.try_into()
.expect("Expected schema to construct without error.");
let foo_name: Name = "A::B::Foo".parse().expect("Expected entity type name");
let foo_type = schema
.entity_types
.get(&foo_name)
.expect("Expected to find entity");
let name_type = foo_type
.attr("name")
.expect("Expected attribute name")
.attr_type
.clone();
let expected_name_type = Type::named_entity_reference(foo_name);
assert_eq!(name_type, expected_name_type);
}
#[test]
fn cannot_declare_action_type_when_prohibited() {
let schema_json: NamespaceDefinition = serde_json::from_str(
r#"
{
"entityTypes": { "Action": {} },
"actions": {}
}
"#,
)
.expect("Expected valid schema");
let schema: Result<ValidatorSchema> = schema_json.try_into();
assert!(matches!(schema, Err(SchemaError::ActionEntityTypeDeclared)));
}
#[test]
fn can_declare_other_type_when_action_type_prohibited() {
let schema_json: NamespaceDefinition = serde_json::from_str(
r#"
{
"entityTypes": { "Foo": { } },
"actions": {}
}
"#,
)
.expect("Expected valid schema");
TryInto::<ValidatorSchema>::try_into(schema_json).expect("Did not expect any errors.");
}
#[test]
fn cannot_declare_action_in_group_when_prohibited() {
let schema_json: SchemaFragment = serde_json::from_str(
r#"
{"": {
"entityTypes": {},
"actions": {
"universe": { },
"view_photo": {
"attributes": {"id": "universe"}
},
"edit_photo": {
"attributes": {"id": "universe"}
},
"delete_photo": {
"attributes": {"id": "universe"}
}
}
}}
"#,
)
.expect("Expected valid schema");
let schema = ValidatorSchemaFragment::from_schema_fragment(
schema_json,
ActionBehavior::ProhibitAttributes,
);
match schema {
Err(SchemaError::ActionHasAttributes(actions)) => {
assert_eq!(
actions.into_iter().collect::<HashSet<_>>(),
HashSet::from([
"view_photo".to_string(),
"edit_photo".to_string(),
"delete_photo".to_string(),
])
)
}
_ => panic!("Did not see expected error."),
}
}
#[test]
fn test_entity_type_no_namespace() {
let src = json!({"type": "Entity", "name": "Foo"});
let schema_ty: SchemaType = serde_json::from_value(src).expect("Parse Error");
assert_eq!(
schema_ty,
SchemaType::Type(SchemaTypeVariant::Entity { name: "Foo".into() })
);
let ty: Type = ValidatorNamespaceDef::try_schema_type_into_validator_type(
Some(&Name::parse_unqualified_name("NS").expect("Expected namespace.")),
schema_ty,
)
.expect("Error converting schema type to type.")
.resolve_type_defs(&HashMap::new())
.unwrap();
assert_eq!(ty, Type::named_entity_reference_from_str("NS::Foo"));
}
#[test]
fn test_entity_type_namespace() {
let src = json!({"type": "Entity", "name": "NS::Foo"});
let schema_ty: SchemaType = serde_json::from_value(src).expect("Parse Error");
assert_eq!(
schema_ty,
SchemaType::Type(SchemaTypeVariant::Entity {
name: "NS::Foo".into()
})
);
let ty: Type = ValidatorNamespaceDef::try_schema_type_into_validator_type(
Some(&Name::parse_unqualified_name("NS").expect("Expected namespace.")),
schema_ty,
)
.expect("Error converting schema type to type.")
.resolve_type_defs(&HashMap::new())
.unwrap();
assert_eq!(ty, Type::named_entity_reference_from_str("NS::Foo"));
}
#[test]
fn test_entity_type_namespace_parse_error() {
let src = json!({"type": "Entity", "name": "::Foo"});
let schema_ty: SchemaType = serde_json::from_value(src).expect("Parse Error");
assert_eq!(
schema_ty,
SchemaType::Type(SchemaTypeVariant::Entity {
name: "::Foo".into()
})
);
match ValidatorNamespaceDef::try_schema_type_into_validator_type(
Some(&Name::parse_unqualified_name("NS").expect("Expected namespace.")),
schema_ty,
) {
Err(SchemaError::EntityTypeParseError(_)) => (),
_ => panic!("Did not see expected EntityTypeParseError."),
}
}
#[test]
fn schema_type_record_is_validator_type_record() {
let src = json!({"type": "Record", "attributes": {}});
let schema_ty: SchemaType = serde_json::from_value(src).expect("Parse Error");
assert_eq!(
schema_ty,
SchemaType::Type(SchemaTypeVariant::Record {
attributes: BTreeMap::new(),
additional_attributes: false,
}),
);
let ty: Type = ValidatorNamespaceDef::try_schema_type_into_validator_type(None, schema_ty)
.expect("Error converting schema type to type.")
.resolve_type_defs(&HashMap::new())
.unwrap();
assert_eq!(ty, Type::closed_record_with_attributes(None));
}
#[test]
fn get_namespaces() {
let fragment: SchemaFragment = serde_json::from_value(json!({
"Foo::Bar::Baz": {
"entityTypes": {},
"actions": {}
},
"Foo": {
"entityTypes": {},
"actions": {}
},
"Bar": {
"entityTypes": {},
"actions": {}
},
}))
.unwrap();
let schema_fragment: ValidatorSchemaFragment = fragment.try_into().unwrap();
assert_eq!(
schema_fragment
.0
.iter()
.map(|f| f.namespace())
.collect::<HashSet<_>>(),
HashSet::from([
&Some("Foo::Bar::Baz".parse().unwrap()),
&Some("Foo".parse().unwrap()),
&Some("Bar".parse().unwrap())
])
);
}
#[test]
fn schema_no_fragments() {
let schema = ValidatorSchema::from_schema_fragments([]).unwrap();
assert!(schema.entity_types.is_empty());
assert!(schema.action_ids.is_empty());
}
#[test]
fn same_action_different_namespace() {
let fragment: SchemaFragment = serde_json::from_value(json!({
"Foo::Bar": {
"entityTypes": {},
"actions": {
"Baz": {}
}
},
"Bar::Foo": {
"entityTypes": {},
"actions": {
"Baz": { }
}
},
"Biz": {
"entityTypes": {},
"actions": {
"Baz": { }
}
}
}))
.unwrap();
let schema: ValidatorSchema = fragment.try_into().unwrap();
assert!(schema
.get_action_id(&"Foo::Bar::Action::\"Baz\"".parse().unwrap())
.is_some());
assert!(schema
.get_action_id(&"Bar::Foo::Action::\"Baz\"".parse().unwrap())
.is_some());
assert!(schema
.get_action_id(&"Biz::Action::\"Baz\"".parse().unwrap())
.is_some());
}
#[test]
fn same_type_different_namespace() {
let fragment: SchemaFragment = serde_json::from_value(json!({
"Foo::Bar": {
"entityTypes": {"Baz" : {}},
"actions": { }
},
"Bar::Foo": {
"entityTypes": {"Baz" : {}},
"actions": { }
},
"Biz": {
"entityTypes": {"Baz" : {}},
"actions": { }
}
}))
.unwrap();
let schema: ValidatorSchema = fragment.try_into().unwrap();
assert!(schema
.get_entity_type(&"Foo::Bar::Baz".parse().unwrap())
.is_some());
assert!(schema
.get_entity_type(&"Bar::Foo::Baz".parse().unwrap())
.is_some());
assert!(schema
.get_entity_type(&"Biz::Baz".parse().unwrap())
.is_some());
}
#[test]
fn member_of_different_namespace() {
let fragment: SchemaFragment = serde_json::from_value(json!({
"Bar": {
"entityTypes": {
"Baz": {
"memberOfTypes": ["Foo::Buz"]
}
},
"actions": {}
},
"Foo": {
"entityTypes": { "Buz": {} },
"actions": { }
}
}))
.unwrap();
let schema: ValidatorSchema = fragment.try_into().unwrap();
let buz = schema
.get_entity_type(&"Foo::Buz".parse().unwrap())
.unwrap();
assert_eq!(
buz.descendants,
HashSet::from(["Bar::Baz".parse().unwrap()])
);
}
#[test]
fn attribute_different_namespace() {
let fragment: SchemaFragment = serde_json::from_value(json!({
"Bar": {
"entityTypes": {
"Baz": {
"shape": {
"type": "Record",
"attributes": {
"fiz": {
"type": "Entity",
"name": "Foo::Buz"
}
}
}
}
},
"actions": {}
},
"Foo": {
"entityTypes": { "Buz": {} },
"actions": { }
}
}))
.unwrap();
let schema: ValidatorSchema = fragment.try_into().unwrap();
let baz = schema
.get_entity_type(&"Bar::Baz".parse().unwrap())
.unwrap();
assert_eq!(
baz.attr("fiz").unwrap().attr_type,
Type::named_entity_reference_from_str("Foo::Buz"),
);
}
#[test]
fn applies_to_different_namespace() {
let fragment: SchemaFragment = serde_json::from_value(json!({
"Foo::Bar": {
"entityTypes": { },
"actions": {
"Baz": {
"appliesTo": {
"principalTypes": [ "Fiz::Buz" ],
"resourceTypes": [ "Fiz::Baz" ],
}
}
}
},
"Fiz": {
"entityTypes": {
"Buz": {},
"Baz": {}
},
"actions": { }
}
}))
.unwrap();
let schema: ValidatorSchema = fragment.try_into().unwrap();
let baz = schema
.get_action_id(&"Foo::Bar::Action::\"Baz\"".parse().unwrap())
.unwrap();
assert_eq!(
baz.applies_to
.applicable_principal_types()
.collect::<HashSet<_>>(),
HashSet::from([&EntityType::Concrete("Fiz::Buz".parse().unwrap())])
);
assert_eq!(
baz.applies_to
.applicable_resource_types()
.collect::<HashSet<_>>(),
HashSet::from([&EntityType::Concrete("Fiz::Baz".parse().unwrap())])
);
}
#[test]
fn simple_defined_type() {
let fragment: SchemaFragment = serde_json::from_value(json!({
"": {
"commonTypes": {
"MyLong": {"type": "Long"}
},
"entityTypes": {
"User": {
"shape": {
"type": "Record",
"attributes": {
"a": {"type": "MyLong"}
}
}
}
},
"actions": {}
}
}))
.unwrap();
let schema: ValidatorSchema = fragment.try_into().unwrap();
assert_eq!(
schema.entity_types.iter().next().unwrap().1.attributes,
Attributes::with_required_attributes([("a".into(), Type::primitive_long())])
);
}
#[test]
fn defined_record_as_attrs() {
let fragment: SchemaFragment = serde_json::from_value(json!({
"": {
"commonTypes": {
"MyRecord": {
"type": "Record",
"attributes": {
"a": {"type": "Long"}
}
}
},
"entityTypes": {
"User": { "shape": { "type": "MyRecord", } }
},
"actions": {}
}
}))
.unwrap();
let schema: ValidatorSchema = fragment.try_into().unwrap();
assert_eq!(
schema.entity_types.iter().next().unwrap().1.attributes,
Attributes::with_required_attributes([("a".into(), Type::primitive_long())])
);
}
#[test]
fn cross_namespace_type() {
let fragment: SchemaFragment = serde_json::from_value(json!({
"A": {
"commonTypes": {
"MyLong": {"type": "Long"}
},
"entityTypes": { },
"actions": {}
},
"B": {
"entityTypes": {
"User": {
"shape": {
"type": "Record",
"attributes": {
"a": {"type": "A::MyLong"}
}
}
}
},
"actions": {}
}
}))
.unwrap();
let schema: ValidatorSchema = fragment.try_into().unwrap();
assert_eq!(
schema.entity_types.iter().next().unwrap().1.attributes,
Attributes::with_required_attributes([("a".into(), Type::primitive_long())])
);
}
#[test]
fn cross_fragment_type() {
let fragment1: ValidatorSchemaFragment = serde_json::from_value::<SchemaFragment>(json!({
"A": {
"commonTypes": {
"MyLong": {"type": "Long"}
},
"entityTypes": { },
"actions": {}
}
}))
.unwrap()
.try_into()
.unwrap();
let fragment2: ValidatorSchemaFragment = serde_json::from_value::<SchemaFragment>(json!({
"A": {
"entityTypes": {
"User": {
"shape": {
"type": "Record",
"attributes": {
"a": {"type": "MyLong"}
}
}
}
},
"actions": {}
}
}))
.unwrap()
.try_into()
.unwrap();
let schema = ValidatorSchema::from_schema_fragments([fragment1, fragment2]).unwrap();
assert_eq!(
schema.entity_types.iter().next().unwrap().1.attributes,
Attributes::with_required_attributes([("a".into(), Type::primitive_long())])
);
}
#[test]
fn cross_fragment_duplicate_type() {
let fragment1: ValidatorSchemaFragment = serde_json::from_value::<SchemaFragment>(json!({
"A": {
"commonTypes": {
"MyLong": {"type": "Long"}
},
"entityTypes": {},
"actions": {}
}
}))
.unwrap()
.try_into()
.unwrap();
let fragment2: ValidatorSchemaFragment = serde_json::from_value::<SchemaFragment>(json!({
"A": {
"commonTypes": {
"MyLong": {"type": "Long"}
},
"entityTypes": {},
"actions": {}
}
}))
.unwrap()
.try_into()
.unwrap();
let schema = ValidatorSchema::from_schema_fragments([fragment1, fragment2]);
match schema {
Err(SchemaError::DuplicateCommonType(s)) if s.contains("A::MyLong") => (),
_ => panic!("should have errored because schema fragments have duplicate types"),
};
}
#[test]
fn undeclared_type_in_attr() {
let fragment: SchemaFragment = serde_json::from_value(json!({
"": {
"commonTypes": { },
"entityTypes": {
"User": {
"shape": {
"type": "Record",
"attributes": {
"a": {"type": "MyLong"}
}
}
}
},
"actions": {}
}
}))
.unwrap();
match TryInto::<ValidatorSchema>::try_into(fragment) {
Err(SchemaError::UndeclaredCommonTypes(_)) => (),
s => panic!(
"Expected Err(SchemaError::UndeclaredCommonType), got {:?}",
s
),
}
}
#[test]
fn undeclared_type_in_type_def() {
let fragment: SchemaFragment = serde_json::from_value(json!({
"": {
"commonTypes": {
"a": { "type": "b" }
},
"entityTypes": { },
"actions": {}
}
}))
.unwrap();
match TryInto::<ValidatorSchema>::try_into(fragment) {
Err(SchemaError::UndeclaredCommonTypes(_)) => (),
s => panic!(
"Expected Err(SchemaError::UndeclaredCommonType), got {:?}",
s
),
}
}
#[test]
fn shape_not_record() {
let fragment: SchemaFragment = serde_json::from_value(json!({
"": {
"commonTypes": {
"MyLong": { "type": "Long" }
},
"entityTypes": {
"User": {
"shape": { "type": "MyLong" }
}
},
"actions": {}
}
}))
.unwrap();
match TryInto::<ValidatorSchema>::try_into(fragment) {
Err(SchemaError::ContextOrShapeNotRecord(_)) => (),
s => panic!(
"Expected Err(SchemaError::ContextOrShapeNotRecord), got {:?}",
s
),
}
}
/// This test checks for regressions on (adapted versions of) the examples
/// mentioned in the thread at
/// [cedar#134](https://github.com/cedar-policy/cedar/pull/134)
#[test]
fn counterexamples_from_cedar_134() {
// non-normalized entity type name
let bad1 = json!({
"": {
"entityTypes": {
"User // comment": {
"memberOfTypes": [
"UserGroup"
]
},
"User": {
"memberOfTypes": [
"UserGroup"
]
},
"UserGroup": {}
},
"actions": {}
}
});
let fragment = serde_json::from_value::<SchemaFragment>(bad1)
.expect("constructing the fragment itself should succeed"); // should this fail in the future?
let err = ValidatorSchema::try_from(fragment)
.expect_err("should error due to invalid entity type name");
assert!(
err.to_string()
.contains("needs to be normalized (e.g., whitespace removed): User // comment"),
"actual error message was {err}"
);
// non-normalized schema namespace
let bad2 = json!({
"ABC :: //comment \n XYZ ": {
"entityTypes": {
"User": {
"memberOfTypes": []
}
},
"actions": {}
}
});
let fragment = serde_json::from_value::<SchemaFragment>(bad2)
.expect("constructing the fragment itself should succeed"); // should this fail in the future?
let err = ValidatorSchema::try_from(fragment)
.expect_err("should error due to invalid schema namespace");
assert!(
err.to_string().contains(
"needs to be normalized (e.g., whitespace removed): ABC :: //comment "
),
"actual error message was {err}"
);
}
#[test]
fn simple_action_entity() {
let src = json!(
{
"entityTypes": { },
"actions": {
"view_photo": { },
}
});
let schema_file: NamespaceDefinition = serde_json::from_value(src).expect("Parse Error");
let schema: ValidatorSchema = schema_file.try_into().expect("Schema Error");
let actions = schema.action_entities().expect("Entity Construct Error");
let action_uid = EntityUID::from_str("Action::\"view_photo\"").unwrap();
let view_photo = actions.entity(&action_uid);
assert_eq!(
view_photo.unwrap(),
&Entity::new(action_uid, HashMap::new(), HashSet::new())
);
}
#[test]
fn action_entity_hierarchy() {
let src = json!(
{
"entityTypes": { },
"actions": {
"read": {},
"view": {
"memberOf": [{"id": "read"}]
},
"view_photo": {
"memberOf": [{"id": "view"}]
},
}
});
let schema_file: NamespaceDefinition = serde_json::from_value(src).expect("Parse Error");
let schema: ValidatorSchema = schema_file.try_into().expect("Schema Error");
let actions = schema.action_entities().expect("Entity Construct Error");
let view_photo_uid = EntityUID::from_str("Action::\"view_photo\"").unwrap();
let view_uid = EntityUID::from_str("Action::\"view\"").unwrap();
let read_uid = EntityUID::from_str("Action::\"read\"").unwrap();
let view_photo_entity = actions.entity(&view_photo_uid);
assert_eq!(
view_photo_entity.unwrap(),
&Entity::new(
view_photo_uid,
HashMap::new(),
HashSet::from([view_uid.clone(), read_uid.clone()])
)
);
let view_entity = actions.entity(&view_uid);
assert_eq!(
view_entity.unwrap(),
&Entity::new(view_uid, HashMap::new(), HashSet::from([read_uid.clone()]))
);
let read_entity = actions.entity(&read_uid);
assert_eq!(
read_entity.unwrap(),
&Entity::new(read_uid, HashMap::new(), HashSet::new())
);
}
#[test]
fn action_entity_attribute() {
let src = json!(
{
"entityTypes": { },
"actions": {
"view_photo": {
"attributes": { "attr": "foo" }
},
}
});
let schema_file: NamespaceDefinitionWithActionAttributes =
serde_json::from_value(src).expect("Parse Error");
let schema: ValidatorSchema = schema_file.try_into().expect("Schema Error");
let actions = schema.action_entities().expect("Entity Construct Error");
let action_uid = EntityUID::from_str("Action::\"view_photo\"").unwrap();
let view_photo = actions.entity(&action_uid);
assert_eq!(
view_photo.unwrap(),
&Entity::new(
action_uid,
HashMap::from([("attr".into(), RestrictedExpr::val("foo"))]),
HashSet::new()
)
);
}
#[test]
fn test_action_namespace_inference_multi_success() {
let src = json!({
"Foo" : {
"entityTypes" : {},
"actions" : {
"read" : {}
}
},
"ExampleCo::Personnel" : {
"entityTypes" : {},
"actions" : {
"viewPhoto" : {
"memberOf" : [
{
"id" : "read",
"type" : "Foo::Action"
}
]
}
}
},
});
let schema_fragment =
serde_json::from_value::<SchemaFragment>(src).expect("Failed to parse schema");
let schema: ValidatorSchema = schema_fragment.try_into().expect("Schema should construct");
let view_photo = schema
.action_entities_iter()
.find(|e| e.uid() == r#"ExampleCo::Personnel::Action::"viewPhoto""#.parse().unwrap())
.unwrap();
let ancestors = view_photo.ancestors().collect::<Vec<_>>();
let read = ancestors[0];
assert_eq!(read.eid().to_string(), "read");
assert_eq!(read.entity_type().to_string(), "Foo::Action");
}
#[test]
fn test_action_namespace_inference_multi() {
let src = json!({
"ExampleCo::Personnel::Foo" : {
"entityTypes" : {},
"actions" : {
"read" : {}
}
},
"ExampleCo::Personnel" : {
"entityTypes" : {},
"actions" : {
"viewPhoto" : {
"memberOf" : [
{
"id" : "read",
"type" : "Foo::Action"
}
]
}
}
},
});
let schema_fragment =
serde_json::from_value::<SchemaFragment>(src).expect("Failed to parse schema");
let schema: std::result::Result<ValidatorSchema, _> = schema_fragment.try_into();
schema.expect_err("Schema should fail to construct as the normalization rules treat any qualification as starting from the root");
}
#[test]
fn test_action_namespace_inference() {
let src = json!({
"ExampleCo::Personnel" : {
"entityTypes" : { },
"actions" : {
"read" : {},
"viewPhoto" : {
"memberOf" : [
{
"id" : "read",
"type" : "Action"
}
]
}
}
}
});
let schema_fragment =
serde_json::from_value::<SchemaFragment>(src).expect("Failed to parse schema");
let schema: ValidatorSchema = schema_fragment.try_into().unwrap();
let view_photo = schema
.action_entities_iter()
.find(|e| e.uid() == r#"ExampleCo::Personnel::Action::"viewPhoto""#.parse().unwrap())
.unwrap();
let ancestors = view_photo.ancestors().collect::<Vec<_>>();
let read = ancestors[0];
assert_eq!(read.eid().to_string(), "read");
assert_eq!(
read.entity_type().to_string(),
"ExampleCo::Personnel::Action"
);
}
}