Skip to content

Commit

Permalink
Replace deprecated ioutil functions with io and os equivalents
Browse files Browse the repository at this point in the history
  • Loading branch information
aidan-mundy committed Dec 19, 2023
1 parent fde9371 commit f0960bc
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 28 deletions.
3 changes: 1 addition & 2 deletions auth/cred_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -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)
}
14 changes: 7 additions & 7 deletions auth/cred_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"testing"

"github.com/hashicorp/hcp-sdk-go/auth/workload"
Expand Down Expand Up @@ -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))

Expand All @@ -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))

Expand All @@ -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))
Expand All @@ -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))

Expand All @@ -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))

Expand Down Expand Up @@ -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))

Expand Down
11 changes: 5 additions & 6 deletions auth/workload/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions auth/workload/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
)

Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions auth/workload/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package workload

import (
"io"
"io/ioutil"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions auth/workload/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"sync"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions auth/workload/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"time"
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions config/tokensource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package config

import (
"io/ioutil"
"os"
"testing"

"github.com/hashicorp/hcp-sdk-go/auth"
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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))

Expand Down
4 changes: 2 additions & 2 deletions config/with_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package config
import (
"crypto/tls"
"fmt"
"io/ioutil"
"math/rand"
"os"
"testing"

"github.com/hashicorp/hcp-sdk-go/auth"
Expand Down Expand Up @@ -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))

Expand Down

0 comments on commit f0960bc

Please sign in to comment.