From 7df242f1e3f7364e3e9007421263e445266e8f57 Mon Sep 17 00:00:00 2001 From: James Elliott Date: Thu, 2 Dec 2021 00:14:15 +1100 Subject: [PATCH] refactor: remove ioutil (#2635) Was deprecated in 1.16 and has more performant options available. --- cmd/authelia-scripts/cmd_bootstrap.go | 5 ++--- cmd/authelia-scripts/cmd_suites.go | 3 +-- cmd/authelia-suites/main.go | 3 +-- internal/authentication/file_user_provider.go | 7 +++---- internal/authentication/file_user_provider_test.go | 3 +-- internal/configuration/helpers.go | 4 ++-- internal/configuration/koanf_callbacks_test.go | 6 +++--- internal/configuration/provider_test.go | 9 ++++----- internal/configuration/template.go | 3 +-- internal/configuration/template_test.go | 7 +++---- internal/logging/logger_test.go | 14 +++++++------- internal/notification/file_notifier.go | 5 ++--- internal/server/template.go | 4 ++-- internal/suites/action_http.go | 4 ++-- internal/suites/suite_standalone.go | 5 ++--- internal/suites/suite_standalone_test.go | 14 +++++++------- internal/suites/utils.go | 7 +++---- internal/utils/certificates.go | 6 +++--- internal/utils/hashing_test.go | 3 +-- 19 files changed, 50 insertions(+), 62 deletions(-) diff --git a/cmd/authelia-scripts/cmd_bootstrap.go b/cmd/authelia-scripts/cmd_bootstrap.go index d24970f69..6d4bec2b1 100644 --- a/cmd/authelia-scripts/cmd_bootstrap.go +++ b/cmd/authelia-scripts/cmd_bootstrap.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "io/ioutil" "os" "os/exec" "strings" @@ -151,7 +150,7 @@ func prepareHostsFile() { modified = true } - fd, err := ioutil.TempFile("/tmp/authelia/", "hosts") + fd, err := os.CreateTemp("/tmp/authelia/", "hosts") if err != nil { panic(err) } @@ -174,7 +173,7 @@ func prepareHostsFile() { // ReadHostsFile reads the hosts file. func readHostsFile() ([]byte, error) { - bs, err := ioutil.ReadFile("/etc/hosts") + bs, err := os.ReadFile("/etc/hosts") if err != nil { return nil, err } diff --git a/cmd/authelia-scripts/cmd_suites.go b/cmd/authelia-scripts/cmd_suites.go index ea490d894..e78c23cbd 100644 --- a/cmd/authelia-scripts/cmd_suites.go +++ b/cmd/authelia-scripts/cmd_suites.go @@ -3,7 +3,6 @@ package main import ( "errors" "fmt" - "io/ioutil" "os" "os/signal" "sort" @@ -260,7 +259,7 @@ func getRunningSuite() (string, error) { return "", nil } - b, err := ioutil.ReadFile(runningSuiteFile) + b, err := os.ReadFile(runningSuiteFile) return string(b), err } diff --git a/cmd/authelia-suites/main.go b/cmd/authelia-suites/main.go index 5bb1bb587..f2a287673 100644 --- a/cmd/authelia-suites/main.go +++ b/cmd/authelia-suites/main.go @@ -2,7 +2,6 @@ package main import ( "bufio" - "io/ioutil" "os" "path/filepath" "strings" @@ -64,7 +63,7 @@ func main() { } func createRunningSuiteFile(suite string) error { - return ioutil.WriteFile(runningSuiteFile, []byte(suite), 0600) + return os.WriteFile(runningSuiteFile, []byte(suite), 0600) } func removeRunningSuiteFile() error { diff --git a/internal/authentication/file_user_provider.go b/internal/authentication/file_user_provider.go index 548c19c7a..6ee607a74 100644 --- a/internal/authentication/file_user_provider.go +++ b/internal/authentication/file_user_provider.go @@ -3,7 +3,6 @@ package authentication import ( _ "embed" // Embed users_database.template.yml. "fmt" - "io/ioutil" "os" "strings" "sync" @@ -107,7 +106,7 @@ func checkDatabase(path string) []error { var cfg []byte func generateDatabaseFromTemplate(path string) error { - err := ioutil.WriteFile(path, cfg, 0600) + err := os.WriteFile(path, cfg, 0600) if err != nil { return fmt.Errorf("Unable to generate %v: %v", path, err) } @@ -116,7 +115,7 @@ func generateDatabaseFromTemplate(path string) error { } func readDatabase(path string) (*DatabaseModel, error) { - content, err := ioutil.ReadFile(path) + content, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("Unable to read database from file %s: %s", path, err) } @@ -200,7 +199,7 @@ func (p *FileUserProvider) UpdatePassword(username string, newPassword string) e return err } - err = ioutil.WriteFile(p.configuration.Path, b, fileAuthenticationMode) + err = os.WriteFile(p.configuration.Path, b, fileAuthenticationMode) p.lock.Unlock() return err diff --git a/internal/authentication/file_user_provider_test.go b/internal/authentication/file_user_provider_test.go index df12e0149..844920ff1 100644 --- a/internal/authentication/file_user_provider_test.go +++ b/internal/authentication/file_user_provider_test.go @@ -1,7 +1,6 @@ package authentication import ( - "io/ioutil" "log" "os" "runtime" @@ -15,7 +14,7 @@ import ( ) func WithDatabase(content []byte, f func(path string)) { - tmpfile, err := ioutil.TempFile("", "users_database.*.yaml") + tmpfile, err := os.CreateTemp("", "users_database.*.yaml") if err != nil { log.Fatal(err) } diff --git a/internal/configuration/helpers.go b/internal/configuration/helpers.go index 43a54e179..0f5e0b215 100644 --- a/internal/configuration/helpers.go +++ b/internal/configuration/helpers.go @@ -1,7 +1,7 @@ package configuration import ( - "io/ioutil" + "os" "strings" "github.com/authelia/authelia/v4/internal/utils" @@ -46,7 +46,7 @@ func isSecretKey(key string) (isSecretKey bool) { } func loadSecret(path string) (value string, err error) { - content, err := ioutil.ReadFile(path) + content, err := os.ReadFile(path) if err != nil { return "", err } diff --git a/internal/configuration/koanf_callbacks_test.go b/internal/configuration/koanf_callbacks_test.go index ea00edaaf..107884ab7 100644 --- a/internal/configuration/koanf_callbacks_test.go +++ b/internal/configuration/koanf_callbacks_test.go @@ -2,7 +2,7 @@ package configuration import ( "fmt" - "io/ioutil" + "os" "path/filepath" "runtime" "testing" @@ -58,7 +58,7 @@ func TestKoanfSecretCallbackWithValidSecrets(t *testing.T) { "AUTHELIA__STORAGE_MYSQL_FAKE_PASSWORD": "storage.mysql.fake_password", } - dir, err := ioutil.TempDir("", "authelia-test-callbacks") + dir, err := os.MkdirTemp("", "authelia-test-callbacks") assert.NoError(t, err) secretOne := filepath.Join(dir, "secert_one") @@ -108,7 +108,7 @@ func TestKoanfSecretCallbackShouldErrorOnFSError(t *testing.T) { "AUTHELIA_THEME": "theme", } - dir, err := ioutil.TempDir("", "authelia-test-callbacks") + dir, err := os.MkdirTemp("", "authelia-test-callbacks") assert.NoError(t, err) secret := filepath.Join(dir, "inaccessible") diff --git a/internal/configuration/provider_test.go b/internal/configuration/provider_test.go index 0bc2a89f9..e708a859f 100644 --- a/internal/configuration/provider_test.go +++ b/internal/configuration/provider_test.go @@ -2,7 +2,6 @@ package configuration import ( "fmt" - "io/ioutil" "os" "path/filepath" "runtime" @@ -20,7 +19,7 @@ import ( func TestShouldErrorSecretNotExist(t *testing.T) { testReset() - dir, err := ioutil.TempDir("", "authelia-test-secret-not-exist") + dir, err := os.MkdirTemp("", "authelia-test-secret-not-exist") assert.NoError(t, err) assert.NoError(t, os.Setenv(DefaultEnvPrefix+"JWT_SECRET_FILE", filepath.Join(dir, "jwt"))) @@ -163,7 +162,7 @@ func TestShouldRaiseIOErrOnUnreadableFile(t *testing.T) { testReset() - dir, err := ioutil.TempDir("", "authelia-conf") + dir, err := os.MkdirTemp("", "authelia-conf") assert.NoError(t, err) assert.NoError(t, os.WriteFile(filepath.Join(dir, "myconf.yml"), []byte("server:\n port: 9091\n"), 0000)) @@ -264,7 +263,7 @@ func TestShouldNotReadConfigurationOnFSAccessDenied(t *testing.T) { testReset() - dir, err := ioutil.TempDir("", "authelia-config") + dir, err := os.MkdirTemp("", "authelia-config") assert.NoError(t, err) cfg := filepath.Join(dir, "config.yml") @@ -282,7 +281,7 @@ func TestShouldNotReadConfigurationOnFSAccessDenied(t *testing.T) { func TestShouldNotLoadDirectoryConfiguration(t *testing.T) { testReset() - dir, err := ioutil.TempDir("", "authelia-config") + dir, err := os.MkdirTemp("", "authelia-config") assert.NoError(t, err) val := schema.NewStructValidator() diff --git a/internal/configuration/template.go b/internal/configuration/template.go index 51511a05e..e777f4d33 100644 --- a/internal/configuration/template.go +++ b/internal/configuration/template.go @@ -3,7 +3,6 @@ package configuration import ( _ "embed" // Embed config.template.yml. "fmt" - "io/ioutil" "os" ) @@ -16,7 +15,7 @@ func EnsureConfigurationExists(path string) (created bool, err error) { _, err = os.Stat(path) if err != nil { if os.IsNotExist(err) { - err := ioutil.WriteFile(path, template, 0600) + err := os.WriteFile(path, template, 0600) if err != nil { return false, fmt.Errorf(errFmtGenerateConfiguration, err) } diff --git a/internal/configuration/template_test.go b/internal/configuration/template_test.go index 49e6eeb64..e4a02a5ee 100644 --- a/internal/configuration/template_test.go +++ b/internal/configuration/template_test.go @@ -2,7 +2,6 @@ package configuration import ( "fmt" - "io/ioutil" "os" "path/filepath" "runtime" @@ -14,7 +13,7 @@ import ( ) func TestShouldGenerateConfiguration(t *testing.T) { - dir, err := ioutil.TempDir("", "authelia-config") + dir, err := os.MkdirTemp("", "authelia-config") assert.NoError(t, err) cfg := filepath.Join(dir, "config.yml") @@ -32,7 +31,7 @@ func TestShouldNotGenerateConfigurationOnFSAccessDenied(t *testing.T) { t.Skip("skipping test due to being on windows") } - dir, err := ioutil.TempDir("", "authelia-config") + dir, err := os.MkdirTemp("", "authelia-config") assert.NoError(t, err) assert.NoError(t, os.Mkdir(filepath.Join(dir, "zero"), 0000)) @@ -45,7 +44,7 @@ func TestShouldNotGenerateConfigurationOnFSAccessDenied(t *testing.T) { } func TestShouldNotGenerateConfiguration(t *testing.T) { - dir, err := ioutil.TempDir("", "authelia-config") + dir, err := os.MkdirTemp("", "authelia-config") assert.NoError(t, err) cfg := filepath.Join(dir, "..", "not-a-dir", "config.yml") diff --git a/internal/logging/logger_test.go b/internal/logging/logger_test.go index 39633acec..71a24447c 100644 --- a/internal/logging/logger_test.go +++ b/internal/logging/logger_test.go @@ -2,7 +2,7 @@ package logging import ( "fmt" - "io/ioutil" + "io" "log" "os" "testing" @@ -14,7 +14,7 @@ import ( ) func TestShouldWriteLogsToFile(t *testing.T) { - dir, err := ioutil.TempDir("/tmp", "logs-dir") + dir, err := os.MkdirTemp("/tmp", "logs-dir") if err != nil { log.Fatal(err) } @@ -30,14 +30,14 @@ func TestShouldWriteLogsToFile(t *testing.T) { f, err := os.OpenFile(path, os.O_RDONLY, 0) require.NoError(t, err) - b, err := ioutil.ReadAll(f) + b, err := io.ReadAll(f) require.NoError(t, err) assert.Contains(t, string(b), "level=info msg=\"This is a test\"\n") } func TestShouldWriteLogsToFileAndStdout(t *testing.T) { - dir, err := ioutil.TempDir("/tmp", "logs-dir") + dir, err := os.MkdirTemp("/tmp", "logs-dir") if err != nil { log.Fatal(err) } @@ -53,14 +53,14 @@ func TestShouldWriteLogsToFileAndStdout(t *testing.T) { f, err := os.OpenFile(path, os.O_RDONLY, 0) require.NoError(t, err) - b, err := ioutil.ReadAll(f) + b, err := io.ReadAll(f) require.NoError(t, err) assert.Contains(t, string(b), "level=info msg=\"This is a test\"\n") } func TestShouldFormatLogsAsJSON(t *testing.T) { - dir, err := ioutil.TempDir("/tmp", "logs-dir") + dir, err := os.MkdirTemp("/tmp", "logs-dir") if err != nil { log.Fatal(err) } @@ -76,7 +76,7 @@ func TestShouldFormatLogsAsJSON(t *testing.T) { f, err := os.OpenFile(path, os.O_RDONLY, 0) require.NoError(t, err) - b, err := ioutil.ReadAll(f) + b, err := io.ReadAll(f) require.NoError(t, err) assert.Contains(t, string(b), "{\"level\":\"info\",\"msg\":\"This is a test\",") diff --git a/internal/notification/file_notifier.go b/internal/notification/file_notifier.go index db287a6de..fe3c555fb 100644 --- a/internal/notification/file_notifier.go +++ b/internal/notification/file_notifier.go @@ -2,7 +2,6 @@ package notification import ( "fmt" - "io/ioutil" "os" "path/filepath" "time" @@ -39,7 +38,7 @@ func (n *FileNotifier) StartupCheck() (err error) { } } - if err := ioutil.WriteFile(n.path, []byte(""), fileNotifierMode); err != nil { + if err := os.WriteFile(n.path, []byte(""), fileNotifierMode); err != nil { return err } @@ -50,7 +49,7 @@ func (n *FileNotifier) StartupCheck() (err error) { func (n *FileNotifier) Send(recipient, subject, body, _ string) error { content := fmt.Sprintf("Date: %s\nRecipient: %s\nSubject: %s\nBody: %s", time.Now(), recipient, subject, body) - err := ioutil.WriteFile(n.path, []byte(content), fileNotifierMode) + err := os.WriteFile(n.path, []byte(content), fileNotifierMode) if err != nil { return err diff --git a/internal/server/template.go b/internal/server/template.go index 907344c6f..a95927563 100644 --- a/internal/server/template.go +++ b/internal/server/template.go @@ -2,7 +2,7 @@ package server import ( "fmt" - "io/ioutil" + "io" "os" "path/filepath" "text/template" @@ -24,7 +24,7 @@ func ServeTemplatedFile(publicDir, file, assetPath, duoSelfEnrollment, rememberM logger.Fatalf("Unable to open %s: %s", file, err) } - b, err := ioutil.ReadAll(a) + b, err := io.ReadAll(a) if err != nil { logger.Fatalf("Unable to read %s: %s", file, err) } diff --git a/internal/suites/action_http.go b/internal/suites/action_http.go index 3f10965bf..2be335e2a 100644 --- a/internal/suites/action_http.go +++ b/internal/suites/action_http.go @@ -1,7 +1,7 @@ package suites import ( - "io/ioutil" + "io" "net/http" "testing" @@ -18,7 +18,7 @@ func doHTTPGetQuery(t *testing.T, url string) []byte { assert.NoError(t, err) defer resp.Body.Close() - body, _ := ioutil.ReadAll(resp.Body) + body, _ := io.ReadAll(resp.Body) return body } diff --git a/internal/suites/suite_standalone.go b/internal/suites/suite_standalone.go index 76e861dae..c6596ed1c 100644 --- a/internal/suites/suite_standalone.go +++ b/internal/suites/suite_standalone.go @@ -2,7 +2,6 @@ package suites import ( "fmt" - "io/ioutil" "os" "time" ) @@ -11,8 +10,8 @@ var standaloneSuiteName = "Standalone" func init() { _ = os.MkdirAll("/tmp/authelia/StandaloneSuite/", 0700) - _ = ioutil.WriteFile("/tmp/authelia/StandaloneSuite/jwt", []byte("very_important_secret"), 0600) //nolint:gosec - _ = ioutil.WriteFile("/tmp/authelia/StandaloneSuite/session", []byte("unsecure_session_secret"), 0600) //nolint:gosec + _ = os.WriteFile("/tmp/authelia/StandaloneSuite/jwt", []byte("very_important_secret"), 0600) + _ = os.WriteFile("/tmp/authelia/StandaloneSuite/session", []byte("unsecure_session_secret"), 0600) dockerEnvironment := NewDockerEnvironment([]string{ "internal/suites/docker-compose.yml", diff --git a/internal/suites/suite_standalone_test.go b/internal/suites/suite_standalone_test.go index dc76412d8..b16e76b30 100644 --- a/internal/suites/suite_standalone_test.go +++ b/internal/suites/suite_standalone_test.go @@ -3,7 +3,7 @@ package suites import ( "context" "fmt" - "io/ioutil" + "io" "log" "net/http" "net/url" @@ -165,7 +165,7 @@ func (s *StandaloneSuite) TestShouldRespectMethodsACL() { res, err := client.Do(req) s.Assert().NoError(err) s.Assert().Equal(res.StatusCode, 302) - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) s.Assert().NoError(err) urlEncodedAdminURL := url.QueryEscape(SecureBaseURL + "/") @@ -191,7 +191,7 @@ func (s *StandaloneSuite) TestShouldRespondWithCorrectStatusCode() { res, err := client.Do(req) s.Assert().NoError(err) s.Assert().Equal(res.StatusCode, 302) - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) s.Assert().NoError(err) urlEncodedAdminURL := url.QueryEscape(SecureBaseURL + "/") @@ -202,7 +202,7 @@ func (s *StandaloneSuite) TestShouldRespondWithCorrectStatusCode() { res, err = client.Do(req) s.Assert().NoError(err) s.Assert().Equal(res.StatusCode, 303) - body, err = ioutil.ReadAll(res.Body) + body, err = io.ReadAll(res.Body) s.Assert().NoError(err) urlEncodedAdminURL = url.QueryEscape(SecureBaseURL + "/") @@ -221,7 +221,7 @@ func (s *StandaloneSuite) TestShouldVerifyAPIVerifyUnauthorized() { res, err := client.Do(req) s.Assert().NoError(err) s.Assert().Equal(res.StatusCode, 401) - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) s.Assert().NoError(err) s.Assert().Equal("Unauthorized", string(body)) } @@ -238,7 +238,7 @@ func (s *StandaloneSuite) TestShouldVerifyAPIVerifyRedirectFromXOriginalURL() { res, err := client.Do(req) s.Assert().NoError(err) s.Assert().Equal(res.StatusCode, 302) - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) s.Assert().NoError(err) urlEncodedAdminURL := url.QueryEscape(AdminBaseURL) @@ -257,7 +257,7 @@ func (s *StandaloneSuite) TestShouldVerifyAPIVerifyRedirectFromXOriginalHostURI( res, err := client.Do(req) s.Assert().NoError(err) s.Assert().Equal(res.StatusCode, 302) - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) s.Assert().NoError(err) urlEncodedAdminURL := url.QueryEscape(SecureBaseURL + "/") diff --git a/internal/suites/utils.go b/internal/suites/utils.go index 0225a5b65..1c6b66b2a 100644 --- a/internal/suites/utils.go +++ b/internal/suites/utils.go @@ -3,7 +3,6 @@ package suites import ( "context" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -37,7 +36,7 @@ func (rs *RodSession) collectCoverage(page *rod.Page) { _ = os.MkdirAll(coverageDir, 0775) if coverageData != "" { - err = ioutil.WriteFile(fmt.Sprintf("%s/coverage-%d.json", coverageDir, now.Unix()), []byte(coverageData), 0664) //nolint:gosec + err = os.WriteFile(fmt.Sprintf("%s/coverage-%d.json", coverageDir, now.Unix()), []byte(coverageData), 0664) //nolint:gosec if err != nil { log.Fatal(err) } @@ -86,7 +85,7 @@ func fixCoveragePath(path string, file os.FileInfo, err error) error { } if coverage { - read, err := ioutil.ReadFile(path) + read, err := os.ReadFile(path) if err != nil { return err } @@ -95,7 +94,7 @@ func fixCoveragePath(path string, file os.FileInfo, err error) error { ciPath := strings.TrimSuffix(wd, "internal/suites") content := strings.ReplaceAll(string(read), "/node/src/app/", ciPath+"web/") - err = ioutil.WriteFile(path, []byte(content), 0) + err = os.WriteFile(path, []byte(content), 0) if err != nil { return err } diff --git a/internal/utils/certificates.go b/internal/utils/certificates.go index e038d5794..523dd28cd 100644 --- a/internal/utils/certificates.go +++ b/internal/utils/certificates.go @@ -4,7 +4,7 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" + "os" "path/filepath" "strings" @@ -40,7 +40,7 @@ func NewX509CertPool(directory string) (certPool *x509.CertPool, warnings []erro logger.Tracef("Starting scan of directory %s for certificates", directory) if directory != "" { - certsFileInfo, err := ioutil.ReadDir(directory) + certsFileInfo, err := os.ReadDir(directory) if err != nil { errors = append(errors, fmt.Errorf("could not read certificates from directory %v", err)) } else { @@ -52,7 +52,7 @@ func NewX509CertPool(directory string) (certPool *x509.CertPool, warnings []erro logger.Tracef("Found possible cert %s, attempting to add it to the pool", certPath) - certBytes, err := ioutil.ReadFile(certPath) + certBytes, err := os.ReadFile(certPath) if err != nil { errors = append(errors, fmt.Errorf("could not read certificate %v", err)) } else if ok := certPool.AppendCertsFromPEM(certBytes); !ok { diff --git a/internal/utils/hashing_test.go b/internal/utils/hashing_test.go index 4436d41f3..81ed04299 100644 --- a/internal/utils/hashing_test.go +++ b/internal/utils/hashing_test.go @@ -2,7 +2,6 @@ package utils import ( "fmt" - "io/ioutil" "os" "path/filepath" "testing" @@ -31,7 +30,7 @@ func TestShouldHashString(t *testing.T) { } func TestShouldHashPath(t *testing.T) { - dir, err := ioutil.TempDir("", "authelia-hashing") + dir, err := os.MkdirTemp("", "authelia-hashing") assert.NoError(t, err) err = os.WriteFile(filepath.Join(dir, "myfile"), []byte("output\n"), 0600)