diff --git a/cmd/osbuild-auth-tests/certificates.go b/cmd/osbuild-auth-tests/certificates.go index beaf14754..7f0d194f1 100644 --- a/cmd/osbuild-auth-tests/certificates.go +++ b/cmd/osbuild-auth-tests/certificates.go @@ -1,10 +1,10 @@ +//go:build integration // +build integration package main import ( "fmt" - "io/ioutil" "log" "os" "os/exec" @@ -37,7 +37,7 @@ func (ckp certificateKeyPair) key() string { } func newSelfSignedCertificateKeyPair(subj string) (*certificateKeyPair, error) { - dir, err := ioutil.TempDir("", "osbuild-auth-tests-") + dir, err := os.MkdirTemp("", "osbuild-auth-tests-") if err != nil { return nil, fmt.Errorf("cannot create a temporary directory for the certificate: %v", err) } @@ -79,7 +79,7 @@ func (c ca) key() string { } func newCA(subj string) (*ca, error) { - baseDir, err := ioutil.TempDir("", "osbuild-auth-tests-ca") + baseDir, err := os.MkdirTemp("", "osbuild-auth-tests-ca") if err != nil { return nil, fmt.Errorf("cannot create a temporary dir for a new CA: %v", err) } @@ -139,7 +139,7 @@ func newCA(subj string) (*ca, error) { } func (c ca) newCertificateKeyPair(subj, extensions, addext string) (*certificateKeyPair, error) { - dir, err := ioutil.TempDir("", "osbuild-auth-tests-") + dir, err := os.MkdirTemp("", "osbuild-auth-tests-") if err != nil { return nil, fmt.Errorf("cannot create a temporary directory for the certificate: %v", err) } diff --git a/cmd/osbuild-auth-tests/main_test.go b/cmd/osbuild-auth-tests/main_test.go index ca6ee3d1c..5aeb79b77 100644 --- a/cmd/osbuild-auth-tests/main_test.go +++ b/cmd/osbuild-auth-tests/main_test.go @@ -8,8 +8,8 @@ import ( "crypto/x509" "encoding/json" "errors" - "io/ioutil" "net/http" + "os" "testing" "github.com/stretchr/testify/require" @@ -24,7 +24,7 @@ type connectionConfig struct { } func createTLSConfig(config *connectionConfig) (*tls.Config, error) { - caCertPEM, err := ioutil.ReadFile(config.CACertFile) + caCertPEM, err := os.ReadFile(config.CACertFile) if err != nil { return nil, err } diff --git a/cmd/osbuild-composer-cli-tests/main_test.go b/cmd/osbuild-composer-cli-tests/main_test.go index 5946fc1e5..773b38b9a 100644 --- a/cmd/osbuild-composer-cli-tests/main_test.go +++ b/cmd/osbuild-composer-cli-tests/main_test.go @@ -6,7 +6,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "log" "os" "os/exec" @@ -160,7 +160,7 @@ func TestStatusCommands(t *testing.T) { } func TestSourcesCommands(t *testing.T) { - sources_toml, err := ioutil.TempFile("", "SOURCES-*.TOML") + sources_toml, err := os.CreateTemp("", "SOURCES-*.TOML") require.NoErrorf(t, err, "Could not create temporary file: %v", err) defer os.Remove(sources_toml.Name()) @@ -317,7 +317,7 @@ func getLogs(t *testing.T, uuid uuid.UUID) string { } func pushBlueprint(t *testing.T, bp *blueprint.Blueprint) { - tmpfile, err := ioutil.TempFile("", "osbuild-test-") + tmpfile, err := os.CreateTemp("", "osbuild-test-") require.Nilf(t, err, "Could not create temporary file: %v", err) defer os.Remove(tmpfile.Name()) @@ -386,10 +386,10 @@ func runComposer(t *testing.T, command ...string) []byte { err = cmd.Start() require.Nilf(t, err, "Could not start command: %v", err) - contents, err := ioutil.ReadAll(stdout) + contents, err := io.ReadAll(stdout) require.NoError(t, err, "Could not read stdout from command") - errcontents, err := ioutil.ReadAll(stderr) + errcontents, err := io.ReadAll(stderr) require.NoError(t, err, "Could not read stderr from command") err = cmd.Wait() @@ -453,8 +453,8 @@ func NewTemporaryWorkDir(t *testing.T, pattern string) TemporaryWorkDir { d.OldWorkDir, err = os.Getwd() require.Nilf(t, err, "os.GetWd: %v", err) - d.Path, err = ioutil.TempDir("", pattern) - require.Nilf(t, err, "ioutil.TempDir: %v", err) + d.Path, err = os.MkdirTemp("", pattern) + require.Nilf(t, err, "os.MkdirTemp: %v", err) err = os.Chdir(d.Path) require.Nilf(t, err, "os.ChDir: %v", err) diff --git a/cmd/osbuild-composer/composer.go b/cmd/osbuild-composer/composer.go index 71a1cb10e..c4ccfe281 100644 --- a/cmd/osbuild-composer/composer.go +++ b/cmd/osbuild-composer/composer.go @@ -6,7 +6,6 @@ import ( "crypto/x509" "errors" "fmt" - "io/ioutil" "log" "net" "net/http" @@ -389,7 +388,7 @@ func createTLSConfig(c *connectionConfig) (*tls.Config, error) { var roots *x509.CertPool if c.CACertFile != "" { - caCertPEM, err := ioutil.ReadFile(c.CACertFile) + caCertPEM, err := os.ReadFile(c.CACertFile) if err != nil { return nil, err } diff --git a/cmd/osbuild-image-tests/main_test.go b/cmd/osbuild-image-tests/main_test.go index 571562c98..0eba212e0 100644 --- a/cmd/osbuild-image-tests/main_test.go +++ b/cmd/osbuild-image-tests/main_test.go @@ -10,7 +10,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "log" "os" "os/exec" @@ -574,7 +573,7 @@ func guessPipelineToExport(rawManifest json.RawMessage) string { // tests the result func runTestcase(t *testing.T, testcase testcaseStruct, store string) { _ = os.Mkdir("/var/lib/osbuild-composer-tests", 0755) - outputDirectory, err := ioutil.TempDir("/var/lib/osbuild-composer-tests", "osbuild-image-tests-*") + outputDirectory, err := os.MkdirTemp("/var/lib/osbuild-composer-tests", "osbuild-image-tests-*") require.NoError(t, err, "error creating temporary output directory") defer func() { @@ -594,7 +593,7 @@ func runTestcase(t *testing.T, testcase testcaseStruct, store string) { // getAllCases returns paths to all testcases in the testcase directory func getAllCases() ([]string, error) { - cases, err := ioutil.ReadDir(constants.TestPaths.TestCasesDirectory) + cases, err := os.ReadDir(constants.TestPaths.TestCasesDirectory) if err != nil { return nil, fmt.Errorf("cannot list test cases: %v", err) } @@ -615,7 +614,7 @@ func getAllCases() ([]string, error) { // runTests opens, parses and runs all the specified testcases func runTests(t *testing.T, cases []string) { _ = os.Mkdir("/var/lib/osbuild-composer-tests", 0755) - store, err := ioutil.TempDir("/var/lib/osbuild-composer-tests", "osbuild-image-tests-*") + store, err := os.MkdirTemp("/var/lib/osbuild-composer-tests", "osbuild-image-tests-*") require.NoError(t, err, "error creating temporary store") defer func() { diff --git a/cmd/osbuild-koji-tests/main_test.go b/cmd/osbuild-koji-tests/main_test.go index 037bd07c6..e4fbe1eda 100644 --- a/cmd/osbuild-koji-tests/main_test.go +++ b/cmd/osbuild-koji-tests/main_test.go @@ -3,6 +3,7 @@ // currently run as a "base test". Instead, it's run as a part of the // koji.sh test because it needs a working Koji instance to pass. +//go:build integration // +build integration package main @@ -12,7 +13,6 @@ import ( "crypto/tls" "crypto/x509" "io" - "io/ioutil" "net/http" "os" "os/exec" @@ -37,14 +37,14 @@ func TestKojiRefund(t *testing.T) { // use the self-signed certificate generated by run-koji-container certPool := x509.NewCertPool() - cert, err := ioutil.ReadFile(shareDir + "/ca-crt.pem") + cert, err := os.ReadFile(shareDir + "/ca-crt.pem") require.NoError(t, err) ok := certPool.AppendCertsFromPEM(cert) require.True(t, ok) transport.TLSClientConfig = &tls.Config{ - RootCAs: certPool, + RootCAs: certPool, MinVersion: tls.VersionTLS12, } @@ -98,14 +98,14 @@ func TestKojiImport(t *testing.T) { // use the self-signed certificate generated by run-koji-container certPool := x509.NewCertPool() - cert, err := ioutil.ReadFile(shareDir + "/ca-crt.pem") + cert, err := os.ReadFile(shareDir + "/ca-crt.pem") require.NoError(t, err) ok := certPool.AppendCertsFromPEM(cert) require.True(t, ok) transport.TLSClientConfig = &tls.Config{ - RootCAs: certPool, + RootCAs: certPool, MinVersion: tls.VersionTLS12, } @@ -125,7 +125,7 @@ func TestKojiImport(t *testing.T) { }() // Create a random file - f, err := ioutil.TempFile("", "osbuild-koji-test-*.qcow2") + f, err := os.CreateTemp("", "osbuild-koji-test-*.qcow2") require.NoError(t, err) defer func() { assert.NoError(t, f.Close()) diff --git a/cmd/osbuild-mock-openid-provider/main.go b/cmd/osbuild-mock-openid-provider/main.go index 88e5051b8..9d2c9d510 100644 --- a/cmd/osbuild-mock-openid-provider/main.go +++ b/cmd/osbuild-mock-openid-provider/main.go @@ -4,10 +4,10 @@ import ( "encoding/base64" "encoding/json" "flag" - "io/ioutil" "log" "math/big" "net/http" + "os" "strings" "time" @@ -46,7 +46,7 @@ func main() { E string `json:"e"` } - rsaPubBytes, err := ioutil.ReadFile(rsaPubPem) + rsaPubBytes, err := os.ReadFile(rsaPubPem) if err != nil { panic(err) } @@ -116,7 +116,7 @@ func main() { token := jwt.NewWithClaims(jwt.SigningMethodRS256, cc) token.Header["kid"] = "key-id" - rsaPrivBytes, err := ioutil.ReadFile(rsaPem) + rsaPrivBytes, err := os.ReadFile(rsaPem) if err != nil { panic(err) } diff --git a/cmd/osbuild-pipeline/main.go b/cmd/osbuild-pipeline/main.go index 3571cc755..85bf98ccf 100644 --- a/cmd/osbuild-pipeline/main.go +++ b/cmd/osbuild-pipeline/main.go @@ -5,7 +5,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "os" "path" @@ -97,7 +96,7 @@ func main() { panic("Could not open compose request: " + err.Error()) } } - file, err := ioutil.ReadAll(reader) + file, err := io.ReadAll(reader) if err != nil { panic("Could not read compose request: " + err.Error()) } diff --git a/cmd/osbuild-playground/main.go b/cmd/osbuild-playground/main.go index f83ca3960..94897af8f 100644 --- a/cmd/osbuild-playground/main.go +++ b/cmd/osbuild-playground/main.go @@ -5,7 +5,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "os" "path" @@ -63,7 +62,7 @@ func main() { panic("Could not open path to image options: " + err.Error()) } } - file, err := ioutil.ReadAll(reader) + file, err := io.ReadAll(reader) if err != nil { panic("Could not read image options: " + err.Error()) } diff --git a/cmd/osbuild-upload-gcp/main.go b/cmd/osbuild-upload-gcp/main.go index 5ea88c17e..7de343a1e 100644 --- a/cmd/osbuild-upload-gcp/main.go +++ b/cmd/osbuild-upload-gcp/main.go @@ -4,7 +4,7 @@ import ( "context" "flag" "fmt" - "io/ioutil" + "os" "github.com/osbuild/osbuild-composer/internal/cloud/gcp" "github.com/sirupsen/logrus" @@ -62,7 +62,7 @@ func main() { var credentials []byte if credentialsPath != "" { var err error - credentials, err = ioutil.ReadFile(credentialsPath) + credentials, err = os.ReadFile(credentialsPath) if err != nil { logrus.Fatalf("[GCP] Error while reading credentials: %v", err) } diff --git a/cmd/osbuild-upload-oci/main.go b/cmd/osbuild-upload-oci/main.go index a65054ae8..55625e181 100644 --- a/cmd/osbuild-upload-oci/main.go +++ b/cmd/osbuild-upload-oci/main.go @@ -3,12 +3,12 @@ package main import ( "crypto/rand" "fmt" - "github.com/osbuild/osbuild-composer/internal/upload/oci" - "github.com/spf13/cobra" - "io/ioutil" "math" "math/big" "os" + + "github.com/osbuild/osbuild-composer/internal/upload/oci" + "github.com/spf13/cobra" ) var ( @@ -77,7 +77,7 @@ func uploaderFromConfig() (oci.Uploader, error) { return nil, fmt.Errorf("when suppling a private key the following args are mandatory as well:" + " fingerprint, tenancy, region, and user-id") } - pk, err := ioutil.ReadFile(privateKeyFile) + pk, err := os.ReadFile(privateKeyFile) if err != nil { return nil, fmt.Errorf("failed to read private key file %w", err) } diff --git a/cmd/osbuild-worker/jobimpl-osbuild.go b/cmd/osbuild-worker/jobimpl-osbuild.go index 3114abd89..b5fecf8d1 100644 --- a/cmd/osbuild-worker/jobimpl-osbuild.go +++ b/cmd/osbuild-worker/jobimpl-osbuild.go @@ -4,7 +4,6 @@ import ( "context" "crypto/rand" "fmt" - "io/ioutil" "math" "math/big" "net/url" @@ -295,7 +294,7 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error { } }() - outputDirectory, err = ioutil.TempDir(impl.Output, job.Id().String()+"-*") + outputDirectory, err = os.MkdirTemp(impl.Output, job.Id().String()+"-*") if err != nil { return fmt.Errorf("error creating temporary output directory: %v", err) } @@ -440,7 +439,7 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error { Datastore: targetOptions.Datastore, } - tempDirectory, err := ioutil.TempDir(impl.Output, job.Id().String()+"-vmware-*") + tempDirectory, err := os.MkdirTemp(impl.Output, job.Id().String()+"-vmware-*") if err != nil { targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorInvalidConfig, err.Error(), nil) break diff --git a/cmd/osbuild-worker/main.go b/cmd/osbuild-worker/main.go index fa767db72..fcfe4b843 100644 --- a/cmd/osbuild-worker/main.go +++ b/cmd/osbuild-worker/main.go @@ -7,7 +7,6 @@ import ( "errors" "flag" "fmt" - "io/ioutil" "os" "path" "strings" @@ -48,7 +47,7 @@ type JobImplementation interface { } func createTLSConfig(config *connectionConfig) (*tls.Config, error) { - caCertPEM, err := ioutil.ReadFile(config.CACertFile) + caCertPEM, err := os.ReadFile(config.CACertFile) if err != nil { return nil, err } @@ -274,7 +273,7 @@ func main() { token := "" if config.Authentication.OfflineTokenPath != "" { - t, err := ioutil.ReadFile(config.Authentication.OfflineTokenPath) + t, err := os.ReadFile(config.Authentication.OfflineTokenPath) if err != nil { logrus.Fatalf("Could not read offline token: %v", err) } @@ -283,7 +282,7 @@ func main() { clientSecret := "" if config.Authentication.ClientSecretPath != "" { - cs, err := ioutil.ReadFile(config.Authentication.ClientSecretPath) + cs, err := os.ReadFile(config.Authentication.ClientSecretPath) if err != nil { logrus.Fatalf("Could not read client secret: %v", err) } diff --git a/internal/auth/jwt_auth_handler.go b/internal/auth/jwt_auth_handler.go index cfb49201b..528d3f64b 100644 --- a/internal/auth/jwt_auth_handler.go +++ b/internal/auth/jwt_auth_handler.go @@ -4,8 +4,8 @@ import ( "context" "crypto/x509" "fmt" - "io/ioutil" "net/http" + "os" "github.com/openshift-online/ocm-sdk-go/authentication" "github.com/openshift-online/ocm-sdk-go/logging" @@ -39,7 +39,7 @@ func BuildJWTAuthHandler(keysURLs []string, caFile, aclFile string, exclude []st if caFile != "" { logger.Warn(context.Background(), "A custom CA is specified to verify jwt tokens, this shouldn't be enabled in a production setting.") - caPEM, err := ioutil.ReadFile(caFile) + caPEM, err := os.ReadFile(caFile) if err != nil { return nil, err } diff --git a/internal/boot/aws.go b/internal/boot/aws.go index 063e8e7d7..cf0d9d242 100644 --- a/internal/boot/aws.go +++ b/internal/boot/aws.go @@ -1,3 +1,4 @@ +//go:build integration // +build integration package boot @@ -6,7 +7,6 @@ import ( "encoding/base64" "errors" "fmt" - "io/ioutil" "os" "github.com/aws/aws-sdk-go/aws" @@ -60,7 +60,7 @@ func encodeBase64(input string) string { // CreateUserData creates cloud-init's user-data that contains user redhat with // the specified public key func CreateUserData(publicKeyFile string) (string, error) { - publicKey, err := ioutil.ReadFile(publicKeyFile) + publicKey, err := os.ReadFile(publicKeyFile) if err != nil { return "", fmt.Errorf("cannot read the public key: %v", err) } diff --git a/internal/boot/azuretest/azure.go b/internal/boot/azuretest/azure.go index 4b9c7d9e6..a83c62111 100644 --- a/internal/boot/azuretest/azure.go +++ b/internal/boot/azuretest/azure.go @@ -1,3 +1,4 @@ +//go:build integration // +build integration package azuretest @@ -6,7 +7,6 @@ import ( "context" "errors" "fmt" - "io/ioutil" "log" "net/url" "os" @@ -137,7 +137,7 @@ func DeleteImageFromAzure(c *azureCredentials, imageName string) error { // readPublicKey reads the public key from a file and returns it as a string func readPublicKey(publicKeyFile string) (string, error) { - publicKey, err := ioutil.ReadFile(publicKeyFile) + publicKey, err := os.ReadFile(publicKeyFile) if err != nil { return "", fmt.Errorf("cannot read the public key file: %v", err) } diff --git a/internal/boot/context-managers.go b/internal/boot/context-managers.go index c66c2ae0d..aa8d9a139 100644 --- a/internal/boot/context-managers.go +++ b/internal/boot/context-managers.go @@ -1,3 +1,4 @@ +//go:build integration // +build integration package boot @@ -5,7 +6,6 @@ package boot import ( "fmt" "io" - "io/ioutil" "log" "os" "os/exec" @@ -38,9 +38,9 @@ func WithNetworkNamespace(f func(ns NetNS) error) error { } // withTempFile provides the function f with a new temporary file -// dir and pattern parameters have the same semantics as in ioutil.TempFile +// dir and pattern parameters have the same semantics as in os.CreateTemp func withTempFile(dir, pattern string, f func(file *os.File) error) error { - tempFile, err := ioutil.TempFile(dir, pattern) + tempFile, err := os.CreateTemp(dir, pattern) if err != nil { return fmt.Errorf("cannot create the temporary file: %v", err) } @@ -56,7 +56,7 @@ func withTempFile(dir, pattern string, f func(file *os.File) error) error { } func withTempDir(dir, pattern string, f func(dir string) error) error { - tempDir, err := ioutil.TempDir(dir, pattern) + tempDir, err := os.MkdirTemp(dir, pattern) if err != nil { return fmt.Errorf("cannot create the temporary directory %v", err) } diff --git a/internal/boot/netns.go b/internal/boot/netns.go index 171f487ed..72b48c5c1 100644 --- a/internal/boot/netns.go +++ b/internal/boot/netns.go @@ -1,3 +1,4 @@ +//go:build integration // +build integration package boot @@ -5,7 +6,6 @@ package boot import ( "context" "fmt" - "io/ioutil" "log" "os" "os/exec" @@ -45,7 +45,7 @@ func newNetworkNamespace() (NetNS, error) { } } - f, err := ioutil.TempFile(netnsDir, "osbuild-composer-namespace") + f, err := os.CreateTemp(netnsDir, "osbuild-composer-namespace") if err != nil { return "", fmt.Errorf("cannot create a tempfile: %v", err) } @@ -90,7 +90,7 @@ func newNetworkNamespace() (NetNS, error) { if err != nil { return "", fmt.Errorf("cannot set up a loopback device in the new namespace: %v", err) } - + // There's no potential command injection vector here /* #nosec G204 */ cmd = exec.Command("mount", "-o", "bind", "/proc/self/ns/net", f.Name()) @@ -134,7 +134,7 @@ func (n NetNS) Path() string { // Delete deletes the namespaces func (n NetNS) Delete() error { // There's no potential command injection vector here - /* #nosec G204 */ + /* #nosec G204 */ cmd := exec.Command("umount", n.Path()) cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout diff --git a/internal/boot/vmwaretest/vmware.go b/internal/boot/vmwaretest/vmware.go index 945f76427..bde4e04ad 100644 --- a/internal/boot/vmwaretest/vmware.go +++ b/internal/boot/vmwaretest/vmware.go @@ -1,3 +1,4 @@ +//go:build integration // +build integration package vmwaretest @@ -5,7 +6,7 @@ package vmwaretest import ( "errors" "fmt" - "io/ioutil" + "io" "os" "path/filepath" "strings" @@ -130,7 +131,7 @@ func runWithStdout(args []string) (string, int) { retcode := cli.Run(args) w.Close() - out, _ := ioutil.ReadAll(r) + out, _ := io.ReadAll(r) os.Stdout = oldStdout return strings.TrimSpace(string(out)), retcode diff --git a/internal/client/client.go b/internal/client/client.go index b0f89d14f..b2b90f90b 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" ) @@ -91,7 +90,7 @@ func NewAPIResponse(body []byte) (*APIResponse, error) { func apiError(resp *http.Response) (*APIResponse, error) { defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } @@ -124,7 +123,7 @@ func GetRaw(socket *http.Client, method, path string) ([]byte, *APIResponse, err } defer body.Close() - bodyBytes, err := ioutil.ReadAll(body) + bodyBytes, err := io.ReadAll(body) if err != nil { return nil, nil, err } @@ -179,7 +178,7 @@ func PostRaw(socket *http.Client, path, body string, headers map[string]string) } defer resp.Body.Close() - responseBody, err := ioutil.ReadAll(resp.Body) + responseBody, err := io.ReadAll(resp.Body) if err != nil { return nil, nil, err } @@ -216,7 +215,7 @@ func DeleteRaw(socket *http.Client, path string) ([]byte, *APIResponse, error) { } defer resp.Body.Close() - responseBody, err := ioutil.ReadAll(resp.Body) + responseBody, err := io.ReadAll(resp.Body) if err != nil { return nil, nil, err } diff --git a/internal/client/compose_test.go b/internal/client/compose_test.go index dd9b5e874..8fd90b0ec 100644 --- a/internal/client/compose_test.go +++ b/internal/client/compose_test.go @@ -24,7 +24,7 @@ package client import ( "fmt" - "io/ioutil" + "io" "net/http" "testing" @@ -162,7 +162,7 @@ func TestUnknownComposeInfoV0(t *testing.T) { // Test compose image for unknown uuid func TestComposeInvalidImageV0(t *testing.T) { - resp, err := WriteComposeImageV0(testState.socket, ioutil.Discard, "c91818f9-8025-47af-89d2-f030d7000c2c") + resp, err := WriteComposeImageV0(testState.socket, io.Discard, "c91818f9-8025-47af-89d2-f030d7000c2c") require.NoError(t, err, "failed with a client error") require.NotNil(t, resp) require.False(t, resp.Status) @@ -173,7 +173,7 @@ func TestComposeInvalidImageV0(t *testing.T) { // Test compose logs for unknown uuid func TestComposeInvalidLogsV0(t *testing.T) { - resp, err := WriteComposeLogsV0(testState.socket, ioutil.Discard, "c91818f9-8025-47af-89d2-f030d7000c2c") + resp, err := WriteComposeLogsV0(testState.socket, io.Discard, "c91818f9-8025-47af-89d2-f030d7000c2c") require.NoError(t, err, "failed with a client error") require.NotNil(t, resp) require.False(t, resp.Status) @@ -184,7 +184,7 @@ func TestComposeInvalidLogsV0(t *testing.T) { // Test compose log for unknown uuid func TestComposeInvalidLogV0(t *testing.T) { - resp, err := WriteComposeLogV0(testState.socket, ioutil.Discard, "c91818f9-8025-47af-89d2-f030d7000c2c") + resp, err := WriteComposeLogV0(testState.socket, io.Discard, "c91818f9-8025-47af-89d2-f030d7000c2c") require.NoError(t, err, "failed with a client error") require.NotNil(t, resp) require.False(t, resp.Status) @@ -195,7 +195,7 @@ func TestComposeInvalidLogV0(t *testing.T) { // Test compose metadata for unknown uuid func TestComposeInvalidMetadataV0(t *testing.T) { - resp, err := WriteComposeMetadataV0(testState.socket, ioutil.Discard, "c91818f9-8025-47af-89d2-f030d7000c2c") + resp, err := WriteComposeMetadataV0(testState.socket, io.Discard, "c91818f9-8025-47af-89d2-f030d7000c2c") require.NoError(t, err, "failed with a client error") require.NotNil(t, resp) require.False(t, resp.Status) @@ -206,7 +206,7 @@ func TestComposeInvalidMetadataV0(t *testing.T) { // Test compose results for unknown uuid func TestComposeInvalidResultsV0(t *testing.T) { - resp, err := WriteComposeResultsV0(testState.socket, ioutil.Discard, "c91818f9-8025-47af-89d2-f030d7000c2c") + resp, err := WriteComposeResultsV0(testState.socket, io.Discard, "c91818f9-8025-47af-89d2-f030d7000c2c") require.NoError(t, err, "failed with a client error") require.NotNil(t, resp) require.False(t, resp.Status) @@ -350,17 +350,17 @@ func TestFailedComposeV0(t *testing.T) { require.Equal(t, buildID, info.ID) // Test requesting the compose logs for the failed build - resp, err = WriteComposeLogsV0(testState.socket, ioutil.Discard, buildID.String()) + resp, err = WriteComposeLogsV0(testState.socket, io.Discard, buildID.String()) require.NoError(t, err, "failed with a client error") require.Nil(t, resp) // Test requesting the compose metadata for the failed build - resp, err = WriteComposeMetadataV0(testState.socket, ioutil.Discard, buildID.String()) + resp, err = WriteComposeMetadataV0(testState.socket, io.Discard, buildID.String()) require.NoError(t, err, "failed with a client error") require.Nil(t, resp) // Test requesting the compose results for the failed build - resp, err = WriteComposeResultsV0(testState.socket, ioutil.Discard, buildID.String()) + resp, err = WriteComposeResultsV0(testState.socket, io.Discard, buildID.String()) require.NoError(t, err, "failed with a client error") require.Nil(t, resp) @@ -455,17 +455,17 @@ func TestFinishedComposeV0(t *testing.T) { require.Equal(t, buildID, info.ID) // Test requesting the compose logs for the finished build - resp, err = WriteComposeLogsV0(testState.socket, ioutil.Discard, buildID.String()) + resp, err = WriteComposeLogsV0(testState.socket, io.Discard, buildID.String()) require.NoError(t, err, "failed with a client error") require.Nil(t, resp) // Test requesting the compose metadata for the finished build - resp, err = WriteComposeMetadataV0(testState.socket, ioutil.Discard, buildID.String()) + resp, err = WriteComposeMetadataV0(testState.socket, io.Discard, buildID.String()) require.NoError(t, err, "failed with a client error") require.Nil(t, resp) // Test requesting the compose results for the finished build - resp, err = WriteComposeResultsV0(testState.socket, ioutil.Discard, buildID.String()) + resp, err = WriteComposeResultsV0(testState.socket, io.Discard, buildID.String()) require.NoError(t, err, "failed with a client error") require.Nil(t, resp) diff --git a/internal/cloud/gcp/gcp.go b/internal/cloud/gcp/gcp.go index 27d454c27..93b85309e 100644 --- a/internal/cloud/gcp/gcp.go +++ b/internal/cloud/gcp/gcp.go @@ -3,7 +3,7 @@ package gcp import ( "context" "fmt" - "io/ioutil" + "os" cloudbuild "cloud.google.com/go/cloudbuild/apiv1" compute "cloud.google.com/go/compute/apiv1" @@ -58,7 +58,7 @@ func New(credentials []byte) (*GCP, error) { // NewFromFile loads the credentials from a file and returns an authenticated // *GCP object instance. func NewFromFile(path string) (*GCP, error) { - gcpCredentials, err := ioutil.ReadFile(path) + gcpCredentials, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("cannot load GCP credentials from file %q: %v", path, err) } diff --git a/internal/container/client_test.go b/internal/container/client_test.go index 7133fad04..e20cd866d 100644 --- a/internal/container/client_test.go +++ b/internal/container/client_test.go @@ -2,7 +2,6 @@ package container_test import ( "context" - "io/ioutil" "os" "testing" "time" @@ -76,7 +75,7 @@ func TestClientAuthFilePath(t *testing.T) { assert.Equal(t, authFilePath, container.GetDefaultAuthFile()) // make sure the file is accessible - _, err = ioutil.ReadFile(authFilePath) + _, err = os.ReadFile(authFilePath) assert.True(t, err == nil || os.IsNotExist(err)) t.Run("XDG_RUNTIME_DIR", func(t *testing.T) { @@ -95,7 +94,7 @@ func TestClientAuthFilePath(t *testing.T) { authFilePath := container.GetDefaultAuthFile() assert.NotEmpty(t, authFilePath) - _, err = ioutil.ReadFile(authFilePath) + _, err = os.ReadFile(authFilePath) assert.True(t, err == nil || os.IsNotExist(err)) }) diff --git a/internal/distro/distro_test_common/distro_test_common.go b/internal/distro/distro_test_common/distro_test_common.go index b29c98be9..8de5edbd0 100644 --- a/internal/distro/distro_test_common/distro_test_common.go +++ b/internal/distro/distro_test_common/distro_test_common.go @@ -3,7 +3,7 @@ package distro_test_common import ( "encoding/json" "fmt" - "io/ioutil" + "os" "path" "path/filepath" "testing" @@ -51,7 +51,7 @@ func TestDistro_Manifest(t *testing.T, pipelinePath string, prefix string, regis Manifest distro.Manifest `json:"manifest,omitempty"` Containers []container.Spec `json:"containers,omitempty"` } - file, err := ioutil.ReadFile(fileName) + file, err := os.ReadFile(fileName) assert.NoErrorf(err, "Could not read test-case '%s': %v", fileName, err) err = json.Unmarshal([]byte(file), &tt) assert.NoErrorf(err, "Could not parse test-case '%s': %v", fileName, err) diff --git a/internal/jsondb/db.go b/internal/jsondb/db.go index 10cef85c1..ce25e72d1 100644 --- a/internal/jsondb/db.go +++ b/internal/jsondb/db.go @@ -18,7 +18,6 @@ package jsondb import ( "encoding/json" "fmt" - "io/ioutil" "os" "path" "path/filepath" @@ -93,7 +92,7 @@ func (db *JSONDatabase) Write(name string, document interface{}) error { // writing succeeded. `writer` gets passed the open file handle to write to and // does not need to take care of closing it. func writeFileAtomically(dir, filename string, mode os.FileMode, writer func(f *os.File) error) error { - tmpfile, err := ioutil.TempFile(dir, filename+"-*.tmp") + tmpfile, err := os.CreateTemp(dir, filename+"-*.tmp") if err != nil { return err } diff --git a/internal/jsondb/db_private_test.go b/internal/jsondb/db_private_test.go index 377cb6bfe..de41ad770 100644 --- a/internal/jsondb/db_private_test.go +++ b/internal/jsondb/db_private_test.go @@ -2,7 +2,6 @@ package jsondb import ( "errors" - "io/ioutil" "os" "path" "testing" @@ -26,14 +25,16 @@ func TestWriteFileAtomically(t *testing.T) { require.NoError(t, err) // ensure that there are no stray temporary files - infos, err := ioutil.ReadDir(dir) + infos, err := os.ReadDir(dir) require.NoError(t, err) require.Equal(t, 1, len(infos)) require.Equal(t, "octopus", infos[0].Name()) - require.Equal(t, perm, infos[0].Mode()) + i, err := infos[0].Info() + require.Nil(t, err) + require.Equal(t, perm, i.Mode()) filename := path.Join(dir, "octopus") - contents, err := ioutil.ReadFile(filename) + contents, err := os.ReadFile(filename) require.NoError(t, err) require.Equal(t, octopus, contents) @@ -51,7 +52,7 @@ func TestWriteFileAtomically(t *testing.T) { require.Error(t, err) // ensure there are no stray temporary files - infos, err := ioutil.ReadDir(dir) + infos, err := os.ReadDir(dir) require.NoError(t, err) require.Equal(t, 0, len(infos)) }) diff --git a/internal/jsondb/db_test.go b/internal/jsondb/db_test.go index fadfa6e87..835585787 100644 --- a/internal/jsondb/db_test.go +++ b/internal/jsondb/db_test.go @@ -1,7 +1,6 @@ package jsondb_test import ( - "io/ioutil" "os" "path" "testing" @@ -42,7 +41,7 @@ func TestDegenerate(t *testing.T) { db := jsondb.New(dir, 0755) // write-only file - err := ioutil.WriteFile(path.Join(dir, "one.json"), []byte("{"), 0600) + err := os.WriteFile(path.Join(dir, "one.json"), []byte("{"), 0600) require.NoError(t, err) var d document @@ -54,7 +53,7 @@ func TestDegenerate(t *testing.T) { func TestCorrupt(t *testing.T) { dir := t.TempDir() - err := ioutil.WriteFile(path.Join(dir, "one.json"), []byte("{"), 0600) + err := os.WriteFile(path.Join(dir, "one.json"), []byte("{"), 0600) require.NoError(t, err) db := jsondb.New(dir, 0755) @@ -66,7 +65,7 @@ func TestCorrupt(t *testing.T) { func TestRead(t *testing.T) { dir := t.TempDir() - err := ioutil.WriteFile(path.Join(dir, "one.json"), []byte("true"), 0600) + err := os.WriteFile(path.Join(dir, "one.json"), []byte("true"), 0600) require.NoError(t, err) db := jsondb.New(dir, 0755) diff --git a/internal/ostree/ostree.go b/internal/ostree/ostree.go index a027ff3ac..337264ae0 100644 --- a/internal/ostree/ostree.go +++ b/internal/ostree/ostree.go @@ -5,9 +5,10 @@ import ( "crypto/x509" "encoding/hex" "fmt" - "io/ioutil" + "io" "net/http" "net/url" + "os" "path" "regexp" "strings" @@ -80,7 +81,7 @@ func ResolveRef(location, ref string, consumerCerts bool, subs *rhsm.Subscriptio } if ca != nil { - caCertPEM, err := ioutil.ReadFile(*ca) + caCertPEM, err := os.ReadFile(*ca) if err != nil { return "", NewResolveRefError("error adding rhsm certificates when resolving ref") } @@ -120,7 +121,7 @@ func ResolveRef(location, ref string, consumerCerts bool, subs *rhsm.Subscriptio if resp.StatusCode != http.StatusOK { return "", NewResolveRefError("ostree repository %q returned status: %s", u.String(), resp.Status) } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return "", NewResolveRefError(fmt.Sprintf("error reading response from ostree repository %q: %v", u.String(), err)) } diff --git a/internal/ostree/test_mtls_server/http_mtls_server.go b/internal/ostree/test_mtls_server/http_mtls_server.go index 66290d636..0a745734f 100644 --- a/internal/ostree/test_mtls_server/http_mtls_server.go +++ b/internal/ostree/test_mtls_server/http_mtls_server.go @@ -5,9 +5,9 @@ import ( "crypto/x509" "errors" "fmt" - "io/ioutil" "net/http" "net/http/httptest" + "os" "path/filepath" ) @@ -30,7 +30,7 @@ func NewMTLSServer(handler http.Handler) (*MTLSServer, error) { clientKeyPath := filepath.Join(certsPath, "client.key") clientCrtPath := filepath.Join(certsPath, "client.crt") - caCertPem, err := ioutil.ReadFile(caPath) + caCertPem, err := os.ReadFile(caPath) if err != nil { return nil, err } diff --git a/internal/rhsm/secrets.go b/internal/rhsm/secrets.go index 9934847a7..e219d9f13 100644 --- a/internal/rhsm/secrets.go +++ b/internal/rhsm/secrets.go @@ -2,7 +2,6 @@ package rhsm import ( "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -65,7 +64,7 @@ func getListOfSubscriptions() ([]subscription, error) { // documented in `man yum.conf`. The same parsing mechanism could // be used for any other repo file in /etc/yum.repos.d/. availableSubscriptionsFile := "/etc/yum.repos.d/redhat.repo" - content, err := ioutil.ReadFile(availableSubscriptionsFile) + content, err := os.ReadFile(availableSubscriptionsFile) if err != nil { if pErr, ok := err.(*os.PathError); ok { if pErr.Err.Error() == "no such file or directory" { diff --git a/internal/rpmmd/repository.go b/internal/rpmmd/repository.go index 5fc7ca470..b956aa053 100644 --- a/internal/rpmmd/repository.go +++ b/internal/rpmmd/repository.go @@ -4,7 +4,6 @@ import ( "crypto/sha256" "encoding/json" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -256,7 +255,7 @@ func LoadAllRepositories(confPaths []string) (DistrosRepoConfigs, error) { for _, confPath := range confPaths { reposPath := filepath.Join(confPath, "repositories") - fileEntries, err := ioutil.ReadDir(reposPath) + fileEntries, err := os.ReadDir(reposPath) if os.IsNotExist(err) { continue } else if err != nil { diff --git a/internal/store/json_test.go b/internal/store/json_test.go index a28827f51..1bdf6f4fd 100644 --- a/internal/store/json_test.go +++ b/internal/store/json_test.go @@ -3,7 +3,7 @@ package store import ( "encoding/json" "fmt" - "io/ioutil" + "os" "path/filepath" "reflect" "testing" @@ -304,7 +304,7 @@ func Test_upgrade(t *testing.T) { require.Greaterf(t, len(fileNames), 0, "No test stores found in %s", testPath) for _, fileName := range fileNames { var storeStruct storeV0 - file, err := ioutil.ReadFile(fileName) + file, err := os.ReadFile(fileName) assert.NoErrorf(err, "Could not read test-store '%s': %v", fileName, err) err = json.Unmarshal([]byte(file), &storeStruct) assert.NoErrorf(err, "Could not parse test-store '%s': %v", fileName, err) diff --git a/internal/test/apicall.go b/internal/test/apicall.go index 7a5610b48..bd1fa70ce 100644 --- a/internal/test/apicall.go +++ b/internal/test/apicall.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "io" - "io/ioutil" "net/http" "net/http/httptest" "testing" @@ -72,7 +71,7 @@ func (a APICall) Do(t *testing.T) APICallResult { a.Handler.ServeHTTP(respRecorder, req) resp := respRecorder.Result() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) require.NoErrorf(t, err, "%s: could not read response body", a.Path) if a.ExpectedStatus != 0 { diff --git a/internal/test/helpers.go b/internal/test/helpers.go index 38910c60e..9a60c0ffa 100644 --- a/internal/test/helpers.go +++ b/internal/test/helpers.go @@ -5,7 +5,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net" "net/http" "net/http/httptest" @@ -107,7 +107,7 @@ func TestRouteWithReply(t *testing.T, api http.Handler, external bool, method, p } var err error - replyJSON, err = ioutil.ReadAll(resp.Body) + replyJSON, err = io.ReadAll(resp.Body) require.NoErrorf(t, err, "%s: could not read response body", path) assert.Equalf(t, expectedStatus, resp.StatusCode, "SendHTTP failed for path %s: %v", path, string(replyJSON)) @@ -147,7 +147,7 @@ func TestTOMLRoute(t *testing.T, api http.Handler, external bool, method, path, t.Skip("This test is for internal testing only") } - replyTOML, err := ioutil.ReadAll(resp.Body) + replyTOML, err := io.ReadAll(resp.Body) require.NoErrorf(t, err, "%s: could not read response body", path) assert.Equalf(t, expectedStatus, resp.StatusCode, "SendHTTP failed for path %s: %v", path, string(replyTOML)) @@ -177,7 +177,7 @@ func TestNonJsonRoute(t *testing.T, api http.Handler, external bool, method, pat response := SendHTTP(api, external, method, path, body) assert.Equalf(t, expectedStatus, response.StatusCode, "%s: status mismatch", path) - responseBodyBytes, err := ioutil.ReadAll(response.Body) + responseBodyBytes, err := io.ReadAll(response.Body) require.NoErrorf(t, err, "%s: could not read response body", path) responseBody := string(responseBodyBytes) @@ -209,7 +209,7 @@ func CompareImageTypes() cmp.Option { // Create a temporary repository func SetUpTemporaryRepository() (string, error) { - dir, err := ioutil.TempDir("/tmp", "osbuild-composer-test-") + dir, err := os.MkdirTemp("/tmp", "osbuild-composer-test-") if err != nil { return "", err } diff --git a/internal/upload/koji/koji.go b/internal/upload/koji/koji.go index e430309c7..5575fd401 100644 --- a/internal/upload/koji/koji.go +++ b/internal/upload/koji/koji.go @@ -17,7 +17,6 @@ import ( "fmt" "hash/adler32" "io" - "io/ioutil" "net/http" "net/url" "os" @@ -325,7 +324,7 @@ func (k *Koji) uploadChunk(chunk []byte, filepath, filename string, offset uint6 defer respData.Body.Close() - body, err := ioutil.ReadAll(respData.Body) + body, err := io.ReadAll(respData.Body) if err != nil { return err } diff --git a/internal/weldr/api.go b/internal/weldr/api.go index db335c370..6b097a053 100644 --- a/internal/weldr/api.go +++ b/internal/weldr/api.go @@ -10,7 +10,6 @@ import ( errors_package "errors" "fmt" "io" - "io/ioutil" "log" "math" "math/big" @@ -812,7 +811,7 @@ func DecodeSourceConfigV0(body io.Reader, contentType string) (source SourceConf } else if contentType == "text/x-toml" { // Read all of body in case it needs to be parsed twice var data []byte - data, err = ioutil.ReadAll(body) + data, err = io.ReadAll(body) if err != nil { return source, err } @@ -845,7 +844,7 @@ func DecodeSourceConfigV1(body io.Reader, contentType string) (source SourceConf } else if contentType == "text/x-toml" { // Read all of body in case it needs to be parsed twice var data []byte - data, err = ioutil.ReadAll(body) + data, err = io.ReadAll(body) if err != nil { return source, err } diff --git a/internal/weldr/api_test.go b/internal/weldr/api_test.go index 83d271b1d..17130d1a4 100644 --- a/internal/weldr/api_test.go +++ b/internal/weldr/api_test.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "math/rand" "net/http" "net/http/httptest" @@ -511,7 +510,7 @@ func TestBlueprintsCustomizationInfoToml(t *testing.T) { } }` resp := test.SendHTTP(api, true, "POST", "/api/v0/blueprints/new", testBlueprint) - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) require.Nil(t, err) require.Equal(t, http.StatusOK, resp.StatusCode, string(body)) @@ -520,7 +519,7 @@ func TestBlueprintsCustomizationInfoToml(t *testing.T) { api.ServeHTTP(recorder, req) resp = recorder.Result() - body, err = ioutil.ReadAll(resp.Body) + body, err = io.ReadAll(resp.Body) require.Nil(t, err) require.Equal(t, http.StatusOK, resp.StatusCode, string(body)) @@ -804,7 +803,7 @@ func TestBlueprintChange(t *testing.T) { test.SendHTTP(api, true, "POST", "/api/v0/blueprints/new", `{"name":"`+id+`","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.2"}`) resp := test.SendHTTP(api, true, "GET", "/api/v0/blueprints/changes/"+id, ``) - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) require.Nil(t, err) require.Equal(t, http.StatusOK, resp.StatusCode) @@ -818,7 +817,7 @@ func TestBlueprintChange(t *testing.T) { // Get the blueprint's oldest commit route := fmt.Sprintf("/api/v1/blueprints/change/%s/%s", id, commit) resp = test.SendHTTP(api, true, "GET", route, ``) - body, err = ioutil.ReadAll(resp.Body) + body, err = io.ReadAll(resp.Body) require.Nil(t, err) require.Equal(t, http.StatusOK, resp.StatusCode) @@ -861,7 +860,7 @@ func TestOldBlueprintsUndo(t *testing.T) { test.TestRoute(t, api, true, "GET", "/api/v0/blueprints/changes/test-old-changes", ``, http.StatusOK, oldBlueprintsUndoResponse, ignoreFields...) resp := test.SendHTTP(api, true, "GET", "/api/v0/blueprints/changes/test-old-changes", ``) - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) require.Nil(t, err) require.Equal(t, http.StatusOK, resp.StatusCode) @@ -899,7 +898,7 @@ func TestNewBlueprintsUndo(t *testing.T) { test.TestRoute(t, api, true, "GET", "/api/v0/blueprints/changes/"+id, ``, http.StatusOK, `{"blueprints":[{"changes":[{"commit":"","message":"Recipe `+id+`, version 0.1.0 saved.","revision":null,"timestamp":""},{"commit":"","message":"Recipe `+id+`, version 0.0.1 saved.","revision":null,"timestamp":""}],"name":"`+id+`","total":2}],"errors":[],"limit":20,"offset":0}`, ignoreFields...) resp := test.SendHTTP(api, true, "GET", "/api/v0/blueprints/changes/"+id, ``) - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) require.Nil(t, err) require.Equal(t, http.StatusOK, resp.StatusCode)