config

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EnvWayfinderServer    = "WAYFINDER_SERVER"
	EnvWayfinderToken     = "WAYFINDER_TOKEN"
	EnvWayfinderWorkspace = "WAYFINDER_WORKSPACE"
)

Variables

View Source
var (
	// ErrNoProfileSelected indicates the current profile is empty
	ErrNoProfileSelected = errors.New("no profile selected")
	// ErrNoProfileEndpoint indicates the profile does not have a endpoint
	ErrNoProfileEndpoint = errors.New("profile does not have a server endpoint")
	// ErrNoProfileAuth indicates the profile does not any auth configured
	ErrNoProfileAuth = errors.New("profile does not have any authentication configured")
)
View Source
var (
	// DefaultWayfinderConfigPath is the default path for the wayfinder configuration file
	DefaultWayfinderConfigPath = path.Join(osutils.UserHomeDir(), ".wayfinder", "config")
	// DefaultWayfinderConfigPathEnv is the default name of the env variable for config
	DefaultWayfinderConfigPathEnv = "WAYFINDER_CONFIG"
)
View Source
var GetOrCreateClientConfiguration = func() (*Config, error) {
	path := GetClientConfigurationPath()
	common.LogWithoutContext().WithField("path", path).Debug("using wayfinder configration file")

	if found, err := fileExists(path); err != nil {
		return nil, err
	} else if !found {

		if err := UpdateConfig(NewEmpty(), path); err != nil {
			return nil, err
		}

		return NewEmpty(), nil
	}

	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}

	return New(file)
}

GetOrCreateClientConfiguration is responsible for retrieving the client configuration

View Source
var UpdateConfig = func(config *Config, path string) error {
	data, err := yaml.Marshal(config)
	if err != nil {
		return err
	}

	if err := os.MkdirAll(filepath.Dir(path), os.FileMode(0750)); err != nil {
		return err
	}

	return os.WriteFile(path, data, os.FileMode(0640))
}

UpdateConfig is responsible for writing the configuration to disk

Functions

func GetClientConfigurationPath

func GetClientConfigurationPath() string

GetClientConfigurationPath returns the path to the client config

func GetClientPath

func GetClientPath() string

GetClientPath returns the base of the client configuration

func IsAccessToken

func IsAccessToken(token []byte) bool

IsAccessToken checks if the token is an access token

func IsEphemeralConfig

func IsEphemeralConfig() bool

func IsExchangeToken

func IsExchangeToken(token []byte) bool

IsExchangeToken checks if the token is an exchange token

Types

type APIInfo

type APIInfo struct {
	// NonResourceAPI is the base path for the non-resource API (i.e. our non-CRD API endpoints such
	// as login)
	NonResourceAPI string `json:"nonResourceAPI,omitempty"`
	// ResourceAPI is the base path for the resource API (i.e. access to our CRDs)
	ResourceAPI string `json:"resourceAPI,omitempty"`
	// KubeProxyAPI is the base path for the kube proxy API (i.e. access to managed clusters)
	KubeProxyAPI string `json:"kubeProxyAPI,omitempty"`
}

APIInfo is a representation of the structure returned from the API server on the unauthenticated /apiinfo endpoint. This needs to be kept in sync with the APIInfo struct from /pkg/apiserver/types/types.go

type AuthInfo

type AuthInfo struct {
	// Identity is a wayfinder managed identity
	Identity *Identity `json:"identity,omitempty" yaml:"identity,omitempty"`
	// Token is a static token to use
	Token *string `json:"token,omitempty" yaml:"token,omitempty"`
}

AuthInfo defines a credential to the api endpoint

type Config

type Config struct {
	// AuthInfos is a collection of credentials
	AuthInfos map[string]*AuthInfo `json:"users,omitempty" yaml:"users,omitempty"`
	// CurrentProfile is the profile in use at the moment
	CurrentProfile string `json:"current-profile,omitempty" yaml:"current-profile,omitempty"`
	// Profiles is a collection of profiles
	Profiles map[string]*Profile `json:"profiles,omitempty" yaml:"profiles,omitempty"`
	// Servers is a collection of api endpoints
	Servers map[string]*Server `json:"servers,omitempty" yaml:"servers,omitempty"`
	// Version is the version of the configuration
	Version string `json:"version,omitempty" yaml:"version,omitempty"`
}

Config is the configuration for the api

func CreateEphemeralConfiguration

func CreateEphemeralConfiguration() *Config

CreateEphemeralConfiguration creates a fake configuration from the environments variables - largely used for CI

func GetConfig

func GetConfig() (*Config, error)

GetConfig returns either the ephemeral configuration from environment variables if provided, or the current configured file - creating it if it does not exist.

func New

func New(reader io.Reader) (*Config, error)

New creates a configuration

func NewEmpty

func NewEmpty() *Config

NewEmpty returns an empty configuration

func (*Config) AddAuthInfo

func (c *Config) AddAuthInfo(name string, auth *AuthInfo)

AddAuthInfo adds a authentication

func (*Config) AddProfile

func (c *Config) AddProfile(name string, ctx *Profile)

AddProfile adds a profile to the config

func (*Config) AddServer

func (c *Config) AddServer(name string, server *Server)

AddServer adds a server

func (*Config) CreateProfile

func (c *Config) CreateProfile(name, endpoint string)

CreateProfile is used to create a profile

func (*Config) GetAuthInfo

func (c *Config) GetAuthInfo(name string) *AuthInfo

GetAuthInfo returns the auth for a profile

func (*Config) GetProfile

func (c *Config) GetProfile(name string) *Profile

GetProfile returns the profile

func (*Config) GetProfileAuthMethod

func (c *Config) GetProfileAuthMethod(name string) string

GetProfileAuthMethod returns the method of authentication for a profile

func (*Config) GetServer

func (c *Config) GetServer(name string) *Server

GetServer returns the endpoint for the profile

func (*Config) HasAuth

func (c *Config) HasAuth(name string) bool

HasAuth checks if we have auth enabled

func (*Config) HasAuthInfo

func (c *Config) HasAuthInfo(name string) bool

HasAuthInfo checks if the context exists in the config

func (*Config) HasProfile

func (c *Config) HasProfile(name string) bool

HasProfile checks if the context exists in the config

func (*Config) HasServer

func (c *Config) HasServer(name string) bool

HasServer checks if the context exists in the config

func (*Config) HasValidProfile

func (c *Config) HasValidProfile(name string) error

HasValidProfile checks we have a current context

func (*Config) IsAccessToken

func (c *Config) IsAccessToken() bool

IsAccessToken returns true if the current profile is for an access token user

func (*Config) IsValid

func (c *Config) IsValid() error

IsValid checks if the configuration is valid

func (*Config) ListProfiles

func (c *Config) ListProfiles() []string

ListProfiles returns a list of profile names

func (*Config) NewProfileWithAuth

func (c *Config) NewProfileWithAuth(name, endpoint string, auth *AuthInfo) error

NewProfileWithAuth creates the profile

func (*Config) RemoveProfile

func (c *Config) RemoveProfile(name string)

RemoveProfile removes the profile

func (*Config) RemoveServer

func (c *Config) RemoveServer(name string)

RemoveServer removes a server instance

func (*Config) RemoveUserInfo

func (c *Config) RemoveUserInfo(name string)

RemoveUserInfo removes the user info

func (*Config) Update

func (c *Config) Update(w io.Writer) error

Update writes the config to the file

type Identity

type Identity struct {
	// RefreshToken represents a wayfinder managed refresh token issued by the wayfinder
	RefreshToken string `json:"refresh-token,omitempty" yaml:"refresh-token,omitempty"`
	// Token represents a wayfinder managed token issued by the wayfinder service
	Token string `json:"token,omitempty" yaml:"token,omitempty"`
}

Identity is a wayfinder manage identity

func (*Identity) IsAccessToken

func (k *Identity) IsAccessToken() bool

IsExchangeToken is used to check if the authentication is an access token

func (*Identity) IsExchangeToken

func (k *Identity) IsExchangeToken() bool

IsExchangeToken is used to check with the authentication is an exchange token

func (*Identity) IsExpired

func (k *Identity) IsExpired() (bool, error)

IsExpired checks if the access token is expired

type Profile

type Profile struct {
	// AuthInfo is the credentials to use
	AuthInfo string `json:"user,omitempty" yaml:"user,omitempty"`
	// Server is a reference to the server config
	Server string `json:"server,omitempty" yaml:"server,omitempty"`
	// Workspace is the default workspace for this profile
	Workspace string `json:"workspace,omitempty" yaml:"workspace,omitempty"`
}

Profile links endpoint and a credential together

type Server

type Server struct {
	// Endpoint the url for the api endpoint of wayfinder
	Endpoint string `json:"server,omitempty" yaml:"server,omitempty"`
	// CACertificate is the ca bundle used to verify a self-signed api
	CACertificate string `json:"caCertificate,omitempty" yaml:"caCertificate,omitempty"`
	// APIInfo is a set of metadata about this instance of Wayfinder
	APIInfo *APIInfo `json:"apiInfo,omitempty"`
}

Server defines an endpoint for the api server

func (*Server) GetAPIInfo

func (s *Server) GetAPIInfo() APIInfo

Jump to

Keyboard shortcuts

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