Skip to content

Commit 6f34ba0

Browse files
committed
bundler: replace uses of deprecated io/ioutil
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 2bc4f21 commit 6f34ba0

File tree

3 files changed

+31
-31
lines changed

3 files changed

+31
-31
lines changed

bundler/bundler.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"encoding/pem"
1313
goerr "errors"
1414
"fmt"
15-
"io/ioutil"
15+
"io"
1616
"net"
1717
"net/http"
1818
"os"
@@ -99,7 +99,7 @@ func NewBundler(caBundleFile, intBundleFile string, opt ...Option) (*Bundler, er
9999

100100
if caBundleFile != "" {
101101
log.Debug("Loading CA bundle: ", caBundleFile)
102-
caBundle, err = ioutil.ReadFile(caBundleFile)
102+
caBundle, err = os.ReadFile(caBundleFile)
103103
if err != nil {
104104
log.Errorf("root bundle failed to load: %v", err)
105105
return nil, errors.Wrap(errors.RootError, errors.ReadFailed, err)
@@ -108,7 +108,7 @@ func NewBundler(caBundleFile, intBundleFile string, opt ...Option) (*Bundler, er
108108

109109
if intBundleFile != "" {
110110
log.Debug("Loading Intermediate bundle: ", intBundleFile)
111-
intBundle, err = ioutil.ReadFile(intBundleFile)
111+
intBundle, err = os.ReadFile(intBundleFile)
112112
if err != nil {
113113
log.Errorf("intermediate bundle failed to load: %v", err)
114114
return nil, errors.Wrap(errors.IntermediatesError, errors.ReadFailed, err)
@@ -199,7 +199,7 @@ func (b *Bundler) VerifyOptions() x509.VerifyOptions {
199199
// and returns the bundle built from that key and the certificate(s).
200200
func (b *Bundler) BundleFromFile(bundleFile, keyFile string, flavor BundleFlavor, password string) (*Bundle, error) {
201201
log.Debug("Loading Certificate: ", bundleFile)
202-
certsRaw, err := ioutil.ReadFile(bundleFile)
202+
certsRaw, err := os.ReadFile(bundleFile)
203203
if err != nil {
204204
return nil, errors.Wrap(errors.CertificateError, errors.ReadFailed, err)
205205
}
@@ -208,7 +208,7 @@ func (b *Bundler) BundleFromFile(bundleFile, keyFile string, flavor BundleFlavor
208208
// Load private key PEM only if a file is given
209209
if keyFile != "" {
210210
log.Debug("Loading private key: ", keyFile)
211-
keyPEM, err = ioutil.ReadFile(keyFile)
211+
keyPEM, err = os.ReadFile(keyFile)
212212
if err != nil {
213213
log.Debugf("failed to read private key: ", err)
214214
return nil, errors.Wrap(errors.PrivateKeyError, errors.ReadFailed, err)
@@ -344,7 +344,7 @@ func fetchRemoteCertificate(certURL string) (fi *fetchedIntermediate, err error)
344344

345345
defer resp.Body.Close()
346346
var certData []byte
347-
certData, err = ioutil.ReadAll(resp.Body)
347+
certData, err = io.ReadAll(resp.Body)
348348
if err != nil {
349349
log.Debugf("failed to read response body: %v", err)
350350
return
@@ -442,7 +442,7 @@ func (b *Bundler) verifyChain(chain []*fetchedIntermediate) bool {
442442

443443
log.Debugf("write intermediate to stash directory: %s", fileName)
444444
// If the write fails, verification should not fail.
445-
err = ioutil.WriteFile(fileName, pem.EncodeToMemory(&block), 0644)
445+
err = os.WriteFile(fileName, pem.EncodeToMemory(&block), 0644)
446446
if err != nil {
447447
log.Errorf("failed to write new intermediate: %v", err)
448448
} else {

bundler/bundler_sha1_deprecation_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package bundler
33
// This test file contains tests on checking Bundle.Status with SHA-1 deprecation warning.
44
import (
55
"crypto/x509"
6-
"io/ioutil"
6+
"os"
77
"testing"
88
"time"
99

@@ -33,7 +33,7 @@ func TestChromeWarning(t *testing.T) {
3333
t.Fatal(err)
3434
}
3535

36-
csrBytes, err := ioutil.ReadFile(leafCSR)
36+
csrBytes, err := os.ReadFile(leafCSR)
3737
if err != nil {
3838
t.Fatal(err)
3939
}
@@ -92,7 +92,7 @@ func TestSHA2Preferences(t *testing.T) {
9292
sha1InterBytes := signCSRFile(sha1CASigner, intermediateCSR, t)
9393
sha2InterBytes := signCSRFile(sha2CASigner, intermediateCSR, t)
9494

95-
interKeyBytes, err := ioutil.ReadFile(intermediateKey)
95+
interKeyBytes, err := os.ReadFile(intermediateKey)
9696
if err != nil {
9797
t.Fatal(err)
9898
}
@@ -124,12 +124,12 @@ func TestSHA2Preferences(t *testing.T) {
124124
}
125125

126126
func makeCASignerFromFile(certFile, keyFile string, sigAlgo x509.SignatureAlgorithm, t *testing.T) signer.Signer {
127-
certBytes, err := ioutil.ReadFile(certFile)
127+
certBytes, err := os.ReadFile(certFile)
128128
if err != nil {
129129
t.Fatal(err)
130130
}
131131

132-
keyBytes, err := ioutil.ReadFile(keyFile)
132+
keyBytes, err := os.ReadFile(keyFile)
133133
if err != nil {
134134
t.Fatal(err)
135135
}
@@ -168,7 +168,7 @@ func makeCASigner(certBytes, keyBytes []byte, sigAlgo x509.SignatureAlgorithm, t
168168
}
169169

170170
func signCSRFile(s signer.Signer, csrFile string, t *testing.T) []byte {
171-
csrBytes, err := ioutil.ReadFile(csrFile)
171+
csrBytes, err := os.ReadFile(csrFile)
172172
if err != nil {
173173
t.Fatal(err)
174174
}

bundler/bundler_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"bytes"
66
"crypto/x509"
77
"encoding/json"
8-
"io/ioutil"
8+
"os"
99
"strings"
1010
"testing"
1111

@@ -176,14 +176,14 @@ func TestBundleWithECDSAKeyMarshalJSON(t *testing.T) {
176176
}
177177

178178
key := obj["key"].(string)
179-
keyBytes, _ := ioutil.ReadFile(leafKeyECDSA256)
179+
keyBytes, _ := os.ReadFile(leafKeyECDSA256)
180180
keyBytes = bytes.Trim(keyBytes, " \n")
181181
if key != string(keyBytes) {
182182
t.Fatal("key is not recovered.")
183183
}
184184

185185
cert := obj["crt"].(string)
186-
certBytes, _ := ioutil.ReadFile(leafECDSA256)
186+
certBytes, _ := os.ReadFile(leafECDSA256)
187187
certBytes = bytes.Trim(certBytes, " \n")
188188
if cert != string(certBytes) {
189189
t.Fatal("cert is not recovered.")
@@ -212,7 +212,7 @@ func TestBundleWithRSAKeyMarshalJSON(t *testing.T) {
212212
}
213213

214214
key := obj["key"].(string)
215-
keyBytes, _ := ioutil.ReadFile(leafKeyRSA2048)
215+
keyBytes, _ := os.ReadFile(leafKeyRSA2048)
216216
keyBytes = bytes.Trim(keyBytes, " \n")
217217
if key != string(keyBytes) {
218218
t.Error("key is", key)
@@ -221,7 +221,7 @@ func TestBundleWithRSAKeyMarshalJSON(t *testing.T) {
221221
}
222222

223223
cert := obj["crt"].(string)
224-
certBytes, _ := ioutil.ReadFile(leafRSA2048)
224+
certBytes, _ := os.ReadFile(leafRSA2048)
225225
certBytes = bytes.Trim(certBytes, " \n")
226226
if cert != string(certBytes) {
227227
t.Fatal("cert is not recovered.")
@@ -373,7 +373,7 @@ func TestForceBundle(t *testing.T) {
373373
interL1Bytes := signCSRFile(caSigner, interL1CSR, t)
374374

375375
// create a inter L1 signer
376-
interL1KeyBytes, err := ioutil.ReadFile(interL1Key)
376+
interL1KeyBytes, err := os.ReadFile(interL1Key)
377377
if err != nil {
378378
t.Fatal(err)
379379
}
@@ -384,7 +384,7 @@ func TestForceBundle(t *testing.T) {
384384
interL2Bytes := signCSRFile(interL1Signer, interL2CSR, t)
385385

386386
// create a inter L2 signer
387-
interL2KeyBytes, err := ioutil.ReadFile(interL2Key)
387+
interL2KeyBytes, err := os.ReadFile(interL2Key)
388388
if err != nil {
389389
t.Fatal(err)
390390
}
@@ -396,7 +396,7 @@ func TestForceBundle(t *testing.T) {
396396

397397
// create two platforms
398398
// both trust the CA cert and L1 intermediate
399-
caBytes, err := ioutil.ReadFile(testCAFile)
399+
caBytes, err := os.ReadFile(testCAFile)
400400
if err != nil {
401401
t.Fatal(err)
402402
}
@@ -476,7 +476,7 @@ func TestUpdateIntermediate(t *testing.T) {
476476
caSigner := makeCASignerFromFile(testCAFile, testCAKeyFile, x509.SHA256WithRSA, t)
477477
sha2InterBytes := signCSRFile(caSigner, interL1CSR, t)
478478

479-
interKeyBytes, err := ioutil.ReadFile(interL1Key)
479+
interKeyBytes, err := os.ReadFile(interL1Key)
480480
if err != nil {
481481
t.Fatal(err)
482482
}
@@ -487,7 +487,7 @@ func TestUpdateIntermediate(t *testing.T) {
487487
leafBytes := signCSRFile(sha2InterSigner, leafCSR, t)
488488

489489
// read CA cert bytes
490-
caCertBytes, err := ioutil.ReadFile(testCAFile)
490+
caCertBytes, err := os.ReadFile(testCAFile)
491491
if err != nil {
492492
t.Fatal(err)
493493
}
@@ -522,7 +522,7 @@ func TestForceBundleNoFallback(t *testing.T) {
522522
caSigner := makeCASignerFromFile(testCAFile, testCAKeyFile, x509.SHA256WithRSA, t)
523523
sha2InterBytes := signCSRFile(caSigner, interL1CSR, t)
524524

525-
interKeyBytes, err := ioutil.ReadFile(interL1Key)
525+
interKeyBytes, err := os.ReadFile(interL1Key)
526526
if err != nil {
527527
t.Fatal(err)
528528
}
@@ -533,7 +533,7 @@ func TestForceBundleNoFallback(t *testing.T) {
533533
leafBytes := signCSRFile(sha2InterSigner, leafCSR, t)
534534

535535
// read CA cert bytes
536-
caCertBytes, err := ioutil.ReadFile(testCAFile)
536+
caCertBytes, err := os.ReadFile(testCAFile)
537537
if err != nil {
538538
t.Fatal(err)
539539
}
@@ -566,7 +566,7 @@ func TestSHA2HomogeneityAgainstUbiquity(t *testing.T) {
566566
interL1Bytes := signCSRFile(caSigner, interL1CSR, t)
567567

568568
// create a inter L1 signer
569-
interL1KeyBytes, err := ioutil.ReadFile(interL1Key)
569+
interL1KeyBytes, err := os.ReadFile(interL1Key)
570570
if err != nil {
571571
t.Fatal(err)
572572
}
@@ -577,7 +577,7 @@ func TestSHA2HomogeneityAgainstUbiquity(t *testing.T) {
577577
interL2Bytes := signCSRFile(interL1Signer, interL2CSR, t)
578578

579579
// create a inter L2 signer
580-
interL2KeyBytes, err := ioutil.ReadFile(interL2Key)
580+
interL2KeyBytes, err := os.ReadFile(interL2Key)
581581
if err != nil {
582582
t.Fatal(err)
583583
}
@@ -590,7 +590,7 @@ func TestSHA2HomogeneityAgainstUbiquity(t *testing.T) {
590590
// create two platforms
591591
// platform A trusts the CA cert and L1 intermediate
592592
// platform B trusts the CA cert
593-
caBytes, err := ioutil.ReadFile(testCAFile)
593+
caBytes, err := os.ReadFile(testCAFile)
594594
if err != nil {
595595
t.Fatal(err)
596596
}
@@ -708,7 +708,7 @@ func TestSHA2Warning(t *testing.T) {
708708
sha2InterBytes := signCSRFile(caSigner, interL1CSR, t)
709709

710710
// read CA cert bytes
711-
caCertBytes, err := ioutil.ReadFile(testCAFile)
711+
caCertBytes, err := os.ReadFile(testCAFile)
712712
if err != nil {
713713
t.Fatal(err)
714714
}
@@ -752,7 +752,7 @@ func TestECDSAWarning(t *testing.T) {
752752

753753
// readCert read a PEM file and returns a cert.
754754
func readCert(filename string) *x509.Certificate {
755-
bytes, _ := ioutil.ReadFile(filename)
755+
bytes, _ := os.ReadFile(filename)
756756
cert, _ := helpers.ParseCertificatePEM(bytes)
757757
return cert
758758
}
@@ -784,7 +784,7 @@ func newCustomizedBundlerFromFile(t *testing.T, caBundle, intBundle, adhocInters
784784
t.Fatal(err)
785785
}
786786
if adhocInters != "" {
787-
moreIntersPEM, err := ioutil.ReadFile(adhocInters)
787+
moreIntersPEM, err := os.ReadFile(adhocInters)
788788
if err != nil {
789789
t.Fatalf("Read additional intermediates failed. %v",
790790
err)

0 commit comments

Comments
 (0)