|
| 1 | +--- |
| 2 | +title: Use Dagger With Chainloop |
| 3 | +--- |
| 4 | + |
| 5 | +# Chainloop Module for Dagger |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +Daggerized version of [Chainloop](https://docs.chainloop.dev) that can be used to attest and collect pieces of evidence from your [Dagger](https://dagger.io/) pipelines. |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +- This module requires existing familiarity with Chainloop and its attestation process. Please refer to [this guide](https://docs.chainloop.dev/getting-started/attestation-crafting) to learn more. |
| 14 | +- You need a `token` (aka workflow robot account) [previously generated](https://docs.chainloop.dev/getting-started/workflow-definition#robot-account-creation) by your Chainloop administrator. |
| 15 | + |
| 16 | +## Attestation Crafting |
| 17 | + |
| 18 | +The [attestation process](https://docs.chainloop.dev/getting-started/attestation-crafting) starts with its initialization (`init`) or `resume`, then adding as many materials/pieces of evidence as needed (`add-raw-evidence` or `add-file-evidence`), and finally, signing and pushing the attestation to the Chainloop control plane (`push`). |
| 19 | + |
| 20 | +You can invoke this module in two ways: either from the Dagger CLI `dagger call ...` or from your own Dagger pipeline by importing this module as a dependency. |
| 21 | + |
| 22 | +### Using the Chainloop module in your Dagger pipeline |
| 23 | + |
| 24 | +To use Chainloop in your module, first, you need to add it as a dependency. |
| 25 | + |
| 26 | +```sh |
| 27 | +dagger install github.com/chainloop-dev/chainloop |
| 28 | +``` |
| 29 | + |
| 30 | +Once done, you'll have access to the Chainloop client via `dag.Chainloop()` and start the attestation process with Init(). |
| 31 | + |
| 32 | +You can find a full example of how to integrate attestation crafting in your `Go` pipeline [here](https://github.com/chainloop-dev/integration-demo/blob/main/chainloop-demo/dagger/src/main.go) |
| 33 | + |
| 34 | +### Using the Dagger CLI |
| 35 | + |
| 36 | +The [attestation process](https://docs.chainloop.dev/getting-started/attestation-crafting) starts with its initialization (`init`) or `resume`, then adding as many materials/pieces of evidence as needed (`add-raw-evidence` or `add-file-evidence`), and finally, signing and pushing the attestation to the Chainloop control plane (`push`). |
| 37 | + |
| 38 | +This module is designed to support function chaining, so after initializing the attestation, you can chain the subcommands to add pieces of evidence and push the attestation. For example |
| 39 | + |
| 40 | +```sh |
| 41 | +dagger call -m github.com/chainloop-dev/chainloop \ |
| 42 | + # Initialize the command |
| 43 | + init --token env:CHAINLOOP_TOKEN \ |
| 44 | + # we chain subcommands after the initialization |
| 45 | + # add a raw evidence |
| 46 | + add-raw-evidence --name my-evidence --value "my-value" \ |
| 47 | + # and push the result |
| 48 | + push --key file:/path/to/cosign.key --passphrase env:COSIGN_PASSPHRASE |
| 49 | +``` |
| 50 | + |
| 51 | +If the attestation process end-to-end is not completed in one go, you can store the attestation-id after init and resume the attestation process using the `resume` method at any time down the line. |
| 52 | + |
| 53 | +```sh |
| 54 | +# Initialize but this time we store the attestation-id |
| 55 | +ATTESTATION_ID=$(dagger call -m github.com/chainloop-dev/chainloop init --token env:CHAINLOOP_TOKEN attestation-id) |
| 56 | + |
| 57 | + |
| 58 | +# and we use it to resume the attestation process |
| 59 | +dagger call -m github.com/chainloop-dev/chainloop \ |
| 60 | + resume --token env:CHAINLOOP_TOKEN --attestation-id $ATTESTATION_ID \ |
| 61 | + # we chain subcommands after the initialization |
| 62 | + .... |
| 63 | +``` |
| 64 | + |
| 65 | +#### 1 - Init attestation ([docs](https://docs.chainloop.dev/getting-started/attestation-crafting#initialization)) |
| 66 | + |
| 67 | +Initialize an attestation using the Chainloop token stored in the `CHAINLOOP_TOKEN` environment variable. |
| 68 | + |
| 69 | +> NOTE: `--token` can be provided only by referencing an environment variable (env:MY_VAR), not by value |
| 70 | +
|
| 71 | +```sh |
| 72 | +# Initialize the attestation and get its ID |
| 73 | +dagger call -m github.com/chainloop-dev/chainloop \ |
| 74 | + init \ |
| 75 | + --token env:CHAINLOOP_TOKEN \ |
| 76 | + --repository /path/to/repo \ # optional flag to automatically attest a Git repository |
| 77 | + --contract-revision 1 # optional flag to specify the revision of the Workflow Contract (default `latest`) |
| 78 | +``` |
| 79 | + |
| 80 | +#### 2 - Get the status ([docs](https://docs.chainloop.dev/getting-started/attestation-crafting#inspecting-the-crafting-status)) |
| 81 | + |
| 82 | +Resuming a previous attestation |
| 83 | + |
| 84 | +```sh |
| 85 | +dagger call -m github.com/chainloop-dev/chainloop \ |
| 86 | + resume --token env:CHAINLOOP_TOKEN --attestation-id $ATTESTATION_ID \ |
| 87 | + status |
| 88 | +``` |
| 89 | + |
| 90 | +or chaining the command right after initialization |
| 91 | + |
| 92 | +```sh |
| 93 | +dagger call -m github.com/chainloop-dev/chainloop \ |
| 94 | + init --token env:CHAINLOOP_TOKEN \ |
| 95 | + status |
| 96 | +``` |
| 97 | + |
| 98 | +#### 3 - Add pieces of evidence ([docs](https://docs.chainloop.dev/getting-started/attestation-crafting#adding-materials)) |
| 99 | + |
| 100 | +You can attest pieces of evidence by providing its material name and its value, either in the form of a path to a file (`--path`) or a raw value (`--value`). |
| 101 | + |
| 102 | +A path to a file is required for materials derived from artifacts, such as Software Bill Of materials, or any other file-based evidence. |
| 103 | + |
| 104 | +```sh |
| 105 | +# Provide a material of kind artifact through its path |
| 106 | +# Remember, we first resume the attestation or chain the commands |
| 107 | +dagger call -m github.com/chainloop-dev/chainloop \ |
| 108 | + resume --token env:CHAINLOOP_TOKEN --attestation-id $ATTESTATION_ID \ |
| 109 | + add-file-evidence --name my-sbom --path ./path/to/sbom.json |
| 110 | +``` |
| 111 | + |
| 112 | +```sh |
| 113 | +# Or one with a raw value such as a container image reference |
| 114 | +dagger call -m github.com/chainloop-dev/chainloop \ |
| 115 | + resume --token env:CHAINLOOP_TOKEN --attestation-id $ATTESTATION_ID \ |
| 116 | + add-raw-evidence --name my-container-image --value ghcr.io/chainloop-dev/chainloop/control-plane |
| 117 | +``` |
| 118 | + |
| 119 | +In some cases, you might be providing a private container image as a piece of evidence. In this case, you'll also need to preload the container registry credentials. |
| 120 | + |
| 121 | +```sh |
| 122 | +dagger call -m github.com/chainloop-dev/chainloop \ |
| 123 | + resume --token env:CHAINLOOP_TOKEN --attestation-id $ATTESTATION_ID \ |
| 124 | + # Now load the registry credentials |
| 125 | + with-registry --address ghcr.io --username my-username --password MY_PAT_TOKEN \ |
| 126 | + # And perform the attestation of the private container image |
| 127 | + add-raw-evidence --name my-container-image --value ghcr.io/chainloop-dev/chainloop/control-plane |
| 128 | +``` |
| 129 | + |
| 130 | +#### 4 - Sign and push attestation ([docs](https://docs.chainloop.dev/getting-started/attestation-crafting#encode-sign-and-push-attestation)) |
| 131 | + |
| 132 | +Sign and push the attestation using a cosign **key stored in a file** and a passphrase stored in an environment variable. |
| 133 | + |
| 134 | +> NOTE: neither --signing-key nor --passphrase can be provided by value. You need to provide them either as a file (file:/) or an environment variable (env:/). |
| 135 | +
|
| 136 | +```sh |
| 137 | +dagger call -m github.com/chainloop-dev/chainloop \ |
| 138 | + resume --token env:CHAINLOOP_TOKEN --attestation-id $ATTESTATION_ID \ |
| 139 | + push --key file:/path/to/cosign.key --passphrase env:COSIGN_PASSPHRASE |
| 140 | +``` |
| 141 | + |
| 142 | +Alternatively, you can also provide the signing key in an environment variable `--key env:MY_COSIGN_KEY` |
| 143 | + |
| 144 | +#### 5 - Cancel/mark attestation as failed |
| 145 | + |
| 146 | +```sh |
| 147 | +dagger call -m github.com/chainloop-dev/chainloop \ |
| 148 | + resume --token env:CHAINLOOP_TOKEN --attestation-id $ATTESTATION_ID \ |
| 149 | + mark-failed --reason "Something went wrong" |
| 150 | +``` |
| 151 | + |
| 152 | +or cancel the attestation process |
| 153 | + |
| 154 | +```sh |
| 155 | +dagger call -m github.com/chainloop-dev/chainloop \ |
| 156 | + resume --token env:CHAINLOOP_TOKEN --attestation-id $ATTESTATION_ID \ |
| 157 | + mark-canceled --reason "nothing to see here" |
| 158 | +``` |
| 159 | + |
| 160 | +## Documentation |
| 161 | + |
| 162 | +To learn more, please visit the Chainloop project's documentation website, https://docs.chainloop.dev where you will find a getting started guide, FAQ, examples, and more. |
| 163 | + |
| 164 | +## Community / Discussion / Support |
| 165 | + |
| 166 | +Chainloop is developed in the open and is constantly improved by our users, contributors and maintainers. Got a question, comment, or idea? Please don't hesitate to reach out via: |
| 167 | + |
| 168 | +- GitHub [Issues](https://github.com/chainloop-dev/chainloop/issues) |
| 169 | +- Discord [Community Server](https://discord.gg/f7atkaZact) |
| 170 | +- Youtube [Channel](https://www.youtube.com/channel/UCISrWrPyR_AFjIQYmxAyKdg) |
| 171 | + |
| 172 | + |
0 commit comments