Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check credential expiration timestamp when generating tokens #737

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,16 @@ func (g generator) GetWithSTS(clusterID string, stsAPI stsiface.STSAPI) (Token,
return Token{}, err
}

// Fetch the timestamp when the credentials we're going to use for signing will not be valid anymore
// This operation is potentially racey, but the worst case is that we expire a token early
// Not all credential providers support this, so we ignore any returned errors
credentialsExpiration, _ := request.Config.Credentials.ExpiresAt()

// Set token expiration to 1 minute before the presigned URL expires for some cushion
tokenExpiration := g.nowFunc().Local().Add(presignedURLExpiration - 1*time.Minute)
if !credentialsExpiration.IsZero() && credentialsExpiration.Before(tokenExpiration) {
tokenExpiration = credentialsExpiration.Add(-1 * time.Minute)
}
// TODO: this may need to be a constant-time base64 encoding
return Token{v1Prefix + base64.RawURLEncoding.EncodeToString([]byte(presignedURLString)), tokenExpiration}, nil
}
Expand Down
62 changes: 48 additions & 14 deletions pkg/token/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,10 @@ func Test_getDefaultHostNameForRegion(t *testing.T) {
func TestGetWithSTS(t *testing.T) {
clusterID := "test-cluster"

// Example non-real credentials
decodedAkid, _ := base64.StdEncoding.DecodeString("QVNJQVIyVEc0NFY2QVMzWlpFN0M=")
decodedSk, _ := base64.StdEncoding.DecodeString("NEtENWNudEdjVm1MV1JkRjV3dk5SdXpOTDVReG1wNk9LVlk2RnovUQ==")

cases := []struct {
name string
creds *credentials.Credentials
Expand All @@ -598,23 +602,34 @@ func TestGetWithSTS(t *testing.T) {
wantErr error
}{
{
"Non-zero time",
// Example non-real credentials
func() *credentials.Credentials {
decodedAkid, _ := base64.StdEncoding.DecodeString("QVNJQVIyVEc0NFY2QVMzWlpFN0M=")
decodedSk, _ := base64.StdEncoding.DecodeString("NEtENWNudEdjVm1MV1JkRjV3dk5SdXpOTDVReG1wNk9LVlk2RnovUQ==")
return credentials.NewStaticCredentials(
string(decodedAkid),
string(decodedSk),
"",
)
}(),
time.Unix(1682640000, 0),
Token{
name: "Non-zero time",
creds: credentials.NewStaticCredentials(
string(decodedAkid),
string(decodedSk),
"",
),
nowTime: time.Unix(1682640000, 0),
want: Token{
Token: "k8s-aws-v1.aHR0cHM6Ly9zdHMudXMtd2VzdC0yLmFtYXpvbmF3cy5jb20vP0FjdGlvbj1HZXRDYWxsZXJJZGVudGl0eSZWZXJzaW9uPTIwMTEtMDYtMTUmWC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BU0lBUjJURzQ0VjZBUzNaWkU3QyUyRjIwMjMwNDI4JTJGdXMtd2VzdC0yJTJGc3RzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyMzA0MjhUMDAwMDAwWiZYLUFtei1FeHBpcmVzPTYwJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCUzQngtazhzLWF3cy1pZCZYLUFtei1TaWduYXR1cmU9ZTIxMWRiYTc3YWJhOWRjNDRiMGI2YmUzOGI4ZWFhZDA5MjU5OWM1MTU3ZjYzMTQ0NDRjNWI5ZDg1NzQ3ZjVjZQ",
Expiration: time.Unix(1682640000, 0).Local().Add(time.Minute * 14),
},
nil,
wantErr: nil,
},
{
name: "Signing creds expire before token",
creds: credentials.NewCredentials(&fakeCredentialProvider{
value: credentials.Value{
AccessKeyID: string(decodedAkid),
SecretAccessKey: string(decodedSk),
},
expiresAt: time.Unix(1682640000, 0).Local().Add(time.Minute * 10),
}),
nowTime: time.Unix(1682640000, 0),
want: Token{
Token: "k8s-aws-v1.aHR0cHM6Ly9zdHMudXMtd2VzdC0yLmFtYXpvbmF3cy5jb20vP0FjdGlvbj1HZXRDYWxsZXJJZGVudGl0eSZWZXJzaW9uPTIwMTEtMDYtMTUmWC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BU0lBUjJURzQ0VjZBUzNaWkU3QyUyRjIwMjMwNDI4JTJGdXMtd2VzdC0yJTJGc3RzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyMzA0MjhUMDAwMDAwWiZYLUFtei1FeHBpcmVzPTYwJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCUzQngtazhzLWF3cy1pZCZYLUFtei1TaWduYXR1cmU9ZTIxMWRiYTc3YWJhOWRjNDRiMGI2YmUzOGI4ZWFhZDA5MjU5OWM1MTU3ZjYzMTQ0NDRjNWI5ZDg1NzQ3ZjVjZQ",
Expiration: time.Unix(1682640000, 0).Local().Add(time.Minute * 9),
},
wantErr: nil,
},
}

Expand Down Expand Up @@ -647,6 +662,25 @@ func TestGetWithSTS(t *testing.T) {
}
}

type fakeCredentialProvider struct {
value credentials.Value
expiresAt time.Time
}

func (f *fakeCredentialProvider) Retrieve() (credentials.Value, error) {
return f.value, nil
}

func (f *fakeCredentialProvider) IsExpired() bool {
return false
}

var _ credentials.Expirer = (*fakeCredentialProvider)(nil)

func (f *fakeCredentialProvider) ExpiresAt() time.Time {
return f.expiresAt
}

func TestGetStsRegion(t *testing.T) {
tests := []struct {
host string
Expand Down
Loading