From 3e6d9bb3323712a77f5bb38cf0d604578493dbd2 Mon Sep 17 00:00:00 2001 From: Tristan Morgan Date: Fri, 8 Sep 2023 09:08:11 +1000 Subject: [PATCH] Remove deprecated ioutil --- auth/auth_test.go | 4 ++-- auth/basic_test.go | 25 +++++++++-------------- cert/file_source.go | 4 ++-- cert/load.go | 6 +++--- cert/source_test.go | 23 +++++++-------------- cert/vault_client.go | 3 +-- config/load_test.go | 3 +-- logger/logger_test.go | 6 +++--- proxy/gzip/gzip_handler_test.go | 12 +++++------ proxy/http_integration_test.go | 4 ++-- proxy/inetaf_tcpproxy_integration_test.go | 4 ++-- proxy/tcp_integration_test.go | 21 ++++++------------- registry/file/backend.go | 6 +++--- 13 files changed, 48 insertions(+), 73 deletions(-) diff --git a/auth/auth_test.go b/auth/auth_test.go index 1f1e51d7c..8c5b68e85 100644 --- a/auth/auth_test.go +++ b/auth/auth_test.go @@ -42,12 +42,12 @@ func TestLoadAuthSchemes(t *testing.T) { }) t.Run("should load multiple auth schemes", func(t *testing.T) { - myauth, err := createBasicAuthFile("foo:bar") + myauth, err := createBasicAuthFile("foo:bar", t) if err != nil { t.Fatalf("could not create file on disk %s", err) } - myotherauth, err := createBasicAuthFile("bar:foo") + myotherauth, err := createBasicAuthFile("bar:foo", t) if err != nil { t.Fatalf("could not create file on disk %s", err) } diff --git a/auth/basic_test.go b/auth/basic_test.go index 396d20781..2b5e6bab7 100644 --- a/auth/basic_test.go +++ b/auth/basic_test.go @@ -3,7 +3,6 @@ package auth import ( "encoding/base64" "fmt" - "io/ioutil" "net/http" "os" "reflect" @@ -37,16 +36,12 @@ func (rw *responseWriter) WriteHeader(statusCode int) { rw.code = statusCode } -func createBasicAuthFile(contents string) (string, error) { - dir, err := ioutil.TempDir("", "basicauth") - - if err != nil { - return "", fmt.Errorf("could not create temp dir: %s", err) - } +func createBasicAuthFile(contents string, t *testing.T) (string, error) { + dir := t.TempDir() filename := fmt.Sprintf("%s/%s", dir, uuid.NewUUID()) - err = ioutil.WriteFile(filename, []byte(contents), 0666) + err := os.WriteFile(filename, []byte(contents), 0666) if err != nil { return "", fmt.Errorf("could not write password file: %s", err) @@ -55,10 +50,10 @@ func createBasicAuthFile(contents string) (string, error) { return filename, nil } -func createBasicAuth(user string, password string) (AuthScheme, error) { +func createBasicAuth(user string, password string, t *testing.T) (AuthScheme, error) { contents := fmt.Sprintf("%s:%s", user, password) - filename, err := createBasicAuthFile(contents) + filename, err := createBasicAuthFile(contents, t) a, err := newBasicAuth(config.BasicAuth{ File: filename, @@ -75,7 +70,7 @@ func createBasicAuth(user string, password string) (AuthScheme, error) { func TestNewBasicAuth(t *testing.T) { t.Run("should create a basic auth scheme from the supplied config", func(t *testing.T) { - filename, err := createBasicAuthFile("foo:bar") + filename, err := createBasicAuthFile("foo:bar", t) if err != nil { t.Error(err) @@ -91,7 +86,7 @@ func TestNewBasicAuth(t *testing.T) { }) t.Run("should log a warning when credentials are malformed", func(t *testing.T) { - filename, err := createBasicAuthFile("foosdlijdgohdgdbar") + filename, err := createBasicAuthFile("foosdlijdgohdgdbar", t) if err != nil { t.Error(err) @@ -108,7 +103,7 @@ func TestNewBasicAuth(t *testing.T) { } func TestBasic_Authorised(t *testing.T) { - basicAuth, err := createBasicAuth("foo", "bar") + basicAuth, err := createBasicAuth("foo", "bar", t) creds := []byte("foo:bar") if err != nil { @@ -171,7 +166,7 @@ func TestBasic_Authorised(t *testing.T) { } func TestBasic_Authorised_should_fail_without_htpasswd_file(t *testing.T) { - filename, err := createBasicAuthFile("foo:bar") + filename, err := createBasicAuthFile("foo:bar", t) if err != nil { t.Error(err) } @@ -213,7 +208,7 @@ func TestBasic_Authorised_should_fail_without_htpasswd_file(t *testing.T) { } func TestBasic_Authorized_should_set_www_realm_header(t *testing.T) { - basicAuth, err := createBasicAuth("foo", "bar") + basicAuth, err := createBasicAuth("foo", "bar", t) if err != nil { t.Fatal(err) diff --git a/cert/file_source.go b/cert/file_source.go index 9880b5a0a..fd4c96b3b 100644 --- a/cert/file_source.go +++ b/cert/file_source.go @@ -3,7 +3,7 @@ package cert import ( "crypto/tls" "crypto/x509" - "io/ioutil" + "os" "github.com/fabiolb/fabio/exit" ) @@ -26,7 +26,7 @@ func (s FileSource) LoadClientCAs() (*x509.CertPool, error) { if s.ClientAuthFile == "" { return nil, nil } - pemBlock, err := ioutil.ReadFile(path) + pemBlock, err := os.ReadFile(path) return map[string][]byte{path: pemBlock}, err }) } diff --git a/cert/load.go b/cert/load.go index 9b0951831..9f3013fad 100644 --- a/cert/load.go +++ b/cert/load.go @@ -5,7 +5,7 @@ import ( "crypto/x509" "encoding/pem" "fmt" - "io/ioutil" + "io" "log" "net/http" "net/url" @@ -34,7 +34,7 @@ func loadURL(listURL string) (pemBlocks map[string][]byte, err error) { return nil, err } defer resp.Body.Close() - return ioutil.ReadAll(resp.Body) + return io.ReadAll(resp.Body) } // fetch the file with the list of filenames @@ -88,7 +88,7 @@ func loadPath(root string) (pemBlocks map[string][]byte, err error) { return nil } - buf, err := ioutil.ReadFile(path) + buf, err := os.ReadFile(path) if err != nil { return fmt.Errorf("cert: %s", err) } diff --git a/cert/source_test.go b/cert/source_test.go index 32c27442d..122811804 100644 --- a/cert/source_test.go +++ b/cert/source_test.go @@ -10,7 +10,6 @@ import ( "encoding/pem" "fmt" "io" - "io/ioutil" "log" "math/big" "net/http" @@ -193,7 +192,7 @@ func TestStaticSource(t *testing.T) { } func TestFileSource(t *testing.T) { - dir := tempDir() + dir := t.TempDir() defer os.RemoveAll(dir) certPEM, keyPEM := makePEM("localhost", time.Minute) certFile, keyFile := saveCert(dir, "localhost", certPEM, keyPEM) @@ -201,7 +200,7 @@ func TestFileSource(t *testing.T) { } func TestPathSource(t *testing.T) { - dir := tempDir() + dir := t.TempDir() defer os.RemoveAll(dir) certPEM, keyPEM := makePEM("localhost", time.Minute) saveCert(dir, "localhost", certPEM, keyPEM) @@ -209,7 +208,7 @@ func TestPathSource(t *testing.T) { } func TestHTTPSource(t *testing.T) { - dir := tempDir() + dir := t.TempDir() defer os.RemoveAll(dir) certPEM, keyPEM := makePEM("localhost", time.Minute) certFile, keyFile := saveCert(dir, "localhost", certPEM, keyPEM) @@ -261,7 +260,7 @@ func TestConsulSource(t *testing.T) { return false } - n, err := io.Copy(ioutil.Discard, resp.Body) + n, err := io.Copy(io.Discard, resp.Body) return err == nil && n > 10 } @@ -582,7 +581,7 @@ func testSource(t *testing.T, source Source, rootCAs *x509.CertPool, sleep time. // disable log output for the next call to prevent // confusing log messages since they are expected // http: TLS handshake error from 127.0.0.1:55044: remote error: bad certificate - log.SetOutput(ioutil.Discard) + log.SetOutput(io.Discard) defer log.SetOutput(os.Stderr) // fail calls https://localhost.org/ for which certificate validation @@ -642,7 +641,7 @@ func roundtrip(serverName string, srvConfig *tls.Config, client *http.Client) (c } defer resp.Body.Close() - data, err := ioutil.ReadAll(resp.Body) + data, err := io.ReadAll(resp.Body) if err != nil { return 0, "", err } @@ -675,16 +674,8 @@ func http20Client(rootCAs *x509.CertPool) (*http.Client, error) { return &http.Client{Transport: t}, nil } -func tempDir() string { - dir, err := ioutil.TempDir("", "fabio") - if err != nil { - panic(err.Error()) - } - return dir -} - func writeFile(filename string, data []byte) { - if err := ioutil.WriteFile(filename, data, 0644); err != nil { + if err := os.WriteFile(filename, data, 0644); err != nil { panic(err.Error()) } } diff --git a/cert/vault_client.go b/cert/vault_client.go index 55836e685..c1d37d004 100644 --- a/cert/vault_client.go +++ b/cert/vault_client.go @@ -3,7 +3,6 @@ package cert import ( "encoding/json" "errors" - "io/ioutil" "log" "os" "strings" @@ -186,7 +185,7 @@ func getVaultToken(c string) string { return token } if cArray[0] == "file" { - b, err := ioutil.ReadFile(cArray[1]) // just pass the file name + b, err := os.ReadFile(cArray[1]) // just pass the file name if err != nil { log.Printf("[WARN] vault: Failed to fetch token from %s", c) } else { diff --git a/config/load_test.go b/config/load_test.go index 747d74215..99edbc0be 100644 --- a/config/load_test.go +++ b/config/load_test.go @@ -4,7 +4,6 @@ import ( "crypto/tls" "errors" "fmt" - "io/ioutil" "net/http" "net/http/httptest" "os" @@ -1183,7 +1182,7 @@ func TestLoad(t *testing.T) { } case tt.path != "": - if err := ioutil.WriteFile(tt.path, []byte(tt.data), 0600); err != nil { + if err := os.WriteFile(tt.path, []byte(tt.data), 0600); err != nil { t.Fatalf("error writing file: %s", err) } defer os.Remove(tt.path) diff --git a/logger/logger_test.go b/logger/logger_test.go index 2d310d8a6..ea7105cf7 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -2,7 +2,7 @@ package logger import ( "bytes" - "io/ioutil" + "io" "net/http" "net/url" "sort" @@ -221,7 +221,7 @@ func BenchmarkLog(b *testing.B) { sort.Strings(keys) format := strings.Join(keys, " ") - l, err := New(ioutil.Discard, format) + l, err := New(io.Discard, format) if err != nil { b.Fatal(err) } @@ -243,7 +243,7 @@ func BenchmarkLog(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - t.Execute(ioutil.Discard, e) + t.Execute(io.Discard, e) } }) } diff --git a/proxy/gzip/gzip_handler_test.go b/proxy/gzip/gzip_handler_test.go index 98dabba23..d89e49097 100644 --- a/proxy/gzip/gzip_handler_test.go +++ b/proxy/gzip/gzip_handler_test.go @@ -4,7 +4,7 @@ package gzip import ( "bytes" "compress/gzip" - "io/ioutil" + "io" "net/http" "net/http/httptest" "regexp" @@ -31,7 +31,7 @@ func Test_GzipHandler_CompressableType(t *testing.T) { assertEqual(resp.Header.Get("Content-Type"), "text/plain; charset=utf-8") assertEqual(resp.Header.Get("Content-Encoding"), "gzip") - gzBytes, err := ioutil.ReadAll(resp.Body) + gzBytes, err := io.ReadAll(resp.Body) assertEqual(err, nil) assertEqual(resp.Header.Get("Content-Length"), strconv.Itoa(len(gzBytes))) @@ -39,7 +39,7 @@ func Test_GzipHandler_CompressableType(t *testing.T) { assertEqual(err, nil) defer reader.Close() - bytes, err := ioutil.ReadAll(reader) + bytes, err := io.ReadAll(reader) assertEqual(err, nil) assertEqual(string(bytes), "Hello World") @@ -63,7 +63,7 @@ func Test_GzipHandler_NotCompressingTwice(t *testing.T) { assertEqual(err, nil) defer reader.Close() - bytes, err := ioutil.ReadAll(reader) + bytes, err := io.ReadAll(reader) assertEqual(err, nil) assertEqual(string(bytes), "Hello World") @@ -83,7 +83,7 @@ func Test_GzipHandler_CompressableType_NoAccept(t *testing.T) { assertEqual(resp.Header.Get("Content-Encoding"), "") - bytes, err := ioutil.ReadAll(resp.Body) + bytes, err := io.ReadAll(resp.Body) assertEqual(err, nil) assertEqual(string(bytes), "Hello World") @@ -103,7 +103,7 @@ func Test_GzipHandler_NonCompressableType(t *testing.T) { assertEqual(resp.Header.Get("Content-Encoding"), "") - bytes, err := ioutil.ReadAll(resp.Body) + bytes, err := io.ReadAll(resp.Body) assertEqual(err, nil) assertEqual(bytes, []byte{42}) diff --git a/proxy/http_integration_test.go b/proxy/http_integration_test.go index 45c88e42e..75b769f03 100644 --- a/proxy/http_integration_test.go +++ b/proxy/http_integration_test.go @@ -6,7 +6,7 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" + "io" "net" "net/http" "net/http/httptest" @@ -750,7 +750,7 @@ func mustDo(req *http.Request) (*http.Response, []byte) { panic(err) } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { panic(err) } diff --git a/proxy/inetaf_tcpproxy_integration_test.go b/proxy/inetaf_tcpproxy_integration_test.go index 0445c4311..5f4e5637d 100644 --- a/proxy/inetaf_tcpproxy_integration_test.go +++ b/proxy/inetaf_tcpproxy_integration_test.go @@ -5,7 +5,7 @@ import ( "context" "crypto/x509" "fmt" - "io/ioutil" + "io" "net" "net/http" "net/http/httptest" @@ -146,7 +146,7 @@ route add tcproute example2.com/ tcp://%s opts "proto=tcp"` t.Errorf("error on request %s", err) return } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) resp.Body.Close() if err != nil { t.Errorf("error reading body: %s", err) diff --git a/proxy/tcp_integration_test.go b/proxy/tcp_integration_test.go index d9295c081..ae02bc3ec 100644 --- a/proxy/tcp_integration_test.go +++ b/proxy/tcp_integration_test.go @@ -5,7 +5,6 @@ import ( "bytes" "crypto/tls" "crypto/x509" - "io/ioutil" "net" "net/url" "os" @@ -104,16 +103,12 @@ func TestTCPProxyWithTLS(t *testing.T) { defer srv.Close() // setup cert source - dir, err := ioutil.TempDir("", "fabio") - if err != nil { - t.Fatal("ioutil.TempDir", err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() mustWrite := func(name string, data []byte) { path := filepath.Join(dir, name) - if err := ioutil.WriteFile(path, data, 0644); err != nil { - t.Fatalf("ioutil.WriteFile: %s", err) + if err := os.WriteFile(path, data, 0644); err != nil { + t.Fatalf("os.WriteFile: %s", err) } } mustWrite("example.com-key.pem", internal.LocalhostKey) @@ -288,16 +283,12 @@ func TestTCPProxyWithTLSWithProxyProto(t *testing.T) { defer srv.Close() // setup cert source - dir, err := ioutil.TempDir("", "fabio") - if err != nil { - t.Fatal("ioutil.TempDir", err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() mustWrite := func(name string, data []byte) { path := filepath.Join(dir, name) - if err := ioutil.WriteFile(path, data, 0644); err != nil { - t.Fatalf("ioutil.WriteFile: %s", err) + if err := os.WriteFile(path, data, 0644); err != nil { + t.Fatalf("os.WriteFile: %s", err) } } mustWrite("example.com-key.pem", internal.LocalhostKey) diff --git a/registry/file/backend.go b/registry/file/backend.go index 9c81d4312..e584f4b89 100644 --- a/registry/file/backend.go +++ b/registry/file/backend.go @@ -3,8 +3,8 @@ package file import ( - "io/ioutil" "log" + "os" "github.com/fabiolb/fabio/config" "github.com/fabiolb/fabio/registry" @@ -12,12 +12,12 @@ import ( ) func NewBackend(cfg *config.File) (registry.Backend, error) { - routes, err := ioutil.ReadFile(cfg.RoutesPath) + routes, err := os.ReadFile(cfg.RoutesPath) if err != nil { log.Println("[ERROR] Cannot read routes from ", cfg.RoutesPath) return nil, err } - noroutehtml, err := ioutil.ReadFile(cfg.NoRouteHTMLPath) + noroutehtml, err := os.ReadFile(cfg.NoRouteHTMLPath) if err != nil { log.Println("[ERROR] Cannot read no route HTML from ", cfg.NoRouteHTMLPath) return nil, err