-
Notifications
You must be signed in to change notification settings - Fork 542
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move logic out of v1util into other packages, leave aliases for back-compat. * Eliminate our own reliance on v1util * Add deprecation notices to trigger golangci-lint * Fix linting issues * Use 'Deprecated:' * Add TODO(issue) for removing deprecated things * Move verification to internal package * Eliminate stutter * Fix victor linter
- Loading branch information
Showing
17 changed files
with
241 additions
and
172 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
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
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
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,96 @@ | ||
// Copyright 2020 Google LLC All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package gzip | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"io" | ||
|
||
"github.com/google/go-containerregistry/pkg/v1/internal/and" | ||
) | ||
|
||
var gzipMagicHeader = []byte{'\x1f', '\x8b'} | ||
|
||
// ReadCloser reads uncompressed input data from the io.ReadCloser and | ||
// returns an io.ReadCloser from which compressed data may be read. | ||
// This uses gzip.BestSpeed for the compression level. | ||
func ReadCloser(r io.ReadCloser) io.ReadCloser { | ||
return ReadCloserLevel(r, gzip.BestSpeed) | ||
} | ||
|
||
// ReadCloserLevel reads uncompressed input data from the io.ReadCloser and | ||
// returns an io.ReadCloser from which compressed data may be read. | ||
// Refer to compress/gzip for the level: | ||
// https://golang.org/pkg/compress/gzip/#pkg-constants | ||
func ReadCloserLevel(r io.ReadCloser, level int) io.ReadCloser { | ||
pr, pw := io.Pipe() | ||
|
||
// Returns err so we can pw.CloseWithError(err) | ||
go func() error { | ||
// TODO(go1.14): Just defer {pw,gw,r}.Close like you'd expect. | ||
// Context: https://golang.org/issue/24283 | ||
gw, err := gzip.NewWriterLevel(pw, level) | ||
if err != nil { | ||
return pw.CloseWithError(err) | ||
} | ||
|
||
if _, err := io.Copy(gw, r); err != nil { | ||
defer r.Close() | ||
defer gw.Close() | ||
return pw.CloseWithError(err) | ||
} | ||
defer pw.Close() | ||
defer r.Close() | ||
defer gw.Close() | ||
|
||
return nil | ||
}() | ||
|
||
return pr | ||
} | ||
|
||
// UnzipReadCloser reads compressed input data from the io.ReadCloser and | ||
// returns an io.ReadCloser from which uncompessed data may be read. | ||
func UnzipReadCloser(r io.ReadCloser) (io.ReadCloser, error) { | ||
gr, err := gzip.NewReader(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &and.ReadCloser{ | ||
Reader: gr, | ||
CloseFunc: func() error { | ||
// If the unzip fails, then this seems to return the same | ||
// error as the read. We don't want this to interfere with | ||
// us closing the main ReadCloser, since this could leave | ||
// an open file descriptor (fails on Windows). | ||
gr.Close() | ||
return r.Close() | ||
}, | ||
}, nil | ||
} | ||
|
||
// Is detects whether the input stream is compressed. | ||
func Is(r io.Reader) (bool, error) { | ||
magicHeader := make([]byte, 2) | ||
n, err := r.Read(magicHeader) | ||
if n == 0 && err == io.EOF { | ||
return false, nil | ||
} | ||
if err != nil { | ||
return false, err | ||
} | ||
return bytes.Equal(magicHeader, gzipMagicHeader), nil | ||
} |
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
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,62 @@ | ||
// Copyright 2020 Google LLC All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package verify | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"hash" | ||
"io" | ||
|
||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/internal/and" | ||
) | ||
|
||
type verifyReader struct { | ||
inner io.Reader | ||
hasher hash.Hash | ||
expected v1.Hash | ||
} | ||
|
||
// Read implements io.Reader | ||
func (vc *verifyReader) Read(b []byte) (int, error) { | ||
n, err := vc.inner.Read(b) | ||
if err == io.EOF { | ||
got := hex.EncodeToString(vc.hasher.Sum(make([]byte, 0, vc.hasher.Size()))) | ||
if want := vc.expected.Hex; got != want { | ||
return n, fmt.Errorf("error verifying %s checksum; got %q, want %q", | ||
vc.expected.Algorithm, got, want) | ||
} | ||
} | ||
return n, err | ||
} | ||
|
||
// ReadCloser wraps the given io.ReadCloser to verify that its contents match | ||
// the provided v1.Hash before io.EOF is returned. | ||
func ReadCloser(r io.ReadCloser, h v1.Hash) (io.ReadCloser, error) { | ||
w, err := v1.Hasher(h.Algorithm) | ||
if err != nil { | ||
return nil, err | ||
} | ||
r2 := io.TeeReader(r, w) | ||
return &and.ReadCloser{ | ||
Reader: &verifyReader{ | ||
inner: r2, | ||
hasher: w, | ||
expected: h, | ||
}, | ||
CloseFunc: r.Close, | ||
}, nil | ||
} |
Oops, something went wrong.