SLSA 定義

BuildKit 支援 建立 SLSA 來源證明,用於它執行的構建。

BuildKit 生成的來源證明格式由 SLSA 來源證明格式 定義。

此頁面介紹 BuildKit 如何填充每個欄位,以及在您生成證明時是否會包含該欄位 mode=minmode=max.

builder.id

對應於 SLSA builder.id.

包含在 mode=minmode=max 中。

如果可用,builder.id 欄位將設定為構建的 URL。

    "builder": {
      "id": "https://github.com/docker/buildx/actions/runs/3709599520"
    },

此值可以使用 builder-id 證明引數設定。

buildType

對應於 SLSA buildType.

包含在 mode=minmode=max 中。

buildType 欄位設定為 https://mobyproject.org/buildkit@v1 可用於確定來源證明內容的結構。

    "buildType": "https://mobyproject.org/buildkit@v1",

invocation.configSource

對應於 SLSA invocation.configSource.

包含在 mode=minmode=max 中。

描述初始化構建的配置。

    "invocation": {
      "configSource": {
        "uri": "https://github.com/moby/buildkit.git#refs/tags/v0.11.0",
        "digest": {
          "sha1": "4b220de5058abfd01ff619c9d2ff6b09a049bea0"
        },
        "entryPoint": "Dockerfile"
      },
      ...
    },

對於從遠端上下文(如 Git 或 HTTP URL)初始化的構建,此物件在 uridigest 欄位中定義上下文 URL 及其不可變摘要。對於使用本地前端(例如 Dockerfile)的構建,entryPoint 欄位定義了用於初始化構建的前端檔案路徑(filename 前端選項)。

invocation.parameters

對應於 SLSA invocation.parameters.

部分包含在 mode=min 中。

描述傳遞給構建的構建輸入。

    "invocation": {
      "parameters": {
        "frontend": "gateway.v0",
        "args": {
          "build-arg:BUILDKIT_CONTEXT_KEEP_GIT_DIR": "1",
          "label:FOO": "bar",
          "source": "docker/dockerfile-upstream:master",
          "target": "release"
        },
        "secrets": [
          {
            "id": "GIT_AUTH_HEADER",
            "optional": true
          },
          ...
        ],
        "ssh": [],
        "locals": []
      },
      ...
    },

以下欄位包含在 mode=minmode=max

  • locals 列出了構建中使用的任何本地來源,包括構建上下文和前端檔案。

  • frontend 定義了用於構建的 BuildKit 前端的型別。目前,這可以是 dockerfile.v0gateway.v0

  • args 定義了傳遞給 BuildKit 前端的構建引數。

    args 物件中的鍵反映了 BuildKit 收到的選項。例如,build-arglabel 字首用於構建引數和標籤,而 target 鍵定義了構建的目標階段。source 鍵定義了閘道器前端的來源映象(如果使用)。

以下欄位僅包含在 mode=max

  • secrets 定義了構建期間使用的秘密。請注意,不會包含實際的秘密值。
  • ssh 定義了構建期間使用的 ssh 轉發。

invocation.environment

對應於 SLSA invocation.environment.

包含在 mode=minmode=max 中。

    "invocation": {
      "environment": {
        "platform": "linux/amd64"
      },
      ...
    },

BuildKit 目前設定的唯一值是當前構建機器的 platform。請注意,這並不一定是構建結果的平臺,可以從 in-toto 主題欄位中確定。

materials

對應於 SLSA materials.

包含在 mode=minmode=max 中。

定義所有作為構建一部分的外部工件。該值取決於工件的型別

  • 包含映象原始碼的 Git 儲存庫的 URL
  • 如果您是從遠端 tarball 構建,或使用 Dockerfile 中的 ADD 命令包含的 HTTP URL
  • 構建期間使用的任何 Docker 映象

Docker 映象的 URL 將使用 Package URL 格式。

所有構建材料都將包括工件的不可變校驗和。從可變標籤構建時,可以使用摘要資訊來確定與構建執行時相比,工件是否已更新。

    "materials": [
      {
        "uri": "pkg:docker/alpine@3.17?platform=linux%2Famd64",
        "digest": {
          "sha256": "8914eb54f968791faf6a8638949e480fef81e697984fba772b3976835194c6d4"
        }
      },
      {
        "uri": "https://github.com/moby/buildkit.git#refs/tags/v0.11.0",
        "digest": {
          "sha1": "4b220de5058abfd01ff619c9d2ff6b09a049bea0"
        }
      },
      ...
    ],

buildConfig

對應於 SLSA buildConfig.

僅包含在 mode=max 中。

定義構建期間執行的構建步驟。

BuildKit 在內部使用 LLB 定義來執行構建步驟。構建步驟的 LLB 定義在 buildConfig.llbDefinition 欄位中定義。

每個 LLB 步驟都是 LLB ProtoBuf API

metadata.buildInvocationId

對應於 SLSA metadata.buildInvocationId.

包含在 mode=minmode=max 中。

構建呼叫的唯一識別符號。當使用單個構建請求構建多平臺映象時,此值將被所有平臺版本的映象共享。

    "metadata": {
      "buildInvocationID": "rpv7a389uzil5lqmrgwhijwjz",
      ...
    },

metadata.buildStartedOn

對應於 SLSA metadata.buildStartedOn.

包含在 mode=minmode=max 中。

構建開始的時間戳。

    "metadata": {
      "buildStartedOn": "2021-11-17T15:00:00Z",
      ...
    },

metadata.buildFinishedOn

對應於 SLSA metadata.buildFinishedOn.

包含在 mode=minmode=max 中。

構建完成的時間戳。

    "metadata": {
      "buildFinishedOn": "2021-11-17T15:01:00Z",
      ...
    },

metadata.completeness

對應於 SLSA metadata.completeness.

包含在 mode=minmode=max 中。

定義溯源資訊是否完整。

completeness.parameters 如果所有構建引數都包含在 invocation.parameters 欄位中,則為 true。當以 min 模式構建時,構建引數不會包含在溯源資訊中,引數也不完整。引數在沒有使用前端的直接 LLB 構建中也不完整。

completeness.environment 對於 BuildKit 構建始終為 true。

completeness.materials 如果 materials 欄位包含構建的所有依賴項,則為 true。當從本地目錄中的未跟蹤源構建時,材料不完整,而當從遠端 Git 儲存庫構建時,所有材料都可以被 BuildKit 跟蹤,並且 completeness.materials 為 true。

    "metadata": {
      "completeness": {
        "parameters": true,
        "environment": true,
        "materials": true
      },
      ...
    },

metadata.reproducible

對應於 SLSA metadata.reproducible.

定義構建結果是否應該是逐位元組可重現的。此值可以透過使用者使用 reproducible=true 證明引數來設定。

    "metadata": {
      "reproducible": false,
      ...
    },

metadata.https://mobyproject.org/buildkit@v1#hermetic

包含在 mode=minmode=max 中。

如果構建是隔離的並且沒有訪問網路,則此擴充套件欄位將設定為 true。在 Dockerfile 中,如果構建不使用 RUN 命令或使用 --network=none 標誌停用網路,則構建是隔離的。

    "metadata": {
      "https://mobyproject.org/buildkit@v1#hermetic": true,
      ...
    },

metadata.https://mobyproject.org/buildkit@v1#metadata

部分包含在 mode=min 中。

此擴充套件欄位定義 BuildKit 特定的附加元資料,它不是 SLSA 溯源規範的一部分。

    "metadata": {
      "https://mobyproject.org/buildkit@v1#metadata": {
        "source": {...},
        "layers": {...},
        "vcs": {...},
      },
      ...
    },

source

僅包含在 mode=max 中。

定義 LLB 構建步驟的源對映,在 buildConfig.llbDefinition 欄位中定義,到它們原始的原始碼(例如,Dockerfile 命令)。source.locations 欄位包含在 LLB 步驟中執行的所有 Dockerfile 命令的範圍。source.infos 陣列包含原始碼本身。如果 BuildKit 前端在建立 LLB 定義時提供了此對映,則此對映存在。

layers

僅包含在 mode=max 中。

定義在 buildConfig.llbDefinition 中定義的 LLB 構建步驟掛載到等效層 OCI 描述符的層對映。如果層資料可用,通常在對映象進行證明或構建步驟作為構建的一部分拉入映象資料時,此對映存在。

vcs

包含在 mode=minmode=max 中。

定義用於構建的版本控制系統(VCS)的可選元資料。如果構建使用來自 Git 儲存庫的遠端上下文,BuildKit 會自動提取版本控制系統(VCS)的詳細資訊,並將其顯示在 invocation.configSource 欄位中。但如果構建使用來自本地目錄的源,即使該目錄包含一個 Git 儲存庫,VCS 資訊也會丟失。在這種情況下,構建客戶端可以傳送額外的 vcs:sourcevcs:revision 構建選項,BuildKit 會將它們新增到溯源證明作為額外的元資料。請注意,與 invocation.configSource 欄位相反,BuildKit 不會驗證 vcs 值,因此不能信任它們,而應該僅用作元資料提示。

輸出

要檢查為容器映象生成的並附加到容器映象的溯源,可以使用 docker buildx imagetools 命令檢查登錄檔中的映象。檢查證明顯示了 證明儲存規範 中描述的格式。

例如,檢查基於 alpine:latest 的簡單 Docker 映象,對於 mode=min 構建,結果是類似於以下的溯源證明

{
  "_type": "https://in-toto.io/Statement/v0.1",
  "predicateType": "https://slsa.dev/provenance/v0.2",
  "subject": [
    {
      "name": "pkg:docker/<registry>/<image>@<tag/digest>?platform=<platform>",
      "digest": {
        "sha256": "e8275b2b76280af67e26f068e5d585eb905f8dfd2f1918b3229db98133cb4862"
      }
    }
  ],
  "predicate": {
    "builder": {
      "id": ""
    },
    "buildType": "https://mobyproject.org/buildkit@v1",
    "materials": [
      {
        "uri": "pkg:docker/alpine@latest?platform=linux%2Famd64",
        "digest": {
          "sha256": "8914eb54f968791faf6a8638949e480fef81e697984fba772b3976835194c6d4"
        }
      }
    ],
    "invocation": {
      "configSource": {
        "entryPoint": "Dockerfile"
      },
      "parameters": {
        "frontend": "dockerfile.v0",
        "args": {},
        "locals": [
          {
            "name": "context"
          },
          {
            "name": "dockerfile"
          }
        ]
      },
      "environment": {
        "platform": "linux/amd64"
      }
    },
    "metadata": {
      "buildInvocationID": "yirbp1aosi1vqjmi3z6bc75nb",
      "buildStartedOn": "2022-12-08T11:48:59.466513707Z",
      "buildFinishedOn": "2022-12-08T11:49:01.256820297Z",
      "reproducible": false,
      "completeness": {
        "parameters": true,
        "environment": true,
        "materials": false
      },
      "https://mobyproject.org/buildkit@v1#metadata": {}
    }
  }
}

對於類似的構建,但使用 mode=max

{
  "_type": "https://in-toto.io/Statement/v0.1",
  "predicateType": "https://slsa.dev/provenance/v0.2",
  "subject": [
    {
      "name": "pkg:docker/<registry>/<image>@<tag/digest>?platform=<platform>",
      "digest": {
        "sha256": "e8275b2b76280af67e26f068e5d585eb905f8dfd2f1918b3229db98133cb4862"
      }
    }
  ],
  "predicate": {
    "builder": {
      "id": ""
    },
    "buildType": "https://mobyproject.org/buildkit@v1",
    "materials": [
      {
        "uri": "pkg:docker/alpine@latest?platform=linux%2Famd64",
        "digest": {
          "sha256": "8914eb54f968791faf6a8638949e480fef81e697984fba772b3976835194c6d4"
        }
      }
    ],
    "invocation": {
      "configSource": {
        "entryPoint": "Dockerfile"
      },
      "parameters": {
        "frontend": "dockerfile.v0",
        "args": {},
        "locals": [
          {
            "name": "context"
          },
          {
            "name": "dockerfile"
          }
        ]
      },
      "environment": {
        "platform": "linux/amd64"
      }
    },
    "buildConfig": {
      "llbDefinition": [
        {
          "id": "step0",
          "op": {
            "Op": {
              "source": {
                "identifier": "docker-image://docker.io/library/alpine:latest@sha256:8914eb54f968791faf6a8638949e480fef81e697984fba772b3976835194c6d4"
              }
            },
            "platform": {
              "Architecture": "amd64",
              "OS": "linux"
            },
            "constraints": {}
          }
        },
        {
          "id": "step1",
          "op": {
            "Op": null
          },
          "inputs": ["step0:0"]
        }
      ]
    },
    "metadata": {
      "buildInvocationID": "46ue2x93k3xj5l463dektwldw",
      "buildStartedOn": "2022-12-08T11:50:54.953375437Z",
      "buildFinishedOn": "2022-12-08T11:50:55.447841328Z",
      "reproducible": false,
      "completeness": {
        "parameters": true,
        "environment": true,
        "materials": false
      },
      "https://mobyproject.org/buildkit@v1#metadata": {
        "source": {
          "locations": {
            "step0": {
              "locations": [
                {
                  "ranges": [
                    {
                      "start": {
                        "line": 1
                      },
                      "end": {
                        "line": 1
                      }
                    }
                  ]
                }
              ]
            }
          },
          "infos": [
            {
              "filename": "Dockerfile",
              "data": "RlJPTSBhbHBpbmU6bGF0ZXN0Cg==",
              "llbDefinition": [
                {
                  "id": "step0",
                  "op": {
                    "Op": {
                      "source": {
                        "identifier": "local://dockerfile",
                        "attrs": {
                          "local.differ": "none",
                          "local.followpaths": "[\"Dockerfile\",\"Dockerfile.dockerignore\",\"dockerfile\"]",
                          "local.session": "q2jnwdkas0i0iu4knchd92jaz",
                          "local.sharedkeyhint": "dockerfile"
                        }
                      }
                    },
                    "constraints": {}
                  }
                },
                {
                  "id": "step1",
                  "op": {
                    "Op": null
                  },
                  "inputs": ["step0:0"]
                }
              ]
            }
          ]
        },
        "layers": {
          "step0:0": [
            [
              {
                "mediaType": "application/vnd.oci.image.layer.v1.tar+gzip",
                "digest": "sha256:c158987b05517b6f2c5913f3acef1f2182a32345a304fe357e3ace5fadcad715",
                "size": 3370706
              }
            ]
          ]
        }
      }
    }
  }
}