config

package
v1.28.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 20 Imported by: 18

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrConfigFileNotFound = errors.New("config file does not exist")

ErrConfigFileNotFound is returned if the config file (~/.cf/config by default) does not exist.

View Source
var Keys = []string{"oidc_issuer", "oidc_client_id", "api_url", "authz_url", "access_url", "oidc_client_secret"}

Keys are all of the allowed keys in the Context section.

Functions

func IsConfigured

func IsConfigured() bool

IsConfigured returns true if a Common Fate config file exists and contains a non-empty current context.

config.Load() is called to load the config file.

func ListContexts

func ListContexts() ([]string, error)

func Save

func Save(cfg *Config) error

func SwitchContext

func SwitchContext(contextName string) error

Types

type Config

type Config struct {
	CurrentContext string `toml:"current_context" json:"current_context"`
	// Contexts allows multiple Common Fate tenancies to be switched between easily.
	// We don't have official support for this yet in the CLI,
	// but the config structure supports it so that it can be easily added in future.
	Contexts map[string]Context `toml:"context" json:"context"`
}

func Default

func Default() *Config

Default returns an empty config.

func (Config) Current

func (c Config) Current() (*Context, error)

Current loads the current context as specified in the 'current_context' field in the config file. It returns an error if there are no contexts, or if the 'current_context' field doesn't match any contexts defined in the config file.

type Context

type Context struct {
	APIURL       string `toml:"api_url,omitempty" json:"api_url,omitempty"`
	AccessURL    string `toml:"access_url,omitempty" json:"access_url,omitempty"`
	AuthzURL     string `toml:"authz_url,omitempty" json:"authz_url,omitempty"`
	OIDCIssuer   string `toml:"oidc_issuer,omitempty" json:"oidc_issuer,omitempty"`
	OIDCClientID string `toml:"oidc_client_id,omitempty" json:"oidc_client_id,omitempty"`
	// OIDCClientSecret, if specified, will cause the client to use machine-to-machine OIDC authentication.
	OIDCClientSecret string `toml:"oidc_client_secret,omitempty" json:"oidc_client_secret,omitempty"`

	// HTTPClient is filled in by calling Initialize()
	HTTPClient *http.Client

	// OIDCProvider is filled in by calling Initialize()
	OIDCProvider rp.RelyingParty

	TokenSource oauth2.TokenSource

	TokenStore TokenStore
	// contains filtered or unexported fields
}

func LoadDefault

func LoadDefault(ctx context.Context) (*Context, error)

LoadDefault is a shorthand function which loads the default SDK configuration by calling New() with default values.

By default, config is loaded from environment variables, and then falling back to a TOML configuration file for any config values which are not set.

The file is ~/.cf/config by default, but this can be overridden with the CF_CONFIG_PATH environment variable.

Environment variables follow the pattern 'CF_CONFIG_VARIABLE_NAME', where CONFIG_VARIABLE_NAME is the name of the configuration value in upper snake-case. For example, 'CF_API_URL'.

func New

func New(ctx context.Context, opts Opts) (*Context, error)

New creates an initialised SDK context to be used for creating SDK clients. For example:

import "github.com/common-fate/sdk/config"
import "github.com/common-fate/sdk/service/access"

cfg, err := config.New(ctx, opts{})
if err != nil {
	return err
}
client := access.NewClient(cfg)

Configuration values, such as the API URL and OIDC Client ID to use, can be provided via the Opts{} argument.

The New() method will look configuration values up from environment variables and the config file (~/.cf/config by default) if they are not provided in Opts{}. By default, the order of priority is:

1. Static value, set in Opts{}

2. Environment variable ('env')

3. Config file ('file')

This behaviour can be customised by setting opts.ConfigSources or by providing the 'CF_CONFIG_SOURCES' environment variable, for example: CF_CONFIG_SOURCES=file,env

Environment variables follow the pattern 'CF_CONFIG_VARIABLE_NAME', where CONFIG_VARIABLE_NAME is the name of the configuration value in upper snake-case. For example, 'CF_API_URL'.

func NewServerContext

func NewServerContext(ctx context.Context, opts Opts) (*Context, error)

NewServerContext requires all option to be passed and does not attempt to read from the local config file it also uses an in memory token store to avoid keychain access

func (*Context) Initialize

func (c *Context) Initialize(ctx context.Context, opts InitializeOpts) error

type EnvSource added in v1.18.0

type EnvSource struct{}

func (EnvSource) Load added in v1.18.0

func (ev EnvSource) Load(key Key) (string, error)

Load config variables.

type FileSource added in v1.18.0

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

func (*FileSource) Load added in v1.18.0

func (s *FileSource) Load(key Key) (string, error)

Load config variables.

type InitializeOpts

type InitializeOpts struct {
	TokenStore TokenStore
}

type Key added in v1.18.0

type Key string
var (
	APIURLKey           Key = "api_url"
	AccessURLKey        Key = "access_url"
	AuthzURLKey         Key = "authz_url"
	OIDCIssuerKey       Key = "oidc_issuer"
	OIDCClientIDKey     Key = "oidc_client_id"
	OIDCClientSecretKey Key = "oidc_client_secret"
	NameKey             Key = "name"
)

type Opts

type Opts struct {
	APIURL string
	// AccessURL is the base URL of the Access Handler.
	// If empty, the API URL is used.
	AccessURL string
	// AccessURL is the base URL of the authz service.
	// If empty, the API URL is used.
	AuthzURL     string
	ClientID     string
	ClientSecret string
	OIDCIssuer   string

	// The token storage backend to use for OIDC tokens.
	// If not provided, will use the keychain backend.
	TokenStore TokenStore

	// the config sources to load config from.
	// Must be 'env', 'file', or may be a URL to fetch remote config from.
	// Defaults to ['env', 'file'] if not provided.
	//
	// Can be overridden by providing the 'CF_CONFIG_SOURCES' environment variable,
	// for example: CF_CONFIG_SOURCES=file,env,https://commonfate.example.com/config.json
	ConfigSources []string
}

type StaticSource added in v1.18.0

type StaticSource struct {
	Result string
}

func (StaticSource) Load added in v1.18.0

func (s StaticSource) Load(key Key) (string, error)

Load config variables. The function must not set config variables if they are already set.

type TokenStore

type TokenStore interface {
	Clear() error
	Save(token *oauth2.Token) error
	Token() (*oauth2.Token, error)
}

type URLConfigJSON added in v1.21.0

type URLConfigJSON struct {
	CliOAuthClientId string `json:"cliOAuthClientId"`
	OAauthIssuer     string `json:"oauthIssuer"`
	APIURL           string `json:"apiUrl"`
	AccessAPIURL     string `json:"accessApiUrl"`
}

type URLSource added in v1.21.0

type URLSource struct {
	DeploymentURL *url.URL
	// contains filtered or unexported fields
}

URLSource configures Common Fate from a deployment URL. The URL should be a config.json which contains discovery variables for a particular deployment.

func (*URLSource) Load added in v1.21.0

func (u *URLSource) Load(key Key) (string, error)

Load config variables.

Jump to

Keyboard shortcuts

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