Access policies as code
読了まで約74 分分、11 節 節の 13 例 例。
この部分はまだ翻訳されていないため、英語で表示している。
By the end of this chapter you can write a data platform's access rules as one policy that denies by default and returns row filters and column masks as data. The policy allows by role and narrows by attribute, and an engine can apply its whole decision. You'll write it in Rego, the policy language of Open Policy Agent (OPA), test it the way opa test, OPA's test runner, does, check it against a who-sees-what table that the data's owners can read, know where the in-process engine this book uses differs from OPA, and see the same rules in Cedar, a narrower policy language.
Each group that reads the platform's warehouse, from support agents to its course assistant, needs something different. Support agents each serve one region, americas, emea or apac, and need learners' contact details to answer tickets. Instructors see the learners and reviews of their own courses, without contact details. Marketing may reach only learners who agreed to marketing email. Finance reconciles payments, and the legacy card_number column of billing.payments must never show more than its last four digits. Data scientists see learner_id as stored, to join tables on it, with personal columns pseudonymized and free text redacted. The course assistant reads only the reviews whose authors agreed to AI training. Chapter 1 showed one rule, support agents see only their own region, and enforced it on one query. Rules for every group need to live in one place, stated exactly, and tested before anyone relies on them.
This chapter's terms are the ones the rest of the book uses. A policy is code that answers whether a request may go ahead. A decision is a policy's answer. The principal is who asks, a role is a group of principals with the same job, and an attribute is any other fact a rule reads, about the principal, the data or the request. A row filter limits which rows a query returns, and a column mask changes how a column's values are shown, such as pseudonymization, which replaces an identifier with a consistent stand-in.
The platform's policy is governance/policies/access.rego, and its tests are in governance/policies/access_test.rego. The book's shared module lib.policy loads both into regopy 1.5.2, the Python binding of rego-cpp, Microsoft's C++ implementation of Rego, which evaluates policies inside the Python process with no server to run.
リクエストの形を Trino に合わせる
ポリシーエンジンはリクエストについての問いに答えるため、リクエストの形によって、どのシステムがエンジンに問い合わせられるかが決まる。OPA の前提は、ポリシーをそれを実施するサービスから切り出すことである。OPA は「ポリシーをソフトウェアサービスから分離できるようにする」ので、ポリシーを単独で読み、バージョン管理し、テストできる(OPA philosophy)。サービスはリクエストを記述した JSON ドキュメントを送り、ポリシーはそれを input として読む。オープンソースの SQL エンジンである Trino はまさにこの方式をとる。クエリが必要とするアクセスチェックごとに、OPA プラグインが次の二つを含むドキュメントを送る。一つは context.identity で、ユーザーとそのグループを表す。もう一つは action で、操作と対象リソースを表し、リソースはカタログ、スキーマ、テーブル、列にまで及ぶ(Trino 484, OPA access control)。
プラットフォームのポリシーは同じフィールドを読むため、列の読み取りを求める Trino のリクエスト SelectFromColumns を、Trino が送るとおりに判定する。ただし、Trino に必要なのはこの一つの判定だけではない。Trino は行フィルターと列マスクをそれぞれ SQL として要求する。行フィルターは {"expression": "clause"} の形をとり、ポリシーが列と値を返す(Trino 484, OPA access control)。Trino は ExecuteQuery や AccessCatalog など、ほかのチェックについても問い合わせるが、このポリシーの allow はそれらを拒否する(Trino 484 の OpaAccessControl.java)。これらに対してポリシーの隣のモジュールが何を返す必要があるかは、第6章で示す。lib.policy.request はプラットフォームのリクエストを Trino の形で組み立てる。次の例では、これを使って、emea のサポートエージェント agent-a05 が support.tickets の三つの列を読む場合を扱う。
1"""The request a support agent's read sends the policy, shaped as Trino's OPA plugin sends it. The point rests on the printed `action`: the operation SelectFromColumns, and the table with the columns the query reads."""2import json34from lib import policy56# A support agent's query reads three columns of the tickets table: the request Trino's OPA plugin would send for it.7# no groups here, because the policy reads them from users.toml; no purpose either, since request adds one only when given8request = policy.request("agent-a05", "support.tickets", ["ticket_id", "opened_at", "body"])9print(json.dumps(request, indent=2))
python trino-shaped-request.py{
"context": {
"identity": {
"user": "agent-a05"
}
},
"action": {
"operation": "SelectFromColumns",
"resource": {
"table": {
"catalogName": "platform",
"schemaName": "support",
"tableName": "tickets",
"columns": [
"ticket_id",
"opened_at",
"body"
]
}
}
}
}
操作は Trino で読み取りを表す名前である SelectFromColumns であり、テーブルは action.resource.table の下に置かれ、カタログ、スキーマ、名前、およびクエリが触れる列を持つ。プラットフォームのリクエストは、重要な二点で Trino のものと異なる。第一に、context.identity.groups を含めない。ポリシーはユーザーのグループを、属性と同じファイルである governance/users.toml から引くため、テストとポリシーが単一の信頼できる情報源を読むことになる。第二に、トップレベルに purpose を持てる。たとえば ai-training のような値であり、Trino はこれを送らない。Trino の背後では、クライアントが設定する値を信頼するのではなく、たとえば学習パイプラインに専用のサービスユーザーを与えるなどして、目的を ID から導出することを推奨する。
実践ルール: ポリシーの入力は、それを実施するエンジンのリクエストにフィールド単位で合わせ、そのエンジンが送れないものだけを、レビュアーの目に入るトップレベルに追加する。使うエンジンが Trino でない場合は、そのエンジンのリクエスト形式から出発し、lib.policy.request のように形を一つの関数にまとめて、テストと呼び出し側がずれないようにする。
ポリシーが読むデータを生成する
リクエストは、誰が何を求めているかを示す。ルールがリクエスト以外に必要とする情報、たとえば各ユーザーのグループや各列の分類は、OPAが data と呼ぶ第2のドキュメントから得られる。このドキュメントに含まれる事実の鮮度は、その情報源の鮮度を超えない。プラットフォームのポリシーが読む事実には、すでにすべて所有者が存在する。ユーザーとその属性は governance/users.toml にある。各列の分類は governance/metadata.toml にある。これは、第2章でIcebergのフィールドIDをキーとして管理したレビュー済みのメタデータファイルである。どの講師がどのコースを教えているかは、learning.courses テーブル自体に記録されている。これらのいずれかをポリシーに直接書き込んだり、別のファイルに複製したりすると、誰かが更新を覚えておかなければならないコピーが生まれる。
lib.policy.data_document は、ポリシーを読み込むたびにこの3つの情報源からドキュメントを組み立てる。手で書いた値は一切使わない。
1"""The data the policy reads besides the request, built from the files that own each fact. The point rests on the last two lines: instructor 17's courses come from learning.courses, not from anything typed."""2from lib import policy34# What the policy reads besides the request, built from the files that own each fact.5# the document's one top-level key, which the policy reads as data.platform6data = policy.data_document()["platform"]78print("users, from governance/users.toml:", len(data["users"]))9print("agent-a05:", data["users"]["agent-a05"])10print("instructor-17:", data["users"]["instructor-17"])1112classifications = data["classifications"]13print("tables, from governance/metadata.toml:", len(classifications))14print("columns classified:", sum(len(columns) for columns in classifications.values()))15print("crm.learners.email:", classifications["crm.learners"]["email"])16print("billing.payments.card_number:", classifications["billing.payments"]["card_number"])1718courses = data["instructor_courses"]19print("instructors with courses, from learning.courses:", len(courses))20# instructor IDs are string keys in the document, so the lookup is "17"21print("instructor 17's courses:", courses["17"])
python generated-policy-data.pyusers, from governance/users.toml: 8
agent-a05: {'groups': ['support'], 'region': 'emea'}
instructor-17: {'groups': ['instructor'], 'instructor_id': 17}
tables, from governance/metadata.toml: 10
columns classified: 77
crm.learners.email: personal
billing.payments.card_number: sensitive
instructors with courses, from learning.courses: 48
instructor 17's courses: [2, 75, 80, 87, 103, 134]
ドキュメント内の各事実は、それを所有するファイルまたはテーブルから取得される。出力では各件数に情報源が明記されている。すなわち、governance/users.toml のユーザー、governance/metadata.toml にあるテーブル全体の分類済み列、learning.courses の講師である。agent-a05 にはグループとリージョンがあり、instructor-17 にはグループと講師IDがある。しかし users.toml には、講師17がどのコースを教えているかを示す記述はない。ドキュメントは [2, 75, 80, 87, 103, 134] を learning.courses から見つけ出す。プラットフォームがその講師の別のコースを公開すれば、次にポリシーのデータを生成したとき、講師の行フィルタにそのコースが含まれる。どこにも編集は要らない。分類についても同じである。card_number の sensitive 分類を変更するには、metadata.toml へのレビュー済みの変更が1つあればよく、ポリシーはそれに追従する。
実践ルール: ポリシーのデータは、ポリシーを読み込むたびに、各事実を所有するファイルまたはテーブルから生成する。事実をポリシーや別のファイルにコピーしてはならない。サポート担当者のリージョンのように、事実にまだ所有者がない場合は、プラットフォームが users.toml で行っているように、コードと同様にレビューされる1つのファイルを所有者として与える。
デフォルトで拒否し、ロールで許可する
リクエストとそのデータが揃ったところで、ポリシーが最初に決めるべきことは、どの規則も想定していなかったリクエストをどう扱うかである。Saltzer と Schroeder のフェイルセーフデフォルトの原則は、「アクセスの判断は、除外ではなく許可に基づいて行え」というものである(Saltzer and Schroeder, 1975)。Rego では、これはデフォルト値として表現される。本体が決して true と評価されない規則には値がなく、Rego はこれを未定義(undefined)と呼ぶ。OPA は、同じ名前を共有するすべての規則が未定義であるときにデフォルトを使う(OPA policy language)。default allow := falseを置くと、allow規則のどれにも一致しないリクエストは、誰が送っても、何を求めても拒否される。
ロールベースアクセス制御は、Ferraiolo と Kuhn が1992年に初めて定式化したもので、主体が誰であるかではなく、主体が担う職務によってアクセスを与える。このプラットフォームでは、ロールはusers.toml内のグループであり、ポリシーのreadableオブジェクトが各グループを、読み取りを許されるテーブルに対応づける。リクエストが許可されるのは、それが読み取りであり、ユーザーのいずれかのグループがそのテーブルを読み取れ、かつグループの行フィルタが依存するものに欠けがない場合である。
default allow := false
allow if {
input.action.operation == "SelectFromColumns"
readable_by_a_group
not missing_attribute
}
最後の行は、本番運用についての節がそれを取り除いて示す失敗を防ぐためのものである。対応表を出力し、4つのリクエストを判定して、デフォルトの働きを確かめる。
1"""Which groups may read which tables, then four requests decided by the default-deny policy. The points rest on the four decisions and their reasons, printed last."""2from lib import policy34rego = policy.interpreter()56# The groups the policy knows, and the tables each may read: the role part of the policy.7readable = policy.evaluate(rego, "data.platform.access.readable", {})8for group, tables in readable.items():9 print(f"{group} may read:", ", ".join(tables))1011# Who may read what. Anything no rule allows is denied, including a user or an operation the policy has never seen.12requests = [13 policy.request("finance-1", "billing.payments", ["amount"]),14 policy.request("marketing-1", "billing.payments", ["amount"]),15 policy.request("someone-else", "crm.learners", ["learner_id"]),16]17delete = policy.request("finance-1", "billing.payments", ["amount"])18# the same request as finance's read, asking to delete instead19delete["action"]["operation"] = "DeleteFromTable"20for request in requests + [delete]:21 decision = policy.decide(request, rego)22 who, table = request["context"]["identity"]["user"], request["action"]["resource"]["table"]23 print(f"{who}, {request['action']['operation']} on {table['schemaName']}.{table['tableName']}:",24 "allowed" if decision["allow"] else "denied", "|", "; ".join(decision["reasons"]))
python allow-by-role.pyai may read: learning.courses, learning.reviews
data-science may read: analytics.course_performance, analytics.learner_activity, billing.payments, crm.learners, learning.courses, learning.enrollments, learning.events, learning.reviews
finance may read: billing.payments, learning.courses, learning.enrollments
instructor may read: learning.courses, learning.enrollments, learning.reviews
marketing may read: crm.learners, learning.courses, marketing.audience
support may read: crm.learners, support.tickets
finance-1, SelectFromColumns on billing.payments: allowed | finance may read billing.payments
marketing-1, SelectFromColumns on billing.payments: denied | no group of this user may read this table
someone-else, SelectFromColumns on crm.learners: denied | no group of this user may read this table
finance-1, DeleteFromTable on billing.payments: denied | DeleteFromTable is not an operation this policy allows
許可されるのは、規則が与えたものだけである。finance はbilling.paymentsを読み取ることができ、理由には許可したグループの名前が示される。marketing は読み取れず、ポリシーが一度も知らないユーザーsomeone-elseも読み取れない。どちらにも何も与える規則がないからである。4つ目のリクエストは再び finance によるもので、読み取りではなく削除を求めている。DeleteFromTableに触れる規則は存在しないため、これも拒否され、理由がその旨を述べる。禁止されるものを列挙しようとするポリシーであれば、この操作のための規則が必要になっていたはずだが、このポリシーには不要である。すべての判定には理由が付くので、拒否された本人に説明でき、ログにも残せる。
実践ルール: すべてのポリシーをdefault allow := falseで始め、ロールとリソースを名指しする規則を通じてのみ許可する。ロールからテーブルへの対応表は一か所にまとめ、レビュアーが一度に全体を読めるようにし、すべての判定に理由を付ける。許可されるべきリクエストが拒否された場合は、それを許可する規則を追加する。デフォルトを弱めてはならない。
この部分はまだ翻訳されていないため、英語で表示している。
Narrow by attribute: region, consent and purpose
Roles say which tables a group may read; they can't say which rows. NIST's guide to attribute-based access control defines the general approach, in which access is decided by attributes of the subject, the object, the requested operations and, sometimes, the environment. It treats a role as one such attribute (NIST SP 800-162). The alternative, a role for every combination, is what NIST's guide calls "role explosion".
The platform shows why. Support by region would need three support roles, and adding marketing's consent and a purpose for AI training would multiply the roles again. Instead, the policy keeps one role per job and reads three attributes. The region is an attribute of the principal, from users.toml. Consent is an attribute of the data: the learner's marketing_consent and ai_training_consent columns in crm.learners. The purpose is an attribute of the request. Each one becomes a row filter, a structured condition on a column that the engine adds to the query. The support agent's filter reads the agent's region:
row_filters contains {"column": "support_region", "equals": user.region} if {
"support" in groups
table in {"crm.learners", "support.tickets"}
}
Reads by support agents, an instructor, marketing and a data scientist show what each attribute does, with the data scientist reading reviews twice, once with no purpose and once for ai-training:
1"""Row filters the policy builds for seven reads, from three attributes: the agent's region, the learner's consent and the request's purpose. The points rest on the analyst's two review reads, with and without a purpose."""2import json34from lib import policy56rego = policy.interpreter()78# Region is the agent's attribute, consent the learner's, and purpose the request's. Each becomes a row filter.9reads = [10 ("agent-a01", "support.tickets", ["ticket_id"], None),11 ("agent-a05", "support.tickets", ["ticket_id"], None),12 ("agent-a09", "support.tickets", ["ticket_id"], None),13 ("instructor-17", "learning.reviews", ["rating"], None),14 ("marketing-1", "crm.learners", ["email"], None),15 ("analyst-1", "learning.reviews", ["rating", "body"], None),16 # the same read with a purpose, the one attribute the requester supplies17 ("analyst-1", "learning.reviews", ["rating", "body"], "ai-training"),18]19for user, table, columns, purpose in reads:20 decision = policy.decide(policy.request(user, table, columns, purpose), rego)21 who = user + (f" for {purpose}" if purpose else "")22 # json shows the consent filter with its learner_consented key, not a column23 print(f"{who} reads {table}, row filters:", json.dumps(decision["row_filters"]))24 if "body" in columns:25 print(f"{who} reads {table}, body shown as:", decision["column_masks"]["body"])
python attributes-as-row-filters.pyagent-a01 reads support.tickets, row filters: [{"column": "support_region", "equals": "americas"}]
agent-a05 reads support.tickets, row filters: [{"column": "support_region", "equals": "emea"}]
agent-a09 reads support.tickets, row filters: [{"column": "support_region", "equals": "apac"}]
instructor-17 reads learning.reviews, row filters: [{"column": "course_id", "in": [2, 75, 80, 87, 103, 134]}]
marketing-1 reads crm.learners, row filters: [{"column": "marketing_consent", "equals": true}, {"column": "status", "equals": "active"}]
analyst-1 reads learning.reviews, row filters: []
analyst-1 reads learning.reviews, body shown as: redact
analyst-1 for ai-training reads learning.reviews, row filters: [{"learner_consented": "ai_training_consent"}]
analyst-1 for ai-training reads learning.reviews, body shown as: redact_contacts
The purpose is what changes a data scientist's view of review text. Without one, every review is visible, but the text, body, is shown as redact. For ai-training, only the reviews of learners who agreed to AI training remain, and the text comes back as redact_contacts, with contact details taken out. The three agents get the same rule with one value per region, from one role, and instructor 17's filter lists their courses, generated from learning.courses. Marketing gets two filters, consent given and account active, and an engine applies both. The AI-training filter, learner_consented, tests the review's author, not the review, so enforcing it takes a join to crm.learners. A learner who signed up before the platform asked about AI training has no answer in ai_training_consent, according to the platform's data card, so enforcing the filter has to count an unknown answer as a no.
The purpose is the one attribute the requester supplies, so it reveals more only under a narrower condition: stating ai-training shows review text, but only for consenting learners and without contact details. Leaving it out, or giving any other purpose, gets what the role allows anyway, with the text hidden.
Working rule: Model each condition as an attribute of the principal, the data or the request, read by one rule per role, rather than as another role. When an attribute comes from the requester, as a purpose does, let it unlock access only under a narrower condition, so that leaving it out, or getting it wrong, falls back to what the role allows anyway.
判定を1つのドキュメントとして返す
yes か no だけでは、属性から生じる行フィルタを運べない。Rego の判定は yes か no である必要はない。OPA のポリシーは、真偽値からマップのリストのマップに至るまで、判定を構造化データとして表現できるからである(OPA philosophy)。このプラットフォームのポリシーは構造化データを用いて、回答の全体を data.platform.access.decision という1つのドキュメントとして返す。ドキュメントには allow、row_filters、column_masks、reasons の4つのキーがある。lib.policy.decide は1回のクエリでこれを要求し、Python の辞書として返す。
フィルタとマスクはデータであり、SQL ではない。フィルタは列と、1つの値または値のリストを指定する。同意に関するフィルタの場合は、学習者の同意列を指定する。マスクは名前であり、redact、last4、pseudonymize、redact_contacts のいずれかである。判定を強制する側が、それぞれを自前のコードで SQL に変換する。そのため、リクエストやユーザーレコードに含まれるもの、たとえば users.toml に入力されたリージョンが、SQL テキストとしてクエリに到達することはない。マスクは各列の分類に従う。機密列は全員に対して隠される。ただし、財務部門が閲覧できるカード番号の下4桁は例外である。個人列は、連絡先を必要とするサポート部門には表示され、それ以外の全員には、その職務の必要に応じてマスクされる。そして、メタデータで分類されていない列は機密として扱われる。
classification(column) := level if {
level := data.platform.classifications[table][column]
} else := "sensitive"
マーケティング部門が crm.learners の4つの列を読む場合を考える。そのうち3つはメタデータで分類済みである。残る linkedin_url は、パイプラインが追加し、まだ誰も分類していない列を表す。
1"""The whole decision for one read: marketing's four columns of crm.learners, one of them unclassified. The point rests on the masks and the reasons, which give one line per mask and per group that allowed the read."""2import json34from lib import policy56# Marketing reads four columns of crm.learners. `linkedin_url` stands for a column added to the table and not yet7# classified in governance/metadata.toml.8request = policy.request("marketing-1", "crm.learners", ["full_name", "email", "phone", "linkedin_url"])9# with no interpreter given, decide loads the platform's policy itself10decision = policy.decide(request)11print(json.dumps(decision, indent=2))12print("keys:", ", ".join(sorted(decision)))
python one-decision-document.py{
"allow": true,
"column_masks": {
"linkedin_url": "redact",
"phone": "redact"
},
"reasons": [
"linkedin_url is sensitive, shown as redact",
"marketing may read crm.learners",
"phone is personal, shown as redact"
],
"row_filters": [
{
"column": "marketing_consent",
"equals": true
},
{
"column": "status",
"equals": "active"
}
]
}
keys: allow, column_masks, reasons, row_filters
マーケティング部門は2つの行フィルタ付きで読み取りを許可され、4つの列のうち2つがマスクされる。phone は個人列であり、マーケティング部門はメールの送信にそれを必要としない。linkedin_url は未分類であるため、誰かがレビューするまで機密として扱われる。full_name と email にはマスクがなく、マスクのない列は保存されたとおりに表示される。マーケティング部門はメールを送るためにこれらを必要とする。reasons には、読み取りを許可したグループごとに1行、マスクごとに1行が含まれるため、ドキュメントはそれ自体で説明になっている。拒否の場合、フィルタとマスクは空になる。ポリシーが2つの結果を2つの別々のルールで構築するので、下流で拒否を、一部の列がマスクされた読み取りと取り違えることはない。
実践ルール: リクエストごとに、許可、行フィルタ、列マスク、理由を含む判定ドキュメントを1つ返し、それを全体として読むこと。フィルタとマスクは、強制側のコードが変換する名前と値として保ち、ポリシーが書いた SQL としては扱わない。エンジンがポリシーから SQL を必要とする場合は、たとえば Trino は行フィルタと列マスクについて SQL を必要とする(Trino 484, OPA access control)。その場合は、両者の間に置いたテスト済みの1つの関数で生成すること。
この部分はまだ翻訳されていないため、英語で表示している。
Unit-test the policy in Rego
The decision document is what a test can compare, which matters because a policy decides who sees personal data, and a change to it can widen access without anyone seeing it in a diff. OPA's answer is tests written in Rego: a test is a rule whose name is prefixed with test_, and "If the test rule is undefined or generates a non-true value the test result is reported as FAIL" (OPA policy testing). A test sets up its request with with, which replaces part of input or data for one expression.
The book's engine, regopy, has no opa test, so lib.policy.run_tests plays its part. It finds every test_ rule in the folder's _test.rego files, evaluates each one, and reports PASS when the rule is true, FAIL when it's false or undefined, and ERROR when evaluation fails. Each of the platform's tests sets the user and the action as two separate paths, each to a literal value, for reasons the section on regopy shows:
test_finance_sees_only_the_last_four_digits if {
decision := access.decision with input.context.identity.user as "finance-1"
with input.action as {"operation": "SelectFromColumns", "resource": {"table": {"schemaName": "billing", "tableName": "payments", "columns": ["amount", "card_number"]}}}
decision.allow
decision.row_filters == []
decision.column_masks == {"card_number": "last4"}
}
The tests cover each group's allow, its filters and its masks, and the ways a request is denied: a table the group isn't given, an operation other than a read, a user the policy doesn't know, and a support agent with no region.
1"""Runs every test in the platform's Rego test file and counts the passes. The point rests on the last line, the tests passed out of all of them."""2from collections import Counter34from lib import policy56# Every `test_` rule in governance/policies/access_test.rego, run by name: PASS, FAIL or ERROR, as `opa test` reports.7results = policy.run_tests()8for test, result in results.items():9 print(f"{result}: {test}")10# len(results) counts every test, so one that fails or errors still shows in the total11print("tests passed:", f"{Counter(results.values())['PASS']} of {len(results)}")
python policy-unit-tests.pyPASS: test_support_reads_only_its_own_region
PASS: test_support_without_a_region_is_denied
PASS: test_marketing_sees_only_consenting_active_learners
PASS: test_a_group_only_reads_its_own_tables
PASS: test_finance_sees_only_the_last_four_digits
PASS: test_data_science_never_sees_card_numbers
PASS: test_data_science_sees_pseudonyms_not_people
PASS: test_an_unclassified_column_is_hidden
PASS: test_an_unknown_user_is_denied
PASS: test_an_instructor_sees_only_their_courses
PASS: test_the_assistant_reads_only_consented_reviews
PASS: test_training_reads_only_consented_reviews_without_contacts
tests passed: 12 of 12
A test that checks only decision.allow would miss most mistakes, so every test of an allowed read compares the row filters and the column masks whole. In finance's test, decision.row_filters == [] fails if the policy adds any filter to finance's reads, and decision.column_masks == {"card_number": "last4"} fails if it masks one column more or one fewer, or shows the card number another way. The run passes 12 of 12 tests.
Working rule: Write a test for every rule that grants or narrows access, and one for every way a request can be denied, and compare whole filters and masks, not single keys. Run the tests on every change to the policy and to the data the policy reads.
この部分はまだ翻訳されていないため、英語で表示している。
Check who sees what from a decision table
Unit tests are written in Rego by whoever writes the policy. The people who own the data, such as the support lead or the finance team, should be able to check the policy too, without reading Rego. A decision table is one row per request, saying who reads which table and what they must get back, in the terms of their job.
The platform's table is lib.policy.WHO_SEES_WHAT, a list of rows in Python. Each gives a user, a table, the columns read and an optional purpose, then whether the read is allowed, which columns the row filters test and which columns are masked, and how. Every row goes through lib.policy.decide, which returns the decision document, and the example prints one line per row, in the form to show the data's owners:
1"""Each row of the who-sees-what table run through the policy and printed as one line, with the count of rows the policy matches. The point rests on the last line, the count of rows whose decision matches the outcome its row expects."""2from lib import policy34rego = policy.interpreter()56# Each row of lib.policy.WHO_SEES_WHAT is a request and the decision a reviewer expects for it.7matching = 08for case in policy.WHO_SEES_WHAT:9 decision = policy.decide(policy.request(case.user, case.table, case.columns, case.purpose), rego)10 # matches returns True or False, which add as 1 or 011 matching += policy.matches(case, decision)12 who = case.user + (f" ({case.purpose})" if case.purpose else "")13 shown = "allowed" if decision["allow"] else "denied"14 # a consent filter has no column key, so filtered_on reads its learner_consented key15 filters = ", ".join(policy.filtered_on(decision)) or "-"16 masks = ", ".join(f"{column}: {mask}" for column, mask in decision["column_masks"].items()) or "-"17 print(f"{who:26} {case.table:17} {shown:8} rows: {filters:29} masks: {masks}")18print("rows of the table the policy matches:", f"{matching} of {len(policy.WHO_SEES_WHAT)}")
python who-sees-what.pyagent-a05 crm.learners allowed rows: support_region masks: -
agent-a05 billing.payments denied rows: - masks: -
instructor-17 learning.reviews allowed rows: course_id masks: body: redact_contacts
instructor-17 crm.learners denied rows: - masks: -
marketing-1 crm.learners allowed rows: marketing_consent, status masks: phone: redact
finance-1 billing.payments allowed rows: - masks: card_number: last4
analyst-1 billing.payments allowed rows: - masks: card_number: redact
analyst-1 crm.learners allowed rows: - masks: email: pseudonymize, notes: redact
analyst-1 learning.reviews allowed rows: - masks: body: redact
analyst-1 (ai-training) learning.reviews allowed rows: ai_training_consent masks: body: redact_contacts
assistant-1 learning.reviews allowed rows: ai_training_consent masks: body: redact_contacts
someone-else crm.learners denied rows: - masks: -
rows of the table the policy matches: 12 of 12
Read down the table and each role's rules show up as outcomes. The policy matches 12 of 12 rows: the support agent filtered by region and denied payments; the instructor filtered by course and denied the learners table; finance seeing card_number as last4 where the data scientist sees redact; and the data scientist's two reads of reviews, with and without a purpose. A reviewer who thinks a row is wrong can say so in a pull request without knowing which rule produced it.
Working rule: Keep a who-sees-what table in the repository with the policy, run it with the unit tests, and show its printed form to the data's owners, in terms they can check. When an owner disagrees with a row, change the row first, and let the failing row drive the change to the policy.
この部分はまだ翻訳されていないため、英語で表示している。
Know where regopy differs from OPA
The platform's tests run in regopy because the book's examples must run offline, in seconds, inside Python, and OPA is a separate program. regopy 1.5.2 is close to OPA. Its engine states that it supports Rego v1.18.1 as OPA defines it, and that it passes all the non-built-in test suites OPA publishes. Its README lists the built-ins it doesn't support, such as net.* and http.send (rego-cpp README at v1.5.2), but regopy also differs from OPA in ways that README doesn't list. The platform's policy and tests avoid each of the cases that follow, and OPA itself is the reference for anything in production.
The first group runs without any error:
1"""Small policies that regopy answers differently from OPA, each printed with the answer regopy gives. The prose's points rest on the printed values, and the bound and literal-mock lines are the controls where regopy agrees with OPA."""2import json34from regopy import Input, Interpreter567def ask(rules: str, query: str, request: dict) -> str:8 """One query's answer from regopy: the value bound to x, or "undefined"."""9 rego = Interpreter()10 rego.add_module("example", "package example\n" + rules)11 rego.set_input(Input(request))12 output = str(rego.query(query))13 # regopy writes undefined as a bare word; otherwise the binding for x is the answer14 return output if output == "undefined" else json.dumps(json.loads(output)["bindings"]["x"])151617admins_only = 'default allow := false\nallow if input.user == "admin"\n'18# no variable in the query19print("a false decision, queried on its own:", ask(admins_only, "data.example.allow", {"user": "guest"}))20# bound to x, the same false decision comes back as a value21print("the same decision, bound to a variable:", ask(admins_only, "x := data.example.allow", {"user": "guest"}))2223pre_1_0 = 'allow { input.user == "admin" }\n' # no `if`: OPA 1.x refuses to load this24print("a rule in pre-1.0 syntax:", ask(pre_1_0, "x := data.example.allow", {"user": "admin"}))2526# regoin is never defined, so deny is undefined and not deny is true for any request27typo = 'region := "emea"\ndefault allow := false\nallow if not deny\ndeny if input.region != regoin\n'28print("a deny with a misspelt, unbound variable, for apac:", ask(typo, "x := data.example.allow", {"region": "apac"}))2930mocks = """region := input.user.region31emea := {"region": "emea"}32with_a_literal := seen if { seen := region with input.user as {"region": "emea"} }33with_a_reference := seen if { seen := region with input.user as emea }34"""35print("region under a mock written as a literal:", ask(mocks, "x := data.example.with_a_literal", {"user": {"region": "apac"}}))36# the mock refers to the rule emea, which regopy ignores, so the real input's region comes back37print("region under a mock that refers to a rule:", ask(mocks, "x := data.example.with_a_reference", {"user": {"region": "apac"}}))
python regopy-silent-differences.pya false decision, queried on its own: undefined
the same decision, bound to a variable: false
a rule in pre-1.0 syntax: true
a deny with a misspelt, unbound variable, for apac: true
region under a mock written as a literal: "emea"
region under a mock that refers to a rule: "apac"
- A false decision comes back undefined. Asked for
data.example.allowon its own, regopy answersundefinedwhere the value isfalse, so "denied" can't be told from "no decision". Bound to a variable, the same query givesfalse.lib.policybinds every decision it reads. - Pre-1.0 syntax runs. Since OPA 1.0,
ifis required for every rule andcontainsfor multi-value rules (OPA v1.0.0 release notes), and rego-cpp's changelog records that it has dropped support for v0 Rego (rego-cpp changelog). Yet regopy evaluatesallow { ... }and answerstrue, so a policy can pass here and fail to load in OPA, and behind Trino as well. - An unbound variable switches a rule off. OPA requires a variable in a comparison to be bound by another expression in the same rule, and rejects the policy otherwise (OPA policy language). regopy runs it anyway. A
denywithregoinmisspelt forregionis silently undefined, soallow if not denyallows anapacrequest, astrueshows. - A mock that refers to another rule is ignored. In a policy whose
regionrule readsinput.user.region, a mock written as a literal,with input.user as {"region": "emea"}, gives"emea". Written as a reference to a rule holding the same object, it gives the real input's"apac".
The second group fails, and one failure takes Python down with it. One case adds a test to a copy of the platform's tests; the other formats the same string from nine sources:
1"""One test that replaces the whole input, and what that does to the interpreter: every query fails, and printing a failed output ends Python. The points rest on the printed error count, the assert that true does not succeed, and the signal the crash ends with."""2import signal3import subprocess4import sys5import tempfile6from collections import Counter7from pathlib import Path89from lib import policy1011# The platform's tests, plus one that replaces the whole input at once rather than one path at a time.12whole_input = """13test_finance_reads_payments if {14 access.decision.allow with input as {"context": {"identity": {"user": "finance-1"}}, "action": {"operation": "SelectFromColumns", "resource": {"table": {"schemaName": "billing", "tableName": "payments", "columns": ["amount"]}}}}15}16"""17with tempfile.TemporaryDirectory() as name:18 folder = Path(name)19 for path in policy.POLICIES.glob("*.rego"):20 (folder / path.name).write_text(path.read_text(encoding="utf-8"), encoding="utf-8")21 with open(folder / "access_test.rego", "a", encoding="utf-8") as tests:22 tests.write(whole_input)23 results = policy.run_tests(folder)24 print("tests that end in ERROR:", f"{Counter(results.values())['ERROR']} of {len(results)}")25 rego = policy.interpreter(folder, tests=True)26 succeeded = rego.query("true").ok()27 # no query succeeds here, not even true: the interpreter loads the file with the broken test28 assert not succeeded29 print("the query true succeeds:", succeeded)3031 # lib.policy checks each output before reading it, so a decision asked of this interpreter raises an error.32 try:33 policy.decide(policy.request("finance-1", "billing.payments", ["amount"]), rego)34 except RuntimeError as error:35 print("lib.policy.decide raises:", error)3637 # Printing one of those failed outputs, in a separate Python process.38 crash = "import sys; from pathlib import Path; from lib import policy; print(policy.interpreter(Path(sys.argv[1]), tests=True).query('true'))"39 ended = subprocess.run([sys.executable, "-c", crash, str(folder)], capture_output=True)40 # a negative return code names the signal that ended the process41 print("printing the failed output ends Python with:", signal.Signals(-ended.returncode).name)
python regopy-failed-outputs.pytests that end in ERROR: 13 of 13
the query true succeeds: False
lib.policy.decide raises: regopy couldn't evaluate data.platform.access.decision, so there is no decision to read
printing the failed output ends Python with: SIGSEGV
1"""One label formatted from nine sources, the schema taken from each in turn. The point rests on the printed lines where regopy's JSON is invalid: a source that keeps its quotes breaks the output."""2import json34from regopy import Input, Interpreter56# The same label, sprintf("%s.%s", [schema, input.table]), with the schema "crm" taken from nine places.7# crm is a rule and schema_of a function, two of the nine sources8helpers = 'crm := "crm"\nschema_of(x) := "crm" if x == 1\n'9cases = [10 ("from input.schema", "input.schema", "true"),11 ("from a rule", "crm", "true"),12 ("from a function", "schema_of(1)", "true"),13 ("from iterating over a set", "s", 'some s in {"crm"}'),14 ("from object.get, the key found", 'object.get(input, "schema", "public")', "true"),15 # namespace is not in the input, so object.get returns its default, crm16 ("from object.get, its default", 'object.get(input, "namespace", "crm")', "true"),17 ("from lower(input.schema)", "lower(input.schema)", "true"),18 ("from concat", 'concat("", ["c", "rm"])', "true"),19 ("from a local variable set to a literal", "v", 'v := "crm"'),20]21for name, schema, body in cases:22 rego = Interpreter()23 rego.add_module("example", f'package example\n{helpers}label := sprintf("%s.%s", [{schema}, input.table]) if {{ {body} }}\n')24 rego.set_input(Input({"schema": "crm", "table": "learners"}))25 try:26 print(f"{name}:", json.loads(str(rego.query("x := data.example.label")))["bindings"]["x"])27 except json.JSONDecodeError as error:28 print(f"{name}, regopy's invalid JSON:", error.doc)
python regopy-sprintf-quotes.pyfrom input.schema: crm.learners
from a rule: crm.learners
from a function: crm.learners
from iterating over a set: crm.learners
from object.get, the key found: crm.learners
from object.get, its default, regopy's invalid JSON: {"expressions":[true], "bindings":{"x":""crm".learners"}}
from lower(input.schema), regopy's invalid JSON: {"expressions":[true], "bindings":{"x":""crm".learners"}}
from concat, regopy's invalid JSON: {"expressions":[true], "bindings":{"x":""crm".learners"}}
from a local variable set to a literal, regopy's invalid JSON: {"expressions":[true], "bindings":{"x":""crm".learners"}}
- Replacing the whole input breaks every query. One extra test that writes its request as
with input as {...}, and evaluates rules that call built-ins such assprintf, makes every query in that interpreter fail. Every test then ends in ERROR, 13 of 13, and so does the first query asked of a fresh interpreter loaded with the same files, before any test runs: even the querytruefails. Replacing one path at a time, as the platform's tests do, works. - Printing a failed output crashes Python. Converting one of those failed outputs to a string ends the process with
SIGSEGV. Solib.policychecksoutput.ok()before it reads anything, and a decision asked of that interpreter raises an error instead:regopy couldn't evaluate data.platform.access.decision, so there is no decision to read. sprintfkeeps stray quotes. Formatting a string returned by some built-ins, such asobject.get's default,lowerorconcat, or a local variable set to a literal, keeps its quotes. regopy's own output is then invalid JSON, which its Python wrapper fails to parse, as{"expressions":[true], "bindings":{"x":""crm".learners"}}shows. Strings frominput, a rule, a function, iteration andobject.get's found value format cleanly, and the platform's policy formats only strings like those.
Working rule: Treat regopy as a fast local evaluator and OPA as the reference. Write Rego v1 only, bind every decision to a variable, mock input one path at a time with literal values, and check that an output succeeded before reading it. Before a policy guards production data, check and test it with OPA itself, with opa check --strict and opa test.
この部分はまだ翻訳されていないため、英語で表示している。
Write the same rules in Cedar
Rego isn't the only language for policies like the platform's. Cedar's designers present it as a language for expressive, fast, safe and analyzable authorization (Cutler et al., 2024), and it takes a narrower view of a decision. A Cedar request names a principal, an action and a resource, and the answer follows three rules: default deny, forbid overrides permit, and skip on error, which the documentation defines as "if a policy's evaluation returns error, the policy does not factor into the authorization response; it is skipped" (Cedar, authorization). A schema declares every type of entity, the kinds of principal and resource a request can name, with its attributes, and attributes are required unless the schema marks them optional (Cedar, schema). Validation checks policies against a schema before any request, and a set that validates avoids most of the errors that evaluation could raise, for requests that match the schema (Cedar, validation). The book's examples run Cedar with cedarpy 4.12.2, which wraps the Rust Cedar Policy library for Python (cedarpy).
The platform's rules in Cedar are in governance/policies/access.cedar, with the schema in access.cedarschema. A permit per group repeats the Rego policy's readable map. A forbid keeps support agents to learners in their own region, and another hides sensitive columns from everyone except finance's view of card numbers. lib.policy.cedar_entities generates the users, groups, tables and columns from the same data document the Rego policy reads, and the code adds the first learner of crm.learners in each region:
1"""The platform's Cedar policies checked against their schema, then asked the questions the Rego policy answers, over entities generated from its data. The points rest on the printed decisions, and on the misspelt-region error the schema gives before any request."""2import cedarpy34from lib import policy5from lib.platform import load67policies = policy.CEDAR_POLICIES.read_text(encoding="utf-8")8schema = policy.CEDAR_SCHEMA.read_text(encoding="utf-8")9print("policies valid against the schema:", cedarpy.validate_policies(policies, schema).validation_passed)10# misspells region in the policy's text, so the schema reports the attribute that User lacks11typo = cedarpy.validate_policies(policies.replace("principal.region", "principal.regoin"), schema)12# the validator's first error13print("with region misspelt:", typo.errors[0].error)1415# Entities generated from the files the Rego policy reads, plus the first learner of crm.learners in each region.16first = {}17for learner in load("crm.learners").select(["learner_id", "support_region"]).to_pylist():18 # setdefault keeps the first learner seen in each region19 first.setdefault(learner["support_region"], learner)20entities = policy.cedar_entities(learners=first.values())212223def ask(user: str, resource: str) -> cedarpy.AuthzResult:24 request = {"principal": f'User::"{user}"', "action": 'Action::"read"', "resource": resource, "context": {}}25 return cedarpy.is_authorized(request, policies, entities, schema)262728for user, kind, name in [("finance-1", "Column", "billing.payments.card_number"),29 ("analyst-1", "Column", "billing.payments.card_number"),30 ("analyst-1", "Column", "billing.payments.amount"),31 ("marketing-1", "Table", "billing.payments")]:32 print(f"{user} reads {name}:", ask(user, f'{kind}::"{name}"').decision.value)33for region, learner in sorted(first.items()):34 print(f"agent-a05 (emea) reads a learner in {region}:", ask("agent-a05", f'Learner::"{learner["learner_id"]}"').decision.value)35answer = ask("finance-1", 'Column::"billing.payments.card_number"')36# the policies that decided the answer, and those skipped on an error, come from its diagnostics37print("everything Cedar returns for finance-1's card numbers:", answer.decision.value, list(answer.diagnostics.reasons), list(answer.diagnostics.errors))
python cedar-same-rules.pypolicies valid against the schema: True
with region misspelt: for policy `policy6`, attribute `regoin` on entity type `User` not found
finance-1 reads billing.payments.card_number: Allow
analyst-1 reads billing.payments.card_number: Deny
analyst-1 reads billing.payments.amount: Allow
marketing-1 reads billing.payments: Deny
agent-a05 (emea) reads a learner in americas: Deny
agent-a05 (emea) reads a learner in apac: Deny
agent-a05 (emea) reads a learner in emea: Allow
everything Cedar returns for finance-1's card numbers: Allow ['policy3'] []
The decisions agree with the Rego policy wherever Cedar can express them. Finance may read the card_number column, and the data scientist may not, where Rego allows the read and redacts the column. The data scientist may read amount, and marketing may not read payments. The emea agent may read the emea learner and neither of the others. The policies validate against the schema too, and misspelling region gives a precise error before anything runs: for policy policy6, attribute regoin on entity type User not found.
Rego has no such check by default. The same typo in an attribute, user.regoin in the support agent's row filter, is undefined in OPA and regopy alike, and opa check --strict passes it, because OPA checks references against a schema only when you give it one (OPA policy language, schemas), and the platform gives none. The agent's row filter disappears, and only a test that compares the filters, such as test_support_reads_only_its_own_region, catches it.
Cedar returns a decision and little else. The last line of the output is everything it gives back for finance's card numbers: the decision, and the ID of the policy that made it. cedarpy names policies by position, policy3 for the fourth, not by their @id annotations such as finance-tables. Cedar's documentation says @id means nothing to the language itself: its CLI uses it to set policy IDs, and other interfaces set them another way (Cedar, syntax). Nothing in the answer says last4, the mask the Rego policy returns for finance's card numbers, because Cedar can permit a column but can't say how to show it. That rule would move into the enforcement code, outside the policy and its tests. Rows are the same. Cedar decides one learner at a time, so filtering the platform's learners means one request per learner, or Cedar's experimental partial evaluation, which returns residual policies for the parts it can't yet decide, and whose result is not a final authorization decision (cedarpy). Cedar's paper makes the trade-off from the other side: "Rego's added expressiveness is a blessing but also a curse". It adds that Rego provides no built-in authorization concepts such as allow or deny. The same paper reports Cedar 42.8 to 80.8 times faster than Rego at the median, on three workloads with transitive relations, with Cedar 3.0.1 against OPA's Rego 0.61.0. Those are its designers' measurements, not this book's, and they say nothing about regopy.
Working rule: Choose Cedar when every question is "may this principal do this to this resource?", as in an application's permission checks, and you want a schema to check every policy before it runs. Choose Rego when the answer has to carry data, as a warehouse's row filters and column masks do, or when the engine already asks OPA, as Trino does. For the platform's warehouse, we recommend Rego.
この部分はまだ翻訳されていないため、英語で表示している。
What goes wrong in production
We consider the most damaging failure a rule that silently doesn't apply because an attribute is missing, and it happens in both languages. In Rego, "Expressions that refer to undefined values are also undefined" (OPA policy language). The support agent's row filter takes its value from user.region, so for a user without a region the filter doesn't exist at all. A rule that allows by group still allows, and no filter means every row.
Take a copy of the platform's policy and tests, in a temporary folder, and remove the one line, not missing_attribute, from allow. Then add a support agent with their group and no region, as happens when someone is given a role before anyone sets their region, and ask both policies for the agent's tickets:
1"""The support policy with and without its guard, asked for a support agent's tickets. The prose's points rest on the printed reasons, row filters and regions seen with each version, and on the test the copy without the guard fails."""2import tempfile3from pathlib import Path45from lib import policy6from lib.platform import load78# A support agent added to users.toml with their group, before anyone has set their region.9data = policy.data_document()10data["platform"]["users"]["agent-new"] = {"groups": ["support"]}11request = policy.request("agent-new", "support.tickets", ["ticket_id", "body"])12# every region with a ticket, the candidates for what the agent may see13regions = sorted(set(load("support.tickets").column("support_region").to_pylist()))1415# A copy of the platform's policy and tests, with one line taken out of `allow`: the missing-attribute guard.16with tempfile.TemporaryDirectory() as name:17 folder = Path(name)18 for path in policy.POLICIES.glob("*.rego"):19 # the guard's line in allow is indented with a tab, and this removes it20 (folder / path.name).write_text(path.read_text(encoding="utf-8").replace("\tnot missing_attribute\n", ""), encoding="utf-8")2122 for label, policies in [("with the guard", policy.POLICIES), ("without the guard", folder)]:23 decision = policy.decide(request, policy.interpreter(policies, data=data))24 # a region counts when the decision allows the read and every row filter names it, so no filters means every region25 seen = [r for r in regions if decision["allow"] and all(f["equals"] == r for f in decision["row_filters"])]26 print(f"{label}, allowed:", decision["allow"])27 print(f"{label}, reasons:", "; ".join(decision["reasons"]))28 print(f"{label}, row filters:", decision["row_filters"])29 print(f"{label}, regions whose tickets the agent sees:", ", ".join(seen) or "none")3031 failing = [test for test, result in policy.run_tests(folder).items() if result != "PASS"]32print("tests that fail without the guard:", ", ".join(failing))
python missing-region-guard.pywith the guard, allowed: False
with the guard, reasons: support's row filter needs the user's region, and there is none
with the guard, row filters: []
with the guard, regions whose tickets the agent sees: none
without the guard, allowed: True
without the guard, reasons: support may read support.tickets; support's row filter needs the user's region, and there is none
without the guard, row filters: []
without the guard, regions whose tickets the agent sees: americas, apac, emea
tests that fail without the guard: test_support_without_a_region_is_denied
Without the guard, the agent is allowed, with row filters [], and sees the tickets of americas, apac, emea, including every learner's message and contact details. The reasons already say the region is missing; nothing in allow asked. With the guard, the agent is denied, and the reason names what is missing: support's row filter needs the user's region, and there is none. default allow := false didn't help either, because a rule did allow. One test catches it, test_support_without_a_region_is_denied, which is why the policy's tests include the case of the missing attribute.
Cedar fails the same way, through skip on error. Strip the has region check from the support agents' forbid in the platform's Cedar policies, and it reads principal.region on an agent who has none:
1"""Cedar's support forbid with and without its `has region` check, asked about a support agent with no region. The points rest on the printed decisions with each version, and on the validation error the unguarded policy gets."""2import cedarpy34from lib import policy5from lib.platform import load67guarded = policy.CEDAR_POLICIES.read_text(encoding="utf-8")8unguarded = guarded.replace("principal has region && ", "") # the same forbid, without its `has` check9schema = policy.CEDAR_SCHEMA.read_text(encoding="utf-8")1011# A support agent with no region yet, as when a role is given before anyone sets one, and the first learner of crm.learners in each region.12data = policy.data_document()13data["platform"]["users"]["agent-new"] = {"groups": ["support"]}14first = {}15for learner in load("crm.learners").select(["learner_id", "support_region"]).to_pylist():16 first.setdefault(learner["support_region"], learner)17entities = policy.cedar_entities(learners=first.values(), data=data)1819for name, policies in [("with `has region`", guarded), ("without it", unguarded)]:20 for region, learner in sorted(first.items()):21 request = {"principal": 'User::"agent-new"', "action": 'Action::"read"',22 "resource": f'Learner::"{learner["learner_id"]}"', "context": {}}23 answer = cedarpy.is_authorized(request, policies, entities, schema)24 print(f"{name}, a learner in {region}:", answer.decision.value)25 # answer is the last learner's request, in emea, the last region in sorted order26 print(f"{name}, policies skipped on an error:", list(answer.diagnostics.errors))27# the first error the validator returns for the unguarded policies28print("validation without it:", cedarpy.validate_policies(unguarded, schema).errors[0].error)
python cedar-missing-region.pywith `has region`, a learner in americas: Deny
with `has region`, a learner in apac: Deny
with `has region`, a learner in emea: Deny
with `has region`, policies skipped on an error: []
without it, a learner in americas: Allow
without it, a learner in apac: Allow
without it, a learner in emea: Allow
without it, policies skipped on an error: ['error while evaluating policy `policy6`: `User::"agent-new"` does not have the attribute `region`']
validation without it: for policy `policy6`, unable to guarantee safety of access to optional attribute `region` on entity type `User`
Without the check, Cedar lets a support agent with no region read the learners of all three regions. Evaluating the forbid raises an error, Cedar skips that policy, and the support permit still applies. With the check, the same agent is denied every learner. Passing the schema to is_authorized didn't stop it, because the schema declares region optional, since users outside support have none. Validation did, before any request: for policy policy6, unable to guarantee safety of access to optional attribute region on entity type User.
Other failures to watch for:
- A misspelt attribute is undefined, not an error.
user.regoinforuser.regionpassesopa check --strict, and the support filter disappears as it does for a missing region. Compare whole filters in the tests, and give OPA a schema if you want the check before any test runs. - A deny rule that reads a missing attribute fails open. The guard fixes
allow; adenyorforbidneeds the same care. Deny explicitly when a required attribute is absent, and write the test for it before the rule. - Policies nobody can test. Rules kept in an engine's settings or in hand-made views per role can't be unit-tested or diffed, so they drift. Keep the policy, its tests and its decision table in the repository, and run all three on every change.
- regopy passes and OPA refuses. Pre-1.0 syntax and unbound variables run in regopy. Check every policy with
opa check --strictbefore deploying it to OPA. - Data that changed under the policy. A support agent moved to another region is one edit to
users.toml, and the decision follows only when the data document is rebuilt. Rebuild it whenever its sources change, and deploy it with the policy.
この章で決めたこと
- Rego による 1 つのポリシーである
governance/policies/access.regoは、既定で拒否し、グループ単位で許可し、3 つの属性で絞り込む。3 つの属性とは、エージェントのリージョン、学習者の同意、リクエストの目的である。 - リクエストは Trino の OPA リクエストと同じ形にする。 リクエストは
lib.policy.requestが組み立て、最上位に任意のpurposeを加える。グループはリクエストからではなくusers.tomlから得る。 - ポリシーのデータは生成する。
lib.policy.data_documentがusers.toml、metadata.toml、learning.coursesから生成し、手で書くことはない。 - 各判定は 1 つのドキュメントである。 ドキュメントは
allow、row_filters、column_masks、reasonsを持つ。フィルターとマスクはデータであり、SQL ではない。マスクはredact、last4、pseudonymize、redact_contactsである。拒否の判定にはフィルターもマスクも含めない。 - 事実が欠けた場合は安全側に倒す。 メタデータが分類していない列は機微として扱い、行フィルターに必要な属性が欠けているユーザーは拒否する。
- ポリシーが知っている目的は
ai-trainingだけである。 この目的は、レビューを AI 学習に同意した学習者のものに限り、連絡先を除いたテキストを表示する。目的のない読み取りでは、ロールが許す内容が得られるが、レビューのテキストは隠される。 - 変更のたびに 2 種類のテストを実行する。 1 つは
lib.policy.run_testsによる Rego のユニットテスト、もう 1 つは誰に何が見えるかを示す表lib.policy.WHO_SEES_WHATである。 - 例には regopy を使い、基準は OPA とする。 Rego v1 のみを使い、すべての判定を変数に束縛し、入力は 1 回に 1 パスずつリテラルでモックし、すべての出力は読む前に確認する。
- Cedar は比較のために示した(
governance/policies/access.cedar)。Cedar は単一リソースに対する許可か拒否かの確認に向く。このプラットフォームのウェアハウスではフィルターとマスクをデータとして扱う必要があるため、Rego を使う。アクセスポリシーの分かりやすさは、テストの網羅性によって決まる。ルールをコードとして書けば、誰に何を見せるかの変更は、レビュアーが読める差分になり、誰かが頼る前にテストを実行できる。
この版における本章
第2026.11.31版2026年11月19日時点で例が通過この章の13 例件のうち13件が合格
英語版の書籍(版2026.11.31)から翻訳した。