Skip to content

Commit b27c723

Browse files
committed
helpers: replace uses of deprecated io/ioutil
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent fc8619e commit b27c723

File tree

4 files changed

+37
-42
lines changed

4 files changed

+37
-42
lines changed

helpers/helpers.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,19 @@ import (
1515
"encoding/pem"
1616
"errors"
1717
"fmt"
18-
"io/ioutil"
1918
"os"
20-
21-
ct "github.com/google/certificate-transparency-go"
22-
cttls "github.com/google/certificate-transparency-go/tls"
23-
ctx509 "github.com/google/certificate-transparency-go/x509"
24-
"golang.org/x/crypto/ocsp"
25-
2619
"strings"
2720
"time"
2821

2922
"github.com/cloudflare/cfssl/crypto/pkcs7"
3023
cferr "github.com/cloudflare/cfssl/errors"
3124
"github.com/cloudflare/cfssl/helpers/derhelpers"
3225
"github.com/cloudflare/cfssl/log"
26+
27+
ct "github.com/google/certificate-transparency-go"
28+
cttls "github.com/google/certificate-transparency-go/tls"
29+
ctx509 "github.com/google/certificate-transparency-go/x509"
30+
"golang.org/x/crypto/ocsp"
3331
"golang.org/x/crypto/pkcs12"
3432
)
3533

@@ -345,7 +343,7 @@ func LoadPEMCertPool(certsFile string) (*x509.CertPool, error) {
345343
if certsFile == "" {
346344
return nil, nil
347345
}
348-
pemCerts, err := ioutil.ReadFile(certsFile)
346+
pemCerts, err := os.ReadFile(certsFile)
349347
if err != nil {
350348
return nil, err
351349
}
@@ -591,13 +589,13 @@ func SCTListFromOCSPResponse(response *ocsp.Response) ([]ct.SignedCertificateTim
591589
func ReadBytes(valFile string) ([]byte, error) {
592590
switch splitVal := strings.SplitN(valFile, ":", 2); len(splitVal) {
593591
case 1:
594-
return ioutil.ReadFile(valFile)
592+
return os.ReadFile(valFile)
595593
case 2:
596594
switch splitVal[0] {
597595
case "env":
598596
return []byte(os.Getenv(splitVal[1])), nil
599597
case "file":
600-
return ioutil.ReadFile(splitVal[1])
598+
return os.ReadFile(splitVal[1])
601599
default:
602600
return nil, fmt.Errorf("unknown prefix: %s", splitVal[0])
603601
}

helpers/helpers_test.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ import (
1010
"crypto/x509/pkix"
1111
"encoding/asn1"
1212
"encoding/pem"
13-
"io/ioutil"
1413
"math"
14+
"os"
1515
"testing"
1616
"time"
1717

18-
"golang.org/x/crypto/ocsp"
19-
2018
"github.com/google/certificate-transparency-go"
19+
"golang.org/x/crypto/ocsp"
2120
)
2221

2322
const (
@@ -52,7 +51,7 @@ const (
5251
func TestParseCertificatesDER(t *testing.T) {
5352
var password = []string{"password", "", ""}
5453
for i, testFile := range []string{testPKCS12Passwordispassword, testPKCS12EmptyPswd, testCertDERFile} {
55-
testDER, err := ioutil.ReadFile(testFile)
54+
testDER, err := os.ReadFile(testFile)
5655
if err != nil {
5756
t.Fatal(err)
5857
}
@@ -65,7 +64,7 @@ func TestParseCertificatesDER(t *testing.T) {
6564
}
6665
}
6766

68-
testDER, err := ioutil.ReadFile(testEmptyPKCS7DER)
67+
testDER, err := os.ReadFile(testEmptyPKCS7DER)
6968
if err != nil {
7069
t.Fatal(err)
7170
}
@@ -89,7 +88,7 @@ func TestKeyLength(t *testing.T) {
8988
t.Fatal("KeyLength malfunctioning on nonsense input")
9089
}
9190

92-
//test the ecdsa branch
91+
// test the ecdsa branch
9392
ecdsaPriv, _ := ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
9493
ecdsaIn, _ := ecdsaPriv.Public().(*ecdsa.PublicKey)
9594
expEcdsa := ecdsaIn.Curve.Params().BitSize
@@ -98,7 +97,7 @@ func TestKeyLength(t *testing.T) {
9897
t.Fatal("KeyLength malfunctioning on ecdsa input")
9998
}
10099

101-
//test the rsa branch
100+
// test the rsa branch
102101
rsaPriv, _ := rsa.GenerateKey(rand.Reader, 256)
103102
rsaIn, _ := rsaPriv.Public().(*rsa.PublicKey)
104103
expRsa := rsaIn.N.BitLen()
@@ -118,8 +117,8 @@ func TestExpiryTime(t *testing.T) {
118117
t.Fatal("Expiry time is malfunctioning on empty input")
119118
}
120119

121-
//read a pem file and use that expiry date
122-
bytes, _ := ioutil.ReadFile(testBundleFile)
120+
// read a pem file and use that expiry date
121+
bytes, _ := os.ReadFile(testBundleFile)
123122
certs, err := ParseCertificatesPEM(bytes)
124123
if err != nil {
125124
t.Fatalf("%v", err)
@@ -269,7 +268,7 @@ func TestSignatureString(t *testing.T) {
269268

270269
func TestParseCertificatePEM(t *testing.T) {
271270
for _, testFile := range []string{testCertFile, testExtraWSCertFile, testSinglePKCS7} {
272-
certPEM, err := ioutil.ReadFile(testFile)
271+
certPEM, err := os.ReadFile(testFile)
273272
if err != nil {
274273
t.Fatal(err)
275274
}
@@ -280,7 +279,7 @@ func TestParseCertificatePEM(t *testing.T) {
280279
}
281280
}
282281
for _, testFile := range []string{testBundleFile, testMessedUpCertFile, testEmptyPKCS7PEM, testEmptyCertFile, testMultiplePKCS7} {
283-
certPEM, err := ioutil.ReadFile(testFile)
282+
certPEM, err := os.ReadFile(testFile)
284283
if err != nil {
285284
t.Fatal(err)
286285
}
@@ -294,7 +293,7 @@ func TestParseCertificatePEM(t *testing.T) {
294293
func TestParseCertificatesPEM(t *testing.T) {
295294
// expected cases
296295
for _, testFile := range []string{testBundleFile, testExtraWSBundleFile, testSinglePKCS7, testMultiplePKCS7} {
297-
bundlePEM, err := ioutil.ReadFile(testFile)
296+
bundlePEM, err := os.ReadFile(testFile)
298297
if err != nil {
299298
t.Fatal(err)
300299
}
@@ -308,7 +307,7 @@ func TestParseCertificatesPEM(t *testing.T) {
308307
// test failure cases
309308
// few lines deleted, then headers removed
310309
for _, testFile := range []string{testMessedUpBundleFile, testEmptyPKCS7PEM, testNoHeaderCert} {
311-
bundlePEM, err := ioutil.ReadFile(testFile)
310+
bundlePEM, err := os.ReadFile(testFile)
312311
if err != nil {
313312
t.Fatal(err)
314313
}
@@ -320,7 +319,7 @@ func TestParseCertificatesPEM(t *testing.T) {
320319
}
321320

322321
func TestSelfSignedCertificatePEM(t *testing.T) {
323-
testPEM, err := ioutil.ReadFile(testCertFile)
322+
testPEM, err := os.ReadFile(testCertFile)
324323
if err != nil {
325324
t.Fatal(err)
326325
}
@@ -330,7 +329,7 @@ func TestSelfSignedCertificatePEM(t *testing.T) {
330329
}
331330

332331
// a few lines deleted from the pem file
333-
wrongPEM, err := ioutil.ReadFile(testMessedUpCertFile)
332+
wrongPEM, err := os.ReadFile(testMessedUpCertFile)
334333
if err != nil {
335334
t.Fatal(err)
336335
}
@@ -353,7 +352,7 @@ func TestSelfSignedCertificatePEM(t *testing.T) {
353352
func TestParsePrivateKeyPEM(t *testing.T) {
354353

355354
// expected cases
356-
testRSAPEM, err := ioutil.ReadFile(testPrivateRSAKey)
355+
testRSAPEM, err := os.ReadFile(testPrivateRSAKey)
357356
if err != nil {
358357
t.Fatal(err)
359358
}
@@ -362,7 +361,7 @@ func TestParsePrivateKeyPEM(t *testing.T) {
362361
t.Fatal(err)
363362
}
364363

365-
testECDSAPEM, err := ioutil.ReadFile(testPrivateECDSAKey)
364+
testECDSAPEM, err := os.ReadFile(testPrivateECDSAKey)
366365
if err != nil {
367366
t.Fatal(err)
368367
}
@@ -371,7 +370,7 @@ func TestParsePrivateKeyPEM(t *testing.T) {
371370
t.Fatal(err)
372371
}
373372

374-
testEd25519PEM, err := ioutil.ReadFile(testPrivateEd25519Key)
373+
testEd25519PEM, err := os.ReadFile(testPrivateEd25519Key)
375374
if err != nil {
376375
t.Fatal(err)
377376
}
@@ -381,7 +380,7 @@ func TestParsePrivateKeyPEM(t *testing.T) {
381380
t.Fatal(err)
382381
}
383382

384-
testOpenSSLECKey, err := ioutil.ReadFile(testPrivateOpenSSLECKey)
383+
testOpenSSLECKey, err := os.ReadFile(testPrivateOpenSSLECKey)
385384
if err != nil {
386385
t.Fatal(err)
387386
}
@@ -400,7 +399,7 @@ func TestParsePrivateKeyPEM(t *testing.T) {
400399
}
401400

402401
for _, fname := range errCases {
403-
testPEM, _ := ioutil.ReadFile(fname)
402+
testPEM, _ := os.ReadFile(fname)
404403
_, err = ParsePrivateKeyPEM(testPEM)
405404
if err == nil {
406405
t.Fatal("Incorrect private key failed to produce an error")
@@ -413,7 +412,7 @@ func TestParsePrivateKeyPEM(t *testing.T) {
413412
const ecdsaTestCSR = "testdata/ecdsa256.csr"
414413

415414
func TestParseCSRPEM(t *testing.T) {
416-
in, err := ioutil.ReadFile(ecdsaTestCSR)
415+
in, err := os.ReadFile(ecdsaTestCSR)
417416
if err != nil {
418417
t.Fatalf("%v", err)
419418
}
@@ -432,7 +431,7 @@ func TestParseCSRPEM(t *testing.T) {
432431
}
433432

434433
func TestParseCSRPEMMore(t *testing.T) {
435-
csrPEM, err := ioutil.ReadFile(testCSRPEM)
434+
csrPEM, err := os.ReadFile(testCSRPEM)
436435
if err != nil {
437436
t.Fatal(err)
438437
}
@@ -441,7 +440,7 @@ func TestParseCSRPEMMore(t *testing.T) {
441440
t.Fatal(err)
442441
}
443442

444-
csrPEM, err = ioutil.ReadFile(testCSRPEMBad)
443+
csrPEM, err = os.ReadFile(testCSRPEMBad)
445444
if err != nil {
446445
t.Fatal(err)
447446
}
@@ -459,7 +458,7 @@ func TestParseCSRPEMMore(t *testing.T) {
459458
const rsaOldTestCSR = "testdata/rsa-old.csr"
460459

461460
func TestParseOldCSR(t *testing.T) {
462-
in, err := ioutil.ReadFile(rsaOldTestCSR)
461+
in, err := os.ReadFile(rsaOldTestCSR)
463462
if err != nil {
464463
t.Fatalf("%v", err)
465464
}
@@ -508,7 +507,7 @@ func TestLoadPEMCertPool(t *testing.T) {
508507
t.Fatal("Empty file name should not generate error or a cert pool")
509508
}
510509

511-
in, err := ioutil.ReadFile(testEmptyPem)
510+
in, err := os.ReadFile(testEmptyPem)
512511
if err != nil {
513512
t.Fatalf("%v", err)
514513
}
@@ -519,7 +518,7 @@ func TestLoadPEMCertPool(t *testing.T) {
519518
t.Fatal("Expected error for empty file")
520519
}
521520

522-
in, err = ioutil.ReadFile(testEmptyCertFile)
521+
in, err = os.ReadFile(testEmptyCertFile)
523522
if err != nil {
524523
t.Fatalf("%v", err)
525524
}
@@ -530,7 +529,7 @@ func TestLoadPEMCertPool(t *testing.T) {
530529
t.Fatal("Expected error for empty cert")
531530
}
532531

533-
in, err = ioutil.ReadFile(clientCertFile)
532+
in, err = os.ReadFile(clientCertFile)
534533
if err != nil {
535534
t.Fatalf("%v", err)
536535
}

helpers/testsuite/testing_helpers.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"encoding/json"
88
"errors"
99
"fmt"
10-
"io/ioutil"
1110
"os"
1211
"os/exec"
1312
"strconv"
@@ -324,7 +323,7 @@ func createTempFile(data []byte) (fileName string, err error) {
324323
}
325324

326325
readWritePermissions := os.FileMode(0664)
327-
err = ioutil.WriteFile(tempFileName, data, readWritePermissions)
326+
err = os.WriteFile(tempFileName, data, readWritePermissions)
328327
if err != nil {
329328
return "", err
330329
}

helpers/testsuite/testing_helpers_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package testsuite
33
import (
44
"crypto/x509"
55
"encoding/json"
6-
"io/ioutil"
76
"math"
87
"math/rand"
98
"os"
@@ -154,7 +153,7 @@ func TestCreateCertificateChain(t *testing.T) {
154153
// the same request data.
155154

156155
CLIOutputFile := preMadeOutput
157-
CLIOutput, err := ioutil.ReadFile(CLIOutputFile)
156+
CLIOutput, err := os.ReadFile(CLIOutputFile)
158157
if err != nil {
159158
t.Fatal(err.Error())
160159
}
@@ -291,7 +290,7 @@ func TestCreateSelfSignedCert(t *testing.T) {
291290
// and is called ca_csr.json.
292291

293292
CLIOutputFile := preMadeOutput
294-
CLIOutput, err := ioutil.ReadFile(CLIOutputFile)
293+
CLIOutput, err := os.ReadFile(CLIOutputFile)
295294
if err != nil {
296295
t.Fatal(err.Error())
297296
}

0 commit comments

Comments
 (0)