Skip to content

Commit

Permalink
Remove uses of ioutil
Browse files Browse the repository at this point in the history
  • Loading branch information
jaksi committed Aug 3, 2024
1 parent ffc0785 commit 1b314b4
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 23 deletions.
5 changes: 2 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
Expand Down Expand Up @@ -148,14 +147,14 @@ func generateKey(dataDir string, signature keySignature) (string, error) {
if err != nil {
return "", err
}
if err := ioutil.WriteFile(keyFile, pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: keyBytes}), 0600); err != nil {
if err := os.WriteFile(keyFile, pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: keyBytes}), 0600); err != nil {
return "", err
}
return keyFile, nil
}

func loadKey(keyFile string) (ssh.Signer, error) {
keyBytes, err := ioutil.ReadFile(keyFile)
keyBytes, err := os.ReadFile(keyFile)
if err != nil {
return nil, err
}
Expand Down
20 changes: 10 additions & 10 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"reflect"
"testing"
Expand Down Expand Up @@ -114,7 +114,7 @@ func verifyConfig(t *testing.T, cfg *config, expected *config) {
}

func verifyDefaultKeys(t *testing.T, dataDir string) {
files, err := ioutil.ReadDir(dataDir)
files, err := os.ReadDir(dataDir)
if err != nil {
t.Fatalf("Faield to list directory: %v", err)
}
Expand All @@ -125,7 +125,7 @@ func verifyDefaultKeys(t *testing.T, dataDir string) {
}
keys := map[string]string{}
for _, file := range files {
keyBytes, err := ioutil.ReadFile(path.Join(dataDir, file.Name()))
keyBytes, err := os.ReadFile(path.Join(dataDir, file.Name()))
if err != nil {
t.Fatalf("Failed to read key: %v", err)
}
Expand Down Expand Up @@ -281,7 +281,7 @@ server:
expectedConfig.SSHProto.Version = "SSH-2.0-sshesame"
expectedConfig.SSHProto.Banner = "This is an SSH honeypot. Everything is logged and monitored."
verifyConfig(t, cfg, expectedConfig)
files, err := ioutil.ReadDir(dataDir)
files, err := os.ReadDir(dataDir)
if err != nil {
t.Fatalf("Failed to read directory: %v", err)
}
Expand Down Expand Up @@ -316,7 +316,7 @@ func TestLogReloadSameFile(t *testing.T) {
if err := cfg.logFileHandle.Close(); err != nil {
t.Fatalf("Failed to close log file: %v", err)
}
logs, err := ioutil.ReadFile(cfg.Logging.File)
logs, err := os.ReadFile(cfg.Logging.File)
if err != nil {
t.Fatalf("Failed to read log file: %v", err)
}
Expand Down Expand Up @@ -344,15 +344,15 @@ func TestLogReloadDifferentFile(t *testing.T) {
if err := cfg.logFileHandle.Close(); err != nil {
t.Fatalf("Failed to close log file: %v", err)
}
logs1, err := ioutil.ReadFile(logFile1)
logs1, err := os.ReadFile(logFile1)
if err != nil {
t.Fatalf("Failed to read log file: %v", err)
}
expectedLogs1 := "test1\n"
if string(logs1) != expectedLogs1 {
t.Errorf("logs1=%v, want %v", string(logs1), expectedLogs1)
}
logs2, err := ioutil.ReadFile(logFile2)
logs2, err := os.ReadFile(logFile2)
if err != nil {
t.Fatalf("Failed to read log file: %v", err)
}
Expand All @@ -368,15 +368,15 @@ func TestExistingKey(t *testing.T) {
if err != nil {
t.Fatalf("Failed to generate key: %v", err)
}
oldKey, err := ioutil.ReadFile(oldKeyFile)
oldKey, err := os.ReadFile(oldKeyFile)
if err != nil {
t.Fatalf("Failed to read key: %v", err)
}
newKeyFile, err := generateKey(dataDir, ed25519_key)
if err != nil {
t.Fatalf("Failed to generate key: %v", err)
}
newKey, err := ioutil.ReadFile(newKeyFile)
newKey, err := os.ReadFile(newKeyFile)
if err != nil {
t.Fatalf("Failed to read key: %v", err)
}
Expand All @@ -386,7 +386,7 @@ func TestExistingKey(t *testing.T) {
}

func TestDefaultConfigFile(t *testing.T) {
configBytes, err := ioutil.ReadFile("sshesame.yaml")
configBytes, err := os.ReadFile("sshesame.yaml")
if err != nil {
t.Fatalf("Failed to read config file: %v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"flag"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -35,7 +34,7 @@ func main() {
cfg := &config{}
configString := ""
if *configFile != "" {
configBytes, err := ioutil.ReadFile(*configFile)
configBytes, err := os.ReadFile(*configFile)
if err != nil {
errorLogger.Fatalf("Failed to read config file: %v", err)
}
Expand All @@ -50,7 +49,7 @@ func main() {
go func() {
for signal := range reloadSignals {
infoLogger.Printf("Reloading config due to %s", signal)
configBytes, err := ioutil.ReadFile(*configFile)
configBytes, err := os.ReadFile(*configFile)
if err != nil {
warningLogger.Printf("Failed to read config file: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -520,7 +520,7 @@ func TestReplay(t *testing.T) {
}
t.Run(testName, func(t *testing.T) {
logBuffer := setupLogBuffer(t, cfg)
testCaseBytes, err := ioutil.ReadFile(testFile)
testCaseBytes, err := os.ReadFile(testFile)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions testproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"os"

"golang.org/x/crypto/ssh"
)
Expand Down Expand Up @@ -479,7 +479,7 @@ func main() {
NoClientAuth: true,
ServerVersion: "SSH-2.0-OpenSSH_7.2",
}
hostKeyBytes, err := ioutil.ReadFile(*hostKeyFile)
hostKeyBytes, err := os.ReadFile(*hostKeyFile)
if err != nil {
panic(err)
}
Expand All @@ -489,7 +489,7 @@ func main() {
}
serverConfig.AddHostKey(hostKey)

clientKeyBytes, err := ioutil.ReadFile(*clientKeyFile)
clientKeyBytes, err := os.ReadFile(*clientKeyFile)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main

import (
"bytes"
"io/ioutil"
"log"
"os"
"path/filepath"
"testing"
)
Expand Down Expand Up @@ -75,7 +75,7 @@ func writeTestKeys(t *testing.T, dataDir string) {
"host_ecdsa_key": testECDSAKey,
"host_ed25519_key": testEd25519Key,
} {
if err := ioutil.WriteFile(filepath.Join(dataDir, fileName), []byte(content), 0600); err != nil {
if err := os.WriteFile(filepath.Join(dataDir, fileName), []byte(content), 0600); err != nil {
t.Fatal(err)
}
}
Expand Down

0 comments on commit 1b314b4

Please sign in to comment.