Skip to content

Commit

Permalink
Merge pull request #229 from crimson-gao/master
Browse files Browse the repository at this point in the history
fix build on go 1.13
  • Loading branch information
crimson-gao authored Sep 1, 2023
2 parents 722847e + 4bd1027 commit 487e9c7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15
go-version: 1.13
10 changes: 5 additions & 5 deletions credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 5 additions & 4 deletions credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
Expand All @@ -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
}
Expand All @@ -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/"
Expand Down
6 changes: 3 additions & 3 deletions credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"errors"
"fmt"
io "io"
"io/ioutil"
"net/http"
"os"
"testing"
Expand Down Expand Up @@ -107,15 +107,15 @@ 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)
body = `{"Code": "Success", "AccessKeyID": "xxxx", "AccessKeySecret": "yyyy",
"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)
Expand Down

0 comments on commit 487e9c7

Please sign in to comment.