| 1 |
/* |
| 2 |
Copyright The ORAS Authors. |
| 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 |
|
| 16 |
package remote |
| 17 |
|
| 18 |
import ( |
| 19 |
"errors" |
| 20 |
"fmt" |
| 21 |
"strings" |
| 22 |
|
| 23 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 24 |
"oras.land/oras-go/v2/content" |
| 25 |
"oras.land/oras-go/v2/internal/descriptor" |
| 26 |
) |
| 27 |
|
| 28 |
// zeroDigest represents a digest that consists of zeros. zeroDigest is used |
| 29 |
// for pinging Referrers API. |
| 30 |
const zeroDigest = "sha256:0000000000000000000000000000000000000000000000000000000000000000" |
| 31 |
|
| 32 |
// referrersState represents the state of Referrers API. |
| 33 |
type referrersState = int32 |
| 34 |
|
| 35 |
const ( |
| 36 |
// referrersStateUnknown represents an unknown state of Referrers API. |
| 37 |
referrersStateUnknown referrersState = iota |
| 38 |
// referrersStateSupported represents that the repository is known to |
| 39 |
// support Referrers API. |
| 40 |
referrersStateSupported |
| 41 |
// referrersStateUnsupported represents that the repository is known to |
| 42 |
// not support Referrers API. |
| 43 |
referrersStateUnsupported |
| 44 |
) |
| 45 |
|
| 46 |
// referrerOperation represents an operation on a referrer. |
| 47 |
type referrerOperation = int32 |
| 48 |
|
| 49 |
const ( |
| 50 |
// referrerOperationAdd represents an addition operation on a referrer. |
| 51 |
referrerOperationAdd referrerOperation = iota |
| 52 |
// referrerOperationRemove represents a removal operation on a referrer. |
| 53 |
referrerOperationRemove |
| 54 |
) |
| 55 |
|
| 56 |
// referrerChange represents a change on a referrer. |
| 57 |
type referrerChange struct { |
| 58 |
referrer ocispec.Descriptor |
| 59 |
operation referrerOperation |
| 60 |
} |
| 61 |
|
| 62 |
var ( |
| 63 |
// ErrReferrersCapabilityAlreadySet is returned by SetReferrersCapability() |
| 64 |
// when the Referrers API capability has been already set. |
| 65 |
ErrReferrersCapabilityAlreadySet = errors.New("referrers capability cannot be changed once set") |
| 66 |
|
| 67 |
// errNoReferrerUpdate is returned by applyReferrerChanges() when there |
| 68 |
// is no any referrer update. |
| 69 |
errNoReferrerUpdate = errors.New("no referrer update") |
| 70 |
) |
| 71 |
|
| 72 |
const ( |
| 73 |
// opDeleteReferrersIndex represents the operation for deleting a |
| 74 |
// referrers index. |
| 75 |
opDeleteReferrersIndex = "DeleteReferrersIndex" |
| 76 |
) |
| 77 |
|
| 78 |
// ReferrersError records an error and the operation and the subject descriptor. |
| 79 |
type ReferrersError struct { |
| 80 |
// Op represents the failing operation. |
| 81 |
Op string |
| 82 |
// Subject is the descriptor of referenced artifact. |
| 83 |
Subject ocispec.Descriptor |
| 84 |
// Err is the entity of referrers error. |
| 85 |
Err error |
| 86 |
} |
| 87 |
|
| 88 |
// Error returns error msg of IgnorableError. |
| 89 |
func (e *ReferrersError) Error() string { |
| 90 |
return e.Err.Error() |
| 91 |
} |
| 92 |
|
| 93 |
// Unwrap returns the inner error of IgnorableError. |
| 94 |
func (e *ReferrersError) Unwrap() error { |
| 95 |
return errors.Unwrap(e.Err) |
| 96 |
} |
| 97 |
|
| 98 |
// IsIndexDelete tells if e is kind of error related to referrers |
| 99 |
// index deletion. |
| 100 |
func (e *ReferrersError) IsReferrersIndexDelete() bool { |
| 101 |
return e.Op == opDeleteReferrersIndex |
| 102 |
} |
| 103 |
|
| 104 |
// buildReferrersTag builds the referrers tag for the given manifest descriptor. |
| 105 |
// Format: <algorithm>-<digest> |
| 106 |
// Reference: https://github.com/opencontainers/distribution-spec/blob/v1.1.1/spec.md#unavailable-referrers-api |
| 107 |
func buildReferrersTag(desc ocispec.Descriptor) (string, error) { |
| 108 |
if err := desc.Digest.Validate(); err != nil { |
| 109 |
return "", fmt.Errorf("failed to build referrers tag for %s: %w", desc.Digest, err) |
| 110 |
} |
| 111 |
alg := desc.Digest.Algorithm().String() |
| 112 |
encoded := desc.Digest.Encoded() |
| 113 |
return alg + "-" + encoded, nil |
| 114 |
} |
| 115 |
|
| 116 |
// isReferrersFilterApplied checks if requsted is in the applied filter list. |
| 117 |
func isReferrersFilterApplied(applied, requested string) bool { |
| 118 |
if applied == "" || requested == "" { |
| 119 |
return false |
| 120 |
} |
| 121 |
filters := strings.SplitSeq(applied, ",") |
| 122 |
for f := range filters { |
| 123 |
if f == requested { |
| 124 |
return true |
| 125 |
} |
| 126 |
} |
| 127 |
return false |
| 128 |
} |
| 129 |
|
| 130 |
// filterReferrers filters a slice of referrers by artifactType in place. |
| 131 |
// The returned slice contains matching referrers. |
| 132 |
func filterReferrers(refs []ocispec.Descriptor, artifactType string) []ocispec.Descriptor { |
| 133 |
if artifactType == "" { |
| 134 |
return refs |
| 135 |
} |
| 136 |
var j int |
| 137 |
for i, ref := range refs { |
| 138 |
if ref.ArtifactType == artifactType { |
| 139 |
if i != j { |
| 140 |
refs[j] = ref |
| 141 |
} |
| 142 |
j++ |
| 143 |
} |
| 144 |
} |
| 145 |
return refs[:j] |
| 146 |
} |
| 147 |
|
| 148 |
// applyReferrerChanges applies referrerChanges on referrers and returns the |
| 149 |
// updated referrers. |
| 150 |
// Returns errNoReferrerUpdate if there is no any referrers updates. |
| 151 |
func applyReferrerChanges(referrers []ocispec.Descriptor, referrerChanges []referrerChange) ([]ocispec.Descriptor, error) { |
| 152 |
referrersMap := make(map[descriptor.Descriptor]int, len(referrers)+len(referrerChanges)) |
| 153 |
updatedReferrers := make([]ocispec.Descriptor, 0, len(referrers)+len(referrerChanges)) |
| 154 |
var updateRequired bool |
| 155 |
for _, r := range referrers { |
| 156 |
if content.Equal(r, ocispec.Descriptor{}) { |
| 157 |
// skip bad entry |
| 158 |
updateRequired = true |
| 159 |
continue |
| 160 |
} |
| 161 |
key := descriptor.FromOCI(r) |
| 162 |
if _, ok := referrersMap[key]; ok { |
| 163 |
// skip duplicates |
| 164 |
updateRequired = true |
| 165 |
continue |
| 166 |
} |
| 167 |
updatedReferrers = append(updatedReferrers, r) |
| 168 |
referrersMap[key] = len(updatedReferrers) - 1 |
| 169 |
} |
| 170 |
|
| 171 |
// apply changes |
| 172 |
for _, change := range referrerChanges { |
| 173 |
key := descriptor.FromOCI(change.referrer) |
| 174 |
switch change.operation { |
| 175 |
case referrerOperationAdd: |
| 176 |
if _, ok := referrersMap[key]; !ok { |
| 177 |
// add distinct referrers |
| 178 |
updatedReferrers = append(updatedReferrers, change.referrer) |
| 179 |
referrersMap[key] = len(updatedReferrers) - 1 |
| 180 |
} |
| 181 |
case referrerOperationRemove: |
| 182 |
if pos, ok := referrersMap[key]; ok { |
| 183 |
// remove referrers that are already in the map |
| 184 |
updatedReferrers[pos] = ocispec.Descriptor{} |
| 185 |
delete(referrersMap, key) |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
// skip unnecessary update |
| 191 |
if !updateRequired && len(referrersMap) == len(referrers) { |
| 192 |
// if the result referrer map contains the same content as the |
| 193 |
// original referrers, consider that there is no update on the |
| 194 |
// referrers. |
| 195 |
for _, r := range referrers { |
| 196 |
key := descriptor.FromOCI(r) |
| 197 |
if _, ok := referrersMap[key]; !ok { |
| 198 |
updateRequired = true |
| 199 |
} |
| 200 |
} |
| 201 |
if !updateRequired { |
| 202 |
return nil, errNoReferrerUpdate |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
return removeEmptyDescriptors(updatedReferrers, len(referrersMap)), nil |
| 207 |
} |
| 208 |
|
| 209 |
// removeEmptyDescriptors in-place removes empty items from descs, given a hint |
| 210 |
// of the number of non-empty descriptors. |
| 211 |
func removeEmptyDescriptors(descs []ocispec.Descriptor, hint int) []ocispec.Descriptor { |
| 212 |
j := 0 |
| 213 |
for i, r := range descs { |
| 214 |
if !content.Equal(r, ocispec.Descriptor{}) { |
| 215 |
if i > j { |
| 216 |
descs[j] = r |
| 217 |
} |
| 218 |
j++ |
| 219 |
} |
| 220 |
if j == hint { |
| 221 |
break |
| 222 |
} |
| 223 |
} |
| 224 |
return descs[:j] |
| 225 |
} |