This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SLSA Provenance for Image Manifest Layer History
Signed-off-by: Johnson Shi <Johnson.Shi@microsoft.com>
- Loading branch information
0 parents
commit aa4ff02
Showing
57 changed files
with
9,909 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Build | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-go@v3 | ||
with: | ||
go-version: '^1.18.0' | ||
- run: go version | ||
|
||
- name: Build CLI | ||
run: make build-cli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
name: Release | ||
on: | ||
push: | ||
tags: | ||
- "v*" | ||
|
||
jobs: | ||
version: | ||
name: Set Version from git ref | ||
runs-on: ubuntu-latest | ||
outputs: | ||
version: ${{ steps.version.outputs.version }} | ||
steps: | ||
- id: version | ||
run: echo "::set-output name=version::$(sed 's#^refs/tags/\(.*\)#\1#' <<< '${{ github.ref }}')" | ||
|
||
binaries: | ||
name: Binaries | ||
runs-on: ubuntu-latest | ||
needs: version | ||
env: | ||
VERSION: ${{ needs.version.outputs.version }} | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-go@v3 | ||
with: | ||
go-version: '^1.18.0' | ||
- run: go version | ||
|
||
- name: Build CLI | ||
run: make build-cli | ||
|
||
- name: Create Release | ||
id: create_release | ||
uses: ncipollo/release-action@v1 | ||
with: | ||
tag: ${{ env.VERSION }} | ||
name: ${{ env.VERSION }} | ||
generateReleaseNotes: true | ||
prerelease: ${{ contains(env.VERSION, '-alpha.') || contains(env.VERSION, '-beta.') || contains(env.VERSION, '-rc.') || contains(env.VERSION, '-nightly.') }} | ||
artifacts: "bin/*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
bin/ | ||
.vscode/ | ||
|
||
# The following entries are from | ||
# https://github.com/github/gitignore/blob/main/Go.gitignore | ||
|
||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Go workspace file | ||
go.work |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Johnson Shi | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.PHONY: build-cli | ||
build-cli: | ||
go build -v -o ./bin/image-layer-dockerfile-history ./cmd/cli | ||
chmod +x ./bin/image-layer-dockerfile-history |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Image Manifest Layer History | ||
|
||
Command-line tool that shows the _**exact**_ Dockerfile commands for each [OCI Image Manifest](https://github.com/opencontainers/image-spec/blob/main/manifest.md) Layer of a container image. | ||
|
||
## Quick Start | ||
|
||
### Install | ||
|
||
To install, run the following commands. | ||
|
||
```bash | ||
curl -LO https://github.com/johnsonshi/image-manifest-layer-history/releases/download/v0.0.1/image-layer-dockerfile-history | ||
chmod +x image-layer-dockerfile-history | ||
sudo mv image-layer-dockerfile-history /usr/local/bin | ||
``` | ||
|
||
### Generate | ||
|
||
Generate the history (including the exact Dockerfile commands) for each OCI Image Manifest Layer of a container image. | ||
|
||
#### Generate – Usage | ||
|
||
```bash | ||
image-layer-dockerfile-history \ | ||
generate \ | ||
--username "$username" \ | ||
--password "$password" \ | ||
--image-ref "$image_ref" \ | ||
--dockerfile "$dockerfile" \ | ||
--output-file "$output_file" \ | ||
--attribution-annotation "git_commit_id: $git_commit_id" \ | ||
--attribution-annotation "git_commit_date: $git_commit_date" \ | ||
--attribution-annotation "git_commit_name: $git_commit_name" \ | ||
--attribution-annotation "git_commit_email: $git_commit_email" \ | ||
--attribution-annotation "git_remote_origin_url: $git_remote_origin_url" | ||
``` | ||
|
||
See [`./scripts/generate-history-all-examples.sh`](./scripts/generate-history-all-examples.sh) for more examples. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
/* | ||
Copyright © 2022 Johnson Shi <Johnson.Shi@microsoft.com> | ||
*/ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
"regexp" | ||
"time" | ||
|
||
"github.com/asottile/dockerfile" | ||
intoto "github.com/in-toto/in-toto-golang/in_toto" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/johnsonshi/image-manifest-layer-history/pkg/image/client" | ||
"github.com/johnsonshi/image-manifest-layer-history/pkg/image/history" | ||
imagehistoryslsa "github.com/johnsonshi/image-manifest-layer-history/pkg/image/history/slsa" | ||
) | ||
|
||
type generateCmdOpts struct { | ||
stdin io.Reader | ||
stdout io.Writer | ||
stderr io.Writer | ||
username string | ||
password string | ||
imageRef string | ||
dockerfilePath string | ||
outputFilePath string | ||
simplifiedJsonOutput bool | ||
slsaProvenanceJsonOutput bool | ||
attributionAnnotations []string | ||
} | ||
|
||
func newGenerateCmd(stdin io.Reader, stdout io.Writer, stderr io.Writer, args []string) *cobra.Command { | ||
opts := &generateCmdOpts{ | ||
stdin: stdin, | ||
stdout: stdout, | ||
stderr: stderr, | ||
} | ||
|
||
cobraCmd := &cobra.Command{ | ||
Use: "generate", | ||
Short: "Generate the history (including the exact Dockerfile commands) for each OCI Image Manifest Layer of a container image.", | ||
Example: `generate -u username -p password -i imageRef -d dockerfilePath -o outputFilePath [-s] [-a "key: value"]`, | ||
RunE: func(_ *cobra.Command, args []string) error { | ||
return opts.run() | ||
}, | ||
} | ||
|
||
f := cobraCmd.Flags() | ||
|
||
f.StringVarP(&opts.username, "username", "u", "", "username to use for authentication with the registry") | ||
cobraCmd.MarkFlagRequired("username") | ||
|
||
// TODO add support for --password-stdin (reading password from stdin) for more secure password input. | ||
f.StringVarP(&opts.password, "password", "p", "", "password to use for authentication with the registry") | ||
cobraCmd.MarkFlagRequired("password") | ||
|
||
cobraCmd.MarkFlagsRequiredTogether("username", "password") | ||
|
||
f.StringVarP(&opts.imageRef, "image-ref", "i", "", "full image reference including registry, repository, and tag/digest, e.g. myregistry.azurecr.io/library/ubuntu:22.04 or myregistry.azurecr.io/library/ubuntu@sha256:123456") | ||
cobraCmd.MarkFlagRequired("image-ref") | ||
|
||
f.StringVarP(&opts.dockerfilePath, "dockerfile", "d", "", "path to the Dockerfile") | ||
cobraCmd.MarkFlagRequired("dockerfile") | ||
|
||
f.StringVarP(&opts.outputFilePath, "output-file", "o", "", "optional path to an output file") | ||
|
||
f.BoolVar(&opts.simplifiedJsonOutput, "simplified-json", false, "optional flag to output in simplified JSON format") | ||
|
||
f.BoolVar(&opts.slsaProvenanceJsonOutput, "slsa-provenance-json", false, "optional flag to output in SLSA Provenance JSON format") | ||
|
||
cobraCmd.MarkFlagsMutuallyExclusive("simplified-json", "slsa-provenance-json") | ||
|
||
f.StringArrayVarP(&opts.attributionAnnotations, "attribution-annotation", "a", []string{}, "optional flag to add 'string-key:string-value' attributions to the manifest layer history (only added for layers not 'FROM <primary-base-image>')") | ||
|
||
return cobraCmd | ||
} | ||
|
||
func (opts *generateCmdOpts) run() error { | ||
ctx := context.Background() | ||
|
||
annotationsMap, err := getAnnotationsMap(opts.attributionAnnotations) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
imageClient, err := client.NewImageClient(opts.username, opts.password, opts.imageRef) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// imageLayerHistory is sorted from top layers (most recent layers) to bottom layers (base image layers). | ||
imageLayerHistory, err := imageClient.GetImageLayerHistory(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
imageManifest, err := imageClient.GetImageManifest(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// dockerfileCommands is sorted based on the original order of commands in the Dockerfile. | ||
// E.g. if the Dockerfile contains the following commands: | ||
// FROM ubuntu:22.04 | ||
// RUN apt-get update | ||
// RUN apt-get install -y vim | ||
// then dockerfileCommands will be: | ||
// []dockerfile.Command{ | ||
// dockerfile.Command{Original: "FROM ubuntu:22.04", Cmd: "FROM", Value: []string{"ubuntu:22.04"}}, | ||
// dockerfile.Command{Original: "RUN apt-get update", Cmd: "RUN", Value: []string{"apt-get", "update"}}, | ||
// dockerfile.Command{Original: "RUN apt-get install -y vim", Cmd: "run", Value: []string{"apt-get", "install", "-y", "vim"}}, | ||
// } | ||
dockerfileCommands, err := dockerfile.ParseFile(opts.dockerfilePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
h := history.ImageHistory{ | ||
ImageLayerHistory: imageLayerHistory, | ||
ImageManifest: imageManifest, | ||
DockerfileCommands: dockerfileCommands, | ||
} | ||
|
||
manifestLayerHistory, err := h.GetImageManifestLayerDockerfileCommandsHistory(annotationsMap) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return opts.writeManifestLayerHistory(manifestLayerHistory) | ||
|
||
} | ||
|
||
func (opts *generateCmdOpts) writeManifestLayerHistory(manifestLayerHistory []history.ImageManifestLayerDockerfileCommandsHistory) error { | ||
output, err := json.MarshalIndent(manifestLayerHistory, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if opts.simplifiedJsonOutput { | ||
simplified := history.GetSimplifiedImageManifestLayerDockerfileCommandsHistory(manifestLayerHistory) | ||
output, err = json.MarshalIndent(simplified, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
} else if opts.slsaProvenanceJsonOutput { | ||
var imageSlsaProvenanceStatements []*intoto.ProvenanceStatement | ||
timeNow := time.Now() | ||
for _, layerHistory := range manifestLayerHistory { | ||
layerSlsaProvenance := imagehistoryslsa.ImageManifestLayerSlsaProvenance{ | ||
LayerHistory: layerHistory, | ||
BuilderID: "URI indicating the builder identity. E.g. pipeline-name", | ||
BuildType: "URI indicating what type of build was performed. E.g. build-type-dockerfile-build", | ||
BuildInvocationID: "Globally Unique Build Invocation ID. Definition: Identifies this particular build invocation, which can be useful for finding associated logs or other ad-hoc analysis. The exact meaning and format is defined by builder.id; by default it is treated as opaque and case-sensitive. The value SHOULD be globally unique.", | ||
BuildStartedOn: &timeNow, | ||
BuildFinishedOn: &timeNow, | ||
RepoURIContainingDockerfile: "URI to Git repo of Dockerfile. Describes where the config file that kicked off the build came from. URI indicating the identity of the source of the config. E.g. https://www.github.com/example/reponame/blob/master/Dockerfile", | ||
RepoGitCommit: "Git commit SHA that kicked off the build.", | ||
RepoPathToDockerfile: "Path to Dockerfile in the repo. Definition: String identifying the entry point into the build. This is often a path to a configuration file and/or a target label within that file. The syntax and meaning are defined by buildType. For example, if the buildType were “make”, then this would reference the directory in which to run make as well as which target to use.", | ||
} | ||
layerSlsaProvenanceStatement, err := layerSlsaProvenance.GetImageManifestLayerSlsaProvenance() | ||
if err != nil { | ||
return err | ||
} | ||
imageSlsaProvenanceStatements = append(imageSlsaProvenanceStatements, layerSlsaProvenanceStatement) | ||
} | ||
output, err = json.MarshalIndent(imageSlsaProvenanceStatements, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
output = append(output, '\n') | ||
|
||
if opts.outputFilePath != "" { | ||
return writeToFile(opts.outputFilePath, output) | ||
} | ||
|
||
_, err = opts.stdout.Write(output) | ||
return err | ||
} | ||
|
||
// getAnnotationsMap returns a map of annotations from a slice of annotation strings. | ||
// strings in the slice should conform to the following format: "key: value". | ||
func getAnnotationsMap(annotationSlice []string) (map[string]string, error) { | ||
re := regexp.MustCompile(`:\s*`) | ||
annotationsMap := make(map[string]string) | ||
for _, rawAnnotation := range annotationSlice { | ||
annotation := re.Split(rawAnnotation, 2) | ||
if len(annotation) != 2 { | ||
return nil, fmt.Errorf("invalid annotation: %s", rawAnnotation) | ||
} | ||
annotationsMap[annotation[0]] = annotation[1] | ||
} | ||
return annotationsMap, nil | ||
} | ||
|
||
func writeToFile(path string, data []byte) error { | ||
f, err := os.Create(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
_, err = f.Write(data) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
Copyright © 2022 Johnson Shi <Johnson.Shi@microsoft.com> | ||
*/ | ||
package main | ||
|
||
func main() { | ||
execute() | ||
} |
Oops, something went wrong.