From bcfc7674d2db79a2e5e92208d448d06304d6cbe8 Mon Sep 17 00:00:00 2001 From: davidzhao98 Date: Mon, 22 Jul 2019 12:43:45 -0700 Subject: [PATCH] chore: add get expiry method for sts auth --- auth/sts.go | 9 +++++++++ auth/sts_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/auth/sts.go b/auth/sts.go index 09bb56f8..4e26ff92 100644 --- a/auth/sts.go +++ b/auth/sts.go @@ -83,6 +83,15 @@ func (a *STSAuth) GetToken(*os.File) (string, error) { return a.token, err } +// GetExpiry returns the expiry time of the token if it already exists. Otherwise, +// it returns a zero-valued time.Time struct and an error. +func (a *STSAuth) GetExpiry(*os.File) (time.Time, error) { + if len(a.token) > 0 { + return a.expiry, nil + } + return time.Time{}, fmt.Errorf("Expiry time not set.") +} + func (a *STSAuth) authenticate() error { builtURL := *a.baseURL builtURL.Path = "v2/auth/sts-identity" diff --git a/auth/sts_test.go b/auth/sts_test.go index 42f3b903..8c7e2ee0 100644 --- a/auth/sts_test.go +++ b/auth/sts_test.go @@ -171,6 +171,31 @@ func TestGetTokenSTS(t *testing.T) { })) } +func TestGetExpiry(t *testing.T) { + Convey("A valid STSAuth", t, func() { + a, err := NewSTSAuth("https://test.example.com", "us-west-2") + So(err, ShouldBeNil) + So(a, ShouldNotBeNil) + a.expiry = time.Now() + a.token = "token" + Convey("Should return an expiry time", func() { + exp, err := a.GetExpiry(nil) + So(exp, ShouldNotBeNil) + So(err, ShouldBeNil) + }) + }) + Convey("An unauthenticated STSAuth", t, func() { + a, err := NewSTSAuth("https://test.example.com", "us-west-2") + So(err, ShouldBeNil) + So(a, ShouldNotBeNil) + Convey("Should return an error", func() { + exp, err := a.GetExpiry(nil) + So(exp, ShouldBeZeroValue) + So(err, ShouldNotBeNil) + }) + }) +} + func TestIsAuthenticated(t *testing.T) { Convey("A valid STSAuth", t, func() { a, err := NewSTSAuth("https://test.example.com", "us-west-2")