configloader

package
v0.6.37 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 13 Imported by: 5

README

configloaded

Installation

go get github.com/effective-security/x/pkg/configloader

Usage

	f, err := NewFactory(nil, []string{"/cfg/override_location"}, "MYSERVICE_")
	if err != nil {
        return err
    }

	var c configuration
	err = f.Load(cfgFile, &c)
	if err != nil {
        return err
    }

Environment variables

The loader interpolates configuration values with the supported environment variables described below, and other OS Env variables that has prefix passed to NewFactory

  • ${HOSTNAME} : host name
  • ${NODENAME} : node name if the cluster
  • ${LOCALIP} : local IP address
  • ${USER} : user name under which the process is running
  • ${NORMALIZED_USER} : user name without dots
  • ${ENVIRONMENT} : environment name of the deployment, aka test,dev,prod etc
  • ${ENVIRONMENT_UPPERCASE} : environment name in upper case
  • any environment variable started with MYSERVICE_ prefix

Config override

The loader supports config overrides by host name, or with a provided file by WithOverride method.

If there is file named as the config file with .hostmap suffix, if will be loaded, and override values will be applied to the main config file, based on the host name.

The format of the .hostmap file:

override:
  HOSTNAME: override.yaml

See testdata folder for examples.

Documentation

Index

Constants

View Source
const (
	// FileSource specifies to load config from a file
	FileSource = "file://"
	// EnvSource specifies to load config from an environment variable
	EnvSource = "env://"
	// SecretSource specifies to load config from a secret manager
	SecretSource = "secret://"
)

Variables

This section is empty.

Functions

func ExpandAll

func ExpandAll(obj interface{}) error

ExpandAll replace variables in the input object, using default Expander. The input object must be a pointer to a struct. If secrets are used, SecretProviderInstance must be set. The values started with env:// , file:// or secret:// must be resolved. The values inside ${} will be tried to be resolved, if not found will be substiduted with empy values as per os.Getenv function.

func GetAbsFilename

func GetAbsFilename(file, projFolder string) (string, error)

GetAbsFilename returns absolute path for the file from the relative path to projFolder

func Marshal

func Marshal(fn string, value interface{}) error

Marshal saves object to file

func ResolveValue

func ResolveValue(val string) (string, error)

ResolveValue returns value loaded from file:// or env:// If val does not start with file:// or env://, then the value is returned as is

func ResolveValueWithSecrets

func ResolveValueWithSecrets(val string, loader SecretProvider) (string, error)

ResolveValue returns value loaded from file:// or env:// If val does not start with file:// or env://, then the value is returned as is

func Unmarshal

func Unmarshal(file string, v interface{}) error

Unmarshal JSON or YAML file to an interface

func UnmarshalAndExpand

func UnmarshalAndExpand(file string, v interface{}) error

UnmarshalAndExpand load JSON or YAML file to an interface and expands variables

Types

type Expander

type Expander struct {
	Variables      map[string]string
	SecretProvider SecretProvider
}

Expander is used to expand variables in the input object

func (*Expander) Expand

func (f *Expander) Expand(s string) (string, error)

Expand replace variables in the input string

func (*Expander) ExpandAll

func (f *Expander) ExpandAll(obj interface{}) error

ExpandAll replace variables in the input object

type Factory

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

Factory is used to create Configuration instance

func NewFactory

func NewFactory(nodeInfo netutil.NodeInfo, searchDirs []string, envPrefix string) (*Factory, error)

NewFactory returns new configuration factory

func (*Factory) Load

func (f *Factory) Load(configFile string, config interface{}) (absConfigFile string, err error)

Load will load the configuration from the named config file, apply any overrides, and resolve relative directory locations.

func (*Factory) LoadForHostName

func (f *Factory) LoadForHostName(configFile, hostnameOverride string, config interface{}) (absConfigFile string, err error)

LoadForHostName will load the configuration from the named config file for specified host name, apply any overrides, and resolve relative directory locations.

func (*Factory) ResolveConfigFile

func (f *Factory) ResolveConfigFile(configFile string) (absConfigFile, baseDir string, err error)

ResolveConfigFile returns absolute path for the config file

func (*Factory) WithEnvironment

func (f *Factory) WithEnvironment(environment string) *Factory

WithEnvironment allows to override environment in Configuration

func (*Factory) WithOverride

func (f *Factory) WithOverride(file string) *Factory

WithOverride allows to specify additional override config file

func (*Factory) WithSecretProvider

func (f *Factory) WithSecretProvider(p SecretProvider) *Factory

WithSecretProvider allows to specify secret provider

type Hostmap

type Hostmap struct {
	// Override is a map of host name to file location
	Override map[string]string
}

Hostmap provides overrides info

type SecretProvider

type SecretProvider interface {
	GetSecret(name string) (string, error)
}

SecretProvider is an interface to provide secrets

var SecretProviderInstance SecretProvider

SecretProviderInstance is a global instance of SecretLoader

Jump to

Keyboard shortcuts

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