Skip to content

Commit 8457d2f

Browse files
Merge branch 'main' into patch-1
2 parents 1362dbe + 061ee6b commit 8457d2f

30 files changed

Lines changed: 613 additions & 61 deletions

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
with:
1818
go-version: 1.18
1919
check-latest: true
20-
- uses: goreleaser/[email protected].0
20+
- uses: goreleaser/[email protected].1
2121
id: run-goreleaser
2222
with:
2323
version: latest
@@ -72,7 +72,7 @@ jobs:
7272
run: |
7373
set -euo pipefail
7474
gh -R "$GITHUB_REPOSITORY" release download "$GITHUB_REF_NAME" -p "*.tar.gz"
75-
gh -R "$GITHUB_REPOSITORY" release download "$GITHUB_REF_NAME" -p "attestation.intoto.jsonl"
75+
gh -R "$GITHUB_REPOSITORY" release download "$GITHUB_REF_NAME" -p "multiple.intoto.jsonl"
7676
- name: Verify assets
7777
env:
7878
CHECKSUMS: ${{ needs.goreleaser.outputs.hashes }}

.github/workflows/style.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
go-version: 1.18
2929
check-latest: true
3030

31-
- uses: golangci/golangci-lint-action@v3.3.1
31+
- uses: golangci/golangci-lint-action@v3.4.0
3232
with:
3333
version: v1.45.2
3434

.golangci.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,9 @@ linters:
3232

3333
disable:
3434
- errcheck
35+
36+
linters-settings:
37+
depguard:
38+
include-go-root: true
39+
packages-with-error-message:
40+
- crypto/sha256: "use crypto.SHA256 instead"

cmd/crane/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ A collection of useful things you can do with `crane` is [here](recipes.md).
1313
1. Download [latest release](https://github.com/google/go-containerregistry/releases/latest):
1414

1515
```sh
16-
$ VERSION=TODO # Latest, or other
16+
$ OS=Linux # or Darwin, Windows
17+
$ ARCH=x86_64 # or arm64, x86_64, armv6, i386, s390x
18+
$ curl -sL "https://github.com/google/go-containerregistry/releases/latest/download/go-containerregistry_${OS}_${ARCH}.tar.gz" > go-containerregistry.tar.gz
19+
```
20+
21+
Download a specific version:
22+
```
23+
$ VERSION=TODO # Version number without leading v
1724
$ OS=Linux # or Darwin, Windows
1825
$ ARCH=x86_64 # or arm64, x86_64, armv6, i386, s390x
1926
$ curl -sL "https://github.com/google/go-containerregistry/releases/download/v${VERSION}/go-containerregistry_${OS}_${ARCH}.tar.gz" > go-containerregistry.tar.gz

internal/cmd/edit.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cmd
1717
import (
1818
"archive/tar"
1919
"bytes"
20+
"encoding/json"
2021
"errors"
2122
"fmt"
2223
"io"
@@ -30,6 +31,7 @@ import (
3031
v1 "github.com/google/go-containerregistry/pkg/v1"
3132
"github.com/google/go-containerregistry/pkg/v1/mutate"
3233
"github.com/google/go-containerregistry/pkg/v1/remote"
34+
"github.com/google/go-containerregistry/pkg/v1/static"
3335
"github.com/google/go-containerregistry/pkg/v1/tarball"
3436
"github.com/google/go-containerregistry/pkg/v1/types"
3537
"github.com/spf13/cobra"
@@ -158,6 +160,15 @@ func editConfig(in io.Reader, out io.Writer, src, dst string, options ...crane.O
158160
return nil, err
159161
}
160162

163+
m, err := img.Manifest()
164+
if err != nil {
165+
return nil, err
166+
}
167+
mt, err := img.MediaType()
168+
if err != nil {
169+
return nil, err
170+
}
171+
161172
var edited []byte
162173
if interactive(in, out) {
163174
rcf, err := img.RawConfigFile()
@@ -176,21 +187,25 @@ func editConfig(in io.Reader, out io.Writer, src, dst string, options ...crane.O
176187
edited = b
177188
}
178189

179-
cf, err := v1.ParseConfigFile(bytes.NewReader(edited))
190+
l := static.NewLayer(edited, m.Config.MediaType)
191+
layerDigest, err := l.Digest()
180192
if err != nil {
181193
return nil, err
182194
}
183195

184-
img, err = mutate.ConfigFile(img, cf)
196+
m.Config.Digest = layerDigest
197+
m.Config.Size = int64(len(edited))
198+
b, err := json.Marshal(m)
185199
if err != nil {
186200
return nil, err
187201
}
188-
189-
digest, err := img.Digest()
190-
if err != nil {
191-
return nil, err
202+
rm := &rawManifest{
203+
body: b,
204+
mediaType: mt,
192205
}
193206

207+
digest, _, _ := v1.SHA256(bytes.NewReader(b))
208+
194209
if dst == "" {
195210
dst = src
196211
ref, err := name.ParseReference(src, o.Name...)
@@ -207,7 +222,11 @@ func editConfig(in io.Reader, out io.Writer, src, dst string, options ...crane.O
207222
return nil, err
208223
}
209224

210-
if err := crane.Push(img, dst, options...); err != nil {
225+
if err := remote.WriteLayer(dstRef.Context(), l, o.Remote...); err != nil {
226+
return nil, err
227+
}
228+
229+
if err := remote.Put(dstRef, rm, o.Remote...); err != nil {
211230
return nil, err
212231
}
213232

pkg/legacy/tarball/write.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ package tarball
1717
import (
1818
"archive/tar"
1919
"bytes"
20-
"crypto/sha256"
21-
"encoding/hex"
2220
"encoding/json"
2321
"fmt"
2422
"io"
@@ -63,8 +61,9 @@ func v1LayerID(layer v1.Layer, parentID string, rawConfig []byte) (string, error
6361
if len(rawConfig) != 0 {
6462
s = fmt.Sprintf("%s %s", s, string(rawConfig))
6563
}
66-
rawDigest := sha256.Sum256([]byte(s))
67-
return hex.EncodeToString(rawDigest[:]), nil
64+
65+
h, _, _ := v1.SHA256(strings.NewReader(s))
66+
return h.Hex, nil
6867
}
6968

7069
// newTopV1Layer creates a new v1Layer for a layer other than the top layer in a v1 image tarball.

pkg/name/digest.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package name
1616

1717
import (
18+
// nolint: depguard
1819
_ "crypto/sha256" // Recommended by go-digest.
1920
"strings"
2021

pkg/registry/manifest.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ package registry
1616

1717
import (
1818
"bytes"
19-
"crypto/sha256"
20-
"encoding/hex"
2119
"encoding/json"
2220
"fmt"
2321
"io"
@@ -110,9 +108,8 @@ func (m *manifests) handle(resp http.ResponseWriter, req *http.Request) *regErro
110108
Message: "Unknown manifest",
111109
}
112110
}
113-
rd := sha256.Sum256(m.blob)
114-
d := "sha256:" + hex.EncodeToString(rd[:])
115-
resp.Header().Set("Docker-Content-Digest", d)
111+
h, _, _ := v1.SHA256(bytes.NewReader(m.blob))
112+
resp.Header().Set("Docker-Content-Digest", h.String())
116113
resp.Header().Set("Content-Type", m.contentType)
117114
resp.Header().Set("Content-Length", fmt.Sprint(len(m.blob)))
118115
resp.WriteHeader(http.StatusOK)
@@ -137,9 +134,8 @@ func (m *manifests) handle(resp http.ResponseWriter, req *http.Request) *regErro
137134
Message: "Unknown manifest",
138135
}
139136
}
140-
rd := sha256.Sum256(m.blob)
141-
d := "sha256:" + hex.EncodeToString(rd[:])
142-
resp.Header().Set("Docker-Content-Digest", d)
137+
h, _, _ := v1.SHA256(bytes.NewReader(m.blob))
138+
resp.Header().Set("Docker-Content-Digest", h.String())
143139
resp.Header().Set("Content-Type", m.contentType)
144140
resp.Header().Set("Content-Length", fmt.Sprint(len(m.blob)))
145141
resp.WriteHeader(http.StatusOK)
@@ -153,8 +149,8 @@ func (m *manifests) handle(resp http.ResponseWriter, req *http.Request) *regErro
153149
}
154150
b := &bytes.Buffer{}
155151
io.Copy(b, req.Body)
156-
rd := sha256.Sum256(b.Bytes())
157-
digest := "sha256:" + hex.EncodeToString(rd[:])
152+
h, _, _ := v1.SHA256(bytes.NewReader(b.Bytes()))
153+
digest := h.String()
158154
mf := manifest{
159155
blob: b.Bytes(),
160156
contentType: req.Header.Get("Content-Type"),

pkg/registry/registry_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
package registry_test
1616

1717
import (
18-
"crypto/sha256"
19-
"encoding/hex"
2018
"fmt"
2119
"io"
2220
"log"
@@ -27,6 +25,7 @@ import (
2725
"testing"
2826

2927
"github.com/google/go-containerregistry/pkg/registry"
28+
v1 "github.com/google/go-containerregistry/pkg/v1"
3029
)
3130

3231
const (
@@ -47,8 +46,8 @@ const (
4746
)
4847

4948
func sha256String(s string) string {
50-
h := sha256.Sum256([]byte(s))
51-
return hex.EncodeToString(h[:])
49+
h, _, _ := v1.SHA256(strings.NewReader(s))
50+
return h.Hex
5251
}
5352

5453
func TestCalls(t *testing.T) {

pkg/v1/hash.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package v1
1616

1717
import (
18-
"crypto/sha256"
18+
"crypto"
1919
"encoding/hex"
2020
"encoding/json"
2121
"fmt"
@@ -78,7 +78,7 @@ func (h *Hash) UnmarshalText(text []byte) error {
7878
func Hasher(name string) (hash.Hash, error) {
7979
switch name {
8080
case "sha256":
81-
return sha256.New(), nil
81+
return crypto.SHA256.New(), nil
8282
default:
8383
return nil, fmt.Errorf("unsupported hash: %q", name)
8484
}
@@ -111,7 +111,7 @@ func (h *Hash) parse(unquoted string) error {
111111

112112
// SHA256 computes the Hash of the provided io.Reader's content.
113113
func SHA256(r io.Reader) (Hash, int64, error) {
114-
hasher := sha256.New()
114+
hasher := crypto.SHA256.New()
115115
n, err := io.Copy(hasher, r)
116116
if err != nil {
117117
return Hash{}, 0, err

0 commit comments

Comments
 (0)