refactor: remove ioutil (#2635)

Was deprecated in 1.16 and has more performant options available.
pull/2653/head
James Elliott 2021-12-02 00:14:15 +11:00 committed by GitHub
parent 8a12af97ab
commit 7df242f1e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 50 additions and 62 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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 {

View File

@ -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

View File

@ -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)
}

View File

@ -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
}

View File

@ -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")

View File

@ -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()

View File

@ -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)
}

View File

@ -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")

View File

@ -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\",")

View File

@ -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

View File

@ -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)
}

View File

@ -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
}

View File

@ -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",

View File

@ -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 + "/")

View File

@ -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 != "<nil>" {
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
}

View File

@ -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 {

View File

@ -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)