From 30d509a0b664eb476e63a43307a4df6654dbe766 Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Mon, 9 Oct 2023 11:30:42 +0800 Subject: [PATCH] chore: remove refs to deprecated io/ioutil Signed-off-by: guoguangwu --- cmd/nebula-cert/ca.go | 9 ++++----- cmd/nebula-cert/ca_test.go | 11 +++++------ cmd/nebula-cert/keygen.go | 5 ++--- cmd/nebula-cert/keygen_test.go | 9 ++++----- cmd/nebula-cert/print.go | 5 ++--- cmd/nebula-cert/print_test.go | 3 +-- cmd/nebula-cert/sign.go | 13 ++++++------- cmd/nebula-cert/sign_test.go | 23 +++++++++++------------ cmd/nebula-cert/verify.go | 5 ++--- cmd/nebula-cert/verify_test.go | 5 ++--- config/config.go | 3 +-- config/config_test.go | 15 +++++++-------- ssh.go | 3 +-- test/logger.go | 4 ++-- 14 files changed, 50 insertions(+), 63 deletions(-) diff --git a/cmd/nebula-cert/ca.go b/cmd/nebula-cert/ca.go index e9ad3cb14..c538b93aa 100644 --- a/cmd/nebula-cert/ca.go +++ b/cmd/nebula-cert/ca.go @@ -7,7 +7,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "math" "net" "os" @@ -219,9 +218,9 @@ func ca(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) error return fmt.Errorf("error while encrypting out-key: %s", err) } - err = ioutil.WriteFile(*cf.outKeyPath, b, 0600) + err = os.WriteFile(*cf.outKeyPath, b, 0600) } else { - err = ioutil.WriteFile(*cf.outKeyPath, cert.MarshalSigningPrivateKey(curve, rawPriv), 0600) + err = os.WriteFile(*cf.outKeyPath, cert.MarshalSigningPrivateKey(curve, rawPriv), 0600) } if err != nil { @@ -233,7 +232,7 @@ func ca(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) error return fmt.Errorf("error while marshalling certificate: %s", err) } - err = ioutil.WriteFile(*cf.outCertPath, b, 0600) + err = os.WriteFile(*cf.outCertPath, b, 0600) if err != nil { return fmt.Errorf("error while writing out-crt: %s", err) } @@ -244,7 +243,7 @@ func ca(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) error return fmt.Errorf("error while generating qr code: %s", err) } - err = ioutil.WriteFile(*cf.outQRPath, b, 0600) + err = os.WriteFile(*cf.outQRPath, b, 0600) if err != nil { return fmt.Errorf("error while writing out-qr: %s", err) } diff --git a/cmd/nebula-cert/ca_test.go b/cmd/nebula-cert/ca_test.go index ae79baf89..3a534051d 100644 --- a/cmd/nebula-cert/ca_test.go +++ b/cmd/nebula-cert/ca_test.go @@ -7,7 +7,6 @@ import ( "bytes" "encoding/pem" "errors" - "io/ioutil" "os" "strings" "testing" @@ -107,7 +106,7 @@ func Test_ca(t *testing.T) { assert.Equal(t, "", eb.String()) // create temp key file - keyF, err := ioutil.TempFile("", "test.key") + keyF, err := os.CreateTemp("", "test.key") assert.Nil(t, err) os.Remove(keyF.Name()) @@ -120,7 +119,7 @@ func Test_ca(t *testing.T) { assert.Equal(t, "", eb.String()) // create temp cert file - crtF, err := ioutil.TempFile("", "test.crt") + crtF, err := os.CreateTemp("", "test.crt") assert.Nil(t, err) os.Remove(crtF.Name()) os.Remove(keyF.Name()) @@ -134,13 +133,13 @@ func Test_ca(t *testing.T) { assert.Equal(t, "", eb.String()) // read cert and key files - rb, _ := ioutil.ReadFile(keyF.Name()) + rb, _ := os.ReadFile(keyF.Name()) lKey, b, err := cert.UnmarshalEd25519PrivateKey(rb) assert.Len(t, b, 0) assert.Nil(t, err) assert.Len(t, lKey, 64) - rb, _ = ioutil.ReadFile(crtF.Name()) + rb, _ = os.ReadFile(crtF.Name()) lCrt, b, err := cert.UnmarshalNebulaCertificateFromPEM(rb) assert.Len(t, b, 0) assert.Nil(t, err) @@ -166,7 +165,7 @@ func Test_ca(t *testing.T) { assert.Equal(t, "", eb.String()) // read encrypted key file and verify default params - rb, _ = ioutil.ReadFile(keyF.Name()) + rb, _ = os.ReadFile(keyF.Name()) k, _ := pem.Decode(rb) ned, err := cert.UnmarshalNebulaEncryptedData(k.Bytes) assert.Nil(t, err) diff --git a/cmd/nebula-cert/keygen.go b/cmd/nebula-cert/keygen.go index 0016fc188..d94cbf145 100644 --- a/cmd/nebula-cert/keygen.go +++ b/cmd/nebula-cert/keygen.go @@ -4,7 +4,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "os" "github.com/slackhq/nebula/cert" @@ -54,12 +53,12 @@ func keygen(args []string, out io.Writer, errOut io.Writer) error { return fmt.Errorf("invalid curve: %s", *cf.curve) } - err = ioutil.WriteFile(*cf.outKeyPath, cert.MarshalPrivateKey(curve, rawPriv), 0600) + err = os.WriteFile(*cf.outKeyPath, cert.MarshalPrivateKey(curve, rawPriv), 0600) if err != nil { return fmt.Errorf("error while writing out-key: %s", err) } - err = ioutil.WriteFile(*cf.outPubPath, cert.MarshalPublicKey(curve, pub), 0600) + err = os.WriteFile(*cf.outPubPath, cert.MarshalPublicKey(curve, pub), 0600) if err != nil { return fmt.Errorf("error while writing out-pub: %s", err) } diff --git a/cmd/nebula-cert/keygen_test.go b/cmd/nebula-cert/keygen_test.go index 1480e5a93..9a3b3f3bb 100644 --- a/cmd/nebula-cert/keygen_test.go +++ b/cmd/nebula-cert/keygen_test.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "io/ioutil" "os" "testing" @@ -54,7 +53,7 @@ func Test_keygen(t *testing.T) { assert.Equal(t, "", eb.String()) // create temp key file - keyF, err := ioutil.TempFile("", "test.key") + keyF, err := os.CreateTemp("", "test.key") assert.Nil(t, err) defer os.Remove(keyF.Name()) @@ -67,7 +66,7 @@ func Test_keygen(t *testing.T) { assert.Equal(t, "", eb.String()) // create temp pub file - pubF, err := ioutil.TempFile("", "test.pub") + pubF, err := os.CreateTemp("", "test.pub") assert.Nil(t, err) defer os.Remove(pubF.Name()) @@ -80,13 +79,13 @@ func Test_keygen(t *testing.T) { assert.Equal(t, "", eb.String()) // read cert and key files - rb, _ := ioutil.ReadFile(keyF.Name()) + rb, _ := os.ReadFile(keyF.Name()) lKey, b, err := cert.UnmarshalX25519PrivateKey(rb) assert.Len(t, b, 0) assert.Nil(t, err) assert.Len(t, lKey, 32) - rb, _ = ioutil.ReadFile(pubF.Name()) + rb, _ = os.ReadFile(pubF.Name()) lPub, b, err := cert.UnmarshalX25519PublicKey(rb) assert.Len(t, b, 0) assert.Nil(t, err) diff --git a/cmd/nebula-cert/print.go b/cmd/nebula-cert/print.go index 222dbc058..746d6a3ab 100644 --- a/cmd/nebula-cert/print.go +++ b/cmd/nebula-cert/print.go @@ -5,7 +5,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "os" "strings" @@ -41,7 +40,7 @@ func printCert(args []string, out io.Writer, errOut io.Writer) error { return err } - rawCert, err := ioutil.ReadFile(*pf.path) + rawCert, err := os.ReadFile(*pf.path) if err != nil { return fmt.Errorf("unable to read cert; %s", err) } @@ -87,7 +86,7 @@ func printCert(args []string, out io.Writer, errOut io.Writer) error { return fmt.Errorf("error while generating qr code: %s", err) } - err = ioutil.WriteFile(*pf.outQRPath, b, 0600) + err = os.WriteFile(*pf.outQRPath, b, 0600) if err != nil { return fmt.Errorf("error while writing out-qr: %s", err) } diff --git a/cmd/nebula-cert/print_test.go b/cmd/nebula-cert/print_test.go index eb117f123..9fa8a5492 100644 --- a/cmd/nebula-cert/print_test.go +++ b/cmd/nebula-cert/print_test.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "io/ioutil" "os" "testing" "time" @@ -54,7 +53,7 @@ func Test_printCert(t *testing.T) { // invalid cert at path ob.Reset() eb.Reset() - tf, err := ioutil.TempFile("", "print-cert") + tf, err := os.CreateTemp("", "print-cert") assert.Nil(t, err) defer os.Remove(tf.Name()) diff --git a/cmd/nebula-cert/sign.go b/cmd/nebula-cert/sign.go index 9938401a0..35d644689 100644 --- a/cmd/nebula-cert/sign.go +++ b/cmd/nebula-cert/sign.go @@ -6,7 +6,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "net" "os" "strings" @@ -73,7 +72,7 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) return newHelpErrorf("cannot set both -in-pub and -out-key") } - rawCAKey, err := ioutil.ReadFile(*sf.caKeyPath) + rawCAKey, err := os.ReadFile(*sf.caKeyPath) if err != nil { return fmt.Errorf("error while reading ca-key: %s", err) } @@ -112,7 +111,7 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) return fmt.Errorf("error while parsing ca-key: %s", err) } - rawCACert, err := ioutil.ReadFile(*sf.caCertPath) + rawCACert, err := os.ReadFile(*sf.caCertPath) if err != nil { return fmt.Errorf("error while reading ca-crt: %s", err) } @@ -178,7 +177,7 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) var pub, rawPriv []byte if *sf.inPubPath != "" { - rawPub, err := ioutil.ReadFile(*sf.inPubPath) + rawPub, err := os.ReadFile(*sf.inPubPath) if err != nil { return fmt.Errorf("error while reading in-pub: %s", err) } @@ -235,7 +234,7 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) return fmt.Errorf("refusing to overwrite existing key: %s", *sf.outKeyPath) } - err = ioutil.WriteFile(*sf.outKeyPath, cert.MarshalPrivateKey(curve, rawPriv), 0600) + err = os.WriteFile(*sf.outKeyPath, cert.MarshalPrivateKey(curve, rawPriv), 0600) if err != nil { return fmt.Errorf("error while writing out-key: %s", err) } @@ -246,7 +245,7 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) return fmt.Errorf("error while marshalling certificate: %s", err) } - err = ioutil.WriteFile(*sf.outCertPath, b, 0600) + err = os.WriteFile(*sf.outCertPath, b, 0600) if err != nil { return fmt.Errorf("error while writing out-crt: %s", err) } @@ -257,7 +256,7 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) return fmt.Errorf("error while generating qr code: %s", err) } - err = ioutil.WriteFile(*sf.outQRPath, b, 0600) + err = os.WriteFile(*sf.outQRPath, b, 0600) if err != nil { return fmt.Errorf("error while writing out-qr: %s", err) } diff --git a/cmd/nebula-cert/sign_test.go b/cmd/nebula-cert/sign_test.go index 7018fd207..adf83a267 100644 --- a/cmd/nebula-cert/sign_test.go +++ b/cmd/nebula-cert/sign_test.go @@ -7,7 +7,6 @@ import ( "bytes" "crypto/rand" "errors" - "io/ioutil" "os" "testing" "time" @@ -104,7 +103,7 @@ func Test_signCert(t *testing.T) { // failed to unmarshal key ob.Reset() eb.Reset() - caKeyF, err := ioutil.TempFile("", "sign-cert.key") + caKeyF, err := os.CreateTemp("", "sign-cert.key") assert.Nil(t, err) defer os.Remove(caKeyF.Name()) @@ -128,7 +127,7 @@ func Test_signCert(t *testing.T) { // failed to unmarshal cert ob.Reset() eb.Reset() - caCrtF, err := ioutil.TempFile("", "sign-cert.crt") + caCrtF, err := os.CreateTemp("", "sign-cert.crt") assert.Nil(t, err) defer os.Remove(caCrtF.Name()) @@ -159,7 +158,7 @@ func Test_signCert(t *testing.T) { // failed to unmarshal pub ob.Reset() eb.Reset() - inPubF, err := ioutil.TempFile("", "in.pub") + inPubF, err := os.CreateTemp("", "in.pub") assert.Nil(t, err) defer os.Remove(inPubF.Name()) @@ -206,7 +205,7 @@ func Test_signCert(t *testing.T) { // mismatched ca key _, caPriv2, _ := ed25519.GenerateKey(rand.Reader) - caKeyF2, err := ioutil.TempFile("", "sign-cert-2.key") + caKeyF2, err := os.CreateTemp("", "sign-cert-2.key") assert.Nil(t, err) defer os.Remove(caKeyF2.Name()) caKeyF2.Write(cert.MarshalEd25519PrivateKey(caPriv2)) @@ -227,7 +226,7 @@ func Test_signCert(t *testing.T) { assert.Empty(t, eb.String()) // create temp key file - keyF, err := ioutil.TempFile("", "test.key") + keyF, err := os.CreateTemp("", "test.key") assert.Nil(t, err) os.Remove(keyF.Name()) @@ -241,7 +240,7 @@ func Test_signCert(t *testing.T) { os.Remove(keyF.Name()) // create temp cert file - crtF, err := ioutil.TempFile("", "test.crt") + crtF, err := os.CreateTemp("", "test.crt") assert.Nil(t, err) os.Remove(crtF.Name()) @@ -254,13 +253,13 @@ func Test_signCert(t *testing.T) { assert.Empty(t, eb.String()) // read cert and key files - rb, _ := ioutil.ReadFile(keyF.Name()) + rb, _ := os.ReadFile(keyF.Name()) lKey, b, err := cert.UnmarshalX25519PrivateKey(rb) assert.Len(t, b, 0) assert.Nil(t, err) assert.Len(t, lKey, 32) - rb, _ = ioutil.ReadFile(crtF.Name()) + rb, _ = os.ReadFile(crtF.Name()) lCrt, b, err := cert.UnmarshalNebulaCertificateFromPEM(rb) assert.Len(t, b, 0) assert.Nil(t, err) @@ -296,7 +295,7 @@ func Test_signCert(t *testing.T) { assert.Empty(t, eb.String()) // read cert file and check pub key matches in-pub - rb, _ = ioutil.ReadFile(crtF.Name()) + rb, _ = os.ReadFile(crtF.Name()) lCrt, b, err = cert.UnmarshalNebulaCertificateFromPEM(rb) assert.Len(t, b, 0) assert.Nil(t, err) @@ -348,11 +347,11 @@ func Test_signCert(t *testing.T) { ob.Reset() eb.Reset() - caKeyF, err = ioutil.TempFile("", "sign-cert.key") + caKeyF, err = os.CreateTemp("", "sign-cert.key") assert.Nil(t, err) defer os.Remove(caKeyF.Name()) - caCrtF, err = ioutil.TempFile("", "sign-cert.crt") + caCrtF, err = os.CreateTemp("", "sign-cert.crt") assert.Nil(t, err) defer os.Remove(caCrtF.Name()) diff --git a/cmd/nebula-cert/verify.go b/cmd/nebula-cert/verify.go index 51b9a9303..c9559136f 100644 --- a/cmd/nebula-cert/verify.go +++ b/cmd/nebula-cert/verify.go @@ -4,7 +4,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "os" "strings" "time" @@ -40,7 +39,7 @@ func verify(args []string, out io.Writer, errOut io.Writer) error { return err } - rawCACert, err := ioutil.ReadFile(*vf.caPath) + rawCACert, err := os.ReadFile(*vf.caPath) if err != nil { return fmt.Errorf("error while reading ca: %s", err) } @@ -57,7 +56,7 @@ func verify(args []string, out io.Writer, errOut io.Writer) error { } } - rawCert, err := ioutil.ReadFile(*vf.certPath) + rawCert, err := os.ReadFile(*vf.certPath) if err != nil { return fmt.Errorf("unable to read crt; %s", err) } diff --git a/cmd/nebula-cert/verify_test.go b/cmd/nebula-cert/verify_test.go index 25014fda3..f0f4c78dd 100644 --- a/cmd/nebula-cert/verify_test.go +++ b/cmd/nebula-cert/verify_test.go @@ -3,7 +3,6 @@ package main import ( "bytes" "crypto/rand" - "io/ioutil" "os" "testing" "time" @@ -56,7 +55,7 @@ func Test_verify(t *testing.T) { // invalid ca at path ob.Reset() eb.Reset() - caFile, err := ioutil.TempFile("", "verify-ca") + caFile, err := os.CreateTemp("", "verify-ca") assert.Nil(t, err) defer os.Remove(caFile.Name()) @@ -92,7 +91,7 @@ func Test_verify(t *testing.T) { // invalid crt at path ob.Reset() eb.Reset() - certFile, err := ioutil.TempFile("", "verify-cert") + certFile, err := os.CreateTemp("", "verify-cert") assert.Nil(t, err) defer os.Remove(certFile.Name()) diff --git a/config/config.go b/config/config.go index bc3818dd4..743d8ce3e 100644 --- a/config/config.go +++ b/config/config.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "io/ioutil" "math" "os" "os/signal" @@ -358,7 +357,7 @@ func (c *C) parse() error { var m map[interface{}]interface{} for _, path := range c.files { - b, err := ioutil.ReadFile(path) + b, err := os.ReadFile(path) if err != nil { return err } diff --git a/config/config_test.go b/config/config_test.go index 1001f8d06..fa9439302 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,7 +1,6 @@ package config import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -16,10 +15,10 @@ import ( func TestConfig_Load(t *testing.T) { l := test.NewLogger() - dir, err := ioutil.TempDir("", "config-test") + dir, err := os.MkdirTemp("", "config-test") // invalid yaml c := NewC(l) - ioutil.WriteFile(filepath.Join(dir, "01.yaml"), []byte(" invalid yaml"), 0644) + os.WriteFile(filepath.Join(dir, "01.yaml"), []byte(" invalid yaml"), 0644) assert.EqualError(t, c.Load(dir), "yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `invalid...` into map[interface {}]interface {}") // simple multi config merge @@ -29,8 +28,8 @@ func TestConfig_Load(t *testing.T) { assert.Nil(t, err) - ioutil.WriteFile(filepath.Join(dir, "01.yaml"), []byte("outer:\n inner: hi"), 0644) - ioutil.WriteFile(filepath.Join(dir, "02.yml"), []byte("outer:\n inner: override\nnew: hi"), 0644) + os.WriteFile(filepath.Join(dir, "01.yaml"), []byte("outer:\n inner: hi"), 0644) + os.WriteFile(filepath.Join(dir, "02.yml"), []byte("outer:\n inner: override\nnew: hi"), 0644) assert.Nil(t, c.Load(dir)) expected := map[interface{}]interface{}{ "outer": map[interface{}]interface{}{ @@ -120,9 +119,9 @@ func TestConfig_HasChanged(t *testing.T) { func TestConfig_ReloadConfig(t *testing.T) { l := test.NewLogger() done := make(chan bool, 1) - dir, err := ioutil.TempDir("", "config-test") + dir, err := os.MkdirTemp("", "config-test") assert.Nil(t, err) - ioutil.WriteFile(filepath.Join(dir, "01.yaml"), []byte("outer:\n inner: hi"), 0644) + os.WriteFile(filepath.Join(dir, "01.yaml"), []byte("outer:\n inner: hi"), 0644) c := NewC(l) assert.Nil(t, c.Load(dir)) @@ -131,7 +130,7 @@ func TestConfig_ReloadConfig(t *testing.T) { assert.False(t, c.HasChanged("outer")) assert.False(t, c.HasChanged("")) - ioutil.WriteFile(filepath.Join(dir, "01.yaml"), []byte("outer:\n inner: ho"), 0644) + os.WriteFile(filepath.Join(dir, "01.yaml"), []byte("outer:\n inner: ho"), 0644) c.RegisterReloadCallback(func(c *C) { done <- true diff --git a/ssh.go b/ssh.go index 30f9aea3d..c410e0101 100644 --- a/ssh.go +++ b/ssh.go @@ -6,7 +6,6 @@ import ( "errors" "flag" "fmt" - "io/ioutil" "net" "os" "reflect" @@ -96,7 +95,7 @@ func configSSH(l *logrus.Logger, ssh *sshd.SSHServer, c *config.C) (func(), erro return nil, fmt.Errorf("sshd.host_key must be provided") } - hostKeyBytes, err := ioutil.ReadFile(hostKeyFile) + hostKeyBytes, err := os.ReadFile(hostKeyFile) if err != nil { return nil, fmt.Errorf("error while loading sshd.host_key file: %s", err) } diff --git a/test/logger.go b/test/logger.go index 197ab44d2..b5a717d82 100644 --- a/test/logger.go +++ b/test/logger.go @@ -1,7 +1,7 @@ package test import ( - "io/ioutil" + "io" "os" "github.com/sirupsen/logrus" @@ -12,7 +12,7 @@ func NewLogger() *logrus.Logger { v := os.Getenv("TEST_LOGS") if v == "" { - l.SetOutput(ioutil.Discard) + l.SetOutput(io.Discard) return l }