AI 翻译。由人工以英文核验。 阅读原文

数据治理即代码

教学套件

来自版次 2026.11.36,即本书当前版次。本书变更时,此版次随之变更。可自由使用和改编,但须注明出处。

教学大纲

阅读时长仅针对本章正文。学生还可在实验环境或自己的机器上运行其中的示例。

每周一章:10周

5 周后

学习目标与大纲

每章的学习目标即其第一句。每份大纲依循该章的节,列出该章定义的术语、运行示例前要求做出的预测,以及每节结尾的规则。 其关键术语和待提问的预测内容采用英文版次,与测验题库的设置一致。

1. 端到端的数据治理

阅读约 57 分钟,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
    • 工作准则 不要把治理元数据保存在没有任何检查的文档中。应把它保存在经过评审的文件中,由测试在每个拉取请求上并按计划将其与实时目录对照。如果人们需要电子表格,就从该文件生成,并在表上注明生成时间。
  7. 一个工具强制执行、另一个工具绕过的策略
    • 运行 read-around-the-layer.py
    • 工作准则 选定一处执行策略,即所有工具查询所经过的引擎,并且只把存储凭证交给它和写入这些表的作业,绝不交给个人或笔记本。在该处拒绝每一个不经由目录表读取数据的查询。在把一张表算作已受保护之前,列出所有能读取其文件的对象,并把任何不询问策略的读取方视为绕过,除非它是一个作业,且其输出像它所读取的表一样已编入目录并完成分类。
  8. 本章的决定

2. 可测试的目录

阅读约 83 分钟,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 查找不描述任何内容的条目。在删除某列的同一个拉取请求中移除该列的条目。删除含个人信息的列并不会移除其值,时间旅行仍可读取这些值,直到保存它们的快照过期、相应文件被删除为止。切勿通过删除再重新添加来重命名列:应原地重命名,这样它才能保留字段 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. 发现个人数据

阅读约 110 分钟,12个示例。

学完本章,你将能够在数据平台上自动发现个人数据,并说明漏掉了多少。

讲义大纲:13节
  1. 根据模式对列分类
    • 运行 column-name-rules.py
    • 工作准则 先对每一列运行名称规则,因为它们没有成本,并且只允许规则提高分类级别,不允许降低。规则要简短,写在代码中,并像其他规则一样接受评审。任何自由文本列,无论叫什么名称,都应视为需要抽样检查其值的列;其他类型的列则只按名称分类。
  2. 离线运行 Presidio
    • 运行 presidio-offline-engine.py
    • 工作准则 显式配置 Presidio 的引擎:只运行模式时使用 NoOpNlpEngine,需要姓名时使用自己安装的固定版本模型。切勿在流水线中直接调用 AnalyzerEngine(),并固定识别器会下载的一切内容,例如公共后缀列表。把每个得分都看作为其模式设定的常量,而不是概率。
  3. 每次运行都以相同方式抽样一列的值
    • 运行 seeded-column-sample.py
    • 工作准则 对非空值抽样,使用固定的种子,在单线程上运行,并将种子与结果一并保存。按必须捕获的最罕见个人数据来确定样本大小,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

阅读约 102 分钟,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

阅读约 84 分钟,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. 用 Cedar 编写同样的规则
    • 运行 cedar-same-rules.py
    • 工作准则 当每个问题都是“该主体可以对该资源执行此操作吗?”时,例如应用程序的权限检查,并且你希望在运行之前由模式检查每条策略,就选择 Cedar。当答案必须携带数据时,例如数据仓库的行过滤器和列掩码,或者当引擎本身已经在询问 OPA 时,例如 Trino,就选择 Rego。对于平台的数据仓库,我们推荐 Rego。
  10. What goes wrong in production
  11. 本章的决定

6. 对查询实施策略

阅读约 82 分钟,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. 本章的决定

阅读约 99 分钟,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 表自行构建的清理来完成;在生产环境中保持以天计的最小存留时间,使正在进行的写入保有其文件。只有在单个进程写入的场合才使用零时间。不要因为没有数据文件含有某人,就认为此人已被擦除:清单中可能仍保留着他们的值。
  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. 审计与证据

阅读约 65 分钟,9个示例。

读完本章,你将能够为每一项访问决策和每一次查询保存一份审计日志,日志中不含任何取值,并且各条记录前后链接,使任何改动都能被发现。

讲义大纲:9节
  1. 在决策发生之处记录每一项决策
    • 运行 decision-record.py
    • 工作准则 在主体、查询和决策交汇的执行层写审计记录,并在查询运行之前写入,使写入失败时查询随之停止。记录涉及的每一种身份,包括本人和代其行事的任何助手,以及每张表的决策及其过滤器、掩码和原因。当引擎自行执行策略时,例如 SQL 引擎 Trino 通过 OPA 执行,应改为开启 OPA 的决策日志,并确保其输入标明所涉及的每一种身份:以 Trino 的 opa.identity.extra-credentials-keys 为例,它会把所列出的凭据“作为身份上下文中的 extraCredentials 转发给 OPA”(Trino 484,OPA 访问控制)。
  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 章会在每个拉取请求上运行这类检查。
  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 中的数据治理

阅读约 85 分钟,10个示例。

学完本章,你将能够把第 2 章至第 7 章的治理检查作为一个作业,在每个拉取请求上运行,并且只评判该拉取请求所改动的内容。

讲义大纲:14节
  1. Run every check as one job
    • 运行 governance-ci-report.py
    • 工作准则 Run every check as one job on every pull request, each reporting all its findings under stable IDs, and a crashed check as a failing finding. Fail the job by exit code on any finding the pull request introduces that is neither waived nor approved. Report every finding already on main, counted and named with its owner, without failing a pull request that leaves it as it is. Keep the list of checks in a reviewed file.
  2. 把关拉取请求,而不是提交
    • 工作准则 用钩子在作者的机器上对单个文件做快速检查,并让拉取请求上的 CI 作业成为关卡,由主分支上的分支保护将其设为必需。把每个发现都放在拉取请求本身中,附上其 ID 和应对办法,不要只放在仪表板上。对于在任何拉取请求之外发生变化的表,还要按计划针对生产目录运行这些检查。
  3. 报告模式变更的影响范围
    • 运行 schema-change-impact.py
    • 工作准则 按字段 ID 而不是按名称或模式 ID 比较模式,并从三个方面报告每一次重命名、类型变更和删除:Iceberg 读取方是否受影响,哪些流水线的 SQL 引用了该列,以及它通过血缘(包括 INDIRECT 边)影响到什么。把报告发送给它所影响的每张表的负责人。会破坏流水线的重命名,要与该流水线的修复在同一个拉取请求中合并,不得早于修复。
  4. 降低分类前先询问所有者
    • 运行 classification-downgrade.py
    • 工作准则 把降低分类、更改所有者和移到另一个域视为需要该表在主分支上的所有者批准的变更,并在批准给出之前让作业失败;绝不要依赖破坏性变更检查器来处理这些变更。要求在同一个拉取请求中于该列的描述里写明理由。提高分类不需要这样的批准,因为它只会隐藏更多内容。
  5. 先弃用列,再删除列
    • 运行 deprecate-then-remove.py
    • 工作准则 分两个拉取请求删除一列:先将其标记为弃用,并在描述中写明替代方案,然后合并;再从表中删除该列,并从元数据中删除其条目。对主分支尚未弃用的列,任何移除都应判定失败。弃用期持续多久,应与该列的读取方商定,因为该作业只检查弃用是否在先。
  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
    • 工作准则 让一项豁免恰好指明拉取请求引入的一个发现,并附上负责团队、理由和最多几个月之后的截止日期;同时列出任何豁免都不得压制的检查,例如缺失的分类或负责人。对每项检查,报告有多少发现无人采取行动,无论是被豁免的还是留在主干上的,并评审其中此类发现占多数的检查:要么检查有问题,要么平台有问题,而这个计数无法说明是哪一种。
  9. 生产环境中的问题
  10. 逐行读取的检查
    • 运行 slow-check-every-row.py
    • 工作准则 拉取请求检查的规模应由变更决定,而不是由数据决定:检查元数据、模式、策略,以及拉取请求所添加列的名称。读取值的检查应按计划运行,并使用固定的样本,如第 3 章所做的那样,其结果交给所属团队。应记录作业的检查在何处止步,例如在以空列方式添加的列上。
  11. 标记每个 ID 的检查
    • 运行 noisy-check-waivers.py
    • 工作准则 在添加检查之前,先在主分支上运行它并阅读每一项发现;如果大部分发现都会被豁免,就修改规则,而不是豁免这些发现。对于发现长期大多被豁免的检查,应将其移除;对于正确的检查出现高比例,则应将其视为需要安排的工作,并指定负责人和日期。
  12. 损坏的文件会变成一条发现项
    • 运行 broken-files-become-findings.py
    • 工作准则 把崩溃的检查转换为一条使作业失败的发现项,其中包含错误的第一行和它所指明的文件;无论如何都要运行其他检查,并且绝不允许豁免将它静默。
  13. 其他失效情形
  14. 本章的决定

测验题库

为通过认证的教育者提供每章的习题及参考答案。每个答案均对应其所在的节。 如何获取