From b08daf3ca4910402f6225863bb07b98459115465 Mon Sep 17 00:00:00 2001 From: Bill Hamilton Date: Thu, 7 Nov 2024 07:28:47 -0800 Subject: [PATCH 1/8] Added simple access token caching Signed-off-by: Bill Hamilton --- .gitignore | 2 ++ vault/vault.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/.gitignore b/.gitignore index 47a7dae..cc217ba 100644 --- a/.gitignore +++ b/.gitignore @@ -51,3 +51,5 @@ _artifacts mage_output_file.go dist/ /test_config.json +.DS_Store +test.go diff --git a/vault/vault.go b/vault/vault.go index 622b37f..f01873a 100644 --- a/vault/vault.go +++ b/vault/vault.go @@ -8,6 +8,8 @@ import ( "io" "log" "net/http" + "math" + "os" "strings" "time" @@ -58,6 +60,11 @@ type Vault struct { Configuration } +type TokenCache struct { + AccessToken string `json:"access_token"` + ExpiresIn int `json:"expires_in"` +} + // New returns a Vault or an error if the Configuration is invalid func New(config Configuration) (*Vault, error) { if config.Provider == auth.CLIENT { @@ -140,10 +147,42 @@ type accessTokenRequest struct { type accessTokenResponse struct { AccessToken string `json:"accessToken"` + ExpiresIn int `json:"expiresIn"` +} + +func (v Vault) setCacheAccessToken(value string, expiresIn int) error { + cache := TokenCache{} + cache.AccessToken = value + cache.ExpiresIn = (int(time.Now().Unix()) + expiresIn) - int(math.Floor(float64(expiresIn)*0.9)) + + data, _ := json.Marshal(cache) + os.Setenv("SS_AT", string(data)) + return nil +} + +func (v Vault) getCacheAccessToken() (string, bool) { + data, ok := os.LookupEnv("SS_AT") + if !ok { + os.Setenv("SS_AT", "") + return "", ok + } + fmt.Println("FOUND CACHED TOKEN") + cache := TokenCache{} + if err := json.Unmarshal([]byte(data), &cache); err != nil { + return "", false + } + if time.Now().Unix() < int64(cache.ExpiresIn) { + return cache.AccessToken, true + } + return "", false } // getAccessToken returns access token fetched from DSV. func (v Vault) getAccessToken() (string, error) { + accessToken, found := v.getCacheAccessToken() + if found { + return accessToken, nil + } var rBody accessTokenRequest switch v.Provider { case auth.AWS: @@ -178,12 +217,16 @@ func (v Vault) getAccessToken() (string, error) { return "", fmt.Errorf("fetching token: %w", err) } + fmt.Println(string(response)) // TODO: cache the token until it expires. resp := &accessTokenResponse{} if err = json.Unmarshal(response, &resp); err != nil { return "", fmt.Errorf("unmarshalling token response: %w", err) } + fmt.Printf("%+v", resp) + v.setCacheAccessToken(resp.AccessToken, resp.ExpiresIn) + return resp.AccessToken, nil } From cdc9dc8a1a41bd4a7d6d3d01a18f394def6c35f8 Mon Sep 17 00:00:00 2001 From: Bill Hamilton Date: Thu, 7 Nov 2024 08:01:18 -0800 Subject: [PATCH 2/8] Revert "Added simple access token caching" This reverts commit b08daf3ca4910402f6225863bb07b98459115465. --- .gitignore | 2 -- vault/vault.go | 43 ------------------------------------------- 2 files changed, 45 deletions(-) diff --git a/.gitignore b/.gitignore index cc217ba..47a7dae 100644 --- a/.gitignore +++ b/.gitignore @@ -51,5 +51,3 @@ _artifacts mage_output_file.go dist/ /test_config.json -.DS_Store -test.go diff --git a/vault/vault.go b/vault/vault.go index f01873a..622b37f 100644 --- a/vault/vault.go +++ b/vault/vault.go @@ -8,8 +8,6 @@ import ( "io" "log" "net/http" - "math" - "os" "strings" "time" @@ -60,11 +58,6 @@ type Vault struct { Configuration } -type TokenCache struct { - AccessToken string `json:"access_token"` - ExpiresIn int `json:"expires_in"` -} - // New returns a Vault or an error if the Configuration is invalid func New(config Configuration) (*Vault, error) { if config.Provider == auth.CLIENT { @@ -147,42 +140,10 @@ type accessTokenRequest struct { type accessTokenResponse struct { AccessToken string `json:"accessToken"` - ExpiresIn int `json:"expiresIn"` -} - -func (v Vault) setCacheAccessToken(value string, expiresIn int) error { - cache := TokenCache{} - cache.AccessToken = value - cache.ExpiresIn = (int(time.Now().Unix()) + expiresIn) - int(math.Floor(float64(expiresIn)*0.9)) - - data, _ := json.Marshal(cache) - os.Setenv("SS_AT", string(data)) - return nil -} - -func (v Vault) getCacheAccessToken() (string, bool) { - data, ok := os.LookupEnv("SS_AT") - if !ok { - os.Setenv("SS_AT", "") - return "", ok - } - fmt.Println("FOUND CACHED TOKEN") - cache := TokenCache{} - if err := json.Unmarshal([]byte(data), &cache); err != nil { - return "", false - } - if time.Now().Unix() < int64(cache.ExpiresIn) { - return cache.AccessToken, true - } - return "", false } // getAccessToken returns access token fetched from DSV. func (v Vault) getAccessToken() (string, error) { - accessToken, found := v.getCacheAccessToken() - if found { - return accessToken, nil - } var rBody accessTokenRequest switch v.Provider { case auth.AWS: @@ -217,16 +178,12 @@ func (v Vault) getAccessToken() (string, error) { return "", fmt.Errorf("fetching token: %w", err) } - fmt.Println(string(response)) // TODO: cache the token until it expires. resp := &accessTokenResponse{} if err = json.Unmarshal(response, &resp); err != nil { return "", fmt.Errorf("unmarshalling token response: %w", err) } - fmt.Printf("%+v", resp) - v.setCacheAccessToken(resp.AccessToken, resp.ExpiresIn) - return resp.AccessToken, nil } From 304575029ae716a6e1136e329a7306cfcc5cf430 Mon Sep 17 00:00:00 2001 From: Bill Hamilton Date: Thu, 7 Nov 2024 08:10:29 -0800 Subject: [PATCH 3/8] added simple access token caching Signed-off-by: Bill Hamilton --- .gitignore | 2 ++ vault/vault.go | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 47a7dae..8de14e8 100644 --- a/.gitignore +++ b/.gitignore @@ -51,3 +51,5 @@ _artifacts mage_output_file.go dist/ /test_config.json +test.go +.DS_Store diff --git a/vault/vault.go b/vault/vault.go index 622b37f..0960128 100644 --- a/vault/vault.go +++ b/vault/vault.go @@ -8,6 +8,8 @@ import ( "io" "log" "net/http" + "math" + "os" "strings" "time" @@ -58,6 +60,11 @@ type Vault struct { Configuration } +type TokenCache struct { + AccessToken string `json:"access_token"` + ExpiresIn int `json:"expires_in"` +} + // New returns a Vault or an error if the Configuration is invalid func New(config Configuration) (*Vault, error) { if config.Provider == auth.CLIENT { @@ -140,10 +147,42 @@ type accessTokenRequest struct { type accessTokenResponse struct { AccessToken string `json:"accessToken"` + ExpiresIn int `json:"expiresIn"` +} + +func (v Vault) setCacheAccessToken(value string, expiresIn int) error { + + cache := TokenCache{} + cache.AccessToken = value + cache.ExpiresIn = (int(time.Now().Unix()) + expiresIn) - int(math.Floor(float64(expiresIn)*0.9)) + + data, _ := json.Marshal(cache) + os.Setenv("SS_AT", string(data)) + return nil +} + +func (v Vault) getCacheAccessToken() (string, bool) { + data, ok := os.LookupEnv("SS_AT") + if !ok { + os.Setenv("SS_AT", "") + return "", ok + } + cache := TokenCache{} + if err := json.Unmarshal([]byte(data), &cache); err != nil { + return "", false + } + if time.Now().Unix() < int64(cache.ExpiresIn) { + return cache.AccessToken, true + } + return "", false } // getAccessToken returns access token fetched from DSV. func (v Vault) getAccessToken() (string, error) { + accessToken, found := v.getCacheAccessToken() + if found { + return accessToken, nil + } var rBody accessTokenRequest switch v.Provider { case auth.AWS: @@ -183,7 +222,7 @@ func (v Vault) getAccessToken() (string, error) { if err = json.Unmarshal(response, &resp); err != nil { return "", fmt.Errorf("unmarshalling token response: %w", err) } - + v.setCacheAccessToken(resp.AccessToken, resp.ExpiresIn) return resp.AccessToken, nil } From 4eb6932bef07661e2e7d709cbe8420a3807e97d9 Mon Sep 17 00:00:00 2001 From: Bill Hamilton Date: Thu, 7 Nov 2024 08:12:36 -0800 Subject: [PATCH 4/8] added changie description Signed-off-by: Bill Hamilton --- .../\360\237\216\211 New Product Feature-20241107-081213.yaml" | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 ".changes/unreleased/\360\237\216\211 New Product Feature-20241107-081213.yaml" diff --git "a/.changes/unreleased/\360\237\216\211 New Product Feature-20241107-081213.yaml" "b/.changes/unreleased/\360\237\216\211 New Product Feature-20241107-081213.yaml" new file mode 100644 index 0000000..b22cc33 --- /dev/null +++ "b/.changes/unreleased/\360\237\216\211 New Product Feature-20241107-081213.yaml" @@ -0,0 +1,3 @@ +kind: "\U0001F389 New Product Feature" +body: Added simple access token caching feature +time: 2024-11-07T08:12:13.07799-08:00 From 08834ebb452903153669fdbd04382413c127cba4 Mon Sep 17 00:00:00 2001 From: Bill Hamilton Date: Thu, 7 Nov 2024 08:24:23 -0800 Subject: [PATCH 5/8] gofmt files Signed-off-by: Bill Hamilton --- vault/vault.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vault/vault.go b/vault/vault.go index 0960128..76db27f 100644 --- a/vault/vault.go +++ b/vault/vault.go @@ -7,8 +7,8 @@ import ( "fmt" "io" "log" - "net/http" "math" + "net/http" "os" "strings" "time" @@ -147,7 +147,7 @@ type accessTokenRequest struct { type accessTokenResponse struct { AccessToken string `json:"accessToken"` - ExpiresIn int `json:"expiresIn"` + ExpiresIn int `json:"expiresIn"` } func (v Vault) setCacheAccessToken(value string, expiresIn int) error { From d7827cfe8005f99d5b7c6b7864c8e48846ca9f11 Mon Sep 17 00:00:00 2001 From: Bill Hamilton Date: Thu, 7 Nov 2024 08:30:30 -0800 Subject: [PATCH 6/8] more file formatting Signed-off-by: Bill Hamilton --- vault/vault.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/vault/vault.go b/vault/vault.go index 76db27f..1a20111 100644 --- a/vault/vault.go +++ b/vault/vault.go @@ -150,15 +150,17 @@ type accessTokenResponse struct { ExpiresIn int `json:"expiresIn"` } -func (v Vault) setCacheAccessToken(value string, expiresIn int) error { - +func (v Vault) setCacheAccessToken(value string, expiresIn int) bool { cache := TokenCache{} cache.AccessToken = value cache.ExpiresIn = (int(time.Now().Unix()) + expiresIn) - int(math.Floor(float64(expiresIn)*0.9)) - data, _ := json.Marshal(cache) + data, err := json.Marshal(cache) + if err != nil { + return false + } os.Setenv("SS_AT", string(data)) - return nil + return true } func (v Vault) getCacheAccessToken() (string, bool) { @@ -222,7 +224,10 @@ func (v Vault) getAccessToken() (string, error) { if err = json.Unmarshal(response, &resp); err != nil { return "", fmt.Errorf("unmarshalling token response: %w", err) } - v.setCacheAccessToken(resp.AccessToken, resp.ExpiresIn) + ok := v.setCacheAccessToken(resp.AccessToken, resp.ExpiresIn) + if !ok { + return "", fmt.Errorf("unable to cache access token") + } return resp.AccessToken, nil } From 23169cb4c710111ceb1fc481de010d6227b12b9a Mon Sep 17 00:00:00 2001 From: Bill Hamilton Date: Thu, 7 Nov 2024 09:33:13 -0800 Subject: [PATCH 7/8] spelling modification Signed-off-by: Bill Hamilton --- vault/vault.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vault/vault.go b/vault/vault.go index 1a20111..7e1987e 100644 --- a/vault/vault.go +++ b/vault/vault.go @@ -151,9 +151,10 @@ type accessTokenResponse struct { } func (v Vault) setCacheAccessToken(value string, expiresIn int) bool { + percentage := 0.9 cache := TokenCache{} cache.AccessToken = value - cache.ExpiresIn = (int(time.Now().Unix()) + expiresIn) - int(math.Floor(float64(expiresIn)*0.9)) + cache.ExpiresIn = (int(time.Now().Unix()) + expiresIn) - int(math.Floor(float64(expiresIn)*percentage)) data, err := json.Marshal(cache) if err != nil { @@ -209,7 +210,6 @@ func (v Vault) getAccessToken() (string, error) { request, err := json.Marshal(&rBody) if err != nil { - return "", fmt.Errorf("marshalling token request body: %w", err) } url := v.urlFor("token", "") @@ -222,7 +222,7 @@ func (v Vault) getAccessToken() (string, error) { // TODO: cache the token until it expires. resp := &accessTokenResponse{} if err = json.Unmarshal(response, &resp); err != nil { - return "", fmt.Errorf("unmarshalling token response: %w", err) + return "", fmt.Errorf("unmarshaling token response: %w", err) } ok := v.setCacheAccessToken(resp.AccessToken, resp.ExpiresIn) if !ok { From 5dd43242ada7d6e95c6855e74565c622c2744329 Mon Sep 17 00:00:00 2001 From: Bill Hamilton Date: Mon, 11 Nov 2024 09:40:26 -0800 Subject: [PATCH 8/8] modified nolint msg Signed-off-by: Bill Hamilton --- vault/vault.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vault/vault.go b/vault/vault.go index 7e1987e..096b92b 100644 --- a/vault/vault.go +++ b/vault/vault.go @@ -60,6 +60,7 @@ type Vault struct { Configuration } +//nolint:tagliatelle // the json is coming from an external API call type TokenCache struct { AccessToken string `json:"access_token"` ExpiresIn int `json:"expires_in"` @@ -145,6 +146,7 @@ type accessTokenRequest struct { AwsHeaders string `json:"aws_headers,omitempty"` } +//nolint:tagliatelle // the json is coming from an external API call type accessTokenResponse struct { AccessToken string `json:"accessToken"` ExpiresIn int `json:"expiresIn"`