Skip to content

Commit

Permalink
chore: remove deprecated io/ioutil
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Kem committed Sep 3, 2023
1 parent c7d18ae commit 0a61d52
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
19 changes: 9 additions & 10 deletions internal/fsStorage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/gofrs/flock"
"github.com/kemsta/go-easyrsa/pkg/pair"
"io"
"io/ioutil"
"math/big"
"os"
"path/filepath"
Expand Down Expand Up @@ -69,7 +68,7 @@ func (h *FileCRLHolder) Get() (*pkix.CertificateList, error) {
if stat, err := os.Stat(h.path); err != nil || stat.Size() == 0 {
return &pkix.CertificateList{}, nil
}
fBytes, err := ioutil.ReadFile(h.path)
fBytes, err := os.ReadFile(h.path)
if err != nil {
return nil, fmt.Errorf("can`t read crl %v: %w", h.path, err)
}
Expand Down Expand Up @@ -101,7 +100,7 @@ func (p *FileSerialProvider) Next() (*big.Int, error) {
_ = p.locker.Unlock()
}()
res := big.NewInt(0)
sBytes, err := ioutil.ReadFile(p.path)
sBytes, err := os.ReadFile(p.path)
if os.IsNotExist(err) {
// nothing to do. New serial
} else if err != nil {
Expand Down Expand Up @@ -193,11 +192,11 @@ func (s *DirKeyStorage) GetByCN(cn string) ([]*pair.X509Pair, error) {
if err != nil {
return nil
}
certBytes, err := ioutil.ReadFile(path)
certBytes, err := os.ReadFile(path)
if err != nil {
return nil
}
keyBytes, err := ioutil.ReadFile(fmt.Sprintf("%s.key", path[0:len(path)-len(filepath.Ext(path))]))
keyBytes, err := os.ReadFile(fmt.Sprintf("%s.key", path[0:len(path)-len(filepath.Ext(path))]))
if err != nil {
return nil
}
Expand Down Expand Up @@ -238,11 +237,11 @@ func (s *DirKeyStorage) GetBySerial(serial *big.Int) (*pair.X509Pair, error) {
}
cn := filepath.Base(filepath.Dir(path))
if serial.Text(16) == big.NewInt(ser).Text(16) {
certBytes, err := ioutil.ReadFile(path)
certBytes, err := os.ReadFile(path)
if err != nil {
return nil
}
keyBytes, err := ioutil.ReadFile(fmt.Sprintf("%s.key", path[0:len(path)-len(filepath.Ext(path))]))
keyBytes, err := os.ReadFile(fmt.Sprintf("%s.key", path[0:len(path)-len(filepath.Ext(path))]))
if err != nil {
return nil
}
Expand Down Expand Up @@ -272,11 +271,11 @@ func (s *DirKeyStorage) GetAll() ([]*pair.X509Pair, error) {
return nil
}
cn := filepath.Base(filepath.Dir(path))
certBytes, err := ioutil.ReadFile(path)
certBytes, err := os.ReadFile(path)
if err != nil {
return nil
}
keyBytes, err := ioutil.ReadFile(fmt.Sprintf("%s.key", path[0:len(path)-len(filepath.Ext(path))]))
keyBytes, err := os.ReadFile(fmt.Sprintf("%s.key", path[0:len(path)-len(filepath.Ext(path))]))
if err != nil {
return nil
}
Expand Down Expand Up @@ -308,7 +307,7 @@ func writeFileAtomic(path string, r io.Reader, mode os.FileMode) error {
if dir == "" {
dir = "."
}
fd, err := ioutil.TempFile(dir, file)
fd, err := os.CreateTemp(dir, file)
if err != nil {
return fmt.Errorf("cannot create temp file: %w", err)
}
Expand Down
19 changes: 9 additions & 10 deletions internal/fsStorage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"github.com/kemsta/go-easyrsa/pkg/pair"
"io"
"io/ioutil"
"math/big"
"os"
"path/filepath"
Expand Down Expand Up @@ -210,11 +209,11 @@ func TestDirKeyStorage_Put(t *testing.T) {
}
})
}
certBytes, _ := ioutil.ReadFile(filepath.Join(getTestDir(), "dir_keystorage", "good_cert/42.crt"))
certBytes, _ := os.ReadFile(filepath.Join(getTestDir(), "dir_keystorage", "good_cert/42.crt"))
if !bytes.Equal(certBytes, []byte("certbytes")) {
t.Errorf("DirKeyStorage.Put() wrong cert bytes in result file")
}
keyBytes, _ := ioutil.ReadFile(filepath.Join(getTestDir(), "dir_keystorage", "good_cert/42.key"))
keyBytes, _ := os.ReadFile(filepath.Join(getTestDir(), "dir_keystorage", "good_cert/42.key"))
if !bytes.Equal(keyBytes, []byte("keybytes")) {
t.Errorf("DirKeyStorage.Put() wrong key bytes in result file")
}
Expand Down Expand Up @@ -379,8 +378,8 @@ func TestDirKeyStorage_GetBySerial(t *testing.T) {
func TestDirKeyStorage_DeleteBySerial(t *testing.T) {

_ = os.MkdirAll(filepath.Join(getTestDir(), "dir_keystorage", "for_delete"), 0755)
_ = ioutil.WriteFile(filepath.Join(getTestDir(), "dir_keystorage", "for_delete", "a.crt"), []byte(""), 0600)
_ = ioutil.WriteFile(filepath.Join(getTestDir(), "dir_keystorage", "for_delete", "a.key"), []byte(""), 0600)
_ = os.WriteFile(filepath.Join(getTestDir(), "dir_keystorage", "for_delete", "a.crt"), []byte(""), 0600)
_ = os.WriteFile(filepath.Join(getTestDir(), "dir_keystorage", "for_delete", "a.key"), []byte(""), 0600)

type fields struct {
keydir string
Expand Down Expand Up @@ -431,7 +430,7 @@ func TestDirKeyStorage_DeleteBySerial(t *testing.T) {
func TestFileSerialProvider_Next(t *testing.T) {
defer func() {
_ = os.RemoveAll(filepath.Join(getTestDir(), "dir_keystorage", "new_serial"))
_ = ioutil.WriteFile(filepath.Join(getTestDir(), "dir_keystorage", "wrong_serial"), []byte("gggg"), 0666)
_ = os.WriteFile(filepath.Join(getTestDir(), "dir_keystorage", "wrong_serial"), []byte("gggg"), 0666)
}()
type fields struct {
path string
Expand Down Expand Up @@ -502,7 +501,7 @@ func TestFileCRLHolder_Put(t *testing.T) {
if err != nil {
t.Errorf("FileCRLHolder.Put() error = %v", err)
}
got, _ := ioutil.ReadFile(fileName)
got, _ := os.ReadFile(fileName)
if !bytes.Equal(got, content) {
t.Errorf("FileCRLHolder.Put() got = %v, want %v", got, content)
}
Expand All @@ -511,14 +510,14 @@ func TestFileCRLHolder_Put(t *testing.T) {
fileName := filepath.Join(getTestDir(), "dir_keystorage", "exist.pem")
content := []byte("content")
defer func() {
_ = ioutil.WriteFile(fileName, []byte("asd"), 0644)
_ = os.WriteFile(fileName, []byte("asd"), 0644)
}()
h := NewFileCRLHolder(fileName)
err := h.Put(content)
if err != nil {
t.Errorf("FileCRLHolder.Put() error = %v", err)
}
got, _ := ioutil.ReadFile(fileName)
got, _ := os.ReadFile(fileName)
if !bytes.Equal(got, content) {
t.Errorf("FileCRLHolder.Put() got = %v, want %v", got, content)
}
Expand All @@ -527,7 +526,7 @@ func TestFileCRLHolder_Put(t *testing.T) {
fileName := filepath.Join(getTestDir(), "dir_keystorage", "crl.dir")
content := []byte("content")
defer func() {
_ = ioutil.WriteFile(fileName, []byte("asd"), 0666)
_ = os.WriteFile(fileName, []byte("asd"), 0666)
}()
h := NewFileCRLHolder(fileName)
err := h.Put(content)
Expand Down

0 comments on commit 0a61d52

Please sign in to comment.