| 1 |
// Copyright 2024 The Archivista Contributors |
| 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 |
// http://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 artifactstore |
| 16 |
|
| 17 |
import ( |
| 18 |
"crypto" |
| 19 |
"errors" |
| 20 |
"fmt" |
| 21 |
"os" |
| 22 |
"strings" |
| 23 |
|
| 24 |
"github.com/in-toto/go-witness/cryptoutil" |
| 25 |
"gopkg.in/yaml.v3" |
| 26 |
) |
| 27 |
|
| 28 |
// Config represents the available artifacts within the store |
| 29 |
type Config struct { |
| 30 |
Artifacts map[string]Artifact `json:"artifacts"` |
| 31 |
} |
| 32 |
|
| 33 |
// Artifact represents an artifact and it's available versions in the store |
| 34 |
type Artifact struct { |
| 35 |
Versions map[string]Version `json:"versions"` |
| 36 |
} |
| 37 |
|
| 38 |
// Version represents a version of an Artifact (ex v0.2.0) with the available Distributions of version |
| 39 |
type Version struct { |
| 40 |
Distributions map[string]Distribution `json:"distributions"` |
| 41 |
Description string `json:"description"` |
| 42 |
} |
| 43 |
|
| 44 |
// Distribution is a specific distribution of a Version of an Artifact(ex linux-amd64) |
| 45 |
type Distribution struct { |
| 46 |
FileLocation string `json:"-"` |
| 47 |
SHA256Digest string `json:"sha256digest"` |
| 48 |
} |
| 49 |
|
| 50 |
// Store is an artifact store served from Archivista |
| 51 |
type Store struct { |
| 52 |
config Config |
| 53 |
} |
| 54 |
|
| 55 |
type Option func(*Store) error |
| 56 |
|
| 57 |
// WithConfig creates a Store with the provided config |
| 58 |
func WithConfig(config Config) Option { |
| 59 |
return func(as *Store) error { |
| 60 |
as.config = config |
| 61 |
return nil |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// WithConfigFile creates a Store with a config loaded from a yaml file on disk |
| 66 |
func WithConfigFile(configPath string) Option { |
| 67 |
return func(as *Store) error { |
| 68 |
configBytes, err := os.ReadFile(configPath) |
| 69 |
if err != nil { |
| 70 |
return err |
| 71 |
} |
| 72 |
|
| 73 |
config := Config{} |
| 74 |
if err := yaml.Unmarshal(configBytes, &config); err != nil { |
| 75 |
return err |
| 76 |
} |
| 77 |
|
| 78 |
return WithConfig(config)(as) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// New creates a new Store with the provided options |
| 83 |
func New(opts ...Option) (Store, error) { |
| 84 |
as := Store{} |
| 85 |
errs := make([]error, 0) |
| 86 |
for _, opt := range opts { |
| 87 |
if err := opt(&as); err != nil { |
| 88 |
errs = append(errs, err) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
if len(errs) > 0 { |
| 93 |
return Store{}, errors.Join(errs...) |
| 94 |
} |
| 95 |
|
| 96 |
if err := verifyConfig(as); err != nil { |
| 97 |
return Store{}, err |
| 98 |
} |
| 99 |
|
| 100 |
return as, nil |
| 101 |
} |
| 102 |
|
| 103 |
// verifyConfig ensures that each file exists on disk and that the sha256sum of the |
| 104 |
// files on disk match those of the config |
| 105 |
func verifyConfig(as Store) error { |
| 106 |
errs := make([]error, 0) |
| 107 |
for artifactName, artifact := range as.config.Artifacts { |
| 108 |
for versionString, version := range artifact.Versions { |
| 109 |
for distroString, distro := range version.Distributions { |
| 110 |
if _, err := os.Stat(distro.FileLocation); err != nil { |
| 111 |
errs = append(errs, fmt.Errorf("%v version %v-%v does not exist on disk: %w", artifactName, versionString, distroString, err)) |
| 112 |
continue |
| 113 |
} |
| 114 |
|
| 115 |
digestSet, err := cryptoutil.CalculateDigestSetFromFile(distro.FileLocation, []cryptoutil.DigestValue{{Hash: crypto.SHA256}}) |
| 116 |
if err != nil { |
| 117 |
errs = append(errs, fmt.Errorf("could not calculate sha256 digest for %v version %v-%v: %w", artifactName, versionString, distroString, err)) |
| 118 |
} |
| 119 |
|
| 120 |
sha256Digest := digestSet[cryptoutil.DigestValue{Hash: crypto.SHA256, GitOID: false}] |
| 121 |
if !strings.EqualFold(sha256Digest, distro.SHA256Digest) { |
| 122 |
errs = append(errs, fmt.Errorf("sha256 digest of %v version %v-%v does not match config: got %v, expected %v", artifactName, versionString, distroString, sha256Digest, distro.SHA256Digest)) |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
if len(errs) > 0 { |
| 129 |
return errors.Join(errs...) |
| 130 |
} |
| 131 |
|
| 132 |
return nil |
| 133 |
} |
| 134 |
|
| 135 |
// Artifacts returns a copy of all the Store's Artifacts |
| 136 |
func (as Store) Artifacts() map[string]Artifact { |
| 137 |
out := make(map[string]Artifact) |
| 138 |
for artifactString, artifact := range as.config.Artifacts { |
| 139 |
out[artifactString] = artifact |
| 140 |
} |
| 141 |
|
| 142 |
return out |
| 143 |
} |
| 144 |
|
| 145 |
// Versions returns all of the available Versions for an Artifact. |
| 146 |
func (as Store) Versions(artifact string) (map[string]Version, bool) { |
| 147 |
out := make(map[string]Version) |
| 148 |
a, ok := as.config.Artifacts[artifact] |
| 149 |
if !ok { |
| 150 |
return out, false |
| 151 |
} |
| 152 |
|
| 153 |
for verString, version := range a.Versions { |
| 154 |
out[verString] = version |
| 155 |
} |
| 156 |
|
| 157 |
return out, ok |
| 158 |
} |
| 159 |
|
| 160 |
// Version returns a specific Version for an artifact, if it exists |
| 161 |
func (as Store) Version(artifact, version string) (Version, bool) { |
| 162 |
a, ok := as.config.Artifacts[artifact] |
| 163 |
if !ok { |
| 164 |
return Version{}, false |
| 165 |
} |
| 166 |
|
| 167 |
v, ok := a.Versions[version] |
| 168 |
return v, ok |
| 169 |
} |
| 170 |
|
| 171 |
// Distributions returns all of the available Distributions for a specified Version of an Artifact |
| 172 |
func (as Store) Distributions(artifact, version string) (map[string]Distribution, bool) { |
| 173 |
out := make(map[string]Distribution) |
| 174 |
a, ok := as.config.Artifacts[artifact] |
| 175 |
if !ok { |
| 176 |
return out, false |
| 177 |
} |
| 178 |
|
| 179 |
vers, ok := a.Versions[version] |
| 180 |
if !ok { |
| 181 |
return out, ok |
| 182 |
} |
| 183 |
|
| 184 |
for distroString, distro := range vers.Distributions { |
| 185 |
out[distroString] = distro |
| 186 |
} |
| 187 |
|
| 188 |
return out, true |
| 189 |
} |
| 190 |
|
| 191 |
// Distribution returns the entry for a specific distribution for a specific version |
| 192 |
func (as Store) Distribution(artifact, version, distribution string) (Distribution, bool) { |
| 193 |
a, ok := as.config.Artifacts[artifact] |
| 194 |
if !ok { |
| 195 |
return Distribution{}, false |
| 196 |
} |
| 197 |
|
| 198 |
vers, ok := a.Versions[version] |
| 199 |
if !ok { |
| 200 |
return Distribution{}, false |
| 201 |
} |
| 202 |
|
| 203 |
distro, ok := vers.Distributions[distribution] |
| 204 |
return distro, ok |
| 205 |
} |