From 4a6fa861ef8d04cad12ed58fd6b2ac15890a6ba3 Mon Sep 17 00:00:00 2001 From: Aidan Mundy Date: Tue, 19 Dec 2023 15:53:15 -0500 Subject: [PATCH] Upgrade Go to 1.20 (#231) * Upgrade Go to 1.20 from 1.18 * Add changelog * Remove redundant code related to an older Go version * Bump golangci-lint version in CI * Fix `golangci-config.yml` format to work with newer versions of golangci-lint * Replace deprecated `ioutil` functions with `io` and `os` equivalents --- .changelog/231.txt | 3 +++ .github/workflows/create-release.yml | 2 +- .github/workflows/test.yml | 2 +- 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/hcp.go | 4 ++-- config/tls.go | 17 ----------------- config/tokensource_test.go | 6 +++--- config/with.go | 6 +++--- config/with_test.go | 4 ++-- go.mod | 2 +- golangci-config.yml | 11 ++++++----- 17 files changed, 40 insertions(+), 58 deletions(-) create mode 100644 .changelog/231.txt delete mode 100644 config/tls.go diff --git a/.changelog/231.txt b/.changelog/231.txt new file mode 100644 index 00000000..e84e0537 --- /dev/null +++ b/.changelog/231.txt @@ -0,0 +1,3 @@ +```release-note:improvement +Update to Go 1.20 per the [Go support policy](https://go.dev/doc/devel/release#policy). +``` diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml index 7162bdb5..9fdf1b99 100644 --- a/.github/workflows/create-release.yml +++ b/.github/workflows/create-release.yml @@ -40,7 +40,7 @@ jobs: GOPRIVATE: 'github.com/hashicorp/*' run: | go install github.com/hashicorp/go-changelog/cmd/changelog-build@6ec9be372335f39c5df27e232c3669db7f5183a5 - go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.49.0 + go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.2 - name: Run Unit Tests and Linter run: make test-ci diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4dc1e2e1..253f99b9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -28,7 +28,7 @@ jobs: - name: Install Dependencies run: | go mod download - go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.49.0 + go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.2 go mod tidy - name: Run Tests 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/hcp.go b/config/hcp.go index 52257014..4f76394f 100644 --- a/config/hcp.go +++ b/config/hcp.go @@ -149,7 +149,7 @@ func (c *hcpConfig) APIAddress() string { } func (c *hcpConfig) APITLSConfig() *tls.Config { - return cloneTLSConfig(c.apiTLSConfig) + return c.apiTLSConfig.Clone() } func (c *hcpConfig) SCADAAddress() string { @@ -157,7 +157,7 @@ func (c *hcpConfig) SCADAAddress() string { } func (c *hcpConfig) SCADATLSConfig() *tls.Config { - return cloneTLSConfig(c.scadaTLSConfig) + return c.scadaTLSConfig.Clone() } func (c *hcpConfig) validate() error { diff --git a/config/tls.go b/config/tls.go deleted file mode 100644 index ccdc0e38..00000000 --- a/config/tls.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package config - -import "crypto/tls" - -// cloneTLSConfig will shallow clone a TLS. tls.Config already has a Clone method -// but versions before Go 1.15 did not allow to clone nil. -// TODO: remove once the SDK only supports Go 1.16 and newer. -func cloneTLSConfig(original *tls.Config) *tls.Config { - if original == nil { - return nil - } - - return original.Clone() -} 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.go b/config/with.go index 030acccc..dcc0a80a 100644 --- a/config/with.go +++ b/config/with.go @@ -45,7 +45,7 @@ func WithWorkloadIdentity(providerConfig *workload.IdentityProviderConfig) HCPCo func WithAPI(address string, tlsConfig *tls.Config) HCPConfigOption { return func(config *hcpConfig) error { config.apiAddress = address - config.apiTLSConfig = cloneTLSConfig(tlsConfig) + config.apiTLSConfig = tlsConfig.Clone() return nil } @@ -60,7 +60,7 @@ func WithAPI(address string, tlsConfig *tls.Config) HCPConfigOption { func WithSCADA(address string, tlsConfig *tls.Config) HCPConfigOption { return func(config *hcpConfig) error { config.scadaAddress = address - config.scadaTLSConfig = cloneTLSConfig(tlsConfig) + config.scadaTLSConfig = tlsConfig.Clone() return nil } @@ -105,7 +105,7 @@ func WithAuth(authURL string, tlsConfig *tls.Config) HCPConfigOption { } config.authURL = parsedAuthURL - config.authTLSConfig = cloneTLSConfig(tlsConfig) + config.authTLSConfig = tlsConfig.Clone() // Ensure the OAuth2 endpoints are updated with the new auth URL config.oauth2Config.Endpoint.AuthURL = authURL + "/oauth2/auth" 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)) diff --git a/go.mod b/go.mod index d772f4c5..905db9d9 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/hashicorp/hcp-sdk-go -go 1.18 +go 1.20 retract v0.26.0 // Pushed accidentally diff --git a/golangci-config.yml b/golangci-config.yml index afb55d3c..363be48d 100644 --- a/golangci-config.yml +++ b/golangci-config.yml @@ -23,11 +23,12 @@ linters: linters-settings: depguard: - list-type: blacklist - packages: - - github.com/gogo/status - - github.com/gogo/codes - - github.com/gogo/protobuf + rules: + main: + deny: + - pkg: "github.com/gogo/status" + - pkg: "github.com/gogo/codes" + - pkg: "github.com/gogo/protobuf" misspell: locale: US