Skip to content

Commit

Permalink
bundler: replace uses of deprecated io/ioutil
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Nov 20, 2022
1 parent 2bc4f21 commit 6f34ba0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
14 changes: 7 additions & 7 deletions bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"encoding/pem"
goerr "errors"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -99,7 +99,7 @@ func NewBundler(caBundleFile, intBundleFile string, opt ...Option) (*Bundler, er

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

if intBundleFile != "" {
log.Debug("Loading Intermediate bundle: ", intBundleFile)
intBundle, err = ioutil.ReadFile(intBundleFile)
intBundle, err = os.ReadFile(intBundleFile)
if err != nil {
log.Errorf("intermediate bundle failed to load: %v", err)
return nil, errors.Wrap(errors.IntermediatesError, errors.ReadFailed, err)
Expand Down Expand Up @@ -199,7 +199,7 @@ func (b *Bundler) VerifyOptions() x509.VerifyOptions {
// and returns the bundle built from that key and the certificate(s).
func (b *Bundler) BundleFromFile(bundleFile, keyFile string, flavor BundleFlavor, password string) (*Bundle, error) {
log.Debug("Loading Certificate: ", bundleFile)
certsRaw, err := ioutil.ReadFile(bundleFile)
certsRaw, err := os.ReadFile(bundleFile)
if err != nil {
return nil, errors.Wrap(errors.CertificateError, errors.ReadFailed, err)
}
Expand All @@ -208,7 +208,7 @@ func (b *Bundler) BundleFromFile(bundleFile, keyFile string, flavor BundleFlavor
// Load private key PEM only if a file is given
if keyFile != "" {
log.Debug("Loading private key: ", keyFile)
keyPEM, err = ioutil.ReadFile(keyFile)
keyPEM, err = os.ReadFile(keyFile)
if err != nil {
log.Debugf("failed to read private key: ", err)
return nil, errors.Wrap(errors.PrivateKeyError, errors.ReadFailed, err)
Expand Down Expand Up @@ -344,7 +344,7 @@ func fetchRemoteCertificate(certURL string) (fi *fetchedIntermediate, err error)

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

log.Debugf("write intermediate to stash directory: %s", fileName)
// If the write fails, verification should not fail.
err = ioutil.WriteFile(fileName, pem.EncodeToMemory(&block), 0644)
err = os.WriteFile(fileName, pem.EncodeToMemory(&block), 0644)
if err != nil {
log.Errorf("failed to write new intermediate: %v", err)
} else {
Expand Down
12 changes: 6 additions & 6 deletions bundler/bundler_sha1_deprecation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package bundler
// This test file contains tests on checking Bundle.Status with SHA-1 deprecation warning.
import (
"crypto/x509"
"io/ioutil"
"os"
"testing"
"time"

Expand Down Expand Up @@ -33,7 +33,7 @@ func TestChromeWarning(t *testing.T) {
t.Fatal(err)
}

csrBytes, err := ioutil.ReadFile(leafCSR)
csrBytes, err := os.ReadFile(leafCSR)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestSHA2Preferences(t *testing.T) {
sha1InterBytes := signCSRFile(sha1CASigner, intermediateCSR, t)
sha2InterBytes := signCSRFile(sha2CASigner, intermediateCSR, t)

interKeyBytes, err := ioutil.ReadFile(intermediateKey)
interKeyBytes, err := os.ReadFile(intermediateKey)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -124,12 +124,12 @@ func TestSHA2Preferences(t *testing.T) {
}

func makeCASignerFromFile(certFile, keyFile string, sigAlgo x509.SignatureAlgorithm, t *testing.T) signer.Signer {
certBytes, err := ioutil.ReadFile(certFile)
certBytes, err := os.ReadFile(certFile)
if err != nil {
t.Fatal(err)
}

keyBytes, err := ioutil.ReadFile(keyFile)
keyBytes, err := os.ReadFile(keyFile)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -168,7 +168,7 @@ func makeCASigner(certBytes, keyBytes []byte, sigAlgo x509.SignatureAlgorithm, t
}

func signCSRFile(s signer.Signer, csrFile string, t *testing.T) []byte {
csrBytes, err := ioutil.ReadFile(csrFile)
csrBytes, err := os.ReadFile(csrFile)
if err != nil {
t.Fatal(err)
}
Expand Down
36 changes: 18 additions & 18 deletions bundler/bundler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"bytes"
"crypto/x509"
"encoding/json"
"io/ioutil"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -176,14 +176,14 @@ func TestBundleWithECDSAKeyMarshalJSON(t *testing.T) {
}

key := obj["key"].(string)
keyBytes, _ := ioutil.ReadFile(leafKeyECDSA256)
keyBytes, _ := os.ReadFile(leafKeyECDSA256)
keyBytes = bytes.Trim(keyBytes, " \n")
if key != string(keyBytes) {
t.Fatal("key is not recovered.")
}

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

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

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

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

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

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

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

// read CA cert bytes
caCertBytes, err := ioutil.ReadFile(testCAFile)
caCertBytes, err := os.ReadFile(testCAFile)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -522,7 +522,7 @@ func TestForceBundleNoFallback(t *testing.T) {
caSigner := makeCASignerFromFile(testCAFile, testCAKeyFile, x509.SHA256WithRSA, t)
sha2InterBytes := signCSRFile(caSigner, interL1CSR, t)

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

// read CA cert bytes
caCertBytes, err := ioutil.ReadFile(testCAFile)
caCertBytes, err := os.ReadFile(testCAFile)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -566,7 +566,7 @@ func TestSHA2HomogeneityAgainstUbiquity(t *testing.T) {
interL1Bytes := signCSRFile(caSigner, interL1CSR, t)

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

// create a inter L2 signer
interL2KeyBytes, err := ioutil.ReadFile(interL2Key)
interL2KeyBytes, err := os.ReadFile(interL2Key)
if err != nil {
t.Fatal(err)
}
Expand All @@ -590,7 +590,7 @@ func TestSHA2HomogeneityAgainstUbiquity(t *testing.T) {
// create two platforms
// platform A trusts the CA cert and L1 intermediate
// platform B trusts the CA cert
caBytes, err := ioutil.ReadFile(testCAFile)
caBytes, err := os.ReadFile(testCAFile)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -708,7 +708,7 @@ func TestSHA2Warning(t *testing.T) {
sha2InterBytes := signCSRFile(caSigner, interL1CSR, t)

// read CA cert bytes
caCertBytes, err := ioutil.ReadFile(testCAFile)
caCertBytes, err := os.ReadFile(testCAFile)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -752,7 +752,7 @@ func TestECDSAWarning(t *testing.T) {

// readCert read a PEM file and returns a cert.
func readCert(filename string) *x509.Certificate {
bytes, _ := ioutil.ReadFile(filename)
bytes, _ := os.ReadFile(filename)
cert, _ := helpers.ParseCertificatePEM(bytes)
return cert
}
Expand Down Expand Up @@ -784,7 +784,7 @@ func newCustomizedBundlerFromFile(t *testing.T, caBundle, intBundle, adhocInters
t.Fatal(err)
}
if adhocInters != "" {
moreIntersPEM, err := ioutil.ReadFile(adhocInters)
moreIntersPEM, err := os.ReadFile(adhocInters)
if err != nil {
t.Fatalf("Read additional intermediates failed. %v",
err)
Expand Down

0 comments on commit 6f34ba0

Please sign in to comment.