util

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2020 License: Apache-2.0 Imports: 39 Imported by: 0

Documentation

Index

Constants

View Source
const APITokenProviderName = "APITokenProvider"
View Source
const (
	EmptyDuration time.Duration = time.Duration(0)
)

Variables

This section is empty.

Functions

func NewAPIClient

func NewAPIClient(input *NewAPIClientInput) *operations.Client

func NewAPITokenProvider

func NewAPITokenProvider(token *string) credentials.Provider

func NewAWSSession

func NewAWSSession(token *string) (*session.Session, error)

func ParseOptions added in v0.4.0

func ParseOptions(s *string) ([]string, error)

ParseOptions parses the given options into an array of strings. It provides for any whitespace between the options.

Types

type APITokenProvider

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

APITokenProvider is a custom AWS Credentials provider which uses a base64 encoded token containing a STS credentials as JSON See https://docs.aws.amazon.com/sdk-for-go/api/aws/credentials/#hdr-Custom_Provider

Using a custom Provider does a few things for us: - Allows chaining credentials, so we can fall back to env vars, creds fil - The `Retrieve` method is cached by the client, so we don't need to re-parse our API token at every call - Provides a mechanism for handling expired creds

func (*APITokenProvider) IsExpired

func (t *APITokenProvider) IsExpired() bool

func (*APITokenProvider) Retrieve

func (t *APITokenProvider) Retrieve() (credentials.Value, error)

type APITokenValue

type APITokenValue struct {
	AccessKeyID     string `json:"accessKeyId"`
	SecretAccessKey string `json:"secretAccessKey"`
	SessionToken    string `json:"sessionToken"`
	Expiration      int64  `json:"expireTime"`
}

type APIer

type APIer interface {
	DeleteAccountsID(*operations.DeleteAccountsIDParams, runtime.ClientAuthInfoWriter) (*operations.DeleteAccountsIDNoContent, error)
	DeleteLeases(*operations.DeleteLeasesParams, runtime.ClientAuthInfoWriter) (*operations.DeleteLeasesOK, error)
	DeleteLeasesID(*operations.DeleteLeasesIDParams, runtime.ClientAuthInfoWriter) (*operations.DeleteLeasesIDOK, error)
	GetAccounts(*operations.GetAccountsParams, runtime.ClientAuthInfoWriter) (*operations.GetAccountsOK, error)
	GetAccountsID(*operations.GetAccountsIDParams, runtime.ClientAuthInfoWriter) (*operations.GetAccountsIDOK, error)
	GetAuth(*operations.GetAuthParams) (*operations.GetAuthOK, error)
	GetAuthFile(*operations.GetAuthFileParams) (*operations.GetAuthFileOK, error)
	GetLeases(*operations.GetLeasesParams, runtime.ClientAuthInfoWriter) (*operations.GetLeasesOK, error)
	GetLeasesID(*operations.GetLeasesIDParams, runtime.ClientAuthInfoWriter) (*operations.GetLeasesIDOK, error)
	GetUsage(*operations.GetUsageParams, runtime.ClientAuthInfoWriter) (*operations.GetUsageOK, error)
	PostAccounts(*operations.PostAccountsParams, runtime.ClientAuthInfoWriter) (*operations.PostAccountsCreated, error)
	PostLeases(*operations.PostLeasesParams, runtime.ClientAuthInfoWriter) (*operations.PostLeasesCreated, error)
	PostLeasesAuth(*operations.PostLeasesAuthParams, runtime.ClientAuthInfoWriter) (*operations.PostLeasesAuthCreated, error)
	PostLeasesIDAuth(*operations.PostLeasesIDAuthParams, runtime.ClientAuthInfoWriter) (*operations.PostLeasesIDAuthCreated, error)
	PutAccountsID(*operations.PutAccountsIDParams, runtime.ClientAuthInfoWriter) (*operations.PutAccountsIDOK, error)
	SetTransport(runtime.ClientTransport)
}

APIer is an interface generated for "github.com/Optum/dce-cli/client/operations.Client".

type AWSUtil

type AWSUtil struct {
	Config      *configs.Root
	Observation *observ.ObservationContainer
	Session     *awsSession.Session
}

func (*AWSUtil) ConfigureAWSCLICredentials

func (u *AWSUtil) ConfigureAWSCLICredentials(accessKeyID, secretAccessKey, sessionToken, profile string)

ConfigureAWSCLICredentials sets credential values in the credentials config file used by the aws cli Care should be taken to mitigate CWE-78 (https://cwe.mitre.org/data/definitions/78.html) by ensuring inputs come from a trusted source.

func (*AWSUtil) UpdateLambdasFromS3Assets

func (u *AWSUtil) UpdateLambdasFromS3Assets(lambdaNames []string, bucket string, namespace string)

func (*AWSUtil) UploadDirectoryToS3

func (u *AWSUtil) UploadDirectoryToS3(localPath string, bucket string, prefix string) ([]string, []string)

UploadDirectoryToS3 uploads the contents of a directory to S3 Care should be taken when using this function to mitigate CWE-22 (https://cwe.mitre.org/data/definitions/22.html) i.e. ensure `localPath` comes from a trusted source.

type AWSer

type AWSer interface {
	UploadDirectoryToS3(localPath string, bucket string, prefix string) ([]string, []string)
	UpdateLambdasFromS3Assets(lambdaNames []string, bucket string, namespace string)
	ConfigureAWSCLICredentials(accessKeyID, secretAccessKey, sessionToken, profile string)
}

type DurationUtil

type DurationUtil struct {
	DayFormatExp      *regexp.Regexp
	TimeUnitFormatExp *regexp.Regexp
}

DurationUtil has the

func NewDurationUtil

func NewDurationUtil() *DurationUtil

NewDurationUtil creates a new `DuractionUtil`

func (*DurationUtil) ExpandEpochTime

func (d *DurationUtil) ExpandEpochTime(str string) (int64, error)

ExpandEpochTime "expands" the given time from a string. If it is an int64, it assumes the time is a UNIX epoch time and is "absolute" and so refers the time. If it is a string, it assumes the time is "relative" to now and returns the UNIX epoch time with the duration added.

func (*DurationUtil) ParseDuration

func (d *DurationUtil) ParseDuration(str string) (time.Duration, error)

ParseDuration accepts a string to parse and return a `time.Duration`. This is used because the default time.Duration in go only supports up to the hour, and for lease expirations we want to support days,

type Durationer

type Durationer interface {
	ExpandEpochTime(str string) (int64, error)
	ParseDuration(str string) (time.Duration, error)
}

Durationer is an interface for exanding strings into times.

type FileSystemUtil

type FileSystemUtil struct {
	Config     *configs.Root
	ConfigFile string
	ConfigDir  string
}

func (*FileSystemUtil) ChToConfigDir added in v0.4.0

func (u *FileSystemUtil) ChToConfigDir() (string, string)

func (*FileSystemUtil) ChToTmpDir added in v0.4.0

func (u *FileSystemUtil) ChToTmpDir() (string, string)

func (*FileSystemUtil) Chdir

func (u *FileSystemUtil) Chdir(path string)

func (*FileSystemUtil) CreateConfigDirTree added in v0.4.0

func (u *FileSystemUtil) CreateConfigDirTree(dceVersion string) error

CreateConfigDirTree creates all the dirs in the dir specified by GetConfigDir(), including the dir itself.

func (*FileSystemUtil) GetArtifactsDir added in v0.4.0

func (u *FileSystemUtil) GetArtifactsDir(dceVersion string) string

GetArtifactsDir returns the cached artifacts dir, which by default is `~/.dce/.cache/dce/${DCE_VERSION}/`

func (*FileSystemUtil) GetCacheDir added in v0.4.0

func (u *FileSystemUtil) GetCacheDir() string

func (*FileSystemUtil) GetConfigDir added in v0.4.0

func (u *FileSystemUtil) GetConfigDir() string

func (*FileSystemUtil) GetConfigFile

func (u *FileSystemUtil) GetConfigFile() string

func (*FileSystemUtil) GetHomeDir

func (u *FileSystemUtil) GetHomeDir() string

func (*FileSystemUtil) GetLocalMainTFFile added in v0.4.0

func (u *FileSystemUtil) GetLocalMainTFFile() string

GetLocalBackendFile returns the full path of the local backend file.

func (*FileSystemUtil) GetLocalTFModuleDir added in v0.4.0

func (u *FileSystemUtil) GetLocalTFModuleDir() string

GetLocalBackendDir returns the dir for the local terraform backend. By default, `~/.dce/.cache/module`

func (*FileSystemUtil) GetLogFile added in v0.4.0

func (u *FileSystemUtil) GetLogFile() string

GetLogFile returns the full path of the log file for the deployment messages.

func (*FileSystemUtil) GetTerraformBin added in v0.4.0

func (u *FileSystemUtil) GetTerraformBin() string

GetTerraformBin returns the full path of the terraform binary.

func (*FileSystemUtil) GetTerraformBinDir added in v0.4.0

func (u *FileSystemUtil) GetTerraformBinDir() string

GetTerraformBinDir returns the dir in which the `terraform` bin is installed, which by default is `~/.dce/.cache/terraform/${TERRAFORM_VERSION}`

func (*FileSystemUtil) GetTerraformStateFile added in v0.4.0

func (u *FileSystemUtil) GetTerraformStateFile() string

GetTerraformStateFile returns the full path of the terraform state file

func (*FileSystemUtil) IsExistingFile

func (u *FileSystemUtil) IsExistingFile(path string) bool

func (*FileSystemUtil) OpenFileWriter added in v0.4.0

func (u *FileSystemUtil) OpenFileWriter(path string) (*os.File, error)

OpenFileWriter opens or creates a file in write-only mode. Data is appended to the file when writing. The file permissions are set to 0644, i.e. user-executable and user/group/other-readable.

func (*FileSystemUtil) ReadDir

func (u *FileSystemUtil) ReadDir(path string) []os.FileInfo

func (*FileSystemUtil) ReadFromFile

func (u *FileSystemUtil) ReadFromFile(path string) string

ReadFromFile returns the contents of a file as a string Care should be taken when using this function to prevent CWE-22 (https://cwe.mitre.org/data/definitions/22.html) i.e. ensure `path` comes from a trusted source.

func (*FileSystemUtil) ReadInConfig

func (u *FileSystemUtil) ReadInConfig() error

ReadInConfig loads the configuration from the configuration file and unmarshals it into the config object

func (*FileSystemUtil) RemoveAll

func (u *FileSystemUtil) RemoveAll(path string)

func (*FileSystemUtil) Unarchive

func (u *FileSystemUtil) Unarchive(source string, destination string) error

func (*FileSystemUtil) WriteConfig

func (u *FileSystemUtil) WriteConfig() error

WriteConfig writes the Config objects as YAML to the config file location

func (*FileSystemUtil) WriteFile

func (u *FileSystemUtil) WriteFile(fileName string, data string)

type FileSystemer

type FileSystemer interface {
	WriteConfig() error
	IsExistingFile(path string) bool
	ReadFromFile(path string) string
	ReadInConfig() error
	Unarchive(source string, destination string) error
	ChToConfigDir() (string, string)
	ChToTmpDir() (string, string)
	RemoveAll(path string)
	Chdir(path string)
	ReadDir(path string) []os.FileInfo
	WriteFile(fileName string, data string)
	OpenFileWriter(path string) (*os.File, error)

	// GetHomeDir returns the user home dir. For example, on *nix systems this
	// be the same as `~` expanded, or the value of `$HOME`
	GetHomeDir() string
	// GetConfigDir returns the DCE configuration dir, which on *nix systems
	// is `~/.dce`
	GetConfigDir() string
	// GetCacheDir returns the local cache dir, which bt default is `~/.dce/.cache`
	GetCacheDir() string
	// GetArtifactsDir returns the cached artifacts dir, which by default is
	// `~/.dce/.cache/dce/${DCE_VERSION}/`
	GetArtifactsDir(dceVersion string) string
	// GetTerraformBinDir returns the dir in which the `terraform` bin is installed,
	// which by default is `~/.dce/.cache/terraform/${TERRAFORM_VERSION}`
	GetTerraformBinDir() string
	// GetLocalBackendDir returns the dir for the local terraform backend.
	// By default, `~/.dce/.cache/module`
	GetLocalTFModuleDir() string
	// CreateConfigDirTree creates all the dirs in the dir specified by GetConfigDir(),
	// including the dir itself.
	CreateConfigDirTree(dceVersion string) error

	// GetConfigFile returns the full path of the configuration file, such as
	// `~/.dce/config.yaml`
	GetConfigFile() string
	// GetLogFile returns the full path of the log file for the deployment messages.
	GetLogFile() string
	// GetLocalBackendFile returns the full path of the local backend file.
	GetLocalMainTFFile() string
	// GetTerraformBin returns the full path of the terraform binary.
	GetTerraformBin() string
	// GetTerraformStateFile returns the full path of the terraform state file
	GetTerraformStateFile() string
}

type GithubUtil

type GithubUtil struct {
	Config      *configs.Root
	Observation *observ.ObservationContainer
}

func (*GithubUtil) DownloadGithubReleaseAsset

func (u *GithubUtil) DownloadGithubReleaseAsset(assetName string, releaseName string) error

type Githuber

type Githuber interface {
	DownloadGithubReleaseAsset(assetName string, dceVersion string) error
}

type MainTFTemplate added in v0.4.0

type MainTFTemplate struct {
	TFVars               []TFVar
	LocalBackend         bool
	LocalTFStateFilePath string
	TFWorkspaceDir       string
	// DCE terraform module source location
	ModuleSource string
}

MainTFTemplate is the template for writing the main.tf file

func NewMainTFTemplate added in v0.4.0

func NewMainTFTemplate(fs FileSystemer) *MainTFTemplate

NewMainTFTemplate creates a new instance of the MainTFTemplate

func (*MainTFTemplate) AddVariable added in v0.4.0

func (t *MainTFTemplate) AddVariable(name string, vartype string, val string) error

AddVariable adds a variable with the given `name`, variable type (`vartype`), and default value (`vardefault`) to the template

func (*MainTFTemplate) SetModuleSource added in v0.5.0

func (t *MainTFTemplate) SetModuleSource(source string)

func (*MainTFTemplate) Write added in v0.4.0

func (t *MainTFTemplate) Write(w io.Writer) error

Write writes the template to the given writer

type NewAPIClientInput

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

type PromptUtil

type PromptUtil struct {
	Config      *configs.Root
	Observation *observ.ObservationContainer
}

func (*PromptUtil) PromptBasic

func (u *PromptUtil) PromptBasic(label string, validator func(input string) error) *string

func (*PromptUtil) PromptSelect

func (u *PromptUtil) PromptSelect(label string, items []string) *string

type Prompter

type Prompter interface {
	PromptBasic(label string, validator func(input string) error) *string
	PromptSelect(label string, items []string) *string
}

type Sig4RoundTripper

type Sig4RoundTripper struct {
	Proxied http.RoundTripper
	Creds   *credentials.Credentials
	Region  string
	Logger  observation.Logger
}

Adapted from https://stackoverflow.com/questions/39527847/is-there-middleware-for-go-http-client

func (Sig4RoundTripper) RoundTrip

func (srt Sig4RoundTripper) RoundTrip(req *http.Request) (res *http.Response, e error)

type TFTemplater added in v0.4.0

type TFTemplater interface {
	// Add a terraform variable to the template
	AddVariable(name string, vartype string, vardefault string) error
	// Set the source path to the DCE terraform module
	SetModuleSource(source string)
	Write(w io.Writer) error
}

TFTemplater is an interface for the templater that generates the main.tf file.

type TFVar added in v0.4.0

type TFVar struct {
	Name  string
	Type  string
	Value string
}

TFVar represents a variable that is in the

type TerraformBinDownloader added in v0.4.0

type TerraformBinDownloader interface {
	Download(url string, localpath string) error
}

TerraformBinDownloader - interface for the downloader to download the Terraform binary from a URL ans save it locally.

type TerraformBinFileSystemUtil added in v0.4.0

type TerraformBinFileSystemUtil interface {
	GetConfigDir() string
	IsExistingFile(path string) bool
	OpenFileWriter(path string) (*os.File, error)
	Unarchive(source string, destination string) error
	GetTerraformBin() string
	RemoveAll(path string)
	GetTerraformBinDir() string
	GetLocalTFModuleDir() string
}

TerraformBinFileSystemUtil - interface for interacting with the file system.

type TerraformBinUtil added in v0.4.0

type TerraformBinUtil struct {
	Config      *configs.Root
	Observation *observ.ObservationContainer
	FileSystem  TerraformBinFileSystemUtil
	Downloader  TerraformBinDownloader
}

TerraformBinUtil uses the Teraform binary to peform the init and apply

func (*TerraformBinUtil) Apply added in v0.4.0

func (t *TerraformBinUtil) Apply(ctx context.Context, args []string) error

Apply will call `terraform apply` with the given vars.

func (*TerraformBinUtil) GetOutput added in v0.4.0

func (t *TerraformBinUtil) GetOutput(ctx context.Context, key string) (string, error)

GetOutput returns the value of the output with the given name.

func (*TerraformBinUtil) Init added in v0.4.0

func (t *TerraformBinUtil) Init(ctx context.Context, args []string) error

Init will download the Terraform binary, put it into the .dce folder, and then call init.

type Terraformer

type Terraformer interface {
	Init(ctx context.Context, args []string) error
	Apply(ctx context.Context, args []string) error
	GetOutput(ctx context.Context, key string) (string, error)
}

type UtilContainer

type UtilContainer struct {
	Config *configs.Root
	// File path location of the configuration file, from which this config was parsed
	// Useful if we want to reload or modify the file later
	ConfigFile  string
	Observation *observ.ObservationContainer
	AWSSession  *session.Session
	AWSer
	APIer
	Terraformer
	Githuber
	Prompter
	FileSystemer
	Weber
	Durationer
	TFTemplater
}

func New

func New(config *configs.Root, configFile string, observation *observ.ObservationContainer) *UtilContainer

New returns a new Util given config

type WebUtil

type WebUtil struct {
	Observation *observ.ObservationContainer
}

func (*WebUtil) Download added in v0.4.0

func (w *WebUtil) Download(url string, localpath string) error

Download will download the file at the given `url` and save it to the `localpath` Care should be taken to mitigate CWE-88 (https://cwe.mitre.org/data/definitions/88.html) by ensuring inputs comes from a trusted source.

func (*WebUtil) OpenURL

func (w *WebUtil) OpenURL(url string)

type Weber

type Weber interface {
	OpenURL(url string)
}

Jump to

Keyboard shortcuts

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