-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup the code that deals with the different hashing algorithms used to create kubectl checksums Signed-off-by: Flavio Castelli <fcastelli@suse.com>
- Loading branch information
Showing
3 changed files
with
112 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package downloader | ||
|
||
import ( | ||
"crypto/sha1" //nolint:gosec // sha1 is needed by old releases of kubectl | ||
"crypto/sha512" | ||
"hash" | ||
|
||
"github.com/blang/semver/v4" | ||
) | ||
|
||
// Hashing contains the hashing details for the downloader | ||
type Hashing struct { | ||
// Suffix of the file containing the hash | ||
Suffix string | ||
|
||
// Hasher is the hash calculator to use | ||
Hasher hash.Hash | ||
} | ||
|
||
// NewHashing returns the hashing details for the downloader | ||
// | ||
//nolint:gosec // sha1 is needed by old releases of kubectl | ||
func NewHashing(version semver.Version) (*Hashing, error) { | ||
// - sha1 is available in range [1.0.0, 1.18) | ||
// - sha256 is available from v1.16.0 | ||
// - sha512 is available from 1.12.0 | ||
|
||
rangeConstraint, parseErr := semver.ParseRange(">=1.12.0") | ||
if parseErr != nil { | ||
return nil, parseErr | ||
} | ||
if rangeConstraint(version) { | ||
return &Hashing{ | ||
Suffix: ".sha512", | ||
Hasher: sha512.New(), | ||
}, nil | ||
} | ||
|
||
// we have to resort to sha1 | ||
return &Hashing{ | ||
Suffix: ".sha1", | ||
Hasher: sha1.New(), | ||
}, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package downloader | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha1" //nolint:gosec // sha1 is needed by old releases of kubectl | ||
"crypto/sha512" | ||
"testing" | ||
|
||
"github.com/blang/semver/v4" | ||
) | ||
|
||
func TestHashingDetails(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
version string | ||
expectedSuffix string | ||
inputData []byte | ||
expectedHash []byte | ||
}{ | ||
{ | ||
name: "sha1", | ||
version: "1.11.0", | ||
expectedSuffix: ".sha1", | ||
inputData: []byte("hello"), | ||
expectedHash: sha1.New().Sum([]byte("hello")), //nolint:gosec // sha1 is needed by old releases of kubectl | ||
}, | ||
{ | ||
name: "sha512", | ||
version: "1.12.0", | ||
expectedSuffix: ".sha512", | ||
inputData: []byte("hello"), | ||
expectedHash: sha512.New().Sum([]byte("hello")), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
tableTest := test // ensure tt is correctly scoped when used in function literal | ||
t.Run(tableTest.name, func(t *testing.T) { | ||
version, err := semver.Parse(tableTest.version) | ||
if err != nil { | ||
t.Fatalf("failed to parse version %s: %v", tableTest.version, err) | ||
} | ||
|
||
hashingDetails, err := NewHashing(version) | ||
if err != nil { | ||
t.Fatalf("failed to create hashing details: %v", err) | ||
} | ||
|
||
if hashingDetails.Suffix != tableTest.expectedSuffix { | ||
t.Errorf("expected suffix %s, got %s", tableTest.expectedSuffix, hashingDetails.Suffix) | ||
} | ||
|
||
hash := hashingDetails.Hasher.Sum(tableTest.inputData) | ||
if !bytes.Equal(hash, tableTest.expectedHash) { | ||
t.Errorf("expected hash %v, got %v", tableTest.expectedHash, hash) | ||
} | ||
}) | ||
} | ||
} |