由 AI 翻譯。經人工以英文驗證。 閱讀原文

資料治理即程式碼

教學套件

版次 2026.11.36 為本書目前的版次,會隨本書更新而變動。可自由使用與改編,但須標示本書出處。

課程大綱

閱讀時間僅計算本章的文字。學生也會在 Lab 或自己的電腦上執行範例。

每週一章:10 週

5 週後

學習目標與大綱

每章的學習目標即為其第一句。每份大綱依循該章各節,列出該節定義的名詞、執行範例前要求的預測,以及每節結尾所歸納的規則。 其關鍵術語與要提問的預測內容,依題庫所列,採用英文版次的內容。

1. 端到端的治理

閱讀約需 58 分鐘,含 6 個範例。

讀完本章,你就能用程式碼,從頭到尾治理資料平台上的一張資料表。

課程大綱:8 節
  1. 為資料表建立目錄,並記錄其負責人與分類
    • 執行 catalog-the-learners.py
    • 實用原則 將負責人、領域、描述與分類集中放在儲存庫中一份經過審查的檔案裡,每張資料表的負責人都是一個團隊,每個欄位都有封閉清單中的一個分類。把描述寫進目錄,讓每位讀者在看資料表時都能看到,並且以走訪目錄(而非走訪檔案)的方式,對照檔案測試目錄。若編輯是在目錄的網頁上進行,就選定其中一邊作為來源,再由它產生另一邊(第 2 章)。
  2. 擷取單一管線的欄位歷程
    • 執行 audience-pipeline-lineage.py
    • 實用原則 要從執行每個作業的程式碼中送出歷程,不要在作業之後才補(第 4 章):先送出 START 事件,再送出 COMPLETE 或 FAIL 事件,並在 COMPLETE 事件中附上輸出的欄位歷程,該歷程從作業的 SQL 剖析而來,且除了提供值的輸入之外,也包含連接與篩選資料列的輸入。要從事件讀回歷程,不要從 SQL 讀取,這樣圖才會顯示實際執行的內容。若作業不是剖析器能讀取的 SQL,例如 Python 腳本,就在同一份程式碼中寫下它的歷程,並將兩者一起審查。
  3. 對查詢執行單一存取決策
    • 執行 support-region-enforced.py
    • 實用原則 將存取規則寫成儲存庫中的單一政策,預設拒絕,並以資料形式回傳決策:允許、列篩選條件與欄位遮罩。在單一層中套用該決策,於引擎執行之前改寫每個查詢,並且只將該層的回應提供給使用者,絕不提供引擎的連線。當引擎能自行詢問政策時,例如 Trino 可以詢問 OPA,就讓引擎擔任該層。
  4. Fail the pull request that adds an unclassified column
    • 執行 unclassified-column-fails.py
    • 實用原則 Run every governance check as one job on every pull request, required by branch protection, and fail it by exit code on any finding the pull request introduces. Report the findings already on the main branch with their owners, without failing the pull request for them. Put every finding in the pull request itself, under an ID that stays the same from run to run, with what to do about it. Run the same checks on a schedule against the production catalog too, for what changes outside pull requests.
  5. 正式環境中會出什麼問題
  6. 無人更新的治理試算表
    • 執行 spreadsheet-misses-new-column.py
    • 實用原則 切勿將治理中繼資料存放在沒有任何機制檢查的文件中。請將它存放在經過審查的檔案裡,並由測試在每個 pull request 與固定排程時,將它與運作中的目錄比對。若人們需要試算表,就從該檔案產生,並在試算表上註明產生的時間。
  7. 一項政策由一個工具執行,卻被另一個工具繞過
    • 執行 read-around-the-layer.py
    • 實用原則 選定一個執行政策的處所,也就是所有工具查詢時都會經過的引擎,並只把儲存憑證交給它和寫入資料表的作業,絕不交給個人或筆記本。在該處所中,拒絕每一個不是透過目錄資料表來讀取資料的查詢。在把某個資料表視為受保護之前,先列出所有能讀取其檔案的對象,並將任何不詢問政策的讀取端視為繞過,除非它是一項作業,其輸出已像它所讀取的資料表一樣被編入目錄並分類。
  8. 本章的決定

2. 可測試的資料目錄

閱讀約需 86 分鐘,含 12 個範例。

讀完本章,你就能以程式碼的形式管理資料目錄的中繼資料。

課程大綱:12 節
  1. Keep the metadata in a reviewed file
    • 關鍵詞 metadata
    • 執行 metadata-model.py
    • 實用原則 Keep owners, domains, descriptions and classifications in a file in the repository, reviewed like the pipelines. Use a closed list of classifications, ordered by how much protection each needs, and one owning team per table. If your organization edits metadata in a catalog server's web page, make one side the source and generate the other from it; two sources that both accept edits drift apart.
  2. 以欄位 ID 作為欄位中介資料的鍵
    • 執行 rename-orphans-name-keys.py
    • 實用原則 以欄位 ID 作為欄位中介資料的鍵,並在旁邊附上名稱供人閱讀。名稱不再相符時,應將其視為重新檢視該欄位描述的理由,而不是新增欄位。若有系統只以名稱認識欄位,應從以 ID 為鍵的檔案產生其以名稱為鍵的檢視,切勿手動維護。
  3. 將描述寫入資料表的結構描述
  4. A description edit is a schema change
    • 執行 doc-edit-schema-id.py
    • 實用原則 Write column descriptions into Iceberg's doc, so every reader of the table gets them, and keep classifications in the reviewed file. Put no more than an owner and a domain in table properties, and say in your code why. To decide whether a schema change needs a review, compare field IDs, names and types, never schema IDs.
  5. Test the catalog against the metadata
    • 執行 metadata-gaps.py
    • 實用原則 Walk the catalog, not the metadata file, so a table nobody described shows up. Fail the test on any table without an owner and any column without one of the four classifications, and never default a missing classification to anything but the most protected value. A pipeline that creates a table ships with that table's metadata, in the same pull request.
  6. 在中繼資料過時之前攔截結構描述變更
    • 執行 schema-changes-checked.py
    • 實用原則 每次結構描述變更都要執行這兩項檢查:以 problems 找出無人分類的欄位,以及無人重新閱讀的已重新命名欄位;以 dropped 找出不描述任何東西的條目。刪除欄位的同一個 pull request 中,就要移除該欄位的條目。刪除個人資料欄位並不會移除其值,時間旅行仍可讀取這些值,直到保存它們的快照過期且其檔案被刪除為止。切勿以刪除再重新新增欄位的方式來重新命名:應就地重新命名,使其保留欄位 ID 與資料。
  7. 為了連接而分類假名化 ID
    • 執行 pseudonymous-learner-id.py
    • 實用原則 依 ID 所在資料表所揭露的內容來分類假名化 ID,讓分析師需要的連接得以持續運作,並在其描述中說明它透過哪張資料表解析。保護解析它的資料表,並在每次刪除作業中納入該 ID。誰能解析該 ID 是一項存取決策:列出可以未遮罩地同時讀取該 ID 與解析資料表個人欄位的使用者,並只把這組權限授予職責為將資料列連結到個人的人。
  8. 以本機模型草擬中繼資料
  9. 提示中的範例可能洩漏答案
    • 執行 suggestions-with-platform-examples.py
    • 實用原則 以模型起草中繼資料時,只使用你有權送出的輸入,並盡量使用在自己機器上運行的模型。記錄每個答案所用的模型、其修訂版本、提示與設定,讓審查者能看出每份草稿是由什麼產生的,並且在任何程式讀取答案之前,先解析並驗證每個答案。提示的範例應取自用來測量它的資料表之外。回報與已審查中繼資料的一致率時,應依各分類分別回報,不要只給一個數字。
  10. Measure where the model's drafts are wrong
    • 執行 suggestion-mistakes.py
    • 執行 figure-model-against-review.py
    • 執行 suggestion-descriptions.py
    • 實用原則 Let a model raise a column's classification, never lower it: only a reviewer lowers one, in the pull request, with the reason in the column's description. Treat the model's descriptions as first drafts for someone who knows the column to rewrite. Before a model drafts metadata for tables nobody has reviewed, measure it against your reviewed metadata, per classification and per direction of error, and measure again whenever the model or the prompt changes.
  11. 正式環境中會出什麼問題
  12. 本章的決定

3. 找出個人資料

閱讀約需 111 分鐘,含 12 個範例。

讀完本章後,你將能在資料平台上自動找出個人資料,並說明自己漏掉了多少。

課程大綱:13 節
  1. 從結構描述分類欄位
    • 執行 column-name-rules.py
    • 實用原則 先對每個欄位執行名稱規則,因為它們不需成本,並且只讓它們調高分類,絕不調低。規則要精簡、寫在程式碼中,並與其他規則一樣經過審查。任何自由文字欄位,無論名稱為何,都視為需要對其值取樣的欄位;其他型別的欄位則只依名稱分類。
  2. 離線執行 Presidio
    • 執行 presidio-offline-engine.py
    • 實用原則 明確設定 Presidio 的引擎:只執行模式時使用 NoOpNlpEngine,需要姓名時則使用自行安裝且固定版本的模型。在管線中絕不單獨呼叫 AnalyzerEngine(),並固定辨識器會下載的任何內容,例如公共尾碼清單。請將每個分數視為依其模式設定的常數,絕不當成機率來解讀。
  3. 每次執行都以相同方式抽樣欄位的值
    • 執行 seeded-column-sample.py
    • 實用原則 抽取非 NULL 的值,使用固定的種子,以單一執行緒執行,並將種子與結果一併保存。依據必須攔截的最罕見個人資料決定樣本大小,99% 的機率適用 n = ln(0.01) / ln(1 − p)。平台對每個文字欄位抽取 200 個值,此數值設定於 governance/pii.toml,對於由 1 in 43 或更多的值所含有的個人資料,有 99% 的機率攔截到。若資料比這更罕見,就掃描整個欄位,或改以規則分類。
  4. 從欄位的值進行分類
    • 執行 classify-sampled-columns.py
    • 實用原則 以名稱或任何取樣值所支持的最嚴格分類作為建議,並以 internal 為下限,同時隨建議一併呈現證據。比已審核的中繼資料更嚴格的建議,對資料表負責人而言是一項發現,由負責人提高分類,或記錄不提高的原因;絕不可讓偵測器降低分類。OpenMetadata 會略過任何已帶有 PII 標籤的欄位;我們建議改為在每次執行時對每個文字欄位取樣,因為已審核的標籤正是要被檢查的對象。
  5. Measure a detector against labelled spans
    • 執行 measure-pattern-recognizers.py
    • 實用原則 Measure every detector against labelled spans before it masks anything: recall per entity type by exact and by overlap matching, both with a label of the right type, coverage whatever the label, precision per label, and the share of texts with nothing personal left. Weigh recall first. Before you trust a detector on your text, label a few hundred real records by hand and measure on those too.
  6. 以真實標註調校辨識器
    • 執行 tune-pattern-recognizers.py
    • 執行 figure-detection-scores.py
    • 實用原則 切勿使用 Presidio 預設的門檻 0,也不要讓所有辨識器共用同一個門檻。只保留文字可能包含之識別碼所對應的辨識器,依各辨識器自己的分數設定門檻,並以從資料推得、符合使用者居住地的地區來讀取電話號碼。把這一切都寫進程式碼,並在每次變更時重新測量。
  7. See where patterns fail
    • 執行 pattern-hard-cases.py
    • 實用原則 Read a detector's misses by case, not by total, and keep a list of the cases your text holds. Use patterns for what has a checkable shape, such as emails, IBANs, card numbers, phone numbers and dates, and never for names or addresses. Keep the checksums and numbering plans, because they're what makes the patterns precise. Check the card pattern against the card numbers your payment provider issues, and the date patterns against the ways your users write dates.
  8. Compare a PII model
  9. Read the model's misses
    • 執行 privacy-filter-hard-cases.py
    • 實用原則 Mask free text with a PII model and the tuned patterns together, whatever either finds: the model for names and addresses, the patterns' checksums for card numbers and IBANs. Record the model's output with its revision, weights and operating point pinned, keep its own labels, and map them in code. Measure it on your own text, by hard case, before use and after every model change; if it misses too much, try an operating point that leans toward recall and record again. Treat masked text as reduced, never as anonymous.
  10. Count what a wrong label costs
    • 執行 privacy-filter-labels.py
    • 實用原則 Report coverage, whatever the label, beside recall with the right label: masking needs the first, classification the second. Read which labels a detector gives each type of span, and keep a recognizer with a checksum for every identifier whose label decides a classification, such as card numbers.
  11. Classify again when the schema changes
    • 執行 reclassify-new-columns.py
    • 實用原則 Classify every field ID the metadata doesn't have, in the pull request that adds it, and don't let it merge without a reviewed classification. Key the trigger on field IDs, never on schema IDs. Sample every text column again on a schedule too, because values change without a schema change.
  12. What goes wrong in production
  13. What this chapter decided

4. Lineage from pipelines

閱讀約需 103 分鐘,含 12 個範例。

學習結束時,學員能夠capture lineage from your pipelines as OpenLineage run events, with column lineage parsed from their SQL, including the columns that only join, filter and group。

課程大綱:11 節
  1. Send a run event from every pipeline
    • 關鍵詞 job, run, dataset, facet
    • 執行 pipeline-run-events.py
    • 實用原則 Send a START and then a COMPLETE or FAIL event from the code that runs each job, so no run can happen without them, and name datasets by catalog identifiers under a namespace you choose, never a path. In tests, use the file transport with a fixed event time and static run IDs, and compare what events say, never their bytes. In production, send through a transport to your lineage server, and derive each run ID from the scheduled time, the job and the attempt, so a resent event never creates a duplicate run.
  2. 將每個輸出欄位追溯到其來源
    • 執行 sqlglot-misses-row-inputs.py
    • 實用原則 用 sqlglot 的 lineage 取得每個輸出值所依據的欄位,並且一律傳入由資料表中繼資料產生的目錄結構描述,絕不手動輸入。不要把它的答案當成某個欄位所依賴的全部內容:它會漏掉用於連接、篩選與分組的欄位,也不會說明它的輸入中哪些是值。
  3. Add the inputs that decide which rows exist
    • 執行 indirect-column-lineage.py
    • 實用原則 Record INDIRECT inputs with every job's column lineage: the join keys, filters and grouping columns of every SELECT, CTEs included, in the facet's dataset list, and type each value input with OpenLineage's subtypes. Expect more edges, many of them join keys. Keep them anyway: a filter on a consent column decides who may be emailed, and it reaches the table only as an INDIRECT input.
  4. Fail on statements the parser can't read
    • 執行 insert-and-merge-lineage.py
    • 執行 misspelt-column-lineage.py
    • 實用原則 Treat a statement your parser can't read correctly as an error, never as empty or partial lineage. Refuse every kind of statement it gets wrong, such as MERGE, before you ask it, and fail on empty lineage, a source the parser can't place and a column the schema doesn't have. Match an INSERT's columns by position against the table's columns in the catalog, never the SELECT's names, and fail when the counts differ. For a MERGE, write a SELECT of the columns it writes, and record the job's lineage from that, as an INSERT ... SELECT into the MERGE's table, until your parser handles MERGE itself.
  5. 找出欄位變更會破壞什麼
    • 執行 column-impact.py
    • 執行 figure-category-reach.py
    • 實用原則 在由事件建立的圖上執行影響分析,並包含 INDIRECT 邊,然後回報兩份清單:值可能改變的欄位,以及列可能改變的資料表。僅用於篩選與連接的欄位若有變更,應視為對其所篩選之資料表的每個欄位都有變更,並將報告送交其所影響之每張資料表的負責人。
  6. Propose classifications from lineage
    • 執行 propagate-classifications.py
    • 執行 personal-filter-propagates.py
    • 實用原則 Propose each derived column's classification as the stricter of its inputs', counting INDIRECT inputs, and send the proposals to the table's owner for review before they become metadata. Never let lineage lower a classification someone declared, and never treat masking as declassification. Count DIRECT inputs only if your pipelines often join or filter on personal columns, such as matching on email, and every output would otherwise come out personal; then review INDIRECT personal inputs by hand.
  7. 正式環境中會出什麼問題
  8. Events that never arrive
    • 執行 lineage-disabled-silently.py
    • 實用原則 After every scheduled run, compare the jobs that ran with the jobs whose COMPLETE events arrived, and alert on any difference. Don't rely on the emitter to report its own silence.
  9. 沒有人加入檢測的工作
    • 執行 uninstrumented-job.py
    • 實用原則 定期以血緣核對目錄,並將沒有任何工作的血緣可以說明其來源的資料表視為事件處理:找出該工作,並讓它送出事件。
  10. Personal data in a table classified internal
    • 執行 personal-copy-tagged-internal.py
    • 實用原則 Run lib.lineage.understated, or your own check like it, on every lineage update, and fail when a declared classification is lower than lineage proposes. Classify by what a table holds, never by who reads it, and fix such a finding by raising the classification, not by dropping the edge.
  11. 本章的決定

5. Access policies as code

閱讀約需 93 分鐘,含 13 個範例。

學習結束時,學員能夠write a data platform's access rules as one policy that denies by default and returns row filters and column masks as data。

課程大綱:11 節
  1. 讓請求的形狀與 Trino 一致
    • 執行 trino-shaped-request.py
    • 實用原則 讓政策的輸入與將執行該政策之引擎的請求逐欄位一致,只加入該引擎無法傳送的內容,並放在頂層,讓審查者一眼就能看見。如果你的引擎不是 Trino,就改以其請求格式為起點,並像 lib.policy.request 那樣把形狀集中在單一函式中,使測試與呼叫端不會彼此偏離。
  2. 產生政策讀取的資料
    • 執行 generated-policy-data.py
    • 實用原則 每次載入政策時,都從擁有各項事實的檔案或資料表產生政策的資料,絕不將事實複製到政策或第二個檔案中。若某項事實尚無擁有者,例如客服人員所屬的地區,就為它指定一個檔案,並像程式碼一樣經過審查,如同平台對 users.toml 的做法。
  3. 預設拒絕,再依角色允許
    • 執行 allow-by-role.py
    • 實用原則 每份政策都以 default allow := false 開頭,並且只透過指明角色與資源的規則來允許。將角色到資料表的對應表集中在同一處,讓審查者能一次讀完全部內容,並為每個決策提供理由。當應該被允許的請求遭到拒絕時,請新增一條允許它的規則;切勿削弱預設值。
  4. Narrow by attribute: region, consent and purpose
    • 關鍵詞 region, purpose
    • 執行 attributes-as-row-filters.py
    • 實用原則 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.
  5. 以單一文件回傳決策
    • 執行 one-decision-document.py
    • 實用原則 每個請求回傳一份決策文件,內含允許與否、列篩選條件、欄位遮罩與原因,並將它視為一個整體來讀取。篩選條件與遮罩應保持為名稱與值,由你的執行程式碼負責轉換,絕不要讓政策寫出 SQL。若引擎需要政策提供 SQL,例如 Trino 對其列篩選條件與欄位遮罩的做法(Trino 484,OPA 存取控制),請在兩者之間以一個經過測試的函式產生。
  6. Unit-test the policy in Rego
    • 執行 policy-unit-tests.py
    • 實用原則 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.
  7. 從決策表檢查誰能看到什麼
    • 執行 who-sees-what.py
    • 實用原則 將「誰能看到什麼」的決策表與政策一同保存在儲存庫中,與單元測試一起執行,並以資料擁有者能夠檢查的用語,將其輸出形式呈現給他們。當擁有者不同意某一列時,先修改該列,再讓失敗的那一列來推動政策的修改。
  8. Know where regopy differs from OPA
    • 執行 regopy-silent-differences.py
    • 執行 regopy-failed-outputs.py
    • 執行 regopy-sprintf-quotes.py
    • 實用原則 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.
  9. Write the same rules in Cedar
    • 執行 cedar-same-rules.py
    • 實用原則 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.
  10. What goes wrong in production
  11. 本章的決定

6. 對查詢強制執行政策

閱讀約需 84 分鐘,含 16 個範例。

讀完本章後,你就能對平台執行的每一個查詢強制執行存取決策。

課程大綱:13 節
  1. 找出查詢讀取的每個資料表與欄位
    • 執行 tables-a-query-reads.py
    • 執行 unknown-tables-denied.py
    • 實用原則 針對查詢所指名的每個資料表與欄位詢問政策,並依據目錄的綱要展開 *,同時拒絕該綱要未包含的任何內容:未知的資料表、檔案、資料表函式,以及單一查詢以外的陳述式。拒絕的代價是向平台團隊提出一個問題;放行的代價則是該資料表所存放的一切。
  2. 只允許目錄資料表、CTE 與子查詢
    • 執行 catalog-sources-only.py
    • 實用原則 以目錄資料表、作用範圍內的 CTE 與子查詢組成的允許清單檢查每個來源,拒絕其餘一切,並拒絕引擎標記為有副作用的每個函式,且該清單取自引擎自己的資料。將這些案例保存為迴歸測試,用於每次變更這一層與每次升級引擎,並為每一種新的來源或函式新增一個案例。
  3. 遮罩欄位參照會造成洩漏
    • 先詢問 Predict how many of the five the rewrite stops:
    • 執行 select-list-masks-leak.py
    • 實用原則 不要以改寫欄位參照的方式來強制執行遮罩。無論你涵蓋多少種參照形式,引擎讀取欄位的方式都多於改寫程式所能辨識的。應改在資料的來源處進行遮罩,讓每一次讀取看到的都是遮罩後的值,如下一節所做的那樣。
  4. 以過濾並遮罩的子查詢取代每個資料表
    • 執行 table-rewrite.py
    • 執行 table-rewrite-holds.py
    • 實用原則 以依決策產生的子查詢取代每個受保護的資料表,沿用查詢本身的別名,並讓使用者的查詢其餘部分保持不變。為未讀取的欄位提供具型別的 NULL。若使用者需要整列讀取才能顯示值,則改為針對每個資料表的每個欄位詢問政策,使遮罩涵蓋所有欄位,代價是請求與稽核記錄會指名查詢從未用到的欄位。
  5. Write each mask as SQL in one place
    • 關鍵詞 redact, pseudonymize
    • 執行 masks-as-sql.py
    • 執行 pseudonym-key.py
    • 執行 redact-contacts-recall.py
    • 實用原則 Keep every mask's SQL in one table in the enforcement layer, and let the policy name masks, never write them. Key every pseudonym with a secret only the layer holds, passed as a parameter. Measure each text mask against labelled data before a policy relies on it, and name masks for what they remove, so nobody reads redact_contacts as "anonymized".
  6. 以決策中的資料建立列篩選條件
    • 執行 row-filters-from-data.py
    • 實用原則 一律以 SQL 建構器,根據決策中的值建立每個列篩選條件,並拒絕該層不認得的任何篩選條件形態或欄位。將未知的同意視為不同意。當篩選條件需要使用者不得讀取的資料表時,在子查詢內以該層自己的存取權限讀取,絕不可藉由擴大使用者的存取權限來達成。
  7. 讓每個欄位都受列篩選條件把關
    • 執行 row-filter-order.py
    • 實用原則 絕不要依賴引擎的評估順序,來讓使用者的條件遠離被篩選條件排除的列。要讓使用者查詢能讀取的每個值都依賴列篩選條件,並在每次引擎升級時測試每一種篩選形式。若引擎有自己的屏障機制,例如 PostgreSQL 的 security_barrier 檢視表,也要一併使用。
  8. 以執行查詢來測試誰能看到什麼
    • 執行 who-sees-what-in-queries.py
    • 執行 group-change-caught.py
    • 實用原則 每次變更政策、使用者檔案、中繼資料或這一層時,都要透過這一層以查詢執行「誰能看到什麼」的案例,只要有任何案例不一致,就讓建置失敗。修正失敗的案例時,應變更政策或使用者檔案。只有在資料擁有者同意使用者應取得的內容已經改變時,才修改案例。
  9. 在報表中隱藏小群組
    • 執行 small-groups-difference.py
    • 實用原則 凡是低於 governance/enforcement.toml 中最小值的報表群組,一律保留不顯示,並把最小值視為防止查找單一個人的保護措施,而絕非防範差分攻擊的措施。不要依讀者無權讀取的屬性來細分報表,再把報表交給看得到其背後資料列的讀者:講師按國家細分的報表,就是國家的查找表。請取消該細分,或在加入雜訊之後才報告。
  10. Hand the same policies to an engine that enforces them
    • 執行 trino-requests.py
    • 實用原則 Put enforcement in the engine every tool queries through, with one policy for it and for any rewrite like this chapter's. With Trino, answer every operation the plugin asks about, set both the row-filter and column-mask URIs (without them it applies no filters or masks), and return row filters only from a decision that allows the read. Build tests from the request shapes in the plugin's source, not from its documentation's example policies.
  11. 代為查詢
    • 執行 assistant-on-behalf.py
    • 實用原則 以助理所回答的對象的身分執行助理的查詢,該身分取自對方已驗證的工作階段,並讓這一層套用該對象的決定。只有在助理執行不針對特定對象的工作時,才使用助理自己的身分,例如為課程頁面每晚彙整經同意的評論摘要,並將兩種身分都記錄在稽核日誌中。建立助理訓練資料集的人,則以自己的身分查詢,並以 ai-training 作為目的,如第 9 章所做的那樣。
  12. 正式環境中會出什麼問題
  13. 本章的決定

閱讀約需 101 分鐘,含 11 個範例。

讀完本章後,你能把保存規則維護成經過審查的設定,讓撤回同意影響下一次查詢,並從個人資料流經的每一張資料表中抹除該人的資料。

課程大綱:13 節
  1. Keep retention rules in a reviewed file
    • 執行 retention-rules-due.py
    • 實用原則 Keep each table's retention period, the column it counts from and the state a row must be in, in a reviewed file, and apply it against a day the scheduler passes in, never the clock. Count from the date the rule means. When the table doesn't record that date, as the platform's tickets don't record when they closed, ask the table's owner to add the column rather than let the rule count from another date for good, and say in the rule's file which way it errs until then.
  2. 以列刪除移除過期的資料列
    • 執行 apply-retention.py
    • 實用原則 以 Iceberg 的列刪除逐條套用保存期限規則,並將規則與日期記錄在每次提交的快照摘要中。讓作業對於特定日期具有冪等性。大型資料表應依規則計算期限所依據的欄位分區,使其刪除能直接捨棄整個檔案而非重寫;在 PyIceberg 中,需安裝其 pyiceberg-core 額外套件才能寫入這類資料表。必須等到舊快照過期且其檔案被移除,才算作業完成。
  3. 在查詢執行之處讀取同意狀態
    • 執行 consent-withdrawal.py
    • 實用原則 將同意狀態保存在查詢執行之處:放在一張由學習者所使用的系統維持最新的資料表中,由政策以資料列篩選條件讀取,如此撤回同意會改變下一次查詢,而不會刪除任何資料。當平台的法務人員判定撤回同意同時也是刪除的理由時,也要將它送入刪除路徑。每當同意狀態改變,就要重建每一張以同意篩選條件建立的資料表,或讓其讀取者套用相同的篩選條件。將每一筆同意記錄為一個狀態及其變更時間,並由此推導出政策所讀取的真假值屬性,其中未知視為未給予。
  4. 從歷程血緣規劃抹除作業
    • 執行 erasure-plan.py
    • 實用原則 每一次抹除都要從資料目錄與歷程血緣圖規劃,而不是依據某人維護的清單:先找出每個具有該人 ID 欄位的資料表,再找出歷程血緣顯示其資料列所觸及的每個資料表。對衍生資料表,依歷程血緣顯示為 ID 原封不動複本的欄位刪除,並重建那些僅受該人資料列影響的衍生資料表。例外放在設定檔中,由法務團隊連同理由一併寫下,讓計畫讀取並顯示;絕不讓程式碼自行決定例外。請先執行第 4 章中檢查沒有任何作業的歷程血緣可說明的資料表,因為計畫無法觸及歷程血緣未顯示的副本。
  5. Erase the rows the plan lists
    • 執行 erase-learner.py
    • 實用原則 Carry out an erasure as row deletes in the plan's order, and record only a request ID in each commit, never a value of the person's. Treat the delete as the first of three steps: the snapshots before it still hold the person, and anyone allowed to time travel can read them until they're expired.
  6. Expire snapshots by the table's own settings
    • 執行 expiry-ignores-table-settings.py
    • 執行 erasure-refuses-to-start.py
    • 實用原則 Expire snapshots through one function of your own that reads gc.enabled and the history.expire settings before it expires anything, and refuses when garbage collection is off, because PyIceberg's expiry reads neither; check again at every PyIceberg release. Before an erasure deletes anything, check every table in its plan the same way, and refuse the whole erasure when one would keep the person. For an erasure, expire every snapshot older than the delete. For retention, expire by age, as history.expire.max-snapshot-age-ms says, measured on the clock, since snapshots carry real commit times.
  7. 移除沒有任何快照參照的檔案
    • 執行 sweep-unreferenced-files.py
    • 實用原則 每次過期之後都要移除未被參照的檔案,由於 PyIceberg 沒有這項功能,請自行以 inspect 資料表建立清除程序;在正式環境中,請保留以天為單位的最短存續時間,讓進行中的寫入保有其檔案。只有在單一行程寫入的情況下,才使用零作為存續時間。不要因為沒有任何資料檔案含有某人,就認定此人已被抹除:manifest 中可能仍保有其值。
  8. Keep personal values out of manifests
    • 執行 statistics-off-for-personal-columns.py
    • 實用原則 Generate per-column metrics properties from your classifications, none or counts for every personal, sensitive or unclassified column, and apply them when a table is created or, for an existing table, with one rewrite followed by expiry and a sweep. Keep statistics on the ID columns you filter and erase by. Generate the properties again after every schema change.
  9. Prove the erasure by reading every file
    • 執行 verify-erasure.py
    • 實用原則 End every erasure with a scan of every file the platform stores, for the person's IDs and identifying values, read with each format's own reader, and keep its result, counts and IDs only, as the evidence. Search for as much of each value as every bound keeps unchanged, which is one character less than the truncation length. Treat any file that still holds the person as a failed erasure, not a warning. Search the stores outside the warehouse that you control, and list in the record the ones you can't search.
  10. 正式環境中會出什麼問題
  11. 已刪除的學員又回來了
    • 執行 erased-learner-returns.py
    • 實用原則 先在來源系統中刪除,或在同一請求中一併刪除,並維護一份已刪除 ID 的金鑰雜湊登記簿,讓每次載入都據以過濾、每次執行都進行檢查,且金鑰作為機密與資料分開保管。出現已刪除 ID 的資料列時,應如同資料外洩一般視為事件處理。
  12. 其他失敗情況
  13. 本章的決定

8. 稽核與證據

閱讀約需 66 分鐘,含 9 個範例。

讀完本章後,你可以為每一項存取決策與查詢建立一份稽核日誌,日誌中不含任何值,並以鏈結方式串接,使任何修改都會顯現。

課程大綱:9 節
  1. 在決策發生之處記錄每一項決策
    • 執行 decision-record.py
    • 實用原則 在 principal、查詢與決策交會的執行層寫入稽核紀錄,並在查詢執行之前寫入,使寫入失敗時能中止查詢。記錄所涉及的每一種身分,包括本人及代其行事的任何助理,以及每個資料表的決策,連同其篩選條件、遮罩與理由。當由引擎自行執行政策時,例如 SQL 引擎 Trino 透過 OPA 進行的方式,應改為啟用 OPA 的決策日誌,並確認其輸入載明所涉及的每一種身分:例如 Trino 的 opa.identity.extra-credentials-keys 會將所列出的憑證「以 extraCredentials 的形式在身分脈絡中轉送給 OPA」(Trino 484, OPA access control)。
  2. 讓資料值遠離紀錄
    • 執行 query-without-literals.py
    • 執行 query-shape-hard-cases.py
    • 實用原則 記錄查詢的形狀,絕不記錄其值:替換每個字面值(引擎接受的每一種語法皆然)與目錄中不存在的每個名稱,捨棄註解,同樣移除拒絕理由中的值,並且只以金鑰摘要保留所執行的 SQL,而日誌的讀取者永遠拿不到該金鑰。不要對可能含有個人資料的文字使用單純的雜湊。若需要知道誰讀取過某個人的資料,請把這項紀錄存放在保存期限與抹除都能涵蓋的另一份日誌中,而不是放在防竄改的日誌裡。
  3. 以原本的政策重播舊決策
    • 執行 explain-old-decision.py
    • 實用原則 為每一項決策取指紋,內容是除請求之外它所依賴的一切的雜湊:政策的模組及其資料文件,合在一起計算。每個修訂版本都要封存,保存時間與日誌一樣長,並且以該決策自己的修訂版本重播來解釋舊決策,絕不使用目前的版本。如果政策會讀取時鐘或呼叫其他服務,也要記錄那些結果,就像 OPA 的 nd_builtin_cache 所做的一樣。
  4. 將紀錄串成鏈,讓修改無所遁形
    • 先詢問 Before you run it, predict which of the five the chain finds on its own, and which against the noon head.
    • 執行 chain-catches-edit.py
    • 實用原則 以 SHA-256 與序號把每筆紀錄串接到前一筆,並且只指望這條鏈保護到最後一個記錄在該層控制範圍之外的鏈頭為止的紀錄。要頻繁記下鏈頭,寫入僅可附加的儲存空間,或寫入使用其他憑證的另一個系統,頻率取決於你能接受多少筆未受保護的紀錄;若這個數字是零,就在每筆紀錄寫入時立即送出機器。依排程對照最後一個鏈頭檢查鏈。若需要 AU-9(3) 所描述的做法,就讓每個鏈頭由一個該層永遠不持有其金鑰的系統簽章。切勿把日後可能必須為某個人而抹除的紀錄串入鏈中;讓整個期間自行到期即可。
  5. 以 DuckDB 的查詢日誌交叉核對稽核日誌
    • 執行 query-log-cross-check.py
    • 實用原則 在你執行政策強制的每個地方,都開啟引擎自己的查詢日誌,在該層內擷取每個結果,並定期以自上次檢查以來寫入的紀錄核對日誌,且雙向核對:引擎執行的每一筆查詢都必須對應一筆紀錄,每一筆被允許的紀錄都必須對應一筆查詢。核對後再將日誌清空,因為其中保存著查詢的值。把不一致視為需要說明的事件,而不是要過濾掉的雜訊。這項檢查只能指望抓出錯誤,抓不到持有連線並存心隱藏的程式碼:對這類程式碼,只有寫在行程之外的日誌才有幫助。切勿把引擎的日誌當作稽核日誌:它沒有使用者,也沒有決策。
  6. 從日誌產生存取審查
    • 執行 access-review.py
    • 實用原則 存取審查應從政策與稽核日誌產生,絕不要從角色成員清單產生:比對每個人可以讀取的內容與實際讀取的內容、他們被拒絕的紀錄,以及政策已不認得的成員關係。審查期間要長到涵蓋每項工作的節奏,例如一季,並把每項發現送交相關資料表或群組的擁有者。除非擁有者說明為何需要,否則應移除期間內無人使用的授權。
  7. 以資料形式產出證據
    • 執行 evidence-report.py
    • 實用原則 以程式碼依據紀錄產生證據,依排程執行,並在每次政策變更時執行;在每一節蓋上戳記,註明政策版本、各項輸入的摘要值,以及重新計算所需的條件,並說明哪些部分無人能重新計算。將報告存放在日誌寫入者無法更動之處,納入未通過的發現而非將其濾除,並讓每份報告以上一份所寫下的鏈頭來核對鏈。能產生報告時,絕不以螢幕截圖代替;第 10 章會在每個 pull request 上執行這類檢查。
  8. 正式環境中會出什麼問題
  9. 本章的決定

9. 為 AI 治理資料

閱讀約需 116 分鐘,含 11 個範例。

讀完本章後,你將能在與平台其他所有資料讀取相同的規則下,建構 AI 助理用來學習的資料。

課程大綱:12 節
  1. Admit only reviews whose authors consented
    • 執行 corpus-consent-filter.py
    • 實用原則 Decide consent per record, when the corpus is built, from the answer your legal team says covers the use, and admit only records whose answer is yes: never filter out the refusals and keep the rest. Build the corpus through the enforcement layer with its purpose stated, and have the build check that the layer filters on that answer. Keep the legal team's answers, with their reasons, in a reviewed file the code reads, and carry licences per record when a source mixes them.
  2. Replace personal data in the text, then measure what's left
    • 執行 corpus-personal-data-left.py
    • 執行 privacy-filter-on-the-corpus.py
    • 實用原則 Detect personal data in corpus text after the enforcement layer's masks, add the identifiers your platform holds about the people the text is about, and measure what's left against labelled data, by value, after every change to the detector. Prefer a short list of the names your text is about to every name you hold, since a long list matches ordinary words. Have a job that may read the names build the list, and record which list each corpus used. Measure a named-entity model the same way before you rely on it for names no list holds. Keep the list out of the corpus, and call the result pseudonymized.
  3. 以金鑰雜湊維持假名的一致性
    • 執行 keyed-pseudonyms.py
    • 實用原則 以只有作業持有的金鑰,用 HMAC 產生假名,並為假名不得互相連結的每個語料庫或對象各配一把獨立的金鑰。不要在未加鹽的情況下以 Presidio 的 hash 運算子產生假名,因為在其鎖定版本中每次雜湊都是隨機的。當兩個人可能共用姓名中的某個字時,應對人員的 ID 而非姓名的字詞做假名化;下游不需要區分人員時,則使用單純的佔位符。
  4. Find near-duplicates with MinHash
    • 執行 corpus-duplicates.py
    • 執行 names-and-near-duplicates.py
    • 實用原則 Deduplicate training text in two steps, exact copies by a hash of the normalized text and then near-duplicates by MinHash, each confirmed by its exact similarity, and report how much each step removed. Pseudonymize first, so that no name reaches the comparison or the sketches and the comparison can tell which pairs differ only in a name. Keep those pairs apart when the text's meaning depends on whom it names, and compare on placeholders when it doesn't. Choose the threshold by reading the pairs close above and below it in your own text.
  5. Tune LSH for recall, then confirm every pair
    • 執行 lsh-misses-near-duplicates.py
    • 執行 figure-lsh-candidate-chance.py
    • 實用原則 Tune LSH below your similarity threshold, so that it misses few pairs, and confirm every candidate by its exact similarity. Measure LSH's misses against an exhaustive comparison on a sample of your corpus whenever its settings, its library or the text change. Pin the MinHash library, write its hashing scheme into your settings, and never compare counts or stored sketches made under different schemes.
  6. 讓每一筆記錄都帶有來源資訊
    • 執行 corpus-provenance.py
    • 實用原則 讓每一筆記錄都帶有其來源資料表、該列的 ID,以及寫入它的作業與執行,並依查詢實際執行的樣貌記錄作業的血緣,包含篩選與遮罩。透過這些來源資訊,以今天的同意狀態檢查舊的建置。在模型看到任何檢索結果之前,依讀者本身對每筆記錄來源列的權限決定,篩選每一次檢索。
  7. Generate the dataset card from the run
    • 執行 corpus-dataset-card.py
    • 實用原則 Generate the card in the run that builds the data, with every count, hash, source and version taken from that run, and write the format's version out. Check the card with your own code for every property the specification requires, since the library's validator doesn't report them all, and don't publish or train on a corpus whose card fails.
  8. 引述歐盟《人工智慧法》對訓練資料的規定
    • 實用原則 請引述經修正後的法規,取自官方公報,並附上連結與你閱讀的日期,因為數位綜合修正案更動了條文與日期。不要把法律放進管線的邏輯中:卡片上的事實應由執行過程產生,並由回答法律問題的人決定哪些條文適用。
  9. 正式環境中會出什麼問題
  10. A learner withdraws after the build
    • 執行 consent-withdrawn-after-build.py
    • 實用原則 Record which build, by run ID, every model and index was made from. Before every training run, check the build's records against today's consent and the file's hash against its card, and refuse a build that fails either. When a learner withdraws, find the builds that hold their records and the models made from them, and let your legal team's answer in config decide what happens to those.
  11. 其他失敗情形
  12. What this chapter decided

10. CI 中的治理

閱讀約需 83 分鐘,含 10 個範例。

讀完本章,你可以在每個 pull request 上,把第 2 章到第 7 章的治理檢查當成單一工作來執行,而且只評判該 pull request 所變更的內容。

課程大綱:14 節
  1. 將所有檢查當作單一工作來執行
    • 執行 governance-ci-report.py
    • 實用原則 在每個 pull request 上,將所有檢查當作單一工作來執行,每項檢查都以穩定的 ID 回報其所有發現,當機的檢查則視為一則失敗的發現。對於 pull request 引入、且既未豁免也未核准的任何發現,以結束碼讓工作失敗。對於主分支上已有的每則發現,予以回報、計數並標明其擁有者,但不讓維持現狀的 pull request 失敗。將檢查清單存放在經過審查的檔案中。
  2. 把關在拉取請求,而不是提交
    • 實用原則 掛鉤用於在作者機器上對單一檔案做快速檢查,並讓拉取請求上的 CI 工作成為把關者,由主要分支的分支保護強制要求。每個發現都要放在拉取請求本身,附上其 ID 與處理方式,絕不只放在儀表板上。對於會在任何拉取請求之外變動的資料表,也要定期對正式環境的資料目錄執行這些檢查。
  3. 回報結構描述變更的影響範圍
    • 執行 schema-change-impact.py
    • 實用原則 以欄位 ID 比對結構描述差異,絕不以名稱或結構描述 ID 比對,並且對每個改名、型別變更與移除,以三種方式回報:Iceberg 讀取端是否受影響、哪些管線的 SQL 指名該欄位,以及它透過血緣(包括 INDIRECT 邊)影響到什麼。把報告寄給它所影響的每個資料表的擁有者。會破壞管線的改名,要與該管線的修正在同一個 pull request 中合併,絕不能早於修正。
  4. 降低分類等級前先詢問擁有者
    • 執行 classification-downgrade.py
    • 實用原則 將降低分類、變更擁有者與移到另一個領域,視為需要主分支上該資料表擁有者核准的變更,並在核准之前讓作業失敗;切勿依賴破壞性變更檢查工具來處理這些變更。要求在同一個拉取請求中,於欄位的描述寫明理由。提高分類不需要這類核准,因為它只會隱藏更多內容。
  5. 移除欄位前先將其標示為棄用
    • 執行 deprecate-then-remove.py
    • 實用原則 以兩個 pull request 移除欄位:先將它標示為棄用,並在描述中寫明替代方案,然後合併;再將它從資料表中移除,並刪除中繼資料中的項目。任何移除主分支尚未標示為棄用之欄位的變更,都應使檢查失敗。棄用期要持續多久,須與該欄位的讀取者商定,因為此作業只檢查棄用是否在先。
  6. Route each domain's reviews by path
    • 執行 domain-contracts.py
    • 實用原則 Keep each domain's metadata under a path of its own, as one ODCS contract per table with one domain and one owning team, generated from the metadata and checked by the job, with a CODEOWNERS line for that team. Give the platform team the CODEOWNERS file, the job's settings, its global rules, the waivers and the migrations. Require code-owner review, and count approvals per team in the job when one isn't enough.
  7. 在全域規則之上疊加領域規則
    • 執行 global-and-domain-rules.py
    • 實用原則 把每個領域都必須遵守的規則寫成一個由平台團隊維護的全域套件,並讓每個領域再加入一個由其團隊擁有、只對該領域合約執行的自有套件。違規應以具有穩定主體的資料形式回傳,而不是單純的通過或失敗。領域規則只能補充全域規則,不能取代它;並把資料表移到另一個領域視為所有權變更,這樣就沒有團隊能靠更改一個字來逃避原領域的規則。
  8. 計算每項檢查被豁免的發現數
    • 執行 waived-findings.py
    • 實用原則 讓一項豁免恰好指明提取請求所引入的一項發現,並附上負責團隊、理由,以及最多只在幾個月之後的結束日期,同時列出任何豁免都不得靜默的檢查,例如缺少分類或擁有者。針對每項檢查,回報有多少發現無人處理,無論是已豁免或留在 main 上,並審查其中多數屬於此類的任何檢查:不是檢查有問題,就是平台有問題,而這個計數無法分辨是哪一種。
  9. 正式環境中會出什麼問題
  10. A check that reads every row
    • 執行 slow-check-every-row.py
    • 實用原則 Size pull-request checks by the change, not by the data: metadata, schemas, policies and the names of the columns a pull request adds. Run checks that read values on a schedule, with a fixed sample, as chapter 3 does, and send their findings to the owning team. Document where the job's checks stop, such as at a column added empty.
  11. 標記每個 ID 的檢查
    • 執行 noisy-check-waivers.py
    • 實用原則 新增檢查之前,先在主要分支上執行並閱讀每一項發現;如果大多數發現都會被豁免,就應修改規則,而不是豁免這些發現。對於發現長期大多被豁免的檢查,應將其移除;對於正確的檢查出現高比例的發現,則應視為要排定的工作,並指定負責人與日期。
  12. 損壞的檔案會成為一項發現
    • 執行 broken-files-become-findings.py
    • 實用原則 把當機的檢查轉成一項會使工作失敗的發現,附上錯誤的第一行與它所指的檔案,仍要執行其他檢查,且絕不讓豁免將它壓下。
  13. 其他失效情形
  14. 本章的決定

測驗題庫

每章皆附習題與解答,供經驗證的教育工作者使用。每個解答都對應其所屬的節。 如何取得