credentials

package
v0.0.0-...-a493ebb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2019 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package credentials supplies credentials to authenticate requests to the Akamai API. Example of using the environment variable credentials.

creds := credentials.NewEnvCredentials()

// Retrieve the credentials value
credValue, err := creds.Get()
if err != nil {
    // handle error
}

Package credentials supplies credentials to authenticate requests to the Akamai API. Example of using the shared credentials provider to read from ~/.edgerc

creds := credentials.NewSharedCredentials()

// Retrieve the credentials value
credValue, err := creds.Get()
if err != nil {
    // handle error
}

Index

Constants

View Source
const EnvProviderName = "EnvProvider"

EnvProviderName provides a name for the Environment provider.

View Source
const SharedCredsProviderName = "SharedCredentialsProvider"

SharedCredsProviderName provides a name of SharedCreds provider

View Source
const StaticProviderName = "StaticProvider"

StaticProviderName provides a name of Static provider

Variables

View Source
var (
	// ErrClientSecretNotFoundEnv is emitted when AKAMAI_CLIENT_SECRET is not found in the env.
	ErrClientSecretNotFoundEnv = errors.New("AKAMAI_CLIENT_SECRET not found in environment")
	// ErrClientTokenNotFoundEnv is emitted when AKAMAI_CLIENT_TOKEN is not found in the env.
	ErrClientTokenNotFoundEnv = errors.New("AKAMAI_CLIENT_TOKEN not found in environment")
	// ErrAccessTokenNotFoundEnv is emitted when AKAMAI_ACCESS_TOKEN is not found in the env.
	ErrAccessTokenNotFoundEnv = errors.New("AKAMAI_ACCESS_TOKEN not found in environment")
	// ErrAkamaiHostNotFoundEnv is emitted when AKAMAI_HOST is not found in the env.
	ErrAkamaiHostNotFoundEnv = errors.New("AKAMAI_HOST not found in environment")
)
View Source
var (
	// ErrSharedCredentialsNotFoundFile is emitted when the .edgerc file is not found
	ErrSharedCredentialsNotFoundFile = errors.New(".edgerc file not found")
	// ErrSharedCredentialsProfileNotFound is emitted when the profile could not be loaded from .edgerc
	ErrSharedCredentialsProfileNotFound = errors.New("could not load profile from .edgerc")
	// ErrClientSecretNotFoundFile is emitted when client_secret is not found in the .edgerc
	ErrClientSecretNotFoundFile = errors.New("client_secret not found in .edgerc")
	// ErrClientTokenNotFoundFile is emitted when client_token is not found in the .edgerc
	ErrClientTokenNotFoundFile = errors.New("client_token not found in .edgerc")
	// ErrAccessTokenNotFoundFile is emitted when access_token is not found in the .edgerc
	ErrAccessTokenNotFoundFile = errors.New("access_token not found in .edgerc")
	// ErrAkamaiHostNotFoundFile is emitted when host is not found in the .edgerc
	ErrAkamaiHostNotFoundFile = errors.New("host not found in .edgerc")
)
View Source
var (
	// ErrStaticCredentialsEmpty is emitted when static credentials are empty.
	ErrStaticCredentialsEmpty = errors.New("EmptyStaticCreds: static credentials are empty")
)

Functions

This section is empty.

Types

type AuthValue

type AuthValue struct {
	// Akamai client_secret
	ClientSecret string

	// Akamai client_token
	ClientToken string

	// Akamai access_token
	AccessToken string

	// Akamai host
	Host string

	// Provider used to get credentials
	ProviderName string
}

AuthValue is a struct holding the Akamai credentials for each field

type Credentials

type Credentials struct {
	// contains filtered or unexported fields
}

Credentials provides concurrency safe retrieval of Akamai credentials.

func NewCredentials

func NewCredentials(provider Provider) *Credentials

NewCredentials returns a pointer to a new Credentials with the provider set.

func NewEnvCredentials

func NewEnvCredentials() *Credentials

NewEnvCredentials returns a pointer to a new Credentials object wrapping the environment variable provider.

func NewSharedCredentials

func NewSharedCredentials(filename, profile string) *Credentials

NewSharedCredentials returns a pointer to a new Credentials object wrapping the Profile file provider.

func NewStaticCredentials

func NewStaticCredentials(cs, ct, at, ah string) *Credentials

NewStaticCredentials returns a pointer to a new Credentials object wrapping a static credentials value provider.

func NewStaticCredentialsFromCreds

func NewStaticCredentialsFromCreds(creds AuthValue) *Credentials

NewStaticCredentialsFromCreds returns a pointer to a new Credentials object wrapping the static credentials value provide. Same as NewStaticCredentials but takes the creds AuthValue instead of individual fields

func (*Credentials) Expire

func (c *Credentials) Expire()

Expire expires the credentials and forces them to be retrieved on the next call to Get().

This will override the Provider's expired state, and force Credentials to call the Provider's Retrieve().

func (*Credentials) ExpiresAt

func (c *Credentials) ExpiresAt() (time.Time, error)

ExpiresAt provides access to the functionality of the Expirer interface of the underlying Provider, if it supports that interface. Otherwise, it returns an error.

func (*Credentials) Get

func (c *Credentials) Get() (AuthValue, error)

Get returns the credentials AuthValue or error in the case of failure.

Will return the cached credentials Value if it has not expired. If the credentials Value has expired the Provider's Retrieve() will be called to refresh the credentials.

func (*Credentials) IsExpired

func (c *Credentials) IsExpired() bool

IsExpired returns if the credentials are no longer valid, and need to be retrieved.

If the Credentials were forced to be expired with Expire() this will reflect that override.

type EnvProvider

type EnvProvider struct {
	// contains filtered or unexported fields
}

An EnvProvider retrieves the credentials from the environment variables of the running process.

Environment variables used:

AKAMAI_ACCESS_TOKEN AKAMAI_CLIENT_SECRET AKAMAI_CLIENT_TOKEN AKAMAI_HOST

func (*EnvProvider) IsExpired

func (e *EnvProvider) IsExpired() bool

IsExpired returns if the credentials have been retrieved.

func (*EnvProvider) Retrieve

func (e *EnvProvider) Retrieve() (AuthValue, error)

Retrieve retrieves the keys from the environment.

type Expirer

type Expirer interface {
	// The time at which the credentials are no longer valid
	ExpiresAt() time.Time
}

An Expirer is an interface that Providers can implement to expose the expiration time, if known. If the Provider cannot accurately provide this info, it should not implement this interface.

type Expiry

type Expiry struct {

	// If set will be used by IsExpired to determine the current time.
	// Defaults to time.Now if CurrentTime is not set.  Available for testing
	// to be able to mock out the current time.
	CurrentTime func() time.Time
	// contains filtered or unexported fields
}

A Expiry provides shared expiration logic to be used by credentials providers to implement expiry functionality.

The best method to use this struct is as an anonymous field within the provider's struct.

Example:

type AkamaiProvider struct {
    Expiry
    ...
}

func (*Expiry) ExpiresAt

func (e *Expiry) ExpiresAt() time.Time

ExpiresAt returns the expiration time of the credential

func (*Expiry) IsExpired

func (e *Expiry) IsExpired() bool

IsExpired returns if the credentials are expired.

func (*Expiry) SetExpiration

func (e *Expiry) SetExpiration(expiration time.Time, window time.Duration)

SetExpiration sets the expiration IsExpired will check when called.

If window is greater than 0 the expiration time will be reduced by the window value.

Using a window is helpful to trigger credentials to expire sooner than the expiration time given to ensure no requests are made with expired tokens.

type Provider

type Provider interface {
	Retrieve() (AuthValue, error)

	// IsExpired returns if the credentials are no longer valid, and need
	// to be retrieved.
	IsExpired() bool
}

Provider is an interface for a component that will provide a CredentialValue This can be used to read from an environment or config file, or any other method that returns the authentication credentials as an AuthValue.

type SharedCredentialsProvider

type SharedCredentialsProvider struct {
	// Path to the shared credentials file.
	//
	// If empty will look for "AKAMAI_EDGERC_FILE" env variable. If the
	// env value is empty will default to current user's home directory.
	// Linux/OSX: "$HOME/.edgerc"
	Filename string

	// Edgerc file to extract credentials from. If empty
	// will default to environment variable "AKAMAI_EDGERC_PROFILE" or "default" if
	// environment variable is also not set.
	Profile string
	// contains filtered or unexported fields
}

SharedCredentialsProvider retrieves credentials from the current user's home directory, and keeps track if those credentials are expired.

Documentation on edgerc: https://developer.akamai.com/legacy/introduction/Conf_Client.html

Profile config file: $HOME/.edgerc

Variables: client_secret client_token access_token host

func (*SharedCredentialsProvider) IsExpired

func (p *SharedCredentialsProvider) IsExpired() bool

IsExpired returns if the shared credentials have expired.

func (*SharedCredentialsProvider) Retrieve

func (p *SharedCredentialsProvider) Retrieve() (AuthValue, error)

Retrieve reads and extracts the shared credentials from the current users home directory.

type StaticProvider

type StaticProvider struct {
	AuthValue
}

A StaticProvider is a set of credentials which are set programmatically, and will never expire.

func (*StaticProvider) IsExpired

func (s *StaticProvider) IsExpired() bool

IsExpired returns if the credentials are expired.

For StaticProvider, the credentials never expired.

func (*StaticProvider) Retrieve

func (s *StaticProvider) Retrieve() (AuthValue, error)

Retrieve returns the credentials or error if the credentials are invalid.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL