From c48fadc4a7655008d13076c7f757c36368e2ca13 Mon Sep 17 00:00:00 2001 From: KallyDev <36319157+kallydev@users.noreply.github.com> Date: Thu, 30 Sep 2021 01:17:48 +0800 Subject: [PATCH] Move from deprecated ioutil to os and io packages (#4364) --- .golangci.yml | 2 +- admin.go | 3 +-- caddy.go | 7 +++---- caddyconfig/caddyfile/parse.go | 4 ++-- caddyconfig/caddyfile/parse_test.go | 11 +++++------ caddyconfig/httpcaddyfile/builtins.go | 4 ++-- caddyconfig/httploader.go | 7 ++++--- caddytest/caddytest.go | 10 +++++----- caddytest/integration/caddyfile_adapt_test.go | 6 +++--- caddytest/integration/reverseproxy_test.go | 7 +++---- caddytest/integration/stream_test.go | 11 +++++------ cmd/commandfuncs.go | 15 +++++++-------- cmd/main.go | 9 ++++----- logging.go | 5 ++--- modules/caddyhttp/replacer.go | 3 +-- modules/caddyhttp/reverseproxy/fastcgi/client.go | 3 +-- .../caddyhttp/reverseproxy/fastcgi/client_test.go | 3 +-- modules/caddyhttp/reverseproxy/healthchecks.go | 5 ++--- modules/caddyhttp/reverseproxy/httptransport.go | 4 ++-- modules/caddyhttp/staticresp_test.go | 4 ++-- modules/caddyhttp/templates/tplcontext_test.go | 9 ++++----- modules/caddypki/crypto.go | 6 +++--- modules/caddytls/acmeissuer.go | 4 ++-- modules/caddytls/connpolicy.go | 4 ++-- modules/caddytls/fileloader.go | 6 +++--- modules/caddytls/folderloader.go | 3 +-- 26 files changed, 71 insertions(+), 84 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 2c6acca5..79fd8f2f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,6 +1,6 @@ linters-settings: errcheck: - ignore: fmt:.*,io/ioutil:^Read.*,go.uber.org/zap/zapcore:^Add.* + ignore: fmt:.*,go.uber.org/zap/zapcore:^Add.* ignoretests: true linters: diff --git a/admin.go b/admin.go index fb451682..684526dd 100644 --- a/admin.go +++ b/admin.go @@ -26,7 +26,6 @@ import ( "expvar" "fmt" "io" - "io/ioutil" "net" "net/http" "net/http/pprof" @@ -1202,7 +1201,7 @@ var ( // will get deleted before the process gracefully exits. func PIDFile(filename string) error { pid := []byte(strconv.Itoa(os.Getpid()) + "\n") - err := ioutil.WriteFile(filename, pid, 0600) + err := os.WriteFile(filename, pid, 0600) if err != nil { return err } diff --git a/caddy.go b/caddy.go index ba025b18..a7e99c11 100644 --- a/caddy.go +++ b/caddy.go @@ -20,7 +20,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "log" "net/http" "os" @@ -300,7 +299,7 @@ func unsyncedDecodeAndRun(cfgJSON []byte, allowPersist bool) error { zap.String("dir", dir), zap.Error(err)) } else { - err := ioutil.WriteFile(ConfigAutosavePath, cfgJSON, 0600) + err := os.WriteFile(ConfigAutosavePath, cfgJSON, 0600) if err == nil { Log().Info("autosaved config (load with --resume flag)", zap.String("file", ConfigAutosavePath)) } else { @@ -700,13 +699,13 @@ func ParseDuration(s string) (time.Duration, error) { // have its own unique ID. func InstanceID() (uuid.UUID, error) { uuidFilePath := filepath.Join(AppDataDir(), "instance.uuid") - uuidFileBytes, err := ioutil.ReadFile(uuidFilePath) + uuidFileBytes, err := os.ReadFile(uuidFilePath) if os.IsNotExist(err) { uuid, err := uuid.NewRandom() if err != nil { return uuid, err } - err = ioutil.WriteFile(uuidFilePath, []byte(uuid.String()), 0600) + err = os.WriteFile(uuidFilePath, []byte(uuid.String()), 0600) return uuid, err } else if err != nil { return [16]byte{}, err diff --git a/caddyconfig/caddyfile/parse.go b/caddyconfig/caddyfile/parse.go index 0fe07ab9..8591759b 100755 --- a/caddyconfig/caddyfile/parse.go +++ b/caddyconfig/caddyfile/parse.go @@ -17,7 +17,7 @@ package caddyfile import ( "bytes" "fmt" - "io/ioutil" + "io" "log" "os" "path/filepath" @@ -447,7 +447,7 @@ func (p *parser) doSingleImport(importFile string) ([]Token, error) { return nil, p.Errf("Could not import %s: is a directory", importFile) } - input, err := ioutil.ReadAll(file) + input, err := io.ReadAll(file) if err != nil { return nil, p.Errf("Could not read imported file %s: %v", importFile, err) } diff --git a/caddyconfig/caddyfile/parse_test.go b/caddyconfig/caddyfile/parse_test.go index 0996ed04..8d43e1aa 100755 --- a/caddyconfig/caddyfile/parse_test.go +++ b/caddyconfig/caddyfile/parse_test.go @@ -16,7 +16,6 @@ package caddyfile import ( "bytes" - "io/ioutil" "os" "path/filepath" "testing" @@ -280,7 +279,7 @@ func TestRecursiveImport(t *testing.T) { } // test relative recursive import - err = ioutil.WriteFile(recursiveFile1, []byte( + err = os.WriteFile(recursiveFile1, []byte( `localhost dir1 import recursive_import_test2`), 0644) @@ -289,7 +288,7 @@ func TestRecursiveImport(t *testing.T) { } defer os.Remove(recursiveFile1) - err = ioutil.WriteFile(recursiveFile2, []byte("dir2 1"), 0644) + err = os.WriteFile(recursiveFile2, []byte("dir2 1"), 0644) if err != nil { t.Fatal(err) } @@ -314,7 +313,7 @@ func TestRecursiveImport(t *testing.T) { } // test absolute recursive import - err = ioutil.WriteFile(recursiveFile1, []byte( + err = os.WriteFile(recursiveFile1, []byte( `localhost dir1 import `+recursiveFile2), 0644) @@ -370,7 +369,7 @@ func TestDirectiveImport(t *testing.T) { t.Fatal(err) } - err = ioutil.WriteFile(directiveFile, []byte(`prop1 1 + err = os.WriteFile(directiveFile, []byte(`prop1 1 prop2 2`), 0644) if err != nil { t.Fatal(err) @@ -633,7 +632,7 @@ func TestSnippets(t *testing.T) { } func writeStringToTempFileOrDie(t *testing.T, str string) (pathToFile string) { - file, err := ioutil.TempFile("", t.Name()) + file, err := os.CreateTemp("", t.Name()) if err != nil { panic(err) // get a stack trace so we know where this was called from. } diff --git a/caddyconfig/httpcaddyfile/builtins.go b/caddyconfig/httpcaddyfile/builtins.go index d52c5ef8..7fdd3e8a 100644 --- a/caddyconfig/httpcaddyfile/builtins.go +++ b/caddyconfig/httpcaddyfile/builtins.go @@ -19,8 +19,8 @@ import ( "encoding/pem" "fmt" "html" - "io/ioutil" "net/http" + "os" "reflect" "strconv" "strings" @@ -230,7 +230,7 @@ func parseTLS(h Helper) ([]ConfigValue, error) { return nil, h.ArgErr() } filename := h.Val() - certDataPEM, err := ioutil.ReadFile(filename) + certDataPEM, err := os.ReadFile(filename) if err != nil { return nil, err } diff --git a/caddyconfig/httploader.go b/caddyconfig/httploader.go index d9e222bb..396e1536 100644 --- a/caddyconfig/httploader.go +++ b/caddyconfig/httploader.go @@ -18,8 +18,9 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" + "io" "net/http" + "os" "time" "github.com/caddyserver/caddy/v2" @@ -95,7 +96,7 @@ func (hl HTTPLoader) LoadConfig(ctx caddy.Context) ([]byte, error) { return nil, fmt.Errorf("server responded with HTTP %d", resp.StatusCode) } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } @@ -144,7 +145,7 @@ func (hl HTTPLoader) makeClient(ctx caddy.Context) (*http.Client, error) { if len(hl.TLS.RootCAPEMFiles) > 0 { rootPool := x509.NewCertPool() for _, pemFile := range hl.TLS.RootCAPEMFiles { - pemData, err := ioutil.ReadFile(pemFile) + pemData, err := os.ReadFile(pemFile) if err != nil { return nil, fmt.Errorf("failed reading ca cert: %v", err) } diff --git a/caddytest/caddytest.go b/caddytest/caddytest.go index 094e45c6..3be332ec 100644 --- a/caddytest/caddytest.go +++ b/caddytest/caddytest.go @@ -7,7 +7,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "log" "net" "net/http" @@ -129,7 +129,7 @@ func (tc *Tester) initServer(rawConfig string, configType string) error { return } defer res.Body.Close() - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) var out bytes.Buffer _ = json.Indent(&out, body, "", " ") @@ -162,7 +162,7 @@ func (tc *Tester) initServer(rawConfig string, configType string) error { timeElapsed(start, "caddytest: config load time") defer res.Body.Close() - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { tc.t.Errorf("unable to read response. %s", err) return err @@ -202,7 +202,7 @@ func (tc *Tester) ensureConfigRunning(rawConfig string, configType string) error return nil } defer resp.Body.Close() - actualBytes, err := ioutil.ReadAll(resp.Body) + actualBytes, err := io.ReadAll(resp.Body) if err != nil { return nil } @@ -471,7 +471,7 @@ func (tc *Tester) AssertResponse(req *http.Request, expectedStatusCode int, expe resp := tc.AssertResponseCode(req, expectedStatusCode) defer resp.Body.Close() - bytes, err := ioutil.ReadAll(resp.Body) + bytes, err := io.ReadAll(resp.Body) if err != nil { tc.t.Fatalf("unable to read the response body %s", err) } diff --git a/caddytest/integration/caddyfile_adapt_test.go b/caddytest/integration/caddyfile_adapt_test.go index e1098999..5b052df4 100644 --- a/caddytest/integration/caddyfile_adapt_test.go +++ b/caddytest/integration/caddyfile_adapt_test.go @@ -3,7 +3,7 @@ package integration import ( jsonMod "encoding/json" "fmt" - "io/ioutil" + "os" "path/filepath" "regexp" "strings" @@ -14,7 +14,7 @@ import ( func TestCaddyfileAdaptToJSON(t *testing.T) { // load the list of test files from the dir - files, err := ioutil.ReadDir("./caddyfile_adapt") + files, err := os.ReadDir("./caddyfile_adapt") if err != nil { t.Errorf("failed to read caddyfile_adapt dir: %s", err) } @@ -29,7 +29,7 @@ func TestCaddyfileAdaptToJSON(t *testing.T) { // read the test file filename := f.Name() - data, err := ioutil.ReadFile("./caddyfile_adapt/" + filename) + data, err := os.ReadFile("./caddyfile_adapt/" + filename) if err != nil { t.Errorf("failed to read %s dir: %s", filename, err) } diff --git a/caddytest/integration/reverseproxy_test.go b/caddytest/integration/reverseproxy_test.go index 422f6716..ca11287a 100644 --- a/caddytest/integration/reverseproxy_test.go +++ b/caddytest/integration/reverseproxy_test.go @@ -2,7 +2,6 @@ package integration import ( "fmt" - "io/ioutil" "net" "net/http" "os" @@ -85,7 +84,7 @@ func TestDialWithPlaceholderUnix(t *testing.T) { t.SkipNow() } - f, err := ioutil.TempFile("", "*.sock") + f, err := os.CreateTemp("", "*.sock") if err != nil { t.Errorf("failed to create TempFile: %s", err) return @@ -387,7 +386,7 @@ func TestReverseProxyHealthCheckUnixSocket(t *testing.T) { t.SkipNow() } tester := caddytest.NewTester(t) - f, err := ioutil.TempFile("", "*.sock") + f, err := os.CreateTemp("", "*.sock") if err != nil { t.Errorf("failed to create TempFile: %s", err) return @@ -442,7 +441,7 @@ func TestReverseProxyHealthCheckUnixSocketWithoutPort(t *testing.T) { t.SkipNow() } tester := caddytest.NewTester(t) - f, err := ioutil.TempFile("", "*.sock") + f, err := os.CreateTemp("", "*.sock") if err != nil { t.Errorf("failed to create TempFile: %s", err) return diff --git a/caddytest/integration/stream_test.go b/caddytest/integration/stream_test.go index b6447c68..d5edb793 100644 --- a/caddytest/integration/stream_test.go +++ b/caddytest/integration/stream_test.go @@ -6,7 +6,6 @@ import ( "crypto/rand" "fmt" "io" - "io/ioutil" "net/http" "net/http/httputil" "net/url" @@ -110,7 +109,7 @@ func TestH2ToH2CStream(t *testing.T) { r, w := io.Pipe() req := &http.Request{ Method: "PUT", - Body: ioutil.NopCloser(r), + Body: io.NopCloser(r), URL: &url.URL{ Scheme: "https", Host: "127.0.0.1:9443", @@ -134,7 +133,7 @@ func TestH2ToH2CStream(t *testing.T) { }() defer resp.Body.Close() - bytes, err := ioutil.ReadAll(resp.Body) + bytes, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("unable to read the response body %s", err) } @@ -319,7 +318,7 @@ func TestH2ToH1ChunkedResponse(t *testing.T) { r, w := io.Pipe() req := &http.Request{ Method: "PUT", - Body: ioutil.NopCloser(r), + Body: io.NopCloser(r), URL: &url.URL{ Scheme: "https", Host: "127.0.0.1:9443", @@ -342,7 +341,7 @@ func TestH2ToH1ChunkedResponse(t *testing.T) { } defer resp.Body.Close() - bytes, err := ioutil.ReadAll(resp.Body) + bytes, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("unable to read the response body %s", err) } @@ -370,7 +369,7 @@ func testH2ToH1ChunkedResponseServeH1(t *testing.T) *http.Server { } defer r.Body.Close() - bytes, err := ioutil.ReadAll(r.Body) + bytes, err := io.ReadAll(r.Body) if err != nil { t.Fatalf("unable to read the response body %s", err) } diff --git a/cmd/commandfuncs.go b/cmd/commandfuncs.go index 8f1c68c8..cc3f65c7 100644 --- a/cmd/commandfuncs.go +++ b/cmd/commandfuncs.go @@ -22,7 +22,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "log" "net" "net/http" @@ -181,7 +180,7 @@ func cmdRun(fl Flags) (int, error) { var config []byte var err error if runCmdResumeFlag { - config, err = ioutil.ReadFile(caddy.ConfigAutosavePath) + config, err = os.ReadFile(caddy.ConfigAutosavePath) if os.IsNotExist(err) { // not a bad error; just can't resume if autosave file doesn't exist caddy.Log().Info("no autosave file exists", zap.String("autosave_file", caddy.ConfigAutosavePath)) @@ -219,7 +218,7 @@ func cmdRun(fl Flags) (int, error) { // if we are to report to another process the successful start // of the server, do so now by echoing back contents of stdin if runCmdPingbackFlag != "" { - confirmationBytes, err := ioutil.ReadAll(os.Stdin) + confirmationBytes, err := io.ReadAll(os.Stdin) if err != nil { return caddy.ExitCodeFailedStartup, fmt.Errorf("reading confirmation bytes from stdin: %v", err) @@ -457,7 +456,7 @@ func cmdAdaptConfig(fl Flags) (int, error) { fmt.Errorf("unrecognized config adapter: %s", adaptCmdAdapterFlag) } - input, err := ioutil.ReadFile(adaptCmdInputFlag) + input, err := os.ReadFile(adaptCmdInputFlag) if err != nil { return caddy.ExitCodeFailedStartup, fmt.Errorf("reading input file: %v", err) @@ -541,7 +540,7 @@ func cmdFmt(fl Flags) (int, error) { // as a special case, read from stdin if the file name is "-" if formatCmdConfigFile == "-" { - input, err := ioutil.ReadAll(os.Stdin) + input, err := io.ReadAll(os.Stdin) if err != nil { return caddy.ExitCodeFailedStartup, fmt.Errorf("reading stdin: %v", err) @@ -550,7 +549,7 @@ func cmdFmt(fl Flags) (int, error) { return caddy.ExitCodeSuccess, nil } - input, err := ioutil.ReadFile(formatCmdConfigFile) + input, err := os.ReadFile(formatCmdConfigFile) if err != nil { return caddy.ExitCodeFailedStartup, fmt.Errorf("reading input file: %v", err) @@ -559,7 +558,7 @@ func cmdFmt(fl Flags) (int, error) { output := caddyfile.Format(input) if fl.Bool("overwrite") { - if err := ioutil.WriteFile(formatCmdConfigFile, output, 0600); err != nil { + if err := os.WriteFile(formatCmdConfigFile, output, 0600); err != nil { return caddy.ExitCodeFailedStartup, nil } } else { @@ -699,7 +698,7 @@ func apiRequest(adminAddr, method, uri string, headers http.Header, body io.Read // if it didn't work, let the user know if resp.StatusCode >= 400 { - respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1024*10)) + respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1024*10)) if err != nil { return fmt.Errorf("HTTP %d: reading error message: %v", resp.StatusCode, err) } diff --git a/cmd/main.go b/cmd/main.go index 8a9a2896..7c33c55f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -20,7 +20,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "log" "net" "os" @@ -94,7 +93,7 @@ func Main() { // the bytes in expect, or returns an error if it doesn't. func handlePingbackConn(conn net.Conn, expect []byte) error { defer conn.Close() - confirmationBytes, err := ioutil.ReadAll(io.LimitReader(conn, 32)) + confirmationBytes, err := io.ReadAll(io.LimitReader(conn, 32)) if err != nil { return err } @@ -124,9 +123,9 @@ func loadConfig(configFile, adapterName string) ([]byte, string, error) { var err error if configFile != "" { if configFile == "-" { - config, err = ioutil.ReadAll(os.Stdin) + config, err = io.ReadAll(os.Stdin) } else { - config, err = ioutil.ReadFile(configFile) + config, err = os.ReadFile(configFile) } if err != nil { return nil, "", fmt.Errorf("reading config file: %v", err) @@ -140,7 +139,7 @@ func loadConfig(configFile, adapterName string) ([]byte, string, error) { // plugged in, and if so, try using a default Caddyfile cfgAdapter = caddyconfig.GetAdapter("caddyfile") if cfgAdapter != nil { - config, err = ioutil.ReadFile("Caddyfile") + config, err = os.ReadFile("Caddyfile") if os.IsNotExist(err) { // okay, no default Caddyfile; pretend like this never happened cfgAdapter = nil diff --git a/logging.go b/logging.go index 7837145d..129aec43 100644 --- a/logging.go +++ b/logging.go @@ -18,7 +18,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "log" "os" "strings" @@ -630,9 +629,9 @@ func (StderrWriter) OpenWriter() (io.WriteCloser, error) { return notClosable{os.Stderr}, nil } -// OpenWriter returns ioutil.Discard that can't be closed. +// OpenWriter returns io.Discard that can't be closed. func (DiscardWriter) OpenWriter() (io.WriteCloser, error) { - return notClosable{ioutil.Discard}, nil + return notClosable{io.Discard}, nil } // notClosable is an io.WriteCloser that can't be closed. diff --git a/modules/caddyhttp/replacer.go b/modules/caddyhttp/replacer.go index d0767f09..4d27a840 100644 --- a/modules/caddyhttp/replacer.go +++ b/modules/caddyhttp/replacer.go @@ -28,7 +28,6 @@ import ( "encoding/pem" "fmt" "io" - "io/ioutil" "net" "net/http" "net/textproto" @@ -162,7 +161,7 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo return "", true } // replace real body with buffered data - req.Body = ioutil.NopCloser(buf) + req.Body = io.NopCloser(buf) return buf.String(), true // original request, before any internal changes diff --git a/modules/caddyhttp/reverseproxy/fastcgi/client.go b/modules/caddyhttp/reverseproxy/fastcgi/client.go index 94df0c77..07720539 100644 --- a/modules/caddyhttp/reverseproxy/fastcgi/client.go +++ b/modules/caddyhttp/reverseproxy/fastcgi/client.go @@ -30,7 +30,6 @@ import ( "encoding/binary" "errors" "io" - "io/ioutil" "mime/multipart" "net" "net/http" @@ -445,7 +444,7 @@ func (c *FCGIClient) Request(p map[string]string, req io.Reader) (resp *http.Res if chunked(resp.TransferEncoding) { resp.Body = clientCloser{c, httputil.NewChunkedReader(rb)} } else { - resp.Body = clientCloser{c, ioutil.NopCloser(rb)} + resp.Body = clientCloser{c, io.NopCloser(rb)} } return } diff --git a/modules/caddyhttp/reverseproxy/fastcgi/client_test.go b/modules/caddyhttp/reverseproxy/fastcgi/client_test.go index f0740a4c..ef3474d4 100644 --- a/modules/caddyhttp/reverseproxy/fastcgi/client_test.go +++ b/modules/caddyhttp/reverseproxy/fastcgi/client_test.go @@ -26,7 +26,6 @@ import ( "encoding/binary" "fmt" "io" - "io/ioutil" "log" "math/rand" "net" @@ -166,7 +165,7 @@ func sendFcgi(reqType int, fcgiParams map[string]string, data []byte, posts map[ } defer resp.Body.Close() - content, _ = ioutil.ReadAll(resp.Body) + content, _ = io.ReadAll(resp.Body) log.Println("c: send data length ≈", length, string(content)) fcgi.Close() diff --git a/modules/caddyhttp/reverseproxy/healthchecks.go b/modules/caddyhttp/reverseproxy/healthchecks.go index 8d5bd77e..230bf3aa 100644 --- a/modules/caddyhttp/reverseproxy/healthchecks.go +++ b/modules/caddyhttp/reverseproxy/healthchecks.go @@ -18,7 +18,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "log" "net" "net/http" @@ -282,7 +281,7 @@ func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, host H } defer func() { // drain any remaining body so connection could be re-used - _, _ = io.Copy(ioutil.Discard, body) + _, _ = io.Copy(io.Discard, body) resp.Body.Close() }() @@ -313,7 +312,7 @@ func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, host H // if body does not match regex, mark down if h.HealthChecks.Active.bodyRegexp != nil { - bodyBytes, err := ioutil.ReadAll(body) + bodyBytes, err := io.ReadAll(body) if err != nil { h.HealthChecks.Active.logger.Info("failed to read response body", zap.String("host", hostAddr), diff --git a/modules/caddyhttp/reverseproxy/httptransport.go b/modules/caddyhttp/reverseproxy/httptransport.go index 19328513..35bc9478 100644 --- a/modules/caddyhttp/reverseproxy/httptransport.go +++ b/modules/caddyhttp/reverseproxy/httptransport.go @@ -20,10 +20,10 @@ import ( "crypto/x509" "encoding/base64" "fmt" - "io/ioutil" weakrand "math/rand" "net" "net/http" + "os" "reflect" "time" @@ -364,7 +364,7 @@ func (t TLSConfig) MakeTLSClientConfig(ctx caddy.Context) (*tls.Config, error) { rootPool.AddCert(caCert) } for _, pemFile := range t.RootCAPEMFiles { - pemData, err := ioutil.ReadFile(pemFile) + pemData, err := os.ReadFile(pemFile) if err != nil { return nil, fmt.Errorf("failed reading ca cert: %v", err) } diff --git a/modules/caddyhttp/staticresp_test.go b/modules/caddyhttp/staticresp_test.go index cd0d1a1e..5844f43f 100644 --- a/modules/caddyhttp/staticresp_test.go +++ b/modules/caddyhttp/staticresp_test.go @@ -16,7 +16,7 @@ package caddyhttp import ( "context" - "io/ioutil" + "io" "net/http" "net/http/httptest" "strconv" @@ -44,7 +44,7 @@ func TestStaticResponseHandler(t *testing.T) { } resp := w.Result() - respBody, _ := ioutil.ReadAll(resp.Body) + respBody, _ := io.ReadAll(resp.Body) if resp.StatusCode != http.StatusNotFound { t.Errorf("expected status %d but got %d", http.StatusNotFound, resp.StatusCode) diff --git a/modules/caddyhttp/templates/tplcontext_test.go b/modules/caddyhttp/templates/tplcontext_test.go index 1d15ca73..c2fe61f4 100644 --- a/modules/caddyhttp/templates/tplcontext_test.go +++ b/modules/caddyhttp/templates/tplcontext_test.go @@ -17,7 +17,6 @@ package templates import ( "bytes" "fmt" - "io/ioutil" "net/http" "os" "path/filepath" @@ -118,7 +117,7 @@ func TestImport(t *testing.T) { // create files for test case if test.fileName != "" { absFilePath := filepath.Join(fmt.Sprintf("%s", context.Root), test.fileName) - if err := ioutil.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil { + if err := os.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil { os.Remove(absFilePath) t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error()) } @@ -198,7 +197,7 @@ func TestInclude(t *testing.T) { // create files for test case if test.fileName != "" { absFilePath := filepath.Join(fmt.Sprintf("%s", context.Root), test.fileName) - if err := ioutil.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil { + if err := os.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil { os.Remove(absFilePath) t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error()) } @@ -357,13 +356,13 @@ func TestFileListing(t *testing.T) { // create files for test case if test.fileNames != nil { - dirPath, err = ioutil.TempDir(fmt.Sprintf("%s", context.Root), "caddy_ctxtest") + dirPath, err = os.MkdirTemp(fmt.Sprintf("%s", context.Root), "caddy_ctxtest") if err != nil { t.Fatalf("Test %d: Expected no error creating directory, got: '%s'", i, err.Error()) } for _, name := range test.fileNames { absFilePath := filepath.Join(dirPath, name) - if err = ioutil.WriteFile(absFilePath, []byte(""), os.ModePerm); err != nil { + if err = os.WriteFile(absFilePath, []byte(""), os.ModePerm); err != nil { os.RemoveAll(dirPath) t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error()) } diff --git a/modules/caddypki/crypto.go b/modules/caddypki/crypto.go index e1a0e354..d8e72c6d 100644 --- a/modules/caddypki/crypto.go +++ b/modules/caddypki/crypto.go @@ -23,7 +23,7 @@ import ( "crypto/x509" "encoding/pem" "fmt" - "io/ioutil" + "os" "strings" ) @@ -137,11 +137,11 @@ type KeyPair struct { func (kp KeyPair) Load() (*x509.Certificate, interface{}, error) { switch kp.Format { case "", "pem_file": - certData, err := ioutil.ReadFile(kp.Certificate) + certData, err := os.ReadFile(kp.Certificate) if err != nil { return nil, nil, err } - keyData, err := ioutil.ReadFile(kp.PrivateKey) + keyData, err := os.ReadFile(kp.PrivateKey) if err != nil { return nil, nil, err } diff --git a/modules/caddytls/acmeissuer.go b/modules/caddytls/acmeissuer.go index 9a7e73cd..42cef021 100644 --- a/modules/caddytls/acmeissuer.go +++ b/modules/caddytls/acmeissuer.go @@ -18,8 +18,8 @@ import ( "context" "crypto/x509" "fmt" - "io/ioutil" "net/url" + "os" "strconv" "time" @@ -152,7 +152,7 @@ func (iss *ACMEIssuer) Provision(ctx caddy.Context) error { if len(iss.TrustedRootsPEMFiles) > 0 { iss.rootPool = x509.NewCertPool() for _, pemFile := range iss.TrustedRootsPEMFiles { - pemData, err := ioutil.ReadFile(pemFile) + pemData, err := os.ReadFile(pemFile) if err != nil { return fmt.Errorf("loading trusted root CA's PEM file: %s: %v", pemFile, err) } diff --git a/modules/caddytls/connpolicy.go b/modules/caddytls/connpolicy.go index 6c7fe3f4..65cda5fb 100644 --- a/modules/caddytls/connpolicy.go +++ b/modules/caddytls/connpolicy.go @@ -19,7 +19,7 @@ import ( "crypto/x509" "encoding/base64" "fmt" - "io/ioutil" + "os" "strings" "github.com/caddyserver/caddy/v2" @@ -369,7 +369,7 @@ func (clientauth *ClientAuthentication) ConfigureTLSConfig(cfg *tls.Config) erro caPool.AddCert(clientCA) } for _, pemFile := range clientauth.TrustedCACertPEMFiles { - pemContents, err := ioutil.ReadFile(pemFile) + pemContents, err := os.ReadFile(pemFile) if err != nil { return fmt.Errorf("reading %s: %v", pemFile, err) } diff --git a/modules/caddytls/fileloader.go b/modules/caddytls/fileloader.go index fdf54864..430932b9 100644 --- a/modules/caddytls/fileloader.go +++ b/modules/caddytls/fileloader.go @@ -17,7 +17,7 @@ package caddytls import ( "crypto/tls" "fmt" - "io/ioutil" + "os" "github.com/caddyserver/caddy/v2" ) @@ -59,11 +59,11 @@ type CertKeyFilePair struct { func (fl FileLoader) LoadCertificates() ([]Certificate, error) { certs := make([]Certificate, 0, len(fl)) for _, pair := range fl { - certData, err := ioutil.ReadFile(pair.Certificate) + certData, err := os.ReadFile(pair.Certificate) if err != nil { return nil, err } - keyData, err := ioutil.ReadFile(pair.Key) + keyData, err := os.ReadFile(pair.Key) if err != nil { return nil, err } diff --git a/modules/caddytls/folderloader.go b/modules/caddytls/folderloader.go index 10b017ee..0ff0629d 100644 --- a/modules/caddytls/folderloader.go +++ b/modules/caddytls/folderloader.go @@ -19,7 +19,6 @@ import ( "crypto/tls" "encoding/pem" "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -79,7 +78,7 @@ func (fl FolderLoader) LoadCertificates() ([]Certificate, error) { } func x509CertFromCertAndKeyPEMFile(fpath string) (tls.Certificate, error) { - bundle, err := ioutil.ReadFile(fpath) + bundle, err := os.ReadFile(fpath) if err != nil { return tls.Certificate{}, err }