Skip to content

Commit

Permalink
migrate ioutil calls to io,os calls (#911)
Browse files Browse the repository at this point in the history
The Go 1.19 and nearby versions marked these calls as deprecated. Clean
them up with their documented replacements.

Ran these commands to fix it up:

```
$ gofmt -w -r 'ioutil.TempFile -> os.CreateTemp' $(find . -type f -name '*.go'| grep -v "/vendor/")
$ gofmt -w -r 'ioutil.ReadAll -> io.ReadAll' $(find . -type f -name '*.go'| grep -v "/vendor/")
$ gofmt -w -r 'ioutil.WriteFile -> os.WriteFile' $(find . -type f -name '*.go'| grep -v "/vendor/")
$ gofmt -w -r 'ioutil.TempDir -> os.MkdirTemp' $(find . -type f -name '*.go'| grep -v "/vendor/")
```

Then removing the `ioutil` import from the files that no longer use it.

Oh, also ha to revert some gofmt changes in `verifier/contentsignature`
that I'll ship separately.
  • Loading branch information
jmhodges authored Jun 21, 2024
1 parent fafa72d commit 131295e
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 87 deletions.
3 changes: 1 addition & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"

log "github.com/sirupsen/logrus"
Expand All @@ -30,7 +29,7 @@ func httpError(w http.ResponseWriter, r *http.Request, errorCode int, errorMessa
// request body is read before writing a response.
// https://github.com/golang/go/issues/15789
if r.Body != nil {
io.Copy(ioutil.Discard, r.Body)
io.Copy(io.Discard, r.Body)
r.Body.Close()
}
http.Error(w, msg, errorCode)
Expand Down
6 changes: 3 additions & 3 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -90,7 +90,7 @@ func (a *autographer) handleSignature(w http.ResponseWriter, r *http.Request) {
httpError(w, r, http.StatusUnauthorized, "authorization verification failed: %v", err)
return
}
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
httpError(w, r, http.StatusBadRequest, "failed to read request body: %s", err)
return
Expand Down Expand Up @@ -453,7 +453,7 @@ func (a *autographer) handleGetAuthKeyIDs(w http.ResponseWriter, r *http.Request
return
}
if r.Body != nil {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
httpError(w, r, http.StatusBadRequest, "failed to read request body: %s", err)
return
Expand Down
6 changes: 3 additions & 3 deletions handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"encoding/json"
"fmt"
"hash"
"io/ioutil"
"io"
"log"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -836,7 +836,7 @@ func verifyAPKSignature(signedAPK []byte) error {
if err != nil {
return err
}
sigdata, err = ioutil.ReadAll(rc)
sigdata, err = io.ReadAll(rc)
if err != nil {
return err
}
Expand All @@ -849,7 +849,7 @@ func verifyAPKSignature(signedAPK []byte) error {
if err != nil {
return err
}
rawsig, err := ioutil.ReadAll(rc)
rawsig, err := io.ReadAll(rc)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"crypto/sha256"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -254,7 +253,7 @@ func (c *configuration) loadFromFile(path string) error {
confSHA [32]byte
err error
)
data, err = ioutil.ReadFile(path)
data, err = os.ReadFile(path)
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"testing"
"time"
Expand Down Expand Up @@ -197,7 +196,7 @@ authorizations:
for i, testcase := range testcases {
var conf configuration
// write conf file to /tmp and read it back
fd, err := ioutil.TempFile("", "autographtestconf")
fd, err := os.CreateTemp("", "autographtestconf")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -228,7 +227,7 @@ func TestDuplicateSigners(t *testing.T) {

var conf configuration
// write conf file to /tmp and read it back
fd, err := ioutil.TempFile("", "autographtestconf")
fd, err := os.CreateTemp("", "autographtestconf")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -283,7 +282,7 @@ func TestDuplicateAuthorization(t *testing.T) {

var conf configuration
// write conf file to /tmp and read it back
fd, err := ioutil.TempFile("", "autographtestconf")
fd, err := os.CreateTemp("", "autographtestconf")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -353,7 +352,7 @@ func TestUnknownSignerInAuthorization(t *testing.T) {

var conf configuration
// write conf file to /tmp and read it back
fd, err := ioutil.TempFile("", "autographtestconf")
fd, err := os.CreateTemp("", "autographtestconf")
if err != nil {
t.Fatal(err)
}
Expand Down
15 changes: 7 additions & 8 deletions signer/apk2/apk2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package apk2
import (
"bytes"
"fmt"
"io/ioutil"

"crypto/ecdsa"
"crypto/sha256"
Expand Down Expand Up @@ -111,35 +110,35 @@ func (s *APK2Signer) Config() signer.Configuration {

// SignFile signs a whole aligned APK file with v1 and v2 signatures
func (s *APK2Signer) SignFile(file []byte, options interface{}) (signer.SignedFile, error) {
keyPath, err := ioutil.TempFile("", fmt.Sprintf("apk2_%s.key", s.ID))
keyPath, err := os.CreateTemp("", fmt.Sprintf("apk2_%s.key", s.ID))
if err != nil {
return nil, fmt.Errorf("apk2: failed to create tempfile with private key: %w", err)
}
defer os.Remove(keyPath.Name())
err = ioutil.WriteFile(keyPath.Name(), []byte(s.pkcs8Key), 0400)
err = os.WriteFile(keyPath.Name(), []byte(s.pkcs8Key), 0400)
if err != nil {
return nil, fmt.Errorf("apk2: failed to write private key to tempfile: %w", err)
}

certPath, err := ioutil.TempFile("", fmt.Sprintf("apk2_%s.cert", s.ID))
certPath, err := os.CreateTemp("", fmt.Sprintf("apk2_%s.cert", s.ID))
if err != nil {
return nil, fmt.Errorf("apk2: failed to create tempfile for input to sign: %w", err)
}
defer os.Remove(certPath.Name())
err = ioutil.WriteFile(certPath.Name(), []byte(s.Certificate), 0400)
err = os.WriteFile(certPath.Name(), []byte(s.Certificate), 0400)
if err != nil {
return nil, fmt.Errorf("apk2: failed to write public cert to tempfile: %w", err)
}

// write the input to a temp file
h := sha256.New()
h.Write(file)
tmpAPKFile, err := ioutil.TempFile("", fmt.Sprintf("apk2_input_%x.apk", h.Sum(nil)))
tmpAPKFile, err := os.CreateTemp("", fmt.Sprintf("apk2_input_%x.apk", h.Sum(nil)))
if err != nil {
return nil, fmt.Errorf("apk2: failed to create tempfile for input to sign: %w", err)
}
defer os.Remove(tmpAPKFile.Name())
err = ioutil.WriteFile(tmpAPKFile.Name(), file, 0755)
err = os.WriteFile(tmpAPKFile.Name(), file, 0755)
if err != nil {
return nil, fmt.Errorf("apk2: failed to write tempfile for input to sign: %w", err)
}
Expand Down Expand Up @@ -169,7 +168,7 @@ func (s *APK2Signer) SignFile(file []byte, options interface{}) (signer.SignedFi
}
log.Debugf("signed as:\n%s\n", string(out))

signedApk, err := ioutil.ReadFile(tmpAPKFile.Name())
signedApk, err := os.ReadFile(tmpAPKFile.Name())
if err != nil {
return nil, fmt.Errorf("apk2: failed to read signed file: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions signer/apk2/apk2_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package apk2

import (
"github.com/mozilla-services/autograph/signer"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"

"github.com/mozilla-services/autograph/signer"
)

func assertNewSignerWithConfOK(t *testing.T, conf signer.Configuration) *APK2Signer {
Expand Down Expand Up @@ -177,12 +177,12 @@ func TestSignFile(t *testing.T) {
t.Parallel()

// write the signature to a temp file
tmpApk, err := ioutil.TempFile("", "apk2_TestSignedApkFile")
tmpApk, err := os.CreateTemp("", "apk2_TestSignedApkFile")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpApk.Name())
err = ioutil.WriteFile(tmpApk.Name(), signedFile, 0755)
err = os.WriteFile(tmpApk.Name(), signedFile, 0755)
if err != nil {
t.Fatalf("error writing file %s: %q", tmpApk.Name(), err)
}
Expand Down
6 changes: 3 additions & 3 deletions signer/contentsignaturepki/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"crypto/sha256"
"crypto/x509"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -63,7 +63,7 @@ func writeLocalFile(data, name string, target *url.URL) error {
}
}
// write the file into the target dir
return ioutil.WriteFile(target.Path+name, []byte(data), 0755)
return os.WriteFile(target.Path+name, []byte(data), 0755)
}

// buildHTTPClient returns the default HTTP.Client for fetching X5Us
Expand Down Expand Up @@ -95,7 +95,7 @@ func GetX5U(client *http.Client, x5u string) (body []byte, certs []*x509.Certifi
err = fmt.Errorf("failed to retrieve x5u from %s: %s", x5u, resp.Status)
return
}
body, err = ioutil.ReadAll(resp.Body)
body, err = io.ReadAll(resp.Body)
if err != nil {
err = fmt.Errorf("failed to parse x5u body: %w", err)
return
Expand Down
23 changes: 11 additions & 12 deletions signer/gpg2/gpg2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -158,13 +157,13 @@ func createKeyRing(s *GPG2Signer) (dir string, err error) {
// reuse keyring in tempdir
prefix := fmt.Sprintf("autograph_%s_%s_%s_", s.Type, s.KeyID, s.Mode)

dir, err = ioutil.TempDir("", prefix)
dir, err = os.MkdirTemp("", prefix)
if err != nil {
return "", fmt.Errorf("gpg2: error creating tempdir for keyring: %w", err)
}

// write the public key to a temp file in our signer's temp dir
tmpPublicKeyFile, err := ioutil.TempFile(dir, "gpg2_publickey")
tmpPublicKeyFile, err := os.CreateTemp(dir, "gpg2_publickey")
if err != nil {
err = fmt.Errorf("gpg2: error creating tempfile for public key: %w", err)
return "", err
Expand All @@ -177,14 +176,14 @@ func createKeyRing(s *GPG2Signer) (dir string, err error) {
}
}()

err = ioutil.WriteFile(tmpPublicKeyFile.Name(), []byte(s.PublicKey), 0755)
err = os.WriteFile(tmpPublicKeyFile.Name(), []byte(s.PublicKey), 0755)
if err != nil {
err = fmt.Errorf("gpg2: error writing public key to tempfile: %w", err)
return "", err
}

// write the private key to a temp file in our signer's temp dir
tmpPrivateKeyFile, err := ioutil.TempFile(dir, "gpg2_privatekey")
tmpPrivateKeyFile, err := os.CreateTemp(dir, "gpg2_privatekey")
if err != nil {
err = fmt.Errorf("gpg2: error creating tempfile for private key: %w", err)
return "", err
Expand All @@ -196,7 +195,7 @@ func createKeyRing(s *GPG2Signer) (dir string, err error) {
err = fmt.Errorf("gpg2: error removing temp private key file %q %w", tmpPrivateKeyFile.Name(), cleanErr)
}
}()
err = ioutil.WriteFile(tmpPrivateKeyFile.Name(), []byte(s.PrivateKey), 0755)
err = os.WriteFile(tmpPrivateKeyFile.Name(), []byte(s.PrivateKey), 0755)
if err != nil {
err = fmt.Errorf("gpg2: error writing private key to tempfile: %w", err)
return "", err
Expand Down Expand Up @@ -253,7 +252,7 @@ func writeGPGConf(gpgHomeDir string) error {
gpgConfPath := filepath.Join(gpgHomeDir, gpgConfFilename)
gpgConfContents := fmt.Sprintf("%s\nkeyring %s\nhomedir %s\n", gpgConfContentsHead, keyRingPath, gpgHomeDir)

err := ioutil.WriteFile(gpgConfPath, []byte(gpgConfContents), 0600)
err := os.WriteFile(gpgConfPath, []byte(gpgConfContents), 0600)
if err != nil {
return err
}
Expand Down Expand Up @@ -290,7 +289,7 @@ func (s *GPG2Signer) SignData(data []byte, options interface{}) (signer.Signatur
keyRingPath := filepath.Join(s.tmpDir, keyRingFilename)

// write the input to a temp file
tmpContentFile, err := ioutil.TempFile(s.tmpDir, fmt.Sprintf("gpg2_%s_input", s.ID))
tmpContentFile, err := os.CreateTemp(s.tmpDir, fmt.Sprintf("gpg2_%s_input", s.ID))
if err != nil {
return nil, fmt.Errorf("gpg2: failed to create tempfile for input to sign: %w", err)
}
Expand All @@ -299,7 +298,7 @@ func (s *GPG2Signer) SignData(data []byte, options interface{}) (signer.Signatur
log.Warnf("gpg2: error removing content file %q: %q", tmpContentFile.Name(), err)
}
}()
err = ioutil.WriteFile(tmpContentFile.Name(), data, 0755)
err = os.WriteFile(tmpContentFile.Name(), data, 0755)
if err != nil {
return nil, fmt.Errorf("gpg2: failed to write tempfile for input to sign: %w", err)
}
Expand Down Expand Up @@ -384,7 +383,7 @@ func (s *GPG2Signer) SignFiles(inputs []signer.NamedUnsignedFile, options interf
}

// create a tmp dir outside the signer GPG home
inputsTmpDir, err := ioutil.TempDir("", fmt.Sprintf("autograph_%s_%s_%s_sign_files", s.Type, s.KeyID, s.Mode))
inputsTmpDir, err := os.MkdirTemp("", fmt.Sprintf("autograph_%s_%s_%s_sign_files", s.Type, s.KeyID, s.Mode))
if err != nil {
err = fmt.Errorf("gpg2: error creating tempdir for debsign: %w", err)
return
Expand All @@ -403,7 +402,7 @@ func (s *GPG2Signer) SignFiles(inputs []signer.NamedUnsignedFile, options interf
return nil, fmt.Errorf("gpg2: cannot sign file %d. Files missing extension .buildinfo, .dsc, or .changes", i)
}
inputFilePath := filepath.Join(inputsTmpDir, input.Name)
err := ioutil.WriteFile(inputFilePath, input.Bytes, 0644)
err := os.WriteFile(inputFilePath, input.Bytes, 0644)
if err != nil {
return nil, fmt.Errorf("gpg2: failed to write tempfile %d for debsign to sign: %w", i, err)
}
Expand Down Expand Up @@ -449,7 +448,7 @@ func (s *GPG2Signer) SignFiles(inputs []signer.NamedUnsignedFile, options interf

// read the signed tempfiles
for i, inputFilePath := range inputFilePaths {
signedFileBytes, err := ioutil.ReadFile(inputFilePath)
signedFileBytes, err := os.ReadFile(inputFilePath)
if err != nil {
return nil, fmt.Errorf("gpg2: failed to read %d %q signed by debsign: %w", i, inputFilePath, err)
}
Expand Down
Loading

0 comments on commit 131295e

Please sign in to comment.