From ecf2bfe569fc7184954bf7eb54c059f2cbedf4ba Mon Sep 17 00:00:00 2001 From: crimson <1291463831@qq.com> Date: Fri, 1 Sep 2023 13:28:19 +0800 Subject: [PATCH 1/3] fix build 1.13 --- credentials_provider.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/credentials_provider.go b/credentials_provider.go index 208fa2b8..812a1d0f 100644 --- a/credentials_provider.go +++ b/credentials_provider.go @@ -3,7 +3,7 @@ package sls import ( "encoding/json" "fmt" - io "io" + "io/ioutil" "net/http" "strings" "time" @@ -207,7 +207,7 @@ func newEcsRamRoleReqBuilder(urlPrefix, ramRole string) func() (*http.Request, e // Parse ECS Ram Role http response, convert it to TempCredentials func ecsRamRoleParser(resp *http.Response) (*TempCredentials, error) { // 1. read body - data, err := io.ReadAll(resp.Body) + data, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("fail to read http resp body: %w", err) } From afce62ec1d45b01a79b719129226124990e10222 Mon Sep 17 00:00:00 2001 From: crimson <1291463831@qq.com> Date: Fri, 1 Sep 2023 14:38:01 +0800 Subject: [PATCH 2/3] fix build on go 1.13 --- credentials.go | 10 +++++----- credentials_provider.go | 9 +++++---- credentials_test.go | 6 +++--- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/credentials.go b/credentials.go index bfbdc95f..1b2e5e76 100644 --- a/credentials.go +++ b/credentials.go @@ -51,19 +51,19 @@ func (t *TempCredentials) WithExpiredFactor(factor float64) *TempCredentials { // Returns true if credentials has expired already or will expire soon. func (t *TempCredentials) ShouldRefresh() bool { - now := time.Now().UnixMilli() - if now >= t.expirationInMills { + nowInMills := time.Now().UnixNano() / 1e6 + if nowInMills >= t.expirationInMills { return true } duration := (float64)(t.expirationInMills-t.lastUpdatedInMills) * t.expiredFactor if duration < 0.0 { // check here duration = 0 } - return (now - t.lastUpdatedInMills) >= int64(duration) + return (nowInMills - t.lastUpdatedInMills) >= int64(duration) } // Returns true if credentials has expired already. func (t *TempCredentials) HasExpired() bool { - now := time.Now().UnixMilli() - return now >= t.expirationInMills + nowInMills := time.Now().UnixNano() / 1e6 + return nowInMills >= t.expirationInMills } diff --git a/credentials_provider.go b/credentials_provider.go index 812a1d0f..bf4e6791 100644 --- a/credentials_provider.go +++ b/credentials_provider.go @@ -142,7 +142,7 @@ func updateFuncFetcher(updateFunc UpdateTokenFunction) CredentialsFetcher { return nil, fmt.Errorf("updateTokenFunc result not valid, expirationTime:%s", expireTime.Format(time.RFC3339)) } - return NewTempCredentials(id, secret, token, expireTime.UnixMilli(), -1), nil + return NewTempCredentials(id, secret, token, expireTime.UnixNano()/1e6, -1), nil } } @@ -168,7 +168,7 @@ func (adp *UpdateFuncProviderAdapter) GetCredentials() (Credentials, error) { adp.cred.Store(&res.Credentials) adp.expirationInMills.Store(res.expirationInMills) level.Debug(Logger).Log("reason", "updateTokenFunc fetch new credentials succeed", - "expirationTime", time.UnixMilli(res.expirationInMills).Format(CRED_TIME_FORMAT), + "expirationTime", time.Unix(res.expirationInMills/1e3, res.expirationInMills%1e3*1e6).Format(CRED_TIME_FORMAT), ) return res.Credentials, nil } @@ -181,11 +181,12 @@ func (adp *UpdateFuncProviderAdapter) shouldRefresh() bool { return true } now := time.Now() - return time.UnixMilli(adp.expirationInMills.Load()).Sub(now) <= adp.advanceDuration + millis := adp.expirationInMills.Load() + return time.Unix(millis/1e3, millis%1e3*1e6).Sub(now) <= adp.advanceDuration } func checkSTSTokenValid(accessKeyID, accessKeySecret, securityToken string, expirationTime time.Time) bool { - return accessKeyID != "" && accessKeySecret != "" && expirationTime.UnixMilli() > 0 + return accessKeyID != "" && accessKeySecret != "" && expirationTime.UnixNano() > 0 } const ECS_RAM_ROLE_URL_PREFIX = "http://100.100.100.200/latest/meta-data/ram/security-credentials/" diff --git a/credentials_test.go b/credentials_test.go index bddc66c2..b4c5cc84 100644 --- a/credentials_test.go +++ b/credentials_test.go @@ -4,7 +4,7 @@ import ( "bytes" "errors" "fmt" - io "io" + "io/ioutil" "net/http" "os" "testing" @@ -107,7 +107,7 @@ func TestBuilderParser(t *testing.T) { body := `` resp := http.Response{ - Body: io.NopCloser(bytes.NewBufferString(body)), + Body: ioutil.NopCloser(bytes.NewBufferString(body)), } _, err = ecsRamRoleParser(&resp) assert.Error(t, err) @@ -115,7 +115,7 @@ func TestBuilderParser(t *testing.T) { "SecurityToken": "zzzz", "Expiration": 234, "LastUpdated": 456 }` resp = http.Response{ - Body: io.NopCloser(bytes.NewBufferString(body)), + Body: ioutil.NopCloser(bytes.NewBufferString(body)), } cred, err := ecsRamRoleParser(&resp) assert.NoError(t, err) From 4bd102778c3fe395bffa2a985455a20de3a5a114 Mon Sep 17 00:00:00 2001 From: crimson <1291463831@qq.com> Date: Fri, 1 Sep 2023 14:43:00 +0800 Subject: [PATCH 3/3] build actions use 1.13 --- .github/workflows/go.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 82406c3b..008e5726 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -16,4 +16,4 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.15 + go-version: 1.13