From 74903bf2b257751bab62020093453b8b8ec65356 Mon Sep 17 00:00:00 2001 From: Tobias Meyer Date: Thu, 26 Oct 2023 21:54:17 +0200 Subject: [PATCH] Delete unused mocks, rename variables and fix imports They were not needed anymore and the linter was complaining. --- auth/mock.go | 42 ----------------------------- auth/tokencache/cache.go | 11 ++++---- auth/tokencache/cache_test.go | 3 ++- auth/tokencache/login.go | 3 ++- auth/tokencache/oauth2config.go | 1 + auth/tokencache/serviceprincipal.go | 3 ++- auth/tokencache/tokensource.go | 3 ++- auth/tokencache/tokensource_test.go | 5 ++-- auth/tokencache/workload.go | 3 ++- config/hcp.go | 3 ++- config/new.go | 5 ++-- config/tokensource.go | 9 ++++--- config/tokensource_test.go | 5 ++-- 13 files changed, 33 insertions(+), 63 deletions(-) delete mode 100644 auth/mock.go diff --git a/auth/mock.go b/auth/mock.go deleted file mode 100644 index 5ea589e2..00000000 --- a/auth/mock.go +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package auth - -import ( - "context" - "fmt" - "time" - - "golang.org/x/oauth2" -) - -// mockBrowser provides a mocked response of the OAuth2 login flow. -type mockBrowser struct{} - -func (b *mockBrowser) GetTokenFromBrowser(ctx context.Context, conf *oauth2.Config) (*oauth2.Token, error) { - tok := oauth2.Token{ - // Cache read expects the access token to look JWT-like - AccessToken: "New.Access.Token", - RefreshToken: "NewRefreshToken", - Expiry: time.Now().Add(time.Hour * 1), - } - return &tok, nil -} - -type mockRefreshTokenSource struct { - err bool -} - -func (m *mockRefreshTokenSource) Token() (*oauth2.Token, error) { - if m.err { - return nil, fmt.Errorf("error") - } - - return &oauth2.Token{ - // Cache read expects the access token to look JWT-like - AccessToken: "Refreshed.Access.Token", - RefreshToken: "RefreshedRefreshToken", - Expiry: time.Now().Add(time.Hour * 1), - }, nil -} diff --git a/auth/tokencache/cache.go b/auth/tokencache/cache.go index 7bb2d424..6cbf8609 100644 --- a/auth/tokencache/cache.go +++ b/auth/tokencache/cache.go @@ -3,10 +3,11 @@ package tokencache import ( "encoding/json" "fmt" - "golang.org/x/oauth2" "os" "path" "time" + + "golang.org/x/oauth2" ) // cache is used to (un-)marshal the cached tokens from/to JSON. @@ -32,13 +33,13 @@ func readCache(cacheFile string) (*cache, error) { } // Read the cache information from the file, if it exists - cacheJson, err := os.ReadFile(cacheFile) + cacheJSON, err := os.ReadFile(cacheFile) if err != nil { return cachedTokens, fmt.Errorf("failed to read credentials cache from %q: %w", cacheFile, err) } // Unmarshal the cached credentials - if err = json.Unmarshal(cacheJson, cachedTokens); err != nil { + if err = json.Unmarshal(cacheJSON, cachedTokens); err != nil { return cachedTokens, fmt.Errorf("failed to unmarshal cached credentials: %w", err) } @@ -51,7 +52,7 @@ func readCache(cacheFile string) (*cache, error) { // future (e.g. by -re-reading the content before writing, locking the file or writing cache information to multiple files). func (c *cache) write(cacheFile string) error { // Marshal the new tokens - cacheJson, err := json.Marshal(c) + cacheJSON, err := json.Marshal(c) if err != nil { return fmt.Errorf("failed to marshal cached tokens: %w", err) } @@ -63,7 +64,7 @@ func (c *cache) write(cacheFile string) error { } // Write the file - err = os.WriteFile(cacheFile, cacheJson, os.FileMode(0600)) + err = os.WriteFile(cacheFile, cacheJSON, os.FileMode(0600)) if err != nil { return fmt.Errorf("failed to write cached credentials to file: %w", err) } diff --git a/auth/tokencache/cache_test.go b/auth/tokencache/cache_test.go index 26ad901c..59a23af6 100644 --- a/auth/tokencache/cache_test.go +++ b/auth/tokencache/cache_test.go @@ -1,9 +1,10 @@ package tokencache import ( - "github.com/stretchr/testify/require" "testing" "time" + + "github.com/stretchr/testify/require" ) func TestCache_ExpiredTokenRemoval(t *testing.T) { diff --git a/auth/tokencache/login.go b/auth/tokencache/login.go index c21362b8..c0612a9d 100644 --- a/auth/tokencache/login.go +++ b/auth/tokencache/login.go @@ -2,8 +2,9 @@ package tokencache import ( "fmt" - "golang.org/x/oauth2" "log" + + "golang.org/x/oauth2" ) // NewLoginTokenSource will create a token source that caches login tokens. Only one login token will be cached at a diff --git a/auth/tokencache/oauth2config.go b/auth/tokencache/oauth2config.go index 643554c3..414e7e6e 100644 --- a/auth/tokencache/oauth2config.go +++ b/auth/tokencache/oauth2config.go @@ -2,6 +2,7 @@ package tokencache import ( "context" + "golang.org/x/oauth2" ) diff --git a/auth/tokencache/serviceprincipal.go b/auth/tokencache/serviceprincipal.go index af1de88c..8be42990 100644 --- a/auth/tokencache/serviceprincipal.go +++ b/auth/tokencache/serviceprincipal.go @@ -2,8 +2,9 @@ package tokencache import ( "fmt" - "golang.org/x/oauth2" "log" + + "golang.org/x/oauth2" ) const sourceTypeServicePrincipal = sourceType("service-principal") diff --git a/auth/tokencache/tokensource.go b/auth/tokencache/tokensource.go index 47a24281..9f009d01 100644 --- a/auth/tokencache/tokensource.go +++ b/auth/tokencache/tokensource.go @@ -3,9 +3,10 @@ package tokencache import ( "context" "fmt" - "golang.org/x/oauth2" "log" "time" + + "golang.org/x/oauth2" ) // sourceType identities the type of token source. diff --git a/auth/tokencache/tokensource_test.go b/auth/tokencache/tokensource_test.go index 67de8827..ecc6fb24 100644 --- a/auth/tokencache/tokensource_test.go +++ b/auth/tokencache/tokensource_test.go @@ -3,12 +3,13 @@ package tokencache import ( "context" "fmt" - requirepkg "github.com/stretchr/testify/require" - "golang.org/x/oauth2" "os" "path" "testing" "time" + + requirepkg "github.com/stretchr/testify/require" + "golang.org/x/oauth2" ) type testTokenSource struct { diff --git a/auth/tokencache/workload.go b/auth/tokencache/workload.go index 8fb2649d..2e892b92 100644 --- a/auth/tokencache/workload.go +++ b/auth/tokencache/workload.go @@ -2,8 +2,9 @@ package tokencache import ( "fmt" - "golang.org/x/oauth2" "log" + + "golang.org/x/oauth2" ) const sourceTypeWorkload = sourceType("workload") diff --git a/config/hcp.go b/config/hcp.go index d103a885..60a8ec0a 100644 --- a/config/hcp.go +++ b/config/hcp.go @@ -6,9 +6,10 @@ package config import ( "crypto/tls" "fmt" - "github.com/hashicorp/hcp-sdk-go/auth/workload" "net/url" + "github.com/hashicorp/hcp-sdk-go/auth/workload" + "github.com/hashicorp/hcp-sdk-go/auth" "github.com/hashicorp/hcp-sdk-go/profile" "golang.org/x/oauth2" diff --git a/config/new.go b/config/new.go index eaf2d6ae..fa357602 100644 --- a/config/new.go +++ b/config/new.go @@ -6,11 +6,12 @@ package config import ( "crypto/tls" "fmt" - "github.com/hashicorp/hcp-sdk-go/profile" - "golang.org/x/oauth2" "io" "log" "net/url" + + "github.com/hashicorp/hcp-sdk-go/profile" + "golang.org/x/oauth2" ) const ( diff --git a/config/tokensource.go b/config/tokensource.go index 58ebf613..57a29bef 100644 --- a/config/tokensource.go +++ b/config/tokensource.go @@ -3,16 +3,17 @@ package config import ( "context" "fmt" + "net/http" + "net/url" + "os" + "path" + "github.com/hashicorp/go-cleanhttp" "github.com/hashicorp/hcp-sdk-go/auth" "github.com/hashicorp/hcp-sdk-go/auth/tokencache" "github.com/hashicorp/hcp-sdk-go/auth/workload" "golang.org/x/oauth2" "golang.org/x/oauth2/clientcredentials" - "net/http" - "net/url" - "os" - "path" ) type sourceType = string diff --git a/config/tokensource_test.go b/config/tokensource_test.go index 9b31fafa..d6b0d7d9 100644 --- a/config/tokensource_test.go +++ b/config/tokensource_test.go @@ -1,11 +1,12 @@ package config import ( + "io/ioutil" + "testing" + "github.com/hashicorp/hcp-sdk-go/auth" "github.com/hashicorp/hcp-sdk-go/auth/workload" requirepkg "github.com/stretchr/testify/require" - "io/ioutil" - "testing" ) func TestTokenSource_GetTokenSource_WithClientCredentials(t *testing.T) {