lwcomponent

package
v2.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2024 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Overview

A development kit for the cloud based of modular components.

A Lacework component package to help facilitate the loading and execution of components

Index

Constants

View Source
const (
	EmptyType Type = ""

	// the component is a binary
	BinaryType = "BINARY"

	// will this component be accessible via the CLI
	CommandType = "CLI_COMMAND"

	// the component is a library, only provides content for the CLI or other components
	LibraryType = "LIBRARY"

	// the component is standalone, should be available in $PATH
	StandaloneType = "STANDALONE"
)
View Source
const (
	DefaultMaxRetry = 3
)
View Source
const (
	DevelopmentEnv = "LW_CDK_DEV_COMPONENT"
)

Variables

View Source
var (
	ErrNonExecutable error  = errors.New("component not executable")
	ErrRun           string = "unable to run component"
)
View Source
var (
	VersionFile     = ".version"
	SignatureFile   = ".signature"
	InfoFile        = ".info"
	DevelopmentFile = ".dev"
)
View Source
var (
	ErrComponentNotFound = errors.New("component not found on disk")
)

Functions

func CatalogCacheDir

func CatalogCacheDir() (string, error)

Returns the local directory that Components will be stored in.

func CatalogV1Enabled

func CatalogV1Enabled(client *api.Client) bool

func Dir

func Dir() (string, error)

Dir returns the directory where the components will be stored

func DownloadFile

func DownloadFile(path string, url string) error

Retry 3 times (4 requests total) Resty default RetryWaitTime is 100ms Exponential backoff to a maximum of RetryWaitTime of 2s

func IsNotFound

func IsNotFound(err error) bool

IsNotFound returns a boolean indicating whether the error is known to have determined the component is not found. It is satisfied by ErrNotApplyComment

func LoadLocalComponents

func LoadLocalComponents() (components map[string]CDKComponent, err error)

Types

type ApiInfo

type ApiInfo struct {
	Id            int32             `json:"id"`
	Name          string            `json:"name"`
	Version       *semver.Version   `json:"version"`
	AllVersions   []*semver.Version `json:"allVersions"`
	Desc          string            `json:"desc"`
	SizeKB        int64             `json:"sizeKB"`
	Deprecated    bool              `json:"deprecated"`
	ComponentType Type              `json:"componentType"`
}

func NewAPIInfo

func NewAPIInfo(
	id int32,
	name string,
	version *semver.Version,
	allVersions []*semver.Version,
	desc string,
	size int64,
	deprecated bool,
	componentType Type,
) *ApiInfo

type Artifact

type Artifact struct {
	OS            string `json:"os"`
	ARCH          string `json:"arch"`
	URL           string `json:"url,omitempty"`
	Signature     string `json:"signature"`
	Version       string `json:"version"`
	UpdateMessage string `json:"updateMessage"`
}
type Breadcrumbs struct {
	InstallationMessage string `json:"installationMessage,omitempty"`
	UpdateMessage       string `json:"updateMessage,omitempty"`
}

Components should leave a trail/crumb after installation or update, these messages will be shown by the Lacework CLI

type CDKComponent

type CDKComponent struct {
	Name           string `json:"name"`
	Description    string `json:"description"`
	Type           Type   `json:"type"`
	Status         Status `json:"-"`
	InstallMessage string `json:"-"`
	UpdateMessage  string `json:"-"`

	Exec Executer `json:"-"`

	ApiInfo  *ApiInfo  `json:"apiInfo,omitempty"`
	HostInfo *HostInfo `json:"-"`
	// contains filtered or unexported fields
}

func NewCDKComponent

func NewCDKComponent(apiInfo *ApiInfo, hostInfo *HostInfo) CDKComponent

func (*CDKComponent) Dir

func (c *CDKComponent) Dir() (string, error)

func (*CDKComponent) EnterDevMode

func (c *CDKComponent) EnterDevMode() error

func (*CDKComponent) InstalledVersion

func (c *CDKComponent) InstalledVersion() *semver.Version

func (*CDKComponent) LatestVersion

func (c *CDKComponent) LatestVersion() *semver.Version

func (*CDKComponent) PrintSummary

func (c *CDKComponent) PrintSummary() []string

type Catalog

type Catalog struct {
	Components map[string]CDKComponent
	// contains filtered or unexported fields
}

func NewCachedCatalog

func NewCachedCatalog(
	client *api.Client,
	stageConstructor StageConstructor,
	cachedComponentsApiInfo map[string]*ApiInfo,
) (*Catalog, error)

func NewCatalog

func NewCatalog(
	client *api.Client,
	stageConstructor StageConstructor,
) (*Catalog, error)

func (*Catalog) ComponentCount

func (c *Catalog) ComponentCount() int

func (*Catalog) Delete

func (c *Catalog) Delete(component *CDKComponent) error

Delete a CDKComponent

Remove the Component install directory and all sub-directory. This function will not return an error if the Component is not installed.

func (*Catalog) GetComponent

func (c *Catalog) GetComponent(name string) (*CDKComponent, error)

Return a CDKComponent that is present on the host.

func (*Catalog) Install

func (c *Catalog) Install(component *CDKComponent) error

func (*Catalog) ListComponentVersions

func (c *Catalog) ListComponentVersions(component *CDKComponent) ([]*semver.Version, error)

func (*Catalog) PrintComponents

func (c *Catalog) PrintComponents() [][]string

func (*Catalog) Stage

func (c *Catalog) Stage(
	component *CDKComponent,
	version string,
	progressClosure func(filepath string, sizeB int64)) (stageClose func(), err error)

func (*Catalog) Verify

func (c *Catalog) Verify(component *CDKComponent) error

type Component

type Component struct {
	Name          string         `json:"name"`
	Description   string         `json:"description"`
	Type          Type           `json:"type"`
	LatestVersion semver.Version `json:"-"`
	Artifacts     []Artifact     `json:"artifacts"`
	Breadcrumbs   Breadcrumbs    `json:"breadcrumbs,omitempty"`

	// @dhazekamp command_name required when CLICommand is true?
	CommandName string `json:"command_name,omitempty"`
}

Component can be a command-line tool, a new command that extends the Lacework CLI, or a library that contains files used by another Lacework component.

func (Component) ArtifactForRunningHost

func (c Component) ArtifactForRunningHost(version string) (*Artifact, bool)

ArtifactForRunningHost returns the right component artifact for the running host,

func (Component) CurrentVersion

func (c Component) CurrentVersion() (*semver.Version, error)

CurrentVersion returns the current installed version of the component

func (Component) EnterDevelopmentMode

func (c Component) EnterDevelopmentMode() error

func (Component) IsCommandType

func (c Component) IsCommandType() bool

func (Component) IsExecutable

func (c Component) IsExecutable() bool

func (Component) IsInstalled

func (c Component) IsInstalled() bool

IsInstalled returns true if the component is installed on disk

TODO: @jon-stewart: remove - is in wrong place

func (Component) ListVersions

func (c Component) ListVersions(installed *semver.Version) string

func (Component) MakeUpdateMessage

func (c Component) MakeUpdateMessage(from, to semver.Version) string

func (Component) MarshalJSON

func (c Component) MarshalJSON() ([]byte, error)

func (Component) Path

func (c Component) Path() (string, error)

Path returns the path to the component ("RootPath()/{name}")

func (Component) RootPath

func (c Component) RootPath() (string, error)

RootPath returns the component's root path ("Dir()/{name}")

func (Component) RunAndOutput

func (c Component) RunAndOutput(args []string, envs ...string) error

RunAndOutput runs the command and outputs to os.Stdout and os.Stderr, the provided environment variables will be accessible by the component

func (Component) RunAndReturn

func (c Component) RunAndReturn(args []string, stdin io.Reader, envs ...string) (
	stdout string,
	stderr string,
	err error,
)

RunAndReturn runs the command and returns its standard output and standard error, the provided environment variables will be accessible by the component

func (Component) SignatureFromDisk

func (c Component) SignatureFromDisk() ([]byte, error)

SignatureFromDisk returns the component signature stored on disk ("RootPath()/.signature")

func (Component) Status

func (c Component) Status() Status

Status returns the component status

func (Component) UnderDevelopment

func (c Component) UnderDevelopment() bool

UnderDevelopment returns true if the component is under development that is, if the component root path has the '.dev' specs file or, if the environment variable 'LW_CDK_DEV_COMPONENT' matches the component name

func (*Component) UnmarshalJSON

func (c *Component) UnmarshalJSON(data []byte) error

func (Component) UpdateAvailable

func (c Component) UpdateAvailable() (bool, error)

UpdateAvailable returns true if there is a newer version of the component

func (Component) WriteSignature

func (c Component) WriteSignature(signature []byte) error

WriteSignature stores the component signature on disk

func (Component) WriteVersion

func (c Component) WriteVersion(installed string) error

WriteVersion stores the component version on disk

type DevInfo

type DevInfo struct {
	ComponentType Type
	Desc          string
	Name          string
	Version       string
}

type Executer

type Executer interface {
	Executable() bool

	Execute(args []string, envs ...string) (stdout string, stderr string, err error)

	ExecuteInline(args []string, envs ...string) (err error)

	Path() string
}

func NewExecuable

func NewExecuable(name string, dir string) Executer

type HostInfo

type HostInfo struct {
	Name          string `json:"name"`
	ComponentType Type   `json:"type"`
	Desc          string `json:"description"`
	Dir           string `json:"-"`
}

func LoadHostInfo

func LoadHostInfo(dir string) (*HostInfo, error)

func NewHostInfo

func NewHostInfo(dir string, desc string, componentType Type) (*HostInfo, error)

func (*HostInfo) Delete

func (h *HostInfo) Delete() error

func (*HostInfo) Development

func (h *HostInfo) Development() bool

func (*HostInfo) Signature

func (h *HostInfo) Signature() (sig []byte, err error)

func (*HostInfo) Validate

func (h *HostInfo) Validate() (err error)

func (*HostInfo) Version

func (h *HostInfo) Version() (version *semver.Version, err error)

type Library

type Library interface {

	// Install downloads the library and deploys the files and index
	Install() error

	// Index returns the index of files that the library contains
	Index() []string

	// GetFile returns the content of one file from the library
	GetFile(string) ([]byte, error)
}

A library component provides one or more files that other components use

type RunError

type RunError struct {
	ExitCode int
	Message  string
	Err      error
}

RunError is a struct used to pass an error when a component tries to run and it fails, a few functions will return this error so that callers (upstream packages) can unwrap and identify that the error comes from this package

func (*RunError) Error

func (e *RunError) Error() string

func (*RunError) Unwrap

func (e *RunError) Unwrap() error

type StageConstructor

type StageConstructor func(name, artifactUrl string, size int64) (stage Stager, err error)

type Stager

type Stager interface {
	Close()

	Commit(string) error

	Directory() string

	Download(progressClosure func(filepath string, sizeB int64)) error

	Filename() string

	Signature() (sig []byte, err error)

	Unpack() error

	Validate() error
}

func NewStageTarGz

func NewStageTarGz(name, artifactUrl string, size int64) (stage Stager, err error)

type State

type State struct {
	Version    string      `json:"version"`
	Components []Component `json:"components"`
}

State holds the components specification

You can load the state from the Lacework API server by passing an `api.Client`.

client, err := api.NewClient(account, opts...)
cState, err := lwcomponent.LoadState(client)

Or, you can load the state from the local storage.

cState, err := lwcomponent.LocalState()

func LoadState

func LoadState(client *api.Client) (*State, error)

LoadState loads the state from the Lacework API server

func LocalState

func LocalState() (*State, error)

LocalState loads the state from the local storage ("Dir()/state")

func (State) GetComponent

func (s State) GetComponent(name string) (*Component, bool)

GetComponent returns the pointer of a component, if the component is not found, this function will return a `nil` pointer and `false`

Usage:

component, found := state.GetComponent(name)

if !found {
	fmt.Println("Component %s not found", name)
}

func (State) Install

func (s State) Install(component *Component, version string, progressClosure func(path string, sizeB int64)) error

func (State) Verify

func (s State) Verify(component *Component, version string) error

func (State) WriteState

func (s State) WriteState() error

WriteState stores the components state to disk

type Status

type Status int
const (
	UnknownStatus Status = iota
	Development
	NotInstalled
	NotInstalledDeprecated
	Installed
	InstalledDeprecated
	UpdateAvailable
	Tainted
)

func (Status) Color

func (s Status) Color() *color.Color

func (Status) String

func (s Status) String() string

type Type

type Type string

Jump to

Keyboard shortcuts

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