Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace uses of deprecated ioutil #229

Merged
merged 1 commit into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions cmd/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"

intoto "github.com/in-toto/in-toto-golang/in_toto"
Expand Down Expand Up @@ -102,22 +101,12 @@ func verify(cmd *cobra.Command, args []string) error {

intermediatePems := make([][]byte, 0, len(intermediatePaths))
for _, intermediate := range intermediatePaths {
f, err := os.Open(intermediate)
if err != nil {
return fmt.Errorf("failed to open intermediate %s: %w", intermediate, err)
}
defer f.Close()

pemBytes, err := ioutil.ReadAll(f)
pemBytes, err := os.ReadFile(intermediate)
if err != nil {
return fmt.Errorf("failed to read intermediate %s: %w", intermediate, err)
}

intermediatePems = append(intermediatePems, pemBytes)

if err := f.Close(); err != nil {
return fmt.Errorf("could not close intermediate cert: %w", err)
}
}

_, err := intoto.InTotoVerify(layoutMb, layoutKeys, linkDir, "", make(map[string]string), intermediatePems, lineNormalization)
Expand Down
7 changes: 3 additions & 4 deletions in_toto/in_toto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package in_toto

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -14,7 +13,7 @@ const testData = "../test/data"
// This can be used for test setup and teardown, e.g. copy test data to a tmp
// test dir, change to that dir and remove the and contents in the end
func TestMain(m *testing.M) {
testDir, err := ioutil.TempDir("", "in_toto_test_dir")
testDir, err := os.MkdirTemp("", "in_toto_test_dir")
if err != nil {
panic("Cannot create temp test dir")
}
Expand All @@ -23,12 +22,12 @@ func TestMain(m *testing.M) {
// NOTE: Only works for a flat directory of files
testFiles, _ := filepath.Glob(filepath.Join(testData, "*"))
for _, inputPath := range testFiles {
input, err := ioutil.ReadFile(inputPath)
input, err := os.ReadFile(inputPath)
if err != nil {
panic(fmt.Sprintf("Cannot copy test files (read error: %s)", err))
}
outputPath := filepath.Join(testDir, filepath.Base(inputPath))
err = ioutil.WriteFile(outputPath, input, 0644)
err = os.WriteFile(outputPath, input, 0644)
if err != nil {
panic(fmt.Sprintf("Cannot copy test files (write error: %s)", err))
}
Expand Down
5 changes: 2 additions & 3 deletions in_toto/keylib.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -325,7 +324,7 @@ func (k *Key) LoadKeyReader(r io.Reader, scheme string, KeyIDHashAlgorithms []st
return ErrNoPEMBlock
}
// Read key bytes
pemBytes, err := ioutil.ReadAll(r)
pemBytes, err := io.ReadAll(r)
if err != nil {
return err
}
Expand All @@ -344,7 +343,7 @@ func (k *Key) LoadKeyReaderDefaults(r io.Reader) error {
return ErrNoPEMBlock
}
// Read key bytes
pemBytes, err := ioutil.ReadAll(r)
pemBytes, err := io.ReadAll(r)
if err != nil {
return err
}
Expand Down
14 changes: 3 additions & 11 deletions in_toto/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"reflect"
"regexp"
Expand Down Expand Up @@ -771,15 +770,8 @@ object on which it was called. It returns an error if it cannot parse
a valid JSON formatted Metablock that contains a Link or Layout.
*/
func (mb *Metablock) Load(path string) error {
// Open file and close before returning
jsonFile, err := os.Open(path)
if err != nil {
return err
}
defer jsonFile.Close()

// Read entire file
jsonBytes, err := ioutil.ReadAll(jsonFile)
jsonBytes, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down Expand Up @@ -852,7 +844,7 @@ func (mb *Metablock) Load(path string) error {
" metadata must be one of 'link' or 'layout'")
}

return jsonFile.Close()
return nil
}

/*
Expand All @@ -868,7 +860,7 @@ func (mb *Metablock) Dump(path string) error {
}

// Write JSON bytes to the passed path with permissions (-rw-r--r--)
err = ioutil.WriteFile(path, jsonBytes, 0644)
err = os.WriteFile(path, jsonBytes, 0644)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions in_toto/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -135,7 +134,7 @@ func TestMetablockLoad(t *testing.T) {

for i := 0; i < len(invalidJSONBytes); i++ {
fn := fmt.Sprintf("invalid-metadata-%v.tmp", i)
if err := ioutil.WriteFile(fn, invalidJSONBytes[i], 0644); err != nil {
if err := os.WriteFile(fn, invalidJSONBytes[i], 0644); err != nil {
fmt.Printf("Could not write file: %s", err)
}
var mb Metablock
Expand Down
8 changes: 4 additions & 4 deletions in_toto/runlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -44,7 +44,7 @@ normalized to Unix-style line separators (LF) before hashing file contents.
func RecordArtifact(path string, hashAlgorithms []string, lineNormalization bool) (map[string]interface{}, error) {
supportedHashMappings := getHashMapping()
// Read file from passed path
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
hashedContentsMap := make(map[string]interface{})
if err != nil {
return nil, err
Expand Down Expand Up @@ -289,8 +289,8 @@ func RunCommand(cmdArgs []string, runDir string) (map[string]interface{}, error)
}

// TODO: duplicate stdout, stderr
stdout, _ := ioutil.ReadAll(stdoutPipe)
stderr, _ := ioutil.ReadAll(stderrPipe)
stdout, _ := io.ReadAll(stdoutPipe)
stderr, _ := io.ReadAll(stderrPipe)

retVal := waitErrToExitCode(cmd.Wait())

Expand Down
7 changes: 3 additions & 4 deletions in_toto/runlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -210,7 +209,7 @@ func TestSymlinkToFolder(t *testing.T) {
// not work correctly on Windows
p := filepath.FromSlash("symTest/symTest2/symTmpfile")

if err := ioutil.WriteFile(p, []byte("abc"), 0400); err != nil {
if err := os.WriteFile(p, []byte("abc"), 0400); err != nil {
t.Errorf("could not write symTmpfile: %s", err)
}

Expand Down Expand Up @@ -285,7 +284,7 @@ func TestRecordArtifacts(t *testing.T) {
if err := os.Mkdir("tmpdir", 0700); err != nil {
t.Errorf("could not create tmpdir: %s", err)
}
if err := ioutil.WriteFile("tmpdir/tmpfile", []byte("abc"), 0400); err != nil {
if err := os.WriteFile("tmpdir/tmpfile", []byte("abc"), 0400); err != nil {
t.Errorf("could not write tmpfile: %s", err)
}
result, err := RecordArtifacts([]string{"foo.tar.gz",
Expand All @@ -303,7 +302,7 @@ func TestRecordArtifacts(t *testing.T) {
result, err, expected)
}
// Test duplicated artifact after left strip
if err := ioutil.WriteFile("tmpdir/foo.tar.gz", []byte("abc"), 0400); err != nil {
if err := os.WriteFile("tmpdir/foo.tar.gz", []byte("abc"), 0400); err != nil {
t.Errorf("could not write tmpfile: %s", err)
}
_, err = RecordArtifacts([]string{"foo.tar.gz",
Expand Down
5 changes: 2 additions & 3 deletions in_toto/util_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package in_toto

import (
"io/ioutil"
"os"
"reflect"
"sort"
Expand Down Expand Up @@ -115,11 +114,11 @@ func TestSubsetCheck(t *testing.T) {
}

func TestIsWritable(t *testing.T) {
notWritable, err := ioutil.TempDir("", "")
notWritable, err := os.MkdirTemp("", "")
if err != nil {
t.Error(err)
}
writable, err := ioutil.TempDir("", "")
writable, err := os.MkdirTemp("", "")
if err != nil {
t.Error(err)
}
Expand Down
Loading