From f0960bc0a0cce1cf886b27bc7ac0c3e692ed9133 Mon Sep 17 00:00:00 2001 From: Aidan Mundy Date: Tue, 19 Dec 2023 13:59:40 -0500 Subject: [PATCH] Replace deprecated `ioutil` functions with `io` and `os` equivalents --- auth/cred_file.go | 3 +-- auth/cred_file_test.go | 14 +++++++------- auth/workload/aws.go | 11 +++++------ auth/workload/file.go | 3 +-- auth/workload/file_test.go | 4 ++-- auth/workload/provider.go | 3 +-- auth/workload/url.go | 3 +-- config/tokensource_test.go | 6 +++--- config/with_test.go | 4 ++-- 9 files changed, 23 insertions(+), 28 deletions(-) diff --git a/auth/cred_file.go b/auth/cred_file.go index 59fdfc2d..8b2eb0dd 100644 --- a/auth/cred_file.go +++ b/auth/cred_file.go @@ -7,7 +7,6 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "os" "path/filepath" @@ -184,5 +183,5 @@ func WriteCredentialFile(path string, cf *CredentialFile) error { return err } - return ioutil.WriteFile(path, data, files.FileMode) + return os.WriteFile(path, data, files.FileMode) } diff --git a/auth/cred_file_test.go b/auth/cred_file_test.go index 01535522..7c456d9b 100644 --- a/auth/cred_file_test.go +++ b/auth/cred_file_test.go @@ -8,8 +8,8 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "math/rand" + "os" "testing" "github.com/hashicorp/hcp-sdk-go/auth/workload" @@ -121,7 +121,7 @@ func TestReadCredentialFile(t *testing.T) { }, } - f, err := ioutil.TempFile("", "") + f, err := os.CreateTemp("", "") r.NoError(err) r.NoError(WriteCredentialFile(f.Name(), cf)) @@ -145,7 +145,7 @@ func TestReadCredentialFile(t *testing.T) { }, } - f, err := ioutil.TempFile("", "") + f, err := os.CreateTemp("", "") r.NoError(err) r.NoError(WriteCredentialFile(f.Name(), cf)) @@ -158,7 +158,7 @@ func TestReadCredentialFile(t *testing.T) { data, err := json.Marshal("hello, world!") r.NoError(err) - f, err := ioutil.TempFile("", "") + f, err := os.CreateTemp("", "") r.NoError(err) _, err = io.Copy(f, bytes.NewBuffer(data)) @@ -181,7 +181,7 @@ func TestGetDefaultCredentialFile(t *testing.T) { }, } - f, err := ioutil.TempFile("", "") + f, err := os.CreateTemp("", "") r.NoError(err) r.NoError(WriteCredentialFile(f.Name(), cf)) @@ -202,7 +202,7 @@ func TestGetDefaultCredentialFile(t *testing.T) { }, } - f, err := ioutil.TempFile("", "") + f, err := os.CreateTemp("", "") r.NoError(err) r.NoError(WriteCredentialFile(f.Name(), cf)) @@ -242,7 +242,7 @@ func Test_WriteCredentialFile(t *testing.T) { }, } - f, err := ioutil.TempFile("", "") + f, err := os.CreateTemp("", "") r.NoError(err) r.NoError(WriteCredentialFile(f.Name(), cf)) diff --git a/auth/workload/aws.go b/auth/workload/aws.go index 1ec391f9..8097ba02 100644 --- a/auth/workload/aws.go +++ b/auth/workload/aws.go @@ -12,7 +12,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "os" "path" @@ -301,7 +300,7 @@ func (s *awsRequestSigner) getSessionToken(ctx context.Context) error { } defer resp.Body.Close() - respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20)) + respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) if err != nil { return fmt.Errorf("failed reading AWS session token response from metadata endpoint: %v", err) } @@ -338,7 +337,7 @@ func (s *awsRequestSigner) getRegion(ctx context.Context) error { } defer resp.Body.Close() - respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20)) + respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) if err != nil { return fmt.Errorf("failed reading AWS region response from metadata endpoint: %v", err) @@ -380,7 +379,7 @@ func (s *awsRequestSigner) getCredentials(ctx context.Context) error { } defer resp.Body.Close() - respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20)) + respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) if err != nil { return fmt.Errorf("failed reading AWS security credential response from metadata endpoint: %v", err) @@ -414,7 +413,7 @@ func (s *awsRequestSigner) getRoleName(ctx context.Context) (string, error) { } defer resp.Body.Close() - respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20)) + respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) if err != nil { return "", fmt.Errorf("failed reading AWS security credential response from metadata endpoint: %v", err) @@ -512,7 +511,7 @@ func requestDataHash(req *http.Request) (string, error) { } defer requestBody.Close() - requestData, err = ioutil.ReadAll(io.LimitReader(requestBody, 1<<20)) + requestData, err = io.ReadAll(io.LimitReader(requestBody, 1<<20)) if err != nil { return "", err } diff --git a/auth/workload/file.go b/auth/workload/file.go index ac30b564..1edff5a3 100644 --- a/auth/workload/file.go +++ b/auth/workload/file.go @@ -7,7 +7,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" ) @@ -40,7 +39,7 @@ func (fc *FileCredentialSource) token() (string, error) { defer credFile.Close() // Read the file but limit the size we read to a MB - credBytes, err := ioutil.ReadAll(io.LimitReader(credFile, 1<<20)) + credBytes, err := io.ReadAll(io.LimitReader(credFile, 1<<20)) if err != nil { return "", fmt.Errorf("failed to read credential file: %v", err) } diff --git a/auth/workload/file_test.go b/auth/workload/file_test.go index b2644d8c..cd043de8 100644 --- a/auth/workload/file_test.go +++ b/auth/workload/file_test.go @@ -5,7 +5,7 @@ package workload import ( "io" - "io/ioutil" + "os" "strings" "testing" @@ -127,7 +127,7 @@ func TestFileCredentialSource_token(t *testing.T) { require := require.New(t) // Create a temp file with the given value - f, err := ioutil.TempFile("", "file_cred_source") + f, err := os.CreateTemp("", "file_cred_source") require.NoError(err) _, err = io.Copy(f, strings.NewReader(tt.fileContent)) require.NoError(err) diff --git a/auth/workload/provider.go b/auth/workload/provider.go index 8e055db7..38af0f94 100644 --- a/auth/workload/provider.go +++ b/auth/workload/provider.go @@ -9,7 +9,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "net/url" "sync" @@ -183,7 +182,7 @@ func (p *Provider) Token() (*oauth2.Token, error) { defer resp.Body.Close() // Read the body - body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20)) + body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) if err != nil { return nil, err } diff --git a/auth/workload/url.go b/auth/workload/url.go index 791054e5..2553038f 100644 --- a/auth/workload/url.go +++ b/auth/workload/url.go @@ -7,7 +7,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "net/http" "net/url" "time" @@ -70,7 +69,7 @@ func (uc *URLCredentialSource) token() (string, error) { defer resp.Body.Close() // Read the response - respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20)) + respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) if err != nil { return "", fmt.Errorf("failed reading body in subject token response: %v", err) } diff --git a/config/tokensource_test.go b/config/tokensource_test.go index 6aa31510..0121fc60 100644 --- a/config/tokensource_test.go +++ b/config/tokensource_test.go @@ -4,7 +4,7 @@ package config import ( - "io/ioutil" + "os" "testing" "github.com/hashicorp/hcp-sdk-go/auth" @@ -64,7 +64,7 @@ func TestTokenSource_GetTokenSource_CredentialFile_Workload(t *testing.T) { }, } - f, err := ioutil.TempFile("", "") + f, err := os.CreateTemp("", "") require.NoError(err) require.NoError(auth.WriteCredentialFile(f.Name(), cf)) @@ -98,7 +98,7 @@ func TestTokenSource_GetTokenSource_CredentialFile_ServicePrincipal(t *testing.T }, } - f, err := ioutil.TempFile("", "") + f, err := os.CreateTemp("", "") require.NoError(err) require.NoError(auth.WriteCredentialFile(f.Name(), cf)) diff --git a/config/with_test.go b/config/with_test.go index 61973353..ecccd9bb 100644 --- a/config/with_test.go +++ b/config/with_test.go @@ -6,8 +6,8 @@ package config import ( "crypto/tls" "fmt" - "io/ioutil" "math/rand" + "os" "testing" "github.com/hashicorp/hcp-sdk-go/auth" @@ -165,7 +165,7 @@ func TestWith_CredentialFilePath(t *testing.T) { ClientSecret: "456", }, } - f, err := ioutil.TempFile("", "") + f, err := os.CreateTemp("", "") require.NoError(err) require.NoError(auth.WriteCredentialFile(f.Name(), cf))