From b500e556bfd15267e00f32c46286b318b86e90b2 Mon Sep 17 00:00:00 2001 From: Devin Carr Date: Fri, 14 Jul 2023 18:42:48 -0700 Subject: [PATCH] TUN-7590: Remove usages of ioutil --- cfapi/tunnel_test.go | 4 ++-- cmd/cloudflared/service_template.go | 5 ++--- cmd/cloudflared/tunnel/cmd.go | 3 +-- cmd/cloudflared/tunnel/filesystem.go | 3 +-- cmd/cloudflared/tunnel/login.go | 3 +-- cmd/cloudflared/tunnel/subcommands.go | 3 +-- cmd/cloudflared/updater/workers_service_test.go | 11 +++++------ connection/http2_test.go | 7 +++---- h2mux/h2mux_test.go | 5 ++--- hello/hello.go | 4 ++-- ingress/origin_connection_test.go | 5 ++--- ingress/origin_proxy_test.go | 6 +++--- orchestration/orchestrator_test.go | 3 +-- proxy/proxy_posix_test.go | 3 +-- socks/connection_handler_test.go | 4 ++-- sshgen/sshgen.go | 6 +++--- sshgen/sshgen_test.go | 3 +-- supervisor/cloudflare_status_page.go | 4 ++-- tlsconfig/certreloader.go | 6 +++--- tlsconfig/tlsconfig.go | 4 ++-- token/token.go | 11 +++++------ tunneldns/https_upstream.go | 4 ++-- validation/validation_test.go | 4 ++-- 23 files changed, 49 insertions(+), 62 deletions(-) diff --git a/cfapi/tunnel_test.go b/cfapi/tunnel_test.go index fbb5fba8..c61bdc45 100644 --- a/cfapi/tunnel_test.go +++ b/cfapi/tunnel_test.go @@ -3,7 +3,7 @@ package cfapi import ( "bytes" "fmt" - "io/ioutil" + "io" "net" "reflect" "strings" @@ -49,7 +49,7 @@ func Test_parseListTunnels(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - body := ioutil.NopCloser(bytes.NewReader([]byte(tt.args.body))) + body := io.NopCloser(bytes.NewReader([]byte(tt.args.body))) got, err := parseListTunnels(body) if (err != nil) != tt.wantErr { t.Errorf("parseListTunnels() error = %v, wantErr %v", err, tt.wantErr) diff --git a/cmd/cloudflared/service_template.go b/cmd/cloudflared/service_template.go index feaf86cc..c2f0d3cf 100644 --- a/cmd/cloudflared/service_template.go +++ b/cmd/cloudflared/service_template.go @@ -5,7 +5,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" "os/exec" "path" @@ -64,7 +63,7 @@ func (st *ServiceTemplate) Generate(args *ServiceTemplateArgs) error { return fmt.Errorf("error creating %s: %v", plistFolder, err) } - err = ioutil.WriteFile(resolvedPath, buffer.Bytes(), fileMode) + err = os.WriteFile(resolvedPath, buffer.Bytes(), fileMode) if err != nil { return fmt.Errorf("error writing %s: %v", resolvedPath, err) } @@ -103,7 +102,7 @@ func runCommand(command string, args ...string) error { return fmt.Errorf("error starting %s: %v", command, err) } - output, _ := ioutil.ReadAll(stderr) + output, _ := io.ReadAll(stderr) err = cmd.Wait() if err != nil { return fmt.Errorf("%s %v returned with error code %v due to: %v", command, args, err, string(output)) diff --git a/cmd/cloudflared/tunnel/cmd.go b/cmd/cloudflared/tunnel/cmd.go index 2d092b0c..c6dff0a6 100644 --- a/cmd/cloudflared/tunnel/cmd.go +++ b/cmd/cloudflared/tunnel/cmd.go @@ -4,7 +4,6 @@ import ( "bufio" "context" "fmt" - "io/ioutil" "net/url" "os" "runtime/trace" @@ -305,7 +304,7 @@ func StartServer( } if c.IsSet("trace-output") { - tmpTraceFile, err := ioutil.TempFile("", "trace") + tmpTraceFile, err := os.CreateTemp("", "trace") if err != nil { log.Err(err).Msg("Failed to create new temporary file to save trace output") } diff --git a/cmd/cloudflared/tunnel/filesystem.go b/cmd/cloudflared/tunnel/filesystem.go index 56f2ff2e..ec2826ba 100644 --- a/cmd/cloudflared/tunnel/filesystem.go +++ b/cmd/cloudflared/tunnel/filesystem.go @@ -1,7 +1,6 @@ package tunnel import ( - "io/ioutil" "os" ) @@ -23,5 +22,5 @@ func (fs realFileSystem) validFilePath(path string) bool { } func (fs realFileSystem) readFile(filePath string) ([]byte, error) { - return ioutil.ReadFile(filePath) + return os.ReadFile(filePath) } diff --git a/cmd/cloudflared/tunnel/login.go b/cmd/cloudflared/tunnel/login.go index c7d2881e..fd9bc01f 100644 --- a/cmd/cloudflared/tunnel/login.go +++ b/cmd/cloudflared/tunnel/login.go @@ -2,7 +2,6 @@ package tunnel import ( "fmt" - "io/ioutil" "net/url" "os" "path/filepath" @@ -66,7 +65,7 @@ func login(c *cli.Context) error { return err } - if err := ioutil.WriteFile(path, resourceData, 0600); err != nil { + if err := os.WriteFile(path, resourceData, 0600); err != nil { return errors.Wrap(err, fmt.Sprintf("error writing cert to %s", path)) } diff --git a/cmd/cloudflared/tunnel/subcommands.go b/cmd/cloudflared/tunnel/subcommands.go index 04f0f0d8..8da12c19 100644 --- a/cmd/cloudflared/tunnel/subcommands.go +++ b/cmd/cloudflared/tunnel/subcommands.go @@ -5,7 +5,6 @@ import ( "encoding/base64" "encoding/json" "fmt" - "io/ioutil" "os" "path/filepath" "regexp" @@ -241,7 +240,7 @@ func writeTunnelCredentials(filePath string, credentials *connection.Credentials if err != nil { return errors.Wrap(err, "Unable to marshal tunnel credentials to JSON") } - return ioutil.WriteFile(filePath, body, 400) + return os.WriteFile(filePath, body, 0400) } func buildListCommand() *cli.Command { diff --git a/cmd/cloudflared/updater/workers_service_test.go b/cmd/cloudflared/updater/workers_service_test.go index 72a51c9c..d5f8e456 100644 --- a/cmd/cloudflared/updater/workers_service_test.go +++ b/cmd/cloudflared/updater/workers_service_test.go @@ -11,7 +11,6 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "log" "net/http" "net/http/httptest" @@ -224,7 +223,7 @@ func TestUpdateService(t *testing.T) { require.Equal(t, v.Version(), mostRecentVersion) require.NoError(t, v.Apply()) - dat, err := ioutil.ReadFile(testFilePath) + dat, err := os.ReadFile(testFilePath) require.NoError(t, err) require.Equal(t, string(dat), mostRecentVersion) @@ -243,7 +242,7 @@ func TestBetaUpdateService(t *testing.T) { require.Equal(t, v.Version(), mostRecentBetaVersion) require.NoError(t, v.Apply()) - dat, err := ioutil.ReadFile(testFilePath) + dat, err := os.ReadFile(testFilePath) require.NoError(t, err) require.Equal(t, string(dat), mostRecentBetaVersion) @@ -289,7 +288,7 @@ func TestForcedUpdateService(t *testing.T) { require.Equal(t, v.Version(), mostRecentVersion) require.NoError(t, v.Apply()) - dat, err := ioutil.ReadFile(testFilePath) + dat, err := os.ReadFile(testFilePath) require.NoError(t, err) require.Equal(t, string(dat), mostRecentVersion) @@ -309,7 +308,7 @@ func TestUpdateSpecificVersionService(t *testing.T) { require.Equal(t, reqVersion, v.Version()) require.NoError(t, v.Apply()) - dat, err := ioutil.ReadFile(testFilePath) + dat, err := os.ReadFile(testFilePath) require.NoError(t, err) require.Equal(t, reqVersion, string(dat)) @@ -328,7 +327,7 @@ func TestCompressedUpdateService(t *testing.T) { require.Equal(t, "2020.09.02", v.Version()) require.NoError(t, v.Apply()) - dat, err := ioutil.ReadFile(testFilePath) + dat, err := os.ReadFile(testFilePath) require.NoError(t, err) require.Equal(t, "2020.09.02", string(dat)) diff --git a/connection/http2_test.go b/connection/http2_test.go index b7f1c49c..1cb39646 100644 --- a/connection/http2_test.go +++ b/connection/http2_test.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net" "net/http" "net/http/httptest" @@ -84,7 +83,7 @@ func TestHTTP2ConfigurationSet(t *testing.T) { resp, err := edgeHTTP2Conn.RoundTrip(req) require.NoError(t, err) require.Equal(t, http.StatusOK, resp.StatusCode) - bdy, err := ioutil.ReadAll(resp.Body) + bdy, err := io.ReadAll(resp.Body) require.NoError(t, err) assert.Equal(t, `{"lastAppliedVersion":2,"err":null}`, string(bdy)) cancel() @@ -149,7 +148,7 @@ func TestServeHTTP(t *testing.T) { require.NoError(t, err) require.Equal(t, test.expectedStatus, resp.StatusCode) if test.expectedBody != nil { - respBody, err := ioutil.ReadAll(resp.Body) + respBody, err := io.ReadAll(resp.Body) require.NoError(t, err) require.Equal(t, test.expectedBody, respBody) } @@ -546,7 +545,7 @@ func benchmarkServeHTTP(b *testing.B, test testRequest) { require.NoError(b, err) require.Equal(b, test.expectedStatus, resp.StatusCode) if test.expectedBody != nil { - respBody, err := ioutil.ReadAll(resp.Body) + respBody, err := io.ReadAll(resp.Body) require.NoError(b, err) require.Equal(b, test.expectedBody, respBody) } diff --git a/h2mux/h2mux_test.go b/h2mux/h2mux_test.go index a28abb59..71687fbe 100644 --- a/h2mux/h2mux_test.go +++ b/h2mux/h2mux_test.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "math/rand" "net" "os" @@ -652,7 +651,7 @@ func TestHPACK(t *testing.T) { if stream.Headers[0].Value != "200" { t.Fatalf("expected status 200, got %s", stream.Headers[0].Value) } - _, _ = ioutil.ReadAll(stream) + _, _ = io.ReadAll(stream) _ = stream.Close() } } @@ -905,7 +904,7 @@ func loadSampleFiles(paths []string) (map[string][]byte, error) { files := make(map[string][]byte) for _, path := range paths { if _, ok := files[path]; !ok { - expectBody, err := ioutil.ReadFile(path) + expectBody, err := os.ReadFile(path) if err != nil { return nil, err } diff --git a/hello/hello.go b/hello/hello.go index 785b341a..f8dc1e71 100644 --- a/hello/hello.go +++ b/hello/hello.go @@ -6,7 +6,7 @@ import ( "encoding/json" "fmt" "html/template" - "io/ioutil" + "io" "net" "net/http" "os" @@ -234,7 +234,7 @@ func rootHandler(serverName string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var buffer bytes.Buffer var body string - rawBody, err := ioutil.ReadAll(r.Body) + rawBody, err := io.ReadAll(r.Body) if err == nil { body = string(rawBody) } else { diff --git a/ingress/origin_connection_test.go b/ingress/origin_connection_test.go index ae8a757e..e47d38ce 100644 --- a/ingress/origin_connection_test.go +++ b/ingress/origin_connection_test.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "net" "net/http" "net/http/httptest" @@ -118,7 +117,7 @@ func TestSocksStreamWSOverTCPConnection(t *testing.T) { } for _, status := range statusCodes { handler := func(w http.ResponseWriter, r *http.Request) { - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) require.NoError(t, err) require.Equal(t, []byte(sendMessage), body) @@ -180,7 +179,7 @@ func TestSocksStreamWSOverTCPConnection(t *testing.T) { assert.NoError(t, err) assert.Equal(t, status, resp.StatusCode) require.Equal(t, echoHeaderReturnValue, resp.Header.Get(echoHeaderName)) - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) require.NoError(t, err) require.Equal(t, []byte(echoMessage), body) diff --git a/ingress/origin_proxy_test.go b/ingress/origin_proxy_test.go index 427b2a65..fb84c837 100644 --- a/ingress/origin_proxy_test.go +++ b/ingress/origin_proxy_test.go @@ -3,7 +3,7 @@ package ingress import ( "context" "fmt" - "io/ioutil" + "io" "net" "net/http" "net/http/httptest" @@ -141,7 +141,7 @@ func TestHTTPServiceHostHeaderOverride(t *testing.T) { require.NoError(t, err) require.Equal(t, http.StatusOK, resp.StatusCode) - respBody, err := ioutil.ReadAll(resp.Body) + respBody, err := io.ReadAll(resp.Body) require.NoError(t, err) require.Equal(t, respBody, []byte(originURL.Host)) } @@ -180,7 +180,7 @@ func TestHTTPServiceUsesIngressRuleScheme(t *testing.T) { require.NoError(t, err) require.Equal(t, http.StatusOK, resp.StatusCode) - respBody, err := ioutil.ReadAll(resp.Body) + respBody, err := io.ReadAll(resp.Body) require.NoError(t, err) require.Equal(t, respBody, []byte(p)) } diff --git a/orchestration/orchestrator_test.go b/orchestration/orchestrator_test.go index 334d349c..01addb8e 100644 --- a/orchestration/orchestrator_test.go +++ b/orchestration/orchestrator_test.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net" "net/http" "net/http/httptest" @@ -338,7 +337,7 @@ func TestConcurrentUpdateAndRead(t *testing.T) { switch resp.StatusCode { // v1 proxy, warp enabled case 200: - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) require.NoError(t, err) require.Equal(t, t.Name(), string(body)) warpRoutingDisabled = false diff --git a/proxy/proxy_posix_test.go b/proxy/proxy_posix_test.go index 40d070c7..6893995d 100644 --- a/proxy/proxy_posix_test.go +++ b/proxy/proxy_posix_test.go @@ -4,7 +4,6 @@ package proxy import ( - "io/ioutil" "net" "net/http" "net/http/httptest" @@ -17,7 +16,7 @@ import ( ) func TestUnixSocketOrigin(t *testing.T) { - file, err := ioutil.TempFile("", "unix.sock") + file, err := os.CreateTemp("", "unix.sock") require.NoError(t, err) os.Remove(file.Name()) // remove the file since binding the socket expects to create it diff --git a/socks/connection_handler_test.go b/socks/connection_handler_test.go index 424d8185..17916a04 100644 --- a/socks/connection_handler_test.go +++ b/socks/connection_handler_test.go @@ -2,7 +2,7 @@ package socks import ( "encoding/json" - "io/ioutil" + "io" "net" "net/http" "testing" @@ -32,7 +32,7 @@ func sendSocksRequest(t *testing.T) []byte { assert.NoError(t, err) defer resp.Body.Close() - b, err := ioutil.ReadAll(resp.Body) + b, err := io.ReadAll(resp.Body) assert.NoError(t, err) return b diff --git a/sshgen/sshgen.go b/sshgen/sshgen.go index 9a69fa79..21782720 100644 --- a/sshgen/sshgen.go +++ b/sshgen/sshgen.go @@ -10,9 +10,9 @@ import ( "encoding/pem" "fmt" "io" - "io/ioutil" "net/http" "net/url" + "os" "time" "github.com/go-jose/go-jose/v3/jwt" @@ -148,7 +148,7 @@ func generateKeyPair(fullName string) ([]byte, error) { return nil, err } if exist { - return ioutil.ReadFile(pubKeyName) + return os.ReadFile(pubKeyName) } key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) @@ -187,5 +187,5 @@ func writeKey(filename string, data []byte) error { return err } - return ioutil.WriteFile(filepath, data, 0600) + return os.WriteFile(filepath, data, 0600) } diff --git a/sshgen/sshgen_test.go b/sshgen/sshgen_test.go index 51259d6d..dec55441 100644 --- a/sshgen/sshgen_test.go +++ b/sshgen/sshgen_test.go @@ -7,7 +7,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "net/http/httptest" "net/url" @@ -64,7 +63,7 @@ func TestCertGenSuccess(t *testing.T) { mockRequest = func(url, contentType string, body io.Reader) (*http.Response, error) { assert.Contains(t, "/cdn-cgi/access/cert_sign", url) assert.Equal(t, "application/json", contentType) - buf, err := ioutil.ReadAll(body) + buf, err := io.ReadAll(body) assert.NoError(t, err) assert.NotEmpty(t, buf) return w.Result(), nil diff --git a/supervisor/cloudflare_status_page.go b/supervisor/cloudflare_status_page.go index 93d9e849..c31069c2 100644 --- a/supervisor/cloudflare_status_page.go +++ b/supervisor/cloudflare_status_page.go @@ -2,7 +2,7 @@ package supervisor import ( "encoding/json" - "io/ioutil" + "io" "net/http" "strings" "time" @@ -72,7 +72,7 @@ func fetchActiveIncidents() (incidents []Incident) { return } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return } diff --git a/tlsconfig/certreloader.go b/tlsconfig/certreloader.go index 41e37c69..cfebadf3 100644 --- a/tlsconfig/certreloader.go +++ b/tlsconfig/certreloader.go @@ -4,7 +4,7 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" + "os" "runtime" "sync" @@ -77,7 +77,7 @@ func LoadOriginCA(originCAPoolFilename string, log *zerolog.Logger) (*x509.CertP if originCAPoolFilename != "" { var err error - originCustomCAPool, err = ioutil.ReadFile(originCAPoolFilename) + originCustomCAPool, err = os.ReadFile(originCAPoolFilename) if err != nil { return nil, errors.Wrap(err, fmt.Sprintf("unable to read the file %s for --%s", originCAPoolFilename, OriginCAPoolFlag)) } @@ -116,7 +116,7 @@ func LoadCustomOriginCA(originCAFilename string) (*x509.CertPool, error) { return certPool, nil } - customOriginCA, err := ioutil.ReadFile(originCAFilename) + customOriginCA, err := os.ReadFile(originCAFilename) if err != nil { return nil, errors.Wrap(err, fmt.Sprintf("unable to read the file %s", originCAFilename)) } diff --git a/tlsconfig/tlsconfig.go b/tlsconfig/tlsconfig.go index 46490373..3e6cea20 100644 --- a/tlsconfig/tlsconfig.go +++ b/tlsconfig/tlsconfig.go @@ -5,7 +5,7 @@ package tlsconfig import ( "crypto/tls" "crypto/x509" - "io/ioutil" + "os" "github.com/pkg/errors" ) @@ -90,7 +90,7 @@ func GetConfig(p *TLSParameters) (*tls.Config, error) { func LoadCert(certPaths []string) (*x509.CertPool, error) { ca := x509.NewCertPool() for _, certPath := range certPaths { - caCert, err := ioutil.ReadFile(certPath) + caCert, err := os.ReadFile(certPath) if err != nil { return nil, errors.Wrapf(err, "Error reading certificate %s", certPath) } diff --git a/token/token.go b/token/token.go index ee91d3e7..478646fe 100644 --- a/token/token.go +++ b/token/token.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "net/http" "net/url" "os" @@ -124,7 +123,7 @@ func (l *lock) Acquire() error { // Create a lock file so other processes won't also try to get the token at // the same time - if err := ioutil.WriteFile(l.lockFilePath, []byte{}, 0600); err != nil { + if err := os.WriteFile(l.lockFilePath, []byte{}, 0600); err != nil { return err } return nil @@ -208,7 +207,7 @@ func getToken(appURL *url.URL, appInfo *AppInfo, useHostOnly bool, log *zerolog. log.Debug().Msgf("failed to exchange org token for app token: %s", err) } else { // generate app path - if err := ioutil.WriteFile(appTokenPath, []byte(appToken), 0600); err != nil { + if err := os.WriteFile(appTokenPath, []byte(appToken), 0600); err != nil { return "", errors.Wrap(err, "failed to write app token to disk") } return appToken, nil @@ -237,12 +236,12 @@ func getTokensFromEdge(appURL *url.URL, appAUD, appTokenPath, orgTokenPath strin // If we were able to get the auth domain and generate an org token path, lets write it to disk. if orgTokenPath != "" { - if err := ioutil.WriteFile(orgTokenPath, []byte(resp.OrgToken), 0600); err != nil { + if err := os.WriteFile(orgTokenPath, []byte(resp.OrgToken), 0600); err != nil { return "", errors.Wrap(err, "failed to write org token to disk") } } - if err := ioutil.WriteFile(appTokenPath, []byte(resp.AppToken), 0600); err != nil { + if err := os.WriteFile(appTokenPath, []byte(resp.AppToken), 0600); err != nil { return "", errors.Wrap(err, "failed to write app token to disk") } @@ -389,7 +388,7 @@ func GetAppTokenIfExists(appInfo *AppInfo) (string, error) { // GetTokenIfExists will return the token from local storage if it exists and not expired func getTokenIfExists(path string) (*jose.JSONWebSignature, error) { - content, err := ioutil.ReadFile(path) + content, err := os.ReadFile(path) if err != nil { return nil, err } diff --git a/tunneldns/https_upstream.go b/tunneldns/https_upstream.go index c5130519..2fe84c38 100644 --- a/tunneldns/https_upstream.go +++ b/tunneldns/https_upstream.go @@ -5,7 +5,7 @@ import ( "context" "crypto/tls" "fmt" - "io/ioutil" + "io" "net" "net/http" "net/url" @@ -105,7 +105,7 @@ func exchangeWireformat(msg []byte, endpoint *url.URL, client *http.Client) ([]b } // Read wireformat response from the body - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { return nil, errors.Wrap(err, "failed to read the response body") } diff --git a/validation/validation_test.go b/validation/validation_test.go index 8b8dda6d..819d503d 100644 --- a/validation/validation_test.go +++ b/validation/validation_test.go @@ -3,7 +3,7 @@ package validation import ( "bytes" "fmt" - "io/ioutil" + "io" "testing" "time" @@ -329,7 +329,7 @@ func (f testRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { func emptyResponse(statusCode int) *http.Response { return &http.Response{ StatusCode: statusCode, - Body: ioutil.NopCloser(bytes.NewReader(nil)), + Body: io.NopCloser(bytes.NewReader(nil)), Header: make(http.Header), } }