Skip to content

Commit

Permalink
clarity :)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendanCoughlan5 committed Feb 14, 2025
1 parent e0bae8f commit c0355ed
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 63 deletions.
2 changes: 1 addition & 1 deletion cmd/createSnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var createSnapshotCmd = &cobra.Command{
}

if err := svc.CreateSnapshot(); err != nil {
l.Sugar().Fatal("failed to create snapshot", zap.Error(err))
l.Sugar().Fatalw("failed to create snapshot", zap.Error(err))
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/restoreSnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Follow the snapshot docs if you need to convert the snapshot to a different sche
}

if err := svc.RestoreSnapshot(); err != nil {
l.Sugar().Fatal("failed to restore snapshot", zap.Error(err))
l.Sugar().Fatalw("failed to restore snapshot", zap.Error(err))
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func init() {
// bind any subcommand flags
createSnapshotCmd.PersistentFlags().String(config.SnapshotOutputFile, "", "Path to save the snapshot file to (required), also creates a hash file")

restoreSnapshotCmd.PersistentFlags().String(config.SnapshotInput, "", "Path to the snapshot file either a URL or a local file (required)")
restoreSnapshotCmd.PersistentFlags().Bool(config.SnapshotVerifyInput, true, "Boolean to verify the input file against its .sha256sum file, if input is a url then it downloads the file, (default is true)")
restoreSnapshotCmd.PersistentFlags().String(config.SnapshotInput, "", "Path to a local snapshot file, or remote URL to download from (required)")
restoreSnapshotCmd.PersistentFlags().Bool(config.SnapshotVerifyInput, true, "Boolean to verify the input file against a sha256 checksum (<file name>.sha256sum). If a URL is provided, the checksum will be downloaded")

rpcCmd.PersistentFlags().String(config.SidecarPrimaryUrl, "", `RPC url of the "primary" Sidecar instance in an HA environment`)

Expand Down
40 changes: 3 additions & 37 deletions pkg/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,6 @@ func (s *SnapshotService) RestoreSnapshot() error {
var resolvedFilePath string
if isHttpURL(s.cfg.Input) {
inputUrl := s.cfg.Input
// Check if the input URL exists
snapshotExists, err := urlExists(inputUrl)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("error checking existence of snapshot URL '%s'", inputUrl))
}
if !snapshotExists {
return errors.Wrap(fmt.Errorf("snapshot file not found at '%s'. Ensure the file exists", inputUrl), "snapshot file not found")
}

fileName, err := getFileNameFromURL(inputUrl)
if err != nil {
Expand All @@ -145,13 +137,6 @@ func (s *SnapshotService) RestoreSnapshot() error {
if s.cfg.VerifyInput {
hashFilePath := getHashName(inputFilePath)
hashUrl := getHashName(inputUrl)
hashFileExists, err := urlExists(hashUrl)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("error checking existence of snapshot hash URL '%s'", hashUrl))
}
if !hashFileExists {
return errors.Wrap(fmt.Errorf("snapshot hash file not found at '%s'. Ensure the file exists or set --verify-input=false to skip verification", hashUrl), "snapshot hash file not found")
}

err = downloadFile(hashUrl, hashFilePath)
if err != nil {
Expand Down Expand Up @@ -183,7 +168,7 @@ func (s *SnapshotService) RestoreSnapshot() error {

// validate snapshot against the hash file
if s.cfg.VerifyInput {
if err := validateInputFileHash(resolvedFilePath, getHashName(resolvedFilePath)); err != nil {
if err := validateInputFileHash(resolvedFilePath); err != nil {
return errors.Wrap(err, fmt.Sprintf("input file hash validation failed for '%s'", resolvedFilePath))
}
s.l.Sugar().Debugw("Input file hash validated successfully",
Expand Down Expand Up @@ -300,7 +285,8 @@ func cleanupTempFiles(files []string, l *zap.Logger) {
}

// validateInputFileHash validates the InputFile against the hash in InputHashFile.
func validateInputFileHash(inputFile, hashFile string) error {
func validateInputFileHash(inputFile string) error {
hashFile := getHashName(inputFile)
hashFileContent, err := os.ReadFile(hashFile)
if err != nil {
return fmt.Errorf("failed to read hash file: %w", err)
Expand Down Expand Up @@ -388,23 +374,3 @@ func getFileNameFromURL(rawURL string) (string, error) {
}
return path.Base(parsedURL.Path), nil
}

// urlExists checks if the given URL is accessible by sending a HEAD request.
func urlExists(url string) (bool, error) {
resp, err := http.Head(url)
if err != nil {
return false, errors.Wrap(err, "failed to send HEAD request")
}
defer resp.Body.Close()

if resp.StatusCode >= 200 && resp.StatusCode < 400 {
return true, nil
}

// Return false for 404 without an error
if resp.StatusCode == http.StatusNotFound {
return false, nil
}

return false, fmt.Errorf("URL not accessible, received status code %d", resp.StatusCode)
}
28 changes: 6 additions & 22 deletions pkg/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,27 +148,11 @@ func TestSaveOutputFileHashCompatibilityWithSha256sum(t *testing.T) {
err = saveOutputFileHash(outputFile, outputHashFile)
assert.NoError(t, err, "Saving output file hash should not fail")

// Shell out to sha256sum to verify the hash
cmd := exec.Command("sha256sum", outputFile)
sha256sumOutput, err := cmd.Output()
assert.NoError(t, err, "Executing sha256sum should not fail")

// Extract the hash from the sha256sum output
sha256sumParts := strings.Fields(string(sha256sumOutput))
assert.Equal(t, 2, len(sha256sumParts), "sha256sum output should contain two parts: hash and filename")

// Read the generated hash file
hashContent, err := os.ReadFile(outputHashFile)
assert.NoError(t, err, "Reading output hash file should not fail")

// Validate the format of the hash file
hashParts := strings.Fields(string(hashContent))
assert.Equal(t, 2, len(hashParts), "Output hash file should contain two parts: hash and filename")
assert.Equal(t, filepath.Base(outputFile), hashParts[1], "Output hash file should contain the correct filename")
assert.Equal(t, sha256.Size*2, len(hashParts[0]), "Output hash file should have the correct hash length")

// Compare the hash from sha256sum with the one generated by saveOutputFileHash
assert.Equal(t, sha256sumParts[0], hashParts[0], "Hashes should match between sha256sum and saveOutputFileHash")
// Use sha256sum -c to verify the hash
cmd := exec.Command("sha256sum", "-c", outputHashFile)
cmd.Dir = tempDir // Ensure the command runs in the directory containing the files
err = cmd.Run()
assert.NoError(t, err, "sha256sum -c should verify the hash successfully")
}

func TestCleanupTempFiles(t *testing.T) {
Expand Down Expand Up @@ -207,7 +191,7 @@ func TestValidateInputFileHash(t *testing.T) {
err = os.WriteFile(hashFile, sha256sumOutput, 0644)
assert.NoError(t, err, "Writing sha256sum output to hash file should not fail")

err = validateInputFileHash(inputFile, hashFile)
err = validateInputFileHash(inputFile)
assert.NoError(t, err, "Input file hash should be valid")
}

Expand Down
1 change: 1 addition & 0 deletions sidecar_testnet_holesky_check.dump.sha256sum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
259dc1acc54745a1cdb8a900f0efba35449e592c08d316c906505565348f4843 sidecar_testnet_holesky_check.dump

0 comments on commit c0355ed

Please sign in to comment.