...

Source file src/github.com/awslabs/amazon-ecr-credential-helper/ecr-login/cache/credentials.go

Documentation: github.com/awslabs/amazon-ecr-credential-helper/ecr-login/cache

     1  // Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"). You may
     4  // not use this file except in compliance with the License. A copy of the
     5  // License is located at
     6  //
     7  //	http://aws.amazon.com/apache2.0/
     8  //
     9  // or in the "license" file accompanying this file. This file is distributed
    10  // on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
    11  // express or implied. See the License for the specific language governing
    12  // permissions and limitations under the License.
    13  
    14  package cache
    15  
    16  import (
    17  	"time"
    18  )
    19  
    20  type CredentialsCache interface {
    21  	Get(registry string) *AuthEntry
    22  	GetPublic() *AuthEntry
    23  	Set(registry string, entry *AuthEntry)
    24  	List() []*AuthEntry
    25  	Clear()
    26  }
    27  
    28  type Service string
    29  
    30  const (
    31  	ServiceECR       Service = "ecr"
    32  	ServiceECRPublic Service = "ecr-public"
    33  )
    34  
    35  type AuthEntry struct {
    36  	AuthorizationToken string
    37  	RequestedAt        time.Time
    38  	ExpiresAt          time.Time
    39  	ProxyEndpoint      string
    40  	Service            Service
    41  }
    42  
    43  // IsValid checks if AuthEntry is still valid at testTime. AuthEntries expire at 1/2 of their original
    44  // requested window.
    45  func (authEntry *AuthEntry) IsValid(testTime time.Time) bool {
    46  	validWindow := authEntry.ExpiresAt.Sub(authEntry.RequestedAt)
    47  	refreshTime := authEntry.ExpiresAt.Add(-1 * validWindow / time.Duration(2))
    48  	return testTime.Before(refreshTime)
    49  }
    50  

View as plain text