Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"os"
Expand Down Expand Up @@ -95,18 +94,23 @@ func TestNewClientWithFileConfig(t *testing.T) {
}
}

tmpfile, err := ioutil.TempFile("", "")
tmpFile, err := os.CreateTemp("", "")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatal(err)
}
}(tmpFile.Name())

err = ioutil.WriteFile(tmpfile.Name(), []byte("test_mode = true"), 0644)
err = os.WriteFile(tmpFile.Name(), []byte("test_mode = true"), 0644)
if err != nil {
t.Fatal(err)
}

cfg, err := LoadConfigFromFile(tmpfile.Name(), "")
cfg, err := LoadConfigFromFile(tmpFile.Name(), "")
if err != nil {
t.Fatal(err)
}
Expand Down
28 changes: 22 additions & 6 deletions cmd/vcert/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
t "log"
"os"
"testing"
Expand Down Expand Up @@ -54,13 +54,21 @@ func TestWriteOutKeyAndCsr(t *testing.T) {
if csr == nil {
t.Fatalf("CSR should not be nil")
}
temp, err := ioutil.TempFile(os.TempDir(), "vcertTest")
temp, err := os.CreateTemp(os.TempDir(), "vcertTest")
if err != nil {
t.Fatalf("%s", err)
}
defer os.Remove(temp.Name())
defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatal(err)
}
}(temp.Name())
fileName := temp.Name()
temp.Close()
err = temp.Close()
if err != nil {
t.Fatalf("%s", err)
}
cf.file = fileName
err = writeOutKeyAndCsr(commandGenCSRName, cf, key, csr)
if err != nil {
Expand Down Expand Up @@ -101,7 +109,11 @@ func TestGenerateCsrJson(t *testing.T) {
}

//Reads the csr file to validate the json format
csrData, err := ioutil.ReadFile(csrName)
file, err := os.Open(csrName)
if err != nil {
t.Fatalf("%s", err)
}
csrData, err := io.ReadAll(file)
if err != nil {
t.Fatalf("%s", err)
}
Expand All @@ -115,7 +127,11 @@ func TestGenerateCsrJson(t *testing.T) {
}

//Reads the private key file to validate the json format
keyData, err := ioutil.ReadFile(keyName)
file, err = os.Open(keyName)
if err != nil {
t.Fatalf("%s", err)
}
keyData, err := io.ReadAll(file)
if err != nil {
t.Fatalf("%s", err)
}
Expand Down
61 changes: 47 additions & 14 deletions cmd/vcert/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"crypto/rsa"
"crypto/x509"
"fmt"
"io/ioutil"
"math/big"
"net"
"net/url"
Expand Down Expand Up @@ -664,13 +663,21 @@ func TestGenerateCertCSRFileRequest(t *testing.T) {

func TestGetFileWriter(t *testing.T) {
//set the pem file var so we get a file handle
temp, err := ioutil.TempFile(os.TempDir(), "vcertTest")
temp, err := os.CreateTemp(os.TempDir(), "vcertTest")
if err != nil {
t.Fatalf("Failed to create temp file for testing getFileWriter. Error: %s", err)
}
defer os.Remove(temp.Name())
defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatal(err)
}
}(temp.Name())
fileName := temp.Name()
temp.Close()
err = temp.Close()
if err != nil {
t.Fatalf("Failed to close temp file for testing getFileWriter. Error: %s", err)
}
writer := getFileWriter(fileName)
f, ok := writer.(*os.File)
if ok {
Expand All @@ -684,17 +691,25 @@ func TestReadPasswordFromInputFlags(t *testing.T) {

flags = commandFlags{}

f, err := ioutil.TempFile(os.TempDir(), "vcertTest")
f, err := os.CreateTemp(os.TempDir(), "vcertTest")
if err != nil {
t.Fatalf("Failed to create temp file for testing readPasswordsFromInputFlags. Error: %s", err)
}
tempFileName := f.Name()
defer os.Remove(tempFileName)
defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatal(err)
}
}(tempFileName)
_, err = f.WriteString("password0\npassword1\npassword2\npassword3")
if err != nil {
t.Fatalf("Failed to write to temp file for testing readPasswordsFromInputFlags. Error: %s", err)
}
f.Close()
err = f.Close()
if err != nil {
t.Fatalf("Failed to close temp file for testing readPasswordsFromInputFlags. Error: %s", err)
}

flags.url = "https://localhost"
flags.password = fmt.Sprintf("file:%s", tempFileName)
Expand All @@ -712,12 +727,17 @@ func TestReadPasswordFromInputFlags(t *testing.T) {
}

flags.password = fmt.Sprintf("file:%s", tempFileName)
f, err = ioutil.TempFile(os.TempDir(), "vcertTest")
f, err = os.CreateTemp(os.TempDir(), "vcertTest")
if err != nil {
t.Fatalf("Failed to create temp file for testing readPasswordsFromInputFlags. Error: %s", err)
}
tempFileName = f.Name()
defer os.Remove(tempFileName)
defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatal(err)
}
}(tempFileName)
_, err = f.WriteString("key-pass")
if err != nil {
t.Fatalf("Failed to write to temp file for testing readPasswordsFromInputFlags. Error: %s", err)
Expand All @@ -742,17 +762,25 @@ func TestReadPasswordFromInputFlags(t *testing.T) {

func TestReadPasswordFromInput(t *testing.T) {

f, err := ioutil.TempFile(os.TempDir(), "vcertTest")
f, err := os.CreateTemp(os.TempDir(), "vcertTest")
if err != nil {
t.Fatalf("Failed to create temp file for testing readPasswordFromInput. Error: %s", err)
}
tempFileName := f.Name()
defer os.Remove(tempFileName)
defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatal(err)
}
}(tempFileName)
_, err = f.WriteString("password0\npassword1\npassword2\npassword3")
if err != nil {
t.Fatalf("Failed to write to temp file for testing readPasswordFromInput. Error: %s", err)
}
f.Close()
err = f.Close()
if err != nil {
t.Fatalf("Failed to close temp file for testing readPasswordFromInput. Error: %s", err)
}
pass, err := readPasswordsFromInputFlag(fmt.Sprintf("file:%s", tempFileName), 0)
if err != nil {
t.Fatalf("Failed to readPasswordFromInput. Error: %s", err)
Expand All @@ -779,12 +807,17 @@ func TestReadPasswordFromInput(t *testing.T) {
}

func TestReadPasswordFromFile(t *testing.T) {
f, err := ioutil.TempFile(os.TempDir(), "vcertTest")
f, err := os.CreateTemp(os.TempDir(), "vcertTest")
if err != nil {
t.Fatalf("Failed to create temp file for testing readPasswordFromFile. Error: %s", err)
}
tempFileName := f.Name()
defer os.Remove(tempFileName)
defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatal(err)
}
}(tempFileName)
_, err = f.WriteString("password0\npassword1\npassword2\npassword3")
if err != nil {
t.Fatalf("Failed to write to temp file for testing readPasswordFromFile. Error: %s", err)
Expand Down
14 changes: 9 additions & 5 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package vcert

import (
"io/ioutil"
"os"
"testing"
)
Expand Down Expand Up @@ -92,18 +91,23 @@ func TestLoadFromFile(t *testing.T) {
{false, invalidCloudConfig},
}
for _, test_case := range cases {
tmpfile, err := ioutil.TempFile("", "")
tmpFile, err := os.CreateTemp("", "")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Fatal(err)
}
}(tmpFile.Name())

err = ioutil.WriteFile(tmpfile.Name(), []byte(test_case.content), 0644)
err = os.WriteFile(tmpFile.Name(), []byte(test_case.content), 0644)
if err != nil {
t.Fatal(err)
}

_, err = LoadConfigFromFile(tmpfile.Name(), "")
_, err = LoadConfigFromFile(tmpFile.Name(), "")
if test_case.valid {
if err != nil {
t.Logf("config: %s", test_case.content)
Expand Down
8 changes: 6 additions & 2 deletions examples/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -52,7 +52,11 @@ func initConfig() *vcert.Config {
}
trustBundleFilePath := os.Getenv("TRUST_BUNDLE_PATH")
if trustBundleFilePath != "" {
buf, err := ioutil.ReadFile(trustBundleFilePath)
file, err := os.Open(trustBundleFilePath)
if err != nil {
panic(err)
}
buf, err := io.ReadAll(file)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also need to call Close in defer:

defer func() {
    err = file.Close()
}()

On the other places where we call .Open as well.

if err != nil {
panic(err)
}
Expand Down
8 changes: 6 additions & 2 deletions examples/simple-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"io/ioutil"
"io"
t "log"
"math/big"
"net"
Expand Down Expand Up @@ -275,7 +275,11 @@ func main() {
var connectionTrustBundle *x509.CertPool
trustBundleFilePath := os.Getenv("TRUST_BUNDLE_PATH")
if trustBundleFilePath != "" {
buf, err := ioutil.ReadFile(trustBundleFilePath)
file, err := os.Open(trustBundleFilePath)
if err != nil {
panic(err)
}
buf, err := io.ReadAll(file)
if err != nil {
panic(err)
}
Expand Down
8 changes: 6 additions & 2 deletions examples/simple-cli/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"os"

Expand Down Expand Up @@ -51,7 +51,11 @@ func init() {
}
trustBundleFilePath := os.Getenv("TRUST_BUNDLE_PATH")
if trustBundleFilePath != "" {
buf, err := ioutil.ReadFile(trustBundleFilePath)
file, err := os.Open(trustBundleFilePath)
if err != nil {
panic(err)
}
buf, err := io.ReadAll(file)
if err != nil {
panic(err)
}
Expand Down
7 changes: 5 additions & 2 deletions listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -64,7 +64,10 @@ func testListener(t *testing.T, host string, domains []string, success bool) {
t.Fatalf("bad code: %v", r.StatusCode)
}

b, _ := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
if string(b) != text {
t.Fatalf("bad text: %v", text)
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/venafi/tpp/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -736,7 +735,7 @@ func TestResetCertificate(t *testing.T) {
t.Errorf("expected request path %q but got %q", mockCalls[i].expectReqPath, r.URL.Path)
}

bytes, err := ioutil.ReadAll(r.Body)
bytes, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("failed to read request body: %s", err)
}
Expand Down Expand Up @@ -3028,7 +3027,11 @@ func TestCreateSshCertProvidedPubKey(t *testing.T) {
t.Fatalf("err is not nil, err: %s", err)
}

fileContent, err = ioutil.ReadFile(absPath)
file, err := os.Open(absPath)
if err != nil {
panic(err)
}
fileContent, err = io.ReadAll(file)
if err != nil {
t.Fatalf("err is not nil, err: %s", err)
}
Expand Down