|
| 1 | +// Copyright 2022 SLSA Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package pkg |
| 16 | + |
| 17 | +// This file contains functionality and structs for validating and |
| 18 | +// representing user inputs and configuration files. |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "net/url" |
| 23 | + "strings" |
| 24 | + |
| 25 | + toml "github.com/pelletier/go-toml" |
| 26 | + "github.com/slsa-framework/slsa-github-generator/internal/utils" |
| 27 | +) |
| 28 | + |
| 29 | +// BuildConfig is a collection of parameters to use for building the artifact. |
| 30 | +type BuildConfig struct { |
| 31 | + // TODO(#1191): Add env and options if needed. |
| 32 | + // Command to pass to `docker run`. The command is taken as an array |
| 33 | + // instead of a single string to avoid unnecessary parsing. See |
| 34 | + // https://docs.docker.com/engine/reference/builder/#cmd and |
| 35 | + // https://man7.org/linux/man-pages/man3/exec.3.html for more details. |
| 36 | + Command []string `toml:"command"` |
| 37 | + |
| 38 | + // The path, relative to the root of the git repository, where the artifact |
| 39 | + // built by the `docker run` command is expected to be found. |
| 40 | + ArtifactPath string `toml:"artifact_path"` |
| 41 | +} |
| 42 | + |
| 43 | +// Digest specifies a digest values, including the name of the hash function |
| 44 | +// that was used for computing the digest. |
| 45 | +type Digest struct { |
| 46 | + Alg string |
| 47 | + Value string |
| 48 | +} |
| 49 | + |
| 50 | +// DockerImage fully specifies a docker image by a URI (e.g., including the |
| 51 | +// docker image name and registry), and its digest. |
| 52 | +type DockerImage struct { |
| 53 | + URI string |
| 54 | + Digest Digest |
| 55 | +} |
| 56 | + |
| 57 | +// ToString returns the builder image in the form of NAME@ALG:VALUE. |
| 58 | +func (bi *DockerImage) ToString() string { |
| 59 | + return fmt.Sprintf("%s@%s:%s", bi.URI, bi.Digest.Alg, bi.Digest.Value) |
| 60 | +} |
| 61 | + |
| 62 | +// DockerBuildConfig is a convenience class for holding validated user inputs. |
| 63 | +type DockerBuildConfig struct { |
| 64 | + SourceRepo string |
| 65 | + SourceDigest Digest |
| 66 | + BuilderImage DockerImage |
| 67 | + BuildConfigPath string |
| 68 | +} |
| 69 | + |
| 70 | +// NewDockerBuildConfig validates the inputs and generates an instance of |
| 71 | +// DockerBuildConfig. |
| 72 | +func NewDockerBuildConfig(io *InputOptions) (*DockerBuildConfig, error) { |
| 73 | + if err := validateURI(io.SourceRepo); err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + sourceRepoDigest, err := validateDigest(io.GitCommitHash) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + dockerImage, err := validateDockerImage(io.BuilderImage) |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + |
| 87 | + if err = validatePath(io.BuildConfigPath); err != nil { |
| 88 | + return nil, fmt.Errorf("invalid build config path: %v", err) |
| 89 | + } |
| 90 | + |
| 91 | + return &DockerBuildConfig{ |
| 92 | + SourceRepo: io.SourceRepo, |
| 93 | + SourceDigest: *sourceRepoDigest, |
| 94 | + BuilderImage: *dockerImage, |
| 95 | + BuildConfigPath: io.BuildConfigPath, |
| 96 | + }, nil |
| 97 | +} |
| 98 | + |
| 99 | +func validateURI(input string) error { |
| 100 | + _, err := url.Parse(input) |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("could not parse string (%q) as URI: %v", input, err) |
| 103 | + } |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func validateDigest(input string) (*Digest, error) { |
| 108 | + // We expect the input to be of the form ALG:VALUE |
| 109 | + parts := strings.Split(input, ":") |
| 110 | + if len(parts) != 2 { |
| 111 | + return nil, fmt.Errorf("got %s, want ALG:VALUE format", input) |
| 112 | + } |
| 113 | + digest := Digest{ |
| 114 | + Alg: parts[0], |
| 115 | + Value: parts[1], |
| 116 | + } |
| 117 | + return &digest, nil |
| 118 | +} |
| 119 | + |
| 120 | +func validateDockerImage(image string) (*DockerImage, error) { |
| 121 | + imageParts := strings.Split(image, "@") |
| 122 | + if len(imageParts) != 2 { |
| 123 | + return nil, fmt.Errorf("got %s, want NAME@DIGEST format", image) |
| 124 | + } |
| 125 | + |
| 126 | + if err := validateURI(imageParts[0]); err != nil { |
| 127 | + return nil, fmt.Errorf("docker image name (%q) is not a valid URI: %v", imageParts[0], err) |
| 128 | + } |
| 129 | + |
| 130 | + digest, err := validateDigest(imageParts[1]) |
| 131 | + if err != nil { |
| 132 | + return nil, fmt.Errorf("docker image digest (%q) is malformed: %v", imageParts[1], err) |
| 133 | + } |
| 134 | + |
| 135 | + dockerImage := DockerImage{ |
| 136 | + URI: imageParts[0], |
| 137 | + Digest: *digest, |
| 138 | + } |
| 139 | + |
| 140 | + return &dockerImage, nil |
| 141 | +} |
| 142 | + |
| 143 | +func validatePath(path string) error { |
| 144 | + err := utils.PathIsUnderCurrentDirectory(path) |
| 145 | + if err != nil { |
| 146 | + return fmt.Errorf("path (%q) is not in the current directory", path) |
| 147 | + } |
| 148 | + return nil |
| 149 | +} |
| 150 | + |
| 151 | +// ToMap returns this instance as a mapping between the algorithm and value. |
| 152 | +func (d *Digest) ToMap() map[string]string { |
| 153 | + return map[string]string{d.Alg: d.Value} |
| 154 | +} |
| 155 | + |
| 156 | +// LoadBuildConfigFromFile loads build configuration from a toml file specified |
| 157 | +// by the BuildConfigPath of this DockerBuildConfig. An instance of BuildConfig |
| 158 | +// is returned on success. |
| 159 | +func (dbc *DockerBuildConfig) LoadBuildConfigFromFile() (*BuildConfig, error) { |
| 160 | + return loadBuildConfigFromFile(dbc.BuildConfigPath) |
| 161 | +} |
| 162 | + |
| 163 | +// loadBuildConfigFromFile does not validate the input path, and is therefore |
| 164 | +// not exposed. The corresponding method LoadBuildConfigFromFile must be called |
| 165 | +// on an instance of DockerBuildConfig which has a validated BuildConfigPath. |
| 166 | +func loadBuildConfigFromFile(path string) (*BuildConfig, error) { |
| 167 | + tomlTree, err := toml.LoadFile(path) |
| 168 | + if err != nil { |
| 169 | + return nil, fmt.Errorf("couldn't load toml file: %v", err) |
| 170 | + } |
| 171 | + |
| 172 | + config := BuildConfig{} |
| 173 | + if err := tomlTree.Unmarshal(&config); err != nil { |
| 174 | + return nil, fmt.Errorf("couldn't ubmarshal toml file: %v", err) |
| 175 | + } |
| 176 | + |
| 177 | + return &config, nil |
| 178 | +} |
0 commit comments