Skip to content

Commit

Permalink
TUN-7590: Remove usages of ioutil
Browse files Browse the repository at this point in the history
  • Loading branch information
DevinCarr committed Jul 17, 2023
1 parent 1b0b6bf commit b500e55
Show file tree
Hide file tree
Showing 23 changed files with 49 additions and 62 deletions.
4 changes: 2 additions & 2 deletions cfapi/tunnel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cfapi
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net"
"reflect"
"strings"
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions cmd/cloudflared/service_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions cmd/cloudflared/tunnel/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"runtime/trace"
Expand Down Expand Up @@ -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")
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/cloudflared/tunnel/filesystem.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tunnel

import (
"io/ioutil"
"os"
)

Expand All @@ -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)
}
3 changes: 1 addition & 2 deletions cmd/cloudflared/tunnel/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tunnel

import (
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -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))
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/cloudflared/tunnel/subcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 5 additions & 6 deletions cmd/cloudflared/updater/workers_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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))
Expand Down
7 changes: 3 additions & 4 deletions connection/http2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions h2mux/h2mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net"
"os"
Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions hello/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"io"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions ingress/origin_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions ingress/origin_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ingress
import (
"context"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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))
}
Expand Down
3 changes: 1 addition & 2 deletions orchestration/orchestrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions proxy/proxy_posix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package proxy

import (
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions socks/connection_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package socks

import (
"encoding/json"
"io/ioutil"
"io"
"net"
"net/http"
"testing"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions sshgen/sshgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
3 changes: 1 addition & 2 deletions sshgen/sshgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit b500e55

Please sign in to comment.